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