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"
26 #include "dolphin_iconsmodesettings.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 setMouseTracking(true);
49 viewport()->setAttribute(Qt::WA_Hover
);
51 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
52 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
53 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
54 // RETURN-key in keyPressEvent().
55 if (KGlobalSettings::singleClick()) {
56 connect(this, SIGNAL(clicked(const QModelIndex
&)),
57 this, SLOT(triggerItem(const QModelIndex
&)));
59 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
60 this, SLOT(triggerItem(const QModelIndex
&)));
62 connect(this, SIGNAL(viewportEntered()),
63 controller
, SLOT(emitViewportEntered()));
64 connect(controller
, SIGNAL(zoomIn()),
65 this, SLOT(zoomIn()));
66 connect(controller
, SIGNAL(zoomOut()),
67 this, SLOT(zoomOut()));
69 const DolphinView
* view
= controller
->dolphinView();
70 connect(view
, SIGNAL(showPreviewChanged()),
71 this, SLOT(slotShowPreviewChanged()));
72 connect(view
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
73 this, SLOT(slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
&)));
75 connect(this, SIGNAL(entered(const QModelIndex
&)),
76 this, SLOT(slotEntered(const QModelIndex
&)));
78 // apply the icons mode settings to the widget
79 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
80 Q_ASSERT(settings
!= 0);
82 m_viewOptions
= KCategorizedView::viewOptions();
83 m_viewOptions
.showDecorationSelected
= true;
85 QFont
font(settings
->fontFamily(), settings
->fontSize());
86 font
.setItalic(settings
->italicFont());
87 font
.setBold(settings
->boldFont());
88 m_viewOptions
.font
= font
;
90 setWordWrap(settings
->numberOfTextlines() > 1);
91 updateGridSize(view
->showPreview(), 0);
93 if (settings
->arrangement() == QListView::TopToBottom
) {
94 setFlow(QListView::LeftToRight
);
95 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
97 setFlow(QListView::TopToBottom
);
98 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
99 m_viewOptions
.displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
102 m_categoryDrawer
= new DolphinCategoryDrawer();
103 setCategoryDrawer(m_categoryDrawer
);
108 DolphinIconsView::~DolphinIconsView()
110 delete m_categoryDrawer
;
111 m_categoryDrawer
= 0;
114 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
116 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
118 QRect itemRect
= KCategorizedView::visualRect(index
);
119 const int maxWidth
= m_itemSize
.width();
120 const int maxHeight
= m_itemSize
.height();
122 if (itemRect
.width() > maxWidth
) {
123 // assure that the maximum item width is not exceeded
124 if (leftToRightFlow
) {
125 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
126 itemRect
.setLeft(left
);
128 itemRect
.setWidth(maxWidth
);
131 if (itemRect
.height() > maxHeight
) {
132 // assure that the maximum item height is not exceeded
133 if (!leftToRightFlow
) {
134 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
135 itemRect
.setTop(top
);
137 itemRect
.setHeight(maxHeight
);
143 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
145 return m_viewOptions
;
148 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
150 KCategorizedView::contextMenuEvent(event
);
151 m_controller
->triggerContextMenuRequest(event
->pos());
154 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
156 m_controller
->requestActivation();
157 if (!indexAt(event
->pos()).isValid()) {
158 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
159 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
164 KCategorizedView::mousePressEvent(event
);
167 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
169 if (event
->mimeData()->hasUrls()) {
170 event
->acceptProposedAction();
175 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
177 KCategorizedView::dragLeaveEvent(event
);
179 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
181 setDirtyRegion(m_dropRect
);
184 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
186 KCategorizedView::dragMoveEvent(event
);
188 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
189 const QModelIndex index
= indexAt(event
->pos());
190 setDirtyRegion(m_dropRect
);
191 m_dropRect
= visualRect(index
);
192 setDirtyRegion(m_dropRect
);
195 void DolphinIconsView::dropEvent(QDropEvent
* event
)
197 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
198 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
199 if (!urls
.isEmpty()) {
200 const QModelIndex index
= indexAt(event
->pos());
201 if (index
.isValid()) {
202 const KFileItem item
= itemForIndex(index
);
203 m_controller
->indicateDroppedUrls(urls
,
207 event
->acceptProposedAction();
212 KCategorizedView::dropEvent(event
);
216 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
218 KCategorizedView::paintEvent(event
);
220 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
222 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
223 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
227 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
229 KCategorizedView::keyPressEvent(event
);
231 const QItemSelectionModel
* selModel
= selectionModel();
232 const QModelIndex currentIndex
= selModel
->currentIndex();
233 const bool trigger
= currentIndex
.isValid()
234 && (event
->key() == Qt::Key_Return
)
235 && (selModel
->selectedIndexes().count() <= 1);
237 triggerItem(currentIndex
);
241 void DolphinIconsView::triggerItem(const QModelIndex
& index
)
243 m_controller
->triggerItem(itemForIndex(index
));
246 void DolphinIconsView::slotEntered(const QModelIndex
& index
)
248 m_controller
->emitItemEntered(itemForIndex(index
));
251 void DolphinIconsView::slotShowPreviewChanged()
253 const DolphinView
* view
= m_controller
->dolphinView();
254 const int infoCount
= view
->additionalInfo().count();
255 updateGridSize(view
->showPreview(), infoCount
);
258 void DolphinIconsView::slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
& info
)
260 const bool showPreview
= m_controller
->dolphinView()->showPreview();
261 updateGridSize(showPreview
, info
.count());
264 void DolphinIconsView::zoomIn()
266 if (isZoomInPossible()) {
267 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
269 const int oldIconSize
= settings
->iconSize();
270 int newIconSize
= oldIconSize
;
272 const bool showPreview
= m_controller
->dolphinView()->showPreview();
274 const int previewSize
= increasedIconSize(settings
->previewSize());
275 settings
->setPreviewSize(previewSize
);
277 newIconSize
= increasedIconSize(oldIconSize
);
278 settings
->setIconSize(newIconSize
);
279 if (settings
->previewSize() < newIconSize
) {
280 // assure that the preview size is always >= the icon size
281 settings
->setPreviewSize(newIconSize
);
285 // increase also the grid size
286 const int diff
= newIconSize
- oldIconSize
;
287 settings
->setItemWidth(settings
->itemWidth() + diff
);
288 settings
->setItemHeight(settings
->itemHeight() + diff
);
290 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
291 updateGridSize(showPreview
, infoCount
);
295 void DolphinIconsView::zoomOut()
297 if (isZoomOutPossible()) {
298 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
300 const int oldIconSize
= settings
->iconSize();
301 int newIconSize
= oldIconSize
;
303 const bool showPreview
= m_controller
->dolphinView()->showPreview();
305 const int previewSize
= decreasedIconSize(settings
->previewSize());
306 settings
->setPreviewSize(previewSize
);
307 if (settings
->iconSize() > previewSize
) {
308 // assure that the icon size is always <= the preview size
309 newIconSize
= previewSize
;
310 settings
->setIconSize(newIconSize
);
313 newIconSize
= decreasedIconSize(settings
->iconSize());
314 settings
->setIconSize(newIconSize
);
317 // decrease also the grid size
318 const int diff
= oldIconSize
- newIconSize
;
319 settings
->setItemWidth(settings
->itemWidth() - diff
);
320 settings
->setItemHeight(settings
->itemHeight() - diff
);
322 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
323 updateGridSize(showPreview
, infoCount
);
327 bool DolphinIconsView::isZoomInPossible() const
329 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
330 const bool showPreview
= m_controller
->dolphinView()->showPreview();
331 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
332 return size
< KIconLoader::SizeEnormous
;
335 bool DolphinIconsView::isZoomOutPossible() const
337 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
338 const bool showPreview
= m_controller
->dolphinView()->showPreview();
339 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
340 return size
> KIconLoader::SizeSmall
;
343 int DolphinIconsView::increasedIconSize(int size
) const
347 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
348 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
349 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
350 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
351 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
352 default: Q_ASSERT(false); break;
357 int DolphinIconsView::decreasedIconSize(int size
) const
361 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
362 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
363 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
364 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
365 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
366 default: Q_ASSERT(false); break;
371 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
373 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
374 Q_ASSERT(settings
!= 0);
376 int itemWidth
= settings
->itemWidth();
377 int itemHeight
= settings
->itemHeight();
378 int size
= settings
->iconSize();
381 const int previewSize
= settings
->previewSize();
382 const int diff
= previewSize
- size
;
390 Q_ASSERT(additionalInfoCount
>= 0);
391 itemHeight
+= additionalInfoCount
* m_viewOptions
.font
.pointSize() * 2;
393 if (settings
->arrangement() == QListView::TopToBottom
) {
394 // The decoration width indirectly defines the maximum
395 // width for the text wrapping. To use the maximum item width
396 // for text wrapping, it is used as decoration width.
397 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
399 m_viewOptions
.decorationSize
= QSize(size
, size
);
402 const int spacing
= settings
->gridSpacing();
403 setGridSize(QSize(itemWidth
+ spacing
, itemHeight
+ spacing
));
405 m_itemSize
= QSize(itemWidth
, itemHeight
);
407 m_controller
->setZoomInPossible(isZoomInPossible());
408 m_controller
->setZoomOutPossible(isZoomOutPossible());
411 KFileItem
DolphinIconsView::itemForIndex(const QModelIndex
& index
) const
413 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
414 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
415 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
416 return dirModel
->itemForIndex(dirIndex
);
420 #include "dolphiniconsview.moc"