]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphindetailsview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
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. *
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. *
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 ***************************************************************************/
21 #include "dolphindetailsview.h"
23 #include "dolphincontroller.h"
24 #include "dolphinsettings.h"
25 #include "dolphinsortfilterproxymodel.h"
26 #include "viewproperties.h"
28 #include "dolphin_detailsmodesettings.h"
30 #include <kdirmodel.h>
31 #include <kfileitemdelegate.h>
33 #include <QHeaderView>
34 #include <QRubberBand>
37 DolphinDetailsView::DolphinDetailsView(QWidget
* parent
, DolphinController
* controller
) :
39 m_controller(controller
),
43 Q_ASSERT(controller
!= 0);
46 setRootIsDecorated(false);
47 setSortingEnabled(true);
48 setUniformRowHeights(true);
49 setSelectionBehavior(SelectItems
);
50 setDragDropMode(QAbstractItemView::DragDrop
);
51 setDropIndicatorShown(false);
53 setMouseTracking(true);
54 viewport()->setAttribute(Qt::WA_Hover
);
56 const ViewProperties
props(controller
->url());
57 setSortIndicatorSection(props
.sorting());
58 setSortIndicatorOrder(props
.sortOrder());
60 connect(header(), SIGNAL(sectionClicked(int)),
61 this, SLOT(synchronizeSortingState(int)));
63 connect(parent
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
64 this, SLOT(setSortIndicatorSection(DolphinView::Sorting
)));
65 connect(parent
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
66 this, SLOT(setSortIndicatorOrder(Qt::SortOrder
)));
68 if (KGlobalSettings::singleClick()) {
69 connect(this, SIGNAL(clicked(const QModelIndex
&)),
70 controller
, SLOT(triggerItem(const QModelIndex
&)));
72 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
73 controller
, SLOT(triggerItem(const QModelIndex
&)));
75 connect(this, SIGNAL(activated(const QModelIndex
&)),
76 controller
, SLOT(triggerItem(const QModelIndex
&)));
77 connect(this, SIGNAL(entered(const QModelIndex
&)),
78 this, SLOT(slotEntered(const QModelIndex
&)));
79 connect(this, SIGNAL(viewportEntered()),
80 controller
, SLOT(emitViewportEntered()));
81 connect(controller
, SIGNAL(zoomIn()),
82 this, SLOT(zoomIn()));
83 connect(controller
, SIGNAL(zoomOut()),
84 this, SLOT(zoomOut()));
86 // apply the details mode settings to the widget
87 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
88 Q_ASSERT(settings
!= 0);
90 m_viewOptions
= QTreeView::viewOptions();
92 QFont
font(settings
->fontFamily(), settings
->fontSize());
93 font
.setItalic(settings
->italicFont());
94 font
.setBold(settings
->boldFont());
95 m_viewOptions
.font
= font
;
97 updateDecorationSize();
100 DolphinDetailsView::~DolphinDetailsView()
104 bool DolphinDetailsView::event(QEvent
* event
)
106 if (event
->type() == QEvent::Polish
) {
107 // Assure that by respecting the available width that:
108 // - the 'Name' column is stretched as large as possible
109 // - the remaining columns are as small as possible
110 QHeaderView
* headerView
= header();
111 headerView
->setStretchLastSection(false);
112 headerView
->setResizeMode(QHeaderView::ResizeToContents
);
113 headerView
->setResizeMode(0, QHeaderView::Stretch
);
115 // hide columns if this is indicated by the settings
116 const DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
117 Q_ASSERT(settings
!= 0);
118 if (!settings
->showDate()) {
119 hideColumn(KDirModel::ModifiedTime
);
122 if (!settings
->showPermissions()) {
123 hideColumn(KDirModel::Permissions
);
126 if (!settings
->showOwner()) {
127 hideColumn(KDirModel::Owner
);
130 if (!settings
->showGroup()) {
131 hideColumn(KDirModel::Group
);
134 if (!settings
->showType()) {
135 hideColumn(KDirModel::Type
);
139 return QTreeView::event(event
);
142 QStyleOptionViewItem
DolphinDetailsView::viewOptions() const
144 return m_viewOptions
;
147 void DolphinDetailsView::contextMenuEvent(QContextMenuEvent
* event
)
149 QTreeView::contextMenuEvent(event
);
150 m_controller
->triggerContextMenuRequest(event
->pos());
153 void DolphinDetailsView::mousePressEvent(QMouseEvent
* event
)
155 QTreeView::mousePressEvent(event
);
157 if (event
->button() == Qt::LeftButton
) {
158 // initialize rubberband for the selection
159 if (m_rubberBand
== 0) {
160 m_rubberBand
= new QRubberBand(QRubberBand::Rectangle
, viewport());
163 const QPoint
pos(contentsPos());
164 m_origin
= event
->pos();
165 m_origin
.setX(m_origin
.x() + pos
.x());
166 m_origin
.setY(m_origin
.y() + pos
.y());
167 updateRubberBandGeometry();
168 m_rubberBand
->show();
172 void DolphinDetailsView::mouseMoveEvent(QMouseEvent
* event
)
174 QTreeView::mouseMoveEvent(event
);
175 if (m_rubberBand
!= 0) {
176 updateRubberBandGeometry();
180 void DolphinDetailsView::mouseReleaseEvent(QMouseEvent
* event
)
182 QTreeView::mouseReleaseEvent(event
);
183 if (m_rubberBand
!= 0) {
184 m_rubberBand
->hide();
186 m_controller
->triggerActivation();
189 void DolphinDetailsView::dragEnterEvent(QDragEnterEvent
* event
)
191 if (event
->mimeData()->hasUrls()) {
192 event
->acceptProposedAction();
194 if (m_rubberBand
!= 0) {
195 m_rubberBand
->hide();
199 void DolphinDetailsView::dropEvent(QDropEvent
* event
)
201 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
202 if (!urls
.isEmpty()) {
203 event
->acceptProposedAction();
204 m_controller
->indicateDroppedUrls(urls
,
205 indexAt(event
->pos()),
208 QTreeView::dropEvent(event
);
211 void DolphinDetailsView::setSortIndicatorSection(DolphinView::Sorting sorting
)
213 QHeaderView
* headerView
= header();
214 headerView
->setSortIndicator(sorting
, headerView
->sortIndicatorOrder());
217 void DolphinDetailsView::setSortIndicatorOrder(Qt::SortOrder sortOrder
)
219 QHeaderView
* headerView
= header();
220 headerView
->setSortIndicator(headerView
->sortIndicatorSection(), sortOrder
);
223 void DolphinDetailsView::synchronizeSortingState(int column
)
225 // The sorting has already been changed in QTreeView if this slot is
226 // invoked, but Dolphin is not informed about this.
227 DolphinView::Sorting sorting
= DolphinSortFilterProxyModel::sortingForColumn(column
);
228 const Qt::SortOrder sortOrder
= header()->sortIndicatorOrder();
229 m_controller
->indicateSortingChange(sorting
);
230 m_controller
->indicateSortOrderChange(sortOrder
);
233 void DolphinDetailsView::slotEntered(const QModelIndex
& index
)
235 const QPoint pos
= viewport()->mapFromGlobal(QCursor::pos());
236 const int nameColumnWidth
= header()->sectionSize(KDirModel::Name
);
237 if (pos
.x() < nameColumnWidth
) {
238 m_controller
->emitItemEntered(index
);
241 m_controller
->emitViewportEntered();
245 void DolphinDetailsView::updateRubberBandGeometry()
247 if (m_rubberBand
!= 0) {
248 const QPoint
pos(contentsPos());
249 const QPoint
origin(m_origin
.x() - pos
.x(), m_origin
.y() - pos
.y());
250 const QPoint
dest(viewport()->mapFromGlobal(QCursor::pos()));
251 m_rubberBand
->setGeometry(QRect(origin
, dest
).normalized());
255 void DolphinDetailsView::zoomIn()
257 if (isZoomInPossible()) {
258 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
259 // TODO: get rid of K3Icon sizes
260 switch (settings
->iconSize()) {
261 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
262 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
263 default: Q_ASSERT(false); break;
265 updateDecorationSize();
269 void DolphinDetailsView::zoomOut()
271 if (isZoomOutPossible()) {
272 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
273 // TODO: get rid of K3Icon sizes
274 switch (settings
->iconSize()) {
275 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
276 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
277 default: Q_ASSERT(false); break;
279 updateDecorationSize();
283 bool DolphinDetailsView::isZoomInPossible() const
285 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
286 return settings
->iconSize() < K3Icon::SizeLarge
;
289 bool DolphinDetailsView::isZoomOutPossible() const
291 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
292 return settings
->iconSize() > K3Icon::SizeSmall
;
295 void DolphinDetailsView::updateDecorationSize()
297 DetailsModeSettings
* settings
= DolphinSettings::instance().detailsModeSettings();
298 const int iconSize
= settings
->iconSize();
299 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
301 m_controller
->setZoomInPossible(isZoomInPossible());
302 m_controller
->setZoomOutPossible(isZoomOutPossible());
307 QPoint
DolphinDetailsView::contentsPos() const
309 // implementation note: the horizonal position is ignored currently, as no
310 // horizontal scrolling is done anyway during a selection
311 const QScrollBar
* scrollbar
= verticalScrollBar();
312 Q_ASSERT(scrollbar
!= 0);
314 const int maxHeight
= maximumViewportSize().height();
315 const int height
= scrollbar
->maximum() - scrollbar
->minimum() + 1;
316 const int visibleHeight
= model()->rowCount() + 1 - height
;
317 if (visibleHeight
<= 0) {
321 const int y
= scrollbar
->sliderPosition() * maxHeight
/ visibleHeight
;
325 #include "dolphindetailsview.moc"