]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
Even better...
[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
101 // apply the details mode settings to the widget
102 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
103 Q_ASSERT(settings != 0);
104
105 m_viewOptions = QTreeView::viewOptions();
106
107 QFont font(settings->fontFamily(), settings->fontSize());
108 font.setItalic(settings->italicFont());
109 font.setBold(settings->boldFont());
110 m_viewOptions.font = font;
111 m_viewOptions.showDecorationSelected = true;
112
113 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
114 // check avoids a division by zero happening on versions before 4.3.1.
115 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
116 // ereslibre
117 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
118 setVerticalScrollMode(QTreeView::ScrollPerPixel);
119 setHorizontalScrollMode(QTreeView::ScrollPerPixel);
120 #endif
121
122 updateDecorationSize();
123 }
124
125 DolphinDetailsView::~DolphinDetailsView()
126 {
127 }
128
129 bool DolphinDetailsView::event(QEvent* event)
130 {
131 if (event->type() == QEvent::Polish) {
132 // Assure that by respecting the available width that:
133 // - the 'Name' column is stretched as large as possible
134 // - the remaining columns are as small as possible
135 QHeaderView* headerView = header();
136 headerView->setStretchLastSection(false);
137 headerView->setResizeMode(QHeaderView::ResizeToContents);
138 headerView->setResizeMode(0, QHeaderView::Stretch);
139 headerView->setMovable(false);
140
141 // hide columns if this is indicated by the settings
142 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
143 Q_ASSERT(settings != 0);
144 if (!settings->showDate()) {
145 hideColumn(DolphinModel::ModifiedTime);
146 }
147
148 if (!settings->showPermissions()) {
149 hideColumn(DolphinModel::Permissions);
150 }
151
152 if (!settings->showOwner()) {
153 hideColumn(DolphinModel::Owner);
154 }
155
156 if (!settings->showGroup()) {
157 hideColumn(DolphinModel::Group);
158 }
159
160 if (!settings->showType()) {
161 hideColumn(DolphinModel::Type);
162 }
163
164 hideColumn(DolphinModel::Rating);
165 hideColumn(DolphinModel::Tags);
166 }
167 // TODO: Remove this check when 4.3.2 is released and KDE requires it... this
168 // check avoids a division by zero happening on versions before 4.3.1.
169 // Right now KDE in theory can be shipped with Qt 4.3.0 and above.
170 // ereslibre
171 #if (QT_VERSION >= QT_VERSION_CHECK(4, 3, 2) || defined(QT_KDE_QT_COPY))
172 else if (event->type() == QEvent::UpdateRequest) {
173 // a wheel movement will scroll 4 items
174 if (model()->rowCount() > 0) {
175 verticalScrollBar()->setSingleStep((sizeHintForRow(0) / 3) * 4);
176 }
177 }
178 #endif
179
180 return QTreeView::event(event);
181 }
182
183 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
184 {
185 return m_viewOptions;
186 }
187
188 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
189 {
190 QTreeView::contextMenuEvent(event);
191 m_controller->triggerContextMenuRequest(event->pos());
192 }
193
194 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
195 {
196 m_controller->requestActivation();
197
198 QTreeView::mousePressEvent(event);
199
200 const QModelIndex index = indexAt(event->pos());
201 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
202 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
203 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
204 clearSelection();
205 }
206 }
207
208 if (event->button() == Qt::LeftButton) {
209 m_showElasticBand = true;
210
211 const QPoint pos(contentsPos());
212 m_elasticBandOrigin = event->pos();
213 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
214 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
215 m_elasticBandDestination = event->pos();
216 }
217 }
218
219 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
220 {
221 QTreeView::mouseMoveEvent(event);
222 if (m_showElasticBand) {
223 updateElasticBand();
224 }
225 }
226
227 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
228 {
229 QTreeView::mouseReleaseEvent(event);
230 if (m_showElasticBand) {
231 updateElasticBand();
232 m_showElasticBand = false;
233 }
234 }
235
236 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
237 {
238 if (event->mimeData()->hasUrls()) {
239 event->acceptProposedAction();
240 }
241
242 if (m_showElasticBand) {
243 updateElasticBand();
244 m_showElasticBand = false;
245 }
246 m_dragging = true;
247 }
248
249 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
250 {
251 QTreeView::dragLeaveEvent(event);
252
253 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
254 m_dragging = false;
255 setDirtyRegion(m_dropRect);
256 }
257
258 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
259 {
260 QTreeView::dragMoveEvent(event);
261
262 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
263 setDirtyRegion(m_dropRect);
264 const QModelIndex index = indexAt(event->pos());
265 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
266 m_dragging = false;
267 } else {
268 m_dragging = true;
269 m_dropRect = visualRect(index);
270 setDirtyRegion(m_dropRect);
271 }
272 }
273
274 void DolphinDetailsView::dropEvent(QDropEvent* event)
275 {
276 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
277 if (!urls.isEmpty()) {
278 event->acceptProposedAction();
279 m_controller->indicateDroppedUrls(urls,
280 m_controller->url(),
281 indexAt(event->pos()),
282 event->source());
283 }
284 QTreeView::dropEvent(event);
285 m_dragging = false;
286 }
287
288 void DolphinDetailsView::paintEvent(QPaintEvent* event)
289 {
290 QTreeView::paintEvent(event);
291 if (m_showElasticBand) {
292 // The following code has been taken from QListView
293 // and adapted to DolphinDetailsView.
294 // (C) 1992-2007 Trolltech ASA
295 QStyleOptionRubberBand opt;
296 opt.initFrom(this);
297 opt.shape = QRubberBand::Rectangle;
298 opt.opaque = false;
299 opt.rect = elasticBandRect();
300
301 QPainter painter(viewport());
302 painter.save();
303 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
304 painter.restore();
305 }
306
307 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
308 if (m_dragging) {
309 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
310 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
311 }
312 }
313
314 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
315 {
316 QTreeView::keyPressEvent(event);
317
318 const QItemSelectionModel* selModel = selectionModel();
319 const QModelIndex currentIndex = selModel->currentIndex();
320 const bool trigger = currentIndex.isValid()
321 && (event->key() == Qt::Key_Return)
322 && (selModel->selectedIndexes().count() <= 1);
323 if (trigger) {
324 triggerItem(currentIndex);
325 }
326 }
327
328 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
329 {
330 QTreeView::resizeEvent(event);
331
332 // assure that the width of the name-column does not get too small
333 const int minWidth = 120;
334 QHeaderView* headerView = header();
335 bool useFixedWidth = (headerView->sectionSize(KDirModel::Name) <= minWidth)
336 && (headerView->resizeMode(0) != QHeaderView::Fixed);
337 if (useFixedWidth) {
338 // the current width of the name-column is too small, hence
339 // use a fixed size
340 headerView->setResizeMode(QHeaderView::Fixed);
341 headerView->setResizeMode(0, QHeaderView::Fixed);
342 headerView->resizeSection(KDirModel::Name, minWidth);
343 } else if (headerView->resizeMode(0) != QHeaderView::Stretch) {
344 // check whether there is enough available viewport width
345 // to automatically resize the columns
346 const int availableWidth = viewport()->width();
347
348 int headerWidth = 0;
349 const int count = headerView->count();
350 for (int i = 0; i < count; ++i) {
351 headerWidth += headerView->sectionSize(i);
352 }
353
354 if (headerWidth < availableWidth) {
355 headerView->setResizeMode(QHeaderView::ResizeToContents);
356 headerView->setResizeMode(0, QHeaderView::Stretch);
357 }
358 }
359 }
360
361 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
362 {
363 QHeaderView* headerView = header();
364 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
365 }
366
367 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
368 {
369 QHeaderView* headerView = header();
370 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
371 }
372
373 void DolphinDetailsView::synchronizeSortingState(int column)
374 {
375 // The sorting has already been changed in QTreeView if this slot is
376 // invoked, but Dolphin is not informed about this.
377 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
378 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
379 m_controller->indicateSortingChange(sorting);
380 m_controller->indicateSortOrderChange(sortOrder);
381 }
382
383 void DolphinDetailsView::slotEntered(const QModelIndex& index)
384 {
385 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
386 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
387 if (pos.x() < nameColumnWidth) {
388 m_controller->emitItemEntered(itemForIndex(index));
389 }
390 else {
391 m_controller->emitViewportEntered();
392 }
393 }
394
395 void DolphinDetailsView::updateElasticBand()
396 {
397 Q_ASSERT(m_showElasticBand);
398 QRect dirtyRegion(elasticBandRect());
399 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
400 dirtyRegion = dirtyRegion.united(elasticBandRect());
401 setDirtyRegion(dirtyRegion);
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::triggerItem(const QModelIndex& index)
438 {
439 const KFileItem item = itemForIndex(index);
440 if (index.isValid() && (index.column() == KDirModel::Name)) {
441 m_controller->triggerItem(item);
442 } else {
443 clearSelection();
444 m_controller->emitItemEntered(item);
445 }
446 }
447
448 void DolphinDetailsView::configureColumns(const QPoint& pos)
449 {
450 KMenu popup(this);
451 popup.addTitle(i18nc("@title:menu", "Columns"));
452
453 QHeaderView* headerView = header();
454 for (int i = DolphinModel::ModifiedTime; i <= DolphinModel::Type; ++i) {
455 const int logicalIndex = headerView->logicalIndex(i);
456 const QString text = model()->headerData(i, Qt::Horizontal).toString();
457 QAction* action = popup.addAction(text);
458 action->setCheckable(true);
459 action->setChecked(!headerView->isSectionHidden(logicalIndex));
460 action->setData(i);
461 }
462
463 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
464 if (activatedAction != 0) {
465 const bool show = activatedAction->isChecked();
466 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
467 Q_ASSERT(settings != 0);
468
469 // remember the changed column visibility in the settings
470 const int columnIndex = activatedAction->data().toInt();
471 switch (columnIndex) {
472 case DolphinModel::ModifiedTime: settings->setShowDate(show); break;
473 case DolphinModel::Permissions: settings->setShowPermissions(show); break;
474 case DolphinModel::Owner: settings->setShowOwner(show); break;
475 case DolphinModel::Group: settings->setShowGroup(show); break;
476 case DolphinModel::Type: settings->setShowType(show); break;
477 default: break;
478 }
479
480 // apply the changed column visibility
481 if (show) {
482 showColumn(columnIndex);
483 } else {
484 hideColumn(columnIndex);
485 }
486 }
487 }
488
489 bool DolphinDetailsView::isZoomInPossible() const
490 {
491 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
492 return settings->iconSize() < KIconLoader::SizeLarge;
493 }
494
495 bool DolphinDetailsView::isZoomOutPossible() const
496 {
497 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
498 return settings->iconSize() > KIconLoader::SizeSmall;
499 }
500
501 void DolphinDetailsView::updateDecorationSize()
502 {
503 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
504 const int iconSize = settings->iconSize();
505 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
506
507 m_controller->setZoomInPossible(isZoomInPossible());
508 m_controller->setZoomOutPossible(isZoomOutPossible());
509
510 doItemsLayout();
511 }
512
513 QPoint DolphinDetailsView::contentsPos() const
514 {
515 // implementation note: the horizonal position is ignored currently, as no
516 // horizontal scrolling is done anyway during a selection
517 const QScrollBar* scrollbar = verticalScrollBar();
518 Q_ASSERT(scrollbar != 0);
519
520 const int maxHeight = maximumViewportSize().height();
521 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
522 const int visibleHeight = model()->rowCount() + 1 - height;
523 if (visibleHeight <= 0) {
524 return QPoint(0, 0);
525 }
526
527 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
528 return QPoint(0, y);
529 }
530
531 KFileItem DolphinDetailsView::itemForIndex(const QModelIndex& index) const
532 {
533 QAbstractProxyModel* proxyModel = static_cast<QAbstractProxyModel*>(model());
534 KDirModel* dirModel = static_cast<KDirModel*>(proxyModel->sourceModel());
535 const QModelIndex dirIndex = proxyModel->mapToSource(index);
536 return dirModel->itemForIndex(dirIndex);
537 }
538
539 #include "dolphindetailsview.moc"