1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "dolphiniconsview.h"
22 #include "dolphincategorydrawer.h"
23 #include "dolphincontroller.h"
24 #include "dolphinsettings.h"
25 #include "dolphin_iconsmodesettings.h"
26 #include "dolphin_generalsettings.h"
27 #include "draganddrophelper.h"
28 #include "selectionmanager.h"
30 #include <kcategorizedsortfilterproxymodel.h>
32 #include <kdirmodel.h>
34 #include <QAbstractProxyModel>
35 #include <QApplication>
40 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
41 KCategorizedView(parent
),
42 m_controller(controller
),
46 m_decorationPosition(QStyleOptionViewItem::Top
),
47 m_displayAlignment(Qt::AlignHCenter
),
52 Q_ASSERT(controller
!= 0);
53 setViewMode(QListView::IconMode
);
54 setResizeMode(QListView::Adjust
);
55 setSpacing(KDialog::spacingHint());
56 setMovement(QListView::Static
);
58 viewport()->setAcceptDrops(true);
60 setMouseTracking(true);
61 viewport()->setAttribute(Qt::WA_Hover
);
63 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
64 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
65 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
66 // RETURN-key in keyPressEvent().
67 if (KGlobalSettings::singleClick()) {
68 connect(this, SIGNAL(clicked(const QModelIndex
&)),
69 controller
, SLOT(triggerItem(const QModelIndex
&)));
70 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
71 SelectionManager
* selManager
= new SelectionManager(this);
72 connect(selManager
, SIGNAL(selectionChanged()),
73 this, SLOT(requestActivation()));
74 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
75 selManager
, SLOT(reset()));
78 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
79 controller
, SLOT(triggerItem(const QModelIndex
&)));
81 connect(this, SIGNAL(entered(const QModelIndex
&)),
82 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
83 connect(this, SIGNAL(viewportEntered()),
84 controller
, SLOT(emitViewportEntered()));
85 connect(controller
, SIGNAL(zoomIn()),
86 this, SLOT(zoomIn()));
87 connect(controller
, SIGNAL(zoomOut()),
88 this, SLOT(zoomOut()));
90 const DolphinView
* view
= controller
->dolphinView();
91 connect(view
, SIGNAL(showPreviewChanged()),
92 this, SLOT(slotShowPreviewChanged()));
93 connect(view
, SIGNAL(additionalInfoChanged()),
94 this, SLOT(slotAdditionalInfoChanged()));
96 // apply the icons mode settings to the widget
97 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
98 Q_ASSERT(settings
!= 0);
100 if (settings
->useSystemFont()) {
101 m_font
= KGlobalSettings::generalFont();
103 m_font
= QFont(settings
->fontFamily(),
104 settings
->fontSize(),
105 settings
->fontWeight(),
106 settings
->italicFont());
109 setWordWrap(settings
->numberOfTextlines() > 1);
110 updateGridSize(view
->showPreview(), 0);
112 if (settings
->arrangement() == QListView::TopToBottom
) {
113 setFlow(QListView::LeftToRight
);
114 m_decorationPosition
= QStyleOptionViewItem::Top
;
115 m_displayAlignment
= Qt::AlignHCenter
;
117 setFlow(QListView::TopToBottom
);
118 m_decorationPosition
= QStyleOptionViewItem::Left
;
119 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
122 m_categoryDrawer
= new DolphinCategoryDrawer();
123 setCategoryDrawer(m_categoryDrawer
);
127 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
128 this, SLOT(updateFont()));
131 DolphinIconsView::~DolphinIconsView()
133 delete m_categoryDrawer
;
134 m_categoryDrawer
= 0;
137 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
139 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
141 QRect itemRect
= KCategorizedView::visualRect(index
);
143 const int maxWidth
= m_itemSize
.width();
144 const int maxHeight
= m_itemSize
.height();
146 if (itemRect
.width() > maxWidth
) {
147 // assure that the maximum item width is not exceeded
148 if (leftToRightFlow
) {
149 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
150 itemRect
.setLeft(left
);
152 itemRect
.setWidth(maxWidth
);
155 if (itemRect
.height() > maxHeight
) {
156 // assure that the maximum item height is not exceeded
157 if (!leftToRightFlow
) {
158 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
159 itemRect
.setTop(top
);
161 itemRect
.setHeight(maxHeight
);
164 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
165 if (leftToRightFlow
&& !proxyModel
->isCategorizedModel()) {
166 // TODO: QListView::visualRect() calculates a wrong position of the items under
167 // certain circumstances (e. g. if the text is too long). This issue is bypassed
168 // by the following code (I'll try create a patch for Qt but as Dolphin must also work with
169 // Qt 4.3.0 this workaround must get applied at least for KDE 4.0).
170 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
171 const int margin
= settings
->gridSpacing();
172 const int gridWidth
= gridSize().width();
173 const int gridIndex
= (itemRect
.left() - margin
+ 1) / gridWidth
;
174 const int centerInc
= (maxWidth
- itemRect
.width()) / 2;
175 itemRect
.moveLeft((gridIndex
* gridWidth
) + margin
+ centerInc
);
181 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
183 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
184 viewOptions
.font
= m_font
;
185 viewOptions
.decorationPosition
= m_decorationPosition
;
186 viewOptions
.decorationSize
= m_decorationSize
;
187 viewOptions
.displayAlignment
= m_displayAlignment
;
188 viewOptions
.showDecorationSelected
= true;
192 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
194 KCategorizedView::contextMenuEvent(event
);
195 m_controller
->triggerContextMenuRequest(event
->pos());
198 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
200 m_controller
->requestActivation();
201 if (!indexAt(event
->pos()).isValid()) {
202 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
203 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
208 KCategorizedView::mousePressEvent(event
);
211 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
213 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
214 // fix this in KDE 4.1
215 KCategorizedView::startDrag(supportedActions
);
216 DragAndDropHelper::startDrag(this, supportedActions
);
219 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
221 if (event
->mimeData()->hasUrls()) {
222 event
->acceptProposedAction();
227 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
229 KCategorizedView::dragLeaveEvent(event
);
231 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
233 setDirtyRegion(m_dropRect
);
236 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
238 KCategorizedView::dragMoveEvent(event
);
240 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
241 const QModelIndex index
= indexAt(event
->pos());
242 setDirtyRegion(m_dropRect
);
244 m_dropRect
.setSize(QSize()); // set as invalid
245 if (index
.isValid()) {
246 const KFileItem item
= m_controller
->itemForIndex(index
);
247 if (!item
.isNull() && item
.isDir()) {
248 m_dropRect
= visualRect(index
);
250 m_dropRect
.setSize(QSize()); // set as invalid
253 if (event
->mimeData()->hasUrls()) {
254 // accept url drops, independently from the destination item
255 event
->acceptProposedAction();
258 setDirtyRegion(m_dropRect
);
261 void DolphinIconsView::dropEvent(QDropEvent
* event
)
263 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
264 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
265 if (!urls
.isEmpty()) {
266 const QModelIndex index
= indexAt(event
->pos());
267 const KFileItem item
= m_controller
->itemForIndex(index
);
268 m_controller
->indicateDroppedUrls(urls
,
271 event
->acceptProposedAction();
275 KCategorizedView::dropEvent(event
);
280 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
282 KCategorizedView::paintEvent(event
);
284 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
286 const QBrush
& brush
= viewOptions().palette
.brush(QPalette::Normal
, QPalette::Highlight
);
287 DragAndDropHelper::drawHoverIndication(this, m_dropRect
, brush
);
291 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
293 KCategorizedView::keyPressEvent(event
);
294 m_controller
->handleKeyPressEvent(event
);
297 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
299 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
300 if ((event
->modifiers() & Qt::ControlModifier
) == Qt::ControlModifier
) {
304 KCategorizedView::wheelEvent(event
);
305 // if the icons are aligned left to right, the vertical wheel event should
306 // be applied to the horizontal scrollbar
307 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
308 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
309 (settings
->arrangement() == QListView::LeftToRight
);
310 if (scrollHorizontal
) {
311 QWheelEvent
horizEvent(event
->pos(),
316 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
320 void DolphinIconsView::slotShowPreviewChanged()
322 const DolphinView
* view
= m_controller
->dolphinView();
323 updateGridSize(view
->showPreview(), additionalInfoCount());
326 void DolphinIconsView::slotAdditionalInfoChanged()
328 const DolphinView
* view
= m_controller
->dolphinView();
329 const bool showPreview
= view
->showPreview();
330 updateGridSize(showPreview
, view
->additionalInfo().count());
333 void DolphinIconsView::zoomIn()
335 if (isZoomInPossible()) {
336 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
338 const int oldIconSize
= settings
->iconSize();
339 int newIconSize
= oldIconSize
;
341 const bool showPreview
= m_controller
->dolphinView()->showPreview();
343 const int previewSize
= increasedIconSize(settings
->previewSize());
344 settings
->setPreviewSize(previewSize
);
346 newIconSize
= increasedIconSize(oldIconSize
);
347 settings
->setIconSize(newIconSize
);
348 if (settings
->previewSize() < newIconSize
) {
349 // assure that the preview size is always >= the icon size
350 settings
->setPreviewSize(newIconSize
);
354 // increase also the grid size
355 const int diff
= newIconSize
- oldIconSize
;
356 settings
->setItemWidth(settings
->itemWidth() + diff
);
357 settings
->setItemHeight(settings
->itemHeight() + diff
);
359 updateGridSize(showPreview
, additionalInfoCount());
363 void DolphinIconsView::zoomOut()
365 if (isZoomOutPossible()) {
366 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
368 const int oldIconSize
= settings
->iconSize();
369 int newIconSize
= oldIconSize
;
371 const bool showPreview
= m_controller
->dolphinView()->showPreview();
373 const int previewSize
= decreasedIconSize(settings
->previewSize());
374 settings
->setPreviewSize(previewSize
);
375 if (settings
->iconSize() > previewSize
) {
376 // assure that the icon size is always <= the preview size
377 newIconSize
= previewSize
;
378 settings
->setIconSize(newIconSize
);
381 newIconSize
= decreasedIconSize(settings
->iconSize());
382 settings
->setIconSize(newIconSize
);
385 // decrease also the grid size
386 const int diff
= oldIconSize
- newIconSize
;
387 settings
->setItemWidth(settings
->itemWidth() - diff
);
388 settings
->setItemHeight(settings
->itemHeight() - diff
);
390 updateGridSize(showPreview
, additionalInfoCount());
394 void DolphinIconsView::requestActivation()
396 m_controller
->requestActivation();
399 void DolphinIconsView::updateFont()
401 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
402 Q_ASSERT(settings
!= 0);
404 if (settings
->useSystemFont()) {
405 m_font
= KGlobalSettings::generalFont();
409 bool DolphinIconsView::isZoomInPossible() const
411 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
412 const bool showPreview
= m_controller
->dolphinView()->showPreview();
413 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
414 return size
< KIconLoader::SizeEnormous
;
417 bool DolphinIconsView::isZoomOutPossible() const
419 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
420 const bool showPreview
= m_controller
->dolphinView()->showPreview();
421 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
422 return size
> KIconLoader::SizeSmall
;
425 int DolphinIconsView::increasedIconSize(int size
) const
429 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
430 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
431 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
432 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
433 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
434 default: Q_ASSERT(false); break;
439 int DolphinIconsView::decreasedIconSize(int size
) const
443 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
444 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
445 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
446 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
447 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
448 default: Q_ASSERT(false); break;
453 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
455 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
456 Q_ASSERT(settings
!= 0);
458 int itemWidth
= settings
->itemWidth();
459 int itemHeight
= settings
->itemHeight();
460 int size
= settings
->iconSize();
463 const int previewSize
= settings
->previewSize();
464 const int diff
= previewSize
- size
;
471 setIconSize(QSize(size
, size
));
473 Q_ASSERT(additionalInfoCount
>= 0);
474 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
476 if (settings
->arrangement() == QListView::TopToBottom
) {
477 // The decoration width indirectly defines the maximum
478 // width for the text wrapping. To use the maximum item width
479 // for text wrapping, it is used as decoration width.
480 m_decorationSize
= QSize(itemWidth
, size
);
482 m_decorationSize
= QSize(size
, size
);
485 m_itemSize
= QSize(itemWidth
, itemHeight
);
487 const int spacing
= settings
->gridSpacing();
488 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
490 m_controller
->setZoomInPossible(isZoomInPossible());
491 m_controller
->setZoomOutPossible(isZoomOutPossible());
494 int DolphinIconsView::additionalInfoCount() const
496 const DolphinView
* view
= m_controller
->dolphinView();
497 return view
->additionalInfo().count();
500 #include "dolphiniconsview.moc"