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"
24 #include "dolphinitemcategorizer.h"
26 #include "dolphin_iconsmodesettings.h"
30 #include <QAbstractProxyModel>
31 #include <QApplication>
35 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
36 KCategorizedView(parent
),
37 m_controller(controller
),
42 Q_ASSERT(controller
!= 0);
43 setViewMode(QListView::IconMode
);
44 setResizeMode(QListView::Adjust
);
45 setSpacing(KDialog::spacingHint());
46 setMouseTracking(true);
47 viewport()->setAttribute(Qt::WA_Hover
);
49 if (KGlobalSettings::singleClick()) {
50 connect(this, SIGNAL(clicked(const QModelIndex
&)),
51 controller
, SLOT(triggerItem(const QModelIndex
&)));
53 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
54 controller
, SLOT(triggerItem(const QModelIndex
&)));
56 connect(this, SIGNAL(entered(const QModelIndex
&)),
57 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
58 connect(this, SIGNAL(viewportEntered()),
59 controller
, SLOT(emitViewportEntered()));
60 connect(controller
, SIGNAL(showPreviewChanged(bool)),
61 this, SLOT(slotShowPreviewChanged(bool)));
62 connect(controller
, SIGNAL(showAdditionalInfoChanged(bool)),
63 this, SLOT(slotShowAdditionalInfoChanged(bool)));
64 connect(controller
, SIGNAL(zoomIn()),
65 this, SLOT(zoomIn()));
66 connect(controller
, SIGNAL(zoomOut()),
67 this, SLOT(zoomOut()));
69 // apply the icons mode settings to the widget
70 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
71 Q_ASSERT(settings
!= 0);
73 m_viewOptions
= KCategorizedView::viewOptions();
74 m_viewOptions
.showDecorationSelected
= true;
76 QFont
font(settings
->fontFamily(), settings
->fontSize());
77 font
.setItalic(settings
->italicFont());
78 font
.setBold(settings
->boldFont());
79 m_viewOptions
.font
= font
;
81 setWordWrap(settings
->numberOfTextlines() > 1);
82 updateGridSize(controller
->showPreview(), controller
->showAdditionalInfo());
84 if (settings
->arrangement() == QListView::TopToBottom
) {
85 setFlow(QListView::LeftToRight
);
86 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
88 setFlow(QListView::TopToBottom
);
89 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
90 m_viewOptions
.displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
94 DolphinIconsView::~DolphinIconsView()
98 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
100 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
102 QRect itemRect
= KCategorizedView::visualRect(index
);
103 const int maxWidth
= m_itemSize
.width();
104 const int maxHeight
= m_itemSize
.height();
106 if (itemRect
.width() > maxWidth
) {
107 // assure that the maximum item width is not exceeded
108 if (leftToRightFlow
) {
109 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
110 itemRect
.setLeft(left
);
112 itemRect
.setWidth(maxWidth
);
115 if (itemRect
.height() > maxHeight
) {
116 // assure that the maximum item height is not exceeded
117 if (!leftToRightFlow
) {
118 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
119 itemRect
.setTop(top
);
121 itemRect
.setHeight(maxHeight
);
127 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
129 return m_viewOptions
;
132 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
134 KCategorizedView::contextMenuEvent(event
);
135 m_controller
->triggerContextMenuRequest(event
->pos());
138 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
140 m_controller
->triggerActivation();
141 if (!indexAt(event
->pos()).isValid()) {
142 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
143 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
148 KCategorizedView::mousePressEvent(event
);
151 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
153 if (event
->mimeData()->hasUrls()) {
154 event
->acceptProposedAction();
159 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
161 KCategorizedView::dragLeaveEvent(event
);
163 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
165 setDirtyRegion(m_dropRect
);
168 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
170 KCategorizedView::dragMoveEvent(event
);
172 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
173 const QModelIndex index
= indexAt(event
->pos());
174 setDirtyRegion(m_dropRect
);
175 m_dropRect
= visualRect(index
);
176 setDirtyRegion(m_dropRect
);
179 void DolphinIconsView::dropEvent(QDropEvent
* event
)
181 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
182 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
183 if (!urls
.isEmpty()) {
184 m_controller
->indicateDroppedUrls(urls
,
185 indexAt(event
->pos()),
187 event
->acceptProposedAction();
190 KCategorizedView::dropEvent(event
);
194 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
196 KCategorizedView::paintEvent(event
);
198 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
200 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
201 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
205 void DolphinIconsView::slotShowPreviewChanged(bool showPreview
)
207 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
210 void DolphinIconsView::slotShowAdditionalInfoChanged(bool showAdditionalInfo
)
212 updateGridSize(m_controller
->showPreview(), showAdditionalInfo
);
215 void DolphinIconsView::zoomIn()
217 if (isZoomInPossible()) {
218 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
220 const int oldIconSize
= settings
->iconSize();
221 int newIconSize
= oldIconSize
;
223 const bool showPreview
= m_controller
->showPreview();
225 const int previewSize
= increasedIconSize(settings
->previewSize());
226 settings
->setPreviewSize(previewSize
);
228 newIconSize
= increasedIconSize(oldIconSize
);
229 settings
->setIconSize(newIconSize
);
230 if (settings
->previewSize() < newIconSize
) {
231 // assure that the preview size is always >= the icon size
232 settings
->setPreviewSize(newIconSize
);
236 // increase also the grid size
237 const int diff
= newIconSize
- oldIconSize
;
238 settings
->setItemWidth(settings
->itemWidth() + diff
);
239 settings
->setItemHeight(settings
->itemHeight() + diff
);
241 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
245 void DolphinIconsView::zoomOut()
247 if (isZoomOutPossible()) {
248 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
250 const int oldIconSize
= settings
->iconSize();
251 int newIconSize
= oldIconSize
;
253 const bool showPreview
= m_controller
->showPreview();
255 const int previewSize
= decreasedIconSize(settings
->previewSize());
256 settings
->setPreviewSize(previewSize
);
257 if (settings
->iconSize() > previewSize
) {
258 // assure that the icon size is always <= the preview size
259 newIconSize
= previewSize
;
260 settings
->setIconSize(newIconSize
);
263 newIconSize
= decreasedIconSize(settings
->iconSize());
264 settings
->setIconSize(newIconSize
);
267 // decrease also the grid size
268 const int diff
= oldIconSize
- newIconSize
;
269 settings
->setItemWidth(settings
->itemWidth() - diff
);
270 settings
->setItemHeight(settings
->itemHeight() - diff
);
272 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
276 bool DolphinIconsView::isZoomInPossible() const
278 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
279 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
280 return size
< K3Icon::SizeEnormous
;
283 bool DolphinIconsView::isZoomOutPossible() const
285 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
286 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
287 return size
> K3Icon::SizeSmall
;
290 int DolphinIconsView::increasedIconSize(int size
) const
292 // TODO: get rid of K3Icon sizes
295 case K3Icon::SizeSmall
: incSize
= K3Icon::SizeSmallMedium
; break;
296 case K3Icon::SizeSmallMedium
: incSize
= K3Icon::SizeMedium
; break;
297 case K3Icon::SizeMedium
: incSize
= K3Icon::SizeLarge
; break;
298 case K3Icon::SizeLarge
: incSize
= K3Icon::SizeHuge
; break;
299 case K3Icon::SizeHuge
: incSize
= K3Icon::SizeEnormous
; break;
300 default: Q_ASSERT(false); break;
305 int DolphinIconsView::decreasedIconSize(int size
) const
307 // TODO: get rid of K3Icon sizes
310 case K3Icon::SizeSmallMedium
: decSize
= K3Icon::SizeSmall
; break;
311 case K3Icon::SizeMedium
: decSize
= K3Icon::SizeSmallMedium
; break;
312 case K3Icon::SizeLarge
: decSize
= K3Icon::SizeMedium
; break;
313 case K3Icon::SizeHuge
: decSize
= K3Icon::SizeLarge
; break;
314 case K3Icon::SizeEnormous
: decSize
= K3Icon::SizeHuge
; break;
315 default: Q_ASSERT(false); break;
320 void DolphinIconsView::updateGridSize(bool showPreview
, bool showAdditionalInfo
)
322 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
323 Q_ASSERT(settings
!= 0);
325 int itemWidth
= settings
->itemWidth();
326 int itemHeight
= settings
->itemHeight();
327 int size
= settings
->iconSize();
330 const int previewSize
= settings
->previewSize();
331 const int diff
= previewSize
- size
;
339 if (showAdditionalInfo
) {
340 itemHeight
+= m_viewOptions
.font
.pointSize() * 2;
343 if (settings
->arrangement() == QListView::TopToBottom
) {
344 // The decoration width indirectly defines the maximum
345 // width for the text wrapping. To use the maximum item width
346 // for text wrapping, it is used as decoration width.
347 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
349 m_viewOptions
.decorationSize
= QSize(size
, size
);
352 const int spacing
= settings
->gridSpacing();
353 setGridSize(QSize(itemWidth
+ spacing
, itemHeight
+ spacing
));
355 m_itemSize
= QSize(itemWidth
, itemHeight
);
357 m_controller
->setZoomInPossible(isZoomInPossible());
358 m_controller
->setZoomOutPossible(isZoomOutPossible());
361 #include "dolphiniconsview.moc"