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