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 "dolphin_iconsmodesettings.h"
26 #include "dolphin_generalsettings.h"
27 #include "draganddrophelper.h"
28 #include "selectionmanager.h"
29 #include "zoomlevelinfo.h"
31 #include <kcategorizedsortfilterproxymodel.h>
33 #include <kdirmodel.h>
34 #include <kfileitemdelegate.h>
36 #include <QAbstractProxyModel>
37 #include <QApplication>
40 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
41 KCategorizedView(parent
),
42 m_enableScrollTo(false),
43 m_controller(controller
),
44 m_selectionManager(0),
48 m_decorationPosition(QStyleOptionViewItem::Top
),
49 m_displayAlignment(Qt::AlignHCenter
),
53 Q_ASSERT(controller
!= 0);
54 setLayoutDirection(Qt::LeftToRight
);
55 setViewMode(QListView::IconMode
);
56 setResizeMode(QListView::Adjust
);
57 setSpacing(KDialog::spacingHint());
58 setMovement(QListView::Static
);
60 setEditTriggers(QAbstractItemView::NoEditTriggers
);
61 viewport()->setAcceptDrops(true);
63 setMouseTracking(true);
65 connect(this, SIGNAL(clicked(const QModelIndex
&)),
66 controller
, SLOT(requestTab(const QModelIndex
&)));
67 if (KGlobalSettings::singleClick()) {
68 connect(this, SIGNAL(clicked(const QModelIndex
&)),
69 controller
, SLOT(triggerItem(const QModelIndex
&)));
71 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
72 controller
, SLOT(triggerItem(const QModelIndex
&)));
75 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
76 m_selectionManager
= new SelectionManager(this);
77 connect(m_selectionManager
, SIGNAL(selectionChanged()),
78 this, SLOT(requestActivation()));
79 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
80 m_selectionManager
, SLOT(reset()));
83 connect(this, SIGNAL(entered(const QModelIndex
&)),
84 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
85 connect(this, SIGNAL(viewportEntered()),
86 controller
, SLOT(emitViewportEntered()));
87 connect(controller
, SIGNAL(zoomLevelChanged(int)),
88 this, SLOT(setZoomLevel(int)));
90 const DolphinView
* view
= controller
->dolphinView();
91 connect(view
, SIGNAL(showPreviewChanged()),
92 this, SLOT(slotShowPreviewChanged()));
93 connect(view
, SIGNAL(additionalInfoChanged()),
94 this, SLOT(slotAdditionalInfoChanged()));
96 // apply the icons mode settings to the widget
97 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
98 Q_ASSERT(settings
!= 0);
100 if (settings
->useSystemFont()) {
101 m_font
= KGlobalSettings::generalFont();
103 m_font
= QFont(settings
->fontFamily(),
104 settings
->fontSize(),
105 settings
->fontWeight(),
106 settings
->italicFont());
109 setWordWrap(settings
->numberOfTextlines() > 1);
110 updateGridSize(view
->showPreview(), 0);
112 if (settings
->arrangement() == QListView::TopToBottom
) {
113 setFlow(QListView::LeftToRight
);
114 m_decorationPosition
= QStyleOptionViewItem::Top
;
115 m_displayAlignment
= Qt::AlignHCenter
;
117 setFlow(QListView::TopToBottom
);
118 m_decorationPosition
= QStyleOptionViewItem::Left
;
119 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
122 m_categoryDrawer
= new DolphinCategoryDrawer();
123 setCategoryDrawer(m_categoryDrawer
);
127 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
128 this, SLOT(updateFont()));
131 DolphinIconsView::~DolphinIconsView()
133 delete m_categoryDrawer
;
134 m_categoryDrawer
= 0;
137 void DolphinIconsView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
139 // Enable the QListView implementation of scrollTo() only if it has been
140 // triggered by a key press. Otherwise QAbstractItemView wants to scroll to the current
141 // index each time the layout has been changed. This becomes an issue when
142 // previews are loaded and the scrollbar is used: the scrollbar will always
143 // be reset to 0 on each new preview.
144 if (m_enableScrollTo
|| (state() != QAbstractItemView::NoState
)) {
145 KCategorizedView::scrollTo(index
, hint
);
146 m_enableScrollTo
= false;
150 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
152 KCategorizedView::dataChanged(topLeft
, bottomRight
);
154 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
155 if ((flow() == QListView::LeftToRight
) && !proxyModel
->isCategorizedModel()) {
156 // bypass a QListView issue that items are not layout correctly if the decoration size of
158 scheduleDelayedItemsLayout();
162 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
164 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
165 viewOptions
.font
= m_font
;
166 viewOptions
.decorationPosition
= m_decorationPosition
;
167 viewOptions
.decorationSize
= m_decorationSize
;
168 viewOptions
.displayAlignment
= m_displayAlignment
;
169 viewOptions
.showDecorationSelected
= true;
173 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
175 KCategorizedView::contextMenuEvent(event
);
176 m_controller
->triggerContextMenuRequest(event
->pos());
179 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
181 m_controller
->requestActivation();
182 const QModelIndex index
= indexAt(event
->pos());
183 if (index
.isValid() && (event
->button() == Qt::LeftButton
)) {
184 // TODO: It should not be necessary to manually set the dragging state, but I could
185 // not reproduce this issue with a Qt-only example yet to find the root cause.
186 // Issue description: start Dolphin, split the view and drag an item from the
187 // inactive view to the active view by a very fast mouse movement. Result:
188 // the item gets selected instead of being dragged...
189 setState(QAbstractItemView::DraggingState
);
192 if (!index
.isValid()) {
193 if (QApplication::mouseButtons() & Qt::MidButton
) {
194 m_controller
->replaceUrlByClipboard();
196 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
197 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
202 KCategorizedView::mousePressEvent(event
);
205 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
207 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
208 // fix this in KDE 4.1
209 KCategorizedView::startDrag(supportedActions
);
210 DragAndDropHelper::instance().startDrag(this, supportedActions
, m_controller
);
213 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
215 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
216 event
->acceptProposedAction();
220 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
222 KCategorizedView::dragLeaveEvent(event
);
223 setDirtyRegion(m_dropRect
);
226 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
228 KCategorizedView::dragMoveEvent(event
);
230 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
231 const QModelIndex index
= indexAt(event
->pos());
232 setDirtyRegion(m_dropRect
);
234 m_dropRect
.setSize(QSize()); // set as invalid
235 if (index
.isValid()) {
236 const KFileItem item
= m_controller
->itemForIndex(index
);
237 if (!item
.isNull() && item
.isDir()) {
238 m_dropRect
= visualRect(index
);
240 m_dropRect
.setSize(QSize()); // set as invalid
243 if (DragAndDropHelper::instance().isMimeDataSupported(event
->mimeData())) {
244 // accept url drops, independently from the destination item
245 event
->acceptProposedAction();
248 setDirtyRegion(m_dropRect
);
251 void DolphinIconsView::dropEvent(QDropEvent
* event
)
253 const QModelIndex index
= indexAt(event
->pos());
254 const KFileItem item
= m_controller
->itemForIndex(index
);
255 m_controller
->indicateDroppedUrls(item
, m_controller
->url(), event
);
257 KCategorizedView::dropEvent(event
);
260 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
262 KCategorizedView::keyPressEvent(event
);
263 m_controller
->handleKeyPressEvent(event
);
264 m_enableScrollTo
= true; // see DolphinIconsView::scrollTo()
267 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
269 if (m_selectionManager
!= 0) {
270 m_selectionManager
->reset();
273 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
274 if (event
->modifiers() & Qt::ControlModifier
) {
278 KCategorizedView::wheelEvent(event
);
279 // if the icons are aligned left to right, the vertical wheel event should
280 // be applied to the horizontal scrollbar
281 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
282 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
283 (settings
->arrangement() == QListView::LeftToRight
);
284 if (scrollHorizontal
) {
285 QWheelEvent
horizEvent(event
->pos(),
290 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
294 void DolphinIconsView::showEvent(QShowEvent
* event
)
296 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
297 delegate
->setMaximumSize(m_itemSize
);
299 KCategorizedView::showEvent(event
);
302 void DolphinIconsView::leaveEvent(QEvent
* event
)
304 KCategorizedView::leaveEvent(event
);
305 // if the mouse is above an item and moved very fast outside the widget,
306 // no viewportEntered() signal might be emitted although the mouse has been moved
307 // above the viewport
308 m_controller
->emitViewportEntered();
311 void DolphinIconsView::slotShowPreviewChanged()
313 const DolphinView
* view
= m_controller
->dolphinView();
314 updateGridSize(view
->showPreview(), additionalInfoCount());
317 void DolphinIconsView::slotAdditionalInfoChanged()
319 const DolphinView
* view
= m_controller
->dolphinView();
320 const bool showPreview
= view
->showPreview();
321 updateGridSize(showPreview
, view
->additionalInfo().count());
324 void DolphinIconsView::setZoomLevel(int level
)
326 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
328 const int oldIconSize
= settings
->iconSize();
329 int newIconSize
= oldIconSize
;
331 const bool showPreview
= m_controller
->dolphinView()->showPreview();
333 const int previewSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
334 settings
->setPreviewSize(previewSize
);
336 newIconSize
= ZoomLevelInfo::iconSizeForZoomLevel(level
);
337 settings
->setIconSize(newIconSize
);
340 // increase also the grid size
341 const int diff
= newIconSize
- oldIconSize
;
342 settings
->setItemWidth(settings
->itemWidth() + diff
);
343 settings
->setItemHeight(settings
->itemHeight() + diff
);
345 updateGridSize(showPreview
, additionalInfoCount());
348 void DolphinIconsView::requestActivation()
350 m_controller
->requestActivation();
353 void DolphinIconsView::updateFont()
355 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
356 Q_ASSERT(settings
!= 0);
358 if (settings
->useSystemFont()) {
359 m_font
= KGlobalSettings::generalFont();
363 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
365 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
366 Q_ASSERT(settings
!= 0);
368 int itemWidth
= settings
->itemWidth();
369 int itemHeight
= settings
->itemHeight();
370 int size
= settings
->iconSize();
373 const int previewSize
= settings
->previewSize();
374 const int diff
= previewSize
- size
;
381 Q_ASSERT(additionalInfoCount
>= 0);
382 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
384 if (settings
->arrangement() == QListView::TopToBottom
) {
385 // The decoration width indirectly defines the maximum
386 // width for the text wrapping. To use the maximum item width
387 // for text wrapping, it is used as decoration width.
388 m_decorationSize
= QSize(itemWidth
, size
);
389 setIconSize(QSize(itemWidth
, size
));
391 m_decorationSize
= QSize(size
, size
);
392 setIconSize(QSize(size
, size
));
395 m_itemSize
= QSize(itemWidth
, itemHeight
);
397 const int spacing
= settings
->gridSpacing();
398 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
400 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
402 delegate
->setMaximumSize(m_itemSize
);
405 if (m_selectionManager
!= 0) {
406 m_selectionManager
->reset();
410 int DolphinIconsView::additionalInfoCount() const
412 const DolphinView
* view
= m_controller
->dolphinView();
413 return view
->additionalInfo().count();
416 #include "dolphiniconsview.moc"