]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
SVN_SILENT made messages (.desktop file)
[dolphin.git] / src / dolphiniconsview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "dolphiniconsview.h"
21
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"
30
31 #include <kcategorizedsortfilterproxymodel.h>
32 #include <kdialog.h>
33 #include <kdirmodel.h>
34 #include <kfileitemdelegate.h>
35
36 #include <QAbstractProxyModel>
37 #include <QApplication>
38 #include <QScrollBar>
39
40 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
41 KCategorizedView(parent),
42 m_enableScrollTo(false),
43 m_controller(controller),
44 m_selectionManager(0),
45 m_categoryDrawer(0),
46 m_font(),
47 m_decorationSize(),
48 m_decorationPosition(QStyleOptionViewItem::Top),
49 m_displayAlignment(Qt::AlignHCenter),
50 m_itemSize(),
51 m_dropRect()
52 {
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);
59 setDragEnabled(true);
60 setEditTriggers(QAbstractItemView::NoEditTriggers);
61 viewport()->setAcceptDrops(true);
62
63 setMouseTracking(true);
64
65 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
66 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
67 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
68 // RETURN-key in keyPressEvent().
69 if (KGlobalSettings::singleClick()) {
70 connect(this, SIGNAL(clicked(const QModelIndex&)),
71 controller, SLOT(triggerItem(const QModelIndex&)));
72 } else {
73 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
74 controller, SLOT(triggerItem(const QModelIndex&)));
75 }
76
77 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
78 m_selectionManager = new SelectionManager(this);
79 connect(m_selectionManager, SIGNAL(selectionChanged()),
80 this, SLOT(requestActivation()));
81 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
82 m_selectionManager, SLOT(reset()));
83 }
84
85 connect(this, SIGNAL(entered(const QModelIndex&)),
86 controller, SLOT(emitItemEntered(const QModelIndex&)));
87 connect(this, SIGNAL(viewportEntered()),
88 controller, SLOT(emitViewportEntered()));
89 connect(controller, SIGNAL(zoomLevelChanged(int)),
90 this, SLOT(setZoomLevel(int)));
91
92 const DolphinView* view = controller->dolphinView();
93 connect(view, SIGNAL(showPreviewChanged()),
94 this, SLOT(slotShowPreviewChanged()));
95 connect(view, SIGNAL(additionalInfoChanged()),
96 this, SLOT(slotAdditionalInfoChanged()));
97
98 // apply the icons mode settings to the widget
99 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
100 Q_ASSERT(settings != 0);
101
102 if (settings->useSystemFont()) {
103 m_font = KGlobalSettings::generalFont();
104 } else {
105 m_font = QFont(settings->fontFamily(),
106 settings->fontSize(),
107 settings->fontWeight(),
108 settings->italicFont());
109 }
110
111 setWordWrap(settings->numberOfTextlines() > 1);
112 updateGridSize(view->showPreview(), 0);
113
114 if (settings->arrangement() == QListView::TopToBottom) {
115 setFlow(QListView::LeftToRight);
116 m_decorationPosition = QStyleOptionViewItem::Top;
117 m_displayAlignment = Qt::AlignHCenter;
118 } else {
119 setFlow(QListView::TopToBottom);
120 m_decorationPosition = QStyleOptionViewItem::Left;
121 m_displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
122 }
123
124 m_categoryDrawer = new DolphinCategoryDrawer();
125 setCategoryDrawer(m_categoryDrawer);
126
127 setFocus();
128
129 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
130 this, SLOT(updateFont()));
131 }
132
133 DolphinIconsView::~DolphinIconsView()
134 {
135 delete m_categoryDrawer;
136 m_categoryDrawer = 0;
137 }
138
139 void DolphinIconsView::scrollTo(const QModelIndex& index, ScrollHint hint)
140 {
141 // Enable the QListView implementation of scrollTo() only if it has been
142 // triggered by a key press. Otherwise QAbstractItemView wants to scroll to the current
143 // index each time the layout has been changed. This becomes an issue when
144 // previews are loaded and the scrollbar is used: the scrollbar will always
145 // be reset to 0 on each new preview.
146 if (m_enableScrollTo || (state() != QAbstractItemView::NoState)) {
147 KCategorizedView::scrollTo(index, hint);
148 m_enableScrollTo = false;
149 }
150 }
151
152 void DolphinIconsView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
153 {
154 KCategorizedView::dataChanged(topLeft, bottomRight);
155
156 KCategorizedSortFilterProxyModel* proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model());
157 if ((flow() == QListView::LeftToRight) && !proxyModel->isCategorizedModel()) {
158 // bypass a QListView issue that items are not layout correctly if the decoration size of
159 // an index changes
160 scheduleDelayedItemsLayout();
161 }
162 }
163
164 QStyleOptionViewItem DolphinIconsView::viewOptions() const
165 {
166 QStyleOptionViewItem viewOptions = KCategorizedView::viewOptions();
167 viewOptions.font = m_font;
168 viewOptions.decorationPosition = m_decorationPosition;
169 viewOptions.decorationSize = m_decorationSize;
170 viewOptions.displayAlignment = m_displayAlignment;
171 viewOptions.showDecorationSelected = true;
172 return viewOptions;
173 }
174
175 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
176 {
177 KCategorizedView::contextMenuEvent(event);
178 m_controller->triggerContextMenuRequest(event->pos());
179 }
180
181 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
182 {
183 m_controller->requestActivation();
184 const QModelIndex index = indexAt(event->pos());
185 if (index.isValid() && (event->button() == Qt::LeftButton)) {
186 // TODO: It should not be necessary to manually set the dragging state, but I could
187 // not reproduce this issue with a Qt-only example yet to find the root cause.
188 // Issue description: start Dolphin, split the view and drag an item from the
189 // inactive view to the active view by a very fast mouse movement. Result:
190 // the item gets selected instead of being dragged...
191 setState(QAbstractItemView::DraggingState);
192 }
193
194 if (!index.isValid()) {
195 if (QApplication::mouseButtons() & Qt::MidButton) {
196 m_controller->replaceUrlByClipboard();
197 }
198 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
199 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
200 clearSelection();
201 }
202 }
203
204 KCategorizedView::mousePressEvent(event);
205 }
206
207 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
208 {
209 // TODO: invoking KCategorizedView::startDrag() should not be necessary, we'll
210 // fix this in KDE 4.1
211 KCategorizedView::startDrag(supportedActions);
212 DragAndDropHelper::startDrag(this, supportedActions, m_controller);
213 }
214
215 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
216 {
217 if (DragAndDropHelper::isMimeDataSupported(event->mimeData())) {
218 event->acceptProposedAction();
219 }
220 }
221
222 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
223 {
224 KCategorizedView::dragLeaveEvent(event);
225 setDirtyRegion(m_dropRect);
226 }
227
228 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
229 {
230 KCategorizedView::dragMoveEvent(event);
231
232 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
233 const QModelIndex index = indexAt(event->pos());
234 setDirtyRegion(m_dropRect);
235
236 m_dropRect.setSize(QSize()); // set as invalid
237 if (index.isValid()) {
238 const KFileItem item = m_controller->itemForIndex(index);
239 if (!item.isNull() && item.isDir()) {
240 m_dropRect = visualRect(index);
241 } else {
242 m_dropRect.setSize(QSize()); // set as invalid
243 }
244 }
245 if (DragAndDropHelper::isMimeDataSupported(event->mimeData())) {
246 // accept url drops, independently from the destination item
247 event->acceptProposedAction();
248 }
249
250 setDirtyRegion(m_dropRect);
251 }
252
253 void DolphinIconsView::dropEvent(QDropEvent* event)
254 {
255 const QModelIndex index = indexAt(event->pos());
256 const KFileItem item = m_controller->itemForIndex(index);
257 m_controller->indicateDroppedUrls(item, m_controller->url(), event);
258
259 KCategorizedView::dropEvent(event);
260 }
261
262 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
263 {
264 KCategorizedView::keyPressEvent(event);
265 m_controller->handleKeyPressEvent(event);
266 m_enableScrollTo = true; // see DolphinIconsView::scrollTo()
267 }
268
269 void DolphinIconsView::wheelEvent(QWheelEvent* event)
270 {
271 if (m_selectionManager != 0) {
272 m_selectionManager->reset();
273 }
274
275 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
276 if (event->modifiers() & Qt::ControlModifier) {
277 event->ignore();
278 return;
279 }
280 KCategorizedView::wheelEvent(event);
281 // if the icons are aligned left to right, the vertical wheel event should
282 // be applied to the horizontal scrollbar
283 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
284 const bool scrollHorizontal = (event->orientation() == Qt::Vertical) &&
285 (settings->arrangement() == QListView::LeftToRight);
286 if (scrollHorizontal) {
287 QWheelEvent horizEvent(event->pos(),
288 event->delta(),
289 event->buttons(),
290 event->modifiers(),
291 Qt::Horizontal);
292 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
293 }
294 }
295
296 void DolphinIconsView::showEvent(QShowEvent* event)
297 {
298 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
299 delegate->setMaximumSize(m_itemSize);
300
301 KCategorizedView::showEvent(event);
302 }
303
304 void DolphinIconsView::leaveEvent(QEvent* event)
305 {
306 KCategorizedView::leaveEvent(event);
307 // if the mouse is above an item and moved very fast outside the widget,
308 // no viewportEntered() signal might be emitted although the mouse has been moved
309 // above the viewport
310 m_controller->emitViewportEntered();
311 }
312
313 void DolphinIconsView::slotShowPreviewChanged()
314 {
315 const DolphinView* view = m_controller->dolphinView();
316 updateGridSize(view->showPreview(), additionalInfoCount());
317 }
318
319 void DolphinIconsView::slotAdditionalInfoChanged()
320 {
321 const DolphinView* view = m_controller->dolphinView();
322 const bool showPreview = view->showPreview();
323 updateGridSize(showPreview, view->additionalInfo().count());
324 }
325
326 void DolphinIconsView::setZoomLevel(int level)
327 {
328 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
329
330 const int oldIconSize = settings->iconSize();
331 int newIconSize = oldIconSize;
332
333 const bool showPreview = m_controller->dolphinView()->showPreview();
334 if (showPreview) {
335 const int previewSize = ZoomLevelInfo::iconSizeForZoomLevel(level);
336 settings->setPreviewSize(previewSize);
337 } else {
338 newIconSize = ZoomLevelInfo::iconSizeForZoomLevel(level);
339 settings->setIconSize(newIconSize);
340 }
341
342 // increase also the grid size
343 const int diff = newIconSize - oldIconSize;
344 settings->setItemWidth(settings->itemWidth() + diff);
345 settings->setItemHeight(settings->itemHeight() + diff);
346
347 updateGridSize(showPreview, additionalInfoCount());
348 }
349
350 void DolphinIconsView::requestActivation()
351 {
352 m_controller->requestActivation();
353 }
354
355 void DolphinIconsView::updateFont()
356 {
357 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
358 Q_ASSERT(settings != 0);
359
360 if (settings->useSystemFont()) {
361 m_font = KGlobalSettings::generalFont();
362 }
363 }
364
365 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
366 {
367 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
368 Q_ASSERT(settings != 0);
369
370 int itemWidth = settings->itemWidth();
371 int itemHeight = settings->itemHeight();
372 int size = settings->iconSize();
373
374 if (showPreview) {
375 const int previewSize = settings->previewSize();
376 const int diff = previewSize - size;
377 itemWidth += diff;
378 itemHeight += diff;
379
380 size = previewSize;
381 }
382
383 Q_ASSERT(additionalInfoCount >= 0);
384 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
385
386 if (settings->arrangement() == QListView::TopToBottom) {
387 // The decoration width indirectly defines the maximum
388 // width for the text wrapping. To use the maximum item width
389 // for text wrapping, it is used as decoration width.
390 m_decorationSize = QSize(itemWidth, size);
391 setIconSize(QSize(itemWidth, size));
392 } else {
393 m_decorationSize = QSize(size, size);
394 setIconSize(QSize(size, size));
395 }
396
397 m_itemSize = QSize(itemWidth, itemHeight);
398
399 const int spacing = settings->gridSpacing();
400 setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
401
402 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
403 if (delegate != 0) {
404 delegate->setMaximumSize(m_itemSize);
405 }
406
407 if (m_selectionManager != 0) {
408 m_selectionManager->reset();
409 }
410 }
411
412 int DolphinIconsView::additionalInfoCount() const
413 {
414 const DolphinView* view = m_controller->dolphinView();
415 return view->additionalInfo().count();
416 }
417
418 #include "dolphiniconsview.moc"