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
) :
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 connect(this, SIGNAL(activated(const QModelIndex
&)),
48 controller
, SLOT(triggerItem(const QModelIndex
&)));
49 connect(this, SIGNAL(entered(const QModelIndex
&)),
50 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
51 connect(this, SIGNAL(viewportEntered()),
52 controller
, SLOT(emitViewportEntered()));
53 connect(controller
, SIGNAL(showPreviewChanged(bool)),
54 this, SLOT(slotShowPreviewChanged(bool)));
55 connect(controller
, SIGNAL(showAdditionalInfoChanged(bool)),
56 this, SLOT(slotShowAdditionalInfoChanged(bool)));
57 connect(controller
, SIGNAL(zoomIn()),
58 this, SLOT(zoomIn()));
59 connect(controller
, SIGNAL(zoomOut()),
60 this, SLOT(zoomOut()));
62 // apply the icons mode settings to the widget
63 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
64 Q_ASSERT(settings
!= 0);
66 m_viewOptions
= KListView::viewOptions();
67 m_viewOptions
.showDecorationSelected
= true;
69 QFont
font(settings
->fontFamily(), settings
->fontSize());
70 font
.setItalic(settings
->italicFont());
71 font
.setBold(settings
->boldFont());
72 m_viewOptions
.font
= font
;
74 setWordWrap(settings
->numberOfTextlines() > 1);
75 updateGridSize(controller
->showPreview(), controller
->showAdditionalInfo());
77 if (settings
->arrangement() == QListView::TopToBottom
) {
78 setFlow(QListView::LeftToRight
);
79 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
81 setFlow(QListView::TopToBottom
);
82 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
83 m_viewOptions
.displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
87 DolphinIconsView::~DolphinIconsView()
91 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
96 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
98 KListView::contextMenuEvent(event
);
99 m_controller
->triggerContextMenuRequest(event
->pos());
102 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
104 m_controller
->triggerActivation();
105 if (!indexAt(event
->pos()).isValid()) {
106 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
107 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
112 KListView::mousePressEvent(event
);
115 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
117 if (event
->mimeData()->hasUrls()) {
118 event
->acceptProposedAction();
123 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
125 KListView::dragLeaveEvent(event
);
127 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
129 setDirtyRegion(m_dropRect
);
132 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
134 KListView::dragMoveEvent(event
);
136 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
137 const QModelIndex index
= indexAt(event
->pos());
138 setDirtyRegion(m_dropRect
);
139 m_dropRect
= visualRect(index
);
140 setDirtyRegion(m_dropRect
);
143 void DolphinIconsView::dropEvent(QDropEvent
* event
)
145 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
146 if (!urls
.isEmpty()) {
147 m_controller
->indicateDroppedUrls(urls
,
148 indexAt(event
->pos()),
150 event
->acceptProposedAction();
152 KListView::dropEvent(event
);
156 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
158 KListView::paintEvent(event
);
160 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
162 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
163 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
167 void DolphinIconsView::slotShowPreviewChanged(bool showPreview
)
169 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
172 void DolphinIconsView::slotShowAdditionalInfoChanged(bool showAdditionalInfo
)
174 updateGridSize(m_controller
->showPreview(), showAdditionalInfo
);
177 void DolphinIconsView::zoomIn()
179 if (isZoomInPossible()) {
180 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
182 const int oldIconSize
= settings
->iconSize();
183 int newIconSize
= oldIconSize
;
185 const bool showPreview
= m_controller
->showPreview();
187 const int previewSize
= increasedIconSize(settings
->previewSize());
188 settings
->setPreviewSize(previewSize
);
190 newIconSize
= increasedIconSize(oldIconSize
);
191 settings
->setIconSize(newIconSize
);
192 if (settings
->previewSize() < newIconSize
) {
193 // assure that the preview size is always >= the icon size
194 settings
->setPreviewSize(newIconSize
);
198 // increase also the grid size
199 const int diff
= newIconSize
- oldIconSize
;
200 settings
->setItemWidth(settings
->itemWidth() + diff
);
201 settings
->setItemHeight(settings
->itemHeight() + diff
);
203 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
207 void DolphinIconsView::zoomOut()
209 if (isZoomOutPossible()) {
210 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
212 const int oldIconSize
= settings
->iconSize();
213 int newIconSize
= oldIconSize
;
215 const bool showPreview
= m_controller
->showPreview();
217 const int previewSize
= decreasedIconSize(settings
->previewSize());
218 settings
->setPreviewSize(previewSize
);
219 if (settings
->iconSize() > previewSize
) {
220 // assure that the icon size is always <= the preview size
221 newIconSize
= previewSize
;
222 settings
->setIconSize(newIconSize
);
225 newIconSize
= decreasedIconSize(settings
->iconSize());
226 settings
->setIconSize(newIconSize
);
229 // decrease also the grid size
230 const int diff
= oldIconSize
- newIconSize
;
231 settings
->setItemWidth(settings
->itemWidth() - diff
);
232 settings
->setItemHeight(settings
->itemHeight() - diff
);
234 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
238 bool DolphinIconsView::isZoomInPossible() const
240 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
241 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
242 return size
< K3Icon::SizeEnormous
;
245 bool DolphinIconsView::isZoomOutPossible() const
247 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
248 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
249 return size
> K3Icon::SizeSmall
;
252 int DolphinIconsView::increasedIconSize(int size
) const
254 // TODO: get rid of K3Icon sizes
257 case K3Icon::SizeSmall
: incSize
= K3Icon::SizeSmallMedium
; break;
258 case K3Icon::SizeSmallMedium
: incSize
= K3Icon::SizeMedium
; break;
259 case K3Icon::SizeMedium
: incSize
= K3Icon::SizeLarge
; break;
260 case K3Icon::SizeLarge
: incSize
= K3Icon::SizeHuge
; break;
261 case K3Icon::SizeHuge
: incSize
= K3Icon::SizeEnormous
; break;
262 default: Q_ASSERT(false); break;
267 int DolphinIconsView::decreasedIconSize(int size
) const
269 // TODO: get rid of K3Icon sizes
272 case K3Icon::SizeSmallMedium
: decSize
= K3Icon::SizeSmall
; break;
273 case K3Icon::SizeMedium
: decSize
= K3Icon::SizeSmallMedium
; break;
274 case K3Icon::SizeLarge
: decSize
= K3Icon::SizeMedium
; break;
275 case K3Icon::SizeHuge
: decSize
= K3Icon::SizeLarge
; break;
276 case K3Icon::SizeEnormous
: decSize
= K3Icon::SizeHuge
; break;
277 default: Q_ASSERT(false); break;
282 void DolphinIconsView::updateGridSize(bool showPreview
, bool showAdditionalInfo
)
284 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
285 Q_ASSERT(settings
!= 0);
287 int itemWidth
= settings
->itemWidth();
288 int itemHeight
= settings
->itemHeight();
289 int size
= settings
->iconSize();
292 const int previewSize
= settings
->previewSize();
293 const int diff
= previewSize
- size
;
301 if (showAdditionalInfo
) {
302 itemHeight
+= m_viewOptions
.font
.pointSize() * 2;
305 if (settings
->arrangement() == QListView::TopToBottom
) {
306 // The decoration width indirectly defines the maximum
307 // width for the text wrapping. To use the maximum item width
308 // for text wrapping, it is used as decoration width.
309 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
311 m_viewOptions
.decorationSize
= QSize(size
, size
);
314 const int spacing
= settings
->gridSpacing();
315 setGridSize(QSize(itemWidth
+ spacing
, itemHeight
+ spacing
));
317 m_controller
->setZoomInPossible(isZoomInPossible());
318 m_controller
->setZoomOutPossible(isZoomOutPossible());
321 #include "dolphiniconsview.moc"