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