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 mousePressEvent(QMouseEvent
* event
);
66 virtual void paintEvent(QPaintEvent
* event
);
69 /** Used by ColumnWidget::setActive(). */
72 /** Used by ColumnWidget::setActive(). */
78 DolphinColumnView
* m_columnView
;
79 QStyleOptionViewItem m_viewOptions
;
81 bool m_dragging
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
82 QRect m_dropRect
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
85 ColumnWidget::ColumnWidget(QWidget
* parent
,
86 DolphinColumnView
* columnView
,
91 m_columnView(columnView
),
96 setDragDropMode(QAbstractItemView::DragDrop
);
97 setDropIndicatorShown(false);
99 setMouseTracking(true);
100 viewport()->setAttribute(Qt::WA_Hover
);
102 // apply the column mode settings to the widget
103 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
104 Q_ASSERT(settings
!= 0);
106 m_viewOptions
= QListView::viewOptions();
108 QFont
font(settings
->fontFamily(), settings
->fontSize());
109 font
.setItalic(settings
->italicFont());
110 font
.setBold(settings
->boldFont());
111 m_viewOptions
.font
= font
;
113 const int iconSize
= settings
->iconSize();
114 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
119 ColumnWidget::~ColumnWidget()
123 void ColumnWidget::setDecorationSize(const QSize
& size
)
125 m_viewOptions
.decorationSize
= size
;
129 void ColumnWidget::setActive(bool active
)
131 if (m_active
== active
) {
144 const KUrl
& ColumnWidget::url() const
149 QStyleOptionViewItem
ColumnWidget::viewOptions() const
151 return m_viewOptions
;
154 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
156 if (event
->mimeData()->hasUrls()) {
157 event
->acceptProposedAction();
163 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
165 QListView::dragLeaveEvent(event
);
167 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
169 setDirtyRegion(m_dropRect
);
172 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
174 QListView::dragMoveEvent(event
);
176 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
177 const QModelIndex index
= indexAt(event
->pos());
178 setDirtyRegion(m_dropRect
);
179 m_dropRect
= visualRect(index
);
180 setDirtyRegion(m_dropRect
);
183 void ColumnWidget::dropEvent(QDropEvent
* event
)
185 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
186 if (!urls
.isEmpty()) {
187 event
->acceptProposedAction();
188 m_columnView
->m_controller
->indicateDroppedUrls(urls
,
189 indexAt(event
->pos()),
192 QListView::dropEvent(event
);
196 void ColumnWidget::mousePressEvent(QMouseEvent
* event
)
198 if (m_active
|| indexAt(event
->pos()).isValid()) {
199 // Only accept the mouse press event in inactive views,
200 // if a click is done on an item. This assures that
201 // the current selection, which usually shows the
202 // the directory for next column, won't get deleted.
203 QListView::mousePressEvent(event
);
207 void ColumnWidget::paintEvent(QPaintEvent
* event
)
209 QListView::paintEvent(event
);
211 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
213 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
214 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
218 void ColumnWidget::activate()
220 const QColor bgColor
= KColorScheme(KColorScheme::View
).background();
221 QPalette palette
= viewport()->palette();
222 palette
.setColor(viewport()->backgroundRole(), bgColor
);
223 viewport()->setPalette(palette
);
225 setSelectionMode(MultiSelection
);
228 void ColumnWidget::deactivate()
230 QColor bgColor
= KColorScheme(KColorScheme::View
).background();
231 const QColor fgColor
= KColorScheme(KColorScheme::View
).foreground();
232 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
234 QPalette palette
= viewport()->palette();
235 palette
.setColor(viewport()->backgroundRole(), bgColor
);
236 viewport()->setPalette(palette
);
238 setSelectionMode(SingleSelection
);
243 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
245 m_controller(controller
)
247 Q_ASSERT(controller
!= 0);
249 setAcceptDrops(true);
250 setSelectionBehavior(SelectItems
);
251 setDragDropMode(QAbstractItemView::DragDrop
);
252 setDropIndicatorShown(false);
254 if (KGlobalSettings::singleClick()) {
255 connect(this, SIGNAL(clicked(const QModelIndex
&)),
256 this, SLOT(triggerItem(const QModelIndex
&)));
258 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
259 this, SLOT(triggerItem(const QModelIndex
&)));
261 connect(this, SIGNAL(activated(const QModelIndex
&)),
262 this, SLOT(triggerItem(const QModelIndex
&)));
263 connect(this, SIGNAL(entered(const QModelIndex
&)),
264 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
265 connect(this, SIGNAL(viewportEntered()),
266 controller
, SLOT(emitViewportEntered()));
267 connect(controller
, SIGNAL(zoomIn()),
268 this, SLOT(zoomIn()));
269 connect(controller
, SIGNAL(zoomOut()),
270 this, SLOT(zoomOut()));
272 updateDecorationSize();
275 DolphinColumnView::~DolphinColumnView()
279 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
281 // To be able to visually indicate whether a column is active (which means
282 // that it represents the content of the URL navigator), the column
283 // must remember its URL.
284 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
285 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
287 const QModelIndex dirModelIndex
= proxyModel
->mapToSource(index
);
288 KFileItem
* fileItem
= dirModel
->itemForIndex(dirModelIndex
);
292 columnUrl
= fileItem
->url();
295 ColumnWidget
* view
= new ColumnWidget(viewport(),
299 // The following code has been copied 1:1 from QColumnView::createColumn().
300 // Copyright (C) 1992-2007 Trolltech ASA.
301 // It would be nice if QColumnView would offer a protected method for this
302 // (already send input to Benjamin, hopefully possible in Qt4.4)
304 view
->setFrameShape(QFrame::NoFrame
);
305 view
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
306 view
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
307 view
->setMinimumWidth(100);
308 view
->setAttribute(Qt::WA_MacShowFocusRect
, false);
310 // copy the 'view' behavior
311 view
->setDragDropMode(dragDropMode());
312 view
->setDragDropOverwriteMode(dragDropOverwriteMode());
313 view
->setDropIndicatorShown(showDropIndicator());
314 view
->setAlternatingRowColors(alternatingRowColors());
315 view
->setAutoScroll(hasAutoScroll());
316 view
->setEditTriggers(editTriggers());
317 view
->setHorizontalScrollMode(horizontalScrollMode());
318 view
->setIconSize(iconSize());
319 view
->setSelectionBehavior(selectionBehavior());
320 view
->setSelectionMode(selectionMode());
321 view
->setTabKeyNavigation(tabKeyNavigation());
322 view
->setTextElideMode(textElideMode());
323 view
->setVerticalScrollMode(verticalScrollMode());
325 view
->setModel(model());
327 // set the delegate to be the columnview delegate
328 QAbstractItemDelegate
* delegate
= view
->itemDelegate();
329 view
->setItemDelegate(itemDelegate());
332 view
->setRootIndex(index
);
334 if (model()->canFetchMore(index
)) {
335 model()->fetchMore(index
);
341 void DolphinColumnView::contextMenuEvent(QContextMenuEvent
* event
)
343 QColumnView::contextMenuEvent(event
);
344 m_controller
->triggerContextMenuRequest(event
->pos());
347 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
349 m_controller
->triggerActivation();
350 QColumnView::mousePressEvent(event
);
353 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
355 if (event
->mimeData()->hasUrls()) {
356 event
->acceptProposedAction();
360 void DolphinColumnView::dropEvent(QDropEvent
* event
)
362 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
363 if (!urls
.isEmpty()) {
364 m_controller
->indicateDroppedUrls(urls
,
365 indexAt(event
->pos()),
367 event
->acceptProposedAction();
369 QColumnView::dropEvent(event
);
372 void DolphinColumnView::zoomIn()
374 if (isZoomInPossible()) {
375 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
376 // TODO: get rid of K3Icon sizes
377 switch (settings
->iconSize()) {
378 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
379 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
380 default: Q_ASSERT(false); break;
382 updateDecorationSize();
386 void DolphinColumnView::zoomOut()
388 if (isZoomOutPossible()) {
389 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
390 // TODO: get rid of K3Icon sizes
391 switch (settings
->iconSize()) {
392 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
393 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
394 default: Q_ASSERT(false); break;
396 updateDecorationSize();
400 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
402 m_controller
->triggerItem(index
);
404 // Update the activation state of all columns. Only the column
405 // which represents the URL of the URL navigator is marked as active.
406 const KUrl
& navigatorUrl
= m_controller
->url();
407 foreach (QObject
* object
, viewport()->children()) {
408 if (object
->inherits("QListView")) {
409 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
410 widget
->setActive(navigatorUrl
== widget
->url());
415 bool DolphinColumnView::isZoomInPossible() const
417 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
418 return settings
->iconSize() < K3Icon::SizeLarge
;
421 bool DolphinColumnView::isZoomOutPossible() const
423 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
424 return settings
->iconSize() > K3Icon::SizeSmall
;
427 void DolphinColumnView::updateDecorationSize()
429 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
430 const int iconSize
= settings
->iconSize();
432 foreach (QObject
* object
, viewport()->children()) {
433 if (object
->inherits("QListView")) {
434 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
435 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
439 m_controller
->setZoomInPossible(isZoomInPossible());
440 m_controller
->setZoomOutPossible(isZoomOutPossible());
445 #include "dolphincolumnview.moc"