]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
use only 1 textline as default for the icons view
[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_clearAdditionalInfo(false),
47 m_dragging(false),
48 m_showElasticBand(false),
49 m_elasticBandOrigin(),
50 m_elasticBandDestination()
51 {
52 Q_ASSERT(controller != 0);
53
54 setAcceptDrops(true);
55 setRootIsDecorated(false);
56 setSortingEnabled(true);
57 setUniformRowHeights(true);
58 setSelectionBehavior(SelectItems);
59 setDragDropMode(QAbstractItemView::DragDrop);
60 setDropIndicatorShown(false);
61 setAlternatingRowColors(true);
62
63 setMouseTracking(true);
64 viewport()->setAttribute(Qt::WA_Hover);
65
66 const ViewProperties props(controller->url());
67 setSortIndicatorSection(props.sorting());
68 setSortIndicatorOrder(props.sortOrder());
69
70 QHeaderView* headerView = header();
71 connect(headerView, SIGNAL(sectionClicked(int)),
72 this, SLOT(synchronizeSortingState(int)));
73 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
74 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
75 this, SLOT(configureColumns(const QPoint&)));
76
77 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
78 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
79 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
80 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
81
82 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
83 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
84 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
85 // RETURN-key in keyPressEvent().
86 if (KGlobalSettings::singleClick()) {
87 connect(this, SIGNAL(clicked(const QModelIndex&)),
88 this, SLOT(triggerItem(const QModelIndex&)));
89 } else {
90 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
91 this, SLOT(triggerItem(const QModelIndex&)));
92 }
93 connect(this, SIGNAL(entered(const QModelIndex&)),
94 this, SLOT(slotEntered(const QModelIndex&)));
95 connect(this, SIGNAL(viewportEntered()),
96 controller, SLOT(emitViewportEntered()));
97 connect(controller, SIGNAL(zoomIn()),
98 this, SLOT(zoomIn()));
99 connect(controller, SIGNAL(zoomOut()),
100 this, SLOT(zoomOut()));
101 connect(controller->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
102 this, SLOT(updateColumnVisibility()));
103
104 // apply the details mode settings to the widget
105 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
106 Q_ASSERT(settings != 0);
107
108 m_viewOptions = QTreeView::viewOptions();
109
110 QFont font(settings->fontFamily(), settings->fontSize());
111 font.setItalic(settings->italicFont());
112 font.setBold(settings->boldFont());
113 m_viewOptions.font = font;
114 m_viewOptions.showDecorationSelected = true;
115
116 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
117 // check avoids a division by zero happening on versions before 4.3.1.
118 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
119 // ereslibre
120 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
121 setVerticalScrollMode(QTreeView::ScrollPerPixel);
122 setHorizontalScrollMode(QTreeView::ScrollPerPixel);
123 #endif
124
125 updateDecorationSize();
126
127 setFocus();
128 }
129
130 DolphinDetailsView::~DolphinDetailsView()
131 {
132 }
133
134 bool DolphinDetailsView::event(QEvent* event)
135 {
136 if (event->type() == QEvent::Polish) {
137 QHeaderView* headerView = header();
138 headerView->setResizeMode(QHeaderView::Fixed);
139 headerView->setMovable(false);
140
141 updateColumnVisibility();
142
143 hideColumn(DolphinModel::Rating);
144 hideColumn(DolphinModel::Tags);
145 }
146 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
147 // check avoids a division by zero happening on versions before 4.3.1.
148 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
149 // ereslibre
150 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
151 else if (event->type() == QEvent::UpdateRequest) {
152 // a wheel movement will scroll 4 items
153 if (model()->rowCount() > 0) {
154 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
155 }
156 }
157 #endif
158
159 return QTreeView::event(event);
160 }
161
162 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
163 {
164 return m_viewOptions;
165 }
166
167 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
168 {
169 QTreeView::contextMenuEvent(event);
170 m_controller->triggerContextMenuRequest(event->pos());
171 }
172
173 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
174 {
175 m_controller->requestActivation();
176
177 QTreeView::mousePressEvent(event);
178
179 const QModelIndex index = indexAt(event->pos());
180 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
181 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
182 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
183 clearSelection();
184 }
185 }
186
187 if (event->button() == Qt::LeftButton) {
188 m_showElasticBand = true;
189
190 const QPoint pos(contentsPos());
191 m_elasticBandOrigin = event->pos();
192 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
193 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
194 m_elasticBandDestination = event->pos();
195 }
196 }
197
198 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
199 {
200 QTreeView::mouseMoveEvent(event);
201 if (m_showElasticBand) {
202 updateElasticBand();
203 }
204 }
205
206 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
207 {
208 QTreeView::mouseReleaseEvent(event);
209 if (m_showElasticBand) {
210 updateElasticBand();
211 m_showElasticBand = false;
212 }
213 }
214
215 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
216 {
217 if (event->mimeData()->hasUrls()) {
218 event->acceptProposedAction();
219 }
220
221 if (m_showElasticBand) {
222 updateElasticBand();
223 m_showElasticBand = false;
224 }
225 m_dragging = true;
226 }
227
228 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
229 {
230 QTreeView::dragLeaveEvent(event);
231
232 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
233 m_dragging = false;
234 setDirtyRegion(m_dropRect);
235 }
236
237 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
238 {
239 QTreeView::dragMoveEvent(event);
240
241 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
242 setDirtyRegion(m_dropRect);
243 const QModelIndex index = indexAt(event->pos());
244 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
245 m_dragging = false;
246 } else {
247 m_dragging = true;
248 if (itemForIndex(index).isDir()) {
249 m_dropRect = visualRect(index);
250 } else {
251 m_dropRect.setSize(QSize()); // set as invalid
252 }
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 KFileItem item;
264 if (index.isValid() && (index.column() == DolphinModel::Name)) {
265 item = itemForIndex(index);
266 }
267 m_controller->indicateDroppedUrls(urls,
268 m_controller->url(),
269 item);
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 resizeColumns();
319 }
320
321 void DolphinDetailsView::closeEvent(QCloseEvent* event)
322 {
323 if (m_clearAdditionalInfo) {
324 disconnect(m_controller->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
325 this, SLOT(updateColumnVisibility()));
326
327 KFileItemDelegate::InformationList info;
328 info.append(KFileItemDelegate::NoInformation);
329 m_controller->indicateAdditionalInfoChange(info);
330 m_clearAdditionalInfo = false;
331 }
332 QTreeView::closeEvent(event);
333 }
334
335 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
336 {
337 QHeaderView* headerView = header();
338 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
339 }
340
341 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
342 {
343 QHeaderView* headerView = header();
344 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
345 }
346
347 void DolphinDetailsView::synchronizeSortingState(int column)
348 {
349 // The sorting has already been changed in QTreeView if this slot is
350 // invoked, but Dolphin is not informed about this.
351 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
352 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
353 m_controller->indicateSortingChange(sorting);
354 m_controller->indicateSortOrderChange(sortOrder);
355 }
356
357 void DolphinDetailsView::slotEntered(const QModelIndex& index)
358 {
359 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
360 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
361 if (pos.x() < nameColumnWidth) {
362 m_controller->emitItemEntered(itemForIndex(index));
363 }
364 else {
365 m_controller->emitViewportEntered();
366 }
367 }
368
369 void DolphinDetailsView::updateElasticBand()
370 {
371 Q_ASSERT(m_showElasticBand);
372 QRect dirtyRegion(elasticBandRect());
373 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
374 dirtyRegion = dirtyRegion.united(elasticBandRect());
375 setDirtyRegion(dirtyRegion);
376 }
377
378 QRect DolphinDetailsView::elasticBandRect() const
379 {
380 const QPoint pos(contentsPos());
381 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
382 return QRect(topLeft, m_elasticBandDestination).normalized();
383 }
384
385 void DolphinDetailsView::zoomIn()
386 {
387 if (isZoomInPossible()) {
388 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
389 switch (settings->iconSize()) {
390 case KIconLoader::SizeSmall: settings->setIconSize(KIconLoader::SizeMedium); break;
391 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeLarge); break;
392 default: Q_ASSERT(false); break;
393 }
394 updateDecorationSize();
395 }
396 }
397
398 void DolphinDetailsView::zoomOut()
399 {
400 if (isZoomOutPossible()) {
401 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
402 switch (settings->iconSize()) {
403 case KIconLoader::SizeLarge: settings->setIconSize(KIconLoader::SizeMedium); break;
404 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeSmall); break;
405 default: Q_ASSERT(false); break;
406 }
407 updateDecorationSize();
408 }
409 }
410
411 void DolphinDetailsView::triggerItem(const QModelIndex& index)
412 {
413 const KFileItem item = itemForIndex(index);
414 if (index.isValid() && (index.column() == KDirModel::Name)) {
415 m_controller->triggerItem(item);
416 } else {
417 clearSelection();
418 m_controller->emitItemEntered(item);
419 }
420 }
421
422 void DolphinDetailsView::configureColumns(const QPoint& pos)
423 {
424 KMenu popup(this);
425 popup.addTitle(i18nc("@title:menu", "Columns"));
426
427 QHeaderView* headerView = header();
428 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
429 const int logicalIndex = headerView->logicalIndex(i);
430 const QString text = model()->headerData(i, Qt::Horizontal).toString();
431 QAction* action = popup.addAction(text);
432 action->setCheckable(true);
433 action->setChecked(!headerView->isSectionHidden(logicalIndex));
434 action->setData(i);
435 }
436
437 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
438 if (activatedAction != 0) {
439 const bool show = activatedAction->isChecked();
440 const int columnIndex = activatedAction->data().toInt();
441
442 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
443 const KFileItemDelegate::Information info = infoForColumn(columnIndex);
444 if (show) {
445 Q_ASSERT(!list.contains(info));
446 list.append(info);
447 } else {
448 Q_ASSERT(list.contains(info));
449 const int index = list.indexOf(info);
450 list.removeAt(index);
451 }
452
453 m_controller->indicateAdditionalInfoChange(list);
454 setColumnHidden(columnIndex, !show);
455 }
456 }
457
458 void DolphinDetailsView::updateColumnVisibility()
459 {
460 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
461 const bool useDefaultColumns = !isVisible() &&
462 (list.isEmpty() ||
463 list.contains(KFileItemDelegate::NoInformation));
464 if (useDefaultColumns) {
465 // Using the details view without any additional information (-> additional column)
466 // makes no sense and leads to a usability problem as no viewport area is available
467 // anymore. Hence as fallback provide at least a size and date column.
468 list.clear();
469 list.append(KFileItemDelegate::Size);
470 list.append(KFileItemDelegate::ModificationTime);
471 m_controller->indicateAdditionalInfoChange(list);
472 m_clearAdditionalInfo = true;
473 }
474
475 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
476 const KFileItemDelegate::Information info = infoForColumn(i);
477 const bool hide = !list.contains(info);
478 if (isColumnHidden(i) != hide) {
479 setColumnHidden(i, hide);
480 m_clearAdditionalInfo = false;
481 }
482 }
483
484 resizeColumns();
485 }
486
487 bool DolphinDetailsView::isZoomInPossible() const
488 {
489 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
490 return settings->iconSize() < KIconLoader::SizeLarge;
491 }
492
493 bool DolphinDetailsView::isZoomOutPossible() const
494 {
495 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
496 return settings->iconSize() > KIconLoader::SizeSmall;
497 }
498
499 void DolphinDetailsView::updateDecorationSize()
500 {
501 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
502 const int iconSize = settings->iconSize();
503 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
504
505 m_controller->setZoomInPossible(isZoomInPossible());
506 m_controller->setZoomOutPossible(isZoomOutPossible());
507
508 doItemsLayout();
509 }
510
511 QPoint DolphinDetailsView::contentsPos() const
512 {
513 // implementation note: the horizonal position is ignored currently, as no
514 // horizontal scrolling is done anyway during a selection
515 const QScrollBar* scrollbar = verticalScrollBar();
516 Q_ASSERT(scrollbar != 0);
517
518 const int maxHeight = maximumViewportSize().height();
519 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
520 const int visibleHeight = model()->rowCount() + 1 - height;
521 if (visibleHeight <= 0) {
522 return QPoint(0, 0);
523 }
524
525 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
526 return QPoint(0, y);
527 }
528
529 KFileItem DolphinDetailsView::itemForIndex(const QModelIndex& index) const
530 {
531 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(model());
532 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
533 const QModelIndex dirIndex = proxyModel->mapToSource(index);
534 return dirModel->itemForIndex(dirIndex);
535 }
536
537 KFileItemDelegate::Information DolphinDetailsView::infoForColumn(int columnIndex) const
538 {
539 KFileItemDelegate::Information info = KFileItemDelegate::NoInformation;
540
541 switch (columnIndex) {
542 case DolphinModel::Size: info = KFileItemDelegate::Size; break;
543 case DolphinModel::ModifiedTime: info = KFileItemDelegate::ModificationTime; break;
544 case DolphinModel::Permissions: info = KFileItemDelegate::Permissions; break;
545 case DolphinModel::Owner: info = KFileItemDelegate::Owner; break;
546 case DolphinModel::Group: info = KFileItemDelegate::OwnerAndGroup; break;
547 case DolphinModel::Type: info = KFileItemDelegate::FriendlyMimeType; break;
548 default: break;
549 }
550
551 return info;
552 }
553
554 void DolphinDetailsView::resizeColumns()
555 {
556 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
557 // around 3 seconds for each (!) resize operation when having > 10000 items).
558 // This gets a problem especially when opening large directories, where several
559 // resize operations are received for showing the currently available items during
560 // loading (the application hangs around 20 seconds when loading > 10000 items).
561
562 QHeaderView* headerView = header();
563 QFontMetrics fontMetrics(viewport()->font());
564
565 int columnWidth[KDirModel::ColumnCount];
566 columnWidth[KDirModel::Size] = fontMetrics.width("00000 Items");
567 columnWidth[KDirModel::ModifiedTime] = fontMetrics.width("0000-00-00 00:00");
568 columnWidth[KDirModel::Permissions] = fontMetrics.width("xxxxxxxxxx");
569 columnWidth[KDirModel::Owner] = fontMetrics.width("xxxxxxxxxx");
570 columnWidth[KDirModel::Group] = fontMetrics.width("xxxxxxxxxx");
571 columnWidth[KDirModel::Type] = fontMetrics.width("XXXX Xxxxxxx");
572
573 int requiredWidth = 0;
574 for (int i = KDirModel::Size; i <= KDirModel::Type; ++i) {
575 if (!isColumnHidden(i)) {
576 columnWidth[i] += 20; // provide a default gap
577 requiredWidth += columnWidth[i];
578 headerView->resizeSection(i, columnWidth[i]);
579 }
580 }
581
582 // resize the name column in a way that the whole available width is used
583 columnWidth[KDirModel::Name] = viewport()->width() - requiredWidth;
584 if (columnWidth[KDirModel::Name] < 120) {
585 columnWidth[KDirModel::Name] = 120;
586 }
587 headerView->resizeSection(KDirModel::Name, columnWidth[KDirModel::Name]);
588 }
589
590 #include "dolphindetailsview.moc"