]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
* open folders always on single click
[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->updatePreviews();
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 if (event->key() == Qt::Key_Right) {
397 // Special key handling for the column: A Key_Right should
398 // open a new column for the currently selected folder.
399 const QModelIndex index = currentIndex();
400 const KFileItem item = controller->itemForIndex(index);
401 if (!item.isNull() && item.isDir()) {
402 controller->emitItemTriggered(item);
403 }
404 }
405
406 if (m_toolTipManager != 0) {
407 m_toolTipManager->hideTip();
408 }
409 }
410
411 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent* event)
412 {
413 if (!m_active) {
414 m_view->requestActivation(this);
415 Q_ASSERT(m_view->m_controller->itemView() == this);
416 m_view->m_controller->triggerUrlChangeRequest(m_url);
417 }
418 Q_ASSERT(m_active);
419
420 QListView::contextMenuEvent(event);
421
422 const QModelIndex index = indexAt(event->pos());
423 if (!index.isValid()) {
424 clearSelection();
425 }
426
427 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
428 Q_ASSERT(m_view->m_controller->itemView() == this);
429 m_view->m_controller->triggerContextMenuRequest(pos);
430 }
431
432 void DolphinColumnWidget::wheelEvent(QWheelEvent* event)
433 {
434 if (m_selectionManager != 0) {
435 m_selectionManager->reset();
436 }
437
438 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
439 if (event->modifiers() & Qt::ControlModifier) {
440 event->ignore();
441 return;
442 }
443
444 const int height = m_decorationSize.height();
445 const int step = (height >= KIconLoader::SizeHuge) ? height / 10 : (KIconLoader::SizeHuge - height) / 2;
446 verticalScrollBar()->setSingleStep(step);
447
448 QListView::wheelEvent(event);
449 }
450
451 void DolphinColumnWidget::leaveEvent(QEvent* event)
452 {
453 QListView::leaveEvent(event);
454 // if the mouse is above an item and moved very fast outside the widget,
455 // no viewportEntered() signal might be emitted although the mouse has been moved
456 // above the viewport
457 m_view->m_controller->emitViewportEntered();
458 }
459
460 void DolphinColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
461 {
462 QListView::selectionChanged(selected, deselected);
463
464 QItemSelectionModel* selModel = m_view->selectionModel();
465 selModel->select(selected, QItemSelectionModel::Select);
466 selModel->select(deselected, QItemSelectionModel::Deselect);
467 }
468
469 void DolphinColumnWidget::currentChanged(const QModelIndex& current, const QModelIndex& previous)
470 {
471 QListView::currentChanged(current, previous);
472 if (current.isValid() && !m_autoScroller->isActive()) {
473 scrollTo(current);
474 }
475 }
476
477 void DolphinColumnWidget::slotEntered(const QModelIndex& index)
478 {
479 m_view->m_controller->setItemView(this);
480 m_view->m_controller->emitItemEntered(index);
481 }
482
483 void DolphinColumnWidget::slotClicked(const QModelIndex& index)
484 {
485 DolphinController* controller = m_view->m_controller;
486 if (KGlobalSettings::singleClick()) {
487 controller->triggerItem(index);
488 } else {
489 // even when using double click, a directory should be opened
490 // after the first click
491 const KFileItem item = controller->itemForIndex(index);
492 if (!item.isNull() && item.isDir()) {
493 controller->triggerItem(index);
494 }
495 }
496 }
497
498 void DolphinColumnWidget::slotDoubleClicked(const QModelIndex& index)
499 {
500 if (!KGlobalSettings::singleClick()) {
501 m_view->m_controller->triggerItem(index);
502 }
503 }
504
505 void DolphinColumnWidget::requestActivation()
506 {
507 m_view->m_controller->setItemView(this);
508 m_view->m_controller->requestActivation();
509 if (!m_active) {
510 m_view->requestActivation(this);
511 m_view->m_controller->triggerUrlChangeRequest(m_url);
512 selectionModel()->clear();
513 }
514 }
515
516 void DolphinColumnWidget::updateFont()
517 {
518 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
519 Q_ASSERT(settings != 0);
520
521 if (settings->useSystemFont()) {
522 m_font = KGlobalSettings::generalFont();
523 }
524 }
525
526 void DolphinColumnWidget::activate()
527 {
528 setFocus(Qt::OtherFocusReason);
529
530 connect(this, SIGNAL(clicked(const QModelIndex&)),
531 m_view->m_controller, SLOT(requestTab(const QModelIndex&)));
532 connect(this, SIGNAL(clicked(const QModelIndex&)),
533 this, SLOT(slotClicked(const QModelIndex&)));
534 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
535 this, SLOT(slotDoubleClicked(const QModelIndex&)));
536
537 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
538 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
539 }
540
541 updateBackground();
542 }
543
544 void DolphinColumnWidget::deactivate()
545 {
546 clearFocus();
547
548 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
549 this, SLOT(slotClicked(const QModelIndex&)));
550 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
551 this, SLOT(slotDoubleClicked(const QModelIndex&)));
552
553 const QModelIndex current = selectionModel()->currentIndex();
554 selectionModel()->clear();
555 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
556 updateBackground();
557 }
558
559 #include "dolphincolumnwidget.moc"