/***************************************************************************
- * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
+ * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
#include "dolphin_columnmodesettings.h"
-#include <QtGui/QAbstractProxyModel>
-#include <QtCore/QPoint>
+#include <kcolorutils.h>
+#include <kcolorscheme.h>
+#include <kdirlister.h>
+#include <kdirmodel.h>
+
+#include <QAbstractProxyModel>
+#include <QPoint>
+
+/**
+ * Represents one column inside the DolphinColumnView and has been
+ * extended to respect view options and hovering information.
+ */
+class ColumnWidget : public QListView
+{
+public:
+ ColumnWidget(QWidget* parent, DolphinColumnView* columnView);
+ virtual ~ColumnWidget();
+
+ /** Sets the size of the icons. */
+ void setDecorationSize(const QSize& size);
+
+ /**
+ * An active column is defined as column, which shows the same URL
+ * as indicated by the URL navigator. The active column is usually
+ * drawn in a lighter color. All operations are applied to this column.
+ */
+ void setActive(bool active);
+
+protected:
+ virtual QStyleOptionViewItem viewOptions() const;
+ virtual void dragEnterEvent(QDragEnterEvent* event);
+ virtual void dragLeaveEvent(QDragLeaveEvent* event);
+ virtual void dragMoveEvent(QDragMoveEvent* event);
+ virtual void dropEvent(QDropEvent* event);
+ virtual void mouseReleaseEvent(QMouseEvent* event);
+ virtual void paintEvent(QPaintEvent* event);
+ virtual void contextMenuEvent(QContextMenuEvent* event);
+
+private:
+ /** Used by ColumnWidget::setActive(). */
+ void activate();
+
+ /** Used by ColumnWidget::setActive(). */
+ void deactivate();
+
+private:
+ bool m_active;
+ DolphinColumnView* m_view;
+ QStyleOptionViewItem m_viewOptions;
+
+ bool m_dragging; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
+ QRect m_dropRect; // TODO: remove this property when the issue #160611 is solved in Qt 4.4
+};
+
+ColumnWidget::ColumnWidget(QWidget* parent,
+ DolphinColumnView* columnView) :
+ QListView(parent),
+ m_active(true),
+ m_view(columnView),
+ m_dragging(false),
+ m_dropRect()
+{
+ setAcceptDrops(true);
+ setDragDropMode(QAbstractItemView::DragDrop);
+ setDropIndicatorShown(false);
+
+ setMouseTracking(true);
+ viewport()->setAttribute(Qt::WA_Hover);
+
+ // apply the column mode settings to the widget
+ const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
+ Q_ASSERT(settings != 0);
+
+ m_viewOptions = QListView::viewOptions();
+
+ QFont font(settings->fontFamily(), settings->fontSize());
+ font.setItalic(settings->italicFont());
+ font.setBold(settings->boldFont());
+ m_viewOptions.font = font;
+
+ const int iconSize = settings->iconSize();
+ m_viewOptions.decorationSize = QSize(iconSize, iconSize);
+
+ activate();
+}
+
+ColumnWidget::~ColumnWidget()
+{
+}
+
+void ColumnWidget::setDecorationSize(const QSize& size)
+{
+ m_viewOptions.decorationSize = size;
+ doItemsLayout();
+}
+
+void ColumnWidget::setActive(bool active)
+{
+ if (m_active == active) {
+ return;
+ }
+
+ m_active = active;
+
+ if (active) {
+ activate();
+ } else {
+ deactivate();
+ }
+}
+
+QStyleOptionViewItem ColumnWidget::viewOptions() const
+{
+ return m_viewOptions;
+}
+
+void ColumnWidget::dragEnterEvent(QDragEnterEvent* event)
+{
+ if (event->mimeData()->hasUrls()) {
+ event->acceptProposedAction();
+ }
+
+ m_dragging = true;
+}
+
+void ColumnWidget::dragLeaveEvent(QDragLeaveEvent* event)
+{
+ QListView::dragLeaveEvent(event);
+
+ // TODO: remove this code when the issue #160611 is solved in Qt 4.4
+ m_dragging = false;
+ setDirtyRegion(m_dropRect);
+}
+
+void ColumnWidget::dragMoveEvent(QDragMoveEvent* event)
+{
+ QListView::dragMoveEvent(event);
+
+ // TODO: remove this code when the issue #160611 is solved in Qt 4.4
+ const QModelIndex index = indexAt(event->pos());
+ setDirtyRegion(m_dropRect);
+ m_dropRect = visualRect(index);
+ setDirtyRegion(m_dropRect);
+}
+
+void ColumnWidget::dropEvent(QDropEvent* event)
+{
+ const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
+ if (!urls.isEmpty()) {
+ event->acceptProposedAction();
+ m_view->m_controller->indicateDroppedUrls(urls,
+ indexAt(event->pos()),
+ event->source());
+ }
+ QListView::dropEvent(event);
+ m_dragging = false;
+}
+
+void ColumnWidget::mouseReleaseEvent(QMouseEvent* event)
+{
+ m_view->requestActivation(this);
+ QListView::mouseReleaseEvent(event);
+}
+
+void ColumnWidget::paintEvent(QPaintEvent* event)
+{
+ QListView::paintEvent(event);
+
+ // TODO: remove this code when the issue #160611 is solved in Qt 4.4
+ if (m_dragging) {
+ const QBrush& brush = m_viewOptions.palette.brush(QPalette::Normal, QPalette::Highlight);
+ DolphinController::drawHoverIndication(viewport(), m_dropRect, brush);
+ }
+}
+
+void ColumnWidget::contextMenuEvent(QContextMenuEvent* event)
+{
+ m_view->requestActivation(this);
+
+ QListView::contextMenuEvent(event);
+
+ const QModelIndex index = indexAt(event->pos());
+ if (index.isValid() || m_active) {
+ // Only open a context menu above an item or if the mouse is above
+ // the active column.
+ const QPoint pos = m_view->viewport()->mapFromGlobal(event->globalPos());
+ m_view->m_controller->triggerContextMenuRequest(pos);
+ }
+}
+
+void ColumnWidget::activate()
+{
+ const QColor bgColor = KColorScheme(KColorScheme::View).background();
+ QPalette palette = viewport()->palette();
+ palette.setColor(viewport()->backgroundRole(), bgColor);
+ viewport()->setPalette(palette);
+
+ setSelectionMode(MultiSelection);
+}
+
+void ColumnWidget::deactivate()
+{
+ QColor bgColor = KColorScheme(KColorScheme::View).background();
+ const QColor fgColor = KColorScheme(KColorScheme::View).foreground();
+ bgColor = KColorUtils::mix(bgColor, fgColor, 0.04);
+
+ QPalette palette = viewport()->palette();
+ palette.setColor(viewport()->backgroundRole(), bgColor);
+ viewport()->setPalette(palette);
+
+ setSelectionMode(SingleSelection);
+}
+
+// ---
DolphinColumnView::DolphinColumnView(QWidget* parent, DolphinController* controller) :
QColumnView(parent),
if (KGlobalSettings::singleClick()) {
connect(this, SIGNAL(clicked(const QModelIndex&)),
- controller, SLOT(triggerItem(const QModelIndex&)));
+ this, SLOT(triggerItem(const QModelIndex&)));
} else {
connect(this, SIGNAL(doubleClicked(const QModelIndex&)),
- controller, SLOT(triggerItem(const QModelIndex&)));
+ this, SLOT(triggerItem(const QModelIndex&)));
}
- connect(this, SIGNAL(activated(const QModelIndex&)),
- controller, SLOT(triggerItem(const QModelIndex&)));
connect(this, SIGNAL(entered(const QModelIndex&)),
controller, SLOT(emitItemEntered(const QModelIndex&)));
connect(this, SIGNAL(viewportEntered()),
this, SLOT(zoomIn()));
connect(controller, SIGNAL(zoomOut()),
this, SLOT(zoomOut()));
-
- // apply the column mode settings to the widget
- const ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
- Q_ASSERT(settings != 0);
-
- m_viewOptions = QColumnView::viewOptions();
-
- QFont font(settings->fontFamily(), settings->fontSize());
- font.setItalic(settings->italicFont());
- font.setBold(settings->boldFont());
- m_viewOptions.font = font;
+ connect(controller, SIGNAL(urlChanged(const KUrl&)),
+ this, SLOT(updateColumnsState(const KUrl&)));
updateDecorationSize();
}
QAbstractItemView* DolphinColumnView::createColumn(const QModelIndex& index)
{
- QAbstractItemView* view = QColumnView::createColumn(index);
- view->setMouseTracking(true);
- view->viewport()->setAttribute(Qt::WA_Hover);
- return view;
-}
+ ColumnWidget* view = new ColumnWidget(viewport(), this);
-QStyleOptionViewItem DolphinColumnView::viewOptions() const
-{
- return m_viewOptions;
-}
+ // The following code has been copied 1:1 from QColumnView::createColumn().
+ // Copyright (C) 1992-2007 Trolltech ASA. In Qt 4.4 the new method
+ // QColumnView::initializeColumn() will be available for this.
-void DolphinColumnView::contextMenuEvent(QContextMenuEvent* event)
-{
- QColumnView::contextMenuEvent(event);
- m_controller->triggerContextMenuRequest(event->pos());
+ view->setFrameShape(QFrame::NoFrame);
+ view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+ view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
+ view->setMinimumWidth(100);
+ view->setAttribute(Qt::WA_MacShowFocusRect, false);
+
+ // copy the 'view' behavior
+ view->setDragDropMode(dragDropMode());
+ view->setDragDropOverwriteMode(dragDropOverwriteMode());
+ view->setDropIndicatorShown(showDropIndicator());
+ view->setAlternatingRowColors(alternatingRowColors());
+ view->setAutoScroll(hasAutoScroll());
+ view->setEditTriggers(editTriggers());
+ view->setHorizontalScrollMode(horizontalScrollMode());
+ view->setIconSize(iconSize());
+ view->setSelectionBehavior(selectionBehavior());
+ view->setSelectionMode(selectionMode());
+ view->setTabKeyNavigation(tabKeyNavigation());
+ view->setTextElideMode(textElideMode());
+ view->setVerticalScrollMode(verticalScrollMode());
+
+ view->setModel(model());
+
+ // set the delegate to be the columnview delegate
+ QAbstractItemDelegate* delegate = view->itemDelegate();
+ view->setItemDelegate(itemDelegate());
+ delete delegate;
+
+ view->setRootIndex(index);
+
+ if (model()->canFetchMore(index)) {
+ model()->fetchMore(index);
+ }
+
+ return view;
}
void DolphinColumnView::mousePressEvent(QMouseEvent* event)
}
}
+void DolphinColumnView::triggerItem(const QModelIndex& index)
+{
+ m_controller->triggerItem(index);
+ updateColumnsState(m_controller->url());
+}
+
+void DolphinColumnView::updateColumnsState(const KUrl& url)
+{
+ const KUrl baseUrl = dirLister()->url();
+ const int activeIndex = url.path().count('/') - baseUrl.path().count('/');
+
+ int index = 0;
+ foreach (QObject* object, viewport()->children()) {
+ if (object->inherits("QListView")) {
+ ColumnWidget* widget = static_cast<ColumnWidget*>(object);
+ widget->setActive(index == activeIndex);
+ ++index;
+ }
+ }
+}
+
bool DolphinColumnView::isZoomInPossible() const
{
ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
return settings->iconSize() > K3Icon::SizeSmall;
}
+void DolphinColumnView::requestActivation(QWidget* column)
+{
+ KUrl::List dirs = dirLister()->directories();
+ KUrl::List::const_iterator it = dirs.constBegin();
+ foreach (QObject* object, viewport()->children()) {
+ if (object->inherits("QListView")) {
+ ColumnWidget* widget = static_cast<ColumnWidget*>(object);
+ const bool isActive = (widget == column);
+ widget->setActive(isActive);
+ if (isActive) {
+ m_controller->setUrl(*it);
+ }
+ ++it;
+ }
+ }
+}
+
void DolphinColumnView::updateDecorationSize()
{
ColumnModeSettings* settings = DolphinSettings::instance().columnModeSettings();
const int iconSize = settings->iconSize();
- m_viewOptions.decorationSize = QSize(iconSize, iconSize);
+
+ foreach (QObject* object, viewport()->children()) {
+ if (object->inherits("QListView")) {
+ ColumnWidget* widget = static_cast<ColumnWidget*>(object);
+ widget->setDecorationSize(QSize(iconSize, iconSize));
+ }
+ }
m_controller->setZoomInPossible(isZoomInPossible());
m_controller->setZoomOutPossible(isZoomOutPossible());
doItemsLayout();
}
+KDirLister* DolphinColumnView::dirLister() const
+{
+ const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(model());
+ const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
+ return dirModel->dirLister();
+}
+
#include "dolphincolumnview.moc"