]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
allow zooming in and zooming out in the details view
[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
35 DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) :
36 QTreeView(parent),
37 m_controller(controller)
38 {
39 Q_ASSERT(controller != 0);
40
41 setAcceptDrops(true);
42 setRootIsDecorated(false);
43 setSortingEnabled(true);
44 setUniformRowHeights(true);
45
46 const ViewProperties props(controller->url());
47 setSortIndicatorSection(props.sorting());
48 setSortIndicatorOrder(props.sortOrder());
49
50 connect(header(), SIGNAL(sectionClicked(int)),
51 this, SLOT(synchronizeSortingState(int)));
52
53 connect(parent, SIGNAL(sortingChanged(DolphinView::Sorting)),
54 this, SLOT(setSortIndicatorSection(DolphinView::Sorting)));
55 connect(parent, SIGNAL(sortOrderChanged(Qt::SortOrder)),
56 this, SLOT(setSortIndicatorOrder(Qt::SortOrder)));
57
58 connect(this, SIGNAL(clicked(const QModelIndex&)),
59 controller, SLOT(triggerItem(const QModelIndex&)));
60
61 connect(controller, SIGNAL(zoomIn()),
62 this, SLOT(zoomIn()));
63 connect(controller, SIGNAL(zoomOut()),
64 this, SLOT(zoomOut()));
65
66 // apply the details mode settings to the widget
67 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
68 Q_ASSERT(settings != 0);
69
70 m_viewOptions = QTreeView::viewOptions();
71 m_viewOptions.font = QFont(settings->fontFamily(), settings->fontSize());
72 updateDecorationSize();
73
74 KFileItemDelegate* delegate = new KFileItemDelegate(parent);
75 setItemDelegate(delegate);
76 }
77
78 DolphinDetailsView::~DolphinDetailsView()
79 {
80 }
81
82 bool DolphinDetailsView::event(QEvent* event)
83 {
84 if (event->type() == QEvent::Polish) {
85 // Assure that by respecting the available width that:
86 // - the 'Name' column is stretched as large as possible
87 // - the remaining columns are as small as possible
88 QHeaderView* headerView = header();
89 headerView->setStretchLastSection(false);
90 headerView->setResizeMode(QHeaderView::ResizeToContents);
91 headerView->setResizeMode(0, QHeaderView::Stretch);
92
93 // hide columns if this is indicated by the settings
94 const DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
95 Q_ASSERT(settings != 0);
96 if (!settings->showDate()) {
97 hideColumn(KDirModel::ModifiedTime);
98 }
99
100 if (!settings->showPermissions()) {
101 hideColumn(KDirModel::Permissions);
102 }
103
104 if (!settings->showOwner()) {
105 hideColumn(KDirModel::Owner);
106 }
107
108 if (!settings->showGroup()) {
109 hideColumn(KDirModel::Group);
110 }
111 }
112
113 return QTreeView::event(event);
114 }
115 QStyleOptionViewItem DolphinDetailsView::viewOptions() const
116 {
117 return m_viewOptions;
118 }
119
120 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event)
121 {
122 QTreeView::contextMenuEvent(event);
123 m_controller->triggerContextMenuRequest(event->pos());
124 }
125
126 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event)
127 {
128 QTreeView::mouseReleaseEvent(event);
129 m_controller->triggerActivation();
130 }
131
132 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event)
133 {
134 if (event->mimeData()->hasUrls()) {
135 event->acceptProposedAction();
136 }
137 }
138
139 void DolphinDetailsView::dropEvent(QDropEvent* event)
140 {
141 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
142 if (urls.isEmpty() || (event->source() == this)) {
143 QTreeView::dropEvent(event);
144 }
145 else {
146 event->acceptProposedAction();
147 m_controller->indicateDroppedUrls(urls, event->pos());
148 }
149 }
150
151 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting)
152 {
153 QHeaderView* headerView = header();
154 headerView->setSortIndicator(sorting, headerView->sortIndicatorOrder());
155 }
156
157 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder)
158 {
159 QHeaderView* headerView = header();
160 headerView->setSortIndicator(headerView->sortIndicatorSection(), sortOrder);
161 }
162
163 void DolphinDetailsView::synchronizeSortingState(int column)
164 {
165 // The sorting has already been changed in QTreeView if this slot is
166 // invoked, but Dolphin is not informed about this.
167 DolphinView::Sorting sorting = DolphinSortFilterProxyModel::sortingForColumn(column);
168 const Qt::SortOrder sortOrder = header()->sortIndicatorOrder();
169 m_controller->indicateSortingChange(sorting);
170 m_controller->indicateSortOrderChange(sortOrder);
171 }
172
173 void DolphinDetailsView::zoomIn()
174 {
175 if (isZoomInPossible()) {
176 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
177 // TODO: get rid of K3Icon sizes
178 switch (settings->iconSize()) {
179 case K3Icon::SizeSmall: settings->setIconSize(K3Icon::SizeMedium); break;
180 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeLarge); break;
181 default: Q_ASSERT(false); break;
182 }
183 updateDecorationSize();
184 }
185 }
186
187 void DolphinDetailsView::zoomOut()
188 {
189 if (isZoomOutPossible()) {
190 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
191 // TODO: get rid of K3Icon sizes
192 switch (settings->iconSize()) {
193 case K3Icon::SizeLarge: settings->setIconSize(K3Icon::SizeMedium); break;
194 case K3Icon::SizeMedium: settings->setIconSize(K3Icon::SizeSmall); break;
195 default: Q_ASSERT(false); break;
196 }
197 updateDecorationSize();
198 }
199 }
200
201 bool DolphinDetailsView::isZoomInPossible() const
202 {
203 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
204 return settings->iconSize() < K3Icon::SizeLarge;
205 }
206
207 bool DolphinDetailsView::isZoomOutPossible() const
208 {
209 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
210 return settings->iconSize() > K3Icon::SizeSmall;
211 }
212
213 void DolphinDetailsView::updateDecorationSize()
214 {
215 DetailsModeSettings* settings = DolphinSettings::instance().detailsModeSettings();
216 const int iconSize = settings->iconSize();
217 m_viewOptions.decorationSize = QSize(iconSize, iconSize);
218
219 m_controller->setZoomInPossible(isZoomInPossible());
220 m_controller->setZoomOutPossible(isZoomOutPossible());
221
222 doItemsLayout();
223 }
224
225 #include "dolphindetailsview.moc"