1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "dolphincolumnview.h"
22 #include "dolphincontroller.h"
23 #include "dolphinsettings.h"
25 #include "dolphin_columnmodesettings.h"
27 #include <QtGui/QAbstractProxyModel>
28 #include <QtCore/QPoint>
31 * Represents one column inside the DolphinColumnView and has been
32 * extended to respect view options and hovering information.
34 class ColumnWidget
: public QListView
37 ColumnWidget(QWidget
* parent
, DolphinColumnView
* columnView
);
38 virtual ~ColumnWidget();
40 void setDecorationSize(const QSize
& size
);
43 virtual QStyleOptionViewItem
viewOptions() const;
44 virtual void dragEnterEvent(QDragEnterEvent
* event
);
45 virtual void dragLeaveEvent(QDragLeaveEvent
* event
);
46 virtual void dragMoveEvent(QDragMoveEvent
* event
);
47 virtual void dropEvent(QDropEvent
* event
);
48 virtual void paintEvent(QPaintEvent
* event
);
51 DolphinColumnView
* m_columnView
;
52 QStyleOptionViewItem m_viewOptions
;
54 bool m_dragging
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
55 QRect m_dropRect
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
58 ColumnWidget::ColumnWidget(QWidget
* parent
, DolphinColumnView
* columnView
) :
60 m_columnView(columnView
),
65 setSelectionBehavior(SelectItems
);
66 setDragDropMode(QAbstractItemView::DragDrop
);
67 setDropIndicatorShown(false);
69 setMouseTracking(true);
70 viewport()->setAttribute(Qt::WA_Hover
);
72 // apply the column mode settings to the widget
73 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
74 Q_ASSERT(settings
!= 0);
76 m_viewOptions
= QListView::viewOptions();
78 QFont
font(settings
->fontFamily(), settings
->fontSize());
79 font
.setItalic(settings
->italicFont());
80 font
.setBold(settings
->boldFont());
81 m_viewOptions
.font
= font
;
84 ColumnWidget::~ColumnWidget()
88 void ColumnWidget::setDecorationSize(const QSize
& size
)
90 m_viewOptions
.decorationSize
= size
;
94 QStyleOptionViewItem
ColumnWidget::viewOptions() const
99 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
101 if (event
->mimeData()->hasUrls()) {
102 event
->acceptProposedAction();
108 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
110 QListView::dragLeaveEvent(event
);
112 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
114 setDirtyRegion(m_dropRect
);
117 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
119 QListView::dragMoveEvent(event
);
121 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
122 const QModelIndex index
= indexAt(event
->pos());
123 setDirtyRegion(m_dropRect
);
124 m_dropRect
= visualRect(index
);
125 setDirtyRegion(m_dropRect
);
128 void ColumnWidget::dropEvent(QDropEvent
* event
)
130 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
131 if (!urls
.isEmpty()) {
132 event
->acceptProposedAction();
133 m_columnView
->m_controller
->indicateDroppedUrls(urls
,
134 indexAt(event
->pos()),
137 QListView::dropEvent(event
);
141 void ColumnWidget::paintEvent(QPaintEvent
* event
)
143 QListView::paintEvent(event
);
145 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
147 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
148 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
154 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
156 m_controller(controller
)
158 Q_ASSERT(controller
!= 0);
160 setAcceptDrops(true);
161 setSelectionBehavior(SelectItems
);
162 setDragDropMode(QAbstractItemView::DragDrop
);
163 setDropIndicatorShown(false);
165 if (KGlobalSettings::singleClick()) {
166 connect(this, SIGNAL(clicked(const QModelIndex
&)),
167 controller
, SLOT(triggerItem(const QModelIndex
&)));
169 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
170 controller
, SLOT(triggerItem(const QModelIndex
&)));
172 connect(this, SIGNAL(activated(const QModelIndex
&)),
173 controller
, SLOT(triggerItem(const QModelIndex
&)));
174 connect(this, SIGNAL(entered(const QModelIndex
&)),
175 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
176 connect(this, SIGNAL(viewportEntered()),
177 controller
, SLOT(emitViewportEntered()));
178 connect(controller
, SIGNAL(zoomIn()),
179 this, SLOT(zoomIn()));
180 connect(controller
, SIGNAL(zoomOut()),
181 this, SLOT(zoomOut()));
183 updateDecorationSize();
186 DolphinColumnView::~DolphinColumnView()
190 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
192 ColumnWidget
* view
= new ColumnWidget(viewport(), this);
194 // The following code has been copied 1:1 from QColumnView::createColumn().
195 // Copyright (C) 1992-2007 Trolltech ASA.
196 // It would be nice if QColumnView would offer a protected method for this
197 // (already send input to Benjamin, hopefully possible in Qt4.4)
199 view
->setFrameShape(QFrame::NoFrame
);
200 view
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
201 view
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
202 view
->setMinimumWidth(100);
203 view
->setAttribute(Qt::WA_MacShowFocusRect
, false);
205 // copy the 'view' behavior
206 view
->setDragDropMode(dragDropMode());
207 view
->setDragDropOverwriteMode(dragDropOverwriteMode());
208 view
->setDropIndicatorShown(showDropIndicator());
209 view
->setAlternatingRowColors(alternatingRowColors());
210 view
->setAutoScroll(hasAutoScroll());
211 view
->setEditTriggers(editTriggers());
212 view
->setHorizontalScrollMode(horizontalScrollMode());
213 view
->setIconSize(iconSize());
214 view
->setSelectionBehavior(selectionBehavior());
215 view
->setSelectionMode(selectionMode());
216 view
->setTabKeyNavigation(tabKeyNavigation());
217 view
->setTextElideMode(textElideMode());
218 view
->setVerticalScrollMode(verticalScrollMode());
220 view
->setModel(model());
222 // set the delegate to be the columnview delegate
223 QAbstractItemDelegate
*delegate
= view
->itemDelegate();
224 view
->setItemDelegate(itemDelegate());
227 view
->setRootIndex(index
);
229 if (model()->canFetchMore(index
)) {
230 model()->fetchMore(index
);
236 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
238 QColumnView::contextMenuEvent(event
);
239 m_controller
->triggerContextMenuRequest(event
->pos());
242 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
244 m_controller
->triggerActivation();
245 QColumnView::mousePressEvent(event
);
248 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
250 if (event
->mimeData()->hasUrls()) {
251 event
->acceptProposedAction();
255 void DolphinColumnView::dropEvent(QDropEvent
* event
)
257 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
258 if (!urls
.isEmpty()) {
259 m_controller
->indicateDroppedUrls(urls
,
260 indexAt(event
->pos()),
262 event
->acceptProposedAction();
264 QColumnView::dropEvent(event
);
267 void DolphinColumnView::zoomIn()
269 if (isZoomInPossible()) {
270 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
271 // TODO: get rid of K3Icon sizes
272 switch (settings
->iconSize()) {
273 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
274 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
275 default: Q_ASSERT(false); break;
277 updateDecorationSize();
281 void DolphinColumnView::zoomOut()
283 if (isZoomOutPossible()) {
284 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
285 // TODO: get rid of K3Icon sizes
286 switch (settings
->iconSize()) {
287 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
288 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
289 default: Q_ASSERT(false); break;
291 updateDecorationSize();
295 bool DolphinColumnView::isZoomInPossible() const
297 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
298 return settings
->iconSize() < K3Icon::SizeLarge
;
301 bool DolphinColumnView::isZoomOutPossible() const
303 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
304 return settings
->iconSize() > K3Icon::SizeSmall
;
307 void DolphinColumnView::updateDecorationSize()
309 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
310 const int iconSize
= settings
->iconSize();
312 foreach (QObject
* object
, viewport()->children()) {
313 if (object
->inherits("QListView")) {
314 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
315 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
319 m_controller
->setZoomInPossible(isZoomInPossible());
320 m_controller
->setZoomOutPossible(isZoomOutPossible());
325 #include "dolphincolumnview.moc"