]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
Fix flicker when resizing the central view area in Dolphin. Only applicable with...
[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 "dolphin_columnmodesettings.h"
29 #include "dolphin_generalsettings.h"
30 #include "draganddrophelper.h"
31 #include "selectionmanager.h"
32
33 #include <kcolorscheme.h>
34 #include <kdirlister.h>
35 #include <kfileitem.h>
36 #include <kio/previewjob.h>
37 #include <kiconeffect.h>
38 #include <kjob.h>
39 #include <kmimetyperesolver.h>
40 #include <konqmimedata.h>
41
42 #include "iconmanager.h"
43
44 #include <QApplication>
45 #include <QClipboard>
46 #include <QPainter>
47 #include <QPoint>
48
49 DolphinColumnWidget::DolphinColumnWidget(QWidget* parent,
50 DolphinColumnView* columnView,
51 const KUrl& url) :
52 QListView(parent),
53 m_active(true),
54 m_view(columnView),
55 m_url(url),
56 m_childUrl(),
57 m_font(),
58 m_decorationSize(),
59 m_dirLister(0),
60 m_dolphinModel(0),
61 m_proxyModel(0),
62 m_iconManager(0),
63 m_dragging(false),
64 m_dropRect()
65 {
66 setMouseTracking(true);
67 viewport()->setAttribute(Qt::WA_Hover);
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
76 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
77 // check avoids a division by zero happening on versions before 4.3.1.
78 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
79 // ereslibre
80 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
81 setVerticalScrollMode(QListView::ScrollPerPixel);
82 setHorizontalScrollMode(QListView::ScrollPerPixel);
83 #endif
84
85 // apply the column mode settings to the widget
86 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
87 Q_ASSERT(settings != 0);
88
89 m_font = QFont(settings->fontFamily(), settings->fontSize());
90 m_font.setItalic(settings->italicFont());
91 m_font.setBold(settings->boldFont());
92
93 const int iconSize = settings->iconSize();
94 setDecorationSize(QSize(iconSize, iconSize));
95
96 KFileItemDelegate* delegate = new KFileItemDelegate(this);
97 setItemDelegate(delegate);
98
99 activate();
100
101 connect(this, SIGNAL(viewportEntered()),
102 m_view->m_controller, SLOT(emitViewportEntered()));
103 connect(this, SIGNAL(entered(const QModelIndex&)),
104 this, SLOT(slotEntered(const QModelIndex&)));
105
106 //m_dirLister = new DolphinDirLister(); TODO
107 m_dirLister = new KDirLister();
108 m_dirLister->setAutoUpdate(true);
109 m_dirLister->setMainWindow(topLevelWidget());
110 m_dirLister->setDelayedMimeTypes(true);
111 const bool showHiddenFiles = m_view->m_controller->dolphinView()->showHiddenFiles();
112 m_dirLister->setShowingDotFiles(showHiddenFiles);
113
114 m_dolphinModel = new DolphinModel(this);
115 m_dolphinModel->setDirLister(m_dirLister);
116 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
117
118 m_proxyModel = new DolphinSortFilterProxyModel(this);
119 m_proxyModel->setSourceModel(m_dolphinModel);
120 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
121 const DolphinView* dolphinView = m_view->m_controller->dolphinView();
122 m_proxyModel->setSorting(dolphinView->sorting());
123 m_proxyModel->setSortOrder(dolphinView->sortOrder());
124
125 setModel(m_proxyModel);
126 const bool useSelManager = KGlobalSettings::singleClick() &&
127 DolphinSettings::instance().generalSettings()->showSelectionToggle();
128 if (useSelManager) {
129 SelectionManager* selManager = new SelectionManager(this);
130 connect(selManager, SIGNAL(selectionChanged()),
131 this, SLOT(requestActivation()));
132 connect(m_view->m_controller, SIGNAL(urlChanged(const KUrl&)),
133 selManager, SLOT(reset()));
134 }
135 new KMimeTypeResolver(this, m_dolphinModel);
136 m_iconManager = new IconManager(this, m_proxyModel);
137 m_iconManager->setShowPreview(m_view->m_controller->dolphinView()->showPreview());
138
139 m_dirLister->openUrl(url, KDirLister::NoFlags);
140 }
141
142 DolphinColumnWidget::~DolphinColumnWidget()
143 {
144 delete m_proxyModel;
145 m_proxyModel = 0;
146 delete m_dolphinModel;
147 m_dolphinModel = 0;
148 m_dirLister = 0; // deleted by m_dolphinModel
149 }
150
151 void DolphinColumnWidget::setDecorationSize(const QSize& size)
152 {
153 setIconSize(size);
154 m_decorationSize = size;
155 doItemsLayout();
156 }
157
158 void DolphinColumnWidget::setActive(bool active)
159 {
160 if (m_active == active) {
161 return;
162 }
163
164 m_active = active;
165
166 if (active) {
167 activate();
168 } else {
169 deactivate();
170 }
171 }
172
173 void DolphinColumnWidget::reload()
174 {
175 m_dirLister->stop();
176 m_dirLister->openUrl(m_url, KDirLister::Reload);
177 }
178
179 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting)
180 {
181 m_proxyModel->setSorting(sorting);
182 }
183
184 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order)
185 {
186 m_proxyModel->setSortOrder(order);
187 }
188
189 void DolphinColumnWidget::setShowHiddenFiles(bool show)
190 {
191 if (show != m_dirLister->showingDotFiles()) {
192 m_dirLister->setShowingDotFiles(show);
193 m_dirLister->stop();
194 m_dirLister->openUrl(m_url, KDirLister::Reload);
195 }
196 }
197
198 void DolphinColumnWidget::setShowPreview(bool show)
199 {
200 m_iconManager->setShowPreview(show);
201
202 m_dirLister->stop();
203 m_dirLister->openUrl(m_url, KDirLister::Reload);
204 }
205
206 void DolphinColumnWidget::updateBackground()
207 {
208 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
209 // cleaning up the cut-indication of DolphinColumnWidget with the code from
210 // DolphinView a common helper-class should be available which can be shared
211 // by all view implementations -> no hardcoded value anymore
212 const QPalette::ColorRole role = viewport()->backgroundRole();
213 QColor color = viewport()->palette().color(role);
214 color.setAlpha((m_active && m_view->m_active) ? 255 : 150);
215
216 QPalette palette = viewport()->palette();
217 palette.setColor(role, color);
218 viewport()->setPalette(palette);
219
220 update();
221 }
222
223 void DolphinColumnWidget::setNameFilter(const QString& nameFilter)
224 {
225 m_proxyModel->setFilterRegExp(nameFilter);
226 }
227
228
229 QStyleOptionViewItem DolphinColumnWidget::viewOptions() const
230 {
231 QStyleOptionViewItem viewOptions = QListView::viewOptions();
232 viewOptions.font = m_font;
233 viewOptions.decorationSize = m_decorationSize;
234 viewOptions.showDecorationSelected = true;
235 return viewOptions;
236 }
237
238 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions)
239 {
240 DragAndDropHelper::startDrag(this, supportedActions);
241 }
242
243 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent* event)
244 {
245 if (event->mimeData()->hasUrls()) {
246 event->acceptProposedAction();
247 }
248
249 m_dragging = true;
250 }
251
252 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
253 {
254 QListView::dragLeaveEvent(event);
255
256 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
257 m_dragging = false;
258 setDirtyRegion(m_dropRect);
259 }
260
261 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent* event)
262 {
263 QListView::dragMoveEvent(event);
264
265 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
266 const QModelIndex index = indexAt(event->pos());
267 setDirtyRegion(m_dropRect);
268
269 m_dropRect.setSize(QSize()); // set as invalid
270 if (index.isValid()) {
271 const KFileItem item = itemForIndex(index);
272 if (!item.isNull() && item.isDir()) {
273 m_dropRect = visualRect(index);
274 }
275 }
276 setDirtyRegion(m_dropRect);
277
278 if (event->mimeData()->hasUrls()) {
279 // accept url drops, independently from the destination item
280 event->acceptProposedAction();
281 }
282 }
283
284 void DolphinColumnWidget::dropEvent(QDropEvent* event)
285 {
286 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
287 if (!urls.isEmpty()) {
288 const QModelIndex index = indexAt(event->pos());
289 const KFileItem item = itemForIndex(index);
290 m_view->m_controller->indicateDroppedUrls(urls,
291 url(),
292 item);
293 event->acceptProposedAction();
294 }
295 QListView::dropEvent(event);
296 m_dragging = false;
297 }
298
299 void DolphinColumnWidget::paintEvent(QPaintEvent* event)
300 {
301 if (!m_childUrl.isEmpty()) {
302 // indicate the shown URL of the next column by highlighting the shown folder item
303 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
304 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
305 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
306 const QRect itemRect = visualRect(proxyIndex);
307 QPainter painter(viewport());
308 painter.save();
309
310 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
311 color.setAlpha(32);
312 painter.setPen(Qt::NoPen);
313 painter.setBrush(color);
314 painter.drawRect(itemRect);
315
316 painter.restore();
317 }
318 }
319
320 QListView::paintEvent(event);
321
322 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
323 if (m_dragging) {
324 const QBrush& brush = viewOptions().palette.brush(QPalette::Normal, QPalette::Highlight);
325 DragAndDropHelper::drawHoverIndication(this, m_dropRect, brush);
326 }
327 }
328
329 void DolphinColumnWidget::mousePressEvent(QMouseEvent* event)
330 {
331 requestActivation();
332 QListView::mousePressEvent(event);
333 }
334
335 void DolphinColumnWidget::keyPressEvent(QKeyEvent* event)
336 {
337 QListView::keyPressEvent(event);
338
339 const QItemSelectionModel* selModel = selectionModel();
340 const QModelIndex currentIndex = selModel->currentIndex();
341 const bool trigger = currentIndex.isValid()
342 && (event->key() == Qt::Key_Return)
343 && (selModel->selectedIndexes().count() <= 1);
344 if (trigger) {
345 triggerItem(currentIndex);
346 }
347 }
348
349 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent* event)
350 {
351 if (!m_active) {
352 m_view->requestActivation(this);
353 m_view->m_controller->triggerUrlChangeRequest(m_url);
354 }
355
356 QListView::contextMenuEvent(event);
357
358 const QModelIndex index = indexAt(event->pos());
359 if (index.isValid() || m_active) {
360 // Only open a context menu above an item or if the mouse is above
361 // the active column.
362 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
363 m_view->m_controller->triggerContextMenuRequest(pos);
364 }
365 }
366
367 void DolphinColumnWidget::wheelEvent(QWheelEvent* event)
368 {
369 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
370 if ((event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier) {
371 event->ignore();
372 return;
373 }
374 QListView::wheelEvent(event);
375 }
376
377 void DolphinColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
378 {
379 QListView::selectionChanged(selected, deselected);
380
381 QItemSelectionModel* selModel = m_view->selectionModel();
382 selModel->select(selected, QItemSelectionModel::Select);
383 selModel->select(deselected, QItemSelectionModel::Deselect);
384 }
385
386 void DolphinColumnWidget::triggerItem(const QModelIndex& index)
387 {
388 const KFileItem item = itemForIndex(index);
389 m_view->m_controller->triggerItem(item);
390 }
391
392 void DolphinColumnWidget::slotEntered(const QModelIndex& index)
393 {
394 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
395 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
396 m_view->m_controller->emitItemEntered(item);
397 }
398
399 void DolphinColumnWidget::requestActivation()
400 {
401 m_view->m_controller->requestActivation();
402 if (!m_active) {
403 m_view->requestActivation(this);
404 m_view->m_controller->triggerUrlChangeRequest(m_url);
405 selectionModel()->clear();
406 }
407 }
408
409 void DolphinColumnWidget::activate()
410 {
411 setFocus(Qt::OtherFocusReason);
412
413 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
414 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
415 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
416 if (KGlobalSettings::singleClick()) {
417 connect(this, SIGNAL(clicked(const QModelIndex&)),
418 this, SLOT(triggerItem(const QModelIndex&)));
419 } else {
420 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
421 this, SLOT(triggerItem(const QModelIndex&)));
422 }
423
424 if (selectionModel() && selectionModel()->currentIndex().isValid())
425 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
426
427 updateBackground();
428 }
429
430 void DolphinColumnWidget::deactivate()
431 {
432 clearFocus();
433
434 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
435 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
436 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
437 if (KGlobalSettings::singleClick()) {
438 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
439 this, SLOT(triggerItem(const QModelIndex&)));
440 } else {
441 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
442 this, SLOT(triggerItem(const QModelIndex&)));
443 }
444
445 const QModelIndex current = selectionModel()->currentIndex();
446 selectionModel()->clear();
447 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
448 updateBackground();
449 }
450
451 KFileItem DolphinColumnWidget::itemForIndex(const QModelIndex& index) const
452 {
453 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
454 return m_dolphinModel->itemForIndex(dirIndex);
455 }
456
457
458 #include "dolphincolumnwidget.moc"