]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
fix issues when the split view is active and the inactive column view gets selected...
[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 <konqmimedata.h>
39
40 #include <QAbstractProxyModel>
41 #include <QApplication>
42 #include <QClipboard>
43 #include <QPoint>
44 #include <QScrollBar>
45 #include <QTimer>
46 #include <QTimeLine>
47
48 DolphinColumnWidget::DolphinColumnWidget(QWidget* parent,
49 DolphinColumnView* columnView,
50 const KUrl& url) :
51 QListView(parent),
52 m_active(true),
53 m_showPreview(false),
54 m_view(columnView),
55 m_url(url),
56 m_childUrl(),
57 m_viewOptions(),
58 m_dirLister(0),
59 m_dolphinModel(0),
60 m_proxyModel(0),
61 m_dragging(false),
62 m_dropRect()
63 {
64 setMouseTracking(true);
65 viewport()->setAttribute(Qt::WA_Hover);
66 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
67 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
68 setSelectionBehavior(SelectItems);
69 setSelectionMode(QAbstractItemView::ExtendedSelection);
70 setDragDropMode(QAbstractItemView::DragDrop);
71 setDropIndicatorShown(false);
72 setFocusPolicy(Qt::NoFocus);
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 m_dirLister->setShowingDotFiles(m_view->m_controller->showHiddenFiles());
115 connect(m_dirLister, SIGNAL(newItems(const KFileItemList&)),
116 this, SLOT(generatePreviews(const KFileItemList&)));
117
118 m_dolphinModel = new DolphinModel(this);
119 m_dolphinModel->setDirLister(m_dirLister);
120 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
121
122 m_proxyModel = new DolphinSortFilterProxyModel(this);
123 m_proxyModel->setSourceModel(m_dolphinModel);
124
125 setModel(m_proxyModel);
126
127 m_dirLister->openUrl(url, KDirLister::NoFlags);
128 }
129
130 DolphinColumnWidget::~DolphinColumnWidget()
131 {
132 delete m_dirLister;
133 m_dirLister = 0;
134 }
135
136 void DolphinColumnWidget::setDecorationSize(const QSize& size)
137 {
138 m_viewOptions.decorationSize = size;
139 doItemsLayout();
140 }
141
142 void DolphinColumnWidget::setActive(bool active)
143 {
144 if (m_active == active) {
145 return;
146 }
147
148 m_active = active;
149
150 if (active) {
151 activate();
152 } else {
153 deactivate();
154 }
155 }
156
157 void DolphinColumnWidget::reload()
158 {
159 m_dirLister->stop();
160 m_dirLister->openUrl(m_url, KDirLister::Reload);
161 }
162
163 void DolphinColumnWidget::setShowHiddenFiles(bool show)
164 {
165 if (show != m_dirLister->showingDotFiles()) {
166 m_dirLister->setShowingDotFiles(show);
167 m_dirLister->stop();
168 m_dirLister->openUrl(m_url, KDirLister::Reload);
169 }
170 }
171
172 void DolphinColumnWidget::setShowPreview(bool show)
173 {
174 if (show != m_showPreview) {
175 m_dirLister->stop();
176 m_dirLister->openUrl(m_url, KDirLister::Reload);
177 }
178 }
179
180 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent* event)
181 {
182 if (event->mimeData()->hasUrls()) {
183 event->acceptProposedAction();
184 }
185
186 m_dragging = true;
187 }
188
189 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
190 {
191 QListView::dragLeaveEvent(event);
192
193 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
194 m_dragging = false;
195 setDirtyRegion(m_dropRect);
196 }
197
198 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent* event)
199 {
200 QListView::dragMoveEvent(event);
201
202 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
203 const QModelIndex index = indexAt(event->pos());
204 setDirtyRegion(m_dropRect);
205 m_dropRect = visualRect(index);
206 setDirtyRegion(m_dropRect);
207 }
208
209 void DolphinColumnWidget::dropEvent(QDropEvent* event)
210 {
211 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
212 if (!urls.isEmpty()) {
213 event->acceptProposedAction();
214 m_view->m_controller->indicateDroppedUrls(urls,
215 url(),
216 indexAt(event->pos()),
217 event->source());
218 }
219 QListView::dropEvent(event);
220 m_dragging = false;
221 }
222
223 void DolphinColumnWidget::paintEvent(QPaintEvent* event)
224 {
225 if (!m_childUrl.isEmpty()) {
226 // indicate the shown URL of the next column by highlighting the shown folder item
227 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
228 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
229 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
230 const QRect itemRect = visualRect(proxyIndex);
231 QPainter painter(viewport());
232 painter.save();
233
234 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
235 color.setAlpha(32);
236 painter.setPen(Qt::NoPen);
237 painter.setBrush(color);
238 painter.drawRect(itemRect);
239
240 painter.restore();
241 }
242 }
243
244 QListView::paintEvent(event);
245
246 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
247 if (m_dragging) {
248 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
249 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
250 }
251 }
252
253 void DolphinColumnWidget::mousePressEvent(QMouseEvent* event)
254 {
255 m_view->m_controller->triggerActivation();
256 if (!m_active) {
257 m_view->requestActivation(this);
258 }
259
260 QListView::mousePressEvent(event);
261 }
262
263 void DolphinColumnWidget::keyPressEvent(QKeyEvent* event)
264 {
265 QListView::keyPressEvent(event);
266
267 const QItemSelectionModel* selModel = selectionModel();
268 const QModelIndex currentIndex = selModel->currentIndex();
269 const bool trigger = currentIndex.isValid()
270 && (event->key() == Qt::Key_Return)
271 && (selModel->selectedIndexes().count() <= 1);
272 if (trigger) {
273 triggerItem(currentIndex);
274 }
275 }
276
277 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent* event)
278 {
279 if (!m_active) {
280 m_view->requestActivation(this);
281 }
282
283 QListView::contextMenuEvent(event);
284
285 const QModelIndex index = indexAt(event->pos());
286 if (index.isValid() || m_active) {
287 // Only open a context menu above an item or if the mouse is above
288 // the active column.
289 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
290 m_view->m_controller->triggerContextMenuRequest(pos);
291 }
292 }
293
294 void DolphinColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
295 {
296 QListView::selectionChanged(selected, deselected);
297
298 QItemSelectionModel* selModel = m_view->selectionModel();
299 selModel->select(selected, QItemSelectionModel::Select);
300 selModel->select(deselected, QItemSelectionModel::Deselect);
301 }
302 void DolphinColumnWidget::triggerItem(const QModelIndex& index)
303 {
304 const KFileItem item = m_dolphinModel->itemForIndex(m_proxyModel->mapToSource(index));
305 m_view->m_controller->triggerItem(item);
306 }
307
308 void DolphinColumnWidget::generatePreviews(const KFileItemList& items)
309 {
310 // TODO: same implementation as in DolphinView; create helper class
311 // for generatePreviews(), showPreview() and isCutItem()
312
313 if (m_view->m_controller->showPreview()) {
314 KIO::PreviewJob* job = KIO::filePreview(items, 128);
315 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
316 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
317 }
318 }
319
320 void DolphinColumnWidget::showPreview(const KFileItem& item, const QPixmap& pixmap)
321 {
322 // TODO: same implementation as in DolphinView; create helper class
323 // for generatePreviews(), showPreview() and isCutItem()
324
325 Q_ASSERT(!item.isNull());
326 if (item.url().directory() != m_dirLister->url().path()) {
327 // the preview job is still working on items of an older URL, hence
328 // the item is not part of the directory model anymore
329 return;
330 }
331
332 const QModelIndex idx = m_dolphinModel->indexForItem(item);
333 if (idx.isValid() && (idx.column() == 0)) {
334 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
335 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
336 KIconEffect iconEffect;
337 const QPixmap cutPixmap = iconEffect.apply(pixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
338 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
339 } else {
340 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
341 }
342 }
343 }
344
345 void DolphinColumnWidget::slotEntered(const QModelIndex& index)
346 {
347 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
348 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
349 m_view->m_controller->emitItemEntered(item);
350 }
351
352 void DolphinColumnWidget::activate()
353 {
354 if (m_view->hasFocus()) {
355 setFocus(Qt::OtherFocusReason);
356 }
357 m_view->setFocusProxy(this);
358
359 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
360 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
361 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
362 if (KGlobalSettings::singleClick()) {
363 connect(this, SIGNAL(clicked(const QModelIndex&)),
364 this, SLOT(triggerItem(const QModelIndex&)));
365 } else {
366 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
367 this, SLOT(triggerItem(const QModelIndex&)));
368 }
369
370 const QColor bgColor = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
371 QPalette palette = viewport()->palette();
372 palette.setColor(viewport()->backgroundRole(), bgColor);
373 viewport()->setPalette(palette);
374
375 if (!m_childUrl.isEmpty()) {
376 // assure that the current index is set on the index that represents
377 // the child URL
378 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
379 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
380 selectionModel()->setCurrentIndex(proxyIndex, QItemSelectionModel::Current);
381 }
382
383 update();
384 }
385
386 void DolphinColumnWidget::deactivate()
387 {
388 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
389 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
390 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
391 if (KGlobalSettings::singleClick()) {
392 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
393 this, SLOT(triggerItem(const QModelIndex&)));
394 } else {
395 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
396 this, SLOT(triggerItem(const QModelIndex&)));
397 }
398
399 const QPalette palette = m_view->viewport()->palette();
400 viewport()->setPalette(palette);
401
402 selectionModel()->clear();
403 update();
404 }
405
406 bool DolphinColumnWidget::isCutItem(const KFileItem& item) const
407 {
408 // TODO: same implementation as in DolphinView; create helper class
409 // for generatePreviews(), showPreview() and isCutItem()
410
411 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
412 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
413
414 const KUrl& itemUrl = item.url();
415 KUrl::List::const_iterator it = cutUrls.begin();
416 const KUrl::List::const_iterator end = cutUrls.end();
417 while (it != end) {
418 if (*it == itemUrl) {
419 return true;
420 }
421 ++it;
422 }
423
424 return false;
425 }
426
427 #include "dolphincolumnwidget.moc"