]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
53bf4e2a7026a241465a71bd554bb59535638dd7
[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 "draganddrophelper.h"
28 #include "selectionmanager.h"
29 #include "viewproperties.h"
30
31 #include "dolphin_detailsmodesettings.h"
32 #include "dolphin_generalsettings.h"
33
34 #include <kdirmodel.h>
35 #include <klocale.h>
36 #include <kmenu.h>
37
38 #include <QAbstractProxyModel>
39 #include <QAction>
40 #include <QApplication>
41 #include <QHeaderView>
42 #include <QRubberBand>
43 #include <QPainter>
44 #include <QScrollBar>
45
46 DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) :
47 QTreeView(parent),
48 m_autoResize(true),
49 m_controller(controller),
50 m_font(),
51 m_decorationSize(),
52 m_showElasticBand(false),
53 m_elasticBandOrigin(),
54 m_elasticBandDestination()
55 {
56 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
57 Q_ASSERT(settings != 0);
58 Q_ASSERT(controller != 0);
59
60 setAcceptDrops(true);
61 setSortingEnabled(true);
62 setUniformRowHeights(true);
63 setSelectionBehavior(SelectItems);
64 setDragDropMode(QAbstractItemView::DragDrop);
65 setDropIndicatorShown(false);
66 setAlternatingRowColors(true);
67 setRootIsDecorated(settings->expandableFolders());
68 setItemsExpandable(settings->expandableFolders());
69
70 setMouseTracking(true);
71
72 const ViewProperties props(controller->url());
73 setSortIndicatorSection(props.sorting());
74 setSortIndicatorOrder(props.sortOrder());
75
76 QHeaderView* headerView = header();
77 connect(headerView, SIGNAL(sectionClicked(int)),
78 this, SLOT(synchronizeSortingState(int)));
79 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
80 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
81 this, SLOT(configureColumns(const QPoint&)));
82 connect(headerView, SIGNAL(sectionResized(int, int, int)),
83 this, SLOT(slotHeaderSectionResized(int, int, int)));
84 connect(headerView, SIGNAL(sectionHandleDoubleClicked(int)),
85 this, SLOT(disableAutoResizing()));
86
87 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
88 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
89 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
90 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
91
92 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
93 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
94 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
95 // RETURN-key in keyPressEvent().
96 if (KGlobalSettings::singleClick()) {
97 connect(this, SIGNAL(clicked(const QModelIndex&)),
98 controller, SLOT(triggerItem(const QModelIndex&)));
99 if (DolphinSettings::instance().generalSettings()->showSelectionToggle()) {
100 SelectionManager* selManager = new SelectionManager(this);
101 connect(selManager, SIGNAL(selectionChanged()),
102 this, SLOT(requestActivation()));
103 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
104 selManager, SLOT(reset()));
105 }
106 } else {
107 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
108 controller, SLOT(triggerItem(const QModelIndex&)));
109 }
110 connect(this, SIGNAL(entered(const QModelIndex&)),
111 this, SLOT(slotEntered(const QModelIndex&)));
112 connect(this, SIGNAL(viewportEntered()),
113 controller, SLOT(emitViewportEntered()));
114 connect(controller, SIGNAL(zoomIn()),
115 this, SLOT(zoomIn()));
116 connect(controller, SIGNAL(zoomOut()),
117 this, SLOT(zoomOut()));
118 connect(controller->dolphinView(), SIGNAL(additionalInfoChanged()),
119 this, SLOT(updateColumnVisibility()));
120
121 if (settings->useSystemFont()) {
122 m_font = KGlobalSettings::generalFont();
123 } else {
124 m_font = QFont(settings->fontFamily(),
125 settings->fontSize(),
126 settings->fontWeight(),
127 settings->italicFont());
128 }
129
130 setVerticalScrollMode(QTreeView::ScrollPerPixel);
131 setHorizontalScrollMode(QTreeView::ScrollPerPixel);
132
133 updateDecorationSize();
134
135 setFocus();
136
137 connect(KGlobalSettings::self(), SIGNAL(kdisplayFontChanged()),
138 this, SLOT(updateFont()));
139 }
140
141 DolphinDetailsView::~DolphinDetailsView()
142 {
143 }
144
145 bool DolphinDetailsView::event(QEvent* event)
146 {
147 if (event->type() == QEvent::Polish) {
148 QHeaderView* headerView = header();
149 headerView->setResizeMode(QHeaderView::Interactive);
150 headerView->setMovable(false);
151
152 updateColumnVisibility();
153
154 hideColumn(DolphinModel::Rating);
155 hideColumn(DolphinModel::Tags);
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
163 return QTreeView::event(event);
164 }
165
166 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
167 {
168 QStyleOptionViewItem viewOptions = QTreeView::viewOptions();
169 viewOptions.font = m_font;
170 viewOptions.showDecorationSelected = true;
171 viewOptions.decorationSize = m_decorationSize;
172 return viewOptions;
173 }
174
175 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
176 {
177 QTreeView::contextMenuEvent(event);
178 m_controller->triggerContextMenuRequest(event->pos());
179 }
180
181 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
182 {
183 m_controller->requestActivation();
184
185 QTreeView::mousePressEvent(event);
186
187 const QModelIndex index = indexAt(event->pos());
188 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
189 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
190 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
191 clearSelection();
192 }
193 }
194
195 if (event->button() == Qt::LeftButton) {
196 m_showElasticBand = true;
197
198 const QPoint pos(contentsPos());
199 m_elasticBandOrigin = event->pos();
200 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
201 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
202 m_elasticBandDestination = event->pos();
203 }
204 }
205
206 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
207 {
208 if (m_showElasticBand) {
209 const QPoint mousePos = event->pos();
210 const QModelIndex index = indexAt(mousePos);
211 if (!index.isValid()) {
212 // the destination of the selection rectangle is above the viewport. In this
213 // case QTreeView does no selection at all, which is not the wanted behavior
214 // in Dolphin -> select all items within the elastic band rectangle
215 clearSelection();
216
217 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
218 QRect selRect = QRect(m_elasticBandOrigin, m_elasticBandDestination).normalized();
219 const QRect nameColumnsRect(0, 0, nameColumnWidth, viewport()->height());
220 selRect = nameColumnsRect.intersected(selRect);
221
222 setSelection(selRect, QItemSelectionModel::Select);
223 }
224
225 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
226 // as the Qt-issue #199631 has been fixed.
227 // QTreeView::mouseMoveEvent(event);
228 QAbstractItemView::mouseMoveEvent(event);
229 updateElasticBand();
230 } else {
231 // TODO: enable QTreeView::mouseMoveEvent(event) again, as soon
232 // as the Qt-issue #199631 has been fixed.
233 // QTreeView::mouseMoveEvent(event);
234 QAbstractItemView::mouseMoveEvent(event);
235 }
236 }
237
238 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
239 {
240 QTreeView::mouseReleaseEvent(event);
241 if (m_showElasticBand) {
242 updateElasticBand();
243 m_showElasticBand = false;
244 }
245 }
246
247 void DolphinDetailsView::startDrag(Qt::DropActions supportedActions)
248 {
249 DragAndDropHelper::startDrag(this, supportedActions);
250 }
251
252 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
253 {
254 if (event->mimeData()->hasUrls()) {
255 event->acceptProposedAction();
256 }
257
258 if (m_showElasticBand) {
259 updateElasticBand();
260 m_showElasticBand = false;
261 }
262 }
263
264 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
265 {
266 QTreeView::dragLeaveEvent(event);
267 setDirtyRegion(m_dropRect);
268 }
269
270 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
271 {
272 QTreeView::dragMoveEvent(event);
273
274 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
275 setDirtyRegion(m_dropRect);
276 const QModelIndex index = indexAt(event->pos());
277 if (index.isValid() && (index.column() == DolphinModel::Name)) {
278 const KFileItem item = m_controller->itemForIndex(index);
279 if (!item.isNull() && item.isDir()) {
280 m_dropRect = visualRect(index);
281 } else {
282 m_dropRect.setSize(QSize()); // set as invalid
283 }
284 setDirtyRegion(m_dropRect);
285 }
286
287 if (event->mimeData()->hasUrls()) {
288 // accept url drops, independently from the destination item
289 event->acceptProposedAction();
290 }
291 }
292
293 void DolphinDetailsView::dropEvent(QDropEvent* event)
294 {
295 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
296 if (!urls.isEmpty()) {
297 event->acceptProposedAction();
298 const QModelIndex index = indexAt(event->pos());
299 KFileItem item;
300 if (index.isValid() && (index.column() == DolphinModel::Name)) {
301 item = m_controller->itemForIndex(index);
302 }
303 m_controller->indicateDroppedUrls(urls,
304 m_controller->url(),
305 item);
306 }
307 QTreeView::dropEvent(event);
308 }
309
310 void DolphinDetailsView::paintEvent(QPaintEvent* event)
311 {
312 QTreeView::paintEvent(event);
313 if (m_showElasticBand) {
314 // The following code has been taken from QListView
315 // and adapted to DolphinDetailsView.
316 // (C) 1992-2007 Trolltech ASA
317 QStyleOptionRubberBand opt;
318 opt.initFrom(this);
319 opt.shape = QRubberBand::Rectangle;
320 opt.opaque = false;
321 opt.rect = elasticBandRect();
322
323 QPainter painter(viewport());
324 painter.save();
325 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
326 painter.restore();
327 }
328 }
329
330 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
331 {
332 QTreeView::keyPressEvent(event);
333 m_controller->handleKeyPressEvent(event);
334 }
335
336 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
337 {
338 if (m_autoResize) {
339 resizeColumns();
340 }
341 QTreeView::resizeEvent(event);
342 }
343
344 void DolphinDetailsView::wheelEvent(QWheelEvent* event)
345 {
346 // let Ctrl+wheel events propagate to the DolphinView for icon zooming
347 if (event->modifiers() & Qt::ControlModifier) {
348 event->ignore();
349 return;
350 }
351 QTreeView::wheelEvent(event);
352 }
353
354 void DolphinDetailsView::currentChanged(const QModelIndex& current, const QModelIndex& previous)
355 {
356 QTreeView::currentChanged(current, previous);
357 selectionModel()->select(current, QItemSelectionModel::ClearAndSelect);
358 }
359
360 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
361 {
362 QHeaderView* headerView = header();
363 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
364 }
365
366 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
367 {
368 QHeaderView* headerView = header();
369 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
370 }
371
372 void DolphinDetailsView::synchronizeSortingState(int column)
373 {
374 // The sorting has already been changed in QTreeView if this slot is
375 // invoked, but Dolphin is not informed about this.
376 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
377 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
378 m_controller->indicateSortingChange(sorting);
379 m_controller->indicateSortOrderChange(sortOrder);
380 }
381
382 void DolphinDetailsView::slotEntered(const QModelIndex& index)
383 {
384 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
385 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
386 if (pos.x() < nameColumnWidth) {
387 m_controller->emitItemEntered(index);
388 }
389 else {
390 m_controller->emitViewportEntered();
391 }
392 }
393
394 void DolphinDetailsView::updateElasticBand()
395 {
396 if (m_showElasticBand) {
397 QRect dirtyRegion(elasticBandRect());
398 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
399 dirtyRegion = dirtyRegion.united(elasticBandRect());
400 setDirtyRegion(dirtyRegion);
401 }
402 }
403
404 QRect DolphinDetailsView::elasticBandRect() const
405 {
406 const QPoint pos(contentsPos());
407 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
408 return QRect(topLeft, m_elasticBandDestination).normalized();
409 }
410
411 void DolphinDetailsView::zoomIn()
412 {
413 if (isZoomInPossible()) {
414 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
415 switch (settings->iconSize()) {
416 case KIconLoader::SizeSmall: settings->setIconSize(KIconLoader::SizeMedium); break;
417 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeLarge); break;
418 default: Q_ASSERT(false); break;
419 }
420 updateDecorationSize();
421 }
422 }
423
424 void DolphinDetailsView::zoomOut()
425 {
426 if (isZoomOutPossible()) {
427 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
428 switch (settings->iconSize()) {
429 case KIconLoader::SizeLarge: settings->setIconSize(KIconLoader::SizeMedium); break;
430 case KIconLoader::SizeMedium: settings->setIconSize(KIconLoader::SizeSmall); break;
431 default: Q_ASSERT(false); break;
432 }
433 updateDecorationSize();
434 }
435 }
436
437 void DolphinDetailsView::configureColumns(const QPoint& pos)
438 {
439 KMenu popup(this);
440 popup.addTitle(i18nc("@title:menu", "Columns"));
441
442 QHeaderView* headerView = header();
443 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
444 const int logicalIndex = headerView->logicalIndex(i);
445 const QString text = model()->headerData(i, Qt::Horizontal).toString();
446 QAction* action = popup.addAction(text);
447 action->setCheckable(true);
448 action->setChecked(!headerView->isSectionHidden(logicalIndex));
449 action->setData(i);
450 }
451
452 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
453 if (activatedAction != 0) {
454 const bool show = activatedAction->isChecked();
455 const int columnIndex = activatedAction->data().toInt();
456
457 KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
458 const KFileItemDelegate::Information info = infoForColumn(columnIndex);
459 if (show) {
460 Q_ASSERT(!list.contains(info));
461 list.append(info);
462 } else {
463 Q_ASSERT(list.contains(info));
464 const int index = list.indexOf(info);
465 list.removeAt(index);
466 }
467
468 m_controller->indicateAdditionalInfoChange(list);
469 setColumnHidden(columnIndex, !show);
470 }
471 }
472
473 void DolphinDetailsView::updateColumnVisibility()
474 {
475 const KFileItemDelegate::InformationList list = m_controller->dolphinView()->additionalInfo();
476 for (int i = DolphinModel::Size; i <= DolphinModel::Type; ++i) {
477 const KFileItemDelegate::Information info = infoForColumn(i);
478 const bool hide = !list.contains(info);
479 if (isColumnHidden(i) != hide) {
480 setColumnHidden(i, hide);
481 }
482 }
483
484 resizeColumns();
485 }
486
487 void DolphinDetailsView::slotHeaderSectionResized(int logicalIndex, int oldSize, int newSize)
488 {
489 Q_UNUSED(logicalIndex);
490 Q_UNUSED(oldSize);
491 Q_UNUSED(newSize);
492 if (QApplication::mouseButtons() & Qt::LeftButton) {
493 disableAutoResizing();
494 }
495 }
496
497 void DolphinDetailsView::disableAutoResizing()
498 {
499 m_autoResize = false;
500 }
501
502 void DolphinDetailsView::requestActivation()
503 {
504 m_controller->requestActivation();
505 }
506
507 void DolphinDetailsView::updateFont()
508 {
509 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
510 Q_ASSERT(settings != 0);
511
512 if (settings->useSystemFont()) {
513 m_font = KGlobalSettings::generalFont();
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 setIconSize(QSize(iconSize, iconSize));
534 m_decorationSize = QSize(iconSize, iconSize);
535
536 m_controller->setZoomInPossible(isZoomInPossible());
537 m_controller->setZoomOutPossible(isZoomOutPossible());
538
539 doItemsLayout();
540 }
541
542 QPoint DolphinDetailsView::contentsPos() const
543 {
544 // implementation note: the horizonal position is ignored currently, as no
545 // horizontal scrolling is done anyway during a selection
546 const QScrollBar* scrollbar = verticalScrollBar();
547 Q_ASSERT(scrollbar != 0);
548
549 const int maxHeight = maximumViewportSize().height();
550 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
551 const int visibleHeight = model()->rowCount() + 1 - height;
552 if (visibleHeight <= 0) {
553 return QPoint(0, 0);
554 }
555
556 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
557 return QPoint(0, y);
558 }
559
560 KFileItemDelegate::Information DolphinDetailsView::infoForColumn(int columnIndex) const
561 {
562 KFileItemDelegate::Information info = KFileItemDelegate::NoInformation;
563
564 switch (columnIndex) {
565 case DolphinModel::Size: info = KFileItemDelegate::Size; break;
566 case DolphinModel::ModifiedTime: info = KFileItemDelegate::ModificationTime; break;
567 case DolphinModel::Permissions: info = KFileItemDelegate::Permissions; break;
568 case DolphinModel::Owner: info = KFileItemDelegate::Owner; break;
569 case DolphinModel::Group: info = KFileItemDelegate::OwnerAndGroup; break;
570 case DolphinModel::Type: info = KFileItemDelegate::FriendlyMimeType; break;
571 default: break;
572 }
573
574 return info;
575 }
576
577 void DolphinDetailsView::resizeColumns()
578 {
579 // Using the resize mode QHeaderView::ResizeToContents is too slow (it takes
580 // around 3 seconds for each (!) resize operation when having > 10000 items).
581 // This gets a problem especially when opening large directories, where several
582 // resize operations are received for showing the currently available items during
583 // loading (the application hangs around 20 seconds when loading > 10000 items).
584
585 QHeaderView* headerView = header();
586 QFontMetrics fontMetrics(viewport()->font());
587
588 int columnWidth[KDirModel::ColumnCount];
589 columnWidth[KDirModel::Size] = fontMetrics.width("00000 Items");
590 columnWidth[KDirModel::ModifiedTime] = fontMetrics.width("0000-00-00 00:00");
591 columnWidth[KDirModel::Permissions] = fontMetrics.width("xxxxxxxxxx");
592 columnWidth[KDirModel::Owner] = fontMetrics.width("xxxxxxxxxx");
593 columnWidth[KDirModel::Group] = fontMetrics.width("xxxxxxxxxx");
594 columnWidth[KDirModel::Type] = fontMetrics.width("XXXX Xxxxxxx");
595
596 int requiredWidth = 0;
597 for (int i = KDirModel::Size; i <= KDirModel::Type; ++i) {
598 if (!isColumnHidden(i)) {
599 columnWidth[i] += 20; // provide a default gap
600 requiredWidth += columnWidth[i];
601 headerView->resizeSection(i, columnWidth[i]);
602 }
603 }
604
605 // resize the name column in a way that the whole available width is used
606 columnWidth[KDirModel::Name] = viewport()->width() - requiredWidth;
607 if (columnWidth[KDirModel::Name] < 120) {
608 columnWidth[KDirModel::Name] = 120;
609 }
610 headerView->resizeSection(KDirModel::Name, columnWidth[KDirModel::Name]);
611 }
612
613 #include "dolphindetailsview.moc"