dolphin/src/dolphiniconsview.cpp
Peter Penz 91d1438168 Respect global single/double click setting for the icons- and details view (thanks to Sam Abed for the patch!). PS: I did not introduce an executed()-signal in KListView, as:
* In general we want to avoid having Kxxx-view classes for existing Qxxx-view classes like this was the case in KDE3. Qt4 allows us to use custom models + delegates and hence there should be no need to introduce KListView, KTreeView or KColumnView.

* It looks like KListView will be renamed to KCategoryView and it's open yet whether it it will be available outside the scope of a file manager for KDE 4.0.

* The details view is derived from QTreeView and the columns view is derived from QColumnView -> adjusting KListView won't help here :-)

svn path=/trunk/KDE/kdebase/apps/; revision=659994
2007-05-01 09:12:24 +00:00

255 lines
8.9 KiB
C++

/***************************************************************************
* Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
#include "dolphiniconsview.h"
#include "dolphincontroller.h"
#include "dolphinsettings.h"
#include "dolphinitemcategorizer.h"
#include "dolphin_iconsmodesettings.h"
#include <kdirmodel.h>
#include <kfileitem.h>
#include <kfileitemdelegate.h>
#include <QAbstractProxyModel>
#include <QPoint>
DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
KListView(parent),
m_controller(controller)
{
Q_ASSERT(controller != 0);
setViewMode(QListView::IconMode);
setResizeMode(QListView::Adjust);
viewport()->setAttribute(Qt::WA_Hover);
if (KGlobalSettings::singleClick()) {
connect(this, SIGNAL(clicked(const QModelIndex&)),
controller, SLOT(triggerItem(const QModelIndex&)));
} else {
connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
controller, SLOT(triggerItem(const QModelIndex&)));
}
connect(this, SIGNAL(activated(const QModelIndex&)),
controller, SLOT(triggerItem(const QModelIndex&)));
connect(controller, SIGNAL(showPreviewChanged(bool)),
this, SLOT(updateGridSize(bool)));
connect(controller, SIGNAL(zoomIn()),
this, SLOT(zoomIn()));
connect(controller, SIGNAL(zoomOut()),
this, SLOT(zoomOut()));
// apply the icons mode settings to the widget
const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
Q_ASSERT(settings != 0);
m_viewOptions = KListView::viewOptions();
m_viewOptions.showDecorationSelected = true;
QFont font(settings->fontFamily(), settings->fontSize());
font.setItalic(settings->italicFont());
font.setBold(settings->boldFont());
m_viewOptions.font = font;
updateGridSize(controller->showPreview());
if (settings->arrangement() == QListView::TopToBottom) {
setFlow(QListView::LeftToRight);
m_viewOptions.decorationPosition = QStyleOptionViewItem::Top;
} else {
setFlow(QListView::TopToBottom);
m_viewOptions.decorationPosition = QStyleOptionViewItem::Left;
}
}
DolphinIconsView::~DolphinIconsView()
{
}
QStyleOptionViewItem DolphinIconsView::viewOptions() const
{
return m_viewOptions;
}
void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
{
KListView::contextMenuEvent(event);
m_controller->triggerContextMenuRequest(event->pos());
}
void DolphinIconsView::mouseReleaseEvent(QMouseEvent* event)
{
KListView::mouseReleaseEvent(event);
m_controller->triggerActivation();
}
void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
{
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
}
void DolphinIconsView::dropEvent(QDropEvent* event)
{
const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
if (!urls.isEmpty()) {
m_controller->indicateDroppedUrls(urls,
indexAt(event->pos()),
event->source());
event->acceptProposedAction();
}
KListView::dropEvent(event);
}
void DolphinIconsView::updateGridSize(bool showPreview)
{
const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
Q_ASSERT(settings != 0);
int gridWidth = settings->gridWidth();
int gridHeight = settings->gridHeight();
int size = settings->iconSize();
if (showPreview) {
const int previewSize = settings->previewSize();
const int diff = previewSize - size;
Q_ASSERT(diff >= 0);
gridWidth += diff;
gridHeight += diff;
size = previewSize;
}
m_viewOptions.decorationSize = QSize(size, size);
setGridSize(QSize(gridWidth, gridHeight));
m_controller->setZoomInPossible(isZoomInPossible());
m_controller->setZoomOutPossible(isZoomOutPossible());
}
void DolphinIconsView::zoomIn()
{
if (isZoomInPossible()) {
IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
const int oldIconSize = settings->iconSize();
int newIconSize = oldIconSize;
const bool showPreview = m_controller->showPreview();
if (showPreview) {
const int previewSize = increasedIconSize(settings->previewSize());
settings->setPreviewSize(previewSize);
} else {
newIconSize = increasedIconSize(oldIconSize);
settings->setIconSize(newIconSize);
if (settings->previewSize() < newIconSize) {
// assure that the preview size is always >= the icon size
settings->setPreviewSize(newIconSize);
}
}
// increase also the grid size
const int diff = newIconSize - oldIconSize;
settings->setGridWidth(settings->gridWidth() + diff);
settings->setGridHeight(settings->gridHeight() + diff);
updateGridSize(showPreview);
}
}
void DolphinIconsView::zoomOut()
{
if (isZoomOutPossible()) {
IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
const int oldIconSize = settings->iconSize();
int newIconSize = oldIconSize;
const bool showPreview = m_controller->showPreview();
if (showPreview) {
const int previewSize = decreasedIconSize(settings->previewSize());
settings->setPreviewSize(previewSize);
if (settings->iconSize() > previewSize) {
// assure that the icon size is always <= the preview size
newIconSize = previewSize;
settings->setIconSize(newIconSize);
}
} else {
newIconSize = decreasedIconSize(settings->iconSize());
settings->setIconSize(newIconSize);
}
// decrease also the grid size
const int diff = oldIconSize - newIconSize;
settings->setGridWidth(settings->gridWidth() - diff);
settings->setGridHeight(settings->gridHeight() - diff);
updateGridSize(showPreview);
}
}
bool DolphinIconsView::isZoomInPossible() const
{
IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
return size < K3Icon::SizeEnormous;
}
bool DolphinIconsView::isZoomOutPossible() const
{
IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
const int size = m_controller->showPreview() ? settings->previewSize() : settings->iconSize();
return size > K3Icon::SizeSmall;
}
int DolphinIconsView::increasedIconSize(int size) const
{
// TODO: get rid of K3Icon sizes
int incSize = 0;
switch (size) {
case K3Icon::SizeSmall: incSize = K3Icon::SizeSmallMedium; break;
case K3Icon::SizeSmallMedium: incSize = K3Icon::SizeMedium; break;
case K3Icon::SizeMedium: incSize = K3Icon::SizeLarge; break;
case K3Icon::SizeLarge: incSize = K3Icon::SizeHuge; break;
case K3Icon::SizeHuge: incSize = K3Icon::SizeEnormous; break;
default: Q_ASSERT(false); break;
}
return incSize;
}
int DolphinIconsView::decreasedIconSize(int size) const
{
// TODO: get rid of K3Icon sizes
int decSize = 0;
switch (size) {
case K3Icon::SizeSmallMedium: decSize = K3Icon::SizeSmall; break;
case K3Icon::SizeMedium: decSize = K3Icon::SizeSmallMedium; break;
case K3Icon::SizeLarge: decSize = K3Icon::SizeMedium; break;
case K3Icon::SizeHuge: decSize = K3Icon::SizeLarge; break;
case K3Icon::SizeEnormous: decSize = K3Icon::SizeHuge; break;
default: Q_ASSERT(false); break;
}
return decSize;
}
#include "dolphiniconsview.moc"