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