]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
fixed issue that the "Ask for confirmation when moving files or folders to trash...
[dolphin.git] / src / dolphindetailsview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphindetailsview.h"
22
23 #include "dolphinmodel.h"
24 #include "dolphincontroller.h"
25 #include "dolphinsettings.h"
26 #include "dolphinsortfilterproxymodel.h"
27 #include "viewproperties.h"
28
29 #include "dolphin_detailsmodesettings.h"
30
31 #include <kdirmodel.h>
32 #include <klocale.h>
33 #include <kmenu.h>
34
35 #include <QAbstractProxyModel>
36 #include <QAction>
37 #include <QApplication>
38 #include <QHeaderView>
39 #include <QRubberBand>
40 #include <QPainter>
41 #include <QScrollBar>
42
43 DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) :
44 QTreeView(parent),
45 m_controller(controller),
46 m_dragging(false),
47 m_showElasticBand(false),
48 m_elasticBandOrigin(),
49 m_elasticBandDestination()
50 {
51 Q_ASSERT(controller != 0);
52
53 setAcceptDrops(true);
54 setRootIsDecorated(false);
55 setSortingEnabled(true);
56 setUniformRowHeights(true);
57 setSelectionBehavior(SelectItems);
58 setDragDropMode(QAbstractItemView::DragDrop);
59 setDropIndicatorShown(false);
60 setAlternatingRowColors(true);
61
62 setMouseTracking(true);
63 viewport()->setAttribute(Qt::WA_Hover);
64
65 const ViewProperties props(controller->url());
66 setSortIndicatorSection(props.sorting());
67 setSortIndicatorOrder(props.sortOrder());
68
69 QHeaderView* headerView = header();
70 connect(headerView, SIGNAL(sectionClicked(int)),
71 this, SLOT(synchronizeSortingState(int)));
72 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
73 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
74 this, SLOT(configureColumns(const QPoint&)));
75
76 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
77 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
78 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
79 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
80
81 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
82 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
83 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
84 // RETURN-key in keyPressEvent().
85 if (KGlobalSettings::singleClick()) {
86 connect(this, SIGNAL(clicked(const QModelIndex&)),
87 this, SLOT(triggerItem(const QModelIndex&)));
88 } else {
89 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
90 this, SLOT(triggerItem(const QModelIndex&)));
91 }
92 connect(this, SIGNAL(entered(const QModelIndex&)),
93 this, SLOT(slotEntered(const QModelIndex&)));
94 connect(this, SIGNAL(viewportEntered()),
95 controller, SLOT(emitViewportEntered()));
96 connect(controller, SIGNAL(zoomIn()),
97 this, SLOT(zoomIn()));
98 connect(controller, SIGNAL(zoomOut()),
99 this, SLOT(zoomOut()));
100 connect(controller->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
101 this, SLOT(updateColumnVisibility()));
102
103 // apply the details mode settings to the widget
104 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
105 Q_ASSERT(settings != 0);
106
107 m_viewOptions = QTreeView::viewOptions();
108
109 QFont font(settings->fontFamily(), settings->fontSize());
110 font.setItalic(settings->italicFont());
111 font.setBold(settings->boldFont());
112 m_viewOptions.font = font;
113 m_viewOptions.showDecorationSelected = true;
114
115 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
116 // check avoids a division by zero happening on versions before 4.3.1.
117 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
118 // ereslibre
119 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
120 setVerticalScrollMode(QTreeView::ScrollPerPixel);
121 setHorizontalScrollMode(QTreeView::ScrollPerPixel);
122 #endif
123
124 updateDecorationSize();
125
126 setFocus();
127 }
128
129 DolphinDetailsView::~DolphinDetailsView()
130 {
131 }
132
133 bool DolphinDetailsView::event(QEvent* event)
134 {
135 if (event->type() == QEvent::Polish) {
136 // Assure that by respecting the available width that:
137 // - the 'Name' column is stretched as large as possible
138 // - the remaining columns are as small as possible
139 QHeaderView* headerView = header();
140 headerView->setStretchLastSection(false);
141 headerView->setResizeMode(QHeaderView::ResizeToContents);
142 headerView->setResizeMode(0, QHeaderView::Stretch);
143 headerView->setMovable(false);
144
145 updateColumnVisibility();
146
147 hideColumn(DolphinModel::Rating);
148 hideColumn(DolphinModel::Tags);
149 }
150 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
151 // check avoids a division by zero happening on versions before 4.3.1.
152 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
153 // ereslibre
154 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
155 else if (event->type() == QEvent::UpdateRequest) {
156 // a wheel movement will scroll 4 items
157 if (model()->rowCount() > 0) {
158 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
159 }
160 }
161 #endif
162
163 return QTreeView::event(event);
164 }
165
166 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
167 {
168 return m_viewOptions;
169 }
170
171 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
172 {
173 QTreeView::contextMenuEvent(event);
174 m_controller->triggerContextMenuRequest(event->pos());
175 }
176
177 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
178 {
179 m_controller->requestActivation();
180
181 QTreeView::mousePressEvent(event);
182
183 const QModelIndex index = indexAt(event->pos());
184 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
185 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
186 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
187 clearSelection();
188 }
189 }
190
191 if (event->button() == Qt::LeftButton) {
192 m_showElasticBand = true;
193
194 const QPoint pos(contentsPos());
195 m_elasticBandOrigin = event->pos();
196 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
197 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
198 m_elasticBandDestination = event->pos();
199 }
200 }
201
202 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
203 {
204 QTreeView::mouseMoveEvent(event);
205 if (m_showElasticBand) {
206 updateElasticBand();
207 }
208 }
209
210 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
211 {
212 QTreeView::mouseReleaseEvent(event);
213 if (m_showElasticBand) {
214 updateElasticBand();
215 m_showElasticBand = false;
216 }
217 }
218
219 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
220 {
221 if (event->mimeData()->hasUrls()) {
222 event->acceptProposedAction();
223 }
224
225 if (m_showElasticBand) {
226 updateElasticBand();
227 m_showElasticBand = false;
228 }
229 m_dragging = true;
230 }
231
232 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
233 {
234 QTreeView::dragLeaveEvent(event);
235
236 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
237 m_dragging = false;
238 setDirtyRegion(m_dropRect);
239 }
240
241 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
242 {
243 QTreeView::dragMoveEvent(event);
244
245 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
246 setDirtyRegion(m_dropRect);
247 const QModelIndex index = indexAt(event->pos());
248 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
249 m_dragging = false;
250 } else {
251 m_dragging = true;
252 if (itemForIndex(index).isDir()) {
253 m_dropRect = visualRect(index);
254 } else {
255 m_dropRect.setSize(QSize()); // set as invalid
256 }
257 setDirtyRegion(m_dropRect);
258 }
259 }
260
261 void DolphinDetailsView::dropEvent(QDropEvent* event)
262 {
263 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
264 if (!urls.isEmpty()) {
265 event->acceptProposedAction();
266 const QModelIndex index = indexAt(event->pos());
267 KFileItem item;
268 if (index.isValid() && (index.column() == DolphinModel::Name)) {
269 item = itemForIndex(index);
270 }
271 m_controller->indicateDroppedUrls(urls,
272 m_controller->url(),
273 item);
274 }
275 QTreeView::dropEvent(event);
276 m_dragging = false;
277 }
278
279 void DolphinDetailsView::paintEvent(QPaintEvent* event)
280 {
281 QTreeView::paintEvent(event);
282 if (m_showElasticBand) {
283 // The following code has been taken from QListView
284 // and adapted to DolphinDetailsView.
285 // (C) 1992-2007 Trolltech ASA
286 QStyleOptionRubberBand opt;
287 opt.initFrom(this);
288 opt.shape = QRubberBand::Rectangle;
289 opt.opaque = false;
290 opt.rect = elasticBandRect();
291
292 QPainter painter(viewport());
293 painter.save();
294 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
295 painter.restore();
296 }
297
298 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
299 if (m_dragging) {
300 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
301 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
302 }
303 }
304
305 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
306 {
307 QTreeView::keyPressEvent(event);
308
309 const QItemSelectionModel* selModel = selectionModel();
310 const QModelIndex currentIndex = selModel->currentIndex();
311 const bool trigger = currentIndex.isValid()
312 && (event->key() == Qt::Key_Return)
313 && (selModel->selectedIndexes().count() <= 1);
314 if (trigger) {
315 triggerItem(currentIndex);
316 }
317 }
318
319 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
320 {
321 QTreeView::resizeEvent(event);
322
323 // assure that the width of the name-column does not get too small
324 const int minWidth = 120;
325 QHeaderView* headerView = header();
326 bool useFixedWidth = (headerView->sectionSize(KDirModel::Name) <= minWidth)
327 && (headerView->resizeMode(0) != QHeaderView::Fixed);
328 if (useFixedWidth) {
329 // the current width of the name-column is too small, hence
330 // use a fixed size
331 headerView->setResizeMode(QHeaderView::Fixed);
332 headerView->setResizeMode(0, QHeaderView::Fixed);
333 headerView->resizeSection(KDirModel::Name, minWidth);
334 } else if (headerView->resizeMode(0) != QHeaderView::Stretch) {
335 // check whether there is enough available viewport width
336 // to automatically resize the columns
337 const int availableWidth = viewport()->width();
338
339 int headerWidth = 0;
340 const int count = headerView->count();
341 for (int i = 0; i < count; ++i) {
342 headerWidth += headerView->sectionSize(i);
343 }
344
345 if (headerWidth < availableWidth) {
346 headerView->setResizeMode(QHeaderView::ResizeToContents);
347 headerView->setResizeMode(0, QHeaderView::Stretch);
348 }
349 }
350 }
351
352 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
353 {
354 QHeaderView* headerView = header();
355 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
356 }
357
358 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
359 {
360 QHeaderView* headerView = header();
361 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
362 }
363
364 void DolphinDetailsView::synchronizeSortingState(int column)
365 {
366 // The sorting has already been changed in QTreeView if this slot is
367 // invoked, but Dolphin is not informed about this.
368 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
369 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
370 m_controller->indicateSortingChange(sorting);
371 m_controller->indicateSortOrderChange(sortOrder);
372 }
373
374 void DolphinDetailsView::slotEntered(const QModelIndex& index)
375 {
376 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
377 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
378 if (pos.x() < nameColumnWidth) {
379 m_controller->emitItemEntered(itemForIndex(index));
380 }
381 else {
382 m_controller->emitViewportEntered();
383 }
384 }
385
386 void DolphinDetailsView::updateElasticBand()
387 {
388 Q_ASSERT(m_showElasticBand);
389 QRect dirtyRegion(elasticBandRect());
390 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
391 dirtyRegion = dirtyRegion.united(elasticBandRect());
392 setDirtyRegion(dirtyRegion);
393 }
394
395 QRect DolphinDetailsView::elasticBandRect() const
396 {
397 const QPoint pos(contentsPos());
398 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
399 return QRect(topLeft, m_elasticBandDestination).normalized();
400 }
401
402 void DolphinDetailsView::zoomIn()
403 {
404 if (isZoomInPossible()) {
405 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
406 switch (settings->iconSize()) {
407 case KIconLoader::SizeSmall: settings->setIconSize(KIconLoader::SizeMedium); break;
408 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeLarge); break;
409 default: Q_ASSERT(false); break;
410 }
411 updateDecorationSize();
412 }
413 }
414
415 void DolphinDetailsView::zoomOut()
416 {
417 if (isZoomOutPossible()) {
418 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
419 switch (settings->iconSize()) {
420 case KIconLoader::SizeLarge: settings->setIconSize(KIconLoader::SizeMedium); break;
421 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeSmall); break;
422 default: Q_ASSERT(false); break;
423 }
424 updateDecorationSize();
425 }
426 }
427
428 void DolphinDetailsView::triggerItem(const QModelIndex& index)
429 {
430 const KFileItem item = itemForIndex(index);
431 if (index.isValid() && (index.column() == KDirModel::Name)) {
432 m_controller->triggerItem(item);
433 } else {
434 clearSelection();
435 m_controller->emitItemEntered(item);
436 }
437 }
438
439 void DolphinDetailsView::configureColumns(const QPoint& pos)
440 {
441 KMenu popup(this);
442 popup.addTitle(i18nc("@title:menu", "Columns"));
443
444 QHeaderView* headerView = header();
445 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
446 const int logicalIndex = headerView->logicalIndex(i);
447 const QString text = model()->headerData(i, Qt::Horizontal).toString();
448 QAction* action = popup.addAction(text);
449 action->setCheckable(true);
450 action->setChecked(!headerView->isSectionHidden(logicalIndex));
451 action->setData(i);
452 }
453
454 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
455 if (activatedAction != 0) {
456 const bool show = activatedAction->isChecked();
457 const int columnIndex = activatedAction->data().toInt();
458
459 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
460 KFileItemDelegate::Information info = KFileItemDelegate::NoInformation;
461 switch (columnIndex) {
462 case DolphinModel::Size: info = KFileItemDelegate::Size; break;
463 case DolphinModel::ModifiedTime: info = KFileItemDelegate::ModificationTime; break;
464 case DolphinModel::Permissions: info = KFileItemDelegate::Permissions; break;
465 case DolphinModel::Owner: info = KFileItemDelegate::Owner; break;
466 case DolphinModel::Group: info = KFileItemDelegate::OwnerAndGroup; break;
467 case DolphinModel::Type: info = KFileItemDelegate::FriendlyMimeType; break;
468 default: break;
469 }
470
471 if (show) {
472 Q_ASSERT(!list.contains(info));
473 list.append(info);
474 } else {
475 Q_ASSERT(list.contains(info));
476 const int index = list.indexOf(info);
477 list.removeAt(index);
478 }
479
480 m_controller->indicateAdditionalInfoChange(list);
481 setColumnHidden(columnIndex, !show);
482 }
483 }
484
485 void DolphinDetailsView::updateColumnVisibility()
486 {
487 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
488 if (list.isEmpty() || list.contains(KFileItemDelegate::NoInformation)) {
489 // Using the details view without any additional information (-> additional column)
490 // makes no sense and leads to a usability problem as no viewport area is available
491 // anymore. Hence as fallback provide at least a size and date column.
492 list.clear();
493 list.append(KFileItemDelegate::Size);
494 list.append(KFileItemDelegate::ModificationTime);
495 m_controller->indicateAdditionalInfoChange(list);
496 }
497
498 setColumnHidden(DolphinModel::Size, !list.contains(KFileItemDelegate::Size));
499 setColumnHidden(DolphinModel::ModifiedTime, !list.contains(KFileItemDelegate::ModificationTime));
500 setColumnHidden(DolphinModel::Permissions, !list.contains(KFileItemDelegate::Permissions));
501 setColumnHidden(DolphinModel::Owner, !list.contains(KFileItemDelegate::Owner));
502 setColumnHidden(DolphinModel::Group, !list.contains(KFileItemDelegate::OwnerAndGroup));
503 setColumnHidden(DolphinModel::Type, !list.contains(KFileItemDelegate::FriendlyMimeType));
504 }
505
506 bool DolphinDetailsView::isZoomInPossible() const
507 {
508 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
509 return settings->iconSize() < KIconLoader::SizeLarge;
510 }
511
512 bool DolphinDetailsView::isZoomOutPossible() const
513 {
514 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
515 return settings->iconSize() > KIconLoader::SizeSmall;
516 }
517
518 void DolphinDetailsView::updateDecorationSize()
519 {
520 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
521 const int iconSize = settings->iconSize();
522 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
523
524 m_controller->setZoomInPossible(isZoomInPossible());
525 m_controller->setZoomOutPossible(isZoomOutPossible());
526
527 doItemsLayout();
528 }
529
530 QPoint DolphinDetailsView::contentsPos() const
531 {
532 // implementation note: the horizonal position is ignored currently, as no
533 // horizontal scrolling is done anyway during a selection
534 const QScrollBar* scrollbar = verticalScrollBar();
535 Q_ASSERT(scrollbar != 0);
536
537 const int maxHeight = maximumViewportSize().height();
538 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
539 const int visibleHeight = model()->rowCount() + 1 - height;
540 if (visibleHeight <= 0) {
541 return QPoint(0, 0);
542 }
543
544 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
545 return QPoint(0, y);
546 }
547
548 KFileItem DolphinDetailsView::itemForIndex(const QModelIndex& index) const
549 {
550 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(model());
551 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
552 const QModelIndex dirIndex = proxyModel->mapToSource(index);
553 return dirModel->itemForIndex(dirIndex);
554 }
555
556 #include "dolphindetailsview.moc"