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