]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphiniconsview.cpp
added missing copyright
[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
30 #include <kcategorizedsortfilterproxymodel.h>
31 #include <kdialog.h>
32 #include <kdirmodel.h>
33 #include <kfileitemdelegate.h>
34
35 #include <QAbstractProxyModel>
36 #include <QApplication>
37 #include <QScrollBar>
38
39 DolphinIconsView::DolphinIconsView(QWidget* parent, DolphinController* controller) :
40 KCategorizedView(parent),
41 m_enableScrollTo(false),
42 m_controller(controller),
43 m_selectionManager(0),
44 m_categoryDrawer(0),
45 m_font(),
46 m_decorationSize(),
47 m_decorationPosition(QStyleOptionViewItem::Top),
48 m_displayAlignment(Qt::AlignHCenter),
49 m_itemSize(),
50 m_dropRect()
51 {
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);
58 setDragEnabled(true);
59 setEditTriggers(QAbstractItemView::NoEditTriggers);
60 viewport()->setAcceptDrops(true);
61
62 setMouseTracking(true);
63
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&)));
71 } else {
72 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
73 controller, SLOT(triggerItem(const QModelIndex&)));
74 }
75
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()));
82 }
83
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)));
90
91 const DolphinView* view = controller->dolphinView();
92 connect(view, SIGNAL(showPreviewChanged()),
93 this, SLOT(slotShowPreviewChanged()));
94 connect(view, SIGNAL(additionalInfoChanged()),
95 this, SLOT(slotAdditionalInfoChanged()));
96
97 // apply the icons mode settings to the widget
98 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
99 Q_ASSERT(settings != 0);
100
101 if (settings->useSystemFont()) {
102 m_font = KGlobalSettings::generalFont();
103 } else {
104 m_font = QFont(settings->fontFamily(),
105 settings->fontSize(),
106 settings->fontWeight(),
107 settings->italicFont());
108 }
109
110 setWordWrap(settings->numberOfTextlines() > 1);
111 updateGridSize(view->showPreview(), 0);
112
113 if (settings->arrangement() == QListView::TopToBottom) {
114 setFlow(QListView::LeftToRight);
115 m_decorationPosition = QStyleOptionViewItem::Top;
116 m_displayAlignment = Qt::AlignHCenter;
117 } else {
118 setFlow(QListView::TopToBottom);
119 m_decorationPosition = QStyleOptionViewItem::Left;
120 m_displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
121 }
122
123 m_categoryDrawer = new DolphinCategoryDrawer();
124 setCategoryDrawer(m_categoryDrawer);
125
126 setFocus();
127
128 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
129 this, SLOT(updateFont()));
130 }
131
132 DolphinIconsView::~DolphinIconsView()
133 {
134 delete m_categoryDrawer;
135 m_categoryDrawer = 0;
136 }
137
138 void DolphinIconsView::scrollTo(const QModelIndex& index, ScrollHint hint)
139 {
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;
148 }
149 }
150
151 void DolphinIconsView::dataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
152 {
153 KCategorizedView::dataChanged(topLeft, bottomRight);
154
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
158 // an index changes
159 scheduleDelayedItemsLayout();
160 }
161 }
162
163 QStyleOptionViewItem DolphinIconsView::viewOptions() const
164 {
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;
171 return viewOptions;
172 }
173
174 void DolphinIconsView::contextMenuEvent(QContextMenuEvent* event)
175 {
176 KCategorizedView::contextMenuEvent(event);
177 m_controller->triggerContextMenuRequest(event->pos());
178 }
179
180 void DolphinIconsView::mousePressEvent(QMouseEvent* event)
181 {
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);
191 }
192
193 if (!index.isValid()) {
194 if (QApplication::mouseButtons() & Qt::MidButton) {
195 m_controller->replaceUrlByClipboard();
196 }
197 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
198 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
199 clearSelection();
200 }
201 }
202
203 KCategorizedView::mousePressEvent(event);
204 }
205
206 void DolphinIconsView::startDrag(Qt::DropActions supportedActions)
207 {
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);
212 }
213
214 void DolphinIconsView::dragEnterEvent(QDragEnterEvent* event)
215 {
216 if (event->mimeData()->hasUrls()) {
217 event->acceptProposedAction();
218 }
219 }
220
221 void DolphinIconsView::dragLeaveEvent(QDragLeaveEvent* event)
222 {
223 KCategorizedView::dragLeaveEvent(event);
224 setDirtyRegion(m_dropRect);
225 }
226
227 void DolphinIconsView::dragMoveEvent(QDragMoveEvent* event)
228 {
229 KCategorizedView::dragMoveEvent(event);
230
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);
234
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);
240 } else {
241 m_dropRect.setSize(QSize()); // set as invalid
242 }
243 }
244 if (event->mimeData()->hasUrls()) {
245 // accept url drops, independently from the destination item
246 event->acceptProposedAction();
247 }
248
249 setDirtyRegion(m_dropRect);
250 }
251
252 void DolphinIconsView::dropEvent(QDropEvent* event)
253 {
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,
260 m_controller->url(),
261 item);
262 event->acceptProposedAction();
263 }
264 }
265
266 KCategorizedView::dropEvent(event);
267 }
268
269 void DolphinIconsView::keyPressEvent(QKeyEvent* event)
270 {
271 KCategorizedView::keyPressEvent(event);
272 m_controller->handleKeyPressEvent(event);
273 m_enableScrollTo = true; // see DolphinIconsView::scrollTo()
274 }
275
276 void DolphinIconsView::wheelEvent(QWheelEvent* event)
277 {
278 if (m_selectionManager != 0) {
279 m_selectionManager->reset();
280 }
281
282 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
283 if (event->modifiers() & Qt::ControlModifier) {
284 event->ignore();
285 return;
286 }
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(),
295 event->delta(),
296 event->buttons(),
297 event->modifiers(),
298 Qt::Horizontal);
299 QApplication::sendEvent(horizontalScrollBar(), &horizEvent);
300 }
301 }
302
303 void DolphinIconsView::showEvent(QShowEvent* event)
304 {
305 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
306 delegate->setMaximumSize(m_itemSize);
307
308 KCategorizedView::showEvent(event);
309 }
310
311 void DolphinIconsView::leaveEvent(QEvent* event)
312 {
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();
318 }
319
320 void DolphinIconsView::slotShowPreviewChanged()
321 {
322 const DolphinView* view = m_controller->dolphinView();
323 updateGridSize(view->showPreview(), additionalInfoCount());
324 }
325
326 void DolphinIconsView::slotAdditionalInfoChanged()
327 {
328 const DolphinView* view = m_controller->dolphinView();
329 const bool showPreview = view->showPreview();
330 updateGridSize(showPreview, view->additionalInfo().count());
331 }
332
333 void DolphinIconsView::setZoomLevel(int level)
334 {
335 IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
336
337 const int oldIconSize = settings->iconSize();
338 int newIconSize = oldIconSize;
339
340 const bool showPreview = m_controller->dolphinView()->showPreview();
341 if (showPreview) {
342 const int previewSize = DolphinController::iconSizeForZoomLevel(level);
343 settings->setPreviewSize(previewSize);
344 } else {
345 newIconSize = DolphinController::iconSizeForZoomLevel(level);
346 settings->setIconSize(newIconSize);
347 }
348
349 // increase also the grid size
350 const int diff = newIconSize - oldIconSize;
351 settings->setItemWidth(settings->itemWidth() + diff);
352 settings->setItemHeight(settings->itemHeight() + diff);
353
354 updateGridSize(showPreview, additionalInfoCount());
355 }
356
357 void DolphinIconsView::requestActivation()
358 {
359 m_controller->requestActivation();
360 }
361
362 void DolphinIconsView::updateFont()
363 {
364 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
365 Q_ASSERT(settings != 0);
366
367 if (settings->useSystemFont()) {
368 m_font = KGlobalSettings::generalFont();
369 }
370 }
371
372 void DolphinIconsView::updateGridSize(bool showPreview, int additionalInfoCount)
373 {
374 const IconsModeSettings* settings = DolphinSettings::instance().iconsModeSettings();
375 Q_ASSERT(settings != 0);
376
377 int itemWidth = settings->itemWidth();
378 int itemHeight = settings->itemHeight();
379 int size = settings->iconSize();
380
381 if (showPreview) {
382 const int previewSize = settings->previewSize();
383 const int diff = previewSize - size;
384 itemWidth += diff;
385 itemHeight += diff;
386
387 size = previewSize;
388 }
389
390 Q_ASSERT(additionalInfoCount >= 0);
391 itemHeight += additionalInfoCount * m_font.pointSize() * 2;
392
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));
399 } else {
400 m_decorationSize = QSize(size, size);
401 setIconSize(QSize(size, size));
402 }
403
404 m_itemSize = QSize(itemWidth, itemHeight);
405
406 const int spacing = settings->gridSpacing();
407 setGridSize(QSize(itemWidth + spacing * 2, itemHeight + spacing));
408
409 KFileItemDelegate* delegate = dynamic_cast<KFileItemDelegate*>(itemDelegate());
410 if (delegate != 0) {
411 delegate->setMaximumSize(m_itemSize);
412 }
413
414 if (m_selectionManager != 0) {
415 m_selectionManager->reset();
416 }
417 }
418
419 int DolphinIconsView::additionalInfoCount() const
420 {
421 const DolphinView* view = m_controller->dolphinView();
422 return view->additionalInfo().count();
423 }
424
425 #include "dolphiniconsview.moc"