]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
assure that the context menu for column view is applied to the correct URL (depends...
[dolphin.git] / src / dolphincolumnview.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 "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 <kdirmodel.h>
30 #include <kdirlister.h>
31 #include <kfileitem.h>
32
33 #include <QAbstractProxyModel>
34 #include <QPoint>
35
36 /**
37 * Represents one column inside the DolphinColumnView and has been
38 * extended to respect view options and hovering information.
39 */
40 class ColumnWidget : public QListView
41 {
42 public:
43 ColumnWidget(QWidget* parent,
44 DolphinColumnView* columnView,
45 const KUrl& url);
46 virtual ~ColumnWidget();
47
48 /** Sets the size of the icons. */
49 void setDecorationSize(const QSize& size);
50
51 /**
52 * An active column is defined as column, which shows the same URL
53 * as indicated by the URL navigator. The active column is usually
54 * drawn in a lighter color. All operations are applied to this column.
55 */
56 void setActive(bool active);
57
58 inline const KUrl& url() const;
59
60 protected:
61 virtual QStyleOptionViewItem viewOptions() const;
62 virtual void dragEnterEvent(QDragEnterEvent* event);
63 virtual void dragLeaveEvent(QDragLeaveEvent* event);
64 virtual void dragMoveEvent(QDragMoveEvent* event);
65 virtual void dropEvent(QDropEvent* event);
66 virtual void mousePressEvent(QMouseEvent* event);
67 virtual void paintEvent(QPaintEvent* event);
68 virtual void contextMenuEvent(QContextMenuEvent* event);
69
70 private:
71 /** Used by ColumnWidget::setActive(). */
72 void activate();
73
74 /** Used by ColumnWidget::setActive(). */
75 void deactivate();
76
77 private:
78 bool m_active;
79 KUrl m_url;
80 DolphinColumnView* m_view;
81 QStyleOptionViewItem m_viewOptions;
82
83 bool m_dragging; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
84 QRect m_dropRect; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
85 };
86
87 ColumnWidget::ColumnWidget(QWidget* parent,
88 DolphinColumnView* columnView,
89 const KUrl& url) :
90 QListView(parent),
91 m_active(true),
92 m_url(url),
93 m_view(columnView),
94 m_dragging(false),
95 m_dropRect()
96 {
97 setAcceptDrops(true);
98 setDragDropMode(QAbstractItemView::DragDrop);
99 setDropIndicatorShown(false);
100
101 setMouseTracking(true);
102 viewport()->setAttribute(Qt::WA_Hover);
103
104 // apply the column mode settings to the widget
105 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
106 Q_ASSERT(settings != 0);
107
108 m_viewOptions = QListView::viewOptions();
109
110 QFont font(settings->fontFamily(), settings->fontSize());
111 font.setItalic(settings->italicFont());
112 font.setBold(settings->boldFont());
113 m_viewOptions.font = font;
114
115 const int iconSize = settings->iconSize();
116 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
117
118 activate();
119 }
120
121 ColumnWidget::~ColumnWidget()
122 {
123 }
124
125 void ColumnWidget::setDecorationSize(const QSize& size)
126 {
127 m_viewOptions.decorationSize = size;
128 doItemsLayout();
129 }
130
131 void ColumnWidget::setActive(bool active)
132 {
133 if (m_active == active) {
134 return;
135 }
136
137 m_active = active;
138
139 if (active) {
140 activate();
141 } else {
142 deactivate();
143 }
144 }
145
146 const KUrl& ColumnWidget::url() const
147 {
148 return m_url;
149 }
150
151 QStyleOptionViewItem ColumnWidget::viewOptions() const
152 {
153 return m_viewOptions;
154 }
155
156 void ColumnWidget::dragEnterEvent(QDragEnterEvent* event)
157 {
158 if (event->mimeData()->hasUrls()) {
159 event->acceptProposedAction();
160 }
161
162 m_dragging = true;
163 }
164
165 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
166 {
167 QListView::dragLeaveEvent(event);
168
169 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
170 m_dragging = false;
171 setDirtyRegion(m_dropRect);
172 }
173
174 void ColumnWidget::dragMoveEvent(QDragMoveEvent* event)
175 {
176 QListView::dragMoveEvent(event);
177
178 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
179 const QModelIndex index = indexAt(event->pos());
180 setDirtyRegion(m_dropRect);
181 m_dropRect = visualRect(index);
182 setDirtyRegion(m_dropRect);
183 }
184
185 void ColumnWidget::dropEvent(QDropEvent* event)
186 {
187 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
188 if (!urls.isEmpty()) {
189 event->acceptProposedAction();
190 m_view->m_controller->indicateDroppedUrls(urls,
191 indexAt(event->pos()),
192 event->source());
193 }
194 QListView::dropEvent(event);
195 m_dragging = false;
196 }
197
198 void ColumnWidget::mousePressEvent(QMouseEvent* event)
199 {
200 if (m_active || indexAt(event->pos()).isValid()) {
201 // Only accept the mouse press event in inactive views,
202 // if a click is done on an item. This assures that
203 // the current selection, which usually shows the
204 // the directory for next column, won't get deleted.
205 QListView::mousePressEvent(event);
206 }
207 }
208
209 void ColumnWidget::paintEvent(QPaintEvent* event)
210 {
211 QListView::paintEvent(event);
212
213 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
214 if (m_dragging) {
215 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
216 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
217 }
218 }
219
220 void ColumnWidget::contextMenuEvent(QContextMenuEvent* event)
221 {
222 if (m_view->viewport()->children().first() == this) {
223 // This column widget represents the root column. DolphinColumnView::createColumn()
224 // cannot retrieve the correct URL at this stage, as the directory lister will be
225 // started after the model has been assigned. This will be fixed here, where it is
226 // assured that the directory lister has been started already.
227 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
228 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
229 const KDirLister* dirLister = dirModel->dirLister();
230 m_url = dirLister->url();
231 }
232
233 QListView::contextMenuEvent(event);
234 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
235 m_view->m_controller->triggerContextMenuRequest(pos, m_url);
236 }
237
238 void ColumnWidget::activate()
239 {
240 const QColor bgColor = KColorScheme(KColorScheme::View).background();
241 QPalette palette = viewport()->palette();
242 palette.setColor(viewport()->backgroundRole(), bgColor);
243 viewport()->setPalette(palette);
244
245 setSelectionMode(MultiSelection);
246 }
247
248 void ColumnWidget::deactivate()
249 {
250 QColor bgColor = KColorScheme(KColorScheme::View).background();
251 const QColor fgColor = KColorScheme(KColorScheme::View).foreground();
252 bgColor = KColorUtils::mix(bgColor, fgColor, 0.04);
253
254 QPalette palette = viewport()->palette();
255 palette.setColor(viewport()->backgroundRole(), bgColor);
256 viewport()->setPalette(palette);
257
258 setSelectionMode(SingleSelection);
259 }
260
261 // ---
262
263 DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
264 QColumnView(parent),
265 m_controller(controller)
266 {
267 Q_ASSERT(controller != 0);
268
269 setAcceptDrops(true);
270 setSelectionBehavior(SelectItems);
271 setDragDropMode(QAbstractItemView::DragDrop);
272 setDropIndicatorShown(false);
273
274 if (KGlobalSettings::singleClick()) {
275 connect(this, SIGNAL(clicked(const QModelIndex&)),
276 this, SLOT(triggerItem(const QModelIndex&)));
277 } else {
278 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
279 this, SLOT(triggerItem(const QModelIndex&)));
280 }
281 connect(this, SIGNAL(activated(const QModelIndex&)),
282 this, SLOT(triggerItem(const QModelIndex&)));
283 connect(this, SIGNAL(entered(const QModelIndex&)),
284 controller, SLOT(emitItemEntered(const QModelIndex&)));
285 connect(this, SIGNAL(viewportEntered()),
286 controller, SLOT(emitViewportEntered()));
287 connect(controller, SIGNAL(zoomIn()),
288 this, SLOT(zoomIn()));
289 connect(controller, SIGNAL(zoomOut()),
290 this, SLOT(zoomOut()));
291
292 updateDecorationSize();
293 }
294
295 DolphinColumnView::~DolphinColumnView()
296 {
297 }
298
299 QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
300 {
301 // To be able to visually indicate whether a column is active (which means
302 // that it represents the content of the URL navigator), the column
303 // must remember its URL.
304 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
305 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
306
307 const QModelIndex dirModelIndex = proxyModel->mapToSource(index);
308 KFileItem* fileItem = dirModel->itemForIndex(dirModelIndex);
309
310 KUrl columnUrl;
311 if (fileItem != 0) {
312 columnUrl = fileItem->url();
313 }
314
315 ColumnWidget* view = new ColumnWidget(viewport(),
316 this,
317 columnUrl);
318
319 // The following code has been copied 1:1 from QColumnView::createColumn().
320 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
321 // QColumnView::initializeColumn() will be available for this.
322
323 view->setFrameShape(QFrame::NoFrame);
324 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
325 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
326 view->setMinimumWidth(100);
327 view->setAttribute(Qt::WA_MacShowFocusRect, false);
328
329 // copy the 'view' behavior
330 view->setDragDropMode(dragDropMode());
331 view->setDragDropOverwriteMode(dragDropOverwriteMode());
332 view->setDropIndicatorShown(showDropIndicator());
333 view->setAlternatingRowColors(alternatingRowColors());
334 view->setAutoScroll(hasAutoScroll());
335 view->setEditTriggers(editTriggers());
336 view->setHorizontalScrollMode(horizontalScrollMode());
337 view->setIconSize(iconSize());
338 view->setSelectionBehavior(selectionBehavior());
339 view->setSelectionMode(selectionMode());
340 view->setTabKeyNavigation(tabKeyNavigation());
341 view->setTextElideMode(textElideMode());
342 view->setVerticalScrollMode(verticalScrollMode());
343
344 view->setModel(model());
345
346 // set the delegate to be the columnview delegate
347 QAbstractItemDelegate* delegate = view->itemDelegate();
348 view->setItemDelegate(itemDelegate());
349 delete delegate;
350
351 view->setRootIndex(index);
352
353 if (model()->canFetchMore(index)) {
354 model()->fetchMore(index);
355 }
356
357 return view;
358 }
359
360 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
361 {
362 m_controller->triggerActivation();
363 QColumnView::mousePressEvent(event);
364 }
365
366 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
367 {
368 if (event->mimeData()->hasUrls()) {
369 event->acceptProposedAction();
370 }
371 }
372
373 void DolphinColumnView::dropEvent(QDropEvent* event)
374 {
375 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
376 if (!urls.isEmpty()) {
377 m_controller->indicateDroppedUrls(urls,
378 indexAt(event->pos()),
379 event->source());
380 event->acceptProposedAction();
381 }
382 QColumnView::dropEvent(event);
383 }
384
385 void DolphinColumnView::zoomIn()
386 {
387 if (isZoomInPossible()) {
388 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
389 // TODO: get rid of K3Icon sizes
390 switch (settings->iconSize()) {
391 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
392 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
393 default: Q_ASSERT(false); break;
394 }
395 updateDecorationSize();
396 }
397 }
398
399 void DolphinColumnView::zoomOut()
400 {
401 if (isZoomOutPossible()) {
402 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
403 // TODO: get rid of K3Icon sizes
404 switch (settings->iconSize()) {
405 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
406 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
407 default: Q_ASSERT(false); break;
408 }
409 updateDecorationSize();
410 }
411 }
412
413 void DolphinColumnView::triggerItem(const QModelIndex& index)
414 {
415 m_controller->triggerItem(index);
416
417 // Update the activation state of all columns. Only the column
418 // which represents the URL of the URL navigator is marked as active.
419 const KUrl& navigatorUrl = m_controller->url();
420 foreach (QObject* object, viewport()->children()) {
421 if (object->inherits("QListView")) {
422 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
423 widget->setActive(navigatorUrl == widget->url());
424 }
425 }
426 }
427
428 bool DolphinColumnView::isZoomInPossible() const
429 {
430 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
431 return settings->iconSize() < K3Icon::SizeLarge;
432 }
433
434 bool DolphinColumnView::isZoomOutPossible() const
435 {
436 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
437 return settings->iconSize() > K3Icon::SizeSmall;
438 }
439
440 void DolphinColumnView::updateDecorationSize()
441 {
442 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
443 const int iconSize = settings->iconSize();
444
445 foreach (QObject* object, viewport()->children()) {
446 if (object->inherits("QListView")) {
447 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
448 widget->setDecorationSize(QSize(iconSize, iconSize));
449 }
450 }
451
452 m_controller->setZoomInPossible(isZoomInPossible());
453 m_controller->setZoomOutPossible(isZoomOutPossible());
454
455 doItemsLayout();
456 }
457
458 #include "dolphincolumnview.moc"