]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
Allow to add menu groups like "View Mode", "Sort By"... to be added as toolbar item...
[dolphin.git] / src / dolphincolumnview.cpp
1 /***************************************************************************
2 * Copyright (C) 2007-2009 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 "dolphinmodel.h"
23 #include "dolphincolumnviewcontainer.h"
24 #include "dolphinviewcontroller.h"
25 #include "dolphindirlister.h"
26 #include "dolphinsortfilterproxymodel.h"
27 #include "settings/dolphinsettings.h"
28 #include "dolphinviewautoscroller.h"
29 #include "dolphin_columnmodesettings.h"
30 #include "dolphin_generalsettings.h"
31 #include "draganddrophelper.h"
32 #include "folderexpander.h"
33 #include "tooltips/tooltipmanager.h"
34 #include "viewextensionsfactory.h"
35 #include "viewmodecontroller.h"
36 #include "zoomlevelinfo.h"
37
38 #include <kcolorscheme.h>
39 #include <kdirlister.h>
40 #include <kfileitem.h>
41 #include <kio/previewjob.h>
42 #include <kiconeffect.h>
43 #include <kjob.h>
44 #include <konqmimedata.h>
45
46 #include <QApplication>
47 #include <QClipboard>
48 #include <QPainter>
49 #include <QPoint>
50 #include <QScrollBar>
51
52 DolphinColumnView::DolphinColumnView(QWidget* parent,
53 DolphinColumnViewContainer* container,
54 const KUrl& url) :
55 QListView(parent),
56 m_active(false),
57 m_container(container),
58 m_extensionsFactory(0),
59 m_url(url),
60 m_childUrl(),
61 m_font(),
62 m_decorationSize(),
63 m_dirLister(0),
64 m_dolphinModel(0),
65 m_proxyModel(0),
66 m_dropRect()
67 {
68 setMouseTracking(true);
69 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
70 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
71 setSelectionBehavior(SelectItems);
72 setSelectionMode(QAbstractItemView::ExtendedSelection);
73 setDragDropMode(QAbstractItemView::DragDrop);
74 setDropIndicatorShown(false);
75 setSelectionRectVisible(true);
76 setEditTriggers(QAbstractItemView::NoEditTriggers);
77
78 setVerticalScrollMode(QListView::ScrollPerPixel);
79 setHorizontalScrollMode(QListView::ScrollPerPixel);
80
81 // apply the column mode settings to the widget
82 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
83 Q_ASSERT(settings != 0);
84
85 if (settings->useSystemFont()) {
86 m_font = KGlobalSettings::generalFont();
87 } else {
88 m_font = QFont(settings->fontFamily(),
89 qRound(settings->fontSize()),
90 settings->fontWeight(),
91 settings->italicFont());
92 m_font.setPointSizeF(settings->fontSize());
93 }
94
95 connect(this, SIGNAL(viewportEntered()),
96 m_container->m_dolphinViewController, SLOT(emitViewportEntered()));
97 connect(this, SIGNAL(entered(const QModelIndex&)),
98 this, SLOT(slotEntered(const QModelIndex&)));
99
100 const DolphinView* dolphinView = m_container->m_dolphinViewController->view();
101 connect(dolphinView, SIGNAL(showPreviewChanged()),
102 this, SLOT(slotShowPreviewChanged()));
103
104 m_dirLister = new DolphinDirLister();
105 m_dirLister->setAutoUpdate(true);
106 m_dirLister->setMainWindow(window());
107 m_dirLister->setDelayedMimeTypes(true);
108 const bool showHiddenFiles = m_container->m_dolphinViewController->view()->showHiddenFiles();
109 m_dirLister->setShowingDotFiles(showHiddenFiles);
110
111 m_dolphinModel = new DolphinModel(this);
112 m_dolphinModel->setDirLister(m_dirLister);
113 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
114
115 m_proxyModel = new DolphinSortFilterProxyModel(this);
116 m_proxyModel->setSourceModel(m_dolphinModel);
117 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
118
119 m_proxyModel->setSorting(dolphinView->sorting());
120 m_proxyModel->setSortOrder(dolphinView->sortOrder());
121 m_proxyModel->setSortFoldersFirst(dolphinView->sortFoldersFirst());
122
123 setModel(m_proxyModel);
124
125 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
126 this, SLOT(updateFont()));
127
128 DolphinViewController* dolphinViewController = m_container->m_dolphinViewController;
129 connect(dolphinViewController, SIGNAL(zoomLevelChanged(int)),
130 this, SLOT(setZoomLevel(int)));
131
132 const ViewModeController* viewModeController = m_container->m_viewModeController;
133 const QString nameFilter = viewModeController->nameFilter();
134 if (!nameFilter.isEmpty()) {
135 m_proxyModel->setFilterFixedString(nameFilter);
136 }
137
138 updateDecorationSize(dolphinView->showPreview());
139 updateBackground();
140 m_extensionsFactory = new ViewExtensionsFactory(this, dolphinViewController, viewModeController);
141
142 m_dirLister->openUrl(url, KDirLister::NoFlags);
143 }
144
145 DolphinColumnView::~DolphinColumnView()
146 {
147 delete m_proxyModel;
148 m_proxyModel = 0;
149 delete m_dolphinModel;
150 m_dolphinModel = 0;
151 m_dirLister = 0; // deleted by m_dolphinModel
152 }
153
154 void DolphinColumnView::setActive(bool active)
155 {
156 if (active && (m_container->focusProxy() != this)) {
157 m_container->setFocusProxy(this);
158 }
159
160 if (m_active != active) {
161 m_active = active;
162
163 if (active) {
164 activate();
165 } else {
166 deactivate();
167 }
168 }
169 }
170
171 void DolphinColumnView::updateBackground()
172 {
173 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
174 // cleaning up the cut-indication of DolphinColumnView with the code from
175 // DolphinView a common helper-class should be available which can be shared
176 // by all view implementations -> no hardcoded value anymore
177 const QPalette::ColorRole role = viewport()->backgroundRole();
178 QColor color = viewport()->palette().color(role);
179 color.setAlpha((m_active && m_container->m_active) ? 255 : 150);
180
181 QPalette palette = viewport()->palette();
182 palette.setColor(role, color);
183 viewport()->setPalette(palette);
184
185 update();
186 }
187
188 KFileItem DolphinColumnView::itemAt(const QPoint& pos) const
189 {
190 KFileItem item;
191 const QModelIndex index = indexAt(pos);
192 if (index.isValid() && (index.column() == DolphinModel::Name)) {
193 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
194 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
195 }
196 return item;
197 }
198
199 void DolphinColumnView::setSelectionModel(QItemSelectionModel* model)
200 {
201 // If a change of the selection is done although the view is not active
202 // (e. g. by the selection markers), the column must be activated. This
203 // is done by listening to the current selectionChanged() signal.
204 if (selectionModel() != 0) {
205 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
206 this, SLOT(requestActivation()));
207 }
208
209 QListView::setSelectionModel(model);
210
211 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
212 this, SLOT(requestActivation()));
213 }
214
215 QStyleOptionViewItem DolphinColumnView::viewOptions() const
216 {
217 QStyleOptionViewItem viewOptions = QListView::viewOptions();
218 viewOptions.font = m_font;
219 viewOptions.fontMetrics = QFontMetrics(m_font);
220 viewOptions.decorationSize = m_decorationSize;
221 viewOptions.showDecorationSelected = true;
222 return viewOptions;
223 }
224
225 void DolphinColumnView::startDrag(Qt::DropActions supportedActions)
226 {
227 DragAndDropHelper::instance().startDrag(this, supportedActions, m_container->m_dolphinViewController);
228 }
229
230 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
231 {
232 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
233 event->acceptProposedAction();
234 requestActivation();
235 }
236 }
237
238 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent* event)
239 {
240 QListView::dragLeaveEvent(event);
241 setDirtyRegion(m_dropRect);
242 }
243
244 void DolphinColumnView::dragMoveEvent(QDragMoveEvent* event)
245 {
246 QListView::dragMoveEvent(event);
247
248 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
249 const QModelIndex index = indexAt(event->pos());
250 setDirtyRegion(m_dropRect);
251
252 m_dropRect.setSize(QSize()); // set as invalid
253 if (index.isValid()) {
254 m_container->m_dolphinViewController->setItemView(this);
255 const KFileItem item = m_container->m_dolphinViewController->itemForIndex(index);
256 if (!item.isNull() && item.isDir()) {
257 m_dropRect = visualRect(index);
258 }
259 }
260 setDirtyRegion(m_dropRect);
261
262 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
263 // accept url drops, independently from the destination item
264 event->acceptProposedAction();
265 }
266 }
267
268 void DolphinColumnView::dropEvent(QDropEvent* event)
269 {
270 const QModelIndex index = indexAt(event->pos());
271 m_container->m_dolphinViewController->setItemView(this);
272 const KFileItem item = m_container->m_dolphinViewController->itemForIndex(index);
273 m_container->m_dolphinViewController->indicateDroppedUrls(item, url(), event);
274 QListView::dropEvent(event);
275 }
276
277 void DolphinColumnView::paintEvent(QPaintEvent* event)
278 {
279 if (!m_childUrl.isEmpty()) {
280 // indicate the shown URL of the next column by highlighting the shown folder item
281 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
282 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
283 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
284 const QRect itemRect = visualRect(proxyIndex);
285 QPainter painter(viewport());
286 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
287 color.setAlpha(32);
288 painter.setPen(Qt::NoPen);
289 painter.setBrush(color);
290 painter.drawRect(itemRect);
291 }
292 }
293
294 QListView::paintEvent(event);
295 }
296
297 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
298 {
299 requestActivation();
300 if (!indexAt(event->pos()).isValid()) {
301 if (QApplication::mouseButtons() & Qt::MidButton) {
302 m_container->m_dolphinViewController->replaceUrlByClipboard();
303 }
304 } else if (event->button() == Qt::LeftButton) {
305 // TODO: see comment in DolphinIconsView::mousePressEvent()
306 setState(QAbstractItemView::DraggingState);
307 }
308 QListView::mousePressEvent(event);
309 }
310
311 void DolphinColumnView::keyPressEvent(QKeyEvent* event)
312 {
313 QListView::keyPressEvent(event);
314 requestActivation();
315
316 DolphinViewController* controller = m_container->m_dolphinViewController;
317 controller->handleKeyPressEvent(event);
318 switch (event->key()) {
319 case Qt::Key_Right: {
320 // Special key handling for the column: A Key_Right should
321 // open a new column for the currently selected folder.
322 const QModelIndex index = currentIndex();
323 const KFileItem item = controller->itemForIndex(index);
324 if (!item.isNull() && item.isDir()) {
325 controller->emitItemTriggered(item);
326 }
327 break;
328 }
329
330 case Qt::Key_Escape:
331 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
332 QItemSelectionModel::Current |
333 QItemSelectionModel::Clear);
334 break;
335
336 default:
337 break;
338 }
339 }
340
341 void DolphinColumnView::contextMenuEvent(QContextMenuEvent* event)
342 {
343 requestActivation();
344 QListView::contextMenuEvent(event);
345 m_container->m_dolphinViewController->triggerContextMenuRequest(event->pos());
346 }
347
348 void DolphinColumnView::wheelEvent(QWheelEvent* event)
349 {
350 const int step = m_decorationSize.height();
351 verticalScrollBar()->setSingleStep(step);
352 QListView::wheelEvent(event);
353 }
354
355 void DolphinColumnView::leaveEvent(QEvent* event)
356 {
357 QListView::leaveEvent(event);
358 // if the mouse is above an item and moved very fast outside the widget,
359 // no viewportEntered() signal might be emitted although the mouse has been moved
360 // above the viewport
361 m_container->m_dolphinViewController->emitViewportEntered();
362 }
363
364 void DolphinColumnView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
365 {
366 QListView::currentChanged(current, previous);
367 m_extensionsFactory->handleCurrentIndexChange(current, previous);
368 }
369
370 void DolphinColumnView::setZoomLevel(int level)
371 {
372 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
373 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
374
375 const bool showPreview = m_container->m_dolphinViewController->view()->showPreview();
376 if (showPreview) {
377 settings->setPreviewSize(size);
378 } else {
379 settings->setIconSize(size);
380 }
381
382 updateDecorationSize(showPreview);
383 }
384
385 void DolphinColumnView::slotEntered(const QModelIndex& index)
386 {
387 m_container->m_dolphinViewController->setItemView(this);
388 m_container->m_dolphinViewController->emitItemEntered(index);
389 }
390
391 void DolphinColumnView::requestActivation()
392 {
393 m_container->m_dolphinViewController->requestActivation();
394 if (!m_active) {
395 m_container->requestActivation(this);
396 selectionModel()->clear();
397 }
398 }
399
400 void DolphinColumnView::updateFont()
401 {
402 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
403 Q_ASSERT(settings != 0);
404
405 if (settings->useSystemFont()) {
406 m_font = KGlobalSettings::generalFont();
407 }
408 }
409
410 void DolphinColumnView::slotShowPreviewChanged()
411 {
412 const DolphinView* view = m_container->m_dolphinViewController->view();
413 updateDecorationSize(view->showPreview());
414 }
415
416 void DolphinColumnView::activate()
417 {
418 setFocus(Qt::OtherFocusReason);
419
420 if (KGlobalSettings::singleClick()) {
421 connect(this, SIGNAL(clicked(const QModelIndex&)),
422 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
423 } else {
424 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
425 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
426 }
427
428 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
429 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
430 }
431
432 updateBackground();
433 }
434
435 void DolphinColumnView::deactivate()
436 {
437 clearFocus();
438 if (KGlobalSettings::singleClick()) {
439 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
440 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
441 } else {
442 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
443 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
444 }
445
446 const QModelIndex current = selectionModel()->currentIndex();
447 selectionModel()->clear();
448 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
449 updateBackground();
450 }
451
452 void DolphinColumnView::updateDecorationSize(bool showPreview)
453 {
454 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
455 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
456 const QSize size(iconSize, iconSize);
457 setIconSize(size);
458
459 m_decorationSize = size;
460
461 doItemsLayout();
462 }
463
464 #include "dolphincolumnview.moc"