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