]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
allow to show/hide the columns of the details-view by a context menu for the header...
[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 <klocale.h>
32 #include <kmenu.h>
33
34 #include <QAction>
35 #include <QApplication>
36 #include <QHeaderView>
37 #include <QRubberBand>
38 #include <QPainter>
39 #include <QScrollBar>
40
41 DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) :
42 QTreeView(parent),
43 m_controller(controller),
44 m_dragging(false),
45 m_showElasticBand(false),
46 m_elasticBandOrigin(),
47 m_elasticBandDestination()
48 {
49 Q_ASSERT(controller != 0);
50
51 setAcceptDrops(true);
52 setRootIsDecorated(false);
53 setSortingEnabled(true);
54 setUniformRowHeights(true);
55 setSelectionBehavior(SelectItems);
56 setDragDropMode(QAbstractItemView::DragDrop);
57 setDropIndicatorShown(false);
58 setAlternatingRowColors(true);
59
60 setMouseTracking(true);
61 viewport()->setAttribute(Qt::WA_Hover);
62
63 const ViewProperties props(controller->url());
64 setSortIndicatorSection(props.sorting());
65 setSortIndicatorOrder(props.sortOrder());
66
67 QHeaderView* headerView = header();
68 connect(headerView, SIGNAL(sectionClicked(int)),
69 this, SLOT(synchronizeSortingState(int)));
70 headerView->setContextMenuPolicy(Qt::CustomContextMenu);
71 connect(headerView, SIGNAL(customContextMenuRequested(const QPoint&)),
72 this, SLOT(configureColumns(const QPoint&)));
73
74 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
75 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
76 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
77 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
78
79 // TODO: Connecting to the signal 'activated()' is not possible, as kstyle
80 // does not forward the single vs. doubleclick to it yet (KDE 4.1?). Hence it is
81 // necessary connecting the signal 'singleClick()' or 'doubleClick' and to handle the
82 // RETURN-key in keyPressEvent().
83 if (KGlobalSettings::singleClick()) {
84 connect(this, SIGNAL(clicked(const QModelIndex&)),
85 this, SLOT(slotItemActivated(const QModelIndex&)));
86 } else {
87 connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
88 this, SLOT(slotItemActivated(const QModelIndex&)));
89 }
90 connect(this, SIGNAL(entered(const QModelIndex&)),
91 this, SLOT(slotEntered(const QModelIndex&)));
92 connect(this, SIGNAL(viewportEntered()),
93 controller, SLOT(emitViewportEntered()));
94 connect(controller, SIGNAL(zoomIn()),
95 this, SLOT(zoomIn()));
96 connect(controller, SIGNAL(zoomOut()),
97 this, SLOT(zoomOut()));
98
99 // apply the details mode settings to the widget
100 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
101 Q_ASSERT(settings != 0);
102
103 m_viewOptions = QTreeView::viewOptions();
104
105 QFont font(settings->fontFamily(), settings->fontSize());
106 font.setItalic(settings->italicFont());
107 font.setBold(settings->boldFont());
108 m_viewOptions.font = font;
109
110 updateDecorationSize();
111 }
112
113 DolphinDetailsView::~DolphinDetailsView()
114 {
115 }
116
117 bool DolphinDetailsView::event(QEvent* event)
118 {
119 if (event->type() == QEvent::Polish) {
120 // Assure that by respecting the available width that:
121 // - the 'Name' column is stretched as large as possible
122 // - the remaining columns are as small as possible
123 QHeaderView* headerView = header();
124 headerView->setStretchLastSection(false);
125 headerView->setResizeMode(QHeaderView::ResizeToContents);
126 headerView->setResizeMode(0, QHeaderView::Stretch);
127
128 // hide columns if this is indicated by the settings
129 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
130 Q_ASSERT(settings != 0);
131 if (!settings->showDate()) {
132 hideColumn(DolphinModel::ModifiedTime);
133 }
134
135 if (!settings->showPermissions()) {
136 hideColumn(DolphinModel::Permissions);
137 }
138
139 if (!settings->showOwner()) {
140 hideColumn(DolphinModel::Owner);
141 }
142
143 if (!settings->showGroup()) {
144 hideColumn(DolphinModel::Group);
145 }
146
147 if (!settings->showType()) {
148 hideColumn(DolphinModel::Type);
149 }
150
151 hideColumn(DolphinModel::Rating);
152 hideColumn(DolphinModel::Tags);
153 }
154
155 return QTreeView::event(event);
156 }
157
158 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
159 {
160 return m_viewOptions;
161 }
162
163 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
164 {
165 QTreeView::contextMenuEvent(event);
166 m_controller->triggerContextMenuRequest(event->pos());
167 }
168
169 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
170 {
171 m_controller->triggerActivation();
172
173 QTreeView::mousePressEvent(event);
174
175 const QModelIndex index = indexAt(event->pos());
176 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
177 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
178 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
179 clearSelection();
180 }
181 }
182
183 if (event->button() == Qt::LeftButton) {
184 m_showElasticBand = true;
185
186 const QPoint pos(contentsPos());
187 m_elasticBandOrigin = event->pos();
188 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
189 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
190 m_elasticBandDestination = event->pos();
191 }
192 }
193
194 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
195 {
196 QTreeView::mouseMoveEvent(event);
197 if (m_showElasticBand) {
198 updateElasticBand();
199 }
200 }
201
202 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
203 {
204 QTreeView::mouseReleaseEvent(event);
205 if (m_showElasticBand) {
206 updateElasticBand();
207 m_showElasticBand = false;
208 }
209 }
210
211 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
212 {
213 if (event->mimeData()->hasUrls()) {
214 event->acceptProposedAction();
215 }
216
217 if (m_showElasticBand) {
218 updateElasticBand();
219 m_showElasticBand = false;
220 }
221 m_dragging = true;
222 }
223
224 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
225 {
226 QTreeView::dragLeaveEvent(event);
227
228 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
229 m_dragging = false;
230 setDirtyRegion(m_dropRect);
231 }
232
233 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
234 {
235 QTreeView::dragMoveEvent(event);
236
237 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
238 setDirtyRegion(m_dropRect);
239 const QModelIndex index = indexAt(event->pos());
240 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
241 m_dragging = false;
242 } else {
243 m_dragging = true;
244 m_dropRect = visualRect(index);
245 setDirtyRegion(m_dropRect);
246 }
247 }
248
249 void DolphinDetailsView::dropEvent(QDropEvent* event)
250 {
251 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
252 if (!urls.isEmpty()) {
253 event->acceptProposedAction();
254 m_controller->indicateDroppedUrls(urls,
255 m_controller->url(),
256 indexAt(event->pos()),
257 event->source());
258 }
259 QTreeView::dropEvent(event);
260 m_dragging = false;
261 }
262
263 void DolphinDetailsView::paintEvent(QPaintEvent* event)
264 {
265 QTreeView::paintEvent(event);
266 if (m_showElasticBand) {
267 // The following code has been taken from QListView
268 // and adapted to DolphinDetailsView.
269 // (C) 1992-2007 Trolltech ASA
270 QStyleOptionRubberBand opt;
271 opt.initFrom(this);
272 opt.shape = QRubberBand::Rectangle;
273 opt.opaque = false;
274 opt.rect = elasticBandRect();
275
276 QPainter painter(viewport());
277 painter.save();
278 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
279 painter.restore();
280 }
281
282 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
283 if (m_dragging) {
284 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
285 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
286 }
287 }
288
289 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
290 {
291 QTreeView::keyPressEvent(event);
292
293 const QItemSelectionModel* selModel = selectionModel();
294 const QModelIndex currentIndex = selModel->currentIndex();
295 const bool triggerItem = currentIndex.isValid()
296 && (event->key() == Qt::Key_Return)
297 && (selModel->selectedIndexes().count() <= 1);
298 if (triggerItem) {
299 m_controller->triggerItem(currentIndex);
300 }
301 }
302
303 void DolphinDetailsView::resizeEvent(QResizeEvent* event)
304 {
305 QTreeView::resizeEvent(event);
306
307 // assure that the width of the name-column does not get too small
308 const int minWidth = 120;
309 QHeaderView* headerView = header();
310 bool useFixedWidth = (headerView->sectionSize(KDirModel::Name) <= minWidth)
311 && (headerView->resizeMode(0) != QHeaderView::Fixed);
312 if (useFixedWidth) {
313 // the current width of the name-column is too small, hence
314 // use a fixed size
315 headerView->setResizeMode(QHeaderView::Fixed);
316 headerView->setResizeMode(0, QHeaderView::Fixed);
317 headerView->resizeSection(KDirModel::Name, minWidth);
318 } else if (headerView->resizeMode(0) != QHeaderView::Stretch) {
319 // check whether there is enough available viewport width
320 // to automatically resize the columns
321 const int availableWidth = viewport()->width();
322
323 int headerWidth = 0;
324 const int count = headerView->count();
325 for (int i = 0; i < count; ++i) {
326 headerWidth += headerView->sectionSize(i);
327 }
328
329 if (headerWidth < availableWidth) {
330 headerView->setResizeMode(QHeaderView::ResizeToContents);
331 headerView->setResizeMode(0, QHeaderView::Stretch);
332 }
333 }
334 }
335
336 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
337 {
338 QHeaderView* headerView = header();
339 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
340 }
341
342 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
343 {
344 QHeaderView* headerView = header();
345 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
346 }
347
348 void DolphinDetailsView::synchronizeSortingState(int column)
349 {
350 // The sorting has already been changed in QTreeView if this slot is
351 // invoked, but Dolphin is not informed about this.
352 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
353 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
354 m_controller->indicateSortingChange(sorting);
355 m_controller->indicateSortOrderChange(sortOrder);
356 }
357
358 void DolphinDetailsView::slotEntered(const QModelIndex& index)
359 {
360 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
361 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
362 if (pos.x() < nameColumnWidth) {
363 m_controller->emitItemEntered(index);
364 }
365 else {
366 m_controller->emitViewportEntered();
367 }
368 }
369
370 void DolphinDetailsView::updateElasticBand()
371 {
372 Q_ASSERT(m_showElasticBand);
373 QRect dirtyRegion(elasticBandRect());
374 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
375 dirtyRegion = dirtyRegion.united(elasticBandRect());
376 setDirtyRegion(dirtyRegion);
377 }
378
379 void DolphinDetailsView::zoomIn()
380 {
381 if (isZoomInPossible()) {
382 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
383 // TODO: get rid of K3Icon sizes
384 switch (settings->iconSize()) {
385 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
386 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
387 default: Q_ASSERT(false); break;
388 }
389 updateDecorationSize();
390 }
391 }
392
393 void DolphinDetailsView::zoomOut()
394 {
395 if (isZoomOutPossible()) {
396 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
397 // TODO: get rid of K3Icon sizes
398 switch (settings->iconSize()) {
399 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
400 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
401 default: Q_ASSERT(false); break;
402 }
403 updateDecorationSize();
404 }
405 }
406
407 void DolphinDetailsView::slotItemActivated(const QModelIndex& index)
408 {
409 if (index.isValid() && (index.column() == KDirModel::Name)) {
410 m_controller->triggerItem(index);
411 } else {
412 clearSelection();
413 m_controller->emitItemEntered(index);
414 }
415 }
416
417 void DolphinDetailsView::configureColumns(const QPoint& pos)
418 {
419 KMenu popup(this);
420 popup.addTitle(i18nc("@title:menu", "Columns"));
421
422 QHeaderView* headerView = header();
423 for (int i = DolphinModel::ModifiedTime; i <= DolphinModel::Type; ++i) {
424 const int logicalIndex = headerView->logicalIndex(i);
425 const QString text = model()->headerData(i, Qt::Horizontal).toString();
426 QAction* action = popup.addAction(text);
427 action->setCheckable(true);
428 action->setChecked(!headerView->isSectionHidden(logicalIndex));
429 action->setData(i);
430 }
431
432 QAction* activatedAction = popup.exec(header()->mapToGlobal(pos));
433 if (activatedAction != 0) {
434 const bool show = activatedAction->isChecked();
435 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
436 Q_ASSERT(settings != 0);
437
438 // remember the changed column visibility in the settings
439 const int columnIndex = activatedAction->data().toInt();
440 switch (columnIndex) {
441 case DolphinModel::ModifiedTime: settings->setShowDate(show); break;
442 case DolphinModel::Permissions: settings->setShowPermissions(show); break;
443 case DolphinModel::Owner: settings->setShowOwner(show); break;
444 case DolphinModel::Group: settings->setShowGroup(show); break;
445 case DolphinModel::Type: settings->setShowType(show); break;
446 default: break;
447 }
448
449 // apply the changed column visibility
450 if (show) {
451 showColumn(columnIndex);
452 } else {
453 hideColumn(columnIndex);
454 }
455 }
456 }
457
458 bool DolphinDetailsView::isZoomInPossible() const
459 {
460 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
461 return settings->iconSize() < K3Icon::SizeLarge;
462 }
463
464 bool DolphinDetailsView::isZoomOutPossible() const
465 {
466 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
467 return settings->iconSize() > K3Icon::SizeSmall;
468 }
469
470 void DolphinDetailsView::updateDecorationSize()
471 {
472 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
473 const int iconSize = settings->iconSize();
474 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
475
476 m_controller->setZoomInPossible(isZoomInPossible());
477 m_controller->setZoomOutPossible(isZoomOutPossible());
478
479 doItemsLayout();
480 }
481
482 QPoint DolphinDetailsView::contentsPos() const
483 {
484 // implementation note: the horizonal position is ignored currently, as no
485 // horizontal scrolling is done anyway during a selection
486 const QScrollBar* scrollbar = verticalScrollBar();
487 Q_ASSERT(scrollbar != 0);
488
489 const int maxHeight = maximumViewportSize().height();
490 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
491 const int visibleHeight = model()->rowCount() + 1 - height;
492 if (visibleHeight <= 0) {
493 return QPoint(0, 0);
494 }
495
496 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
497 return QPoint(0, y);
498 }
499
500 QRect DolphinDetailsView::elasticBandRect() const
501 {
502 const QPoint pos(contentsPos());
503 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
504 return QRect(topLeft, m_elasticBandDestination).normalized();
505 }
506
507 static bool isValidNameIndex(const QModelIndex& index)
508 {
509 return index.isValid() && (index.column() == KDirModel::Name);
510 }
511
512 #include "dolphindetailsview.moc"