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