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"
25 #include "dolphinviewautoscroller.h"
26 #include "dolphin_iconsmodesettings.h"
27 #include "dolphin_generalsettings.h"
28 #include "draganddrophelper.h"
29 #include "selectionmanager.h"
30 #include "zoomlevelinfo.h"
32 #include <kcategorizedsortfilterproxymodel.h>
34 #include <kdirmodel.h>
35 #include <kfileitemdelegate.h>
37 #include <QAbstractProxyModel>
38 #include <QApplication>
41 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
42 KCategorizedView(parent
),
43 m_enableScrollTo(false),
44 m_controller(controller
),
45 m_selectionManager(0),
50 m_decorationPosition(QStyleOptionViewItem::Top
),
51 m_displayAlignment(Qt::AlignHCenter
),
55 Q_ASSERT(controller
!= 0);
56 setLayoutDirection(Qt::LeftToRight
);
57 setViewMode(QListView::IconMode
);
58 setResizeMode(QListView::Adjust
);
59 setSpacing(KDialog::spacingHint());
60 setMovement(QListView::Static
);
62 setEditTriggers(QAbstractItemView::NoEditTriggers
);
63 viewport()->setAcceptDrops(true);
65 setMouseTracking(true);
66 m_autoScroller
= new DolphinViewAutoScroller(this);
68 connect(this, SIGNAL(clicked(const QModelIndex
&)),
69 controller
, SLOT(requestTab(const QModelIndex
&)));
70 if (KGlobalSettings::singleClick()) {
71 connect(this, SIGNAL(clicked(const QModelIndex
&)),
72 controller
, SLOT(triggerItem(const QModelIndex
&)));
74 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
75 controller
, SLOT(triggerItem(const QModelIndex
&)));
78 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
79 m_selectionManager
= new SelectionManager(this);
80 connect(m_selectionManager
, SIGNAL(selectionChanged()),
81 this, SLOT(requestActivation()));
82 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
83 m_selectionManager
, SLOT(reset()));
86 connect(this, SIGNAL(entered(const QModelIndex
&)),
87 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
88 connect(this, SIGNAL(viewportEntered()),
89 controller
, SLOT(emitViewportEntered()));
90 connect(controller
, SIGNAL(zoomLevelChanged(int)),
91 this, SLOT(setZoomLevel(int)));
93 const DolphinView
* view
= controller
->dolphinView();
94 connect(view
, SIGNAL(showPreviewChanged()),
95 this, SLOT(slotShowPreviewChanged()));
96 connect(view
, SIGNAL(additionalInfoChanged()),
97 this, SLOT(slotAdditionalInfoChanged()));
99 // apply the icons mode settings to the widget
100 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
101 Q_ASSERT(settings
!= 0);
103 if (settings
->useSystemFont()) {
104 m_font
= KGlobalSettings::generalFont();
106 m_font
= QFont(settings
->fontFamily(),
107 settings
->fontSize(),
108 settings
->fontWeight(),
109 settings
->italicFont());
112 setWordWrap(settings
->numberOfTextlines() > 1);
113 updateGridSize(view
->showPreview(), 0);
115 if (settings
->arrangement() == QListView::TopToBottom
) {
116 setFlow(QListView::LeftToRight
);
117 m_decorationPosition
= QStyleOptionViewItem::Top
;
118 m_displayAlignment
= Qt::AlignHCenter
;
120 setFlow(QListView::TopToBottom
);
121 m_decorationPosition
= QStyleOptionViewItem::Left
;
122 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
125 m_categoryDrawer
= new DolphinCategoryDrawer();
126 setCategoryDrawer(m_categoryDrawer
);
130 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
131 this, SLOT(updateFont()));
134 DolphinIconsView::~DolphinIconsView()
136 delete m_categoryDrawer
;
137 m_categoryDrawer
= 0;
140 void DolphinIconsView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
142 // Enable the QListView implementation of scrollTo() only if it has been
143 // triggered by a key press. Otherwise QAbstractItemView wants to scroll to the current
144 // index each time the layout has been changed. This becomes an issue when
145 // previews are loaded and the scrollbar is used: the scrollbar will always
146 // be reset to 0 on each new preview.
147 if (m_enableScrollTo
|| (state() != QAbstractItemView::NoState
)) {
148 KCategorizedView::scrollTo(index
, hint
);
149 m_enableScrollTo
= false;
153 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
155 KCategorizedView::dataChanged(topLeft
, bottomRight
);
157 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
158 if (!proxyModel
->isCategorizedModel()) {
159 // bypass a QListView issue that items are not layout correctly if the decoration size of
161 scheduleDelayedItemsLayout();
165 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
167 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
168 viewOptions
.font
= m_font
;
169 viewOptions
.decorationPosition
= m_decorationPosition
;
170 viewOptions
.decorationSize
= m_decorationSize
;
171 viewOptions
.displayAlignment
= m_displayAlignment
;
172 viewOptions
.showDecorationSelected
= true;
176 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
178 KCategorizedView::contextMenuEvent(event
);
179 m_controller
->triggerContextMenuRequest(event
->pos());
182 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
184 m_controller
->requestActivation();
185 const QModelIndex index
= indexAt(event
->pos());
186 if (index
.isValid() && (event
->button() == Qt::LeftButton
)) {
187 // TODO: It should not be necessary to manually set the dragging state, but I could
188 // not reproduce this issue with a Qt-only example yet to find the root cause.
189 // Issue description: start Dolphin, split the view and drag an item from the
190 // inactive view to the active view by a very fast mouse movement. Result:
191 // the item gets selected instead of being dragged...
192 setState(QAbstractItemView::DraggingState
);
195 if (!index
.isValid()) {
196 if (QApplication::mouseButtons() & Qt::MidButton
) {
197 m_controller
->replaceUrlByClipboard();
199 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
200 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
205 KCategorizedView::mousePressEvent(event
);
208 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
210 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
211 // fix this in KDE 4.1
212 KCategorizedView::startDrag(supportedActions
);
213 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
216 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
218 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
219 event
->acceptProposedAction();
223 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
225 KCategorizedView::dragLeaveEvent(event
);
226 setDirtyRegion(m_dropRect
);
229 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
231 KCategorizedView::dragMoveEvent(event
);
233 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
234 const QModelIndex index
= indexAt(event
->pos());
235 setDirtyRegion(m_dropRect
);
237 m_dropRect
.setSize(QSize()); // set as invalid
238 if (index
.isValid()) {
239 const KFileItem item
= m_controller
->itemForIndex(index
);
240 if (!item
.isNull() && item
.isDir()) {
241 m_dropRect
= visualRect(index
);
243 m_dropRect
.setSize(QSize()); // set as invalid
246 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
247 // accept url drops, independently from the destination item
248 event
->acceptProposedAction();
251 setDirtyRegion(m_dropRect
);
254 void DolphinIconsView::dropEvent(QDropEvent
* event
)
256 const QModelIndex index
= indexAt(event
->pos());
257 const KFileItem item
= m_controller
->itemForIndex(index
);
258 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
260 KCategorizedView::dropEvent(event
);
263 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
265 KCategorizedView::keyPressEvent(event
);
266 m_controller
->handleKeyPressEvent(event
);
267 m_enableScrollTo
= true; // see DolphinIconsView::scrollTo()
270 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
272 if (m_selectionManager
!= 0) {
273 m_selectionManager
->reset();
276 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
277 if (event
->modifiers() & Qt::ControlModifier
) {
282 horizontalScrollBar()->setSingleStep(m_itemSize
.width() / 10);
283 verticalScrollBar()->setSingleStep(m_itemSize
.height() / 10);
285 KCategorizedView::wheelEvent(event
);
286 // if the icons are aligned left to right, the vertical wheel event should
287 // be applied to the horizontal scrollbar
288 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
289 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
290 (settings
->arrangement() == QListView::LeftToRight
);
291 if (scrollHorizontal
) {
292 QWheelEvent
horizEvent(event
->pos(),
297 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
301 void DolphinIconsView::showEvent(QShowEvent
* event
)
303 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
304 delegate
->setMaximumSize(m_itemSize
);
306 KCategorizedView::showEvent(event
);
309 void DolphinIconsView::leaveEvent(QEvent
* event
)
311 KCategorizedView::leaveEvent(event
);
312 // if the mouse is above an item and moved very fast outside the widget,
313 // no viewportEntered() signal might be emitted although the mouse has been moved
314 // above the viewport
315 m_controller
->emitViewportEntered();
318 void DolphinIconsView::currentChanged(const QModelIndex
& current
, const QModelIndex
& previous
)
320 KCategorizedView::currentChanged(current
, previous
);
321 if (current
.isValid() && !m_autoScroller
->isActive()) {
326 void DolphinIconsView::slotShowPreviewChanged()
328 const DolphinView
* view
= m_controller
->dolphinView();
329 updateGridSize(view
->showPreview(), additionalInfoCount());
332 void DolphinIconsView::slotAdditionalInfoChanged()
334 const DolphinView
* view
= m_controller
->dolphinView();
335 const bool showPreview
= view
->showPreview();
336 updateGridSize(showPreview
, view
->additionalInfo().count());
339 void DolphinIconsView::setZoomLevel(int level
)
341 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
343 const int oldIconSize
= settings
->iconSize();
344 int newIconSize
= oldIconSize
;
346 const bool showPreview
= m_controller
->dolphinView()->showPreview();
348 const int previewSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
349 settings
->setPreviewSize(previewSize
);
351 newIconSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
352 settings
->setIconSize(newIconSize
);
355 // increase also the grid size
356 const int diff
= newIconSize
- oldIconSize
;
357 settings
->setItemWidth(settings
->itemWidth() + diff
);
358 settings
->setItemHeight(settings
->itemHeight() + diff
);
360 updateGridSize(showPreview
, additionalInfoCount());
363 void DolphinIconsView::requestActivation()
365 m_controller
->requestActivation();
368 void DolphinIconsView::updateFont()
370 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
371 Q_ASSERT(settings
!= 0);
373 if (settings
->useSystemFont()) {
374 m_font
= KGlobalSettings::generalFont();
378 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
380 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
381 Q_ASSERT(settings
!= 0);
383 int itemWidth
= settings
->itemWidth();
384 int itemHeight
= settings
->itemHeight();
385 int size
= settings
->iconSize();
388 const int previewSize
= settings
->previewSize();
389 const int diff
= previewSize
- size
;
396 Q_ASSERT(additionalInfoCount
>= 0);
397 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
399 if (settings
->arrangement() == QListView::TopToBottom
) {
400 // The decoration width indirectly defines the maximum
401 // width for the text wrapping. To use the maximum item width
402 // for text wrapping, it is used as decoration width.
403 m_decorationSize
= QSize(itemWidth
, size
);
404 setIconSize(QSize(itemWidth
, size
));
406 m_decorationSize
= QSize(size
, size
);
407 setIconSize(QSize(size
, size
));
410 m_itemSize
= QSize(itemWidth
, itemHeight
);
412 const int spacing
= settings
->gridSpacing();
413 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
415 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
417 delegate
->setMaximumSize(m_itemSize
);
420 if (m_selectionManager
!= 0) {
421 m_selectionManager
->reset();
425 int DolphinIconsView::additionalInfoCount() const
427 const DolphinView
* view
= m_controller
->dolphinView();
428 return view
->additionalInfo().count();
431 #include "dolphiniconsview.moc"