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(showPreviewChanged(bool)),
65 this, SLOT(slotShowPreviewChanged(bool)));
66 connect(controller
, SIGNAL(additionalInfoCountChanged(int)),
67 this, SLOT(slotAdditionalInfoCountChanged(int)));
68 connect(controller
, SIGNAL(zoomIn()),
69 this, SLOT(zoomIn()));
70 connect(controller
, SIGNAL(zoomOut()),
71 this, SLOT(zoomOut()));
73 connect(this, SIGNAL(entered(const QModelIndex
&)),
74 this, SLOT(slotEntered(const QModelIndex
&)));
76 // apply the icons mode settings to the widget
77 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
78 Q_ASSERT(settings
!= 0);
80 m_viewOptions
= KCategorizedView::viewOptions();
81 m_viewOptions
.showDecorationSelected
= true;
83 QFont
font(settings
->fontFamily(), settings
->fontSize());
84 font
.setItalic(settings
->italicFont());
85 font
.setBold(settings
->boldFont());
86 m_viewOptions
.font
= font
;
88 setWordWrap(settings
->numberOfTextlines() > 1);
89 updateGridSize(controller
->showPreview(), controller
->additionalInfoCount());
91 if (settings
->arrangement() == QListView::TopToBottom
) {
92 setFlow(QListView::LeftToRight
);
93 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
95 setFlow(QListView::TopToBottom
);
96 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
97 m_viewOptions
.displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
100 m_categoryDrawer
= new DolphinCategoryDrawer();
101 setCategoryDrawer(m_categoryDrawer
);
104 DolphinIconsView::~DolphinIconsView()
106 delete m_categoryDrawer
;
107 m_categoryDrawer
= 0;
110 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
112 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
114 QRect itemRect
= KCategorizedView::visualRect(index
);
115 const int maxWidth
= m_itemSize
.width();
116 const int maxHeight
= m_itemSize
.height();
118 if (itemRect
.width() > maxWidth
) {
119 // assure that the maximum item width is not exceeded
120 if (leftToRightFlow
) {
121 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
122 itemRect
.setLeft(left
);
124 itemRect
.setWidth(maxWidth
);
127 if (itemRect
.height() > maxHeight
) {
128 // assure that the maximum item height is not exceeded
129 if (!leftToRightFlow
) {
130 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
131 itemRect
.setTop(top
);
133 itemRect
.setHeight(maxHeight
);
139 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
141 return m_viewOptions
;
144 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
146 KCategorizedView::contextMenuEvent(event
);
147 m_controller
->triggerContextMenuRequest(event
->pos());
150 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
152 m_controller
->triggerActivation();
153 if (!indexAt(event
->pos()).isValid()) {
154 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
155 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
160 KCategorizedView::mousePressEvent(event
);
163 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
165 if (event
->mimeData()->hasUrls()) {
166 event
->acceptProposedAction();
171 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
173 KCategorizedView::dragLeaveEvent(event
);
175 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
177 setDirtyRegion(m_dropRect
);
180 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
182 KCategorizedView::dragMoveEvent(event
);
184 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
185 const QModelIndex index
= indexAt(event
->pos());
186 setDirtyRegion(m_dropRect
);
187 m_dropRect
= visualRect(index
);
188 setDirtyRegion(m_dropRect
);
191 void DolphinIconsView::dropEvent(QDropEvent
* event
)
193 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
194 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
195 if (!urls
.isEmpty()) {
196 m_controller
->indicateDroppedUrls(urls
,
198 indexAt(event
->pos()),
200 event
->acceptProposedAction();
204 KCategorizedView::dropEvent(event
);
208 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
210 KCategorizedView::paintEvent(event
);
212 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
214 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
215 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
219 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
221 KCategorizedView::keyPressEvent(event
);
223 const QItemSelectionModel
* selModel
= selectionModel();
224 const QModelIndex currentIndex
= selModel
->currentIndex();
225 const bool trigger
= currentIndex
.isValid()
226 && (event
->key() == Qt::Key_Return
)
227 && (selModel
->selectedIndexes().count() <= 1);
229 triggerItem(currentIndex
);
233 void DolphinIconsView::triggerItem(const QModelIndex
& index
)
235 m_controller
->triggerItem(itemForIndex(index
));
238 void DolphinIconsView::slotEntered(const QModelIndex
& index
)
240 m_controller
->emitItemEntered(itemForIndex(index
));
243 void DolphinIconsView::slotShowPreviewChanged(bool showPreview
)
245 updateGridSize(showPreview
, m_controller
->additionalInfoCount());
248 void DolphinIconsView::slotAdditionalInfoCountChanged(int count
)
250 updateGridSize(m_controller
->showPreview(), count
);
253 void DolphinIconsView::zoomIn()
255 if (isZoomInPossible()) {
256 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
258 const int oldIconSize
= settings
->iconSize();
259 int newIconSize
= oldIconSize
;
261 const bool showPreview
= m_controller
->showPreview();
263 const int previewSize
= increasedIconSize(settings
->previewSize());
264 settings
->setPreviewSize(previewSize
);
266 newIconSize
= increasedIconSize(oldIconSize
);
267 settings
->setIconSize(newIconSize
);
268 if (settings
->previewSize() < newIconSize
) {
269 // assure that the preview size is always >= the icon size
270 settings
->setPreviewSize(newIconSize
);
274 // increase also the grid size
275 const int diff
= newIconSize
- oldIconSize
;
276 settings
->setItemWidth(settings
->itemWidth() + diff
);
277 settings
->setItemHeight(settings
->itemHeight() + diff
);
279 updateGridSize(showPreview
, m_controller
->additionalInfoCount());
283 void DolphinIconsView::zoomOut()
285 if (isZoomOutPossible()) {
286 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
288 const int oldIconSize
= settings
->iconSize();
289 int newIconSize
= oldIconSize
;
291 const bool showPreview
= m_controller
->showPreview();
293 const int previewSize
= decreasedIconSize(settings
->previewSize());
294 settings
->setPreviewSize(previewSize
);
295 if (settings
->iconSize() > previewSize
) {
296 // assure that the icon size is always <= the preview size
297 newIconSize
= previewSize
;
298 settings
->setIconSize(newIconSize
);
301 newIconSize
= decreasedIconSize(settings
->iconSize());
302 settings
->setIconSize(newIconSize
);
305 // decrease also the grid size
306 const int diff
= oldIconSize
- newIconSize
;
307 settings
->setItemWidth(settings
->itemWidth() - diff
);
308 settings
->setItemHeight(settings
->itemHeight() - diff
);
310 updateGridSize(showPreview
, m_controller
->additionalInfoCount());
314 bool DolphinIconsView::isZoomInPossible() const
316 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
317 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
318 return size
< KIconLoader::SizeEnormous
;
321 bool DolphinIconsView::isZoomOutPossible() const
323 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
324 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
325 return size
> KIconLoader::SizeSmall
;
328 int DolphinIconsView::increasedIconSize(int size
) const
332 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
333 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
334 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
335 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
336 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
337 default: Q_ASSERT(false); break;
342 int DolphinIconsView::decreasedIconSize(int size
) const
346 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
347 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
348 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
349 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
350 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
351 default: Q_ASSERT(false); break;
356 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
358 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
359 Q_ASSERT(settings
!= 0);
361 int itemWidth
= settings
->itemWidth();
362 int itemHeight
= settings
->itemHeight();
363 int size
= settings
->iconSize();
366 const int previewSize
= settings
->previewSize();
367 const int diff
= previewSize
- size
;
375 Q_ASSERT(additionalInfoCount
>= 0);
376 itemHeight
+= additionalInfoCount
* m_viewOptions
.font
.pointSize() * 2;
378 if (settings
->arrangement() == QListView::TopToBottom
) {
379 // The decoration width indirectly defines the maximum
380 // width for the text wrapping. To use the maximum item width
381 // for text wrapping, it is used as decoration width.
382 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
384 m_viewOptions
.decorationSize
= QSize(size
, size
);
387 const int spacing
= settings
->gridSpacing();
388 setGridSize(QSize(itemWidth
+ spacing
, itemHeight
+ spacing
));
390 m_itemSize
= QSize(itemWidth
, itemHeight
);
392 m_controller
->setZoomInPossible(isZoomInPossible());
393 m_controller
->setZoomOutPossible(isZoomOutPossible());
396 KFileItem
DolphinIconsView::itemForIndex(const QModelIndex
& index
) const
398 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
399 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
400 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
401 return dirModel
->itemForIndex(dirIndex
);
405 #include "dolphiniconsview.moc"