]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
Add needed method for keyboard navigation
[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(entered(const QModelIndex&)),
261 controller, SLOT(emitItemEntered(const QModelIndex&)));
262 connect(this, SIGNAL(viewportEntered()),
263 controller, SLOT(emitViewportEntered()));
264 connect(controller, SIGNAL(zoomIn()),
265 this, SLOT(zoomIn()));
266 connect(controller, SIGNAL(zoomOut()),
267 this, SLOT(zoomOut()));
268
269 updateDecorationSize();
270 }
271
272 DolphinColumnView::~DolphinColumnView()
273 {
274 }
275
276 QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
277 {
278 ColumnWidget* view = new ColumnWidget(viewport(), this);
279
280 // The following code has been copied 1:1 from QColumnView::createColumn().
281 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
282 // QColumnView::initializeColumn() will be available for this.
283
284 view->setFrameShape(QFrame::NoFrame);
285 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
286 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
287 view->setMinimumWidth(100);
288 view->setAttribute(Qt::WA_MacShowFocusRect, false);
289
290 // copy the 'view' behavior
291 view->setDragDropMode(dragDropMode());
292 view->setDragDropOverwriteMode(dragDropOverwriteMode());
293 view->setDropIndicatorShown(showDropIndicator());
294 view->setAlternatingRowColors(alternatingRowColors());
295 view->setAutoScroll(hasAutoScroll());
296 view->setEditTriggers(editTriggers());
297 view->setHorizontalScrollMode(horizontalScrollMode());
298 view->setIconSize(iconSize());
299 view->setSelectionBehavior(selectionBehavior());
300 view->setSelectionMode(selectionMode());
301 view->setTabKeyNavigation(tabKeyNavigation());
302 view->setTextElideMode(textElideMode());
303 view->setVerticalScrollMode(verticalScrollMode());
304
305 view->setModel(model());
306
307 // set the delegate to be the columnview delegate
308 QAbstractItemDelegate* delegate = view->itemDelegate();
309 view->setItemDelegate(itemDelegate());
310 delete delegate;
311
312 view->setRootIndex(index);
313
314 if (model()->canFetchMore(index)) {
315 model()->fetchMore(index);
316 }
317
318 return view;
319 }
320
321 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
322 {
323 m_controller->triggerActivation();
324 QColumnView::mousePressEvent(event);
325 }
326
327 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
328 {
329 if (event->mimeData()->hasUrls()) {
330 event->acceptProposedAction();
331 }
332 }
333
334 void DolphinColumnView::dropEvent(QDropEvent* event)
335 {
336 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
337 if (!urls.isEmpty()) {
338 m_controller->indicateDroppedUrls(urls,
339 indexAt(event->pos()),
340 event->source());
341 event->acceptProposedAction();
342 }
343 QColumnView::dropEvent(event);
344 }
345
346 void DolphinColumnView::zoomIn()
347 {
348 if (isZoomInPossible()) {
349 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
350 // TODO: get rid of K3Icon sizes
351 switch (settings->iconSize()) {
352 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
353 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
354 default: Q_ASSERT(false); break;
355 }
356 updateDecorationSize();
357 }
358 }
359
360 void DolphinColumnView::zoomOut()
361 {
362 if (isZoomOutPossible()) {
363 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
364 // TODO: get rid of K3Icon sizes
365 switch (settings->iconSize()) {
366 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
367 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
368 default: Q_ASSERT(false); break;
369 }
370 updateDecorationSize();
371 }
372 }
373
374 void DolphinColumnView::triggerItem(const QModelIndex& index)
375 {
376 m_controller->triggerItem(index);
377
378 // assure that the last column gets marked as active and all
379 // other columns as inactive
380 QObject* lastWidget = viewport()->children().last();
381 foreach (QObject* object, viewport()->children()) {
382 if (object->inherits("QListView")) {
383 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
384 widget->setActive(widget == lastWidget);
385 }
386 }
387 }
388
389 bool DolphinColumnView::isZoomInPossible() const
390 {
391 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
392 return settings->iconSize() < K3Icon::SizeLarge;
393 }
394
395 bool DolphinColumnView::isZoomOutPossible() const
396 {
397 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
398 return settings->iconSize() > K3Icon::SizeSmall;
399 }
400
401 void DolphinColumnView::updateDecorationSize()
402 {
403 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
404 const int iconSize = settings->iconSize();
405
406 foreach (QObject* object, viewport()->children()) {
407 if (object->inherits("QListView")) {
408 ColumnWidget* widget = static_cast<ColumnWidget*>(object);
409 widget->setDecorationSize(QSize(iconSize, iconSize));
410 }
411 }
412
413 m_controller->setZoomInPossible(isZoomInPossible());
414 m_controller->setZoomOutPossible(isZoomOutPossible());
415
416 doItemsLayout();
417 }
418
419 #include "dolphincolumnview.moc"