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"
29 #include <kdirmodel.h>
31 #include <QAbstractProxyModel>
32 #include <QApplication>
36 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
37 KCategorizedView(parent
),
38 m_controller(controller
),
44 Q_ASSERT(controller
!= 0);
45 setViewMode(QListView::IconMode
);
46 setResizeMode(QListView::Adjust
);
47 setSpacing(KDialog::spacingHint());
48 setMouseTracking(true);
49 viewport()->setAttribute(Qt::WA_Hover
);
51 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
52 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
53 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
54 // RETURN-key in keyPressEvent().
55 if (KGlobalSettings::singleClick()) {
56 connect(this, SIGNAL(clicked(const QModelIndex
&)),
57 this, SLOT(triggerItem(const QModelIndex
&)));
59 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
60 this, SLOT(triggerItem(const QModelIndex
&)));
62 connect(this, SIGNAL(viewportEntered()),
63 controller
, SLOT(emitViewportEntered()));
64 connect(controller
, SIGNAL(zoomIn()),
65 this, SLOT(zoomIn()));
66 connect(controller
, SIGNAL(zoomOut()),
67 this, SLOT(zoomOut()));
69 const DolphinView
* view
= controller
->dolphinView();
70 connect(view
, SIGNAL(showPreviewChanged()),
71 this, SLOT(slotShowPreviewChanged()));
72 connect(view
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
73 this, SLOT(slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
&)));
75 connect(this, SIGNAL(entered(const QModelIndex
&)),
76 this, SLOT(slotEntered(const QModelIndex
&)));
78 // apply the icons mode settings to the widget
79 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
80 Q_ASSERT(settings
!= 0);
82 m_viewOptions
= KCategorizedView::viewOptions();
83 m_viewOptions
.showDecorationSelected
= true;
85 QFont
font(settings
->fontFamily(), settings
->fontSize());
86 font
.setItalic(settings
->italicFont());
87 font
.setBold(settings
->boldFont());
88 m_viewOptions
.font
= font
;
90 setWordWrap(settings
->numberOfTextlines() > 1);
91 updateGridSize(view
->showPreview(), 0);
93 if (settings
->arrangement() == QListView::TopToBottom
) {
94 setFlow(QListView::LeftToRight
);
95 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Top
;
97 setFlow(QListView::TopToBottom
);
98 m_viewOptions
.decorationPosition
= QStyleOptionViewItem::Left
;
99 m_viewOptions
.displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
102 m_categoryDrawer
= new DolphinCategoryDrawer();
103 setCategoryDrawer(m_categoryDrawer
);
108 DolphinIconsView::~DolphinIconsView()
110 delete m_categoryDrawer
;
111 m_categoryDrawer
= 0;
114 QRect
DolphinIconsView::visualRect(const QModelIndex
& index
) const
116 const bool leftToRightFlow
= (flow() == QListView::LeftToRight
);
118 QRect itemRect
= KCategorizedView::visualRect(index
);
119 const int maxWidth
= m_itemSize
.width();
120 const int maxHeight
= m_itemSize
.height();
122 if (itemRect
.width() > maxWidth
) {
123 // assure that the maximum item width is not exceeded
124 if (leftToRightFlow
) {
125 const int left
= itemRect
.left() + (itemRect
.width() - maxWidth
) / 2;
126 itemRect
.setLeft(left
);
128 itemRect
.setWidth(maxWidth
);
131 if (itemRect
.height() > maxHeight
) {
132 // assure that the maximum item height is not exceeded
133 if (!leftToRightFlow
) {
134 const int top
= itemRect
.top() + (itemRect
.height() - maxHeight
) / 2;
135 itemRect
.setTop(top
);
137 itemRect
.setHeight(maxHeight
);
143 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
145 return m_viewOptions
;
148 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
150 KCategorizedView::contextMenuEvent(event
);
151 m_controller
->triggerContextMenuRequest(event
->pos());
154 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
156 m_controller
->requestActivation();
157 if (!indexAt(event
->pos()).isValid()) {
158 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
159 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
164 KCategorizedView::mousePressEvent(event
);
167 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
169 if (event
->mimeData()->hasUrls()) {
170 event
->acceptProposedAction();
175 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
177 KCategorizedView::dragLeaveEvent(event
);
179 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
181 setDirtyRegion(m_dropRect
);
184 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
186 KCategorizedView::dragMoveEvent(event
);
188 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
189 const QModelIndex index
= indexAt(event
->pos());
190 setDirtyRegion(m_dropRect
);
191 if (itemForIndex(index
).isDir()) {
192 m_dropRect
= visualRect(index
);
194 m_dropRect
.setSize(QSize()); // set as invalid
196 setDirtyRegion(m_dropRect
);
199 void DolphinIconsView::dropEvent(QDropEvent
* event
)
201 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
202 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
203 if (!urls
.isEmpty()) {
204 const QModelIndex index
= indexAt(event
->pos());
205 const KFileItem item
= itemForIndex(index
);
206 m_controller
->indicateDroppedUrls(urls
,
209 event
->acceptProposedAction();
213 KCategorizedView::dropEvent(event
);
217 void DolphinIconsView::paintEvent(QPaintEvent
* event
)
219 KCategorizedView::paintEvent(event
);
221 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
223 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
224 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
228 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
230 KCategorizedView::keyPressEvent(event
);
232 const QItemSelectionModel
* selModel
= selectionModel();
233 const QModelIndex currentIndex
= selModel
->currentIndex();
234 const bool trigger
= currentIndex
.isValid()
235 && (event
->key() == Qt::Key_Return
)
236 && (selModel
->selectedIndexes().count() <= 1);
238 triggerItem(currentIndex
);
242 void DolphinIconsView::triggerItem(const QModelIndex
& index
)
244 m_controller
->triggerItem(itemForIndex(index
));
247 void DolphinIconsView::slotEntered(const QModelIndex
& index
)
249 m_controller
->emitItemEntered(itemForIndex(index
));
252 void DolphinIconsView::slotShowPreviewChanged()
254 const DolphinView
* view
= m_controller
->dolphinView();
255 const int infoCount
= view
->additionalInfo().count();
256 updateGridSize(view
->showPreview(), infoCount
);
259 void DolphinIconsView::slotAdditionalInfoChanged(const KFileItemDelegate::InformationList
& info
)
261 const bool showPreview
= m_controller
->dolphinView()->showPreview();
262 updateGridSize(showPreview
, info
.count());
265 void DolphinIconsView::zoomIn()
267 if (isZoomInPossible()) {
268 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
270 const int oldIconSize
= settings
->iconSize();
271 int newIconSize
= oldIconSize
;
273 const bool showPreview
= m_controller
->dolphinView()->showPreview();
275 const int previewSize
= increasedIconSize(settings
->previewSize());
276 settings
->setPreviewSize(previewSize
);
278 newIconSize
= increasedIconSize(oldIconSize
);
279 settings
->setIconSize(newIconSize
);
280 if (settings
->previewSize() < newIconSize
) {
281 // assure that the preview size is always >= the icon size
282 settings
->setPreviewSize(newIconSize
);
286 // increase also the grid size
287 const int diff
= newIconSize
- oldIconSize
;
288 settings
->setItemWidth(settings
->itemWidth() + diff
);
289 settings
->setItemHeight(settings
->itemHeight() + diff
);
291 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
292 updateGridSize(showPreview
, infoCount
);
296 void DolphinIconsView::zoomOut()
298 if (isZoomOutPossible()) {
299 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
301 const int oldIconSize
= settings
->iconSize();
302 int newIconSize
= oldIconSize
;
304 const bool showPreview
= m_controller
->dolphinView()->showPreview();
306 const int previewSize
= decreasedIconSize(settings
->previewSize());
307 settings
->setPreviewSize(previewSize
);
308 if (settings
->iconSize() > previewSize
) {
309 // assure that the icon size is always <= the preview size
310 newIconSize
= previewSize
;
311 settings
->setIconSize(newIconSize
);
314 newIconSize
= decreasedIconSize(settings
->iconSize());
315 settings
->setIconSize(newIconSize
);
318 // decrease also the grid size
319 const int diff
= oldIconSize
- newIconSize
;
320 settings
->setItemWidth(settings
->itemWidth() - diff
);
321 settings
->setItemHeight(settings
->itemHeight() - diff
);
323 const int infoCount
= m_controller
->dolphinView()->additionalInfo().count();
324 updateGridSize(showPreview
, infoCount
);
328 bool DolphinIconsView::isZoomInPossible() const
330 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
331 const bool showPreview
= m_controller
->dolphinView()->showPreview();
332 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
333 return size
< KIconLoader::SizeEnormous
;
336 bool DolphinIconsView::isZoomOutPossible() const
338 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
339 const bool showPreview
= m_controller
->dolphinView()->showPreview();
340 const int size
= showPreview
? settings
->previewSize() : settings
->iconSize();
341 return size
> KIconLoader::SizeSmall
;
344 int DolphinIconsView::increasedIconSize(int size
) const
348 case KIconLoader::SizeSmall
: incSize
= KIconLoader::SizeSmallMedium
; break;
349 case KIconLoader::SizeSmallMedium
: incSize
= KIconLoader::SizeMedium
; break;
350 case KIconLoader::SizeMedium
: incSize
= KIconLoader::SizeLarge
; break;
351 case KIconLoader::SizeLarge
: incSize
= KIconLoader::SizeHuge
; break;
352 case KIconLoader::SizeHuge
: incSize
= KIconLoader::SizeEnormous
; break;
353 default: Q_ASSERT(false); break;
358 int DolphinIconsView::decreasedIconSize(int size
) const
362 case KIconLoader::SizeSmallMedium
: decSize
= KIconLoader::SizeSmall
; break;
363 case KIconLoader::SizeMedium
: decSize
= KIconLoader::SizeSmallMedium
; break;
364 case KIconLoader::SizeLarge
: decSize
= KIconLoader::SizeMedium
; break;
365 case KIconLoader::SizeHuge
: decSize
= KIconLoader::SizeLarge
; break;
366 case KIconLoader::SizeEnormous
: decSize
= KIconLoader::SizeHuge
; break;
367 default: Q_ASSERT(false); break;
372 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
374 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
375 Q_ASSERT(settings
!= 0);
377 int itemWidth
= settings
->itemWidth();
378 int itemHeight
= settings
->itemHeight();
379 int size
= settings
->iconSize();
382 const int previewSize
= settings
->previewSize();
383 const int diff
= previewSize
- size
;
391 Q_ASSERT(additionalInfoCount
>= 0);
392 itemHeight
+= additionalInfoCount
* m_viewOptions
.font
.pointSize() * 2;
394 if (settings
->arrangement() == QListView::TopToBottom
) {
395 // The decoration width indirectly defines the maximum
396 // width for the text wrapping. To use the maximum item width
397 // for text wrapping, it is used as decoration width.
398 m_viewOptions
.decorationSize
= QSize(itemWidth
, size
);
400 m_viewOptions
.decorationSize
= QSize(size
, size
);
403 const int spacing
= settings
->gridSpacing();
404 setGridSize(QSize(itemWidth
+ spacing
, itemHeight
+ spacing
));
406 m_itemSize
= QSize(itemWidth
, itemHeight
);
408 m_controller
->setZoomInPossible(isZoomInPossible());
409 m_controller
->setZoomOutPossible(isZoomOutPossible());
412 KFileItem
DolphinIconsView::itemForIndex(const QModelIndex
& index
) const
414 QAbstractProxyModel
* proxyModel
= static_cast<QAbstractProxyModel
*>(model());
415 KDirModel
* dirModel
= static_cast<KDirModel
*>(proxyModel
->sourceModel());
416 const QModelIndex dirIndex
= proxyModel
->mapToSource(index
);
417 return dirModel
->itemForIndex(dirIndex
);
421 #include "dolphiniconsview.moc"