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"
30 #include <QAbstractProxyModel>
31 #include <QApplication>
35 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
36 KCategorizedView(parent
),
37 m_controller(controller
),
43 Q_ASSERT(controller
!= 0);
44 setViewMode(QListView::IconMode
);
45 setResizeMode(QListView::Adjust
);
46 setSpacing(KDialog::spacingHint());
47 setMouseTracking(true);
48 viewport()->setAttribute(Qt::WA_Hover
);
50 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
51 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
52 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
53 // RETURN-key in keyPressEvent().
54 if (KGlobalSettings::singleClick()) {
55 connect(this, SIGNAL(clicked(const QModelIndex
&)),
56 controller
, SLOT(triggerItem(const QModelIndex
&)));
58 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
59 controller
, SLOT(triggerItem(const QModelIndex
&)));
61 connect(this, SIGNAL(entered(const QModelIndex
&)),
62 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
63 connect(this, SIGNAL(viewportEntered()),
64 controller
, SLOT(emitViewportEntered()));
65 connect(controller
, SIGNAL(showPreviewChanged(bool)),
66 this, SLOT(slotShowPreviewChanged(bool)));
67 connect(controller
, SIGNAL(showAdditionalInfoChanged(bool)),
68 this, SLOT(slotShowAdditionalInfoChanged(bool)));
69 connect(controller
, SIGNAL(zoomIn()),
70 this, SLOT(zoomIn()));
71 connect(controller
, SIGNAL(zoomOut()),
72 this, SLOT(zoomOut()));
74 // apply the icons mode settings to the widget
75 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
76 Q_ASSERT(settings
!= 0);
78 m_viewOptions
= KCategorizedView::viewOptions();
79 m_viewOptions
.showDecorationSelected
= true;
81 QFont
font(settings
->fontFamily(), settings
->fontSize());
82 font
.setItalic(settings
->italicFont());
83 font
.setBold(settings
->boldFont());
84 m_viewOptions
.font
= font
;
86 setWordWrap(settings
->numberOfTextlines() > 1);
87 updateGridSize(controller
->showPreview(), controller
->showAdditionalInfo());
89 if (settings
->arrangement() == QListView::TopToBottom
) {
90 setFlow(QListView::LeftToRight
);
91 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
93 setFlow(QListView::TopToBottom
);
94 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
95 m_viewOptions
.displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
98 m_categoryDrawer
= new DolphinCategoryDrawer();
99 setCategoryDrawer(m_categoryDrawer
);
102 DolphinIconsView::~DolphinIconsView()
104 delete m_categoryDrawer
;
105 m_categoryDrawer
= 0;
108 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
110 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
112 QRect itemRect
= KCategorizedView::visualRect(index
);
113 const int maxWidth
= m_itemSize
.width();
114 const int maxHeight
= m_itemSize
.height();
116 if (itemRect
.width() > maxWidth
) {
117 // assure that the maximum item width is not exceeded
118 if (leftToRightFlow
) {
119 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
120 itemRect
.setLeft(left
);
122 itemRect
.setWidth(maxWidth
);
125 if (itemRect
.height() > maxHeight
) {
126 // assure that the maximum item height is not exceeded
127 if (!leftToRightFlow
) {
128 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
129 itemRect
.setTop(top
);
131 itemRect
.setHeight(maxHeight
);
137 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
139 return m_viewOptions
;
142 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
144 KCategorizedView::contextMenuEvent(event
);
145 m_controller
->triggerContextMenuRequest(event
->pos());
148 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
150 m_controller
->triggerActivation();
151 if (!indexAt(event
->pos()).isValid()) {
152 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
153 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
158 KCategorizedView::mousePressEvent(event
);
161 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
163 if (event
->mimeData()->hasUrls()) {
164 event
->acceptProposedAction();
169 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
171 KCategorizedView::dragLeaveEvent(event
);
173 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
175 setDirtyRegion(m_dropRect
);
178 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
180 KCategorizedView::dragMoveEvent(event
);
182 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
183 const QModelIndex index
= indexAt(event
->pos());
184 setDirtyRegion(m_dropRect
);
185 m_dropRect
= visualRect(index
);
186 setDirtyRegion(m_dropRect
);
189 void DolphinIconsView::dropEvent(QDropEvent
* event
)
191 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
192 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
193 if (!urls
.isEmpty()) {
194 m_controller
->indicateDroppedUrls(urls
,
196 indexAt(event
->pos()),
198 event
->acceptProposedAction();
202 KCategorizedView::dropEvent(event
);
206 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
208 KCategorizedView::paintEvent(event
);
210 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
212 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
213 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
217 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
219 KCategorizedView::keyPressEvent(event
);
221 const QItemSelectionModel
* selModel
= selectionModel();
222 const QModelIndex currentIndex
= selModel
->currentIndex();
223 const bool triggerItem
= currentIndex
.isValid()
224 && (event
->key() == Qt::Key_Return
)
225 && (selModel
->selectedIndexes().count() <= 1);
227 m_controller
->triggerItem(currentIndex
);
231 void DolphinIconsView::slotShowPreviewChanged(bool showPreview
)
233 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
236 void DolphinIconsView::slotShowAdditionalInfoChanged(bool showAdditionalInfo
)
238 updateGridSize(m_controller
->showPreview(), showAdditionalInfo
);
241 void DolphinIconsView::zoomIn()
243 if (isZoomInPossible()) {
244 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
246 const int oldIconSize
= settings
->iconSize();
247 int newIconSize
= oldIconSize
;
249 const bool showPreview
= m_controller
->showPreview();
251 const int previewSize
= increasedIconSize(settings
->previewSize());
252 settings
->setPreviewSize(previewSize
);
254 newIconSize
= increasedIconSize(oldIconSize
);
255 settings
->setIconSize(newIconSize
);
256 if (settings
->previewSize() < newIconSize
) {
257 // assure that the preview size is always >= the icon size
258 settings
->setPreviewSize(newIconSize
);
262 // increase also the grid size
263 const int diff
= newIconSize
- oldIconSize
;
264 settings
->setItemWidth(settings
->itemWidth() + diff
);
265 settings
->setItemHeight(settings
->itemHeight() + diff
);
267 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
271 void DolphinIconsView::zoomOut()
273 if (isZoomOutPossible()) {
274 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
276 const int oldIconSize
= settings
->iconSize();
277 int newIconSize
= oldIconSize
;
279 const bool showPreview
= m_controller
->showPreview();
281 const int previewSize
= decreasedIconSize(settings
->previewSize());
282 settings
->setPreviewSize(previewSize
);
283 if (settings
->iconSize() > previewSize
) {
284 // assure that the icon size is always <= the preview size
285 newIconSize
= previewSize
;
286 settings
->setIconSize(newIconSize
);
289 newIconSize
= decreasedIconSize(settings
->iconSize());
290 settings
->setIconSize(newIconSize
);
293 // decrease also the grid size
294 const int diff
= oldIconSize
- newIconSize
;
295 settings
->setItemWidth(settings
->itemWidth() - diff
);
296 settings
->setItemHeight(settings
->itemHeight() - diff
);
298 updateGridSize(showPreview
, m_controller
->showAdditionalInfo());
302 bool DolphinIconsView::isZoomInPossible() const
304 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
305 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
306 return size
< K3Icon::SizeEnormous
;
309 bool DolphinIconsView::isZoomOutPossible() const
311 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
312 const int size
= m_controller
->showPreview() ? settings
->previewSize() : settings
->iconSize();
313 return size
> K3Icon::SizeSmall
;
316 int DolphinIconsView::increasedIconSize(int size
) const
318 // TODO: get rid of K3Icon sizes
321 case K3Icon::SizeSmall
: incSize
= K3Icon::SizeSmallMedium
; break;
322 case K3Icon::SizeSmallMedium
: incSize
= K3Icon::SizeMedium
; break;
323 case K3Icon::SizeMedium
: incSize
= K3Icon::SizeLarge
; break;
324 case K3Icon::SizeLarge
: incSize
= K3Icon::SizeHuge
; break;
325 case K3Icon::SizeHuge
: incSize
= K3Icon::SizeEnormous
; break;
326 default: Q_ASSERT(false); break;
331 int DolphinIconsView::decreasedIconSize(int size
) const
333 // TODO: get rid of K3Icon sizes
336 case K3Icon::SizeSmallMedium
: decSize
= K3Icon::SizeSmall
; break;
337 case K3Icon::SizeMedium
: decSize
= K3Icon::SizeSmallMedium
; break;
338 case K3Icon::SizeLarge
: decSize
= K3Icon::SizeMedium
; break;
339 case K3Icon::SizeHuge
: decSize
= K3Icon::SizeLarge
; break;
340 case K3Icon::SizeEnormous
: decSize
= K3Icon::SizeHuge
; break;
341 default: Q_ASSERT(false); break;
346 void DolphinIconsView::updateGridSize(bool showPreview
, bool showAdditionalInfo
)
348 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
349 Q_ASSERT(settings
!= 0);
351 int itemWidth
= settings
->itemWidth();
352 int itemHeight
= settings
->itemHeight();
353 int size
= settings
->iconSize();
356 const int previewSize
= settings
->previewSize();
357 const int diff
= previewSize
- size
;
365 if (showAdditionalInfo
) {
366 itemHeight
+= m_viewOptions
.font
.pointSize() * 2;
369 if (settings
->arrangement() == QListView::TopToBottom
) {
370 // The decoration width indirectly defines the maximum
371 // width for the text wrapping. To use the maximum item width
372 // for text wrapping, it is used as decoration width.
373 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
375 m_viewOptions
.decorationSize
= QSize(size
, size
);
378 const int spacing
= settings
->gridSpacing();
379 setGridSize(QSize(itemWidth
+ spacing
, itemHeight
+ spacing
));
381 m_itemSize
= QSize(itemWidth
, itemHeight
);
383 m_controller
->setZoomInPossible(isZoomInPossible());
384 m_controller
->setZoomOutPossible(isZoomOutPossible());
387 #include "dolphiniconsview.moc"