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