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