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