]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincolumnwidget.cpp
No longer use Obsolete Member for QWidget:
[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 const KFileItem item = itemForIndex(index);
281 if (!item.isNull() && item.isDir()) {
282 m_dropRect = visualRect(index);
283 }
284 }
285 setDirtyRegion(m_dropRect);
286
287 if (event->mimeData()->hasUrls()) {
288 // accept url drops, independently from the destination item
289 event->acceptProposedAction();
290 }
291 }
292
293 void DolphinColumnWidget::dropEvent(QDropEvent* event)
294 {
295 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
296 if (!urls.isEmpty()) {
297 const QModelIndex index = indexAt(event->pos());
298 const KFileItem item = itemForIndex(index);
299 m_view->m_controller->indicateDroppedUrls(urls,
300 url(),
301 item);
302 event->acceptProposedAction();
303 }
304 QListView::dropEvent(event);
305 m_dragging = false;
306 }
307
308 void DolphinColumnWidget::paintEvent(QPaintEvent* event)
309 {
310 if (!m_childUrl.isEmpty()) {
311 // indicate the shown URL of the next column by highlighting the shown folder item
312 const QModelIndex dirIndex = m_dolphinModel->indexForUrl(m_childUrl);
313 const QModelIndex proxyIndex = m_proxyModel->mapFromSource(dirIndex);
314 if (proxyIndex.isValid() && !selectionModel()->isSelected(proxyIndex)) {
315 const QRect itemRect = visualRect(proxyIndex);
316 QPainter painter(viewport());
317 painter.save();
318
319 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).foreground().color();
320 color.setAlpha(32);
321 painter.setPen(Qt::NoPen);
322 painter.setBrush(color);
323 painter.drawRect(itemRect);
324
325 painter.restore();
326 }
327 }
328
329 QListView::paintEvent(event);
330
331 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
332 if (m_dragging) {
333 const QBrush& brush = viewOptions().palette.brush(QPalette::Normal, QPalette::Highlight);
334 DragAndDropHelper::drawHoverIndication(this, m_dropRect, brush);
335 }
336 }
337
338 void DolphinColumnWidget::mousePressEvent(QMouseEvent* event)
339 {
340 requestActivation();
341 QListView::mousePressEvent(event);
342 }
343
344 void DolphinColumnWidget::keyPressEvent(QKeyEvent* event)
345 {
346 QListView::keyPressEvent(event);
347
348 const QItemSelectionModel* selModel = selectionModel();
349 const QModelIndex currentIndex = selModel->currentIndex();
350 const bool trigger = currentIndex.isValid()
351 && (event->key() == Qt::Key_Return)
352 && (selModel->selectedIndexes().count() <= 1);
353 if (trigger) {
354 triggerItem(currentIndex);
355 }
356 }
357
358 void DolphinColumnWidget::contextMenuEvent(QContextMenuEvent* event)
359 {
360 if (!m_active) {
361 m_view->requestActivation(this);
362 m_view->m_controller->triggerUrlChangeRequest(m_url);
363 }
364
365 QListView::contextMenuEvent(event);
366
367 const QModelIndex index = indexAt(event->pos());
368 if (index.isValid() || m_active) {
369 // Only open a context menu above an item or if the mouse is above
370 // the active column.
371 const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
372 m_view->m_controller->triggerContextMenuRequest(pos);
373 }
374 }
375
376 void DolphinColumnWidget::wheelEvent(QWheelEvent* event)
377 {
378 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
379 if ((event->modifiers() & Qt::ControlModifier) == Qt::ControlModifier) {
380 event->ignore();
381 return;
382 }
383 QListView::wheelEvent(event);
384 }
385
386 void DolphinColumnWidget::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
387 {
388 QListView::selectionChanged(selected, deselected);
389
390 QItemSelectionModel* selModel = m_view->selectionModel();
391 selModel->select(selected, QItemSelectionModel::Select);
392 selModel->select(deselected, QItemSelectionModel::Deselect);
393 }
394
395 void DolphinColumnWidget::triggerItem(const QModelIndex& index)
396 {
397 const KFileItem item = itemForIndex(index);
398 m_view->m_controller->triggerItem(item);
399 }
400
401 void DolphinColumnWidget::slotEntered(const QModelIndex& index)
402 {
403 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
404 const KFileItem item = m_dolphinModel->itemForIndex(dirIndex);
405 m_view->m_controller->emitItemEntered(item);
406 }
407
408 void DolphinColumnWidget::requestActivation()
409 {
410 m_view->m_controller->requestActivation();
411 if (!m_active) {
412 m_view->requestActivation(this);
413 m_view->m_controller->triggerUrlChangeRequest(m_url);
414 selectionModel()->clear();
415 }
416 }
417
418 void DolphinColumnWidget::updateFont()
419 {
420 const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
421 Q_ASSERT(settings != 0);
422
423 if (settings->useSystemFont()) {
424 m_font = KGlobalSettings::generalFont();
425 }
426 }
427
428 void DolphinColumnWidget::activate()
429 {
430 setFocus(Qt::OtherFocusReason);
431
432 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
433 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
434 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
435 if (KGlobalSettings::singleClick()) {
436 connect(this, SIGNAL(clicked(const QModelIndex&)),
437 this, SLOT(triggerItem(const QModelIndex&)));
438 } else {
439 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
440 this, SLOT(triggerItem(const QModelIndex&)));
441 }
442
443 if (selectionModel() && selectionModel()->currentIndex().isValid())
444 selectionModel()->setCurrentIndex(selectionModel()->currentIndex(), QItemSelectionModel::SelectCurrent);
445
446 updateBackground();
447 }
448
449 void DolphinColumnWidget::deactivate()
450 {
451 clearFocus();
452
453 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
454 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
455 // necessary connecting the signal 'singleClick()' or 'doubleClick'.
456 if (KGlobalSettings::singleClick()) {
457 disconnect(this, SIGNAL(clicked(const QModelIndex&)),
458 this, SLOT(triggerItem(const QModelIndex&)));
459 } else {
460 disconnect(this, SIGNAL(doubleClicked(const QModelIndex&)),
461 this, SLOT(triggerItem(const QModelIndex&)));
462 }
463
464 const QModelIndex current = selectionModel()->currentIndex();
465 selectionModel()->clear();
466 selectionModel()->setCurrentIndex(current, QItemSelectionModel::NoUpdate);
467 updateBackground();
468 }
469
470 KFileItem DolphinColumnWidget::itemForIndex(const QModelIndex& index) const
471 {
472 const QModelIndex dirIndex = m_proxyModel->mapToSource(index);
473 return m_dolphinModel->itemForIndex(dirIndex);
474 }
475
476
477 #include "dolphincolumnwidget.moc"