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 "dolphinfileitemdelegate.h"
25 #include "dolphinsettings.h"
26 #include "dolphin_iconsmodesettings.h"
27 #include "dolphin_generalsettings.h"
28 #include "draganddrophelper.h"
29 #include "selectionmanager.h"
31 #include <kcategorizedsortfilterproxymodel.h>
33 #include <kdirmodel.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 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
138 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
139 viewOptions
.font
= m_font
;
140 viewOptions
.decorationPosition
= m_decorationPosition
;
141 viewOptions
.decorationSize
= m_decorationSize
;
142 viewOptions
.displayAlignment
= m_displayAlignment
;
143 viewOptions
.showDecorationSelected
= true;
147 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
149 KCategorizedView::contextMenuEvent(event
);
150 m_controller
->triggerContextMenuRequest(event
->pos());
153 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
155 m_controller
->requestActivation();
156 if (!indexAt(event
->pos()).isValid()) {
157 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
158 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
163 KCategorizedView::mousePressEvent(event
);
166 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
168 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
169 // fix this in KDE 4.1
170 KCategorizedView::startDrag(supportedActions
);
171 DragAndDropHelper::startDrag(this, supportedActions
);
174 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
176 if (event
->mimeData()->hasUrls()) {
177 event
->acceptProposedAction();
181 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
183 KCategorizedView::dragLeaveEvent(event
);
184 setDirtyRegion(m_dropRect
);
187 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
189 KCategorizedView::dragMoveEvent(event
);
191 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
192 const QModelIndex index
= indexAt(event
->pos());
193 setDirtyRegion(m_dropRect
);
195 m_dropRect
.setSize(QSize()); // set as invalid
196 if (index
.isValid()) {
197 const KFileItem item
= m_controller
->itemForIndex(index
);
198 if (!item
.isNull() && item
.isDir()) {
199 m_dropRect
= visualRect(index
);
201 m_dropRect
.setSize(QSize()); // set as invalid
204 if (event
->mimeData()->hasUrls()) {
205 // accept url drops, independently from the destination item
206 event
->acceptProposedAction();
209 setDirtyRegion(m_dropRect
);
212 void DolphinIconsView::dropEvent(QDropEvent
* event
)
214 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
215 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
216 if (!urls
.isEmpty()) {
217 const QModelIndex index
= indexAt(event
->pos());
218 const KFileItem item
= m_controller
->itemForIndex(index
);
219 m_controller
->indicateDroppedUrls(urls
,
222 event
->acceptProposedAction();
226 KCategorizedView::dropEvent(event
);
229 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
231 KCategorizedView::keyPressEvent(event
);
232 m_controller
->handleKeyPressEvent(event
);
235 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
237 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
238 if (event
->modifiers() & Qt::ControlModifier
) {
242 KCategorizedView::wheelEvent(event
);
243 // if the icons are aligned left to right, the vertical wheel event should
244 // be applied to the horizontal scrollbar
245 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
246 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
247 (settings
->arrangement() == QListView::LeftToRight
);
248 if (scrollHorizontal
) {
249 QWheelEvent
horizEvent(event
->pos(),
254 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
258 void DolphinIconsView::showEvent(QShowEvent
* event
)
260 Q_ASSERT(qobject_cast
<DolphinFileItemDelegate
*>(itemDelegate()) != 0);
261 DolphinFileItemDelegate
* delegate
= static_cast<DolphinFileItemDelegate
*>(itemDelegate());
262 delegate
->setMaximumSize(m_itemSize
);
264 KCategorizedView::showEvent(event
);
267 void DolphinIconsView::slotShowPreviewChanged()
269 const DolphinView
* view
= m_controller
->dolphinView();
270 updateGridSize(view
->showPreview(), additionalInfoCount());
273 void DolphinIconsView::slotAdditionalInfoChanged()
275 const DolphinView
* view
= m_controller
->dolphinView();
276 const bool showPreview
= view
->showPreview();
277 updateGridSize(showPreview
, view
->additionalInfo().count());
280 void DolphinIconsView::zoomIn()
282 if (isZoomInPossible()) {
283 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
285 const int oldIconSize
= settings
->iconSize();
286 int newIconSize
= oldIconSize
;
288 const bool showPreview
= m_controller
->dolphinView()->showPreview();
290 const int previewSize
= increasedIconSize(settings
->previewSize());
291 settings
->setPreviewSize(previewSize
);
293 newIconSize
= increasedIconSize(oldIconSize
);
294 settings
->setIconSize(newIconSize
);
297 // increase also the grid size
298 const int diff
= newIconSize
- oldIconSize
;
299 settings
->setItemWidth(settings
->itemWidth() + diff
);
300 settings
->setItemHeight(settings
->itemHeight() + diff
);
302 updateGridSize(showPreview
, additionalInfoCount());
306 void DolphinIconsView::zoomOut()
308 if (isZoomOutPossible()) {
309 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
311 const int oldIconSize
= settings
->iconSize();
312 int newIconSize
= oldIconSize
;
314 const bool showPreview
= m_controller
->dolphinView()->showPreview();
316 const int previewSize
= decreasedIconSize(settings
->previewSize());
317 settings
->setPreviewSize(previewSize
);
319 newIconSize
= decreasedIconSize(settings
->iconSize());
320 settings
->setIconSize(newIconSize
);
323 // decrease also the grid size
324 const int diff
= oldIconSize
- newIconSize
;
325 settings
->setItemWidth(settings
->itemWidth() - diff
);
326 settings
->setItemHeight(settings
->itemHeight() - diff
);
328 updateGridSize(showPreview
, additionalInfoCount());
332 void DolphinIconsView::requestActivation()
334 m_controller
->requestActivation();
337 void DolphinIconsView::updateFont()
339 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
340 Q_ASSERT(settings
!= 0);
342 if (settings
->useSystemFont()) {
343 m_font
= KGlobalSettings::generalFont();
347 bool DolphinIconsView::isZoomInPossible() const
349 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
350 const bool showPreview
= m_controller
->dolphinView()->showPreview();
351 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
352 return size
< KIconLoader::SizeEnormous
;
355 bool DolphinIconsView::isZoomOutPossible() const
357 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
358 const bool showPreview
= m_controller
->dolphinView()->showPreview();
359 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
360 return size
> KIconLoader::SizeSmall
;
363 int DolphinIconsView::increasedIconSize(int size
) const
367 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
368 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
369 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
370 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
371 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
372 default: Q_ASSERT(false); break;
377 int DolphinIconsView::decreasedIconSize(int size
) const
381 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
382 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
383 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
384 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
385 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
386 default: Q_ASSERT(false); break;
391 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
393 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
394 Q_ASSERT(settings
!= 0);
396 int itemWidth
= settings
->itemWidth();
397 int itemHeight
= settings
->itemHeight();
398 int size
= settings
->iconSize();
401 const int previewSize
= settings
->previewSize();
402 const int diff
= previewSize
- size
;
409 Q_ASSERT(additionalInfoCount
>= 0);
410 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
412 if (settings
->arrangement() == QListView::TopToBottom
) {
413 // The decoration width indirectly defines the maximum
414 // width for the text wrapping. To use the maximum item width
415 // for text wrapping, it is used as decoration width.
416 m_decorationSize
= QSize(itemWidth
, size
);
417 setIconSize(QSize(itemWidth
, size
));
419 m_decorationSize
= QSize(size
, size
);
420 setIconSize(QSize(size
, size
));
423 m_itemSize
= QSize(itemWidth
, itemHeight
);
425 const int spacing
= settings
->gridSpacing();
426 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
428 m_controller
->setZoomInPossible(isZoomInPossible());
429 m_controller
->setZoomOutPossible(isZoomOutPossible());
431 DolphinFileItemDelegate
* delegate
= qobject_cast
<DolphinFileItemDelegate
*>(itemDelegate());
433 delegate
->setMaximumSize(m_itemSize
);
437 int DolphinIconsView::additionalInfoCount() const
439 const DolphinView
* view
= m_controller
->dolphinView();
440 return view
->additionalInfo().count();
443 #include "dolphiniconsview.moc"