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