]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Provide a rubberband for the Details View when selecting items. This assures a consis...
authorPeter Penz <peter.penz19@gmail.com>
Thu, 31 May 2007 16:14:47 +0000 (16:14 +0000)
committerPeter Penz <peter.penz19@gmail.com>
Thu, 31 May 2007 16:14:47 +0000 (16:14 +0000)
svn path=/trunk/KDE/kdebase/apps/; revision=670181

src/dolphindetailsview.cpp
src/dolphindetailsview.h

index 87fa851655ae943578544918769f73ecf10b6f80..2bb145cc4c30bdeac6476d6275472923a89d8430 100644 (file)
 #include <kdirmodel.h>
 #include <kfileitemdelegate.h>
 
-#include <QtGui/QHeaderView>
+#include <QHeaderView>
+#include <QRubberBand>
+#include <QScrollBar>
 
 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"
index 03f1b8d53ed7d5baf7ffd4350fa7131d8cc405c2..da3f2ec90c20743e42ccd546946a0fae807fbf5a 100644 (file)
@@ -27,6 +27,7 @@
 #include <libdolphin_export.h>
 
 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