]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
When the view is the Details or Columns one do not have in count lessThanGeneralPurpo...
[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->setResizeMode(0, QHeaderView::Stretch);
121
122 // hide columns if this is indicated by the settings
123 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
124 Q_ASSERT(settings != 0);
125 if (!settings->showDate()) {
126 hideColumn(DolphinModel::ModifiedTime);
127 }
128
129 if (!settings->showPermissions()) {
130 hideColumn(DolphinModel::Permissions);
131 }
132
133 if (!settings->showOwner()) {
134 hideColumn(DolphinModel::Owner);
135 }
136
137 if (!settings->showGroup()) {
138 hideColumn(DolphinModel::Group);
139 }
140
141 if (!settings->showType()) {
142 hideColumn(DolphinModel::Type);
143 }
144 }
145
146 return QTreeView::event(event);
147 }
148
149 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
150 {
151 return m_viewOptions;
152 }
153
154 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
155 {
156 QTreeView::contextMenuEvent(event);
157 m_controller->triggerContextMenuRequest(event->pos());
158 }
159
160 void DolphinDetailsView::mousePressEvent(QMouseEvent* event)
161 {
162 m_controller->triggerActivation();
163
164 QTreeView::mousePressEvent(event);
165
166 const QModelIndex index = indexAt(event->pos());
167 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
168 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
169 if (!(modifier & Qt::ShiftModifier) && !(modifier & Qt::ControlModifier)) {
170 clearSelection();
171 }
172 }
173
174 if (event->button() == Qt::LeftButton) {
175 m_showElasticBand = true;
176
177 const QPoint pos(contentsPos());
178 m_elasticBandOrigin = event->pos();
179 m_elasticBandOrigin.setX(m_elasticBandOrigin.x() + pos.x());
180 m_elasticBandOrigin.setY(m_elasticBandOrigin.y() + pos.y());
181 m_elasticBandDestination = event->pos();
182 }
183 }
184
185 void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event)
186 {
187 QTreeView::mouseMoveEvent(event);
188 if (m_showElasticBand) {
189 updateElasticBand();
190 }
191 }
192
193 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
194 {
195 QTreeView::mouseReleaseEvent(event);
196 if (m_showElasticBand) {
197 updateElasticBand();
198 m_showElasticBand = false;
199 }
200 }
201
202 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
203 {
204 if (event->mimeData()->hasUrls()) {
205 event->acceptProposedAction();
206 }
207
208 if (m_showElasticBand) {
209 updateElasticBand();
210 m_showElasticBand = false;
211 }
212 m_dragging = true;
213 }
214
215 void DolphinDetailsView::dragLeaveEvent(QDragLeaveEvent* event)
216 {
217 QTreeView::dragLeaveEvent(event);
218
219 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
220 m_dragging = false;
221 setDirtyRegion(m_dropRect);
222 }
223
224 void DolphinDetailsView::dragMoveEvent(QDragMoveEvent* event)
225 {
226 QTreeView::dragMoveEvent(event);
227
228 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
229 setDirtyRegion(m_dropRect);
230 const QModelIndex index = indexAt(event->pos());
231 if (!index.isValid() || (index.column() != DolphinModel::Name)) {
232 m_dragging = false;
233 } else {
234 m_dragging = true;
235 m_dropRect = visualRect(index);
236 setDirtyRegion(m_dropRect);
237 }
238 }
239
240 void DolphinDetailsView::dropEvent(QDropEvent* event)
241 {
242 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
243 if (!urls.isEmpty()) {
244 event->acceptProposedAction();
245 m_controller->indicateDroppedUrls(urls,
246 m_controller->url(),
247 indexAt(event->pos()),
248 event->source());
249 }
250 QTreeView::dropEvent(event);
251 m_dragging = false;
252 }
253
254 void DolphinDetailsView::paintEvent(QPaintEvent* event)
255 {
256 QTreeView::paintEvent(event);
257 if (m_showElasticBand) {
258 // The following code has been taken from QListView
259 // and adapted to DolphinDetailsView.
260 // (C) 1992-2007 Trolltech ASA
261 QStyleOptionRubberBand opt;
262 opt.initFrom(this);
263 opt.shape = QRubberBand::Rectangle;
264 opt.opaque = false;
265 opt.rect = elasticBandRect();
266
267 QPainter painter(viewport());
268 painter.save();
269 style()->drawControl(QStyle::CE_RubberBand, &opt, &painter);
270 painter.restore();
271 }
272
273 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
274 if (m_dragging) {
275 const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
276 DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
277 }
278 }
279
280 void DolphinDetailsView::keyPressEvent(QKeyEvent* event)
281 {
282 QTreeView::keyPressEvent(event);
283
284 const QItemSelectionModel* selModel = selectionModel();
285 const QModelIndex currentIndex = selModel->currentIndex();
286 const bool triggerItem = currentIndex.isValid()
287 && (event->key() == Qt::Key_Return)
288 && (selModel->selectedIndexes().count() <= 1);
289 if (triggerItem) {
290 m_controller->triggerItem(currentIndex);
291 }
292 }
293
294 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
295 {
296 QHeaderView* headerView = header();
297 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
298 }
299
300 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
301 {
302 QHeaderView* headerView = header();
303 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
304 }
305
306 void DolphinDetailsView::synchronizeSortingState(int column)
307 {
308 // The sorting has already been changed in QTreeView if this slot is
309 // invoked, but Dolphin is not informed about this.
310 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
311 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
312 m_controller->indicateSortingChange(sorting);
313 m_controller->indicateSortOrderChange(sortOrder);
314 }
315
316 void DolphinDetailsView::slotEntered(const QModelIndex& index)
317 {
318 const QPoint pos = viewport()->mapFromGlobal(QCursor::pos());
319 const int nameColumnWidth = header()->sectionSize(DolphinModel::Name);
320 if (pos.x() < nameColumnWidth) {
321 m_controller->emitItemEntered(index);
322 }
323 else {
324 m_controller->emitViewportEntered();
325 }
326 }
327
328 void DolphinDetailsView::updateElasticBand()
329 {
330 Q_ASSERT(m_showElasticBand);
331 QRect dirtyRegion(elasticBandRect());
332 m_elasticBandDestination = viewport()->mapFromGlobal(QCursor::pos());
333 dirtyRegion = dirtyRegion.united(elasticBandRect());
334 setDirtyRegion(dirtyRegion);
335 }
336
337 void DolphinDetailsView::zoomIn()
338 {
339 if (isZoomInPossible()) {
340 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
341 // TODO: get rid of K3Icon sizes
342 switch (settings->iconSize()) {
343 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
344 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
345 default: Q_ASSERT(false); break;
346 }
347 updateDecorationSize();
348 }
349 }
350
351 void DolphinDetailsView::zoomOut()
352 {
353 if (isZoomOutPossible()) {
354 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
355 // TODO: get rid of K3Icon sizes
356 switch (settings->iconSize()) {
357 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
358 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
359 default: Q_ASSERT(false); break;
360 }
361 updateDecorationSize();
362 }
363 }
364
365 bool DolphinDetailsView::isZoomInPossible() const
366 {
367 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
368 return settings->iconSize() < K3Icon::SizeLarge;
369 }
370
371 bool DolphinDetailsView::isZoomOutPossible() const
372 {
373 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
374 return settings->iconSize() > K3Icon::SizeSmall;
375 }
376
377 void DolphinDetailsView::updateDecorationSize()
378 {
379 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
380 const int iconSize = settings->iconSize();
381 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
382
383 m_controller->setZoomInPossible(isZoomInPossible());
384 m_controller->setZoomOutPossible(isZoomOutPossible());
385
386 doItemsLayout();
387 }
388
389 QPoint DolphinDetailsView::contentsPos() const
390 {
391 // implementation note: the horizonal position is ignored currently, as no
392 // horizontal scrolling is done anyway during a selection
393 const QScrollBar* scrollbar = verticalScrollBar();
394 Q_ASSERT(scrollbar != 0);
395
396 const int maxHeight = maximumViewportSize().height();
397 const int height = scrollbar->maximum() - scrollbar->minimum() + 1;
398 const int visibleHeight = model()->rowCount() + 1 - height;
399 if (visibleHeight <= 0) {
400 return QPoint(0, 0);
401 }
402
403 const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight;
404 return QPoint(0, y);
405 }
406
407 QRect DolphinDetailsView::elasticBandRect() const
408 {
409 const QPoint pos(contentsPos());
410 const QPoint topLeft(m_elasticBandOrigin.x() - pos.x(), m_elasticBandOrigin.y() - pos.y());
411 return QRect(topLeft, m_elasticBandDestination).normalized();
412 }
413
414 static bool isValidNameIndex(const QModelIndex& index)
415 {
416 return index.isValid() && (index.column() == KDirModel::Name);
417 }
418
419 void DolphinDetailsView::slotItemActivated(const QModelIndex& index)
420 {
421 if (!isValidNameIndex(index)) {
422 clearSelection();
423 m_controller->emitItemEntered(index);
424 } else {
425 m_controller->triggerItem(index);
426 }
427 }
428
429 #include "dolphindetailsview.moc"