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
);
67 virtual void contextMenuEvent(QContextMenuEvent
* event
);
70 /** Used by ColumnWidget::setActive(). */
73 /** Used by ColumnWidget::setActive(). */
79 DolphinColumnView
* m_columnView
;
80 QStyleOptionViewItem m_viewOptions
;
82 bool m_dragging
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
83 QRect m_dropRect
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
86 ColumnWidget::ColumnWidget(QWidget
* parent
,
87 DolphinColumnView
* columnView
,
92 m_columnView(columnView
),
97 setDragDropMode(QAbstractItemView::DragDrop
);
98 setDropIndicatorShown(false);
100 setMouseTracking(true);
101 viewport()->setAttribute(Qt::WA_Hover
);
103 // apply the column mode settings to the widget
104 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
105 Q_ASSERT(settings
!= 0);
107 m_viewOptions
= QListView::viewOptions();
109 QFont
font(settings
->fontFamily(), settings
->fontSize());
110 font
.setItalic(settings
->italicFont());
111 font
.setBold(settings
->boldFont());
112 m_viewOptions
.font
= font
;
114 const int iconSize
= settings
->iconSize();
115 m_viewOptions
.decorationSize
= QSize(iconSize
, iconSize
);
120 ColumnWidget::~ColumnWidget()
124 void ColumnWidget::setDecorationSize(const QSize
& size
)
126 m_viewOptions
.decorationSize
= size
;
130 void ColumnWidget::setActive(bool active
)
132 if (m_active
== active
) {
145 const KUrl
& ColumnWidget::url() const
150 QStyleOptionViewItem
ColumnWidget::viewOptions() const
152 return m_viewOptions
;
155 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
157 if (event
->mimeData()->hasUrls()) {
158 event
->acceptProposedAction();
164 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
166 QListView::dragLeaveEvent(event
);
168 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
170 setDirtyRegion(m_dropRect
);
173 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
175 QListView::dragMoveEvent(event
);
177 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
178 const QModelIndex index
= indexAt(event
->pos());
179 setDirtyRegion(m_dropRect
);
180 m_dropRect
= visualRect(index
);
181 setDirtyRegion(m_dropRect
);
184 void ColumnWidget::dropEvent(QDropEvent
* event
)
186 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
187 if (!urls
.isEmpty()) {
188 event
->acceptProposedAction();
189 m_columnView
->m_controller
->indicateDroppedUrls(urls
,
190 indexAt(event
->pos()),
193 QListView::dropEvent(event
);
197 void ColumnWidget::mousePressEvent(QMouseEvent
* event
)
199 if (m_active
|| indexAt(event
->pos()).isValid()) {
200 // Only accept the mouse press event in inactive views,
201 // if a click is done on an item. This assures that
202 // the current selection, which usually shows the
203 // the directory for next column, won't get deleted.
204 QListView::mousePressEvent(event
);
208 void ColumnWidget::paintEvent(QPaintEvent
* event
)
210 QListView::paintEvent(event
);
212 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
214 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
215 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
219 void ColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
221 QListView::contextMenuEvent(event
);
222 m_columnView
->m_controller
->triggerContextMenuRequest(event
->pos(), m_url
);
225 void ColumnWidget::activate()
227 const QColor bgColor
= KColorScheme(KColorScheme::View
).background();
228 QPalette palette
= viewport()->palette();
229 palette
.setColor(viewport()->backgroundRole(), bgColor
);
230 viewport()->setPalette(palette
);
232 setSelectionMode(MultiSelection
);
235 void ColumnWidget::deactivate()
237 QColor bgColor
= KColorScheme(KColorScheme::View
).background();
238 const QColor fgColor
= KColorScheme(KColorScheme::View
).foreground();
239 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
241 QPalette palette
= viewport()->palette();
242 palette
.setColor(viewport()->backgroundRole(), bgColor
);
243 viewport()->setPalette(palette
);
245 setSelectionMode(SingleSelection
);
250 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
252 m_controller(controller
)
254 Q_ASSERT(controller
!= 0);
256 setAcceptDrops(true);
257 setSelectionBehavior(SelectItems
);
258 setDragDropMode(QAbstractItemView::DragDrop
);
259 setDropIndicatorShown(false);
261 if (KGlobalSettings::singleClick()) {
262 connect(this, SIGNAL(clicked(const QModelIndex
&)),
263 this, SLOT(triggerItem(const QModelIndex
&)));
265 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
266 this, SLOT(triggerItem(const QModelIndex
&)));
268 connect(this, SIGNAL(activated(const QModelIndex
&)),
269 this, SLOT(triggerItem(const QModelIndex
&)));
270 connect(this, SIGNAL(entered(const QModelIndex
&)),
271 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
272 connect(this, SIGNAL(viewportEntered()),
273 controller
, SLOT(emitViewportEntered()));
274 connect(controller
, SIGNAL(zoomIn()),
275 this, SLOT(zoomIn()));
276 connect(controller
, SIGNAL(zoomOut()),
277 this, SLOT(zoomOut()));
279 updateDecorationSize();
282 DolphinColumnView::~DolphinColumnView()
286 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
288 // To be able to visually indicate whether a column is active (which means
289 // that it represents the content of the URL navigator), the column
290 // must remember its URL.
291 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
292 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
294 const QModelIndex dirModelIndex
= proxyModel
->mapToSource(index
);
295 KFileItem
* fileItem
= dirModel
->itemForIndex(dirModelIndex
);
299 columnUrl
= fileItem
->url();
302 ColumnWidget
* view
= new ColumnWidget(viewport(),
306 // The following code has been copied 1:1 from QColumnView::createColumn().
307 // Copyright (C) 1992-2007 Trolltech ASA.
308 // It would be nice if QColumnView would offer a protected method for this
309 // (already send input to Benjamin, hopefully possible in Qt4.4)
311 view
->setFrameShape(QFrame::NoFrame
);
312 view
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
313 view
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
314 view
->setMinimumWidth(100);
315 view
->setAttribute(Qt::WA_MacShowFocusRect
, false);
317 // copy the 'view' behavior
318 view
->setDragDropMode(dragDropMode());
319 view
->setDragDropOverwriteMode(dragDropOverwriteMode());
320 view
->setDropIndicatorShown(showDropIndicator());
321 view
->setAlternatingRowColors(alternatingRowColors());
322 view
->setAutoScroll(hasAutoScroll());
323 view
->setEditTriggers(editTriggers());
324 view
->setHorizontalScrollMode(horizontalScrollMode());
325 view
->setIconSize(iconSize());
326 view
->setSelectionBehavior(selectionBehavior());
327 view
->setSelectionMode(selectionMode());
328 view
->setTabKeyNavigation(tabKeyNavigation());
329 view
->setTextElideMode(textElideMode());
330 view
->setVerticalScrollMode(verticalScrollMode());
332 view
->setModel(model());
334 // set the delegate to be the columnview delegate
335 QAbstractItemDelegate
* delegate
= view
->itemDelegate();
336 view
->setItemDelegate(itemDelegate());
339 view
->setRootIndex(index
);
341 if (model()->canFetchMore(index
)) {
342 model()->fetchMore(index
);
348 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
350 m_controller
->triggerActivation();
351 QColumnView::mousePressEvent(event
);
354 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
356 if (event
->mimeData()->hasUrls()) {
357 event
->acceptProposedAction();
361 void DolphinColumnView::dropEvent(QDropEvent
* event
)
363 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
364 if (!urls
.isEmpty()) {
365 m_controller
->indicateDroppedUrls(urls
,
366 indexAt(event
->pos()),
368 event
->acceptProposedAction();
370 QColumnView::dropEvent(event
);
373 void DolphinColumnView::zoomIn()
375 if (isZoomInPossible()) {
376 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
377 // TODO: get rid of K3Icon sizes
378 switch (settings
->iconSize()) {
379 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
380 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
381 default: Q_ASSERT(false); break;
383 updateDecorationSize();
387 void DolphinColumnView::zoomOut()
389 if (isZoomOutPossible()) {
390 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
391 // TODO: get rid of K3Icon sizes
392 switch (settings
->iconSize()) {
393 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
394 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
395 default: Q_ASSERT(false); break;
397 updateDecorationSize();
401 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
403 m_controller
->triggerItem(index
);
405 // Update the activation state of all columns. Only the column
406 // which represents the URL of the URL navigator is marked as active.
407 const KUrl
& navigatorUrl
= m_controller
->url();
408 foreach (QObject
* object
, viewport()->children()) {
409 if (object
->inherits("QListView")) {
410 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
411 widget
->setActive(navigatorUrl
== widget
->url());
416 bool DolphinColumnView::isZoomInPossible() const
418 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
419 return settings
->iconSize() < K3Icon::SizeLarge
;
422 bool DolphinColumnView::isZoomOutPossible() const
424 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
425 return settings
->iconSize() > K3Icon::SizeSmall
;
428 void DolphinColumnView::updateDecorationSize()
430 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
431 const int iconSize
= settings
->iconSize();
433 foreach (QObject
* object
, viewport()->children()) {
434 if (object
->inherits("QListView")) {
435 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
436 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
440 m_controller
->setZoomInPossible(isZoomInPossible());
441 m_controller
->setZoomOutPossible(isZoomOutPossible());
446 #include "dolphincolumnview.moc"