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