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