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 "dolphincontroller.h"
23 #include "dolphinsettings.h"
25 #include "dolphin_iconsmodesettings.h"
29 #include <QAbstractProxyModel>
30 #include <QApplication>
34 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
35 KCategorizedView(parent
),
36 m_controller(controller
),
41 Q_ASSERT(controller
!= 0);
42 setViewMode(QListView::IconMode
);
43 setResizeMode(QListView::Adjust
);
44 setSpacing(KDialog::spacingHint());
45 setMouseTracking(true);
46 viewport()->setAttribute(Qt::WA_Hover
);
48 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
49 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
50 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
51 // RETURN-key in keyPressEvent().
52 if (KGlobalSettings::singleClick()) {
53 connect(this, SIGNAL(clicked(const QModelIndex
&)),
54 controller
, SLOT(triggerItem(const QModelIndex
&)));
56 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
57 controller
, SLOT(triggerItem(const QModelIndex
&)));
59 connect(this, SIGNAL(entered(const QModelIndex
&)),
60 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
61 connect(this, SIGNAL(viewportEntered()),
62 controller
, SLOT(emitViewportEntered()));
63 connect(controller
, SIGNAL(showPreviewChanged(bool)),
64 this, SLOT(slotShowPreviewChanged(bool)));
65 connect(controller
, SIGNAL(showAdditionalInfoChanged(bool)),
66 this, SLOT(slotShowAdditionalInfoChanged(bool)));
67 connect(controller
, SIGNAL(zoomIn()),
68 this, SLOT(zoomIn()));
69 connect(controller
, SIGNAL(zoomOut()),
70 this, SLOT(zoomOut()));
72 // apply the icons mode settings to the widget
73 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
74 Q_ASSERT(settings
!= 0);
76 m_viewOptions
= KCategorizedView::viewOptions();
77 m_viewOptions
.showDecorationSelected
= true;
79 QFont
font(settings
->fontFamily(), settings
->fontSize());
80 font
.setItalic(settings
->italicFont());
81 font
.setBold(settings
->boldFont());
82 m_viewOptions
.font
= font
;
84 setWordWrap(settings
->numberOfTextlines() > 1);
85 updateGridSize(controller
->showPreview(), controller
->showAdditionalInfo());
87 if (settings
->arrangement() == QListView::TopToBottom
) {
88 setFlow(QListView::LeftToRight
);
89 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
91 setFlow(QListView::TopToBottom
);
92 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
93 m_viewOptions
.displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
97 DolphinIconsView::~DolphinIconsView()
101 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
103 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
105 QRect itemRect
= KCategorizedView::visualRect(index
);
106 const int maxWidth
= m_itemSize
.width();
107 const int maxHeight
= m_itemSize
.height();
109 if (itemRect
.width() > maxWidth
) {
110 // assure that the maximum item width is not exceeded
111 if (leftToRightFlow
) {
112 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
113 itemRect
.setLeft(left
);
115 itemRect
.setWidth(maxWidth
);
118 if (itemRect
.height() > maxHeight
) {
119 // assure that the maximum item height is not exceeded
120 if (!leftToRightFlow
) {
121 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
122 itemRect
.setTop(top
);
124 itemRect
.setHeight(maxHeight
);
130 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
132 return m_viewOptions
;
135 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
137 KCategorizedView::contextMenuEvent(event
);
138 m_controller
->triggerContextMenuRequest(event
->pos());
141 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
143 m_controller
->triggerActivation();
144 if (!indexAt(event
->pos()).isValid()) {
145 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
146 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
151 KCategorizedView::mousePressEvent(event
);
154 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
156 if (event
->mimeData()->hasUrls()) {
157 event
->acceptProposedAction();
162 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
164 KCategorizedView::dragLeaveEvent(event
);
166 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
168 setDirtyRegion(m_dropRect
);
171 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
173 KCategorizedView::dragMoveEvent(event
);
175 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
176 const QModelIndex index
= indexAt(event
->pos());
177 setDirtyRegion(m_dropRect
);
178 m_dropRect
= visualRect(index
);
179 setDirtyRegion(m_dropRect
);
182 void DolphinIconsView::dropEvent(QDropEvent
* event
)
184 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
185 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
186 if (!urls
.isEmpty()) {
187 m_controller
->indicateDroppedUrls(urls
,
189 indexAt(event
->pos()),
191 event
->acceptProposedAction();
195 KCategorizedView::dropEvent(event
);
199 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
201 KCategorizedView::paintEvent(event
);
203 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
205 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
206 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
210 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
212 KCategorizedView::keyPressEvent(event
);
214 const QItemSelectionModel
* selModel
= selectionModel();
215 const QModelIndex currentIndex
= selModel
->currentIndex();
216 const bool triggerItem
= currentIndex
.isValid()
217 && (event
->key() == Qt::Key_Return
)
218 && (selModel
->selectedIndexes().count() <= 1);
220 m_controller
->triggerItem(currentIndex
);
224 void DolphinIconsView::slotShowPreviewChanged(bool showPreview
)
226 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
229 void DolphinIconsView::slotShowAdditionalInfoChanged(bool showAdditionalInfo
)
231 updateGridSize(m_controller
->showPreview(), showAdditionalInfo
);
234 void DolphinIconsView::zoomIn()
236 if (isZoomInPossible()) {
237 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
239 const int oldIconSize
= settings
->iconSize();
240 int newIconSize
= oldIconSize
;
242 const bool showPreview
= m_controller
->showPreview();
244 const int previewSize
= increasedIconSize(settings
->previewSize());
245 settings
->setPreviewSize(previewSize
);
247 newIconSize
= increasedIconSize(oldIconSize
);
248 settings
->setIconSize(newIconSize
);
249 if (settings
->previewSize() < newIconSize
) {
250 // assure that the preview size is always >= the icon size
251 settings
->setPreviewSize(newIconSize
);
255 // increase also the grid size
256 const int diff
= newIconSize
- oldIconSize
;
257 settings
->setItemWidth(settings
->itemWidth() + diff
);
258 settings
->setItemHeight(settings
->itemHeight() + diff
);
260 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
264 void DolphinIconsView::zoomOut()
266 if (isZoomOutPossible()) {
267 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
269 const int oldIconSize
= settings
->iconSize();
270 int newIconSize
= oldIconSize
;
272 const bool showPreview
= m_controller
->showPreview();
274 const int previewSize
= decreasedIconSize(settings
->previewSize());
275 settings
->setPreviewSize(previewSize
);
276 if (settings
->iconSize() > previewSize
) {
277 // assure that the icon size is always <= the preview size
278 newIconSize
= previewSize
;
279 settings
->setIconSize(newIconSize
);
282 newIconSize
= decreasedIconSize(settings
->iconSize());
283 settings
->setIconSize(newIconSize
);
286 // decrease also the grid size
287 const int diff
= oldIconSize
- newIconSize
;
288 settings
->setItemWidth(settings
->itemWidth() - diff
);
289 settings
->setItemHeight(settings
->itemHeight() - diff
);
291 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
295 bool DolphinIconsView::isZoomInPossible() const
297 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
298 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
299 return size
< K3Icon::SizeEnormous
;
302 bool DolphinIconsView::isZoomOutPossible() const
304 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
305 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
306 return size
> K3Icon::SizeSmall
;
309 int DolphinIconsView::increasedIconSize(int size
) const
311 // TODO: get rid of K3Icon sizes
314 case K3Icon::SizeSmall
: incSize
= K3Icon::SizeSmallMedium
; break;
315 case K3Icon::SizeSmallMedium
: incSize
= K3Icon::SizeMedium
; break;
316 case K3Icon::SizeMedium
: incSize
= K3Icon::SizeLarge
; break;
317 case K3Icon::SizeLarge
: incSize
= K3Icon::SizeHuge
; break;
318 case K3Icon::SizeHuge
: incSize
= K3Icon::SizeEnormous
; break;
319 default: Q_ASSERT(false); break;
324 int DolphinIconsView::decreasedIconSize(int size
) const
326 // TODO: get rid of K3Icon sizes
329 case K3Icon::SizeSmallMedium
: decSize
= K3Icon::SizeSmall
; break;
330 case K3Icon::SizeMedium
: decSize
= K3Icon::SizeSmallMedium
; break;
331 case K3Icon::SizeLarge
: decSize
= K3Icon::SizeMedium
; break;
332 case K3Icon::SizeHuge
: decSize
= K3Icon::SizeLarge
; break;
333 case K3Icon::SizeEnormous
: decSize
= K3Icon::SizeHuge
; break;
334 default: Q_ASSERT(false); break;
339 void DolphinIconsView::updateGridSize(bool showPreview
, bool showAdditionalInfo
)
341 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
342 Q_ASSERT(settings
!= 0);
344 int itemWidth
= settings
->itemWidth();
345 int itemHeight
= settings
->itemHeight();
346 int size
= settings
->iconSize();
349 const int previewSize
= settings
->previewSize();
350 const int diff
= previewSize
- size
;
358 if (showAdditionalInfo
) {
359 itemHeight
+= m_viewOptions
.font
.pointSize() * 2;
362 if (settings
->arrangement() == QListView::TopToBottom
) {
363 // The decoration width indirectly defines the maximum
364 // width for the text wrapping. To use the maximum item width
365 // for text wrapping, it is used as decoration width.
366 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
368 m_viewOptions
.decorationSize
= QSize(size
, size
);
371 const int spacing
= settings
->gridSpacing();
372 setGridSize(QSize(itemWidth
+ spacing
, itemHeight
+ spacing
));
374 m_itemSize
= QSize(itemWidth
, itemHeight
);
376 m_controller
->setZoomInPossible(isZoomInPossible());
377 m_controller
->setZoomOutPossible(isZoomOutPossible());
380 #include "dolphiniconsview.moc"