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