]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
Simplify DolphinController: don't remember the show-preview state in the controller...
[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 m_dropRect = visualRect(index);
253 setDirtyRegion(m_dropRect);
254 }
255 }
256
257 void DolphinDetailsView::dropEvent(QDropEvent* event)
258 {
259 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
260 if (!urls.isEmpty()) {
261 event->acceptProposedAction();
262 const QModelIndex index = indexAt(event->pos());
263 if (index.isValid() && (index.column() == DolphinModel::Name)) {
264 const KFileItem item = itemForIndex(index);
265 m_controller->indicateDroppedUrls(urls,
266 m_controller->url(),
267 item,
268 event->source());
269 }
270 }
271 QTreeView::dropEvent(event);
272 m_dragging = false;
273 }
274
275 void DolphinDetailsView::paintEvent(QPaintEvent* event)
276 {
277 QTreeView::paintEvent(event);
278 if (m_showElasticBand) {
279 // The following code has been taken from QListView
280 // and adapted to DolphinDetailsView.
281 // (C) 1992-2007 Trolltech ASA
282 QStyleOptionRubberBand opt;
283 opt.initFrom(this);
284 opt.shape = QRubberBand::Rectangle;
285 opt.opaque = false;
286 opt.rect = elasticBandRect();
287
288 QPainter painter(viewport());
289 painter.save();
290 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
291 painter.restore();
292 }
293
294 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
295 if (m_dragging) {
296 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
297 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
298 }
299 }
300
301 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
302 {
303 QTreeView::keyPressEvent(event);
304
305 const QItemSelectionModel* selModel = selectionModel();
306 const QModelIndex currentIndex = selModel->currentIndex();
307 const bool trigger = currentIndex.isValid()
308 && (event->key() == Qt::Key_Return)
309 && (selModel->selectedIndexes().count() <= 1);
310 if (trigger) {
311 triggerItem(currentIndex);
312 }
313 }
314
315 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
316 {
317 QTreeView::resizeEvent(event);
318
319 // assure that the width of the name-column does not get too small
320 const int minWidth = 120;
321 QHeaderView* headerView = header();
322 bool useFixedWidth = (headerView->sectionSize(KDirModel::Name) <= minWidth)
323 && (headerView->resizeMode(0) != QHeaderView::Fixed);
324 if (useFixedWidth) {
325 // the current width of the name-column is too small, hence
326 // use a fixed size
327 headerView->setResizeMode(QHeaderView::Fixed);
328 headerView->setResizeMode(0, QHeaderView::Fixed);
329 headerView->resizeSection(KDirModel::Name, minWidth);
330 } else if (headerView->resizeMode(0) != QHeaderView::Stretch) {
331 // check whether there is enough available viewport width
332 // to automatically resize the columns
333 const int availableWidth = viewport()->width();
334
335 int headerWidth = 0;
336 const int count = headerView->count();
337 for (int i = 0; i < count; ++i) {
338 headerWidth += headerView->sectionSize(i);
339 }
340
341 if (headerWidth < availableWidth) {
342 headerView->setResizeMode(QHeaderView::ResizeToContents);
343 headerView->setResizeMode(0, QHeaderView::Stretch);
344 }
345 }
346 }
347
348 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
349 {
350 QHeaderView* headerView = header();
351 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
352 }
353
354 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
355 {
356 QHeaderView* headerView = header();
357 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
358 }
359
360 void DolphinDetailsView::synchronizeSortingState(int column)
361 {
362 // The sorting has already been changed in QTreeView if this slot is
363 // invoked, but Dolphin is not informed about this.
364 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
365 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
366 m_controller->indicateSortingChange(sorting);
367 m_controller->indicateSortOrderChange(sortOrder);
368 }
369
370 void DolphinDetailsView::slotEntered(const QModelIndex& index)
371 {
372 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
373 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
374 if (pos.x() < nameColumnWidth) {
375 m_controller->emitItemEntered(itemForIndex(index));
376 }
377 else {
378 m_controller->emitViewportEntered();
379 }
380 }
381
382 void DolphinDetailsView::updateElasticBand()
383 {
384 Q_ASSERT(m_showElasticBand);
385 QRect dirtyRegion(elasticBandRect());
386 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
387 dirtyRegion = dirtyRegion.united(elasticBandRect());
388 setDirtyRegion(dirtyRegion);
389 }
390
391 QRect DolphinDetailsView::elasticBandRect() const
392 {
393 const QPoint pos(contentsPos());
394 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
395 return QRect(topLeft, m_elasticBandDestination).normalized();
396 }
397
398 void DolphinDetailsView::zoomIn()
399 {
400 if (isZoomInPossible()) {
401 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
402 switch (settings->iconSize()) {
403 case KIconLoader::SizeSmall: settings->setIconSize(KIconLoader::SizeMedium); break;
404 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeLarge); break;
405 default: Q_ASSERT(false); break;
406 }
407 updateDecorationSize();
408 }
409 }
410
411 void DolphinDetailsView::zoomOut()
412 {
413 if (isZoomOutPossible()) {
414 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
415 switch (settings->iconSize()) {
416 case KIconLoader::SizeLarge: settings->setIconSize(KIconLoader::SizeMedium); break;
417 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeSmall); break;
418 default: Q_ASSERT(false); break;
419 }
420 updateDecorationSize();
421 }
422 }
423
424 void DolphinDetailsView::triggerItem(const QModelIndex& index)
425 {
426 const KFileItem item = itemForIndex(index);
427 if (index.isValid() && (index.column() == KDirModel::Name)) {
428 m_controller->triggerItem(item);
429 } else {
430 clearSelection();
431 m_controller->emitItemEntered(item);
432 }
433 }
434
435 void DolphinDetailsView::configureColumns(const QPoint& pos)
436 {
437 KMenu popup(this);
438 popup.addTitle(i18nc("@title:menu", "Columns"));
439
440 QHeaderView* headerView = header();
441 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
442 const int logicalIndex = headerView->logicalIndex(i);
443 const QString text = model()->headerData(i, Qt::Horizontal).toString();
444 QAction* action = popup.addAction(text);
445 action->setCheckable(true);
446 action->setChecked(!headerView->isSectionHidden(logicalIndex));
447 action->setData(i);
448 }
449
450 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
451 if (activatedAction != 0) {
452 const bool show = activatedAction->isChecked();
453 const int columnIndex = activatedAction->data().toInt();
454
455 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
456 KFileItemDelegate::Information info = KFileItemDelegate::NoInformation;
457 switch (columnIndex) {
458 case DolphinModel::Size: info = KFileItemDelegate::Size; break;
459 case DolphinModel::ModifiedTime: info = KFileItemDelegate::ModificationTime; break;
460 case DolphinModel::Permissions: info = KFileItemDelegate::Permissions; break;
461 case DolphinModel::Owner: info = KFileItemDelegate::Owner; break;
462 case DolphinModel::Group: info = KFileItemDelegate::OwnerAndGroup; break;
463 case DolphinModel::Type: info = KFileItemDelegate::FriendlyMimeType; break;
464 default: break;
465 }
466
467 if (show) {
468 Q_ASSERT(!list.contains(info));
469 list.append(info);
470 } else {
471 Q_ASSERT(list.contains(info));
472 const int index = list.indexOf(info);
473 list.removeAt(index);
474 }
475
476 m_controller->indicateAdditionalInfoChange(list);
477 setColumnHidden(columnIndex, !show);
478 }
479 }
480
481 void DolphinDetailsView::updateColumnVisibility()
482 {
483 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
484 if (list.isEmpty() || list.contains(KFileItemDelegate::NoInformation)) {
485 // Using the details view without any additional information (-> additional column)
486 // makes no sense and leads to a usability problem as no viewport area is available
487 // anymore. Hence as fallback provide at least a size and date column.
488 list.clear();
489 list.append(KFileItemDelegate::Size);
490 list.append(KFileItemDelegate::ModificationTime);
491 m_controller->indicateAdditionalInfoChange(list);
492 }
493
494 setColumnHidden(DolphinModel::Size, !list.contains(KFileItemDelegate::Size));
495 setColumnHidden(DolphinModel::ModifiedTime, !list.contains(KFileItemDelegate::ModificationTime));
496 setColumnHidden(DolphinModel::Permissions, !list.contains(KFileItemDelegate::Permissions));
497 setColumnHidden(DolphinModel::Owner, !list.contains(KFileItemDelegate::Owner));
498 setColumnHidden(DolphinModel::Group, !list.contains(KFileItemDelegate::OwnerAndGroup));
499 setColumnHidden(DolphinModel::Type, !list.contains(KFileItemDelegate::FriendlyMimeType));
500 }
501
502 bool DolphinDetailsView::isZoomInPossible() const
503 {
504 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
505 return settings->iconSize() < KIconLoader::SizeLarge;
506 }
507
508 bool DolphinDetailsView::isZoomOutPossible() const
509 {
510 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
511 return settings->iconSize() > KIconLoader::SizeSmall;
512 }
513
514 void DolphinDetailsView::updateDecorationSize()
515 {
516 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
517 const int iconSize = settings->iconSize();
518 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
519
520 m_controller->setZoomInPossible(isZoomInPossible());
521 m_controller->setZoomOutPossible(isZoomOutPossible());
522
523 doItemsLayout();
524 }
525
526 QPoint DolphinDetailsView::contentsPos() const
527 {
528 // implementation note: the horizonal position is ignored currently, as no
529 // horizontal scrolling is done anyway during a selection
530 const QScrollBar* scrollbar = verticalScrollBar();
531 Q_ASSERT(scrollbar != 0);
532
533 const int maxHeight = maximumViewportSize().height();
534 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
535 const int visibleHeight = model()->rowCount() + 1 - height;
536 if (visibleHeight <= 0) {
537 return QPoint(0, 0);
538 }
539
540 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
541 return QPoint(0, y);
542 }
543
544 KFileItem DolphinDetailsView::itemForIndex(const QModelIndex& index) const
545 {
546 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(model());
547 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
548 const QModelIndex dirIndex = proxyModel->mapToSource(index);
549 return dirModel->itemForIndex(dirIndex);
550 }
551
552 #include "dolphindetailsview.moc"