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