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