From a14db5e7f2297baf9df088df07ca55ac61bcc67b Mon Sep 17 00:00:00 2001
From: Michael Zanetti <mzanetti@kde.org>
Date: Sat, 20 Aug 2011 21:30:55 +0200
Subject: [PATCH] readded the editorEvent which somehow got lost in the
 previous merge and cleaned up a bit

---
 src/sourcetree/items/sourcetreeitem.cpp |  1 -
 src/sourcetree/items/sourcetreeitem.h   |  2 --
 src/sourcetree/sourcedelegate.cpp       | 28 +++++++++++++++++++++++++
 src/sourcetree/sourcedelegate.h         |  2 ++
 src/sourcetree/sourcetreeview.cpp       | 11 ----------
 5 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/src/sourcetree/items/sourcetreeitem.cpp b/src/sourcetree/items/sourcetreeitem.cpp
index 51e5ed14b..5f82833c3 100644
--- a/src/sourcetree/items/sourcetreeitem.cpp
+++ b/src/sourcetree/items/sourcetreeitem.cpp
@@ -28,7 +28,6 @@ SourceTreeItem::SourceTreeItem( SourcesModel* model, SourceTreeItem* parent, Sou
     , m_type( thisType )
     , m_parent( parent )
     , m_model( model )
-    //, m_dropHovering( false )
 {
     connect( this, SIGNAL( beginChildRowsAdded( int,int ) ), m_model, SLOT( onItemRowsAddedBegin( int,int ) ) );
     connect( this, SIGNAL( beginChildRowsRemoved( int,int ) ), m_model, SLOT( onItemRowsRemovedBegin( int,int ) ) );
diff --git a/src/sourcetree/items/sourcetreeitem.h b/src/sourcetree/items/sourcetreeitem.h
index 4234c3e10..4c5ea93f8 100644
--- a/src/sourcetree/items/sourcetreeitem.h
+++ b/src/sourcetree/items/sourcetreeitem.h
@@ -66,8 +66,6 @@ public:
     virtual bool setData( const QVariant&, bool ) { return false; }
     virtual int peerSortValue() const { return 0; } // How to sort relative to peers in the tree.
     virtual int IDValue() const { return 0; }
-//    virtual bool dropHovering() const { return m_dropHovering; }
-//    virtual void setDropHovering( bool dropHovering ) { m_dropHovering = dropHovering; emit updated(); }
     virtual DropTypes supportedDropTypes() const { return DropTypesNone; }
     virtual void setDropType( DropType type ) { m_dropType = type; }
 
diff --git a/src/sourcetree/sourcedelegate.cpp b/src/sourcetree/sourcedelegate.cpp
index 5b8bb25d2..41381db90 100644
--- a/src/sourcetree/sourcedelegate.cpp
+++ b/src/sourcetree/sourcedelegate.cpp
@@ -5,9 +5,11 @@
 #include "items/playlistitems.h"
 
 #include "utils/tomahawkutils.h"
+#include "items/temporarypageitem.h"
 
 #include <QApplication>
 #include <QPainter>
+#include <QMouseEvent>
 
 #define TREEVIEW_INDENT_ADD -7
 
@@ -291,6 +293,32 @@ SourceDelegate::updateEditorGeometry( QWidget* editor, const QStyleOptionViewIte
     editor->setGeometry( editor->geometry().adjusted( 2*TREEVIEW_INDENT_ADD, 0, 0, 0 ) );
 }
 
+bool
+SourceDelegate::editorEvent ( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index )
+{
+
+    if ( event->type() == QEvent::MouseButtonRelease )
+    {
+        SourcesModel::RowType type = static_cast< SourcesModel::RowType >( index.data( SourcesModel::SourceTreeItemTypeRole ).toInt() );
+        if ( type == SourcesModel::TemporaryPage )
+        {
+            TemporaryPageItem* gpi = qobject_cast< TemporaryPageItem* >( index.data( SourcesModel::SourceTreeItemRole ).value< SourceTreeItem* >() );
+            Q_ASSERT( gpi );
+            QMouseEvent* ev = static_cast< QMouseEvent* >( event );
+
+            QStyleOptionViewItemV4 o = option;
+            initStyleOption( &o, index );
+            int padding = 3;
+            QRect r ( o.rect.right() - padding - m_iconHeight, padding + o.rect.y(), m_iconHeight, m_iconHeight );
+
+            if ( r.contains( ev->pos() ) )
+                gpi->removeFromList();
+        }
+    }
+
+    return QStyledItemDelegate::editorEvent ( event, model, option, index );
+}
+
 int
 SourceDelegate::dropTypeCount( SourceTreeItem* item ) const
 {
diff --git a/src/sourcetree/sourcedelegate.h b/src/sourcetree/sourcedelegate.h
index a05f31b6f..f0e7ad18a 100644
--- a/src/sourcetree/sourcedelegate.h
+++ b/src/sourcetree/sourcedelegate.h
@@ -20,9 +20,11 @@ protected:
     virtual void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const;
     virtual void updateEditorGeometry( QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index ) const;
     virtual int dropTypeCount( SourceTreeItem* item ) const;
+    virtual bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index );
 
 private:
     QAbstractItemView* m_parent;
+    mutable int m_iconHeight;
     QModelIndex m_dropHoverIndex;
     mutable SourceTreeItem::DropType m_hoveredDropType; // Hack to keep easily track of the current highlighted DropType in paint()
 };
diff --git a/src/sourcetree/sourcetreeview.cpp b/src/sourcetree/sourcetreeview.cpp
index eaa68af99..feaf44efb 100644
--- a/src/sourcetree/sourcetreeview.cpp
+++ b/src/sourcetree/sourcetreeview.cpp
@@ -439,11 +439,6 @@ SourceTreeView::dragLeaveEvent( QDragLeaveEvent* event )
     m_dragging = false;
     setDirtyRegion( m_dropRect );
 
-//    SourceTreeItem* oldItem = m_dropIndex.data( SourcesModel::SourceTreeItemRole ).value< SourceTreeItem* >();
-//    if ( oldItem )
-//    {
-//        oldItem->setDropHovering( false );
-//    }
     m_delegate->setDropHoverIndex( QModelIndex() );
     dataChanged(m_dropIndex, m_dropIndex);
     m_dropIndex = QPersistentModelIndex();
@@ -461,11 +456,6 @@ SourceTreeView::dragMoveEvent( QDragMoveEvent* event )
         setDirtyRegion( m_dropRect );
         const QPoint pos = event->pos();
         const QModelIndex index = indexAt( pos );
-//        SourceTreeItem* oldItem = m_dropIndex.data( SourcesModel::SourceTreeItemRole ).value< SourceTreeItem* >();
-//        if ( oldItem )
-//        {
-//            oldItem->setDropHovering( false );
-//        }
         m_delegate->setDropHoverIndex( QModelIndex() );
         dataChanged(m_dropIndex, m_dropIndex);
         m_dropIndex = QPersistentModelIndex( index );
@@ -479,7 +469,6 @@ SourceTreeView::dragMoveEvent( QDragMoveEvent* event )
             if( item->willAcceptDrag( event->mimeData() ) )
             {
                 accept = true;
-//                item->setDropHovering( true );
                 m_delegate->setDropHoverIndex( index );
                 dataChanged(index, index);
             }