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