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 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 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
261 delegate
->setMaximumSize(m_itemSize
);
263 KCategorizedView::showEvent(event
);
266 void DolphinIconsView::slotShowPreviewChanged()
268 const DolphinView
* view
= m_controller
->dolphinView();
269 updateGridSize(view
->showPreview(), additionalInfoCount());
272 void DolphinIconsView::slotAdditionalInfoChanged()
274 const DolphinView
* view
= m_controller
->dolphinView();
275 const bool showPreview
= view
->showPreview();
276 updateGridSize(showPreview
, view
->additionalInfo().count());
279 void DolphinIconsView::zoomIn()
281 if (isZoomInPossible()) {
282 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
284 const int oldIconSize
= settings
->iconSize();
285 int newIconSize
= oldIconSize
;
287 const bool showPreview
= m_controller
->dolphinView()->showPreview();
289 const int previewSize
= increasedIconSize(settings
->previewSize());
290 settings
->setPreviewSize(previewSize
);
292 newIconSize
= increasedIconSize(oldIconSize
);
293 settings
->setIconSize(newIconSize
);
296 // increase also the grid size
297 const int diff
= newIconSize
- oldIconSize
;
298 settings
->setItemWidth(settings
->itemWidth() + diff
);
299 settings
->setItemHeight(settings
->itemHeight() + diff
);
301 updateGridSize(showPreview
, additionalInfoCount());
305 void DolphinIconsView::zoomOut()
307 if (isZoomOutPossible()) {
308 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
310 const int oldIconSize
= settings
->iconSize();
311 int newIconSize
= oldIconSize
;
313 const bool showPreview
= m_controller
->dolphinView()->showPreview();
315 const int previewSize
= decreasedIconSize(settings
->previewSize());
316 settings
->setPreviewSize(previewSize
);
318 newIconSize
= decreasedIconSize(settings
->iconSize());
319 settings
->setIconSize(newIconSize
);
322 // decrease also the grid size
323 const int diff
= oldIconSize
- newIconSize
;
324 settings
->setItemWidth(settings
->itemWidth() - diff
);
325 settings
->setItemHeight(settings
->itemHeight() - diff
);
327 updateGridSize(showPreview
, additionalInfoCount());
331 void DolphinIconsView::requestActivation()
333 m_controller
->requestActivation();
336 void DolphinIconsView::updateFont()
338 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
339 Q_ASSERT(settings
!= 0);
341 if (settings
->useSystemFont()) {
342 m_font
= KGlobalSettings::generalFont();
346 bool DolphinIconsView::isZoomInPossible() const
348 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
349 const bool showPreview
= m_controller
->dolphinView()->showPreview();
350 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
351 return size
< KIconLoader::SizeEnormous
;
354 bool DolphinIconsView::isZoomOutPossible() const
356 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
357 const bool showPreview
= m_controller
->dolphinView()->showPreview();
358 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
359 return size
> KIconLoader::SizeSmall
;
362 int DolphinIconsView::increasedIconSize(int size
) const
366 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
367 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
368 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
369 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
370 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
371 default: Q_ASSERT(false); break;
376 int DolphinIconsView::decreasedIconSize(int size
) const
380 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
381 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
382 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
383 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
384 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
385 default: Q_ASSERT(false); break;
390 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
392 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
393 Q_ASSERT(settings
!= 0);
395 int itemWidth
= settings
->itemWidth();
396 int itemHeight
= settings
->itemHeight();
397 int size
= settings
->iconSize();
400 const int previewSize
= settings
->previewSize();
401 const int diff
= previewSize
- size
;
408 Q_ASSERT(additionalInfoCount
>= 0);
409 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
411 if (settings
->arrangement() == QListView::TopToBottom
) {
412 // The decoration width indirectly defines the maximum
413 // width for the text wrapping. To use the maximum item width
414 // for text wrapping, it is used as decoration width.
415 m_decorationSize
= QSize(itemWidth
, size
);
416 setIconSize(QSize(itemWidth
, size
));
418 m_decorationSize
= QSize(size
, size
);
419 setIconSize(QSize(size
, size
));
422 m_itemSize
= QSize(itemWidth
, itemHeight
);
424 const int spacing
= settings
->gridSpacing();
425 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
427 m_controller
->setZoomInPossible(isZoomInPossible());
428 m_controller
->setZoomOutPossible(isZoomOutPossible());
430 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
432 delegate
->setMaximumSize(m_itemSize
);
436 int DolphinIconsView::additionalInfoCount() const
438 const DolphinView
* view
= m_controller
->dolphinView();
439 return view
->additionalInfo().count();
442 #include "dolphiniconsview.moc"