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