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