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