]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
b0d40f5b6ab833781cac68df9626cc8e8949fae8
[dolphin.git] / src / dolphincolumnwidget.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 "dolphincolumnwidget.h"
21
22 #include "dolphinmodel.h"
23 #include "dolphincolumnview.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 "selectionmanager.h"
34 #include "tooltips/tooltipmanager.h"
35
36 #include <kcolorscheme.h>
37 #include <kdirlister.h>
38 #include <kfileitem.h>
39 #include <kfilepreviewgenerator.h>
40 #include <kio/previewjob.h>
41 #include <kiconeffect.h>
42 #include <kjob.h>
43 #include <konqmimedata.h>
44
45 #include <QApplication>
46 #include <QClipboard>
47 #include <QPainter>
48 #include <QPoint>
49 #include <QScrollBar>
50
51 DolphinColumnWidget::DolphinColumnWidget(QWidget* parent,
52 DolphinColumnView* columnView,
53 const KUrl& url) :
54 QListView(parent),
55 m_active(true),
56 m_view(columnView),
57 m_selectionManager(0),
58 m_autoScroller(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_previewGenerator(0),
67 m_toolTipManager(0),
68 m_dropRect()
69 {
70 setMouseTracking(true);
71 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
72 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
73 setSelectionBehavior(SelectItems);
74 setSelectionMode(QAbstractItemView::ExtendedSelection);
75 setDragDropMode(QAbstractItemView::DragDrop);
76 setDropIndicatorShown(false);
77 setSelectionRectVisible(true);
78 setEditTriggers(QAbstractItemView::NoEditTriggers);
79
80 setVerticalScrollMode(QListView::ScrollPerPixel);
81 setHorizontalScrollMode(QListView::ScrollPerPixel);
82
83 m_autoScroller = new DolphinViewAutoScroller(this);
84
85 // apply the column mode settings to the widget
86 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
87 Q_ASSERT(settings != 0);
88
89 if (settings->useSystemFont()) {
90 m_font = KGlobalSettings::generalFont();
91 } else {
92 m_font = QFont(settings->fontFamily(),
93 settings->fontSize(),
94 settings->fontWeight(),
95 settings->italicFont());
96 }
97
98 const int iconSize = settings->iconSize();
99 setDecorationSize(QSize(iconSize, iconSize));
100
101 KFileItemDelegate* delegate = new KFileItemDelegate(this);
102 delegate->setShowToolTipWhenElided(false);
103 setItemDelegate(delegate);
104
105 activate();
106
107 connect(this, SIGNAL(viewportEntered()),
108 m_view->m_controller, SLOT(emitViewportEntered()));
109 connect(this, SIGNAL(entered(const QModelIndex&)),
110 this, SLOT(slotEntered(const QModelIndex&)));
111
112 m_dirLister = new DolphinDirLister();
113 m_dirLister->setAutoUpdate(true);
114 m_dirLister->setMainWindow(window());
115 m_dirLister->setDelayedMimeTypes(true);
116 const bool showHiddenFiles = m_view->m_controller->dolphinView()->showHiddenFiles();
117 m_dirLister->setShowingDotFiles(showHiddenFiles);
118
119 m_dolphinModel = new DolphinModel(this);
120 m_dolphinModel->setDirLister(m_dirLister);
121 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
122
123 m_proxyModel = new DolphinSortFilterProxyModel(this);
124 m_proxyModel->setSourceModel(m_dolphinModel);
125 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
126 const DolphinView* dolphinView = m_view->m_controller->dolphinView();
127 m_proxyModel->setSorting(dolphinView->sorting());
128 m_proxyModel->setSortOrder(dolphinView->sortOrder());
129
130 setModel(m_proxyModel);
131
132 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
133 m_selectionManager = new SelectionManager(this);
134 connect(m_selectionManager, SIGNAL(selectionChanged()),
135 this, SLOT(requestActivation()));
136 connect(m_view->m_controller, SIGNAL(urlChanged(const KUrl&)),
137 m_selectionManager, SLOT(reset()));
138 }
139
140 m_previewGenerator = new KFilePreviewGenerator(this);
141 m_previewGenerator->setPreviewShown(m_view->m_controller->dolphinView()->showPreview());
142
143 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
144 m_toolTipManager = new ToolTipManager(this, m_proxyModel);
145 }
146
147 m_dirLister->openUrl(url, KDirLister::NoFlags);
148
149 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
150 this, SLOT(updateFont()));
151
152 FolderExpander* folderExpander = new FolderExpander(this, m_proxyModel);
153 folderExpander->setEnabled(DolphinSettings::instance().generalSettings()->autoExpandFolders());
154 connect (folderExpander, SIGNAL(enterDir(const QModelIndex&)),
155 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
156 }
157
158 DolphinColumnWidget::~DolphinColumnWidget()
159 {
160 delete m_proxyModel;
161 m_proxyModel = 0;
162 delete m_dolphinModel;
163 m_dolphinModel = 0;
164 m_dirLister = 0; // deleted by m_dolphinModel
165 }
166
167 void DolphinColumnWidget::setDecorationSize(const QSize& size)
168 {
169 setIconSize(size);
170 m_decorationSize = size;
171 doItemsLayout();
172 if (m_previewGenerator != 0) {
173 m_previewGenerator->updateIcons();
174 }
175 if (m_selectionManager != 0) {
176 m_selectionManager->reset();
177 }
178 }
179
180 void DolphinColumnWidget::setActive(bool active)
181 {
182 if (active && (m_view->focusProxy() != this)) {
183 m_view->setFocusProxy(this);
184 }
185
186 if (m_active != active) {
187 m_active = active;
188
189 if (active) {
190 activate();
191 } else {
192 deactivate();
193 }
194 }
195 }
196
197 void DolphinColumnWidget::reload()
198 {
199 m_dirLister->stop();
200 m_dirLister->openUrl(m_url, KDirLister::Reload);
201 }
202
203 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting)
204 {
205 m_proxyModel->setSorting(sorting);
206 }
207
208 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order)
209 {
210 m_proxyModel->setSortOrder(order);
211 }
212
213 void DolphinColumnWidget::setShowHiddenFiles(bool show)
214 {
215 if (show != m_dirLister->showingDotFiles()) {
216 m_dirLister->setShowingDotFiles(show);
217 m_dirLister->stop();
218 m_dirLister->openUrl(m_url, KDirLister::Reload);
219 }
220 }
221
222 void DolphinColumnWidget::setShowPreview(bool show)
223 {
224 m_previewGenerator->setPreviewShown(show);
225
226 m_dirLister->stop();
227 m_dirLister->openUrl(m_url, KDirLister::Reload);
228 }
229
230 void DolphinColumnWidget::updateBackground()
231 {
232 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
233 // cleaning up the cut-indication of DolphinColumnWidget with the code from
234 // DolphinView a common helper-class should be available which can be shared
235 // by all view implementations -> no hardcoded value anymore
236 const QPalette::ColorRole role = viewport()->backgroundRole();
237 QColor color = viewport()->palette().color(role);
238 color.setAlpha((m_active && m_view->m_active) ? 255 : 150);
239
240 QPalette palette = viewport()->palette();
241 palette.setColor(role, color);
242 viewport()->setPalette(palette);
243
244 update();
245 }
246
247 void DolphinColumnWidget::setNameFilter(const QString& nameFilter)
248 {
249 m_proxyModel->setFilterRegExp(nameFilter);
250 }
251
252 void DolphinColumnWidget::editItem(const KFileItem& item)
253 {
254 const QModelIndex dirIndex = m_dolphinModel->indexForItem(item);
255 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
256 if (proxyIndex.isValid()) {
257 edit(proxyIndex);
258 }
259 }
260
261 KFileItem DolphinColumnWidget::itemAt(const QPoint& pos) const
262 {
263 KFileItem item;
264 const QModelIndex index = indexAt(pos);
265 if (index.isValid() && (index.column() == DolphinModel::Name)) {
266 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
267 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
268 }
269 return item;
270 }
271
272 KFileItemList DolphinColumnWidget::selectedItems() const
273 {
274 const QItemSelection selection = m_proxyModel->mapSelectionToSource(selectionModel()->selection());
275 KFileItemList itemList;
276
277 const QModelIndexList indexList = selection.indexes();
278 foreach (const QModelIndex &index, indexList) {
279 KFileItem item = m_dolphinModel->itemForIndex(index);
280 if (!item.isNull()) {
281 itemList.append(item);
282 }
283 }
284
285 return itemList;
286 }
287
288 QMimeData* DolphinColumnWidget::selectionMimeData() const
289 {
290 const QItemSelection selection = m_proxyModel->mapSelectionToSource(selectionModel()->selection());
291 return m_dolphinModel->mimeData(selection.indexes());
292 }
293
294 QStyleOptionViewItem DolphinColumnWidget::viewOptions() const
295 {
296 QStyleOptionViewItem viewOptions = QListView::viewOptions();
297 viewOptions.font = m_font;
298 viewOptions.decorationSize = m_decorationSize;
299 viewOptions.showDecorationSelected = true;
300 return viewOptions;
301 }
302
303 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions)
304 {
305 DragAndDropHelper::instance().startDrag(this, supportedActions, m_view->m_controller);
306 }
307
308 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent* event)
309 {
310 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
311 event->acceptProposedAction();
312 requestActivation();
313 }
314 }
315
316 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
317 {
318 QListView::dragLeaveEvent(event);
319 setDirtyRegion(m_dropRect);
320 }
321
322 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent* event)
323 {
324 QListView::dragMoveEvent(event);
325
326 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
327 const QModelIndex index = indexAt(event->pos());
328 setDirtyRegion(m_dropRect);
329
330 m_dropRect.setSize(QSize()); // set as invalid
331 if (index.isValid()) {
332 m_view->m_controller->setItemView(this);
333 const KFileItem item = m_view->m_controller->itemForIndex(index);
334 if (!item.isNull() && item.isDir()) {
335 m_dropRect = visualRect(index);
336 }
337 }
338 setDirtyRegion(m_dropRect);
339
340 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
341 // accept url drops, independently from the destination item
342 event->acceptProposedAction();
343 }
344 }
345
346 void DolphinColumnWidget::dropEvent(QDropEvent* event)
347 {
348 const QModelIndex index = indexAt(event->pos());
349 m_view->m_controller->setItemView(this);
350 const KFileItem item = m_view->m_controller->itemForIndex(index);
351 m_view->m_controller->indicateDroppedUrls(item, url(), event);
352 QListView::dropEvent(event);
353 }
354
355 void DolphinColumnWidget::paintEvent(QPaintEvent* event)
356 {
357 if (!m_childUrl.isEmpty()) {
358 // indicate the shown URL of the next column by highlighting the shown folder item
359 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
360 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
361 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
362 const QRect itemRect = visualRect(proxyIndex);
363 QPainter painter(viewport());
364 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
365 color.setAlpha(32);
366 painter.setPen(Qt::NoPen);
367 painter.setBrush(color);
368 painter.drawRect(itemRect);
369 }
370 }
371
372 QListView::paintEvent(event);
373 }
374
375 void DolphinColumnWidget::mousePressEvent(QMouseEvent* event)
376 {
377 requestActivation();
378 if (!indexAt(event->pos()).isValid()) {
379 if (QApplication::mouseButtons() & Qt::MidButton) {
380 m_view->m_controller->replaceUrlByClipboard();
381 }
382 } else if (event->button() == Qt::LeftButton) {
383 // TODO: see comment in DolphinIconsView::mousePressEvent()
384 setState(QAbstractItemView::DraggingState);
385 }
386 QListView::mousePressEvent(event);
387 }
388
389 void DolphinColumnWidget::keyPressEvent(QKeyEvent* event)
390 {
391 QListView::keyPressEvent(event);
392 requestActivation();
393
394 DolphinController* controller = m_view->m_controller;
395 controller->handleKeyPressEvent(event);
396 switch (event->key()) {
397 case Qt::Key_Right: {
398 // Special key handling for the column: A Key_Right should
399 // open a new column for the currently selected folder.
400 const QModelIndex index = currentIndex();
401 const KFileItem item = controller->itemForIndex(index);
402 if (!item.isNull() && item.isDir()) {
403 controller->emitItemTriggered(item);
404 }
405 break;
406 }
407
408 case Qt::Key_Escape:
409 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
410 QItemSelectionModel::Current |
411 QItemSelectionModel::Clear);
412 break;
413
414 default:
415 break;
416 }
417
418 if (m_toolTipManager != 0) {
419 m_toolTipManager->hideTip();
420 }
421 }
422
423 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent* event)
424 {
425 if (!m_active) {
426 m_view->requestActivation(this);
427 Q_ASSERT(m_view->m_controller->itemView() == this);
428 m_view->m_controller->triggerUrlChangeRequest(m_url);
429 }
430 Q_ASSERT(m_active);
431
432 QListView::contextMenuEvent(event);
433
434 const QModelIndex index = indexAt(event->pos());
435 if (!index.isValid()) {
436 clearSelection();
437 }
438
439 if (m_toolTipManager != 0) {
440 m_toolTipManager->hideTip();
441 }
442
443 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
444 Q_ASSERT(m_view->m_controller->itemView() == this);
445 m_view->m_controller->triggerContextMenuRequest(pos);
446 }
447
448 void DolphinColumnWidget::wheelEvent(QWheelEvent* event)
449 {
450 if (m_selectionManager != 0) {
451 m_selectionManager->reset();
452 }
453
454 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
455 if (event->modifiers() & Qt::ControlModifier) {
456 event->ignore();
457 return;
458 }
459
460 const int height = m_decorationSize.height();
461 const int step = (height >= KIconLoader::SizeHuge) ? height / 10 : (KIconLoader::SizeHuge - height) / 2;
462 verticalScrollBar()->setSingleStep(step);
463
464 QListView::wheelEvent(event);
465 }
466
467 void DolphinColumnWidget::leaveEvent(QEvent* event)
468 {
469 QListView::leaveEvent(event);
470 // if the mouse is above an item and moved very fast outside the widget,
471 // no viewportEntered() signal might be emitted although the mouse has been moved
472 // above the viewport
473 m_view->m_controller->emitViewportEntered();
474 }
475
476 void DolphinColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
477 {
478 QListView::selectionChanged(selected, deselected);
479
480 QItemSelectionModel* selModel = m_view->selectionModel();
481 selModel->select(selected, QItemSelectionModel::Select);
482 selModel->select(deselected, QItemSelectionModel::Deselect);
483 }
484
485 void DolphinColumnWidget::currentChanged(const QModelIndex& current, const QModelIndex& previous)
486 {
487 QListView::currentChanged(current, previous);
488 if (current.isValid() && !m_autoScroller->isActive()) {
489 scrollTo(current);
490 }
491 }
492
493 void DolphinColumnWidget::slotEntered(const QModelIndex& index)
494 {
495 m_view->m_controller->setItemView(this);
496 m_view->m_controller->emitItemEntered(index);
497 }
498
499 void DolphinColumnWidget::slotClicked(const QModelIndex& index)
500 {
501 DolphinController* controller = m_view->m_controller;
502 if (KGlobalSettings::singleClick()) {
503 controller->triggerItem(index);
504 } else {
505 // even when using double click, a directory should be opened
506 // after the first click
507 const KFileItem item = controller->itemForIndex(index);
508 if (!item.isNull() && item.isDir()) {
509 controller->triggerItem(index);
510 }
511 }
512 }
513
514 void DolphinColumnWidget::slotDoubleClicked(const QModelIndex& index)
515 {
516 if (!KGlobalSettings::singleClick()) {
517 m_view->m_controller->triggerItem(index);
518 }
519 }
520
521 void DolphinColumnWidget::requestActivation()
522 {
523 m_view->m_controller->setItemView(this);
524 m_view->m_controller->requestActivation();
525 if (!m_active) {
526 m_view->requestActivation(this);
527 m_view->m_controller->triggerUrlChangeRequest(m_url);
528 selectionModel()->clear();
529 }
530 }
531
532 void DolphinColumnWidget::updateFont()
533 {
534 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
535 Q_ASSERT(settings != 0);
536
537 if (settings->useSystemFont()) {
538 m_font = KGlobalSettings::generalFont();
539 }
540 }
541
542 void DolphinColumnWidget::activate()
543 {
544 setFocus(Qt::OtherFocusReason);
545
546 connect(this, SIGNAL(clicked(const QModelIndex&)),
547 m_view->m_controller, SLOT(requestTab(const QModelIndex&)));
548 connect(this, SIGNAL(clicked(const QModelIndex&)),
549 this, SLOT(slotClicked(const QModelIndex&)));
550 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
551 this, SLOT(slotDoubleClicked(const QModelIndex&)));
552
553 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
554 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
555 }
556
557 updateBackground();
558 }
559
560 void DolphinColumnWidget::deactivate()
561 {
562 clearFocus();
563 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
564 m_view->m_controller, SLOT(requestTab(const QModelIndex&)));
565 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
566 this, SLOT(slotClicked(const QModelIndex&)));
567 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
568 this, SLOT(slotDoubleClicked(const QModelIndex&)));
569
570 const QModelIndex current = selectionModel()->currentIndex();
571 selectionModel()->clear();
572 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
573 updateBackground();
574 }
575
576 #include "dolphincolumnwidget.moc"