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 viewport()->setAcceptDrops(true);
60 setMouseTracking(true);
62 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
63 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
64 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
65 // RETURN-key in keyPressEvent().
66 if (KGlobalSettings::singleClick()) {
67 connect(this, SIGNAL(clicked(const QModelIndex
&)),
68 controller
, SLOT(triggerItem(const QModelIndex
&)));
69 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
70 SelectionManager
* selManager
= new SelectionManager(this);
71 connect(selManager
, SIGNAL(selectionChanged()),
72 this, SLOT(requestActivation()));
73 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
74 selManager
, SLOT(reset()));
77 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
78 controller
, SLOT(triggerItem(const QModelIndex
&)));
80 connect(this, SIGNAL(entered(const QModelIndex
&)),
81 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
82 connect(this, SIGNAL(viewportEntered()),
83 controller
, SLOT(emitViewportEntered()));
84 connect(controller
, SIGNAL(zoomIn()),
85 this, SLOT(zoomIn()));
86 connect(controller
, SIGNAL(zoomOut()),
87 this, SLOT(zoomOut()));
89 const DolphinView
* view
= controller
->dolphinView();
90 connect(view
, SIGNAL(showPreviewChanged()),
91 this, SLOT(slotShowPreviewChanged()));
92 connect(view
, SIGNAL(additionalInfoChanged()),
93 this, SLOT(slotAdditionalInfoChanged()));
95 // apply the icons mode settings to the widget
96 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
97 Q_ASSERT(settings
!= 0);
99 if (settings
->useSystemFont()) {
100 m_font
= KGlobalSettings::generalFont();
102 m_font
= QFont(settings
->fontFamily(),
103 settings
->fontSize(),
104 settings
->fontWeight(),
105 settings
->italicFont());
108 setWordWrap(settings
->numberOfTextlines() > 1);
109 updateGridSize(view
->showPreview(), 0);
111 if (settings
->arrangement() == QListView::TopToBottom
) {
112 setFlow(QListView::LeftToRight
);
113 m_decorationPosition
= QStyleOptionViewItem::Top
;
114 m_displayAlignment
= Qt::AlignHCenter
;
116 setFlow(QListView::TopToBottom
);
117 m_decorationPosition
= QStyleOptionViewItem::Left
;
118 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
121 m_categoryDrawer
= new DolphinCategoryDrawer();
122 setCategoryDrawer(m_categoryDrawer
);
126 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
127 this, SLOT(updateFont()));
130 DolphinIconsView::~DolphinIconsView()
132 delete m_categoryDrawer
;
133 m_categoryDrawer
= 0;
136 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
138 KCategorizedView::dataChanged(topLeft
, bottomRight
);
140 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
141 if ((flow() == QListView::LeftToRight
) && !proxyModel
->isCategorizedModel()) {
142 // bypass a QListView issue that items are not layout correctly if the decoration size of
144 scheduleDelayedItemsLayout();
148 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
150 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
151 viewOptions
.font
= m_font
;
152 viewOptions
.decorationPosition
= m_decorationPosition
;
153 viewOptions
.decorationSize
= m_decorationSize
;
154 viewOptions
.displayAlignment
= m_displayAlignment
;
155 viewOptions
.showDecorationSelected
= true;
159 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
161 KCategorizedView::contextMenuEvent(event
);
162 m_controller
->triggerContextMenuRequest(event
->pos());
165 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
167 m_controller
->requestActivation();
168 if (!indexAt(event
->pos()).isValid()) {
169 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
170 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
175 KCategorizedView::mousePressEvent(event
);
178 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
180 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
181 // fix this in KDE 4.1
182 KCategorizedView::startDrag(supportedActions
);
183 DragAndDropHelper::startDrag(this, supportedActions
);
186 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
188 if (event
->mimeData()->hasUrls()) {
189 event
->acceptProposedAction();
193 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
195 KCategorizedView::dragLeaveEvent(event
);
196 setDirtyRegion(m_dropRect
);
199 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
201 KCategorizedView::dragMoveEvent(event
);
203 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
204 const QModelIndex index
= indexAt(event
->pos());
205 setDirtyRegion(m_dropRect
);
207 m_dropRect
.setSize(QSize()); // set as invalid
208 if (index
.isValid()) {
209 const KFileItem item
= m_controller
->itemForIndex(index
);
210 if (!item
.isNull() && item
.isDir()) {
211 m_dropRect
= visualRect(index
);
213 m_dropRect
.setSize(QSize()); // set as invalid
216 if (event
->mimeData()->hasUrls()) {
217 // accept url drops, independently from the destination item
218 event
->acceptProposedAction();
221 setDirtyRegion(m_dropRect
);
224 void DolphinIconsView::dropEvent(QDropEvent
* event
)
226 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
227 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
228 if (!urls
.isEmpty()) {
229 const QModelIndex index
= indexAt(event
->pos());
230 const KFileItem item
= m_controller
->itemForIndex(index
);
231 m_controller
->indicateDroppedUrls(urls
,
234 event
->acceptProposedAction();
238 KCategorizedView::dropEvent(event
);
241 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
243 KCategorizedView::keyPressEvent(event
);
244 m_controller
->handleKeyPressEvent(event
);
247 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
249 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
250 if (event
->modifiers() & Qt::ControlModifier
) {
254 KCategorizedView::wheelEvent(event
);
255 // if the icons are aligned left to right, the vertical wheel event should
256 // be applied to the horizontal scrollbar
257 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
258 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
259 (settings
->arrangement() == QListView::LeftToRight
);
260 if (scrollHorizontal
) {
261 QWheelEvent
horizEvent(event
->pos(),
266 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
270 void DolphinIconsView::showEvent(QShowEvent
* event
)
272 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
273 delegate
->setMaximumSize(m_itemSize
);
275 KCategorizedView::showEvent(event
);
278 void DolphinIconsView::slotShowPreviewChanged()
280 const DolphinView
* view
= m_controller
->dolphinView();
281 updateGridSize(view
->showPreview(), additionalInfoCount());
284 void DolphinIconsView::slotAdditionalInfoChanged()
286 const DolphinView
* view
= m_controller
->dolphinView();
287 const bool showPreview
= view
->showPreview();
288 updateGridSize(showPreview
, view
->additionalInfo().count());
291 void DolphinIconsView::zoomIn()
293 if (isZoomInPossible()) {
294 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
296 const int oldIconSize
= settings
->iconSize();
297 int newIconSize
= oldIconSize
;
299 const bool showPreview
= m_controller
->dolphinView()->showPreview();
301 const int previewSize
= increasedIconSize(settings
->previewSize());
302 settings
->setPreviewSize(previewSize
);
304 newIconSize
= increasedIconSize(oldIconSize
);
305 settings
->setIconSize(newIconSize
);
308 // increase also the grid size
309 const int diff
= newIconSize
- oldIconSize
;
310 settings
->setItemWidth(settings
->itemWidth() + diff
);
311 settings
->setItemHeight(settings
->itemHeight() + diff
);
313 updateGridSize(showPreview
, additionalInfoCount());
317 void DolphinIconsView::zoomOut()
319 if (isZoomOutPossible()) {
320 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
322 const int oldIconSize
= settings
->iconSize();
323 int newIconSize
= oldIconSize
;
325 const bool showPreview
= m_controller
->dolphinView()->showPreview();
327 const int previewSize
= decreasedIconSize(settings
->previewSize());
328 settings
->setPreviewSize(previewSize
);
330 newIconSize
= decreasedIconSize(settings
->iconSize());
331 settings
->setIconSize(newIconSize
);
334 // decrease also the grid size
335 const int diff
= oldIconSize
- newIconSize
;
336 settings
->setItemWidth(settings
->itemWidth() - diff
);
337 settings
->setItemHeight(settings
->itemHeight() - diff
);
339 updateGridSize(showPreview
, additionalInfoCount());
343 void DolphinIconsView::requestActivation()
345 m_controller
->requestActivation();
348 void DolphinIconsView::updateFont()
350 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
351 Q_ASSERT(settings
!= 0);
353 if (settings
->useSystemFont()) {
354 m_font
= KGlobalSettings::generalFont();
358 bool DolphinIconsView::isZoomInPossible() const
360 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
361 const bool showPreview
= m_controller
->dolphinView()->showPreview();
362 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
363 return size
< KIconLoader::SizeEnormous
;
366 bool DolphinIconsView::isZoomOutPossible() const
368 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
369 const bool showPreview
= m_controller
->dolphinView()->showPreview();
370 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
371 return size
> KIconLoader::SizeSmall
;
374 int DolphinIconsView::increasedIconSize(int size
) const
378 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
379 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
380 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
381 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
382 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
383 default: Q_ASSERT(false); break;
388 int DolphinIconsView::decreasedIconSize(int size
) const
392 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
393 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
394 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
395 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
396 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
397 default: Q_ASSERT(false); break;
402 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
404 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
405 Q_ASSERT(settings
!= 0);
407 int itemWidth
= settings
->itemWidth();
408 int itemHeight
= settings
->itemHeight();
409 int size
= settings
->iconSize();
412 const int previewSize
= settings
->previewSize();
413 const int diff
= previewSize
- size
;
420 Q_ASSERT(additionalInfoCount
>= 0);
421 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
423 if (settings
->arrangement() == QListView::TopToBottom
) {
424 // The decoration width indirectly defines the maximum
425 // width for the text wrapping. To use the maximum item width
426 // for text wrapping, it is used as decoration width.
427 m_decorationSize
= QSize(itemWidth
, size
);
428 setIconSize(QSize(itemWidth
, size
));
430 m_decorationSize
= QSize(size
, size
);
431 setIconSize(QSize(size
, size
));
434 m_itemSize
= QSize(itemWidth
, itemHeight
);
436 const int spacing
= settings
->gridSpacing();
437 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
439 m_controller
->setZoomInPossible(isZoomInPossible());
440 m_controller
->setZoomOutPossible(isZoomOutPossible());
442 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
444 delegate
->setMaximumSize(m_itemSize
);
448 int DolphinIconsView::additionalInfoCount() const
450 const DolphinView
* view
= m_controller
->dolphinView();
451 return view
->additionalInfo().count();
454 #include "dolphiniconsview.moc"