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>
33 #include <kfileitemdelegate.h>
35 #include <QAbstractProxyModel>
36 #include <QApplication>
41 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
42 KCategorizedView(parent
),
43 m_controller(controller
),
47 m_decorationPosition(QStyleOptionViewItem::Top
),
48 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 setEditTriggers(QAbstractItemView::NoEditTriggers
);
59 viewport()->setAcceptDrops(true);
61 setMouseTracking(true);
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 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
139 KCategorizedView::dataChanged(topLeft
, bottomRight
);
141 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
142 if ((flow() == QListView::LeftToRight
) && !proxyModel
->isCategorizedModel()) {
143 // bypass a QListView issue that items are not layout correctly if the decoration size of
145 scheduleDelayedItemsLayout();
149 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
151 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
152 viewOptions
.font
= m_font
;
153 viewOptions
.decorationPosition
= m_decorationPosition
;
154 viewOptions
.decorationSize
= m_decorationSize
;
155 viewOptions
.displayAlignment
= m_displayAlignment
;
156 viewOptions
.showDecorationSelected
= true;
160 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
162 KCategorizedView::contextMenuEvent(event
);
163 m_controller
->triggerContextMenuRequest(event
->pos());
166 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
168 m_controller
->requestActivation();
169 if (!indexAt(event
->pos()).isValid()) {
170 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
171 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
176 KCategorizedView::mousePressEvent(event
);
179 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
181 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
182 // fix this in KDE 4.1
183 KCategorizedView::startDrag(supportedActions
);
184 DragAndDropHelper::startDrag(this, supportedActions
);
187 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
189 if (event
->mimeData()->hasUrls()) {
190 event
->acceptProposedAction();
194 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
196 KCategorizedView::dragLeaveEvent(event
);
197 setDirtyRegion(m_dropRect
);
200 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
202 KCategorizedView::dragMoveEvent(event
);
204 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
205 const QModelIndex index
= indexAt(event
->pos());
206 setDirtyRegion(m_dropRect
);
208 m_dropRect
.setSize(QSize()); // set as invalid
209 if (index
.isValid()) {
210 const KFileItem item
= m_controller
->itemForIndex(index
);
211 if (!item
.isNull() && item
.isDir()) {
212 m_dropRect
= visualRect(index
);
214 m_dropRect
.setSize(QSize()); // set as invalid
217 if (event
->mimeData()->hasUrls()) {
218 // accept url drops, independently from the destination item
219 event
->acceptProposedAction();
222 setDirtyRegion(m_dropRect
);
225 void DolphinIconsView::dropEvent(QDropEvent
* event
)
227 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
228 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
229 if (!urls
.isEmpty()) {
230 const QModelIndex index
= indexAt(event
->pos());
231 const KFileItem item
= m_controller
->itemForIndex(index
);
232 m_controller
->indicateDroppedUrls(urls
,
235 event
->acceptProposedAction();
239 KCategorizedView::dropEvent(event
);
242 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
244 KCategorizedView::keyPressEvent(event
);
245 m_controller
->handleKeyPressEvent(event
);
248 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
250 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
251 if (event
->modifiers() & Qt::ControlModifier
) {
255 KCategorizedView::wheelEvent(event
);
256 // if the icons are aligned left to right, the vertical wheel event should
257 // be applied to the horizontal scrollbar
258 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
259 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
260 (settings
->arrangement() == QListView::LeftToRight
);
261 if (scrollHorizontal
) {
262 QWheelEvent
horizEvent(event
->pos(),
267 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
271 void DolphinIconsView::showEvent(QShowEvent
* event
)
273 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
274 delegate
->setMaximumSize(m_itemSize
);
276 KCategorizedView::showEvent(event
);
279 void DolphinIconsView::slotShowPreviewChanged()
281 const DolphinView
* view
= m_controller
->dolphinView();
282 updateGridSize(view
->showPreview(), additionalInfoCount());
285 void DolphinIconsView::slotAdditionalInfoChanged()
287 const DolphinView
* view
= m_controller
->dolphinView();
288 const bool showPreview
= view
->showPreview();
289 updateGridSize(showPreview
, view
->additionalInfo().count());
292 void DolphinIconsView::zoomIn()
294 if (isZoomInPossible()) {
295 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
297 const int oldIconSize
= settings
->iconSize();
298 int newIconSize
= oldIconSize
;
300 const bool showPreview
= m_controller
->dolphinView()->showPreview();
302 const int previewSize
= increasedIconSize(settings
->previewSize());
303 settings
->setPreviewSize(previewSize
);
305 newIconSize
= increasedIconSize(oldIconSize
);
306 settings
->setIconSize(newIconSize
);
309 // increase also the grid size
310 const int diff
= newIconSize
- oldIconSize
;
311 settings
->setItemWidth(settings
->itemWidth() + diff
);
312 settings
->setItemHeight(settings
->itemHeight() + diff
);
314 updateGridSize(showPreview
, additionalInfoCount());
318 void DolphinIconsView::zoomOut()
320 if (isZoomOutPossible()) {
321 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
323 const int oldIconSize
= settings
->iconSize();
324 int newIconSize
= oldIconSize
;
326 const bool showPreview
= m_controller
->dolphinView()->showPreview();
328 const int previewSize
= decreasedIconSize(settings
->previewSize());
329 settings
->setPreviewSize(previewSize
);
331 newIconSize
= decreasedIconSize(settings
->iconSize());
332 settings
->setIconSize(newIconSize
);
335 // decrease also the grid size
336 const int diff
= oldIconSize
- newIconSize
;
337 settings
->setItemWidth(settings
->itemWidth() - diff
);
338 settings
->setItemHeight(settings
->itemHeight() - diff
);
340 updateGridSize(showPreview
, additionalInfoCount());
344 void DolphinIconsView::requestActivation()
346 m_controller
->requestActivation();
349 void DolphinIconsView::updateFont()
351 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
352 Q_ASSERT(settings
!= 0);
354 if (settings
->useSystemFont()) {
355 m_font
= KGlobalSettings::generalFont();
359 bool DolphinIconsView::isZoomInPossible() const
361 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
362 const bool showPreview
= m_controller
->dolphinView()->showPreview();
363 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
364 return size
< KIconLoader::SizeEnormous
;
367 bool DolphinIconsView::isZoomOutPossible() const
369 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
370 const bool showPreview
= m_controller
->dolphinView()->showPreview();
371 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
372 return size
> KIconLoader::SizeSmall
;
375 int DolphinIconsView::increasedIconSize(int size
) const
379 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
380 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
381 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
382 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
383 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
384 default: Q_ASSERT(false); break;
389 int DolphinIconsView::decreasedIconSize(int size
) const
393 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
394 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
395 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
396 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
397 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
398 default: Q_ASSERT(false); break;
403 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
405 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
406 Q_ASSERT(settings
!= 0);
408 int itemWidth
= settings
->itemWidth();
409 int itemHeight
= settings
->itemHeight();
410 int size
= settings
->iconSize();
413 const int previewSize
= settings
->previewSize();
414 const int diff
= previewSize
- size
;
421 Q_ASSERT(additionalInfoCount
>= 0);
422 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
424 if (settings
->arrangement() == QListView::TopToBottom
) {
425 // The decoration width indirectly defines the maximum
426 // width for the text wrapping. To use the maximum item width
427 // for text wrapping, it is used as decoration width.
428 m_decorationSize
= QSize(itemWidth
, size
);
429 setIconSize(QSize(itemWidth
, size
));
431 m_decorationSize
= QSize(size
, size
);
432 setIconSize(QSize(size
, size
));
435 m_itemSize
= QSize(itemWidth
, itemHeight
);
437 const int spacing
= settings
->gridSpacing();
438 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
440 m_controller
->setZoomInPossible(isZoomInPossible());
441 m_controller
->setZoomOutPossible(isZoomOutPossible());
443 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
445 delegate
->setMaximumSize(m_itemSize
);
449 int DolphinIconsView::additionalInfoCount() const
451 const DolphinView
* view
= m_controller
->dolphinView();
452 return view
->additionalInfo().count();
455 #include "dolphiniconsview.moc"