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 <kcolorutils.h>
28 #include <kcolorscheme.h>
29 #include <kdirmodel.h>
30 #include <kfileitem.h>
32 #include <QAbstractProxyModel>
36 * Represents one column inside the DolphinColumnView and has been
37 * extended to respect view options and hovering information.
39 class ColumnWidget
: public QListView
42 ColumnWidget(QWidget
* parent
,
43 DolphinColumnView
* columnView
,
45 virtual ~ColumnWidget();
47 /** Sets the size of the icons. */
48 void setDecorationSize(const QSize
& size
);
51 * An active column is defined as column, which shows the same URL
52 * as indicated by the URL navigator. The active column is usually
53 * drawn in a lighter color. All operations are applied to this column.
55 void setActive(bool active
);
57 inline const KUrl
& url() const;
60 virtual QStyleOptionViewItem
viewOptions() const;
61 virtual void dragEnterEvent(QDragEnterEvent
* event
);
62 virtual void dragLeaveEvent(QDragLeaveEvent
* event
);
63 virtual void dragMoveEvent(QDragMoveEvent
* event
);
64 virtual void dropEvent(QDropEvent
* event
);
65 virtual void paintEvent(QPaintEvent
* event
);
70 DolphinColumnView
* m_columnView
;
71 QStyleOptionViewItem m_viewOptions
;
73 bool m_dragging
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
74 QRect m_dropRect
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
77 ColumnWidget::ColumnWidget(QWidget
* parent
,
78 DolphinColumnView
* columnView
,
83 m_columnView(columnView
),
88 setSelectionBehavior(SelectItems
);
89 setDragDropMode(QAbstractItemView::DragDrop
);
90 setDropIndicatorShown(false);
92 setMouseTracking(true);
93 viewport()->setAttribute(Qt::WA_Hover
);
95 // apply the column mode settings to the widget
96 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
97 Q_ASSERT(settings
!= 0);
99 m_viewOptions
= QListView::viewOptions();
101 QFont
font(settings
->fontFamily(), settings
->fontSize());
102 font
.setItalic(settings
->italicFont());
103 font
.setBold(settings
->boldFont());
104 m_viewOptions
.font
= font
;
106 const int iconSize
= settings
->iconSize();
107 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
110 ColumnWidget::~ColumnWidget()
114 void ColumnWidget::setDecorationSize(const QSize
& size
)
116 m_viewOptions
.decorationSize
= size
;
120 void ColumnWidget::setActive(bool active
)
122 if (m_active
== active
) {
128 QColor bgColor
= KColorScheme(KColorScheme::View
).background();
130 const QColor fgColor
= KColorScheme(KColorScheme::View
).foreground();
131 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
134 QPalette palette
= viewport()->palette();
135 palette
.setColor(viewport()->backgroundRole(), bgColor
);
136 viewport()->setPalette(palette
);
139 const KUrl
& ColumnWidget::url() const
144 QStyleOptionViewItem
ColumnWidget::viewOptions() const
146 return m_viewOptions
;
149 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
151 if (event
->mimeData()->hasUrls()) {
152 event
->acceptProposedAction();
158 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
160 QListView::dragLeaveEvent(event
);
162 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
164 setDirtyRegion(m_dropRect
);
167 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
169 QListView::dragMoveEvent(event
);
171 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
172 const QModelIndex index
= indexAt(event
->pos());
173 setDirtyRegion(m_dropRect
);
174 m_dropRect
= visualRect(index
);
175 setDirtyRegion(m_dropRect
);
178 void ColumnWidget::dropEvent(QDropEvent
* event
)
180 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
181 if (!urls
.isEmpty()) {
182 event
->acceptProposedAction();
183 m_columnView
->m_controller
->indicateDroppedUrls(urls
,
184 indexAt(event
->pos()),
187 QListView::dropEvent(event
);
191 void ColumnWidget::paintEvent(QPaintEvent
* event
)
193 QListView::paintEvent(event
);
195 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
197 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
198 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
204 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
206 m_controller(controller
)
208 Q_ASSERT(controller
!= 0);
210 setAcceptDrops(true);
211 setSelectionBehavior(SelectItems
);
212 setDragDropMode(QAbstractItemView::DragDrop
);
213 setDropIndicatorShown(false);
215 if (KGlobalSettings::singleClick()) {
216 connect(this, SIGNAL(clicked(const QModelIndex
&)),
217 this, SLOT(triggerItem(const QModelIndex
&)));
219 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
220 this, SLOT(triggerItem(const QModelIndex
&)));
222 connect(this, SIGNAL(activated(const QModelIndex
&)),
223 this, SLOT(triggerItem(const QModelIndex
&)));
224 connect(this, SIGNAL(entered(const QModelIndex
&)),
225 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
226 connect(this, SIGNAL(viewportEntered()),
227 controller
, SLOT(emitViewportEntered()));
228 connect(controller
, SIGNAL(zoomIn()),
229 this, SLOT(zoomIn()));
230 connect(controller
, SIGNAL(zoomOut()),
231 this, SLOT(zoomOut()));
233 updateDecorationSize();
236 DolphinColumnView::~DolphinColumnView()
240 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
242 // To be able to visually indicate whether a column is active (which means
243 // that it represents the content of the URL navigator), the column
244 // must remember its URL.
245 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
246 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
248 const QModelIndex dirModelIndex
= proxyModel
->mapToSource(index
);
249 KFileItem
* fileItem
= dirModel
->itemForIndex(dirModelIndex
);
253 columnUrl
= fileItem
->url();
256 ColumnWidget
* view
= new ColumnWidget(viewport(),
260 // The following code has been copied 1:1 from QColumnView::createColumn().
261 // Copyright (C) 1992-2007 Trolltech ASA.
262 // It would be nice if QColumnView would offer a protected method for this
263 // (already send input to Benjamin, hopefully possible in Qt4.4)
265 view
->setFrameShape(QFrame::NoFrame
);
266 view
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
267 view
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
268 view
->setMinimumWidth(100);
269 view
->setAttribute(Qt::WA_MacShowFocusRect
, false);
271 // copy the 'view' behavior
272 view
->setDragDropMode(dragDropMode());
273 view
->setDragDropOverwriteMode(dragDropOverwriteMode());
274 view
->setDropIndicatorShown(showDropIndicator());
275 view
->setAlternatingRowColors(alternatingRowColors());
276 view
->setAutoScroll(hasAutoScroll());
277 view
->setEditTriggers(editTriggers());
278 view
->setHorizontalScrollMode(horizontalScrollMode());
279 view
->setIconSize(iconSize());
280 view
->setSelectionBehavior(selectionBehavior());
281 view
->setSelectionMode(selectionMode());
282 view
->setTabKeyNavigation(tabKeyNavigation());
283 view
->setTextElideMode(textElideMode());
284 view
->setVerticalScrollMode(verticalScrollMode());
286 view
->setModel(model());
288 // set the delegate to be the columnview delegate
289 QAbstractItemDelegate
* delegate
= view
->itemDelegate();
290 view
->setItemDelegate(itemDelegate());
293 view
->setRootIndex(index
);
295 if (model()->canFetchMore(index
)) {
296 model()->fetchMore(index
);
302 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
304 QColumnView::contextMenuEvent(event
);
305 m_controller
->triggerContextMenuRequest(event
->pos());
308 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
310 m_controller
->triggerActivation();
311 QColumnView::mousePressEvent(event
);
314 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
316 if (event
->mimeData()->hasUrls()) {
317 event
->acceptProposedAction();
321 void DolphinColumnView::dropEvent(QDropEvent
* event
)
323 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
324 if (!urls
.isEmpty()) {
325 m_controller
->indicateDroppedUrls(urls
,
326 indexAt(event
->pos()),
328 event
->acceptProposedAction();
330 QColumnView::dropEvent(event
);
333 void DolphinColumnView::zoomIn()
335 if (isZoomInPossible()) {
336 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
337 // TODO: get rid of K3Icon sizes
338 switch (settings
->iconSize()) {
339 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
340 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
341 default: Q_ASSERT(false); break;
343 updateDecorationSize();
347 void DolphinColumnView::zoomOut()
349 if (isZoomOutPossible()) {
350 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
351 // TODO: get rid of K3Icon sizes
352 switch (settings
->iconSize()) {
353 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
354 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
355 default: Q_ASSERT(false); break;
357 updateDecorationSize();
361 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
363 m_controller
->triggerItem(index
);
365 // Update the activation state of all columns. Only the column
366 // which represents the URL of the URL navigator is marked as active.
367 const KUrl
& navigatorUrl
= m_controller
->url();
368 foreach (QObject
* object
, viewport()->children()) {
369 if (object
->inherits("QListView")) {
370 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
371 widget
->setActive(navigatorUrl
== widget
->url());
376 bool DolphinColumnView::isZoomInPossible() const
378 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
379 return settings
->iconSize() < K3Icon::SizeLarge
;
382 bool DolphinColumnView::isZoomOutPossible() const
384 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
385 return settings
->iconSize() > K3Icon::SizeSmall
;
388 void DolphinColumnView::updateDecorationSize()
390 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
391 const int iconSize
= settings
->iconSize();
393 foreach (QObject
* object
, viewport()->children()) {
394 if (object
->inherits("QListView")) {
395 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
396 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
400 m_controller
->setZoomInPossible(isZoomInPossible());
401 m_controller
->setZoomOutPossible(isZoomOutPossible());
406 #include "dolphincolumnview.moc"