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