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 setLayoutDirection(Qt::LeftToRight
);
54 setViewMode(QListView::IconMode
);
55 setResizeMode(QListView::Adjust
);
56 setSpacing(KDialog::spacingHint());
57 setMovement(QListView::Static
);
59 setEditTriggers(QAbstractItemView::NoEditTriggers
);
60 viewport()->setAcceptDrops(true);
62 setMouseTracking(true);
64 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
65 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
66 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
67 // RETURN-key in keyPressEvent().
68 if (KGlobalSettings::singleClick()) {
69 connect(this, SIGNAL(clicked(const QModelIndex
&)),
70 controller
, SLOT(triggerItem(const QModelIndex
&)));
72 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
73 controller
, SLOT(triggerItem(const QModelIndex
&)));
76 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
77 m_selectionManager
= new SelectionManager(this);
78 connect(m_selectionManager
, SIGNAL(selectionChanged()),
79 this, SLOT(requestActivation()));
80 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
81 m_selectionManager
, SLOT(reset()));
84 connect(this, SIGNAL(entered(const QModelIndex
&)),
85 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
86 connect(this, SIGNAL(viewportEntered()),
87 controller
, SLOT(emitViewportEntered()));
88 connect(controller
, SIGNAL(zoomLevelChanged(int)),
89 this, SLOT(setZoomLevel(int)));
91 const DolphinView
* view
= controller
->dolphinView();
92 connect(view
, SIGNAL(showPreviewChanged()),
93 this, SLOT(slotShowPreviewChanged()));
94 connect(view
, SIGNAL(additionalInfoChanged()),
95 this, SLOT(slotAdditionalInfoChanged()));
97 // apply the icons mode settings to the widget
98 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
99 Q_ASSERT(settings
!= 0);
101 if (settings
->useSystemFont()) {
102 m_font
= KGlobalSettings::generalFont();
104 m_font
= QFont(settings
->fontFamily(),
105 settings
->fontSize(),
106 settings
->fontWeight(),
107 settings
->italicFont());
110 setWordWrap(settings
->numberOfTextlines() > 1);
111 updateGridSize(view
->showPreview(), 0);
113 if (settings
->arrangement() == QListView::TopToBottom
) {
114 setFlow(QListView::LeftToRight
);
115 m_decorationPosition
= QStyleOptionViewItem::Top
;
116 m_displayAlignment
= Qt::AlignHCenter
;
118 setFlow(QListView::TopToBottom
);
119 m_decorationPosition
= QStyleOptionViewItem::Left
;
120 m_displayAlignment
= Qt::AlignLeft
| Qt::AlignVCenter
;
123 m_categoryDrawer
= new DolphinCategoryDrawer();
124 setCategoryDrawer(m_categoryDrawer
);
128 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
129 this, SLOT(updateFont()));
132 DolphinIconsView::~DolphinIconsView()
134 delete m_categoryDrawer
;
135 m_categoryDrawer
= 0;
138 void DolphinIconsView::scrollTo(const QModelIndex
& index
, ScrollHint hint
)
140 // Enable the QListView implementation of scrollTo() only if it has been
141 // triggered by a key press. Otherwise QAbstractItemView wants to scroll to the current
142 // index each time the layout has been changed. This becomes an issue when
143 // previews are loaded and the scrollbar is used: the scrollbar will always
144 // be reset to 0 on each new preview.
145 if (m_enableScrollTo
|| (state() != QAbstractItemView::NoState
)) {
146 KCategorizedView::scrollTo(index
, hint
);
147 m_enableScrollTo
= false;
151 void DolphinIconsView::dataChanged(const QModelIndex
& topLeft
, const QModelIndex
& bottomRight
)
153 KCategorizedView::dataChanged(topLeft
, bottomRight
);
155 KCategorizedSortFilterProxyModel
* proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model());
156 if ((flow() == QListView::LeftToRight
) && !proxyModel
->isCategorizedModel()) {
157 // bypass a QListView issue that items are not layout correctly if the decoration size of
159 scheduleDelayedItemsLayout();
163 QStyleOptionViewItem
DolphinIconsView::viewOptions() const
165 QStyleOptionViewItem viewOptions
= KCategorizedView::viewOptions();
166 viewOptions
.font
= m_font
;
167 viewOptions
.decorationPosition
= m_decorationPosition
;
168 viewOptions
.decorationSize
= m_decorationSize
;
169 viewOptions
.displayAlignment
= m_displayAlignment
;
170 viewOptions
.showDecorationSelected
= true;
174 void DolphinIconsView::contextMenuEvent(QContextMenuEvent
* event
)
176 KCategorizedView::contextMenuEvent(event
);
177 m_controller
->triggerContextMenuRequest(event
->pos());
180 void DolphinIconsView::mousePressEvent(QMouseEvent
* event
)
182 m_controller
->requestActivation();
183 const QModelIndex index
= indexAt(event
->pos());
184 if (index
.isValid() && (event
->button() == Qt::LeftButton
)) {
185 // TODO: It should not be necessary to manually set the dragging state, but I could
186 // not reproduce this issue with a Qt-only example yet to find the root cause.
187 // Issue description: start Dolphin, split the view and drag an item from the
188 // inactive view to the active view by a very fast mouse movement. Result:
189 // the item gets selected instead of being dragged...
190 setState(QAbstractItemView::DraggingState
);
193 if (!index
.isValid()) {
194 if (QApplication::mouseButtons() & Qt::MidButton
) {
195 m_controller
->replaceUrlByClipboard();
197 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
198 if (!(modifier
& Qt::ShiftModifier
) && !(modifier
& Qt::ControlModifier
)) {
203 KCategorizedView::mousePressEvent(event
);
206 void DolphinIconsView::startDrag(Qt::DropActions supportedActions
)
208 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
209 // fix this in KDE 4.1
210 KCategorizedView::startDrag(supportedActions
);
211 DragAndDropHelper::startDrag(this, supportedActions
);
214 void DolphinIconsView::dragEnterEvent(QDragEnterEvent
* event
)
216 if (event
->mimeData()->hasUrls()) {
217 event
->acceptProposedAction();
221 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent
* event
)
223 KCategorizedView::dragLeaveEvent(event
);
224 setDirtyRegion(m_dropRect
);
227 void DolphinIconsView::dragMoveEvent(QDragMoveEvent
* event
)
229 KCategorizedView::dragMoveEvent(event
);
231 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
232 const QModelIndex index
= indexAt(event
->pos());
233 setDirtyRegion(m_dropRect
);
235 m_dropRect
.setSize(QSize()); // set as invalid
236 if (index
.isValid()) {
237 const KFileItem item
= m_controller
->itemForIndex(index
);
238 if (!item
.isNull() && item
.isDir()) {
239 m_dropRect
= visualRect(index
);
241 m_dropRect
.setSize(QSize()); // set as invalid
244 if (event
->mimeData()->hasUrls()) {
245 // accept url drops, independently from the destination item
246 event
->acceptProposedAction();
249 setDirtyRegion(m_dropRect
);
252 void DolphinIconsView::dropEvent(QDropEvent
* event
)
254 if (!selectionModel()->isSelected(indexAt(event
->pos()))) {
255 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
256 if (!urls
.isEmpty()) {
257 const QModelIndex index
= indexAt(event
->pos());
258 const KFileItem item
= m_controller
->itemForIndex(index
);
259 m_controller
->indicateDroppedUrls(urls
,
262 event
->acceptProposedAction();
266 KCategorizedView::dropEvent(event
);
269 void DolphinIconsView::keyPressEvent(QKeyEvent
* event
)
271 KCategorizedView::keyPressEvent(event
);
272 m_controller
->handleKeyPressEvent(event
);
273 m_enableScrollTo
= true; // see DolphinIconsView::scrollTo()
276 void DolphinIconsView::wheelEvent(QWheelEvent
* event
)
278 if (m_selectionManager
!= 0) {
279 m_selectionManager
->reset();
282 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
283 if (event
->modifiers() & Qt::ControlModifier
) {
287 KCategorizedView::wheelEvent(event
);
288 // if the icons are aligned left to right, the vertical wheel event should
289 // be applied to the horizontal scrollbar
290 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
291 const bool scrollHorizontal
= (event
->orientation() == Qt::Vertical
) &&
292 (settings
->arrangement() == QListView::LeftToRight
);
293 if (scrollHorizontal
) {
294 QWheelEvent
horizEvent(event
->pos(),
299 QApplication::sendEvent(horizontalScrollBar(), &horizEvent
);
303 void DolphinIconsView::showEvent(QShowEvent
* event
)
305 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
306 delegate
->setMaximumSize(m_itemSize
);
308 KCategorizedView::showEvent(event
);
311 void DolphinIconsView::leaveEvent(QEvent
* event
)
313 KCategorizedView::leaveEvent(event
);
314 // if the mouse is above an item and moved very fast outside the widget,
315 // no viewportEntered() signal might be emitted although the mouse has been moved
316 // above the viewport
317 m_controller
->emitViewportEntered();
320 void DolphinIconsView::slotShowPreviewChanged()
322 const DolphinView
* view
= m_controller
->dolphinView();
323 updateGridSize(view
->showPreview(), additionalInfoCount());
326 void DolphinIconsView::slotAdditionalInfoChanged()
328 const DolphinView
* view
= m_controller
->dolphinView();
329 const bool showPreview
= view
->showPreview();
330 updateGridSize(showPreview
, view
->additionalInfo().count());
333 void DolphinIconsView::setZoomLevel(int level
)
335 IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
337 const int oldIconSize
= settings
->iconSize();
338 int newIconSize
= oldIconSize
;
340 const bool showPreview
= m_controller
->dolphinView()->showPreview();
342 const int previewSize
= DolphinController::iconSizeForZoomLevel(level
);
343 settings
->setPreviewSize(previewSize
);
345 newIconSize
= DolphinController::iconSizeForZoomLevel(level
);
346 settings
->setIconSize(newIconSize
);
349 // increase also the grid size
350 const int diff
= newIconSize
- oldIconSize
;
351 settings
->setItemWidth(settings
->itemWidth() + diff
);
352 settings
->setItemHeight(settings
->itemHeight() + diff
);
354 updateGridSize(showPreview
, additionalInfoCount());
357 void DolphinIconsView::requestActivation()
359 m_controller
->requestActivation();
362 void DolphinIconsView::updateFont()
364 const IconsModeSettings
* settings
= DolphinSettings::instance().iconsModeSettings();
365 Q_ASSERT(settings
!= 0);
367 if (settings
->useSystemFont()) {
368 m_font
= KGlobalSettings::generalFont();
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
;
390 Q_ASSERT(additionalInfoCount
>= 0);
391 itemHeight
+= additionalInfoCount
* m_font
.pointSize() * 2;
393 if (settings
->arrangement() == QListView::TopToBottom
) {
394 // The decoration width indirectly defines the maximum
395 // width for the text wrapping. To use the maximum item width
396 // for text wrapping, it is used as decoration width.
397 m_decorationSize
= QSize(itemWidth
, size
);
398 setIconSize(QSize(itemWidth
, size
));
400 m_decorationSize
= QSize(size
, size
);
401 setIconSize(QSize(size
, size
));
404 m_itemSize
= QSize(itemWidth
, itemHeight
);
406 const int spacing
= settings
->gridSpacing();
407 setGridSize(QSize(itemWidth
+ spacing
* 2, itemHeight
+ spacing
));
409 KFileItemDelegate
* delegate
= dynamic_cast<KFileItemDelegate
*>(itemDelegate());
411 delegate
->setMaximumSize(m_itemSize
);
414 if (m_selectionManager
!= 0) {
415 m_selectionManager
->reset();
419 int DolphinIconsView::additionalInfoCount() const
421 const DolphinView
* view
= m_controller
->dolphinView();
422 return view
->additionalInfo().count();
425 #include "dolphiniconsview.moc"