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"
27 #include <kcategorizedsortfilterproxymodel.h>
29 #include <kdirmodel.h>
31 #include <QAbstractProxyModel>
32 #include <QApplication>
36 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
37 KCategorizedView(parent
),
38 m_controller(controller
),
44 Q_ASSERT(controller
!= 0);
45 setViewMode(QListView::IconMode
);
46 setResizeMode(QListView::Adjust
);
47 setSpacing(KDialog::spacingHint());
48 setMovement(QListView::Snap
);
49 setMouseTracking(true);
50 viewport()->setAttribute(Qt::WA_Hover
);
52 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
53 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
54 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
55 // RETURN-key in keyPressEvent().
56 if (KGlobalSettings::singleClick()) {
57 connect(this, SIGNAL(clicked(const QModelIndex
&)),
58 this, SLOT(triggerItem(const QModelIndex
&)));
60 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
61 this, SLOT(triggerItem(const QModelIndex
&)));
63 connect(this, SIGNAL(viewportEntered()),
64 controller
, SLOT(emitViewportEntered()));
65 connect(controller
, SIGNAL(zoomIn()),
66 this, SLOT(zoomIn()));
67 connect(controller
, SIGNAL(zoomOut()),
68 this, SLOT(zoomOut()));
70 const DolphinView
* view
= controller
->dolphinView();
71 connect(view
, SIGNAL(showPreviewChanged()),
72 this, SLOT(slotShowPreviewChanged()));
73 connect(view
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
74 this, SLOT(slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
&)));
76 connect(this, SIGNAL(entered(const QModelIndex
&)),
77 this, SLOT(slotEntered(const QModelIndex
&)));
79 // apply the icons mode settings to the widget
80 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
81 Q_ASSERT(settings
!= 0);
83 m_viewOptions
= KCategorizedView::viewOptions();
84 m_viewOptions
.showDecorationSelected
= true;
86 QFont
font(settings
->fontFamily(), settings
->fontSize());
87 font
.setItalic(settings
->italicFont());
88 font
.setBold(settings
->boldFont());
89 m_viewOptions
.font
= font
;
91 setWordWrap(settings
->numberOfTextlines() > 1);
92 updateGridSize(view
->showPreview(), 0);
94 if (settings
->arrangement() == QListView::TopToBottom
) {
95 setFlow(QListView::LeftToRight
);
96 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
98 setFlow(QListView::TopToBottom
);
99 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
100 m_viewOptions
.displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
103 m_categoryDrawer
= new DolphinCategoryDrawer();
104 setCategoryDrawer(m_categoryDrawer
);
109 DolphinIconsView::~DolphinIconsView()
111 delete m_categoryDrawer
;
112 m_categoryDrawer
= 0;
115 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
117 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
119 QRect itemRect
= KCategorizedView::visualRect(index
);
120 const int maxWidth
= m_itemSize
.width();
121 const int maxHeight
= m_itemSize
.height();
123 if (itemRect
.width() > maxWidth
) {
124 // assure that the maximum item width is not exceeded
125 if (leftToRightFlow
) {
126 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
127 itemRect
.setLeft(left
);
129 itemRect
.setWidth(maxWidth
);
132 if (itemRect
.height() > maxHeight
) {
133 // assure that the maximum item height is not exceeded
134 if (!leftToRightFlow
) {
135 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
136 itemRect
.setTop(top
);
138 itemRect
.setHeight(maxHeight
);
141 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
142 if (leftToRightFlow
&& !proxyModel
->isCategorizedModel()) {
143 // TODO: This workaround bypasses a layout issue in QListView::visualRect(), where
144 // the horizontal position of items might get calculated in a wrong manner when the item
145 // name is too long. I'll try create a patch for Qt but as Dolphin must also work with
146 // Qt 4.3.0 this workaround must get applied at least for KDE 4.0.
147 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
148 const int margin
= settings
->gridSpacing() / 2;
149 const int gridWidth
= gridSize().width();
150 int left
= itemRect
.left();
151 left
= ((left
- margin
+ 1) / gridWidth
) * gridWidth
+ margin
;
152 itemRect
.moveLeft(left
);
158 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
160 return m_viewOptions
;
163 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
165 KCategorizedView::contextMenuEvent(event
);
166 m_controller
->triggerContextMenuRequest(event
->pos());
169 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
171 m_controller
->requestActivation();
172 if (!indexAt(event
->pos()).isValid()) {
173 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
174 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
179 KCategorizedView::mousePressEvent(event
);
182 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
184 if (event
->mimeData()->hasUrls()) {
185 event
->acceptProposedAction();
190 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
192 KCategorizedView::dragLeaveEvent(event
);
194 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
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
);
206 if (itemForIndex(index
).isDir()) {
207 m_dropRect
= visualRect(index
);
209 m_dropRect
.setSize(QSize()); // set as invalid
211 setDirtyRegion(m_dropRect
);
214 void DolphinIconsView::dropEvent(QDropEvent
* event
)
216 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
217 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
218 if (!urls
.isEmpty()) {
219 const QModelIndex index
= indexAt(event
->pos());
220 const KFileItem item
= itemForIndex(index
);
221 m_controller
->indicateDroppedUrls(urls
,
224 event
->acceptProposedAction();
228 KCategorizedView::dropEvent(event
);
232 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
234 KCategorizedView::paintEvent(event
);
236 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
238 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
239 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
243 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
245 KCategorizedView::keyPressEvent(event
);
247 const QItemSelectionModel
* selModel
= selectionModel();
248 const QModelIndex currentIndex
= selModel
->currentIndex();
249 const bool trigger
= currentIndex
.isValid()
250 && (event
->key() == Qt::Key_Return
)
251 && (selModel
->selectedIndexes().count() <= 1);
253 triggerItem(currentIndex
);
257 void DolphinIconsView::triggerItem(const QModelIndex
& index
)
259 m_controller
->triggerItem(itemForIndex(index
));
262 void DolphinIconsView::slotEntered(const QModelIndex
& index
)
264 m_controller
->emitItemEntered(itemForIndex(index
));
267 void DolphinIconsView::slotShowPreviewChanged()
269 const DolphinView
* view
= m_controller
->dolphinView();
270 const int infoCount
= view
->additionalInfo().count();
271 updateGridSize(view
->showPreview(), infoCount
);
274 void DolphinIconsView::slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
& info
)
276 const bool showPreview
= m_controller
->dolphinView()->showPreview();
277 updateGridSize(showPreview
, info
.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
);
295 if (settings
->previewSize() < newIconSize
) {
296 // assure that the preview size is always >= the icon size
297 settings
->setPreviewSize(newIconSize
);
301 // increase also the grid size
302 const int diff
= newIconSize
- oldIconSize
;
303 settings
->setItemWidth(settings
->itemWidth() + diff
);
304 settings
->setItemHeight(settings
->itemHeight() + diff
);
306 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
307 updateGridSize(showPreview
, infoCount
);
311 void DolphinIconsView::zoomOut()
313 if (isZoomOutPossible()) {
314 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
316 const int oldIconSize
= settings
->iconSize();
317 int newIconSize
= oldIconSize
;
319 const bool showPreview
= m_controller
->dolphinView()->showPreview();
321 const int previewSize
= decreasedIconSize(settings
->previewSize());
322 settings
->setPreviewSize(previewSize
);
323 if (settings
->iconSize() > previewSize
) {
324 // assure that the icon size is always <= the preview size
325 newIconSize
= previewSize
;
326 settings
->setIconSize(newIconSize
);
329 newIconSize
= decreasedIconSize(settings
->iconSize());
330 settings
->setIconSize(newIconSize
);
333 // decrease also the grid size
334 const int diff
= oldIconSize
- newIconSize
;
335 settings
->setItemWidth(settings
->itemWidth() - diff
);
336 settings
->setItemHeight(settings
->itemHeight() - diff
);
338 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
339 updateGridSize(showPreview
, infoCount
);
343 bool DolphinIconsView::isZoomInPossible() const
345 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
346 const bool showPreview
= m_controller
->dolphinView()->showPreview();
347 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
348 return size
< KIconLoader::SizeEnormous
;
351 bool DolphinIconsView::isZoomOutPossible() const
353 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
354 const bool showPreview
= m_controller
->dolphinView()->showPreview();
355 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
356 return size
> KIconLoader::SizeSmall
;
359 int DolphinIconsView::increasedIconSize(int size
) const
363 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
364 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
365 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
366 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
367 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
368 default: Q_ASSERT(false); break;
373 int DolphinIconsView::decreasedIconSize(int size
) const
377 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
378 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
379 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
380 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
381 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
382 default: Q_ASSERT(false); break;
387 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
389 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
390 Q_ASSERT(settings
!= 0);
392 int itemWidth
= settings
->itemWidth();
393 int itemHeight
= settings
->itemHeight();
394 int size
= settings
->iconSize();
397 const int previewSize
= settings
->previewSize();
398 const int diff
= previewSize
- size
;
406 Q_ASSERT(additionalInfoCount
>= 0);
407 itemHeight
+= additionalInfoCount
* m_viewOptions
.font
.pointSize() * 2;
409 if (settings
->arrangement() == QListView::TopToBottom
) {
410 // The decoration width indirectly defines the maximum
411 // width for the text wrapping. To use the maximum item width
412 // for text wrapping, it is used as decoration width.
413 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
415 m_viewOptions
.decorationSize
= QSize(size
, size
);
418 const int spacing
= settings
->gridSpacing();
419 setGridSize(QSize(itemWidth
+ spacing
, itemHeight
+ spacing
));
421 m_itemSize
= QSize(itemWidth
, itemHeight
);
423 m_controller
->setZoomInPossible(isZoomInPossible());
424 m_controller
->setZoomOutPossible(isZoomOutPossible());
427 KFileItem
DolphinIconsView::itemForIndex(const QModelIndex
& index
) const
429 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
430 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
431 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
432 return dirModel
->itemForIndex(dirIndex
);
436 #include "dolphiniconsview.moc"