]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
* Install an event-filter for the view implementations. Whenever a view implementatio...
[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 "dolphin_generalsettings.h"
30 #include "draganddrophelper.h"
31 #include "selectionmanager.h"
32
33 #include <kcolorscheme.h>
34 #include <kdirlister.h>
35 #include <kfileitem.h>
36 #include <kio/previewjob.h>
37 #include <kiconeffect.h>
38 #include <kjob.h>
39 #include <kmimetyperesolver.h>
40 #include <konqmimedata.h>
41
42 #include "iconmanager.h"
43
44 #include <QApplication>
45 #include <QClipboard>
46 #include <QPainter>
47 #include <QPoint>
48
49 DolphinColumnWidget::DolphinColumnWidget(QWidget* parent,
50 DolphinColumnView* columnView,
51 const KUrl& url) :
52 QListView(parent),
53 m_active(true),
54 m_view(columnView),
55 m_url(url),
56 m_childUrl(),
57 m_font(),
58 m_decorationSize(),
59 m_dirLister(0),
60 m_dolphinModel(0),
61 m_proxyModel(0),
62 m_iconManager(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::ScrollBarAsNeeded);
70 setSelectionBehavior(SelectItems);
71 setSelectionMode(QAbstractItemView::ExtendedSelection);
72 setDragDropMode(QAbstractItemView::DragDrop);
73 setDropIndicatorShown(false);
74 setSelectionRectVisible(true);
75
76 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
77 // check avoids a division by zero happening on versions before 4.3.1.
78 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
79 // ereslibre
80 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
81 setVerticalScrollMode(QListView::ScrollPerPixel);
82 setHorizontalScrollMode(QListView::ScrollPerPixel);
83 #endif
84
85 // apply the column mode settings to the widget
86 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
87 Q_ASSERT(settings != 0);
88
89 if (settings->useSystemFont()) {
90 m_font = KGlobalSettings::generalFont();
91 } else {
92 m_font = QFont(settings->fontFamily(),
93 settings->fontSize(),
94 settings->fontWeight(),
95 settings->italicFont());
96 }
97
98 const int iconSize = settings->iconSize();
99 setDecorationSize(QSize(iconSize, iconSize));
100
101 KFileItemDelegate* delegate = new KFileItemDelegate(this);
102 setItemDelegate(delegate);
103
104 activate();
105
106 connect(this, SIGNAL(viewportEntered()),
107 m_view->m_controller, SLOT(emitViewportEntered()));
108 connect(this, SIGNAL(entered(const QModelIndex&)),
109 this, SLOT(slotEntered(const QModelIndex&)));
110
111 //m_dirLister = new DolphinDirLister(); TODO
112 m_dirLister = new KDirLister();
113 m_dirLister->setAutoUpdate(true);
114 m_dirLister->setMainWindow(window());
115 m_dirLister->setDelayedMimeTypes(true);
116 const bool showHiddenFiles = m_view->m_controller->dolphinView()->showHiddenFiles();
117 m_dirLister->setShowingDotFiles(showHiddenFiles);
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 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
126 const DolphinView* dolphinView = m_view->m_controller->dolphinView();
127 m_proxyModel->setSorting(dolphinView->sorting());
128 m_proxyModel->setSortOrder(dolphinView->sortOrder());
129
130 setModel(m_proxyModel);
131 const bool useSelManager = KGlobalSettings::singleClick() &&
132 DolphinSettings::instance().generalSettings()->showSelectionToggle();
133 if (useSelManager) {
134 SelectionManager* selManager = new SelectionManager(this);
135 connect(selManager, SIGNAL(selectionChanged()),
136 this, SLOT(requestActivation()));
137 connect(m_view->m_controller, SIGNAL(urlChanged(const KUrl&)),
138 selManager, SLOT(reset()));
139 }
140
141 new KMimeTypeResolver(this, m_dolphinModel);
142 m_iconManager = new IconManager(this, m_proxyModel);
143 m_iconManager->setShowPreview(m_view->m_controller->dolphinView()->showPreview());
144
145 m_dirLister->openUrl(url, KDirLister::NoFlags);
146
147 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
148 this, SLOT(updateFont()));
149 }
150
151 DolphinColumnWidget::~DolphinColumnWidget()
152 {
153 delete m_proxyModel;
154 m_proxyModel = 0;
155 delete m_dolphinModel;
156 m_dolphinModel = 0;
157 m_dirLister = 0; // deleted by m_dolphinModel
158 }
159
160 void DolphinColumnWidget::setDecorationSize(const QSize& size)
161 {
162 setIconSize(size);
163 m_decorationSize = size;
164 doItemsLayout();
165 }
166
167 void DolphinColumnWidget::setActive(bool active)
168 {
169 if (m_active == active) {
170 return;
171 }
172
173 m_active = active;
174
175 if (active) {
176 activate();
177 } else {
178 deactivate();
179 }
180 }
181
182 void DolphinColumnWidget::reload()
183 {
184 m_dirLister->stop();
185 m_dirLister->openUrl(m_url, KDirLister::Reload);
186 }
187
188 void DolphinColumnWidget::setSorting(DolphinView::Sorting sorting)
189 {
190 m_proxyModel->setSorting(sorting);
191 }
192
193 void DolphinColumnWidget::setSortOrder(Qt::SortOrder order)
194 {
195 m_proxyModel->setSortOrder(order);
196 }
197
198 void DolphinColumnWidget::setShowHiddenFiles(bool show)
199 {
200 if (show != m_dirLister->showingDotFiles()) {
201 m_dirLister->setShowingDotFiles(show);
202 m_dirLister->stop();
203 m_dirLister->openUrl(m_url, KDirLister::Reload);
204 }
205 }
206
207 void DolphinColumnWidget::setShowPreview(bool show)
208 {
209 m_iconManager->setShowPreview(show);
210
211 m_dirLister->stop();
212 m_dirLister->openUrl(m_url, KDirLister::Reload);
213 }
214
215 void DolphinColumnWidget::updateBackground()
216 {
217 // TODO: The alpha-value 150 is copied from DolphinView::setActive(). When
218 // cleaning up the cut-indication of DolphinColumnWidget with the code from
219 // DolphinView a common helper-class should be available which can be shared
220 // by all view implementations -> no hardcoded value anymore
221 const QPalette::ColorRole role = viewport()->backgroundRole();
222 QColor color = viewport()->palette().color(role);
223 color.setAlpha((m_active && m_view->m_active) ? 255 : 150);
224
225 QPalette palette = viewport()->palette();
226 palette.setColor(role, color);
227 viewport()->setPalette(palette);
228
229 update();
230 }
231
232 void DolphinColumnWidget::setNameFilter(const QString& nameFilter)
233 {
234 m_proxyModel->setFilterRegExp(nameFilter);
235 }
236
237
238 QStyleOptionViewItem DolphinColumnWidget::viewOptions() const
239 {
240 QStyleOptionViewItem viewOptions = QListView::viewOptions();
241 viewOptions.font = m_font;
242 viewOptions.decorationSize = m_decorationSize;
243 viewOptions.showDecorationSelected = true;
244 return viewOptions;
245 }
246
247 void DolphinColumnWidget::startDrag(Qt::DropActions supportedActions)
248 {
249 DragAndDropHelper::startDrag(this, supportedActions);
250 }
251
252 void DolphinColumnWidget::dragEnterEvent(QDragEnterEvent* event)
253 {
254 if (event->mimeData()->hasUrls()) {
255 event->acceptProposedAction();
256 }
257
258 m_dragging = true;
259 }
260
261 void DolphinColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
262 {
263 QListView::dragLeaveEvent(event);
264
265 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
266 m_dragging = false;
267 setDirtyRegion(m_dropRect);
268 }
269
270 void DolphinColumnWidget::dragMoveEvent(QDragMoveEvent* event)
271 {
272 QListView::dragMoveEvent(event);
273
274 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
275 const QModelIndex index = indexAt(event->pos());
276 setDirtyRegion(m_dropRect);
277
278 m_dropRect.setSize(QSize()); // set as invalid
279 if (index.isValid()) {
280 m_view->m_controller->setItemView(this);
281 const KFileItem item = m_view->m_controller->itemForIndex(index);
282 if (!item.isNull() && item.isDir()) {
283 m_dropRect = visualRect(index);
284 }
285 }
286 setDirtyRegion(m_dropRect);
287
288 if (event->mimeData()->hasUrls()) {
289 // accept url drops, independently from the destination item
290 event->acceptProposedAction();
291 }
292 }
293
294 void DolphinColumnWidget::dropEvent(QDropEvent* event)
295 {
296 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
297 if (!urls.isEmpty()) {
298 const QModelIndex index = indexAt(event->pos());
299 m_view->m_controller->setItemView(this);
300 const KFileItem item = m_view->m_controller->itemForIndex(index);
301 m_view->m_controller->indicateDroppedUrls(urls,
302 url(),
303 item);
304 event->acceptProposedAction();
305 }
306 QListView::dropEvent(event);
307 m_dragging = false;
308 }
309
310 void DolphinColumnWidget::paintEvent(QPaintEvent* event)
311 {
312 if (!m_childUrl.isEmpty()) {
313 // indicate the shown URL of the next column by highlighting the shown folder item
314 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
315 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
316 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
317 const QRect itemRect = visualRect(proxyIndex);
318 QPainter painter(viewport());
319 painter.save();
320
321 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
322 color.setAlpha(32);
323 painter.setPen(Qt::NoPen);
324 painter.setBrush(color);
325 painter.drawRect(itemRect);
326
327 painter.restore();
328 }
329 }
330
331 QListView::paintEvent(event);
332
333 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
334 if (m_dragging) {
335 const QBrush& brush = viewOptions().palette.brush(QPalette::Normal, QPalette::Highlight);
336 DragAndDropHelper::drawHoverIndication(this, m_dropRect, brush);
337 }
338 }
339
340 void DolphinColumnWidget::mousePressEvent(QMouseEvent* event)
341 {
342 requestActivation();
343 QListView::mousePressEvent(event);
344 }
345
346 void DolphinColumnWidget::keyPressEvent(QKeyEvent* event)
347 {
348 QListView::keyPressEvent(event);
349 requestActivation();
350 m_view->m_controller->handleKeyPressEvent(event);
351 }
352
353 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent* event)
354 {
355 if (!m_active) {
356 m_view->requestActivation(this);
357 Q_ASSERT(m_view->m_controller->itemView() == this);
358 m_view->m_controller->triggerUrlChangeRequest(m_url);
359 }
360
361 QListView::contextMenuEvent(event);
362
363 const QModelIndex index = indexAt(event->pos());
364 if (index.isValid() || m_active) {
365 // Only open a context menu above an item or if the mouse is above
366 // the active column.
367 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
368 Q_ASSERT(m_view->m_controller->itemView() == this);
369 m_view->m_controller->triggerContextMenuRequest(pos);
370 }
371 }
372
373 void DolphinColumnWidget::wheelEvent(QWheelEvent* event)
374 {
375 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
376 // (installing an event filter does not work, as the wheel event is handled first)
377 if (event->modifiers() & Qt::ControlModifier) {
378 event->ignore();
379 return;
380 }
381 QListView::wheelEvent(event);
382 }
383
384 void DolphinColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
385 {
386 QListView::selectionChanged(selected, deselected);
387
388 QItemSelectionModel* selModel = m_view->selectionModel();
389 selModel->select(selected, QItemSelectionModel::Select);
390 selModel->select(deselected, QItemSelectionModel::Deselect);
391 }
392
393 void DolphinColumnWidget::slotEntered(const QModelIndex& index)
394 {
395 m_view->m_controller->setItemView(this);
396 m_view->m_controller->emitItemEntered(index);
397 }
398
399 void DolphinColumnWidget::requestActivation()
400 {
401 m_view->m_controller->requestActivation();
402 if (!m_active) {
403 m_view->requestActivation(this);
404 m_view->m_controller->triggerUrlChangeRequest(m_url);
405 selectionModel()->clear();
406 }
407 Q_ASSERT(m_view->m_controller->itemView() == this);
408 }
409
410 void DolphinColumnWidget::updateFont()
411 {
412 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
413 Q_ASSERT(settings != 0);
414
415 if (settings->useSystemFont()) {
416 m_font = KGlobalSettings::generalFont();
417 }
418 }
419
420 void DolphinColumnWidget::activate()
421 {
422 setFocus(Qt::OtherFocusReason);
423
424 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
425 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
426 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
427 if (KGlobalSettings::singleClick()) {
428 connect(this, SIGNAL(clicked(const QModelIndex&)),
429 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
430 } else {
431 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
432 m_view->m_controller, SLOT(triggerItem(const QModelIndex&)));
433 }
434
435 if (selectionModel() && selectionModel()->currentIndex().isValid())
436 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
437
438 updateBackground();
439 }
440
441 void DolphinColumnWidget::deactivate()
442 {
443 clearFocus();
444
445 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
446 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
447 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
448 if (KGlobalSettings::singleClick()) {
449 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
450 this, SLOT(triggerItem(const QModelIndex&)));
451 } else {
452 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
453 this, SLOT(triggerItem(const QModelIndex&)));
454 }
455
456 const QModelIndex current = selectionModel()->currentIndex();
457 selectionModel()->clear();
458 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
459 updateBackground();
460 }
461
462 #include "dolphincolumnwidget.moc"