]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
SVN_SILENT: add line break
[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 // Assure that by respecting the available width that:
138 // - the 'Name' column is stretched as large as possible
139 // - the remaining columns are as small as possible
140 QHeaderView* headerView = header();
141 headerView->setStretchLastSection(false);
142 headerView->setResizeMode(QHeaderView::ResizeToContents);
143 headerView->setResizeMode(0, QHeaderView::Stretch);
144 headerView->setMovable(false);
145
146 updateColumnVisibility();
147
148 hideColumn(DolphinModel::Rating);
149 hideColumn(DolphinModel::Tags);
150 }
151 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
152 // check avoids a division by zero happening on versions before 4.3.1.
153 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
154 // ereslibre
155 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
156 else if (event->type() == QEvent::UpdateRequest) {
157 // a wheel movement will scroll 4 items
158 if (model()->rowCount() > 0) {
159 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
160 }
161 }
162 #endif
163
164 return QTreeView::event(event);
165 }
166
167 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
168 {
169 return m_viewOptions;
170 }
171
172 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
173 {
174 QTreeView::contextMenuEvent(event);
175 m_controller->triggerContextMenuRequest(event->pos());
176 }
177
178 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
179 {
180 m_controller->requestActivation();
181
182 QTreeView::mousePressEvent(event);
183
184 const QModelIndex index = indexAt(event->pos());
185 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
186 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
187 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
188 clearSelection();
189 }
190 }
191
192 if (event->button() == Qt::LeftButton) {
193 m_showElasticBand = true;
194
195 const QPoint pos(contentsPos());
196 m_elasticBandOrigin = event->pos();
197 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
198 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
199 m_elasticBandDestination = event->pos();
200 }
201 }
202
203 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
204 {
205 QTreeView::mouseMoveEvent(event);
206 if (m_showElasticBand) {
207 updateElasticBand();
208 }
209 }
210
211 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
212 {
213 QTreeView::mouseReleaseEvent(event);
214 if (m_showElasticBand) {
215 updateElasticBand();
216 m_showElasticBand = false;
217 }
218 }
219
220 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
221 {
222 if (event->mimeData()->hasUrls()) {
223 event->acceptProposedAction();
224 }
225
226 if (m_showElasticBand) {
227 updateElasticBand();
228 m_showElasticBand = false;
229 }
230 m_dragging = true;
231 }
232
233 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
234 {
235 QTreeView::dragLeaveEvent(event);
236
237 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
238 m_dragging = false;
239 setDirtyRegion(m_dropRect);
240 }
241
242 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
243 {
244 QTreeView::dragMoveEvent(event);
245
246 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
247 setDirtyRegion(m_dropRect);
248 const QModelIndex index = indexAt(event->pos());
249 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
250 m_dragging = false;
251 } else {
252 m_dragging = true;
253 if (itemForIndex(index).isDir()) {
254 m_dropRect = visualRect(index);
255 } else {
256 m_dropRect.setSize(QSize()); // set as invalid
257 }
258 setDirtyRegion(m_dropRect);
259 }
260 }
261
262 void DolphinDetailsView::dropEvent(QDropEvent* event)
263 {
264 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
265 if (!urls.isEmpty()) {
266 event->acceptProposedAction();
267 const QModelIndex index = indexAt(event->pos());
268 KFileItem item;
269 if (index.isValid() && (index.column() == DolphinModel::Name)) {
270 item = itemForIndex(index);
271 }
272 m_controller->indicateDroppedUrls(urls,
273 m_controller->url(),
274 item);
275 }
276 QTreeView::dropEvent(event);
277 m_dragging = false;
278 }
279
280 void DolphinDetailsView::paintEvent(QPaintEvent* event)
281 {
282 QTreeView::paintEvent(event);
283 if (m_showElasticBand) {
284 // The following code has been taken from QListView
285 // and adapted to DolphinDetailsView.
286 // (C) 1992-2007 Trolltech ASA
287 QStyleOptionRubberBand opt;
288 opt.initFrom(this);
289 opt.shape = QRubberBand::Rectangle;
290 opt.opaque = false;
291 opt.rect = elasticBandRect();
292
293 QPainter painter(viewport());
294 painter.save();
295 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
296 painter.restore();
297 }
298
299 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
300 if (m_dragging) {
301 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
302 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
303 }
304 }
305
306 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
307 {
308 QTreeView::keyPressEvent(event);
309
310 const QItemSelectionModel* selModel = selectionModel();
311 const QModelIndex currentIndex = selModel->currentIndex();
312 const bool trigger = currentIndex.isValid()
313 && (event->key() == Qt::Key_Return)
314 && (selModel->selectedIndexes().count() <= 1);
315 if (trigger) {
316 triggerItem(currentIndex);
317 }
318 }
319
320 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
321 {
322 QTreeView::resizeEvent(event);
323
324 // assure that the width of the name-column does not get too small
325 const int minWidth = 120;
326 QHeaderView* headerView = header();
327 bool useFixedWidth = (headerView->sectionSize(KDirModel::Name) <= minWidth)
328 && (headerView->resizeMode(0) != QHeaderView::Fixed);
329 if (useFixedWidth) {
330 // the current width of the name-column is too small, hence
331 // use a fixed size
332 headerView->setResizeMode(QHeaderView::Fixed);
333 headerView->setResizeMode(0, QHeaderView::Fixed);
334 headerView->resizeSection(KDirModel::Name, minWidth);
335 } else if (headerView->resizeMode(0) != QHeaderView::Stretch) {
336 // check whether there is enough available viewport width
337 // to automatically resize the columns
338 const int availableWidth = viewport()->width();
339
340 int headerWidth = 0;
341 const int count = headerView->count();
342 for (int i = 0; i < count; ++i) {
343 headerWidth += headerView->sectionSize(i);
344 }
345
346 if (headerWidth < availableWidth) {
347 headerView->setResizeMode(QHeaderView::ResizeToContents);
348 headerView->setResizeMode(0, QHeaderView::Stretch);
349 }
350 }
351 }
352
353 void DolphinDetailsView::closeEvent(QCloseEvent* event)
354 {
355 if (m_clearAdditionalInfo) {
356 disconnect(m_controller->dolphinView(), SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
357 this, SLOT(updateColumnVisibility()));
358
359 KFileItemDelegate::InformationList info;
360 info.append(KFileItemDelegate::NoInformation);
361 m_controller->indicateAdditionalInfoChange(info);
362 m_clearAdditionalInfo = false;
363 }
364 QTreeView::closeEvent(event);
365 }
366
367 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
368 {
369 QHeaderView* headerView = header();
370 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
371 }
372
373 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
374 {
375 QHeaderView* headerView = header();
376 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
377 }
378
379 void DolphinDetailsView::synchronizeSortingState(int column)
380 {
381 // The sorting has already been changed in QTreeView if this slot is
382 // invoked, but Dolphin is not informed about this.
383 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
384 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
385 m_controller->indicateSortingChange(sorting);
386 m_controller->indicateSortOrderChange(sortOrder);
387 }
388
389 void DolphinDetailsView::slotEntered(const QModelIndex& index)
390 {
391 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
392 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
393 if (pos.x() < nameColumnWidth) {
394 m_controller->emitItemEntered(itemForIndex(index));
395 }
396 else {
397 m_controller->emitViewportEntered();
398 }
399 }
400
401 void DolphinDetailsView::updateElasticBand()
402 {
403 Q_ASSERT(m_showElasticBand);
404 QRect dirtyRegion(elasticBandRect());
405 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
406 dirtyRegion = dirtyRegion.united(elasticBandRect());
407 setDirtyRegion(dirtyRegion);
408 }
409
410 QRect DolphinDetailsView::elasticBandRect() const
411 {
412 const QPoint pos(contentsPos());
413 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
414 return QRect(topLeft, m_elasticBandDestination).normalized();
415 }
416
417 void DolphinDetailsView::zoomIn()
418 {
419 if (isZoomInPossible()) {
420 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
421 switch (settings->iconSize()) {
422 case KIconLoader::SizeSmall: settings->setIconSize(KIconLoader::SizeMedium); break;
423 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeLarge); break;
424 default: Q_ASSERT(false); break;
425 }
426 updateDecorationSize();
427 }
428 }
429
430 void DolphinDetailsView::zoomOut()
431 {
432 if (isZoomOutPossible()) {
433 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
434 switch (settings->iconSize()) {
435 case KIconLoader::SizeLarge: settings->setIconSize(KIconLoader::SizeMedium); break;
436 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeSmall); break;
437 default: Q_ASSERT(false); break;
438 }
439 updateDecorationSize();
440 }
441 }
442
443 void DolphinDetailsView::triggerItem(const QModelIndex& index)
444 {
445 const KFileItem item = itemForIndex(index);
446 if (index.isValid() && (index.column() == KDirModel::Name)) {
447 m_controller->triggerItem(item);
448 } else {
449 clearSelection();
450 m_controller->emitItemEntered(item);
451 }
452 }
453
454 void DolphinDetailsView::configureColumns(const QPoint& pos)
455 {
456 KMenu popup(this);
457 popup.addTitle(i18nc("@title:menu", "Columns"));
458
459 QHeaderView* headerView = header();
460 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
461 const int logicalIndex = headerView->logicalIndex(i);
462 const QString text = model()->headerData(i, Qt::Horizontal).toString();
463 QAction* action = popup.addAction(text);
464 action->setCheckable(true);
465 action->setChecked(!headerView->isSectionHidden(logicalIndex));
466 action->setData(i);
467 }
468
469 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
470 if (activatedAction != 0) {
471 const bool show = activatedAction->isChecked();
472 const int columnIndex = activatedAction->data().toInt();
473
474 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
475 const KFileItemDelegate::Information info = infoForColumn(columnIndex);
476 if (show) {
477 Q_ASSERT(!list.contains(info));
478 list.append(info);
479 } else {
480 Q_ASSERT(list.contains(info));
481 const int index = list.indexOf(info);
482 list.removeAt(index);
483 }
484
485 m_controller->indicateAdditionalInfoChange(list);
486 setColumnHidden(columnIndex, !show);
487 }
488 }
489
490 void DolphinDetailsView::updateColumnVisibility()
491 {
492 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
493 const bool useDefaultColumns = !isVisible() &&
494 (list.isEmpty() ||
495 list.contains(KFileItemDelegate::NoInformation));
496 if (useDefaultColumns) {
497 // Using the details view without any additional information (-> additional column)
498 // makes no sense and leads to a usability problem as no viewport area is available
499 // anymore. Hence as fallback provide at least a size and date column.
500 list.clear();
501 list.append(KFileItemDelegate::Size);
502 list.append(KFileItemDelegate::ModificationTime);
503 m_controller->indicateAdditionalInfoChange(list);
504 m_clearAdditionalInfo = true;
505 }
506
507 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
508 const KFileItemDelegate::Information info = infoForColumn(i);
509 const bool hide = !list.contains(info);
510 if (isColumnHidden(i) != hide) {
511 setColumnHidden(i, hide);
512 m_clearAdditionalInfo = false;
513 }
514 }
515 }
516
517 bool DolphinDetailsView::isZoomInPossible() const
518 {
519 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
520 return settings->iconSize() < KIconLoader::SizeLarge;
521 }
522
523 bool DolphinDetailsView::isZoomOutPossible() const
524 {
525 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
526 return settings->iconSize() > KIconLoader::SizeSmall;
527 }
528
529 void DolphinDetailsView::updateDecorationSize()
530 {
531 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
532 const int iconSize = settings->iconSize();
533 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
534
535 m_controller->setZoomInPossible(isZoomInPossible());
536 m_controller->setZoomOutPossible(isZoomOutPossible());
537
538 doItemsLayout();
539 }
540
541 QPoint DolphinDetailsView::contentsPos() const
542 {
543 // implementation note: the horizonal position is ignored currently, as no
544 // horizontal scrolling is done anyway during a selection
545 const QScrollBar* scrollbar = verticalScrollBar();
546 Q_ASSERT(scrollbar != 0);
547
548 const int maxHeight = maximumViewportSize().height();
549 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
550 const int visibleHeight = model()->rowCount() + 1 - height;
551 if (visibleHeight <= 0) {
552 return QPoint(0, 0);
553 }
554
555 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
556 return QPoint(0, y);
557 }
558
559 KFileItem DolphinDetailsView::itemForIndex(const QModelIndex& index) const
560 {
561 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(model());
562 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
563 const QModelIndex dirIndex = proxyModel->mapToSource(index);
564 return dirModel->itemForIndex(dirIndex);
565 }
566
567 KFileItemDelegate::Information DolphinDetailsView::infoForColumn(int columnIndex) const
568 {
569 KFileItemDelegate::Information info = KFileItemDelegate::NoInformation;
570
571 switch (columnIndex) {
572 case DolphinModel::Size: info = KFileItemDelegate::Size; break;
573 case DolphinModel::ModifiedTime: info = KFileItemDelegate::ModificationTime; break;
574 case DolphinModel::Permissions: info = KFileItemDelegate::Permissions; break;
575 case DolphinModel::Owner: info = KFileItemDelegate::Owner; break;
576 case DolphinModel::Group: info = KFileItemDelegate::OwnerAndGroup; break;
577 case DolphinModel::Type: info = KFileItemDelegate::FriendlyMimeType; break;
578 default: break;
579 }
580
581 return info;
582 }
583
584 #include "dolphindetailsview.moc"