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