]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
Fix for the scroll+click column bug
[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 "iconmanager.h"
41
42 #include <QApplication>
43 #include <QClipboard>
44 #include <QPainter>
45 #include <QPoint>
46
47 DolphinColumnWidget::DolphinColumnWidget(QWidget* parent,
48 DolphinColumnView* columnView,
49 const KUrl& url) :
50 QListView(parent),
51 m_active(true),
52 m_view(columnView),
53 m_url(url),
54 m_childUrl(),
55 m_font(),
56 m_decorationSize(),
57 m_dirLister(0),
58 m_dolphinModel(0),
59 m_proxyModel(0),
60 m_iconManager(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 setSelectionRectVisible(true);
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_font = QFont(settings->fontFamily(), settings->fontSize());
88 m_font.setItalic(settings->italicFont());
89 m_font.setBold(settings->boldFont());
90
91 const int iconSize = settings->iconSize();
92 m_decorationSize = QSize(iconSize, iconSize);
93
94 KFileItemDelegate* delegate = new KFileItemDelegate(this);
95 setItemDelegate(delegate);
96
97 activate();
98
99 connect(this, SIGNAL(viewportEntered()),
100 m_view->m_controller, SLOT(emitViewportEntered()));
101 connect(this, SIGNAL(entered(const QModelIndex&)),
102 this, SLOT(slotEntered(const QModelIndex&)));
103
104 //m_dirLister = new DolphinDirLister(); TODO
105 m_dirLister = new KDirLister();
106 m_dirLister->setAutoUpdate(true);
107 m_dirLister->setMainWindow(this);
108 m_dirLister->setDelayedMimeTypes(true);
109 const bool showHiddenFiles = m_view->m_controller->dolphinView()->showHiddenFiles();
110 m_dirLister->setShowingDotFiles(showHiddenFiles);
111
112 m_dolphinModel = new DolphinModel(this);
113 m_dolphinModel->setDirLister(m_dirLister);
114 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
115
116 m_proxyModel = new DolphinSortFilterProxyModel(this);
117 m_proxyModel->setSourceModel(m_dolphinModel);
118 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
119
120 setModel(m_proxyModel);
121 new KMimeTypeResolver(this, m_dolphinModel);
122 m_iconManager = new IconManager(this, m_proxyModel);
123 m_iconManager->setShowPreview(m_view->m_controller->dolphinView()->showPreview());
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 kDebug() << "-------------- column widget: show" << show;
176 m_iconManager->setShowPreview(show);
177
178 m_dirLister->stop();
179 m_dirLister->openUrl(m_url, KDirLister::Reload);
180 }
181
182 void DolphinColumnWidget::updateBackground()
183 {
184 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
185 // cleaning up the cut-indication of DolphinColumnWidget with the code from
186 // DolphinView a common helper-class should be available which can be shared
187 // by all view implementations -> no hardcoded value anymore
188 const QPalette::ColorRole role = viewport()->backgroundRole();
189 QColor color = viewport()->palette().color(role);
190 color.setAlpha((m_active && m_view->m_active) ? 255 : 150);
191
192 QPalette palette = viewport()->palette();
193 palette.setColor(role, color);
194 viewport()->setPalette(palette);
195
196 update();
197 }
198
199 void DolphinColumnWidget::setNameFilter(const QString& nameFilter)
200 {
201 m_proxyModel->setFilterRegExp(nameFilter);
202 }
203
204
205 QStyleOptionViewItem DolphinColumnWidget::viewOptions() const
206 {
207 QStyleOptionViewItem viewOptions = QListView::viewOptions();
208 viewOptions.font = m_font;
209 viewOptions.decorationSize = m_decorationSize;
210 viewOptions.showDecorationSelected = true;
211 return viewOptions;
212 }
213
214 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions)
215 {
216 DragAndDropHelper::startDrag(this, supportedActions);
217 }
218
219 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent* event)
220 {
221 if (event->mimeData()->hasUrls()) {
222 event->acceptProposedAction();
223 }
224
225 m_dragging = true;
226 }
227
228 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
229 {
230 QListView::dragLeaveEvent(event);
231
232 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
233 m_dragging = false;
234 setDirtyRegion(m_dropRect);
235 }
236
237 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent* event)
238 {
239 QListView::dragMoveEvent(event);
240
241 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
242 const QModelIndex index = indexAt(event->pos());
243 setDirtyRegion(m_dropRect);
244
245 m_dropRect.setSize(QSize()); // set as invalid
246 if (index.isValid()) {
247 const KFileItem item = itemForIndex(index);
248 if (!item.isNull() && item.isDir()) {
249 m_dropRect = visualRect(index);
250 }
251 }
252 setDirtyRegion(m_dropRect);
253
254 if (event->mimeData()->hasUrls()) {
255 // accept url drops, independently from the destination item
256 event->acceptProposedAction();
257 }
258 }
259
260 void DolphinColumnWidget::dropEvent(QDropEvent* event)
261 {
262 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
263 if (!urls.isEmpty()) {
264 const QModelIndex index = indexAt(event->pos());
265 const KFileItem item = itemForIndex(index);
266 m_view->m_controller->indicateDroppedUrls(urls,
267 url(),
268 item);
269 event->acceptProposedAction();
270 }
271 QListView::dropEvent(event);
272 m_dragging = false;
273 }
274
275 void DolphinColumnWidget::paintEvent(QPaintEvent* event)
276 {
277 if (!m_childUrl.isEmpty()) {
278 // indicate the shown URL of the next column by highlighting the shown folder item
279 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
280 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
281 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
282 const QRect itemRect = visualRect(proxyIndex);
283 QPainter painter(viewport());
284 painter.save();
285
286 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
287 color.setAlpha(32);
288 painter.setPen(Qt::NoPen);
289 painter.setBrush(color);
290 painter.drawRect(itemRect);
291
292 painter.restore();
293 }
294 }
295
296 QListView::paintEvent(event);
297
298 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
299 if (m_dragging) {
300 const QBrush& brush = viewOptions().palette.brush(QPalette::Normal, QPalette::Highlight);
301 DragAndDropHelper::drawHoverIndication(this, m_dropRect, brush);
302 }
303 }
304
305 void DolphinColumnWidget::mousePressEvent(QMouseEvent* event)
306 {
307 m_view->m_controller->requestActivation();
308 if (!m_active) {
309 m_view->requestActivation(this);
310 m_view->m_controller->triggerUrlChangeRequest(m_url);
311 }
312
313 QListView::mousePressEvent(event);
314 }
315
316 void DolphinColumnWidget::keyPressEvent(QKeyEvent* event)
317 {
318 QListView::keyPressEvent(event);
319
320 const QItemSelectionModel* selModel = selectionModel();
321 const QModelIndex currentIndex = selModel->currentIndex();
322 const bool trigger = currentIndex.isValid()
323 && (event->key() == Qt::Key_Return)
324 && (selModel->selectedIndexes().count() <= 1);
325 if (trigger) {
326 triggerItem(currentIndex);
327 }
328 }
329
330 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent* event)
331 {
332 if (!m_active) {
333 m_view->requestActivation(this);
334 m_view->m_controller->triggerUrlChangeRequest(m_url);
335 }
336
337 QListView::contextMenuEvent(event);
338
339 const QModelIndex index = indexAt(event->pos());
340 if (index.isValid() || m_active) {
341 // Only open a context menu above an item or if the mouse is above
342 // the active column.
343 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
344 m_view->m_controller->triggerContextMenuRequest(pos);
345 }
346 }
347
348 void DolphinColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
349 {
350 QListView::selectionChanged(selected, deselected);
351
352 QItemSelectionModel* selModel = m_view->selectionModel();
353 selModel->select(selected, QItemSelectionModel::Select);
354 selModel->select(deselected, QItemSelectionModel::Deselect);
355 }
356
357 void DolphinColumnWidget::triggerItem(const QModelIndex& index)
358 {
359 const KFileItem item = itemForIndex(index);
360 m_view->m_controller->triggerItem(item);
361 }
362
363 void DolphinColumnWidget::slotEntered(const QModelIndex& index)
364 {
365 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
366 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
367 m_view->m_controller->emitItemEntered(item);
368 }
369
370 void DolphinColumnWidget::activate()
371 {
372 setFocus(Qt::OtherFocusReason);
373
374 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
375 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
376 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
377 if (KGlobalSettings::singleClick()) {
378 connect(this, SIGNAL(clicked(const QModelIndex&)),
379 this, SLOT(triggerItem(const QModelIndex&)));
380 } else {
381 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
382 this, SLOT(triggerItem(const QModelIndex&)));
383 }
384
385 updateBackground();
386 }
387
388 void DolphinColumnWidget::deactivate()
389 {
390 clearFocus();
391
392 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
393 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
394 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
395 if (KGlobalSettings::singleClick()) {
396 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
397 this, SLOT(triggerItem(const QModelIndex&)));
398 } else {
399 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
400 this, SLOT(triggerItem(const QModelIndex&)));
401 }
402
403 selectionModel()->clear();
404 updateBackground();
405 }
406
407 KFileItem DolphinColumnWidget::itemForIndex(const QModelIndex& index) const
408 {
409 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
410 return m_dolphinModel->itemForIndex(dirIndex);
411 }
412
413
414 #include "dolphincolumnwidget.moc"