]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
SVN_SILENT compile
[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 "dolphincontroller.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 "versioncontrolobserver.h"
35 #include "viewextensionsfactory.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 settings->fontSize(),
90 settings->fontWeight(),
91 settings->italicFont());
92 }
93
94 activate();
95
96 connect(this, SIGNAL(viewportEntered()),
97 m_container->m_controller, SLOT(emitViewportEntered()));
98 connect(this, SIGNAL(entered(const QModelIndex&)),
99 this, SLOT(slotEntered(const QModelIndex&)));
100
101 const DolphinView* dolphinView = m_container->m_controller->dolphinView();
102 connect(dolphinView, SIGNAL(sortingChanged(DolphinView::Sorting)),
103 this, SLOT(slotSortingChanged(DolphinView::Sorting)));
104 connect(dolphinView, SIGNAL(sortOrderChanged(Qt::SortOrder)),
105 this, SLOT(slotSortOrderChanged(Qt::SortOrder)));
106 connect(dolphinView, SIGNAL(sortFoldersFirstChanged(bool)),
107 this, SLOT(slotSortFoldersFirstChanged(bool)));
108 connect(dolphinView, SIGNAL(showHiddenFilesChanged()),
109 this, SLOT(slotShowHiddenFilesChanged()));
110 connect(dolphinView, SIGNAL(showPreviewChanged()),
111 this, SLOT(slotShowPreviewChanged()));
112
113 m_dirLister = new DolphinDirLister();
114 m_dirLister->setAutoUpdate(true);
115 m_dirLister->setMainWindow(window());
116 m_dirLister->setDelayedMimeTypes(true);
117 const bool showHiddenFiles = m_container->m_controller->dolphinView()->showHiddenFiles();
118 m_dirLister->setShowingDotFiles(showHiddenFiles);
119
120 m_dolphinModel = new DolphinModel(this);
121 m_dolphinModel->setDirLister(m_dirLister);
122 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
123
124 m_proxyModel = new DolphinSortFilterProxyModel(this);
125 m_proxyModel->setSourceModel(m_dolphinModel);
126 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
127
128 m_proxyModel->setSorting(dolphinView->sorting());
129 m_proxyModel->setSortOrder(dolphinView->sortOrder());
130 m_proxyModel->setSortFoldersFirst(dolphinView->sortFoldersFirst());
131
132 setModel(m_proxyModel);
133
134 //m_dirLister->openUrl(url, KDirLister::NoFlags);
135
136 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
137 this, SLOT(updateFont()));
138
139 /*FolderExpander* folderExpander = new FolderExpander(this, m_proxyModel);
140 folderExpander->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
141 connect (folderExpander, SIGNAL(enterDir(const QModelIndex&)),
142 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
143
144 new VersionControlObserver(this);*/
145
146 DolphinController* controller = m_container->m_controller;
147 connect(controller, SIGNAL(zoomLevelChanged(int)),
148 this, SLOT(setZoomLevel(int)));
149
150 const QString nameFilter = controller->nameFilter();
151 if (!nameFilter.isEmpty()) {
152 m_proxyModel->setFilterRegExp(nameFilter);
153 }
154 connect(controller, SIGNAL(nameFilterChanged(const QString&)),
155 this, SLOT(setNameFilter(const QString&)));
156
157 updateDecorationSize(dolphinView->showPreview());
158
159 m_extensionsFactory = new ViewExtensionsFactory(this, controller);
160 }
161
162 DolphinColumnView::~DolphinColumnView()
163 {
164 delete m_proxyModel;
165 m_proxyModel = 0;
166 delete m_dolphinModel;
167 m_dolphinModel = 0;
168 m_dirLister = 0; // deleted by m_dolphinModel
169 }
170
171 void DolphinColumnView::setActive(bool active)
172 {
173 if (active && (m_container->focusProxy() != this)) {
174 m_container->setFocusProxy(this);
175 }
176
177 if (m_active != active) {
178 m_active = active;
179
180 if (active) {
181 activate();
182 } else {
183 deactivate();
184 }
185 }
186 }
187
188 /*void DolphinColumnView::setSorting(DolphinView::Sorting sorting)
189 {
190 m_proxyModel->setSorting(sorting);
191 }
192
193 void DolphinColumnView::setSortOrder(Qt::SortOrder order)
194 {
195 m_proxyModel->setSortOrder(order);
196 }
197
198 void DolphinColumnView::setSortFoldersFirst(bool foldersFirst)
199 {
200 m_proxyModel->setSortFoldersFirst(foldersFirst);
201 }
202
203 void DolphinColumnView::setShowHiddenFiles(bool show)
204 {
205 if (show != m_dirLister->showingDotFiles()) {
206 m_dirLister->setShowingDotFiles(show);
207 m_dirLister->stop();
208 m_dirLister->openUrl(m_url, KDirLister::Reload);
209 }
210 }
211
212 void DolphinColumnView::setShowPreview(bool show)
213 {
214 m_previewGenerator->setPreviewShown(show);
215
216 m_dirLister->stop();
217 m_dirLister->openUrl(m_url, KDirLister::Reload);
218 }*/
219
220 void DolphinColumnView::updateBackground()
221 {
222 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
223 // cleaning up the cut-indication of DolphinColumnView with the code from
224 // DolphinView a common helper-class should be available which can be shared
225 // by all view implementations -> no hardcoded value anymore
226 const QPalette::ColorRole role = viewport()->backgroundRole();
227 QColor color = viewport()->palette().color(role);
228 color.setAlpha((m_active && m_container->m_active) ? 255 : 150);
229
230 QPalette palette = viewport()->palette();
231 palette.setColor(role, color);
232 viewport()->setPalette(palette);
233
234 update();
235 }
236
237 KFileItem DolphinColumnView::itemAt(const QPoint& pos) const
238 {
239 KFileItem item;
240 const QModelIndex index = indexAt(pos);
241 if (index.isValid() && (index.column() == DolphinModel::Name)) {
242 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
243 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
244 }
245 return item;
246 }
247
248 QStyleOptionViewItem DolphinColumnView::viewOptions() const
249 {
250 QStyleOptionViewItem viewOptions = QListView::viewOptions();
251 viewOptions.font = m_font;
252 viewOptions.decorationSize = m_decorationSize;
253 viewOptions.showDecorationSelected = true;
254 return viewOptions;
255 }
256
257 void DolphinColumnView::startDrag(Qt::DropActions supportedActions)
258 {
259 DragAndDropHelper::instance().startDrag(this, supportedActions, m_container->m_controller);
260 }
261
262 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
263 {
264 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
265 event->acceptProposedAction();
266 requestActivation();
267 }
268 }
269
270 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent* event)
271 {
272 QListView::dragLeaveEvent(event);
273 setDirtyRegion(m_dropRect);
274 }
275
276 void DolphinColumnView::dragMoveEvent(QDragMoveEvent* event)
277 {
278 QListView::dragMoveEvent(event);
279
280 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
281 const QModelIndex index = indexAt(event->pos());
282 setDirtyRegion(m_dropRect);
283
284 m_dropRect.setSize(QSize()); // set as invalid
285 if (index.isValid()) {
286 m_container->m_controller->setItemView(this);
287 const KFileItem item = m_container->m_controller->itemForIndex(index);
288 if (!item.isNull() && item.isDir()) {
289 m_dropRect = visualRect(index);
290 }
291 }
292 setDirtyRegion(m_dropRect);
293
294 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
295 // accept url drops, independently from the destination item
296 event->acceptProposedAction();
297 }
298 }
299
300 void DolphinColumnView::dropEvent(QDropEvent* event)
301 {
302 const QModelIndex index = indexAt(event->pos());
303 m_container->m_controller->setItemView(this);
304 const KFileItem item = m_container->m_controller->itemForIndex(index);
305 m_container->m_controller->indicateDroppedUrls(item, url(), event);
306 QListView::dropEvent(event);
307 }
308
309 void DolphinColumnView::paintEvent(QPaintEvent* event)
310 {
311 if (!m_childUrl.isEmpty()) {
312 // indicate the shown URL of the next column by highlighting the shown folder item
313 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
314 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
315 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
316 const QRect itemRect = visualRect(proxyIndex);
317 QPainter painter(viewport());
318 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
319 color.setAlpha(32);
320 painter.setPen(Qt::NoPen);
321 painter.setBrush(color);
322 painter.drawRect(itemRect);
323 }
324 }
325
326 QListView::paintEvent(event);
327 }
328
329 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
330 {
331 requestActivation();
332 if (!indexAt(event->pos()).isValid()) {
333 if (QApplication::mouseButtons() & Qt::MidButton) {
334 m_container->m_controller->replaceUrlByClipboard();
335 }
336 } else if (event->button() == Qt::LeftButton) {
337 // TODO: see comment in DolphinIconsView::mousePressEvent()
338 setState(QAbstractItemView::DraggingState);
339 }
340 QListView::mousePressEvent(event);
341 }
342
343 void DolphinColumnView::keyPressEvent(QKeyEvent* event)
344 {
345 QListView::keyPressEvent(event);
346 requestActivation();
347
348 DolphinController* controller = m_container->m_controller;
349 controller->handleKeyPressEvent(event);
350 switch (event->key()) {
351 case Qt::Key_Right: {
352 // Special key handling for the column: A Key_Right should
353 // open a new column for the currently selected folder.
354 const QModelIndex index = currentIndex();
355 const KFileItem item = controller->itemForIndex(index);
356 if (!item.isNull() && item.isDir()) {
357 controller->emitItemTriggered(item);
358 }
359 break;
360 }
361
362 case Qt::Key_Escape:
363 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
364 QItemSelectionModel::Current |
365 QItemSelectionModel::Clear);
366 break;
367
368 default:
369 break;
370 }
371 }
372
373 void DolphinColumnView::contextMenuEvent(QContextMenuEvent* event)
374 {
375 if (!m_active) {
376 m_container->requestActivation(this);
377 Q_ASSERT(m_container->m_controller->itemView() == this);
378 m_container->m_controller->triggerUrlChangeRequest(m_url);
379 }
380 Q_ASSERT(m_active);
381
382 QListView::contextMenuEvent(event);
383
384 const QModelIndex index = indexAt(event->pos());
385 if (!index.isValid()) {
386 clearSelection();
387 }
388
389 const QPoint pos = m_container->viewport()->mapFromGlobal(event->globalPos());
390 Q_ASSERT(m_container->m_controller->itemView() == this);
391 m_container->m_controller->triggerContextMenuRequest(pos);
392 }
393
394 void DolphinColumnView::wheelEvent(QWheelEvent* event)
395 {
396 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
397 if (event->modifiers() & Qt::ControlModifier) {
398 event->ignore();
399 return;
400 }
401
402 const int height = m_decorationSize.height();
403 const int step = (height >= KIconLoader::SizeHuge) ? height / 10 : (KIconLoader::SizeHuge - height) / 2;
404 verticalScrollBar()->setSingleStep(step);
405
406 QListView::wheelEvent(event);
407 }
408
409 void DolphinColumnView::leaveEvent(QEvent* event)
410 {
411 QListView::leaveEvent(event);
412 // if the mouse is above an item and moved very fast outside the widget,
413 // no viewportEntered() signal might be emitted although the mouse has been moved
414 // above the viewport
415 m_container->m_controller->emitViewportEntered();
416 }
417
418 void DolphinColumnView::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
419 {
420 QListView::selectionChanged(selected, deselected);
421
422 //QItemSelectionModel* selModel = m_container->selectionModel();
423 //selModel->select(selected, QItemSelectionModel::Select);
424 //selModel->select(deselected, QItemSelectionModel::Deselect);
425 }
426
427 void DolphinColumnView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
428 {
429 QListView::currentChanged(current, previous);
430 m_extensionsFactory->handleCurrentIndexChange(current, previous);
431 }
432
433 void DolphinColumnView::setNameFilter(const QString& nameFilter)
434 {
435 m_proxyModel->setFilterRegExp(nameFilter);
436 }
437
438 void DolphinColumnView::setZoomLevel(int level)
439 {
440 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
441 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
442
443 const bool showPreview = m_container->m_controller->dolphinView()->showPreview();
444 if (showPreview) {
445 settings->setPreviewSize(size);
446 } else {
447 settings->setIconSize(size);
448 }
449
450 updateDecorationSize(showPreview);
451 }
452
453 void DolphinColumnView::slotEntered(const QModelIndex& index)
454 {
455 m_container->m_controller->setItemView(this);
456 m_container->m_controller->emitItemEntered(index);
457 }
458
459 void DolphinColumnView::requestActivation()
460 {
461 m_container->m_controller->setItemView(this);
462 m_container->m_controller->requestActivation();
463 if (!m_active) {
464 m_container->requestActivation(this);
465 m_container->m_controller->triggerUrlChangeRequest(m_url);
466 selectionModel()->clear();
467 }
468 }
469
470 void DolphinColumnView::updateFont()
471 {
472 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
473 Q_ASSERT(settings != 0);
474
475 if (settings->useSystemFont()) {
476 m_font = KGlobalSettings::generalFont();
477 }
478 }
479
480 void DolphinColumnView::slotShowPreviewChanged()
481 {
482 const DolphinView* view = m_container->m_controller->dolphinView();
483 updateDecorationSize(view->showPreview());
484 }
485
486 void DolphinColumnView::activate()
487 {
488 setFocus(Qt::OtherFocusReason);
489
490 if (KGlobalSettings::singleClick()) {
491 connect(this, SIGNAL(clicked(const QModelIndex&)),
492 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
493 } else {
494 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
495 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
496 }
497
498 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
499 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
500 }
501
502 updateBackground();
503 }
504
505 void DolphinColumnView::deactivate()
506 {
507 clearFocus();
508 if (KGlobalSettings::singleClick()) {
509 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
510 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
511 } else {
512 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
513 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
514 }
515
516 const QModelIndex current = selectionModel()->currentIndex();
517 selectionModel()->clear();
518 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
519 updateBackground();
520 }
521
522 void DolphinColumnView::updateDecorationSize(bool showPreview)
523 {
524 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
525 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
526 const QSize size(iconSize, iconSize);
527 setIconSize(size);
528
529 m_decorationSize = size;
530
531 doItemsLayout();
532 }
533
534 #include "dolphincolumnview.moc"