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
,
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 mouseReleaseEvent(QMouseEvent
* event
);
66 virtual void paintEvent(QPaintEvent
* event
);
67 virtual void contextMenuEvent(QContextMenuEvent
* event
);
70 /** Used by ColumnWidget::setActive(). */
73 /** Used by ColumnWidget::setActive(). */
78 DolphinColumnView
* m_view
;
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
,
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_view
->m_controller
->indicateDroppedUrls(urls
,
190 indexAt(event
->pos()),
193 QListView::dropEvent(event
);
197 void ColumnWidget::mouseReleaseEvent(QMouseEvent
* event
)
199 m_view
->requestActivation(this);
200 QListView::mouseReleaseEvent(event
);
203 void ColumnWidget::paintEvent(QPaintEvent
* event
)
205 QListView::paintEvent(event
);
207 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
209 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
210 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
214 void ColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
216 m_view
->requestActivation(this);
218 QListView::contextMenuEvent(event
);
220 const QModelIndex index
= indexAt(event
->pos());
221 if (index
.isValid() || m_active
) {
222 // Only open a context menu above an item or if the mouse is above
223 // the active column.
224 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
225 m_view
->m_controller
->triggerContextMenuRequest(pos
);
229 void ColumnWidget::activate()
231 const QColor bgColor
= KColorScheme(KColorScheme::View
).background();
232 QPalette palette
= viewport()->palette();
233 palette
.setColor(viewport()->backgroundRole(), bgColor
);
234 viewport()->setPalette(palette
);
236 setSelectionMode(MultiSelection
);
240 void ColumnWidget::deactivate()
242 QColor bgColor
= KColorScheme(KColorScheme::View
).background();
243 const QColor fgColor
= KColorScheme(KColorScheme::View
).foreground();
244 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
246 QPalette palette
= viewport()->palette();
247 palette
.setColor(viewport()->backgroundRole(), bgColor
);
248 viewport()->setPalette(palette
);
250 setSelectionMode(SingleSelection
);
256 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
258 m_controller(controller
)
260 Q_ASSERT(controller
!= 0);
262 setAcceptDrops(true);
263 setSelectionBehavior(SelectItems
);
264 setDragDropMode(QAbstractItemView::DragDrop
);
265 setDropIndicatorShown(false);
267 if (KGlobalSettings::singleClick()) {
268 connect(this, SIGNAL(clicked(const QModelIndex
&)),
269 this, SLOT(triggerItem(const QModelIndex
&)));
271 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
272 this, SLOT(triggerItem(const QModelIndex
&)));
274 connect(this, SIGNAL(entered(const QModelIndex
&)),
275 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
276 connect(this, SIGNAL(viewportEntered()),
277 controller
, SLOT(emitViewportEntered()));
278 connect(controller
, SIGNAL(zoomIn()),
279 this, SLOT(zoomIn()));
280 connect(controller
, SIGNAL(zoomOut()),
281 this, SLOT(zoomOut()));
282 connect(controller
, SIGNAL(urlChanged(const KUrl
&)),
283 this, SLOT(updateColumnsState(const KUrl
&)));
285 updateDecorationSize();
288 DolphinColumnView::~DolphinColumnView()
292 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
294 // let the column widget be aware about its URL...
296 if (viewport()->children().count() == 0) {
297 // For the first column widget the directory lister has not been started
298 // yet, hence use the URL from the controller instead.
299 columnUrl
= m_controller
->url();
301 const QAbstractProxyModel
* proxyModel
= static_cast<const QAbstractProxyModel
*>(model());
302 const KDirModel
* dirModel
= static_cast<const KDirModel
*>(proxyModel
->sourceModel());
304 const QModelIndex dirModelIndex
= proxyModel
->mapToSource(index
);
305 KFileItem
* fileItem
= dirModel
->itemForIndex(dirModelIndex
);
307 columnUrl
= fileItem
->url();
311 ColumnWidget
* view
= new ColumnWidget(viewport(), this, columnUrl
);
313 // The following code has been copied 1:1 from QColumnView::createColumn().
314 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
315 // QColumnView::initializeColumn() will be available for this.
317 view
->setFrameShape(QFrame::NoFrame
);
318 view
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
319 view
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
320 view
->setMinimumWidth(100);
321 view
->setAttribute(Qt::WA_MacShowFocusRect
, false);
323 // copy the 'view' behavior
324 view
->setDragDropMode(dragDropMode());
325 view
->setDragDropOverwriteMode(dragDropOverwriteMode());
326 view
->setDropIndicatorShown(showDropIndicator());
327 view
->setAlternatingRowColors(alternatingRowColors());
328 view
->setAutoScroll(hasAutoScroll());
329 view
->setEditTriggers(editTriggers());
330 view
->setHorizontalScrollMode(horizontalScrollMode());
331 view
->setIconSize(iconSize());
332 view
->setSelectionBehavior(selectionBehavior());
333 view
->setSelectionMode(selectionMode());
334 view
->setTabKeyNavigation(tabKeyNavigation());
335 view
->setTextElideMode(textElideMode());
336 view
->setVerticalScrollMode(verticalScrollMode());
338 view
->setModel(model());
340 // set the delegate to be the columnview delegate
341 QAbstractItemDelegate
* delegate
= view
->itemDelegate();
342 view
->setItemDelegate(itemDelegate());
345 view
->setRootIndex(index
);
347 if (model()->canFetchMore(index
)) {
348 model()->fetchMore(index
);
354 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
356 m_controller
->triggerActivation();
357 QColumnView::mousePressEvent(event
);
360 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
362 if (event
->mimeData()->hasUrls()) {
363 event
->acceptProposedAction();
367 void DolphinColumnView::dropEvent(QDropEvent
* event
)
369 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
370 if (!urls
.isEmpty()) {
371 m_controller
->indicateDroppedUrls(urls
,
372 indexAt(event
->pos()),
374 event
->acceptProposedAction();
376 QColumnView::dropEvent(event
);
379 void DolphinColumnView::zoomIn()
381 if (isZoomInPossible()) {
382 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
383 // TODO: get rid of K3Icon sizes
384 switch (settings
->iconSize()) {
385 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
386 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
387 default: Q_ASSERT(false); break;
389 updateDecorationSize();
393 void DolphinColumnView::zoomOut()
395 if (isZoomOutPossible()) {
396 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
397 // TODO: get rid of K3Icon sizes
398 switch (settings
->iconSize()) {
399 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
400 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
401 default: Q_ASSERT(false); break;
403 updateDecorationSize();
407 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
409 m_controller
->triggerItem(index
);
410 updateColumnsState(m_controller
->url());
413 void DolphinColumnView::updateColumnsState(const KUrl
& url
)
415 foreach (QObject
* object
, viewport()->children()) {
416 if (object
->inherits("QListView")) {
417 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
418 widget
->setActive(widget
->url() == url
);
423 bool DolphinColumnView::isZoomInPossible() const
425 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
426 return settings
->iconSize() < K3Icon::SizeLarge
;
429 bool DolphinColumnView::isZoomOutPossible() const
431 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
432 return settings
->iconSize() > K3Icon::SizeSmall
;
435 void DolphinColumnView::requestActivation(QWidget
* column
)
437 foreach (QObject
* object
, viewport()->children()) {
438 if (object
->inherits("QListView")) {
439 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
440 const bool isActive
= (widget
== column
);
441 widget
->setActive(isActive
);
443 m_controller
->setUrl(widget
->url());
449 void DolphinColumnView::updateDecorationSize()
451 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
452 const int iconSize
= settings
->iconSize();
454 foreach (QObject
* object
, viewport()->children()) {
455 if (object
->inherits("QListView")) {
456 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
457 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
461 m_controller
->setZoomInPossible(isZoomInPossible());
462 m_controller
->setZoomOutPossible(isZoomOutPossible());
467 #include "dolphincolumnview.moc"