1 /***************************************************************************
2 * Copyright (C) 2007 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 <kdirlister.h>
30 #include <kdirmodel.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
, DolphinColumnView
* columnView
);
43 virtual ~ColumnWidget();
45 /** Sets the size of the icons. */
46 void setDecorationSize(const QSize
& size
);
49 * An active column is defined as column, which shows the same URL
50 * as indicated by the URL navigator. The active column is usually
51 * drawn in a lighter color. All operations are applied to this column.
53 void setActive(bool active
);
56 virtual QStyleOptionViewItem
viewOptions() const;
57 virtual void dragEnterEvent(QDragEnterEvent
* event
);
58 virtual void dragLeaveEvent(QDragLeaveEvent
* event
);
59 virtual void dragMoveEvent(QDragMoveEvent
* event
);
60 virtual void dropEvent(QDropEvent
* event
);
61 virtual void mousePressEvent(QMouseEvent
* event
);
62 virtual void paintEvent(QPaintEvent
* event
);
63 virtual void contextMenuEvent(QContextMenuEvent
* event
);
66 /** Used by ColumnWidget::setActive(). */
69 /** Used by ColumnWidget::setActive(). */
74 DolphinColumnView
* m_view
;
75 QStyleOptionViewItem m_viewOptions
;
77 bool m_dragging
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
78 QRect m_dropRect
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
81 ColumnWidget::ColumnWidget(QWidget
* parent
,
82 DolphinColumnView
* columnView
) :
90 setDragDropMode(QAbstractItemView::DragDrop
);
91 setDropIndicatorShown(false);
93 setMouseTracking(true);
94 viewport()->setAttribute(Qt::WA_Hover
);
96 // apply the column mode settings to the widget
97 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
98 Q_ASSERT(settings
!= 0);
100 m_viewOptions
= QListView::viewOptions();
102 QFont
font(settings
->fontFamily(), settings
->fontSize());
103 font
.setItalic(settings
->italicFont());
104 font
.setBold(settings
->boldFont());
105 m_viewOptions
.font
= font
;
107 const int iconSize
= settings
->iconSize();
108 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
113 ColumnWidget::~ColumnWidget()
117 void ColumnWidget::setDecorationSize(const QSize
& size
)
119 m_viewOptions
.decorationSize
= size
;
123 void ColumnWidget::setActive(bool active
)
125 if (m_active
== active
) {
138 QStyleOptionViewItem
ColumnWidget::viewOptions() const
140 return m_viewOptions
;
143 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
145 if (event
->mimeData()->hasUrls()) {
146 event
->acceptProposedAction();
152 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
154 QListView::dragLeaveEvent(event
);
156 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
158 setDirtyRegion(m_dropRect
);
161 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
163 QListView::dragMoveEvent(event
);
165 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
166 const QModelIndex index
= indexAt(event
->pos());
167 setDirtyRegion(m_dropRect
);
168 m_dropRect
= visualRect(index
);
169 setDirtyRegion(m_dropRect
);
172 void ColumnWidget::dropEvent(QDropEvent
* event
)
174 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
175 if (!urls
.isEmpty()) {
176 event
->acceptProposedAction();
177 m_view
->m_controller
->indicateDroppedUrls(urls
,
178 indexAt(event
->pos()),
181 QListView::dropEvent(event
);
185 void ColumnWidget::mousePressEvent(QMouseEvent
* event
)
187 m_view
->requestActivation(this);
188 QListView::mousePressEvent(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
);
202 void ColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
204 QListView::contextMenuEvent(event
);
206 const QModelIndex index
= indexAt(event
->pos());
207 if (index
.isValid() || m_active
) {
208 // Only open a context menu above an item or if the mouse is above
209 // the active column.
210 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
211 m_view
->m_controller
->triggerContextMenuRequest(pos
);
215 void ColumnWidget::activate()
217 const QColor bgColor
= KColorScheme(KColorScheme::View
).background();
218 QPalette palette
= viewport()->palette();
219 palette
.setColor(viewport()->backgroundRole(), bgColor
);
220 viewport()->setPalette(palette
);
222 setSelectionMode(MultiSelection
);
225 void ColumnWidget::deactivate()
227 QColor bgColor
= KColorScheme(KColorScheme::View
).background();
228 const QColor fgColor
= KColorScheme(KColorScheme::View
).foreground();
229 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
231 QPalette palette
= viewport()->palette();
232 palette
.setColor(viewport()->backgroundRole(), bgColor
);
233 viewport()->setPalette(palette
);
235 setSelectionMode(SingleSelection
);
240 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
242 m_controller(controller
)
244 Q_ASSERT(controller
!= 0);
246 setAcceptDrops(true);
247 setSelectionBehavior(SelectItems
);
248 setDragDropMode(QAbstractItemView::DragDrop
);
249 setDropIndicatorShown(false);
251 if (KGlobalSettings::singleClick()) {
252 connect(this, SIGNAL(clicked(const QModelIndex
&)),
253 this, SLOT(triggerItem(const QModelIndex
&)));
255 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
256 this, SLOT(triggerItem(const QModelIndex
&)));
258 connect(this, SIGNAL(entered(const QModelIndex
&)),
259 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
260 connect(this, SIGNAL(viewportEntered()),
261 controller
, SLOT(emitViewportEntered()));
262 connect(controller
, SIGNAL(zoomIn()),
263 this, SLOT(zoomIn()));
264 connect(controller
, SIGNAL(zoomOut()),
265 this, SLOT(zoomOut()));
266 connect(controller
, SIGNAL(urlChanged(const KUrl
&)),
267 this, SLOT(updateColumnsState(const KUrl
&)));
269 updateDecorationSize();
272 DolphinColumnView::~DolphinColumnView()
276 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
278 ColumnWidget
* view
= new ColumnWidget(viewport(), this);
280 // The following code has been copied 1:1 from QColumnView::createColumn().
281 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
282 // QColumnView::initializeColumn() will be available for this.
284 view
->setFrameShape(QFrame::NoFrame
);
285 view
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
286 view
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
287 view
->setMinimumWidth(100);
288 view
->setAttribute(Qt::WA_MacShowFocusRect
, false);
290 // copy the 'view' behavior
291 view
->setDragDropMode(dragDropMode());
292 view
->setDragDropOverwriteMode(dragDropOverwriteMode());
293 view
->setDropIndicatorShown(showDropIndicator());
294 view
->setAlternatingRowColors(alternatingRowColors());
295 view
->setAutoScroll(hasAutoScroll());
296 view
->setEditTriggers(editTriggers());
297 view
->setHorizontalScrollMode(horizontalScrollMode());
298 view
->setIconSize(iconSize());
299 view
->setSelectionBehavior(selectionBehavior());
300 view
->setSelectionMode(selectionMode());
301 view
->setTabKeyNavigation(tabKeyNavigation());
302 view
->setTextElideMode(textElideMode());
303 view
->setVerticalScrollMode(verticalScrollMode());
305 view
->setModel(model());
307 // set the delegate to be the columnview delegate
308 QAbstractItemDelegate
* delegate
= view
->itemDelegate();
309 view
->setItemDelegate(itemDelegate());
312 view
->setRootIndex(index
);
314 if (model()->canFetchMore(index
)) {
315 model()->fetchMore(index
);
321 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
323 m_controller
->triggerActivation();
324 QColumnView::mousePressEvent(event
);
327 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
329 if (event
->mimeData()->hasUrls()) {
330 event
->acceptProposedAction();
334 void DolphinColumnView::dropEvent(QDropEvent
* event
)
336 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
337 if (!urls
.isEmpty()) {
338 m_controller
->indicateDroppedUrls(urls
,
339 indexAt(event
->pos()),
341 event
->acceptProposedAction();
343 QColumnView::dropEvent(event
);
346 void DolphinColumnView::zoomIn()
348 if (isZoomInPossible()) {
349 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
350 // TODO: get rid of K3Icon sizes
351 switch (settings
->iconSize()) {
352 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
353 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
354 default: Q_ASSERT(false); break;
356 updateDecorationSize();
360 void DolphinColumnView::zoomOut()
362 if (isZoomOutPossible()) {
363 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
364 // TODO: get rid of K3Icon sizes
365 switch (settings
->iconSize()) {
366 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
367 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
368 default: Q_ASSERT(false); break;
370 updateDecorationSize();
374 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
376 m_controller
->triggerItem(index
);
377 updateColumnsState(m_controller
->url());
380 void DolphinColumnView::updateColumnsState(const KUrl
& url
)
382 const KUrl baseUrl
= dirLister()->url();
383 const int activeIndex
= url
.path().count('/') - baseUrl
.path().count('/');
386 foreach (QObject
* object
, viewport()->children()) {
387 if (object
->inherits("QListView")) {
388 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
389 widget
->setActive(index
== activeIndex
);
395 bool DolphinColumnView::isZoomInPossible() const
397 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
398 return settings
->iconSize() < K3Icon::SizeLarge
;
401 bool DolphinColumnView::isZoomOutPossible() const
403 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
404 return settings
->iconSize() > K3Icon::SizeSmall
;
407 void DolphinColumnView::requestActivation(QWidget
* column
)
409 KUrl::List dirs
= dirLister()->directories();
410 KUrl::List::const_iterator it
= dirs
.constBegin();
411 foreach (QObject
* object
, viewport()->children()) {
412 if (object
->inherits("QListView")) {
413 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
414 const bool isActive
= (widget
== column
);
415 widget
->setActive(isActive
);
417 m_controller
->setUrl(*it
);
424 void DolphinColumnView::updateDecorationSize()
426 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
427 const int iconSize
= settings
->iconSize();
429 foreach (QObject
* object
, viewport()->children()) {
430 if (object
->inherits("QListView")) {
431 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
432 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
436 m_controller
->setZoomInPossible(isZoomInPossible());
437 m_controller
->setZoomOutPossible(isZoomOutPossible());
442 KDirLister
* DolphinColumnView::dirLister() const
444 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
445 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
446 return dirModel
->dirLister();
449 #include "dolphincolumnview.moc"