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>
33 * Represents one column inside the DolphinColumnView and has been
34 * extended to respect view options and hovering information.
36 class ColumnWidget
: public QListView
39 ColumnWidget(QWidget
* parent
, DolphinColumnView
* columnView
);
40 virtual ~ColumnWidget();
42 /** Sets the size of the icons. */
43 void setDecorationSize(const QSize
& size
);
46 * An active column is defined as column, which shows the same URL
47 * as indicated by the URL navigator. The active column is usually
48 * drawn in a lighter color. All operations are applied to this column.
50 void setActive(bool active
);
53 virtual QStyleOptionViewItem
viewOptions() const;
54 virtual void dragEnterEvent(QDragEnterEvent
* event
);
55 virtual void dragLeaveEvent(QDragLeaveEvent
* event
);
56 virtual void dragMoveEvent(QDragMoveEvent
* event
);
57 virtual void dropEvent(QDropEvent
* event
);
58 virtual void mousePressEvent(QMouseEvent
* event
);
59 virtual void paintEvent(QPaintEvent
* event
);
60 virtual void contextMenuEvent(QContextMenuEvent
* event
);
63 /** Used by ColumnWidget::setActive(). */
66 /** Used by ColumnWidget::setActive(). */
71 DolphinColumnView
* m_view
;
72 QStyleOptionViewItem m_viewOptions
;
74 bool m_dragging
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
75 QRect m_dropRect
; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
78 ColumnWidget::ColumnWidget(QWidget
* parent
,
79 DolphinColumnView
* columnView
) :
87 setDragDropMode(QAbstractItemView::DragDrop
);
88 setDropIndicatorShown(false);
90 setMouseTracking(true);
91 viewport()->setAttribute(Qt::WA_Hover
);
93 // apply the column mode settings to the widget
94 const ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
95 Q_ASSERT(settings
!= 0);
97 m_viewOptions
= QListView::viewOptions();
99 QFont
font(settings
->fontFamily(), settings
->fontSize());
100 font
.setItalic(settings
->italicFont());
101 font
.setBold(settings
->boldFont());
102 m_viewOptions
.font
= font
;
104 const int iconSize
= settings
->iconSize();
105 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
) {
135 QStyleOptionViewItem
ColumnWidget::viewOptions() const
137 return m_viewOptions
;
140 void ColumnWidget::dragEnterEvent(QDragEnterEvent
* event
)
142 if (event
->mimeData()->hasUrls()) {
143 event
->acceptProposedAction();
149 void ColumnWidget::dragLeaveEvent(QDragLeaveEvent
* event
)
151 QListView::dragLeaveEvent(event
);
153 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
155 setDirtyRegion(m_dropRect
);
158 void ColumnWidget::dragMoveEvent(QDragMoveEvent
* event
)
160 QListView::dragMoveEvent(event
);
162 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
163 const QModelIndex index
= indexAt(event
->pos());
164 setDirtyRegion(m_dropRect
);
165 m_dropRect
= visualRect(index
);
166 setDirtyRegion(m_dropRect
);
169 void ColumnWidget::dropEvent(QDropEvent
* event
)
171 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
172 if (!urls
.isEmpty()) {
173 event
->acceptProposedAction();
174 m_view
->m_controller
->indicateDroppedUrls(urls
,
175 indexAt(event
->pos()),
178 QListView::dropEvent(event
);
182 void ColumnWidget::mousePressEvent(QMouseEvent
* event
)
184 if (m_active
|| indexAt(event
->pos()).isValid()) {
185 // Only accept the mouse press event in inactive views,
186 // if a click is done on an item. This assures that
187 // the current selection, which usually shows the
188 // the directory for next column, won't get deleted.
189 QListView::mousePressEvent(event
);
193 void ColumnWidget::paintEvent(QPaintEvent
* event
)
195 QListView::paintEvent(event
);
197 // TODO: remove this code when the issue #160611 is solved in Qt 4.4
199 const QBrush
& brush
= m_viewOptions
.palette
.brush(QPalette::Normal
, QPalette::Highlight
);
200 DolphinController::drawHoverIndication(viewport(), m_dropRect
, brush
);
204 void ColumnWidget::contextMenuEvent(QContextMenuEvent
* event
)
206 QListView::contextMenuEvent(event
);
208 const QModelIndex index
= indexAt(event
->pos());
209 if (index
.isValid() || m_active
) {
210 // Only open a context menu above an item or if the mouse is above
211 // the active column.
212 const QPoint pos
= m_view
->viewport()->mapFromGlobal(event
->globalPos());
213 m_view
->m_controller
->triggerContextMenuRequest(pos
);
217 void ColumnWidget::activate()
219 const QColor bgColor
= KColorScheme(KColorScheme::View
).background();
220 QPalette palette
= viewport()->palette();
221 palette
.setColor(viewport()->backgroundRole(), bgColor
);
222 viewport()->setPalette(palette
);
224 setSelectionMode(MultiSelection
);
227 void ColumnWidget::deactivate()
229 QColor bgColor
= KColorScheme(KColorScheme::View
).background();
230 const QColor fgColor
= KColorScheme(KColorScheme::View
).foreground();
231 bgColor
= KColorUtils::mix(bgColor
, fgColor
, 0.04);
233 QPalette palette
= viewport()->palette();
234 palette
.setColor(viewport()->backgroundRole(), bgColor
);
235 viewport()->setPalette(palette
);
237 setSelectionMode(SingleSelection
);
242 DolphinColumnView::DolphinColumnView(QWidget
* parent
, DolphinController
* controller
) :
244 m_controller(controller
)
246 Q_ASSERT(controller
!= 0);
248 setAcceptDrops(true);
249 setSelectionBehavior(SelectItems
);
250 setDragDropMode(QAbstractItemView::DragDrop
);
251 setDropIndicatorShown(false);
253 if (KGlobalSettings::singleClick()) {
254 connect(this, SIGNAL(clicked(const QModelIndex
&)),
255 this, SLOT(triggerItem(const QModelIndex
&)));
257 connect(this, SIGNAL(doubleClicked(const QModelIndex
&)),
258 this, SLOT(triggerItem(const QModelIndex
&)));
260 connect(this, SIGNAL(activated(const QModelIndex
&)),
261 this, SLOT(triggerItem(const QModelIndex
&)));
262 connect(this, SIGNAL(entered(const QModelIndex
&)),
263 controller
, SLOT(emitItemEntered(const QModelIndex
&)));
264 connect(this, SIGNAL(viewportEntered()),
265 controller
, SLOT(emitViewportEntered()));
266 connect(controller
, SIGNAL(zoomIn()),
267 this, SLOT(zoomIn()));
268 connect(controller
, SIGNAL(zoomOut()),
269 this, SLOT(zoomOut()));
271 updateDecorationSize();
274 DolphinColumnView::~DolphinColumnView()
278 QAbstractItemView
* DolphinColumnView::createColumn(const QModelIndex
& index
)
280 ColumnWidget
* view
= new ColumnWidget(viewport(), this);
282 // The following code has been copied 1:1 from QColumnView::createColumn().
283 // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
284 // QColumnView::initializeColumn() will be available for this.
286 view
->setFrameShape(QFrame::NoFrame
);
287 view
->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
288 view
->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn
);
289 view
->setMinimumWidth(100);
290 view
->setAttribute(Qt::WA_MacShowFocusRect
, false);
292 // copy the 'view' behavior
293 view
->setDragDropMode(dragDropMode());
294 view
->setDragDropOverwriteMode(dragDropOverwriteMode());
295 view
->setDropIndicatorShown(showDropIndicator());
296 view
->setAlternatingRowColors(alternatingRowColors());
297 view
->setAutoScroll(hasAutoScroll());
298 view
->setEditTriggers(editTriggers());
299 view
->setHorizontalScrollMode(horizontalScrollMode());
300 view
->setIconSize(iconSize());
301 view
->setSelectionBehavior(selectionBehavior());
302 view
->setSelectionMode(selectionMode());
303 view
->setTabKeyNavigation(tabKeyNavigation());
304 view
->setTextElideMode(textElideMode());
305 view
->setVerticalScrollMode(verticalScrollMode());
307 view
->setModel(model());
309 // set the delegate to be the columnview delegate
310 QAbstractItemDelegate
* delegate
= view
->itemDelegate();
311 view
->setItemDelegate(itemDelegate());
314 view
->setRootIndex(index
);
316 if (model()->canFetchMore(index
)) {
317 model()->fetchMore(index
);
323 void DolphinColumnView::mousePressEvent(QMouseEvent
* event
)
325 m_controller
->triggerActivation();
326 QColumnView::mousePressEvent(event
);
329 void DolphinColumnView::dragEnterEvent(QDragEnterEvent
* event
)
331 if (event
->mimeData()->hasUrls()) {
332 event
->acceptProposedAction();
336 void DolphinColumnView::dropEvent(QDropEvent
* event
)
338 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
339 if (!urls
.isEmpty()) {
340 m_controller
->indicateDroppedUrls(urls
,
341 indexAt(event
->pos()),
343 event
->acceptProposedAction();
345 QColumnView::dropEvent(event
);
348 void DolphinColumnView::zoomIn()
350 if (isZoomInPossible()) {
351 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
352 // TODO: get rid of K3Icon sizes
353 switch (settings
->iconSize()) {
354 case K3Icon::SizeSmall
: settings
->setIconSize(K3Icon::SizeMedium
); break;
355 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeLarge
); break;
356 default: Q_ASSERT(false); break;
358 updateDecorationSize();
362 void DolphinColumnView::zoomOut()
364 if (isZoomOutPossible()) {
365 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
366 // TODO: get rid of K3Icon sizes
367 switch (settings
->iconSize()) {
368 case K3Icon::SizeLarge
: settings
->setIconSize(K3Icon::SizeMedium
); break;
369 case K3Icon::SizeMedium
: settings
->setIconSize(K3Icon::SizeSmall
); break;
370 default: Q_ASSERT(false); break;
372 updateDecorationSize();
376 void DolphinColumnView::triggerItem(const QModelIndex
& index
)
378 m_controller
->triggerItem(index
);
380 // assure that the last column gets marked as active and all
381 // other columns as inactive
382 QObject
* lastWidget
= viewport()->children().last();
383 foreach (QObject
* object
, viewport()->children()) {
384 if (object
->inherits("QListView")) {
385 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
386 widget
->setActive(widget
== lastWidget
);
391 bool DolphinColumnView::isZoomInPossible() const
393 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
394 return settings
->iconSize() < K3Icon::SizeLarge
;
397 bool DolphinColumnView::isZoomOutPossible() const
399 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
400 return settings
->iconSize() > K3Icon::SizeSmall
;
403 void DolphinColumnView::updateDecorationSize()
405 ColumnModeSettings
* settings
= DolphinSettings::instance().columnModeSettings();
406 const int iconSize
= settings
->iconSize();
408 foreach (QObject
* object
, viewport()->children()) {
409 if (object
->inherits("QListView")) {
410 ColumnWidget
* widget
= static_cast<ColumnWidget
*>(object
);
411 widget
->setDecorationSize(QSize(iconSize
, iconSize
));
415 m_controller
->setZoomInPossible(isZoomInPossible());
416 m_controller
->setZoomOutPossible(isZoomOutPossible());
421 #include "dolphincolumnview.moc"