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"
30 #include <kcategorizedsortfilterproxymodel.h>
32 #include <kdirmodel.h>
33 #include <kfileitemdelegate.h>
35 #include <QAbstractProxyModel>
36 #include <QApplication>
39 DolphinIconsView::DolphinIconsView(QWidget
* parent
, DolphinController
* controller
) :
40 KCategorizedView(parent
),
41 m_enableScrollTo(false),
42 m_controller(controller
),
43 m_selectionManager(0),
47 m_decorationPosition(QStyleOptionViewItem::Top
),
48 m_displayAlignment(Qt::AlignHCenter
),
52 Q_ASSERT(controller
!= 0);
53 setViewMode(QListView::IconMode
);
54 setResizeMode(QListView::Adjust
);
55 setSpacing(KDialog::spacingHint());
56 setMovement(QListView::Static
);
58 setEditTriggers(QAbstractItemView::NoEditTriggers
);
59 viewport()->setAcceptDrops(true);
61 setMouseTracking(true);
63 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
64 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
65 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
66 // RETURN-key in keyPressEvent().
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::startDrag(this, supportedActions
);
213 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
215 if (event
->mimeData()->hasUrls()) {
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 (event
->mimeData()->hasUrls()) {
244 // accept url drops, independently from the destination item
245 event
->acceptProposedAction();
248 setDirtyRegion(m_dropRect
);
251 void DolphinIconsView::dropEvent(QDropEvent
* event
)
253 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
254 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
255 if (!urls
.isEmpty()) {
256 const QModelIndex index
= indexAt(event
->pos());
257 const KFileItem item
= m_controller
->itemForIndex(index
);
258 m_controller
->indicateDroppedUrls(urls
,
261 event
->acceptProposedAction();
265 KCategorizedView::dropEvent(event
);
268 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
270 KCategorizedView::keyPressEvent(event
);
271 m_controller
->handleKeyPressEvent(event
);
272 m_enableScrollTo
= true; // see DolphinIconsView::scrollTo()
275 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
277 if (m_selectionManager
!= 0) {
278 m_selectionManager
->reset();
281 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
282 if (event
->modifiers() & Qt::ControlModifier
) {
286 KCategorizedView::wheelEvent(event
);
287 // if the icons are aligned left to right, the vertical wheel event should
288 // be applied to the horizontal scrollbar
289 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
290 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
291 (settings
->arrangement() == QListView::LeftToRight
);
292 if (scrollHorizontal
) {
293 QWheelEvent
horizEvent(event
->pos(),
298 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
302 void DolphinIconsView::showEvent(QShowEvent
* event
)
304 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
305 delegate
->setMaximumSize(m_itemSize
);
307 KCategorizedView::showEvent(event
);
310 void DolphinIconsView::leaveEvent(QEvent
* event
)
312 KCategorizedView::leaveEvent(event
);
313 // if the mouse is above an item and moved very fast outside the widget,
314 // no viewportEntered() signal might be emitted although the mouse has been moved
315 // above the viewport
316 m_controller
->emitViewportEntered();
319 void DolphinIconsView::slotShowPreviewChanged()
321 const DolphinView
* view
= m_controller
->dolphinView();
322 updateGridSize(view
->showPreview(), additionalInfoCount());
325 void DolphinIconsView::slotAdditionalInfoChanged()
327 const DolphinView
* view
= m_controller
->dolphinView();
328 const bool showPreview
= view
->showPreview();
329 updateGridSize(showPreview
, view
->additionalInfo().count());
332 void DolphinIconsView::setZoomLevel(int level
)
334 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
336 const int oldIconSize
= settings
->iconSize();
337 int newIconSize
= oldIconSize
;
339 const bool showPreview
= m_controller
->dolphinView()->showPreview();
341 const int previewSize
= DolphinController::iconSizeForZoomLevel(level
);
342 settings
->setPreviewSize(previewSize
);
344 newIconSize
= DolphinController::iconSizeForZoomLevel(level
);
345 settings
->setIconSize(newIconSize
);
348 // increase also the grid size
349 const int diff
= newIconSize
- oldIconSize
;
350 settings
->setItemWidth(settings
->itemWidth() + diff
);
351 settings
->setItemHeight(settings
->itemHeight() + diff
);
353 updateGridSize(showPreview
, additionalInfoCount());
356 void DolphinIconsView::requestActivation()
358 m_controller
->requestActivation();
361 void DolphinIconsView::updateFont()
363 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
364 Q_ASSERT(settings
!= 0);
366 if (settings
->useSystemFont()) {
367 m_font
= KGlobalSettings::generalFont();
371 void DolphinIconsView::updateGridSize(bool showPreview
, int additionalInfoCount
)
373 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
374 Q_ASSERT(settings
!= 0);
376 int itemWidth
= settings
->itemWidth();
377 int itemHeight
= settings
->itemHeight();
378 int size
= settings
->iconSize();
381 const int previewSize
= settings
->previewSize();
382 const int diff
= previewSize
- size
;
389 Q_ASSERT(additionalInfoCount
>= 0);
390 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
392 if (settings
->arrangement() == QListView::TopToBottom
) {
393 // The decoration width indirectly defines the maximum
394 // width for the text wrapping. To use the maximum item width
395 // for text wrapping, it is used as decoration width.
396 m_decorationSize
= QSize(itemWidth
, size
);
397 setIconSize(QSize(itemWidth
, size
));
399 m_decorationSize
= QSize(size
, size
);
400 setIconSize(QSize(size
, size
));
403 m_itemSize
= QSize(itemWidth
, itemHeight
);
405 const int spacing
= settings
->gridSpacing();
406 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
408 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
410 delegate
->setMaximumSize(m_itemSize
);
413 if (m_selectionManager
!= 0) {
414 m_selectionManager
->reset();
418 int DolphinIconsView::additionalInfoCount() const
420 const DolphinView
* view
= m_controller
->dolphinView();
421 return view
->additionalInfo().count();
424 #include "dolphiniconsview.moc"