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
),
40 Q_ASSERT(controller
!= 0);
41 setViewMode(QListView::IconMode
);
42 setResizeMode(QListView::Adjust
);
43 setSpacing(KDialog::spacingHint());
44 setMouseTracking(true);
45 viewport()->setAttribute(Qt::WA_Hover
);
47 if (KGlobalSettings::singleClick()) {
48 connect(this, SIGNAL(clicked(const QModelIndex
&)),
49 controller
, SLOT(triggerItem(const QModelIndex
&)));
51 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
52 controller
, SLOT(triggerItem(const QModelIndex
&)));
54 connect(this, SIGNAL(entered(const QModelIndex
&)),
55 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
56 connect(this, SIGNAL(viewportEntered()),
57 controller
, SLOT(emitViewportEntered()));
58 connect(controller
, SIGNAL(showPreviewChanged(bool)),
59 this, SLOT(slotShowPreviewChanged(bool)));
60 connect(controller
, SIGNAL(showAdditionalInfoChanged(bool)),
61 this, SLOT(slotShowAdditionalInfoChanged(bool)));
62 connect(controller
, SIGNAL(zoomIn()),
63 this, SLOT(zoomIn()));
64 connect(controller
, SIGNAL(zoomOut()),
65 this, SLOT(zoomOut()));
67 // apply the icons mode settings to the widget
68 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
69 Q_ASSERT(settings
!= 0);
71 m_viewOptions
= KCategorizedView::viewOptions();
72 m_viewOptions
.showDecorationSelected
= true;
74 QFont
font(settings
->fontFamily(), settings
->fontSize());
75 font
.setItalic(settings
->italicFont());
76 font
.setBold(settings
->boldFont());
77 m_viewOptions
.font
= font
;
79 setWordWrap(settings
->numberOfTextlines() > 1);
80 updateGridSize(controller
->showPreview(), controller
->showAdditionalInfo());
82 if (settings
->arrangement() == QListView::TopToBottom
) {
83 setFlow(QListView::LeftToRight
);
84 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
86 setFlow(QListView::TopToBottom
);
87 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
88 m_viewOptions
.displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
92 DolphinIconsView::~DolphinIconsView()
96 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
101 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
103 KCategorizedView::contextMenuEvent(event
);
104 m_controller
->triggerContextMenuRequest(event
->pos());
107 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
109 m_controller
->triggerActivation();
110 if (!indexAt(event
->pos()).isValid()) {
111 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
112 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
117 KCategorizedView::mousePressEvent(event
);
120 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
122 if (event
->mimeData()->hasUrls()) {
123 event
->acceptProposedAction();
128 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
130 KCategorizedView::dragLeaveEvent(event
);
132 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
134 setDirtyRegion(m_dropRect
);
137 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
139 KCategorizedView::dragMoveEvent(event
);
141 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
142 const QModelIndex index
= indexAt(event
->pos());
143 setDirtyRegion(m_dropRect
);
144 m_dropRect
= visualRect(index
);
145 setDirtyRegion(m_dropRect
);
148 void DolphinIconsView::dropEvent(QDropEvent
* event
)
150 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
151 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
152 if (!urls
.isEmpty()) {
153 m_controller
->indicateDroppedUrls(urls
,
154 indexAt(event
->pos()),
156 event
->acceptProposedAction();
159 KCategorizedView::dropEvent(event
);
163 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
165 KCategorizedView::paintEvent(event
);
167 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
169 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
170 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
174 void DolphinIconsView::slotShowPreviewChanged(bool showPreview
)
176 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
179 void DolphinIconsView::slotShowAdditionalInfoChanged(bool showAdditionalInfo
)
181 updateGridSize(m_controller
->showPreview(), showAdditionalInfo
);
184 void DolphinIconsView::zoomIn()
186 if (isZoomInPossible()) {
187 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
189 const int oldIconSize
= settings
->iconSize();
190 int newIconSize
= oldIconSize
;
192 const bool showPreview
= m_controller
->showPreview();
194 const int previewSize
= increasedIconSize(settings
->previewSize());
195 settings
->setPreviewSize(previewSize
);
197 newIconSize
= increasedIconSize(oldIconSize
);
198 settings
->setIconSize(newIconSize
);
199 if (settings
->previewSize() < newIconSize
) {
200 // assure that the preview size is always >= the icon size
201 settings
->setPreviewSize(newIconSize
);
205 // increase also the grid size
206 const int diff
= newIconSize
- oldIconSize
;
207 settings
->setItemWidth(settings
->itemWidth() + diff
);
208 settings
->setItemHeight(settings
->itemHeight() + diff
);
210 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
214 void DolphinIconsView::zoomOut()
216 if (isZoomOutPossible()) {
217 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
219 const int oldIconSize
= settings
->iconSize();
220 int newIconSize
= oldIconSize
;
222 const bool showPreview
= m_controller
->showPreview();
224 const int previewSize
= decreasedIconSize(settings
->previewSize());
225 settings
->setPreviewSize(previewSize
);
226 if (settings
->iconSize() > previewSize
) {
227 // assure that the icon size is always <= the preview size
228 newIconSize
= previewSize
;
229 settings
->setIconSize(newIconSize
);
232 newIconSize
= decreasedIconSize(settings
->iconSize());
233 settings
->setIconSize(newIconSize
);
236 // decrease also the grid size
237 const int diff
= oldIconSize
- newIconSize
;
238 settings
->setItemWidth(settings
->itemWidth() - diff
);
239 settings
->setItemHeight(settings
->itemHeight() - diff
);
241 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
245 bool DolphinIconsView::isZoomInPossible() const
247 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
248 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
249 return size
< K3Icon::SizeEnormous
;
252 bool DolphinIconsView::isZoomOutPossible() const
254 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
255 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
256 return size
> K3Icon::SizeSmall
;
259 int DolphinIconsView::increasedIconSize(int size
) const
261 // TODO: get rid of K3Icon sizes
264 case K3Icon::SizeSmall
: incSize
= K3Icon::SizeSmallMedium
; break;
265 case K3Icon::SizeSmallMedium
: incSize
= K3Icon::SizeMedium
; break;
266 case K3Icon::SizeMedium
: incSize
= K3Icon::SizeLarge
; break;
267 case K3Icon::SizeLarge
: incSize
= K3Icon::SizeHuge
; break;
268 case K3Icon::SizeHuge
: incSize
= K3Icon::SizeEnormous
; break;
269 default: Q_ASSERT(false); break;
274 int DolphinIconsView::decreasedIconSize(int size
) const
276 // TODO: get rid of K3Icon sizes
279 case K3Icon::SizeSmallMedium
: decSize
= K3Icon::SizeSmall
; break;
280 case K3Icon::SizeMedium
: decSize
= K3Icon::SizeSmallMedium
; break;
281 case K3Icon::SizeLarge
: decSize
= K3Icon::SizeMedium
; break;
282 case K3Icon::SizeHuge
: decSize
= K3Icon::SizeLarge
; break;
283 case K3Icon::SizeEnormous
: decSize
= K3Icon::SizeHuge
; break;
284 default: Q_ASSERT(false); break;
289 void DolphinIconsView::updateGridSize(bool showPreview
, bool showAdditionalInfo
)
291 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
292 Q_ASSERT(settings
!= 0);
294 int itemWidth
= settings
->itemWidth();
295 int itemHeight
= settings
->itemHeight();
296 int size
= settings
->iconSize();
299 const int previewSize
= settings
->previewSize();
300 const int diff
= previewSize
- size
;
308 if (showAdditionalInfo
) {
309 itemHeight
+= m_viewOptions
.font
.pointSize() * 2;
312 if (settings
->arrangement() == QListView::TopToBottom
) {
313 // The decoration width indirectly defines the maximum
314 // width for the text wrapping. To use the maximum item width
315 // for text wrapping, it is used as decoration width.
316 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
318 m_viewOptions
.decorationSize
= QSize(size
, size
);
321 const int spacing
= settings
->gridSpacing();
322 setGridSize(QSize(itemWidth
+ spacing
, itemHeight
+ spacing
));
324 m_controller
->setZoomInPossible(isZoomInPossible());
325 m_controller
->setZoomOutPossible(isZoomOutPossible());
328 #include "dolphiniconsview.moc"