]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
Fix the problem "the scrollbar remains if it was shown on the
[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 <kfileitem.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 mousePressEvent(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 KUrl m_url;
79 DolphinColumnView* m_columnView;
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_url(url),
92 m_columnView(columnView),
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_columnView->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::mousePressEvent(QMouseEvent* event)
198 {
199 if (m_active || indexAt(event->pos()).isValid()) {
200 // Only accept the mouse press event in inactive views,
201 // if a click is done on an item. This assures that
202 // the current selection, which usually shows the
203 // the directory for next column, won't get deleted.
204 QListView::mousePressEvent(event);
205 }
206 }
207
208 void ColumnWidget::paintEvent(QPaintEvent* event)
209 {
210 QListView::paintEvent(event);
211
212 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
213 if (m_dragging) {
214 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
215 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
216 }
217 }
218
219 void ColumnWidget::contextMenuEvent(QContextMenuEvent* event)
220 {
221 QListView::contextMenuEvent(event);
222 m_columnView->m_controller->triggerContextMenuRequest(event->pos(), m_url);
223 }
224
225 void ColumnWidget::activate()
226 {
227 const QColor bgColor = KColorScheme(KColorScheme::View).background();
228 QPalette palette = viewport()->palette();
229 palette.setColor(viewport()->backgroundRole(), bgColor);
230 viewport()->setPalette(palette);
231
232 setSelectionMode(MultiSelection);
233 }
234
235 void ColumnWidget::deactivate()
236 {
237 QColor bgColor = KColorScheme(KColorScheme::View).background();
238 const QColor fgColor = KColorScheme(KColorScheme::View).foreground();
239 bgColor = KColorUtils::mix(bgColor, fgColor, 0.04);
240
241 QPalette palette = viewport()->palette();
242 palette.setColor(viewport()->backgroundRole(), bgColor);
243 viewport()->setPalette(palette);
244
245 setSelectionMode(SingleSelection);
246 }
247
248 // ---
249
250 DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
251 QColumnView(parent),
252 m_controller(controller)
253 {
254 Q_ASSERT(controller != 0);
255
256 setAcceptDrops(true);
257 setSelectionBehavior(SelectItems);
258 setDragDropMode(QAbstractItemView::DragDrop);
259 setDropIndicatorShown(false);
260
261 if (KGlobalSettings::singleClick()) {
262 connect(this, SIGNAL(clicked(const QModelIndex&)),
263 this, SLOT(triggerItem(const QModelIndex&)));
264 } else {
265 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
266 this, SLOT(triggerItem(const QModelIndex&)));
267 }
268 connect(this, SIGNAL(activated(const QModelIndex&)),
269 this, SLOT(triggerItem(const QModelIndex&)));
270 connect(this, SIGNAL(entered(const QModelIndex&)),
271 controller, SLOT(emitItemEntered(const QModelIndex&)));
272 connect(this, SIGNAL(viewportEntered()),
273 controller, SLOT(emitViewportEntered()));
274 connect(controller, SIGNAL(zoomIn()),
275 this, SLOT(zoomIn()));
276 connect(controller, SIGNAL(zoomOut()),
277 this, SLOT(zoomOut()));
278
279 updateDecorationSize();
280 }
281
282 DolphinColumnView::~DolphinColumnView()
283 {
284 }
285
286 QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
287 {
288 // To be able to visually indicate whether a column is active (which means
289 // that it represents the content of the URL navigator), the column
290 // must remember its URL.
291 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
292 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
293
294 const QModelIndex dirModelIndex = proxyModel->mapToSource(index);
295 KFileItem* fileItem = dirModel->itemForIndex(dirModelIndex);
296
297 KUrl columnUrl;
298 if (fileItem != 0) {
299 columnUrl = fileItem->url();
300 }
301
302 ColumnWidget* view = new ColumnWidget(viewport(),
303 this,
304 columnUrl);
305
306 // The following code has been copied 1:1 from QColumnView::createColumn().
307 // Copyright (C) 1992-2007 Trolltech ASA.
308 // It would be nice if QColumnView would offer a protected method for this
309 // (already send input to Benjamin, hopefully possible in Qt4.4)
310
311 view->setFrameShape(QFrame::NoFrame);
312 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
313 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
314 view->setMinimumWidth(100);
315 view->setAttribute(Qt::WA_MacShowFocusRect, false);
316
317 // copy the 'view' behavior
318 view->setDragDropMode(dragDropMode());
319 view->setDragDropOverwriteMode(dragDropOverwriteMode());
320 view->setDropIndicatorShown(showDropIndicator());
321 view->setAlternatingRowColors(alternatingRowColors());
322 view->setAutoScroll(hasAutoScroll());
323 view->setEditTriggers(editTriggers());
324 view->setHorizontalScrollMode(horizontalScrollMode());
325 view->setIconSize(iconSize());
326 view->setSelectionBehavior(selectionBehavior());
327 view->setSelectionMode(selectionMode());
328 view->setTabKeyNavigation(tabKeyNavigation());
329 view->setTextElideMode(textElideMode());
330 view->setVerticalScrollMode(verticalScrollMode());
331
332 view->setModel(model());
333
334 // set the delegate to be the columnview delegate
335 QAbstractItemDelegate* delegate = view->itemDelegate();
336 view->setItemDelegate(itemDelegate());
337 delete delegate;
338
339 view->setRootIndex(index);
340
341 if (model()->canFetchMore(index)) {
342 model()->fetchMore(index);
343 }
344
345 return view;
346 }
347
348 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
349 {
350 m_controller->triggerActivation();
351 QColumnView::mousePressEvent(event);
352 }
353
354 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
355 {
356 if (event->mimeData()->hasUrls()) {
357 event->acceptProposedAction();
358 }
359 }
360
361 void DolphinColumnView::dropEvent(QDropEvent* event)
362 {
363 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
364 if (!urls.isEmpty()) {
365 m_controller->indicateDroppedUrls(urls,
366 indexAt(event->pos()),
367 event->source());
368 event->acceptProposedAction();
369 }
370 QColumnView::dropEvent(event);
371 }
372
373 void DolphinColumnView::zoomIn()
374 {
375 if (isZoomInPossible()) {
376 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
377 // TODO: get rid of K3Icon sizes
378 switch (settings->iconSize()) {
379 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
380 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
381 default: Q_ASSERT(false); break;
382 }
383 updateDecorationSize();
384 }
385 }
386
387 void DolphinColumnView::zoomOut()
388 {
389 if (isZoomOutPossible()) {
390 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
391 // TODO: get rid of K3Icon sizes
392 switch (settings->iconSize()) {
393 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
394 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
395 default: Q_ASSERT(false); break;
396 }
397 updateDecorationSize();
398 }
399 }
400
401 void DolphinColumnView::triggerItem(const QModelIndex& index)
402 {
403 m_controller->triggerItem(index);
404
405 // Update the activation state of all columns. Only the column
406 // which represents the URL of the URL navigator is marked as active.
407 const KUrl& navigatorUrl = m_controller->url();
408 foreach (QObject* object, viewport()->children()) {
409 if (object->inherits("QListView")) {
410 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
411 widget->setActive(navigatorUrl == widget->url());
412 }
413 }
414 }
415
416 bool DolphinColumnView::isZoomInPossible() const
417 {
418 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
419 return settings->iconSize() < K3Icon::SizeLarge;
420 }
421
422 bool DolphinColumnView::isZoomOutPossible() const
423 {
424 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
425 return settings->iconSize() > K3Icon::SizeSmall;
426 }
427
428 void DolphinColumnView::updateDecorationSize()
429 {
430 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
431 const int iconSize = settings->iconSize();
432
433 foreach (QObject* object, viewport()->children()) {
434 if (object->inherits("QListView")) {
435 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
436 widget->setDecorationSize(QSize(iconSize, iconSize));
437 }
438 }
439
440 m_controller->setZoomInPossible(isZoomInPossible());
441 m_controller->setZoomOutPossible(isZoomOutPossible());
442
443 doItemsLayout();
444 }
445
446 #include "dolphincolumnview.moc"