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();
149 const int gridWidth
= gridSize().width();
150 const int gridIndex
= (itemRect
.left() - margin
+ 1) / gridWidth
;
151 itemRect
.moveLeft(gridIndex
* gridWidth
+ margin
);
157 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
159 return m_viewOptions
;
162 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
164 KCategorizedView::contextMenuEvent(event
);
165 m_controller
->triggerContextMenuRequest(event
->pos());
168 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
170 m_controller
->requestActivation();
171 if (!indexAt(event
->pos()).isValid()) {
172 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
173 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
178 KCategorizedView::mousePressEvent(event
);
181 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
183 if (event
->mimeData()->hasUrls()) {
184 event
->acceptProposedAction();
189 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
191 KCategorizedView::dragLeaveEvent(event
);
193 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
195 setDirtyRegion(m_dropRect
);
198 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
200 KCategorizedView::dragMoveEvent(event
);
202 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
203 const QModelIndex index
= indexAt(event
->pos());
204 setDirtyRegion(m_dropRect
);
205 if (itemForIndex(index
).isDir()) {
206 m_dropRect
= visualRect(index
);
208 m_dropRect
.setSize(QSize()); // set as invalid
210 setDirtyRegion(m_dropRect
);
213 void DolphinIconsView::dropEvent(QDropEvent
* event
)
215 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
216 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
217 if (!urls
.isEmpty()) {
218 const QModelIndex index
= indexAt(event
->pos());
219 const KFileItem item
= itemForIndex(index
);
220 m_controller
->indicateDroppedUrls(urls
,
223 event
->acceptProposedAction();
227 KCategorizedView::dropEvent(event
);
231 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
233 KCategorizedView::paintEvent(event
);
235 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
237 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
238 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
242 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
244 KCategorizedView::keyPressEvent(event
);
246 const QItemSelectionModel
* selModel
= selectionModel();
247 const QModelIndex currentIndex
= selModel
->currentIndex();
248 const bool trigger
= currentIndex
.isValid()
249 && (event
->key() == Qt::Key_Return
)
250 && (selModel
->selectedIndexes().count() <= 1);
252 triggerItem(currentIndex
);
256 void DolphinIconsView::triggerItem(const QModelIndex
& index
)
258 m_controller
->triggerItem(itemForIndex(index
));
261 void DolphinIconsView::slotEntered(const QModelIndex
& index
)
263 m_controller
->emitItemEntered(itemForIndex(index
));
266 void DolphinIconsView::slotShowPreviewChanged()
268 const DolphinView
* view
= m_controller
->dolphinView();
269 const int infoCount
= view
->additionalInfo().count();
270 updateGridSize(view
->showPreview(), infoCount
);
273 void DolphinIconsView::slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
& info
)
275 const bool showPreview
= m_controller
->dolphinView()->showPreview();
276 updateGridSize(showPreview
, info
.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
);
294 if (settings
->previewSize() < newIconSize
) {
295 // assure that the preview size is always >= the icon size
296 settings
->setPreviewSize(newIconSize
);
300 // increase also the grid size
301 const int diff
= newIconSize
- oldIconSize
;
302 settings
->setItemWidth(settings
->itemWidth() + diff
);
303 settings
->setItemHeight(settings
->itemHeight() + diff
);
305 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
306 updateGridSize(showPreview
, infoCount
);
310 void DolphinIconsView::zoomOut()
312 if (isZoomOutPossible()) {
313 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
315 const int oldIconSize
= settings
->iconSize();
316 int newIconSize
= oldIconSize
;
318 const bool showPreview
= m_controller
->dolphinView()->showPreview();
320 const int previewSize
= decreasedIconSize(settings
->previewSize());
321 settings
->setPreviewSize(previewSize
);
322 if (settings
->iconSize() > previewSize
) {
323 // assure that the icon size is always <= the preview size
324 newIconSize
= previewSize
;
325 settings
->setIconSize(newIconSize
);
328 newIconSize
= decreasedIconSize(settings
->iconSize());
329 settings
->setIconSize(newIconSize
);
332 // decrease also the grid size
333 const int diff
= oldIconSize
- newIconSize
;
334 settings
->setItemWidth(settings
->itemWidth() - diff
);
335 settings
->setItemHeight(settings
->itemHeight() - diff
);
337 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
338 updateGridSize(showPreview
, infoCount
);
342 bool DolphinIconsView::isZoomInPossible() const
344 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
345 const bool showPreview
= m_controller
->dolphinView()->showPreview();
346 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
347 return size
< KIconLoader::SizeEnormous
;
350 bool DolphinIconsView::isZoomOutPossible() const
352 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
353 const bool showPreview
= m_controller
->dolphinView()->showPreview();
354 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
355 return size
> KIconLoader::SizeSmall
;
358 int DolphinIconsView::increasedIconSize(int size
) const
362 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
363 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
364 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
365 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
366 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
367 default: Q_ASSERT(false); break;
372 int DolphinIconsView::decreasedIconSize(int size
) const
376 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
377 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
378 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
379 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
380 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
381 default: Q_ASSERT(false); break;
386 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
388 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
389 Q_ASSERT(settings
!= 0);
391 int itemWidth
= settings
->itemWidth();
392 int itemHeight
= settings
->itemHeight();
393 int size
= settings
->iconSize();
396 const int previewSize
= settings
->previewSize();
397 const int diff
= previewSize
- size
;
405 Q_ASSERT(additionalInfoCount
>= 0);
406 itemHeight
+= additionalInfoCount
* m_viewOptions
.font
.pointSize() * 2;
408 if (settings
->arrangement() == QListView::TopToBottom
) {
409 // The decoration width indirectly defines the maximum
410 // width for the text wrapping. To use the maximum item width
411 // for text wrapping, it is used as decoration width.
412 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
414 m_viewOptions
.decorationSize
= QSize(size
, size
);
417 const int spacing
= settings
->gridSpacing();
418 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
420 m_itemSize
= QSize(itemWidth
, itemHeight
);
422 m_controller
->setZoomInPossible(isZoomInPossible());
423 m_controller
->setZoomOutPossible(isZoomOutPossible());
426 KFileItem
DolphinIconsView::itemForIndex(const QModelIndex
& index
) const
428 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
429 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
430 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
431 return dirModel
->itemForIndex(dirIndex
);
435 #include "dolphiniconsview.moc"