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