From: Peter Penz Date: Thu, 31 May 2007 16:14:47 +0000 (+0000) Subject: Provide a rubberband for the Details View when selecting items. This assures a consis... X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/8d0e873f670cab8e7bc8df902abed6ebc17dd09d Provide a rubberband for the Details View when selecting items. This assures a consistent behavior with the Icons View and the selection style of other file managers (in opposite to QListView it is not possible in QTreeView to show a rubberband automatically within Qt 4.3 :-(). svn path=/trunk/KDE/kdebase/apps/; revision=670181 --- diff --git a/src/dolphindetailsview.cpp b/src/dolphindetailsview.cpp index 87fa85165..2bb145cc4 100644 --- a/src/dolphindetailsview.cpp +++ b/src/dolphindetailsview.cpp @@ -30,11 +30,15 @@ #include #include -#include +#include +#include +#include DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* controller) : QTreeView(parent), - m_controller(controller) + m_controller(controller), + m_rubberBand(0), + m_origin() { Q_ASSERT(controller != 0); @@ -94,7 +98,8 @@ DolphinDetailsView::DolphinDetailsView(QWidget* parent, DolphinController* contr } DolphinDetailsView::~DolphinDetailsView() -{} +{ +} bool DolphinDetailsView::event(QEvent* event) { @@ -145,9 +150,39 @@ void DolphinDetailsView::contextMenuEvent(QContextMenuEvent* event) m_controller->triggerContextMenuRequest(event->pos()); } +void DolphinDetailsView::mousePressEvent(QMouseEvent* event) +{ + QTreeView::mousePressEvent(event); + + if (event->button() == Qt::LeftButton) { + // initialize rubberband for the selection + if (m_rubberBand == 0) { + m_rubberBand = new QRubberBand(QRubberBand::Rectangle, viewport()); + } + + const QPoint pos(contentsPos()); + m_origin = event->pos(); + m_origin.setX(m_origin.x() + pos.x()); + m_origin.setY(m_origin.y() + pos.y()); + updateRubberBandGeometry(); + m_rubberBand->show(); + } +} + +void DolphinDetailsView::mouseMoveEvent(QMouseEvent* event) +{ + QTreeView::mouseMoveEvent(event); + if (m_rubberBand != 0) { + updateRubberBandGeometry(); + } +} + void DolphinDetailsView::mouseReleaseEvent(QMouseEvent* event) { QTreeView::mouseReleaseEvent(event); + if (m_rubberBand != 0) { + m_rubberBand->hide(); + } m_controller->triggerActivation(); } @@ -156,6 +191,9 @@ void DolphinDetailsView::dragEnterEvent(QDragEnterEvent* event) if (event->mimeData()->hasUrls()) { event->acceptProposedAction(); } + if (m_rubberBand != 0) { + m_rubberBand->hide(); + } } void DolphinDetailsView::dropEvent(QDropEvent* event) @@ -204,6 +242,16 @@ void DolphinDetailsView::slotEntered(const QModelIndex& index) } } +void DolphinDetailsView::updateRubberBandGeometry() +{ + if (m_rubberBand != 0) { + const QPoint pos(contentsPos()); + const QPoint origin(m_origin.x() - pos.x(), m_origin.y() - pos.y()); + const QPoint dest(viewport()->mapFromGlobal(QCursor::pos())); + m_rubberBand->setGeometry(QRect(origin, dest).normalized()); + } +} + void DolphinDetailsView::zoomIn() { if (isZoomInPossible()) { @@ -256,4 +304,22 @@ void DolphinDetailsView::updateDecorationSize() doItemsLayout(); } +QPoint DolphinDetailsView::contentsPos() const +{ + // implementation note: the horizonal position is ignored currently, as no + // horizontal scrolling is done anyway during a selection + const QScrollBar* scrollbar = verticalScrollBar(); + Q_ASSERT(scrollbar != 0); + + const int maxHeight = maximumViewportSize().height(); + const int height = scrollbar->maximum() - scrollbar->minimum() + 1; + const int visibleHeight = model()->rowCount() + 1 - height; + if (visibleHeight <= 0) { + return QPoint(0, 0); + } + + const int y = scrollbar->sliderPosition() * maxHeight / visibleHeight; + return QPoint(0, y); +} + #include "dolphindetailsview.moc" diff --git a/src/dolphindetailsview.h b/src/dolphindetailsview.h index 03f1b8d53..da3f2ec90 100644 --- a/src/dolphindetailsview.h +++ b/src/dolphindetailsview.h @@ -27,6 +27,7 @@ #include class DolphinController; +class QRubberBand; /** * @brief Represents the details view which shows the name, size, @@ -48,6 +49,8 @@ protected: virtual bool event(QEvent* event); virtual QStyleOptionViewItem viewOptions() const; virtual void contextMenuEvent(QContextMenuEvent* event); + virtual void mousePressEvent(QMouseEvent* event); + virtual void mouseMoveEvent(QMouseEvent* event); virtual void mouseReleaseEvent(QMouseEvent* event); virtual void dragEnterEvent(QDragEnterEvent* event); virtual void dropEvent(QDropEvent* event); @@ -81,6 +84,12 @@ private slots: */ void slotEntered(const QModelIndex& index); + /** + * Updates the geometry of the rubberband dependent from the current + * mouse position and the starting origin \a m_origin. + */ + void updateRubberBandGeometry(); + void zoomIn(); void zoomOut(); @@ -96,9 +105,15 @@ private: */ void updateDecorationSize(); + /** Return the upper left position in pixels of the viewport content. */ + QPoint contentsPos() const; + private: DolphinController* m_controller; QStyleOptionViewItem m_viewOptions; + + QRubberBand* m_rubberBand; + QPoint m_origin; }; #endif