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