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