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