]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
fixed HIG color violations
[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 setSelectionRectVisible(true);
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_font = QFont(settings->fontFamily(), settings->fontSize());
87 m_font.setItalic(settings->italicFont());
88 m_font.setBold(settings->boldFont());
89
90 const int iconSize = settings->iconSize();
91 m_decorationSize = QSize(iconSize, iconSize);
92
93 KFileItemDelegate* delegate = new KFileItemDelegate(this);
94 setItemDelegate(delegate);
95
96 activate();
97
98 connect(this, SIGNAL(viewportEntered()),
99 m_view->m_controller, SLOT(emitViewportEntered()));
100 connect(this, SIGNAL(entered(const QModelIndex&)),
101 this, SLOT(slotEntered(const QModelIndex&)));
102
103 //m_dirLister = new DolphinDirLister(); TODO
104 m_dirLister = new KDirLister();
105 m_dirLister->setAutoUpdate(true);
106 m_dirLister->setMainWindow(this);
107 m_dirLister->setDelayedMimeTypes(true);
108 const bool showHiddenFiles = m_view->m_controller->dolphinView()->showHiddenFiles();
109 m_dirLister->setShowingDotFiles(showHiddenFiles);
110 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
111 this, SLOT(generatePreviews(const KFileItemList&)));
112
113 m_dolphinModel = new DolphinModel(this);
114 m_dolphinModel->setDirLister(m_dirLister);
115 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
116
117 m_proxyModel = new DolphinSortFilterProxyModel(this);
118 m_proxyModel->setSourceModel(m_dolphinModel);
119 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
120
121 setModel(m_proxyModel);
122 new KMimeTypeResolver(this, m_dolphinModel);
123
124 m_dirLister->openUrl(url, KDirLister::NoFlags);
125 }
126
127 DolphinColumnWidget::~DolphinColumnWidget()
128 {
129 delete m_proxyModel;
130 m_proxyModel = 0;
131 delete m_dolphinModel;
132 m_dolphinModel = 0;
133 m_dirLister = 0; // deleted by m_dolphinModel
134
135 if (m_previewJob != 0) {
136 m_previewJob->kill();
137 m_previewJob = 0;
138 }
139 }
140
141 void DolphinColumnWidget::setDecorationSize(const QSize& size)
142 {
143 m_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::updateBackground()
186 {
187 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
188 // cleaning up the cut-indication of DolphinColumnWidget with the code from
189 // DolphinView a common helper-class should be available which can be shared
190 // by all view implementations -> no hardcoded value anymore
191 const QPalette::ColorRole role = viewport()->backgroundRole();
192 QColor color = viewport()->palette().color(role);
193 color.setAlpha((m_active && m_view->m_active) ? 255 : 150);
194
195 QPalette palette = viewport()->palette();
196 palette.setColor(role, color);
197 viewport()->setPalette(palette);
198
199 update();
200 }
201
202 void DolphinColumnWidget::setNameFilter(const QString& nameFilter)
203 {
204 m_proxyModel->setFilterRegExp(nameFilter);
205 }
206
207
208 QStyleOptionViewItem DolphinColumnWidget::viewOptions() const
209 {
210 QStyleOptionViewItem viewOptions = QListView::viewOptions();
211 viewOptions.font = m_font;
212 viewOptions.decorationSize = m_decorationSize;
213 viewOptions.showDecorationSelected = true;
214 return viewOptions;
215 }
216
217 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions)
218 {
219 DragAndDropHelper::startDrag(this, supportedActions);
220 }
221
222 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent* event)
223 {
224 if (event->mimeData()->hasUrls()) {
225 event->acceptProposedAction();
226 }
227
228 m_dragging = true;
229 }
230
231 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
232 {
233 QListView::dragLeaveEvent(event);
234
235 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
236 m_dragging = false;
237 setDirtyRegion(m_dropRect);
238 }
239
240 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent* event)
241 {
242 QListView::dragMoveEvent(event);
243
244 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
245 const QModelIndex index = indexAt(event->pos());
246 setDirtyRegion(m_dropRect);
247
248 m_dropRect.setSize(QSize()); // set as invalid
249 if (index.isValid()) {
250 const KFileItem item = itemForIndex(index);
251 if (!item.isNull() && item.isDir()) {
252 m_dropRect = visualRect(index);
253 }
254 }
255 setDirtyRegion(m_dropRect);
256
257 if (event->mimeData()->hasUrls()) {
258 // accept url drops, independently from the destination item
259 event->acceptProposedAction();
260 }
261 }
262
263 void DolphinColumnWidget::dropEvent(QDropEvent* event)
264 {
265 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
266 if (!urls.isEmpty()) {
267 const QModelIndex index = indexAt(event->pos());
268 const KFileItem item = itemForIndex(index);
269 m_view->m_controller->indicateDroppedUrls(urls,
270 url(),
271 item);
272 event->acceptProposedAction();
273 }
274 QListView::dropEvent(event);
275 m_dragging = false;
276 }
277
278 void DolphinColumnWidget::paintEvent(QPaintEvent* event)
279 {
280 if (!m_childUrl.isEmpty()) {
281 // indicate the shown URL of the next column by highlighting the shown folder item
282 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
283 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
284 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
285 const QRect itemRect = visualRect(proxyIndex);
286 QPainter painter(viewport());
287 painter.save();
288
289 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
290 color.setAlpha(32);
291 painter.setPen(Qt::NoPen);
292 painter.setBrush(color);
293 painter.drawRect(itemRect);
294
295 painter.restore();
296 }
297 }
298
299 QListView::paintEvent(event);
300
301 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
302 if (m_dragging) {
303 const QBrush& brush = viewOptions().palette.brush(QPalette::Normal, QPalette::Highlight);
304 DragAndDropHelper::drawHoverIndication(this, m_dropRect, brush);
305 }
306 }
307
308 void DolphinColumnWidget::mousePressEvent(QMouseEvent* event)
309 {
310 m_view->m_controller->requestActivation();
311 if (!m_active) {
312 m_view->requestActivation(this);
313 m_view->m_controller->triggerUrlChangeRequest(m_url);
314 }
315
316 QListView::mousePressEvent(event);
317 }
318
319 void DolphinColumnWidget::keyPressEvent(QKeyEvent* event)
320 {
321 QListView::keyPressEvent(event);
322
323 const QItemSelectionModel* selModel = selectionModel();
324 const QModelIndex currentIndex = selModel->currentIndex();
325 const bool trigger = currentIndex.isValid()
326 && (event->key() == Qt::Key_Return)
327 && (selModel->selectedIndexes().count() <= 1);
328 if (trigger) {
329 triggerItem(currentIndex);
330 }
331 }
332
333 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent* event)
334 {
335 if (!m_active) {
336 m_view->requestActivation(this);
337 m_view->m_controller->triggerUrlChangeRequest(m_url);
338 }
339
340 QListView::contextMenuEvent(event);
341
342 const QModelIndex index = indexAt(event->pos());
343 if (index.isValid() || m_active) {
344 // Only open a context menu above an item or if the mouse is above
345 // the active column.
346 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
347 m_view->m_controller->triggerContextMenuRequest(pos);
348 }
349 }
350
351 void DolphinColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
352 {
353 QListView::selectionChanged(selected, deselected);
354
355 QItemSelectionModel* selModel = m_view->selectionModel();
356 selModel->select(selected, QItemSelectionModel::Select);
357 selModel->select(deselected, QItemSelectionModel::Deselect);
358 }
359
360 void DolphinColumnWidget::triggerItem(const QModelIndex& index)
361 {
362 const KFileItem item = itemForIndex(index);
363 m_view->m_controller->triggerItem(item);
364 }
365
366 void DolphinColumnWidget::generatePreviews(const KFileItemList& items)
367 {
368 // TODO: same implementation as in DolphinView; create helper class
369 // for generatePreviews(), showPreview() and isCutItem()
370
371 if (m_view->m_controller->dolphinView()->showPreview()) {
372 if (m_previewJob != 0) {
373 m_previewJob->kill();
374 m_previewJob = 0;
375 }
376
377 m_previewJob = KIO::filePreview(items, 128);
378 connect(m_previewJob, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
379 this, SLOT(replaceIcon(const KFileItem&, const QPixmap&)));
380 connect(m_previewJob, SIGNAL(finished(KJob*)),
381 this, SLOT(slotPreviewJobFinished(KJob*)));
382 }
383 }
384
385 void DolphinColumnWidget::replaceIcon(const KFileItem& item, const QPixmap& pixmap)
386 {
387 // TODO: same implementation as in DolphinView; create helper class
388 // for generatePreviews(), showPreview() and isCutItem()
389
390 Q_ASSERT(!item.isNull());
391 const bool showPreview = m_view->m_controller->dolphinView()->showPreview();
392 if (!showPreview || (item.url().directory() != m_dirLister->url().path())) {
393 // the preview job is still working on items of an older URL, hence
394 // the item is not part of the directory model anymore
395 return;
396 }
397
398 const QModelIndex idx = m_dolphinModel->indexForItem(item);
399 if (idx.isValid() && (idx.column() == 0)) {
400 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
401 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
402 KIconEffect iconEffect;
403 const QPixmap cutPixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
404 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
405 } else {
406 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
407 }
408 }
409 }
410
411 void DolphinColumnWidget::slotEntered(const QModelIndex& index)
412 {
413 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
414 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
415 m_view->m_controller->emitItemEntered(item);
416 }
417
418 void DolphinColumnWidget::slotPreviewJobFinished(KJob* job)
419 {
420 Q_ASSERT(job == m_previewJob);
421 m_previewJob = 0;
422 }
423
424 void DolphinColumnWidget::activate()
425 {
426 setFocus(Qt::OtherFocusReason);
427
428 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
429 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
430 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
431 if (KGlobalSettings::singleClick()) {
432 connect(this, SIGNAL(clicked(const QModelIndex&)),
433 this, SLOT(triggerItem(const QModelIndex&)));
434 } else {
435 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
436 this, SLOT(triggerItem(const QModelIndex&)));
437 }
438
439 if (!m_childUrl.isEmpty()) {
440 // assure that the current index is set on the index that represents
441 // the child URL
442 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
443 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
444 selectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::Current);
445 }
446
447 updateBackground();
448 }
449
450 void DolphinColumnWidget::deactivate()
451 {
452 clearFocus();
453
454 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
455 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
456 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
457 if (KGlobalSettings::singleClick()) {
458 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
459 this, SLOT(triggerItem(const QModelIndex&)));
460 } else {
461 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
462 this, SLOT(triggerItem(const QModelIndex&)));
463 }
464
465 selectionModel()->clear();
466 updateBackground();
467 }
468
469 bool DolphinColumnWidget::isCutItem(const KFileItem& item) const
470 {
471 // TODO: same implementation as in DolphinView; create helper class
472 // for generatePreviews(), showPreview() and isCutItem()
473
474 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
475 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
476
477 const KUrl& itemUrl = item.url();
478 KUrl::List::const_iterator it = cutUrls.begin();
479 const KUrl::List::const_iterator end = cutUrls.end();
480 while (it != end) {
481 if (*it == itemUrl) {
482 return true;
483 }
484 ++it;
485 }
486
487 return false;
488 }
489
490 KFileItem DolphinColumnWidget::itemForIndex(const QModelIndex& index) const
491 {
492 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
493 return m_dolphinModel->itemForIndex(dirIndex);
494 }
495
496
497 #include "dolphincolumnwidget.moc"