]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
Keep consistent all views. Peter, if you decide to revert the change that David did...
[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 "draganddrophelper.h"
30
31 #include <kcolorscheme.h>
32 #include <kdirlister.h>
33 #include <kfileitem.h>
34 #include <kio/previewjob.h>
35 #include <kiconeffect.h>
36 #include <kjob.h>
37 #include <kmimetyperesolver.h>
38 #include <konqmimedata.h>
39
40 #include <QApplication>
41 #include <QClipboard>
42 #include <QPainter>
43 #include <QPoint>
44
45 DolphinColumnWidget::DolphinColumnWidget(QWidget* parent,
46 DolphinColumnView* columnView,
47 const KUrl& url) :
48 QListView(parent),
49 m_active(true),
50 m_showPreview(false),
51 m_view(columnView),
52 m_url(url),
53 m_childUrl(),
54 m_font(),
55 m_decorationSize(),
56 m_dirLister(0),
57 m_dolphinModel(0),
58 m_proxyModel(0),
59 m_previewJob(0),
60 m_dragging(false),
61 m_dropRect()
62 {
63 setMouseTracking(true);
64 viewport()->setAttribute(Qt::WA_Hover);
65 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
67 setSelectionBehavior(SelectItems);
68 setSelectionMode(QAbstractItemView::ExtendedSelection);
69 setDragDropMode(QAbstractItemView::DragDrop);
70 setDropIndicatorShown(false);
71
72 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
73 // check avoids a division by zero happening on versions before 4.3.1.
74 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
75 // ereslibre
76 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
77 setVerticalScrollMode(QListView::ScrollPerPixel);
78 setHorizontalScrollMode(QListView::ScrollPerPixel);
79 #endif
80
81 // apply the column mode settings to the widget
82 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
83 Q_ASSERT(settings != 0);
84
85 m_font = QFont(settings->fontFamily(), settings->fontSize());
86 m_font.setItalic(settings->italicFont());
87 m_font.setBold(settings->boldFont());
88
89 const int iconSize = settings->iconSize();
90 m_decorationSize = QSize(iconSize, iconSize);
91
92 KFileItemDelegate* delegate = new KFileItemDelegate(this);
93 setItemDelegate(delegate);
94
95 activate();
96
97 connect(this, SIGNAL(viewportEntered()),
98 m_view->m_controller, SLOT(emitViewportEntered()));
99 connect(this, SIGNAL(entered(const QModelIndex&)),
100 this, SLOT(slotEntered(const QModelIndex&)));
101
102 //m_dirLister = new DolphinDirLister(); TODO
103 m_dirLister = new KDirLister();
104 m_dirLister->setAutoUpdate(true);
105 m_dirLister->setMainWindow(this);
106 m_dirLister->setDelayedMimeTypes(true);
107 const bool showHiddenFiles = m_view->m_controller->dolphinView()->showHiddenFiles();
108 m_dirLister->setShowingDotFiles(showHiddenFiles);
109 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
110 this, SLOT(generatePreviews(const KFileItemList&)));
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
119 setModel(m_proxyModel);
120 new KMimeTypeResolver(this, m_dolphinModel);
121
122 m_dirLister->openUrl(url, KDirLister::NoFlags);
123 }
124
125 DolphinColumnWidget::~DolphinColumnWidget()
126 {
127 delete m_proxyModel;
128 m_proxyModel = 0;
129 delete m_dolphinModel;
130 m_dolphinModel = 0;
131 m_dirLister = 0; // deleted by m_dolphinModel
132
133 if (m_previewJob != 0) {
134 m_previewJob->kill();
135 m_previewJob = 0;
136 }
137 }
138
139 void DolphinColumnWidget::setDecorationSize(const QSize& size)
140 {
141 m_decorationSize = size;
142 doItemsLayout();
143 }
144
145 void DolphinColumnWidget::setActive(bool active)
146 {
147 if (m_active == active) {
148 return;
149 }
150
151 m_active = active;
152
153 if (active) {
154 activate();
155 } else {
156 deactivate();
157 }
158 }
159
160 void DolphinColumnWidget::reload()
161 {
162 m_dirLister->stop();
163 m_dirLister->openUrl(m_url, KDirLister::Reload);
164 }
165
166 void DolphinColumnWidget::setShowHiddenFiles(bool show)
167 {
168 if (show != m_dirLister->showingDotFiles()) {
169 m_dirLister->setShowingDotFiles(show);
170 m_dirLister->stop();
171 m_dirLister->openUrl(m_url, KDirLister::Reload);
172 }
173 }
174
175 void DolphinColumnWidget::setShowPreview(bool show)
176 {
177 if (show != m_showPreview) {
178 m_dirLister->stop();
179 m_dirLister->openUrl(m_url, KDirLister::Reload);
180 }
181 }
182
183 void DolphinColumnWidget::updateBackground()
184 {
185 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
186 if (!m_active || !m_view->m_active) {
187 color.setAlpha(150);
188 }
189 QPalette palette = viewport()->palette();
190 palette.setColor(viewport()->backgroundRole(), color);
191 viewport()->setPalette(palette);
192
193 update();
194 }
195
196 void DolphinColumnWidget::setNameFilter(const QString& nameFilter)
197 {
198 m_proxyModel->setFilterRegExp(nameFilter);
199 }
200
201
202 QStyleOptionViewItem DolphinColumnWidget::viewOptions() const
203 {
204 QStyleOptionViewItem viewOptions = QListView::viewOptions();
205 viewOptions.font = m_font;
206 viewOptions.decorationSize = m_decorationSize;
207 viewOptions.showDecorationSelected = true;
208 return viewOptions;
209 }
210
211 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions)
212 {
213 DragAndDropHelper::startDrag(this, supportedActions);
214 }
215
216 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent* event)
217 {
218 if (event->mimeData()->hasUrls()) {
219 event->acceptProposedAction();
220 }
221
222 m_dragging = true;
223 }
224
225 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
226 {
227 QListView::dragLeaveEvent(event);
228
229 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
230 m_dragging = false;
231 setDirtyRegion(m_dropRect);
232 }
233
234 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent* event)
235 {
236 QListView::dragMoveEvent(event);
237
238 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
239 const QModelIndex index = indexAt(event->pos());
240 setDirtyRegion(m_dropRect);
241
242 m_dropRect.setSize(QSize()); // set as invalid
243 if (index.isValid()) {
244 const KFileItem item = itemForIndex(index);
245 if (!item.isNull() && item.isDir()) {
246 m_dropRect = visualRect(index);
247 }
248 }
249 setDirtyRegion(m_dropRect);
250
251 bool destIsDir = false;
252 if (index.isValid()) {
253 const KFileItem item = itemForIndex(index);
254 if (!item.isNull() && item.isDir()) {
255 m_dropRect = visualRect(index);
256 destIsDir = true;
257 }
258 } else { // dropping on viewport
259 destIsDir = true;
260 }
261 if (destIsDir && event->mimeData()->hasUrls()) {
262 event->acceptProposedAction();
263 }
264 }
265
266 void DolphinColumnWidget::dropEvent(QDropEvent* event)
267 {
268 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
269 if (!urls.isEmpty()) {
270 const QModelIndex index = indexAt(event->pos());
271 const KFileItem item = itemForIndex(index);
272 m_view->m_controller->indicateDroppedUrls(urls,
273 url(),
274 item);
275 event->acceptProposedAction();
276 }
277 QListView::dropEvent(event);
278 m_dragging = false;
279 }
280
281 void DolphinColumnWidget::paintEvent(QPaintEvent* event)
282 {
283 if (!m_childUrl.isEmpty()) {
284 // indicate the shown URL of the next column by highlighting the shown folder item
285 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
286 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
287 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
288 const QRect itemRect = visualRect(proxyIndex);
289 QPainter painter(viewport());
290 painter.save();
291
292 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
293 color.setAlpha(32);
294 painter.setPen(Qt::NoPen);
295 painter.setBrush(color);
296 painter.drawRect(itemRect);
297
298 painter.restore();
299 }
300 }
301
302 QListView::paintEvent(event);
303
304 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
305 if (m_dragging) {
306 const QBrush& brush = viewOptions().palette.brush(QPalette::Normal, QPalette::Highlight);
307 DragAndDropHelper::drawHoverIndication(this, m_dropRect, brush);
308 }
309 }
310
311 void DolphinColumnWidget::mousePressEvent(QMouseEvent* event)
312 {
313 m_view->m_controller->requestActivation();
314 if (!m_active) {
315 m_view->requestActivation(this);
316 m_view->m_controller->triggerUrlChangeRequest(m_url);
317 }
318
319 QListView::mousePressEvent(event);
320 }
321
322 void DolphinColumnWidget::keyPressEvent(QKeyEvent* event)
323 {
324 QListView::keyPressEvent(event);
325
326 const QItemSelectionModel* selModel = selectionModel();
327 const QModelIndex currentIndex = selModel->currentIndex();
328 const bool trigger = currentIndex.isValid()
329 && (event->key() == Qt::Key_Return)
330 && (selModel->selectedIndexes().count() <= 1);
331 if (trigger) {
332 triggerItem(currentIndex);
333 }
334 }
335
336 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent* event)
337 {
338 if (!m_active) {
339 m_view->requestActivation(this);
340 m_view->m_controller->triggerUrlChangeRequest(m_url);
341 }
342
343 QListView::contextMenuEvent(event);
344
345 const QModelIndex index = indexAt(event->pos());
346 if (index.isValid() || m_active) {
347 // Only open a context menu above an item or if the mouse is above
348 // the active column.
349 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
350 m_view->m_controller->triggerContextMenuRequest(pos);
351 }
352 }
353
354 void DolphinColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
355 {
356 QListView::selectionChanged(selected, deselected);
357
358 QItemSelectionModel* selModel = m_view->selectionModel();
359 selModel->select(selected, QItemSelectionModel::Select);
360 selModel->select(deselected, QItemSelectionModel::Deselect);
361 }
362
363 void DolphinColumnWidget::triggerItem(const QModelIndex& index)
364 {
365 const KFileItem item = itemForIndex(index);
366 m_view->m_controller->triggerItem(item);
367 }
368
369 void DolphinColumnWidget::generatePreviews(const KFileItemList& items)
370 {
371 // TODO: same implementation as in DolphinView; create helper class
372 // for generatePreviews(), showPreview() and isCutItem()
373
374 if (m_view->m_controller->dolphinView()->showPreview()) {
375 if (m_previewJob != 0) {
376 m_previewJob->kill();
377 m_previewJob = 0;
378 }
379
380 m_previewJob = KIO::filePreview(items, 128);
381 connect(m_previewJob, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
382 this, SLOT(replaceIcon(const KFileItem&, const QPixmap&)));
383 connect(m_previewJob, SIGNAL(finished(KJob*)),
384 this, SLOT(slotPreviewJobFinished(KJob*)));
385 }
386 }
387
388 void DolphinColumnWidget::replaceIcon(const KFileItem& item, const QPixmap& pixmap)
389 {
390 // TODO: same implementation as in DolphinView; create helper class
391 // for generatePreviews(), showPreview() and isCutItem()
392
393 Q_ASSERT(!item.isNull());
394 const bool showPreview = m_view->m_controller->dolphinView()->showPreview();
395 if (!showPreview || (item.url().directory() != m_dirLister->url().path())) {
396 // the preview job is still working on items of an older URL, hence
397 // the item is not part of the directory model anymore
398 return;
399 }
400
401 const QModelIndex idx = m_dolphinModel->indexForItem(item);
402 if (idx.isValid() && (idx.column() == 0)) {
403 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
404 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
405 KIconEffect iconEffect;
406 const QPixmap cutPixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
407 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
408 } else {
409 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
410 }
411 }
412 }
413
414 void DolphinColumnWidget::slotEntered(const QModelIndex& index)
415 {
416 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
417 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
418 m_view->m_controller->emitItemEntered(item);
419 }
420
421 void DolphinColumnWidget::slotPreviewJobFinished(KJob* job)
422 {
423 Q_ASSERT(job == m_previewJob);
424 m_previewJob = 0;
425 }
426
427 void DolphinColumnWidget::activate()
428 {
429 setFocus(Qt::OtherFocusReason);
430
431 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
432 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
433 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
434 if (KGlobalSettings::singleClick()) {
435 connect(this, SIGNAL(clicked(const QModelIndex&)),
436 this, SLOT(triggerItem(const QModelIndex&)));
437 } else {
438 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
439 this, SLOT(triggerItem(const QModelIndex&)));
440 }
441
442 if (!m_childUrl.isEmpty()) {
443 // assure that the current index is set on the index that represents
444 // the child URL
445 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
446 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
447 selectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::Current);
448 }
449
450 updateBackground();
451 }
452
453 void DolphinColumnWidget::deactivate()
454 {
455 clearFocus();
456
457 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
458 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
459 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
460 if (KGlobalSettings::singleClick()) {
461 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
462 this, SLOT(triggerItem(const QModelIndex&)));
463 } else {
464 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
465 this, SLOT(triggerItem(const QModelIndex&)));
466 }
467
468 selectionModel()->clear();
469 updateBackground();
470 }
471
472 bool DolphinColumnWidget::isCutItem(const KFileItem& item) const
473 {
474 // TODO: same implementation as in DolphinView; create helper class
475 // for generatePreviews(), showPreview() and isCutItem()
476
477 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
478 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
479
480 const KUrl& itemUrl = item.url();
481 KUrl::List::const_iterator it = cutUrls.begin();
482 const KUrl::List::const_iterator end = cutUrls.end();
483 while (it != end) {
484 if (*it == itemUrl) {
485 return true;
486 }
487 ++it;
488 }
489
490 return false;
491 }
492
493 KFileItem DolphinColumnWidget::itemForIndex(const QModelIndex& index) const
494 {
495 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
496 return m_dolphinModel->itemForIndex(dirIndex);
497 }
498
499
500 #include "dolphincolumnwidget.moc"