]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnview.cpp
* Assure that the URL navigator is synchronized with the active column.
[dolphin.git] / src / dolphincolumnview.cpp
1 /***************************************************************************
2 * Copyright (C) 2007-2009 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 "dolphincolumnview.h"
21
22 #include "dolphinmodel.h"
23 #include "dolphincolumnviewcontainer.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 "tooltips/tooltipmanager.h"
34 #include "viewextensionsfactory.h"
35 #include "zoomlevelinfo.h"
36
37 #include <kcolorscheme.h>
38 #include <kdirlister.h>
39 #include <kfileitem.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 DolphinColumnView::DolphinColumnView(QWidget* parent,
52 DolphinColumnViewContainer* container,
53 const KUrl& url) :
54 QListView(parent),
55 m_active(false),
56 m_container(container),
57 m_extensionsFactory(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_dropRect()
66 {
67 setMouseTracking(true);
68 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
69 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
70 setSelectionBehavior(SelectItems);
71 setSelectionMode(QAbstractItemView::ExtendedSelection);
72 setDragDropMode(QAbstractItemView::DragDrop);
73 setDropIndicatorShown(false);
74 setSelectionRectVisible(true);
75 setEditTriggers(QAbstractItemView::NoEditTriggers);
76
77 setVerticalScrollMode(QListView::ScrollPerPixel);
78 setHorizontalScrollMode(QListView::ScrollPerPixel);
79
80 // apply the column mode settings to the widget
81 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
82 Q_ASSERT(settings != 0);
83
84 if (settings->useSystemFont()) {
85 m_font = KGlobalSettings::generalFont();
86 } else {
87 m_font = QFont(settings->fontFamily(),
88 settings->fontSize(),
89 settings->fontWeight(),
90 settings->italicFont());
91 }
92
93 connect(this, SIGNAL(viewportEntered()),
94 m_container->m_controller, SLOT(emitViewportEntered()));
95 connect(this, SIGNAL(entered(const QModelIndex&)),
96 this, SLOT(slotEntered(const QModelIndex&)));
97
98 const DolphinView* dolphinView = m_container->m_controller->dolphinView();
99 connect(dolphinView, SIGNAL(showPreviewChanged()),
100 this, SLOT(slotShowPreviewChanged()));
101
102 m_dirLister = new DolphinDirLister();
103 m_dirLister->setAutoUpdate(true);
104 m_dirLister->setMainWindow(window());
105 m_dirLister->setDelayedMimeTypes(true);
106 const bool showHiddenFiles = m_container->m_controller->dolphinView()->showHiddenFiles();
107 m_dirLister->setShowingDotFiles(showHiddenFiles);
108
109 m_dolphinModel = new DolphinModel(this);
110 m_dolphinModel->setDirLister(m_dirLister);
111 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
112
113 m_proxyModel = new DolphinSortFilterProxyModel(this);
114 m_proxyModel->setSourceModel(m_dolphinModel);
115 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
116
117 m_proxyModel->setSorting(dolphinView->sorting());
118 m_proxyModel->setSortOrder(dolphinView->sortOrder());
119 m_proxyModel->setSortFoldersFirst(dolphinView->sortFoldersFirst());
120
121 setModel(m_proxyModel);
122
123 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
124 this, SLOT(updateFont()));
125
126 DolphinController* controller = m_container->m_controller;
127 connect(controller, SIGNAL(zoomLevelChanged(int)),
128 this, SLOT(setZoomLevel(int)));
129
130 const QString nameFilter = controller->nameFilter();
131 if (!nameFilter.isEmpty()) {
132 m_proxyModel->setFilterFixedString(nameFilter);
133 }
134
135 updateDecorationSize(dolphinView->showPreview());
136 m_extensionsFactory = new ViewExtensionsFactory(this, controller);
137 }
138
139 DolphinColumnView::~DolphinColumnView()
140 {
141 delete m_proxyModel;
142 m_proxyModel = 0;
143 delete m_dolphinModel;
144 m_dolphinModel = 0;
145 m_dirLister = 0; // deleted by m_dolphinModel
146 }
147
148 void DolphinColumnView::setActive(bool active)
149 {
150 if (active && (m_container->focusProxy() != this)) {
151 m_container->setFocusProxy(this);
152 }
153
154 if (m_active != active) {
155 m_active = active;
156
157 if (active) {
158 activate();
159 } else {
160 deactivate();
161 }
162 }
163 }
164
165 void DolphinColumnView::updateBackground()
166 {
167 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
168 // cleaning up the cut-indication of DolphinColumnView with the code from
169 // DolphinView a common helper-class should be available which can be shared
170 // by all view implementations -> no hardcoded value anymore
171 const QPalette::ColorRole role = viewport()->backgroundRole();
172 QColor color = viewport()->palette().color(role);
173 color.setAlpha((m_active && m_container->m_active) ? 255 : 150);
174
175 QPalette palette = viewport()->palette();
176 palette.setColor(role, color);
177 viewport()->setPalette(palette);
178
179 update();
180 }
181
182 KFileItem DolphinColumnView::itemAt(const QPoint& pos) const
183 {
184 KFileItem item;
185 const QModelIndex index = indexAt(pos);
186 if (index.isValid() && (index.column() == DolphinModel::Name)) {
187 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
188 item = m_dolphinModel->itemForIndex(dolphinModelIndex);
189 }
190 return item;
191 }
192
193 void DolphinColumnView::setSelectionModel(QItemSelectionModel* model)
194 {
195 // If a change of the selection is done although the view is not active
196 // (e. g. by the selection markers), the column must be activated. This
197 // is done by listening to the current selectionChanged() signal.
198 if (selectionModel() != 0) {
199 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
200 this, SLOT(requestActivation()));
201 }
202
203 QListView::setSelectionModel(model);
204
205 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
206 this, SLOT(requestActivation()));
207 }
208
209 QStyleOptionViewItem DolphinColumnView::viewOptions() const
210 {
211 QStyleOptionViewItem viewOptions = QListView::viewOptions();
212 viewOptions.font = m_font;
213 viewOptions.fontMetrics = QFontMetrics(m_font);
214 viewOptions.decorationSize = m_decorationSize;
215 viewOptions.showDecorationSelected = true;
216 return viewOptions;
217 }
218
219 void DolphinColumnView::startDrag(Qt::DropActions supportedActions)
220 {
221 DragAndDropHelper::instance().startDrag(this, supportedActions, m_container->m_controller);
222 }
223
224 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
225 {
226 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
227 event->acceptProposedAction();
228 requestActivation();
229 }
230 }
231
232 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent* event)
233 {
234 QListView::dragLeaveEvent(event);
235 setDirtyRegion(m_dropRect);
236 }
237
238 void DolphinColumnView::dragMoveEvent(QDragMoveEvent* event)
239 {
240 QListView::dragMoveEvent(event);
241
242 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
243 const QModelIndex index = indexAt(event->pos());
244 setDirtyRegion(m_dropRect);
245
246 m_dropRect.setSize(QSize()); // set as invalid
247 if (index.isValid()) {
248 m_container->m_controller->setItemView(this);
249 const KFileItem item = m_container->m_controller->itemForIndex(index);
250 if (!item.isNull() && item.isDir()) {
251 m_dropRect = visualRect(index);
252 }
253 }
254 setDirtyRegion(m_dropRect);
255
256 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
257 // accept url drops, independently from the destination item
258 event->acceptProposedAction();
259 }
260 }
261
262 void DolphinColumnView::dropEvent(QDropEvent* event)
263 {
264 const QModelIndex index = indexAt(event->pos());
265 m_container->m_controller->setItemView(this);
266 const KFileItem item = m_container->m_controller->itemForIndex(index);
267 m_container->m_controller->indicateDroppedUrls(item, url(), event);
268 QListView::dropEvent(event);
269 }
270
271 void DolphinColumnView::paintEvent(QPaintEvent* event)
272 {
273 if (!m_childUrl.isEmpty()) {
274 // indicate the shown URL of the next column by highlighting the shown folder item
275 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
276 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
277 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
278 const QRect itemRect = visualRect(proxyIndex);
279 QPainter painter(viewport());
280 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
281 color.setAlpha(32);
282 painter.setPen(Qt::NoPen);
283 painter.setBrush(color);
284 painter.drawRect(itemRect);
285 }
286 }
287
288 QListView::paintEvent(event);
289 }
290
291 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
292 {
293 requestActivation();
294 if (!indexAt(event->pos()).isValid()) {
295 if (QApplication::mouseButtons() & Qt::MidButton) {
296 m_container->m_controller->replaceUrlByClipboard();
297 }
298 } else if (event->button() == Qt::LeftButton) {
299 // TODO: see comment in DolphinIconsView::mousePressEvent()
300 setState(QAbstractItemView::DraggingState);
301 }
302 QListView::mousePressEvent(event);
303 }
304
305 void DolphinColumnView::keyPressEvent(QKeyEvent* event)
306 {
307 QListView::keyPressEvent(event);
308 requestActivation();
309
310 DolphinController* controller = m_container->m_controller;
311 controller->handleKeyPressEvent(event);
312 switch (event->key()) {
313 case Qt::Key_Right: {
314 // Special key handling for the column: A Key_Right should
315 // open a new column for the currently selected folder.
316 const QModelIndex index = currentIndex();
317 const KFileItem item = controller->itemForIndex(index);
318 if (!item.isNull() && item.isDir()) {
319 controller->emitItemTriggered(item);
320 }
321 break;
322 }
323
324 case Qt::Key_Escape:
325 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
326 QItemSelectionModel::Current |
327 QItemSelectionModel::Clear);
328 break;
329
330 default:
331 break;
332 }
333 }
334
335 void DolphinColumnView::contextMenuEvent(QContextMenuEvent* event)
336 {
337 requestActivation();
338 QListView::contextMenuEvent(event);
339 m_container->m_controller->triggerContextMenuRequest(event->pos());
340 }
341
342 void DolphinColumnView::wheelEvent(QWheelEvent* event)
343 {
344 const int step = m_decorationSize.height();
345 verticalScrollBar()->setSingleStep(step);
346 QListView::wheelEvent(event);
347 }
348
349 void DolphinColumnView::leaveEvent(QEvent* event)
350 {
351 QListView::leaveEvent(event);
352 // if the mouse is above an item and moved very fast outside the widget,
353 // no viewportEntered() signal might be emitted although the mouse has been moved
354 // above the viewport
355 m_container->m_controller->emitViewportEntered();
356 }
357
358 void DolphinColumnView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
359 {
360 QListView::currentChanged(current, previous);
361 m_extensionsFactory->handleCurrentIndexChange(current, previous);
362 }
363
364 void DolphinColumnView::setZoomLevel(int level)
365 {
366 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
367 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
368
369 const bool showPreview = m_container->m_controller->dolphinView()->showPreview();
370 if (showPreview) {
371 settings->setPreviewSize(size);
372 } else {
373 settings->setIconSize(size);
374 }
375
376 updateDecorationSize(showPreview);
377 }
378
379 void DolphinColumnView::slotEntered(const QModelIndex& index)
380 {
381 m_container->m_controller->setItemView(this);
382 m_container->m_controller->emitItemEntered(index);
383 }
384
385 void DolphinColumnView::requestActivation()
386 {
387 if (!m_active) {
388 m_container->requestActivation(this);
389 selectionModel()->clear();
390 }
391 }
392
393 void DolphinColumnView::updateFont()
394 {
395 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
396 Q_ASSERT(settings != 0);
397
398 if (settings->useSystemFont()) {
399 m_font = KGlobalSettings::generalFont();
400 }
401 }
402
403 void DolphinColumnView::slotShowPreviewChanged()
404 {
405 const DolphinView* view = m_container->m_controller->dolphinView();
406 updateDecorationSize(view->showPreview());
407 }
408
409 void DolphinColumnView::activate()
410 {
411 setFocus(Qt::OtherFocusReason);
412
413 if (KGlobalSettings::singleClick()) {
414 connect(this, SIGNAL(clicked(const QModelIndex&)),
415 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
416 } else {
417 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
418 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
419 }
420
421 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
422 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
423 }
424
425 updateBackground();
426 }
427
428 void DolphinColumnView::deactivate()
429 {
430 clearFocus();
431 if (KGlobalSettings::singleClick()) {
432 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
433 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
434 } else {
435 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
436 m_container->m_controller, SLOT(triggerItem(const QModelIndex&)));
437 }
438
439 const QModelIndex current = selectionModel()->currentIndex();
440 selectionModel()->clear();
441 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
442 updateBackground();
443 }
444
445 void DolphinColumnView::updateDecorationSize(bool showPreview)
446 {
447 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
448 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
449 const QSize size(iconSize, iconSize);
450 setIconSize(size);
451
452 m_decorationSize = size;
453
454 doItemsLayout();
455 }
456
457 #include "dolphincolumnview.moc"