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