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