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