]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphincolumnview.cpp
Fix spelling errors reported by crazy
[dolphin.git] / src / views / 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 "dolphinviewcontroller.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 "viewmodecontroller.h"
36 #include "zoomlevelinfo.h"
37
38 #include <kcolorscheme.h>
39 #include <kdirlister.h>
40 #include <kfileitem.h>
41 #include <kio/previewjob.h>
42 #include <kiconeffect.h>
43 #include <kjob.h>
44 #include <konqmimedata.h>
45
46 #include <QApplication>
47 #include <QClipboard>
48 #include <QPainter>
49 #include <QPoint>
50 #include <QScrollBar>
51
52 DolphinColumnView::DolphinColumnView(QWidget* parent,
53 DolphinColumnViewContainer* container,
54 const KUrl& url) :
55 QListView(parent),
56 m_active(false),
57 m_container(container),
58 m_extensionsFactory(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_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 // apply the column mode settings to the widget
82 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
83 Q_ASSERT(settings != 0);
84
85 if (settings->useSystemFont()) {
86 m_font = KGlobalSettings::generalFont();
87 } else {
88 m_font = QFont(settings->fontFamily(),
89 qRound(settings->fontSize()),
90 settings->fontWeight(),
91 settings->italicFont());
92 m_font.setPointSizeF(settings->fontSize());
93 }
94
95 connect(this, SIGNAL(viewportEntered()),
96 m_container->m_dolphinViewController, SLOT(emitViewportEntered()));
97 connect(this, SIGNAL(entered(const QModelIndex&)),
98 this, SLOT(slotEntered(const QModelIndex&)));
99
100 const DolphinView* dolphinView = m_container->m_dolphinViewController->view();
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_dolphinViewController->view()->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 const ViewModeController* viewModeController = m_container->m_viewModeController;
129 connect(viewModeController, SIGNAL(zoomLevelChanged(int)),
130 this, SLOT(setZoomLevel(int)));
131 const QString nameFilter = viewModeController->nameFilter();
132 if (!nameFilter.isEmpty()) {
133 m_proxyModel->setFilterFixedString(nameFilter);
134 }
135
136 updateDecorationSize(dolphinView->showPreview());
137 updateBackground();
138
139 DolphinViewController* dolphinViewController = m_container->m_dolphinViewController;
140 m_extensionsFactory = new ViewExtensionsFactory(this, dolphinViewController, viewModeController);
141
142 m_dirLister->openUrl(url, KDirLister::NoFlags);
143 }
144
145 DolphinColumnView::~DolphinColumnView()
146 {
147 delete m_proxyModel;
148 m_proxyModel = 0;
149 delete m_dolphinModel;
150 m_dolphinModel = 0;
151 m_dirLister = 0; // deleted by m_dolphinModel
152 }
153
154 void DolphinColumnView::setActive(bool active)
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 void DolphinColumnView::setSelectionModel(QItemSelectionModel* model)
196 {
197 // If a change of the selection is done although the view is not active
198 // (e. g. by the selection markers), the column must be activated. This
199 // is done by listening to the current selectionChanged() signal.
200 if (selectionModel() != 0) {
201 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
202 this, SLOT(requestActivation()));
203 }
204
205 QListView::setSelectionModel(model);
206
207 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
208 this, SLOT(requestActivation()));
209 }
210
211 QStyleOptionViewItem DolphinColumnView::viewOptions() const
212 {
213 QStyleOptionViewItem viewOptions = QListView::viewOptions();
214 viewOptions.font = m_font;
215 viewOptions.fontMetrics = QFontMetrics(m_font);
216 viewOptions.decorationSize = m_decorationSize;
217 viewOptions.showDecorationSelected = true;
218 return viewOptions;
219 }
220
221 void DolphinColumnView::startDrag(Qt::DropActions supportedActions)
222 {
223 DragAndDropHelper::instance().startDrag(this, supportedActions, m_container->m_dolphinViewController);
224 }
225
226 void DolphinColumnView::dragEnterEvent(QDragEnterEvent* event)
227 {
228 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
229 event->acceptProposedAction();
230 requestActivation();
231 }
232 }
233
234 void DolphinColumnView::dragLeaveEvent(QDragLeaveEvent* event)
235 {
236 QListView::dragLeaveEvent(event);
237 setDirtyRegion(m_dropRect);
238 }
239
240 void DolphinColumnView::dragMoveEvent(QDragMoveEvent* event)
241 {
242 QListView::dragMoveEvent(event);
243
244 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
245 const QModelIndex index = indexAt(event->pos());
246 setDirtyRegion(m_dropRect);
247
248 m_dropRect.setSize(QSize()); // set as invalid
249 if (index.isValid()) {
250 m_container->m_dolphinViewController->setItemView(this);
251 const KFileItem item = m_container->m_dolphinViewController->itemForIndex(index);
252 if (!item.isNull() && item.isDir()) {
253 m_dropRect = visualRect(index);
254 }
255 }
256 setDirtyRegion(m_dropRect);
257
258 if (DragAndDropHelper::instance().isMimeDataSupported(event->mimeData())) {
259 // accept url drops, independently from the destination item
260 event->acceptProposedAction();
261 }
262 }
263
264 void DolphinColumnView::dropEvent(QDropEvent* event)
265 {
266 const QModelIndex index = indexAt(event->pos());
267 m_container->m_dolphinViewController->setItemView(this);
268 const KFileItem item = m_container->m_dolphinViewController->itemForIndex(index);
269 m_container->m_dolphinViewController->indicateDroppedUrls(item, url(), event);
270 QListView::dropEvent(event);
271 }
272
273 void DolphinColumnView::paintEvent(QPaintEvent* event)
274 {
275 if (!m_childUrl.isEmpty()) {
276 // indicate the shown URL of the next column by highlighting the shown folder item
277 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
278 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
279 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
280 const QRect itemRect = visualRect(proxyIndex);
281 QPainter painter(viewport());
282 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
283 color.setAlpha(32);
284 painter.setPen(Qt::NoPen);
285 painter.setBrush(color);
286 painter.drawRect(itemRect);
287 }
288 }
289
290 QListView::paintEvent(event);
291 }
292
293 void DolphinColumnView::mousePressEvent(QMouseEvent* event)
294 {
295 requestActivation();
296 if (!indexAt(event->pos()).isValid()) {
297 if (QApplication::mouseButtons() & Qt::MidButton) {
298 m_container->m_dolphinViewController->replaceUrlByClipboard();
299 }
300 } else if (event->button() == Qt::LeftButton) {
301 // TODO: see comment in DolphinIconsView::mousePressEvent()
302 setState(QAbstractItemView::DraggingState);
303 }
304 QListView::mousePressEvent(event);
305 }
306
307 void DolphinColumnView::keyPressEvent(QKeyEvent* event)
308 {
309 QListView::keyPressEvent(event);
310
311 DolphinViewController* controller = m_container->m_dolphinViewController;
312 controller->handleKeyPressEvent(event);
313 switch (event->key()) {
314 case Qt::Key_Right: {
315 // Special key handling for the column: A Key_Right should
316 // open a new column for the currently selected folder.
317 const QModelIndex index = currentIndex();
318 const KFileItem item = controller->itemForIndex(index);
319 if (!item.isNull() && item.isDir()) {
320 controller->emitItemTriggered(item);
321 }
322 break;
323 }
324
325 case Qt::Key_Escape:
326 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(),
327 QItemSelectionModel::Current |
328 QItemSelectionModel::Clear);
329 break;
330
331 default:
332 break;
333 }
334 }
335
336 void DolphinColumnView::contextMenuEvent(QContextMenuEvent* event)
337 {
338 requestActivation();
339 QListView::contextMenuEvent(event);
340 m_container->m_dolphinViewController->triggerContextMenuRequest(event->pos());
341 }
342
343 void DolphinColumnView::wheelEvent(QWheelEvent* event)
344 {
345 const int step = m_decorationSize.height();
346 verticalScrollBar()->setSingleStep(step);
347 QListView::wheelEvent(event);
348 }
349
350 void DolphinColumnView::leaveEvent(QEvent* event)
351 {
352 QListView::leaveEvent(event);
353 // if the mouse is above an item and moved very fast outside the widget,
354 // no viewportEntered() signal might be emitted although the mouse has been moved
355 // above the viewport
356 m_container->m_dolphinViewController->emitViewportEntered();
357 }
358
359 void DolphinColumnView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
360 {
361 QListView::currentChanged(current, previous);
362 m_extensionsFactory->handleCurrentIndexChange(current, previous);
363 }
364
365 void DolphinColumnView::setZoomLevel(int level)
366 {
367 const int size = ZoomLevelInfo::iconSizeForZoomLevel(level);
368 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
369
370 const bool showPreview = m_container->m_dolphinViewController->view()->showPreview();
371 if (showPreview) {
372 settings->setPreviewSize(size);
373 } else {
374 settings->setIconSize(size);
375 }
376
377 updateDecorationSize(showPreview);
378 }
379
380 void DolphinColumnView::slotEntered(const QModelIndex& index)
381 {
382 m_container->m_dolphinViewController->setItemView(this);
383 m_container->m_dolphinViewController->emitItemEntered(index);
384 }
385
386 void DolphinColumnView::requestActivation()
387 {
388 m_container->m_dolphinViewController->requestActivation();
389 if (!m_active) {
390 m_container->requestActivation(this);
391 selectionModel()->clear();
392 }
393 }
394
395 void DolphinColumnView::updateFont()
396 {
397 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
398 Q_ASSERT(settings != 0);
399
400 if (settings->useSystemFont()) {
401 m_font = KGlobalSettings::generalFont();
402 }
403 }
404
405 void DolphinColumnView::slotShowPreviewChanged()
406 {
407 const DolphinView* view = m_container->m_dolphinViewController->view();
408 updateDecorationSize(view->showPreview());
409 }
410
411 void DolphinColumnView::activate()
412 {
413 setFocus(Qt::OtherFocusReason);
414
415 if (KGlobalSettings::singleClick()) {
416 connect(this, SIGNAL(clicked(const QModelIndex&)),
417 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
418 } else {
419 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
420 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
421 }
422
423 if (selectionModel() && selectionModel()->currentIndex().isValid()) {
424 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
425 }
426
427 updateBackground();
428 }
429
430 void DolphinColumnView::deactivate()
431 {
432 clearFocus();
433 if (KGlobalSettings::singleClick()) {
434 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
435 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
436 } else {
437 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
438 m_container->m_dolphinViewController, SLOT(triggerItem(const QModelIndex&)));
439 }
440
441 // It is important to disconnect the connection to requestActivation() temporary, otherwise the internal
442 // clearing of the selection would result in activating the column again.
443 disconnect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
444 this, SLOT(requestActivation()));
445 const QModelIndex current = selectionModel()->currentIndex();
446 selectionModel()->clear();
447 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
448 connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
449 this, SLOT(requestActivation()));
450
451 updateBackground();
452 }
453
454 void DolphinColumnView::updateDecorationSize(bool showPreview)
455 {
456 ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
457 const int iconSize = showPreview ? settings->previewSize() : settings->iconSize();
458 const QSize size(iconSize, iconSize);
459 setIconSize(size);
460
461 m_decorationSize = size;
462
463 doItemsLayout();
464 }
465
466 #include "dolphincolumnview.moc"