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