]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
temporary fixes until MultiSelection will be implemented
[dolphin.git] / src / dolphincolumnview.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 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 "dolphincolumnview.h"
21
22 #include "dolphincontroller.h"
23 #include "dolphinsettings.h"
24
25 #include "dolphin_columnmodesettings.h"
26
27 #include <kcolorutils.h>
28 #include <kcolorscheme.h>
29 #include <kdirlister.h>
30 #include <kdirmodel.h>
31
32 #include <QAbstractProxyModel>
33 #include <QPoint>
34
35 /**
36 * Represents one column inside the DolphinColumnView and has been
37 * extended to respect view options and hovering information.
38 */
39 class ColumnWidget : public QListView
40 {
41 public:
42 ColumnWidget(QWidget* parent,
43 DolphinColumnView* columnView,
44 const KUrl& url);
45 virtual ~ColumnWidget();
46
47 /** Sets the size of the icons. */
48 void setDecorationSize(const QSize& size);
49
50 /**
51 * An active column is defined as column, which shows the same URL
52 * as indicated by the URL navigator. The active column is usually
53 * drawn in a lighter color. All operations are applied to this column.
54 */
55 void setActive(bool active);
56 inline bool isActive() const;
57
58 inline const KUrl& url() const;
59
60 /**
61 * Updates the selection that the folder gets selected which represents
62 * the URL \a url. If \a url is empty, the selection of the column widget
63 * gets cleared.
64 */
65 void updateSelection(const KUrl& url);
66
67 protected:
68 virtual QStyleOptionViewItem viewOptions() const;
69 virtual void dragEnterEvent(QDragEnterEvent* event);
70 virtual void dragLeaveEvent(QDragLeaveEvent* event);
71 virtual void dragMoveEvent(QDragMoveEvent* event);
72 virtual void dropEvent(QDropEvent* event);
73 virtual void mousePressEvent(QMouseEvent* event);
74 virtual void paintEvent(QPaintEvent* event);
75 virtual void contextMenuEvent(QContextMenuEvent* event);
76
77 private:
78 /** Used by ColumnWidget::setActive(). */
79 void activate();
80
81 /** Used by ColumnWidget::setActive(). */
82 void deactivate();
83
84 private:
85 bool m_active;
86 DolphinColumnView* m_view;
87 KUrl m_url;
88 QStyleOptionViewItem m_viewOptions;
89
90 bool m_dragging; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
91 QRect m_dropRect; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
92 };
93
94 ColumnWidget::ColumnWidget(QWidget* parent,
95 DolphinColumnView* columnView,
96 const KUrl& url) :
97 QListView(parent),
98 m_active(true),
99 m_view(columnView),
100 m_url(url),
101 m_dragging(false),
102 m_dropRect()
103 {
104 setMouseTracking(true);
105 viewport()->setAttribute(Qt::WA_Hover);
106
107 // apply the column mode settings to the widget
108 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
109 Q_ASSERT(settings != 0);
110
111 m_viewOptions = QListView::viewOptions();
112
113 QFont font(settings->fontFamily(), settings->fontSize());
114 font.setItalic(settings->italicFont());
115 font.setBold(settings->boldFont());
116 m_viewOptions.font = font;
117
118 const int iconSize = settings->iconSize();
119 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
120
121 activate();
122 }
123
124 ColumnWidget::~ColumnWidget()
125 {
126 }
127
128 void ColumnWidget::setDecorationSize(const QSize& size)
129 {
130 m_viewOptions.decorationSize = size;
131 doItemsLayout();
132 }
133
134 void ColumnWidget::setActive(bool active)
135 {
136 if (m_active == active) {
137 return;
138 }
139
140 m_active = active;
141
142 if (active) {
143 activate();
144 } else {
145 deactivate();
146 }
147 }
148
149 inline bool ColumnWidget::isActive() const
150 {
151 return m_active;
152 }
153
154 const KUrl& ColumnWidget::url() const
155 {
156 return m_url;
157 }
158
159 void ColumnWidget::updateSelection(const KUrl& url)
160 {
161 setSelectionMode(SingleSelection);
162 QItemSelectionModel* selModel = selectionModel();
163 if (url.isEmpty()) {
164 selModel->clear();
165 return;
166 }
167
168 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(m_view->model());
169 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
170 const QModelIndex dirIndex = dirModel->indexForUrl(url);
171 const QModelIndex proxyIndex = proxyModel->mapFromSource(dirIndex);
172
173 const QItemSelection selection = selModel->selection();
174 const bool isIndexSelected = selModel->isSelected(proxyIndex);
175
176 if (!m_active && ((selection.count() > 1) || !isIndexSelected)) {
177 selModel->clear();
178 }
179 if (!isIndexSelected) {
180 selModel->select(proxyIndex, QItemSelectionModel::Select);
181 }
182 }
183
184 QStyleOptionViewItem ColumnWidget::viewOptions() const
185 {
186 return m_viewOptions;
187 }
188
189 void ColumnWidget::dragEnterEvent(QDragEnterEvent* event)
190 {
191 if (event->mimeData()->hasUrls()) {
192 event->acceptProposedAction();
193 }
194
195 m_dragging = true;
196 }
197
198 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
199 {
200 QListView::dragLeaveEvent(event);
201
202 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
203 m_dragging = false;
204 setDirtyRegion(m_dropRect);
205 }
206
207 void ColumnWidget::dragMoveEvent(QDragMoveEvent* event)
208 {
209 QListView::dragMoveEvent(event);
210
211 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
212 const QModelIndex index = indexAt(event->pos());
213 setDirtyRegion(m_dropRect);
214 m_dropRect = visualRect(index);
215 setDirtyRegion(m_dropRect);
216 }
217
218 void ColumnWidget::dropEvent(QDropEvent* event)
219 {
220 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
221 if (!urls.isEmpty()) {
222 event->acceptProposedAction();
223 m_view->m_controller->indicateDroppedUrls(urls,
224 indexAt(event->pos()),
225 event->source());
226 }
227 QListView::dropEvent(event);
228 m_dragging = false;
229 }
230
231 void ColumnWidget::mousePressEvent(QMouseEvent* event)
232 {
233 if (m_active) {
234 selectionModel()->clear();
235 QListView::mousePressEvent(event);
236 return;
237 }
238
239 QListView::mousePressEvent(event);
240
241 const QModelIndex index = indexAt(event->pos());
242
243 bool requestActivation = false;
244 if (index.isValid()) {
245 // A click on an item has been done. Only request an activation
246 // if the item is not a directory.
247 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(m_view->model());
248 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
249 const QModelIndex dirIndex = proxyModel->mapToSource(index);
250 KFileItem* item = dirModel->itemForIndex(dirIndex);
251 requestActivation = (item != 0) && !item->isDir();
252 } else {
253 // a click on the viewport has been done
254 requestActivation = true;
255 }
256
257 if (requestActivation) {
258 m_view->requestActivation(this);
259 } else {
260 m_view->updateSelections();
261 }
262 }
263
264 void ColumnWidget::paintEvent(QPaintEvent* event)
265 {
266 QListView::paintEvent(event);
267
268 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
269 if (m_dragging) {
270 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
271 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
272 }
273 }
274
275 void ColumnWidget::contextMenuEvent(QContextMenuEvent* event)
276 {
277 if (!m_active) {
278 m_view->requestActivation(this);
279 }
280
281 QListView::contextMenuEvent(event);
282
283 const QModelIndex index = indexAt(event->pos());
284 if (index.isValid() || m_active) {
285 // Only open a context menu above an item or if the mouse is above
286 // the active column.
287 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
288 m_view->m_controller->triggerContextMenuRequest(pos);
289 }
290 }
291
292 void ColumnWidget::activate()
293 {
294 const QColor bgColor = KColorScheme(KColorScheme::View).background();
295 QPalette palette = viewport()->palette();
296 palette.setColor(viewport()->backgroundRole(), bgColor);
297 viewport()->setPalette(palette);
298
299 update();
300 }
301
302 void ColumnWidget::deactivate()
303 {
304 QColor bgColor = KColorScheme(KColorScheme::View).background();
305 const QColor fgColor = KColorScheme(KColorScheme::View).foreground();
306 bgColor = KColorUtils::mix(bgColor, fgColor, 0.04);
307
308 QPalette palette = viewport()->palette();
309 palette.setColor(viewport()->backgroundRole(), bgColor);
310 viewport()->setPalette(palette);
311
312 update();
313 }
314
315 // ---
316
317 DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
318 QColumnView(parent),
319 m_controller(controller)
320 {
321 Q_ASSERT(controller != 0);
322
323 setAcceptDrops(true);
324 setDragDropMode(QAbstractItemView::DragDrop);
325 setDropIndicatorShown(false);
326 setSelectionMode(SingleSelection);
327
328 if (KGlobalSettings::singleClick()) {
329 connect(this, SIGNAL(clicked(const QModelIndex&)),
330 this, SLOT(triggerItem(const QModelIndex&)));
331 } else {
332 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
333 this, SLOT(triggerItem(const QModelIndex&)));
334 }
335 connect(this, SIGNAL(entered(const QModelIndex&)),
336 controller, SLOT(emitItemEntered(const QModelIndex&)));
337 connect(this, SIGNAL(viewportEntered()),
338 controller, SLOT(emitViewportEntered()));
339 connect(controller, SIGNAL(zoomIn()),
340 this, SLOT(zoomIn()));
341 connect(controller, SIGNAL(zoomOut()),
342 this, SLOT(zoomOut()));
343 connect(controller, SIGNAL(urlChanged(const KUrl&)),
344 this, SLOT(updateColumnsState(const KUrl&)));
345
346 updateDecorationSize();
347 }
348
349 DolphinColumnView::~DolphinColumnView()
350 {
351 }
352
353 QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
354 {
355 // let the column widget be aware about its URL...
356 KUrl columnUrl;
357 if (viewport()->children().count() == 0) {
358 // For the first column widget the directory lister has not been started
359 // yet, hence use the URL from the controller instead.
360 columnUrl = m_controller->url();
361 } else {
362 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
363 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
364
365 const QModelIndex dirModelIndex = proxyModel->mapToSource(index);
366 KFileItem* fileItem = dirModel->itemForIndex(dirModelIndex);
367 if (fileItem != 0) {
368 columnUrl = fileItem->url();
369 }
370 }
371
372 ColumnWidget* view = new ColumnWidget(viewport(), this, columnUrl);
373
374 // The following code has been copied 1:1 from QColumnView::createColumn().
375 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
376 // QColumnView::initializeColumn() will be available for this.
377
378 view->setFrameShape(QFrame::NoFrame);
379 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
380 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
381 view->setMinimumWidth(100);
382 view->setAttribute(Qt::WA_MacShowFocusRect, false);
383
384 // copy the 'view' behavior
385 view->setDragDropMode(dragDropMode());
386 view->setDragDropOverwriteMode(dragDropOverwriteMode());
387 view->setDropIndicatorShown(showDropIndicator());
388 view->setAlternatingRowColors(alternatingRowColors());
389 view->setAutoScroll(hasAutoScroll());
390 view->setEditTriggers(editTriggers());
391 view->setHorizontalScrollMode(horizontalScrollMode());
392 view->setIconSize(iconSize());
393 view->setSelectionBehavior(selectionBehavior());
394 view->setSelectionMode(selectionMode());
395 view->setTabKeyNavigation(tabKeyNavigation());
396 view->setTextElideMode(textElideMode());
397 view->setVerticalScrollMode(verticalScrollMode());
398
399 view->setModel(model());
400
401 // set the delegate to be the columnview delegate
402 QAbstractItemDelegate* delegate = view->itemDelegate();
403 view->setItemDelegate(itemDelegate());
404 delete delegate;
405
406 view->setRootIndex(index);
407
408 if (model()->canFetchMore(index)) {
409 model()->fetchMore(index);
410 }
411
412 return view;
413 }
414
415 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
416 {
417 m_controller->triggerActivation();
418 QColumnView::mousePressEvent(event);
419 }
420
421 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
422 {
423 if (event->mimeData()->hasUrls()) {
424 event->acceptProposedAction();
425 }
426 }
427
428 void DolphinColumnView::dropEvent(QDropEvent* event)
429 {
430 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
431 if (!urls.isEmpty()) {
432 m_controller->indicateDroppedUrls(urls,
433 indexAt(event->pos()),
434 event->source());
435 event->acceptProposedAction();
436 }
437 QColumnView::dropEvent(event);
438 }
439
440 void DolphinColumnView::showEvent(QShowEvent* event)
441 {
442 QColumnView::showEvent(event);
443 if (!event->spontaneous()) {
444 // QColumnView might clear the selection for folders that are shown in the next column.
445 // As this is not wanted the selection is updated if the directory lister has been completed.
446 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
447 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
448 KDirLister* dirLister = dirModel->dirLister();
449 connect(dirLister, SIGNAL(completed()),
450 this, SLOT(updateSelections()));
451 }
452 }
453
454 void DolphinColumnView::zoomIn()
455 {
456 if (isZoomInPossible()) {
457 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
458 // TODO: get rid of K3Icon sizes
459 switch (settings->iconSize()) {
460 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
461 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
462 default: Q_ASSERT(false); break;
463 }
464 updateDecorationSize();
465 }
466 }
467
468 void DolphinColumnView::zoomOut()
469 {
470 if (isZoomOutPossible()) {
471 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
472 // TODO: get rid of K3Icon sizes
473 switch (settings->iconSize()) {
474 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
475 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
476 default: Q_ASSERT(false); break;
477 }
478 updateDecorationSize();
479 }
480 }
481
482 void DolphinColumnView::triggerItem(const QModelIndex& index)
483 {
484 m_controller->triggerItem(index);
485 updateColumnsState(m_controller->url());
486 }
487
488 void DolphinColumnView::updateColumnsState(const KUrl& url)
489 {
490 foreach (QObject* object, viewport()->children()) {
491 if (object->inherits("QListView")) {
492 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
493 widget->setActive(widget->url() == url);
494 }
495 }
496 }
497
498
499 void DolphinColumnView::updateDecorationSize()
500 {
501 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
502 const int iconSize = settings->iconSize();
503
504 foreach (QObject* object, viewport()->children()) {
505 if (object->inherits("QListView")) {
506 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
507 widget->setDecorationSize(QSize(iconSize, iconSize));
508 }
509 }
510
511 m_controller->setZoomInPossible(isZoomInPossible());
512 m_controller->setZoomOutPossible(isZoomOutPossible());
513
514 doItemsLayout();
515 }
516
517 void DolphinColumnView::updateSelections()
518 {
519 ColumnWidget* previousWidget = 0;
520 foreach (QObject* object, viewport()->children()) {
521 if (object->inherits("QListView")) {
522 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
523 if (previousWidget != 0) {
524 previousWidget->updateSelection(widget->url());
525 }
526 previousWidget = widget;
527 }
528 }
529 if (previousWidget != 0) {
530 previousWidget->updateSelection(KUrl());
531 }
532 }
533
534 bool DolphinColumnView::isZoomInPossible() const
535 {
536 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
537 return settings->iconSize() < K3Icon::SizeLarge;
538 }
539
540 bool DolphinColumnView::isZoomOutPossible() const
541 {
542 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
543 return settings->iconSize() > K3Icon::SizeSmall;
544 }
545
546 void DolphinColumnView::requestActivation(QWidget* column)
547 {
548 foreach (QObject* object, viewport()->children()) {
549 if (object->inherits("QListView")) {
550 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
551 const bool isActive = (widget == column);
552 widget->setActive(isActive);
553 if (isActive) {
554 m_controller->setUrl(widget->url());
555 }
556 }
557 }
558 updateSelections();
559 }
560
561 #include "dolphincolumnview.moc"