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