From 714e18556420cb2bf122b85c95b80773725f5b84 Mon Sep 17 00:00:00 2001
From: Aleksei Kanash <sc.kanash.aleksei@gmail.com>
Date: Mon, 18 Jun 2012 13:54:51 +0300
Subject: [PATCH] Fixed behavior of locked items: - locked items is not
 editable now. - toolbar for text item hides when item is locked

Fixed behavior of toolbar:
toolbar of media items hides after 5s of idle.
---
 src/board/UBBoardView.cpp                  | 106 ++++++++++++++++++---
 src/board/UBBoardView.h                    |   4 +
 src/domain/UBGraphicsDelegateFrame.cpp     |   8 +-
 src/domain/UBGraphicsItemDelegate.cpp      |  19 +++-
 src/domain/UBGraphicsItemDelegate.h        |   9 +-
 src/domain/UBGraphicsMediaItem.cpp         |  14 +--
 src/domain/UBGraphicsMediaItem.h           |   4 +
 src/domain/UBGraphicsMediaItemDelegate.cpp |  28 +++++-
 src/domain/UBGraphicsMediaItemDelegate.h   |   6 ++
 src/domain/UBGraphicsTextItemDelegate.cpp  |  11 ++-
 10 files changed, 172 insertions(+), 37 deletions(-)

diff --git a/src/board/UBBoardView.cpp b/src/board/UBBoardView.cpp
index 8a61d756..c07a9151 100644
--- a/src/board/UBBoardView.cpp
+++ b/src/board/UBBoardView.cpp
@@ -375,6 +375,45 @@ void UBBoardView::tabletEvent (QTabletEvent * event)
 
 }
 
+bool UBBoardView::hasToolBarAsParent(QGraphicsItem *item)
+{
+    if (!item)
+        return false;
+
+    if (!item->parentItem())
+        return hasToolBarAsParent(0); 
+
+    if (UBGraphicsToolBarItem::Type == item->parentItem()->type())
+        return true;
+
+    else 
+        return hasToolBarAsParent(item->parentItem());        
+}
+
+bool UBBoardView::itemIsLocked(QGraphicsItem *item)
+{
+    if (!item)
+        return false;
+
+    if (item->data(UBGraphicsItemData::ItemLocked).toBool())
+        return true;
+
+    return itemIsLocked(item->parentItem());
+
+}
+
+bool UBBoardView::itemHaveType(QGraphicsItem *item, int type)
+{
+    if (!item)
+        return false;
+
+    if (type == item->type())
+        return true;
+    
+    return itemHaveType(item->parentItem(), type);
+
+}
+
 void UBBoardView::mousePressEvent (QMouseEvent *event)
 {
     if (isAbsurdPoint (event->pos ()))
@@ -438,10 +477,14 @@ void UBBoardView::mousePressEvent (QMouseEvent *event)
                 || movingItem == this->scene()->backgroundObject()
                 || (movingItem->parentItem() && movingItem->parentItem()->type() == UBGraphicsGroupContainerItem::Type))
             {
-                    movingItem = NULL;
-                    QGraphicsView::mousePressEvent (event);
+                if (!itemIsLocked(movingItem)
+                    || itemHaveType(movingItem, UBGraphicsMediaItem::Type))
+                {
 
+                    QGraphicsView::mousePressEvent (event);
                 }
+                movingItem = NULL;
+            }
             else
             {
                 mLastPressedMousePos = mapToScene(event->pos());
@@ -452,12 +495,29 @@ void UBBoardView::mousePressEvent (QMouseEvent *event)
                 suspendedMousePressEvent = new QMouseEvent(event->type(), event->pos(), event->button(), event->buttons(), event->modifiers()); // удалить
             }
 
-
             event->accept();
         }
         else if (currentTool == UBStylusTool::Play)
         {
-            QGraphicsView::mousePressEvent (event);
+
+            movingItem = scene()->itemAt(this->mapToScene(event->posF().toPoint()));
+
+            mLastPressedMousePos = mapToScene(event->pos());
+
+            if (movingItem 
+                && (UBGraphicsGroupContainerItem::Type == movingItem->type()
+                    || UBGraphicsMediaItem::Type == movingItem->type()
+                    || hasToolBarAsParent(movingItem)))
+            {
+                movingItem = NULL;
+                QGraphicsView::mousePressEvent (event);
+                return;
+            }
+
+            if(movingItem && movingItem->parentItem() && movingItem->parentItem()->type() == UBGraphicsGroupContainerItem::Type)
+            {
+                movingItem = movingItem->parentItem(); 
+            }                
 
             event->accept();
         }
@@ -579,7 +639,8 @@ UBBoardView::mouseMoveEvent (QMouseEvent *event)
           }
       }
 
-        if (movingItem && (mMouseButtonIsPressed || mTabletStylusIsPressed))
+        if (movingItem && (mMouseButtonIsPressed || mTabletStylusIsPressed) &&
+            !movingItem->data(UBGraphicsItemData::ItemLocked).toBool())
         {
             QPointF scenePos = mapToScene(event->pos());
             QPointF newPos = movingItem->pos() + scenePos - mLastPressedMousePos;
@@ -592,7 +653,18 @@ UBBoardView::mouseMoveEvent (QMouseEvent *event)
     }
   else if (currentTool == UBStylusTool::Play)
   {
-      QGraphicsView::mouseMoveEvent (event);
+      if (movingItem && (mMouseButtonIsPressed || mTabletStylusIsPressed) &&
+          !movingItem->data(UBGraphicsItemData::ItemLocked).toBool())
+      {
+          QPointF scenePos = mapToScene(event->pos());
+          QPointF newPos = movingItem->pos() + scenePos - mLastPressedMousePos;
+          movingItem->setPos(newPos);
+          mLastPressedMousePos = scenePos;
+          mWidgetMoved = true;
+          event->accept();
+      }
+      else 
+        QGraphicsView::mouseMoveEvent (event);
   }
   else if ((UBDrawingController::drawingController()->isDrawingTool())
     && !mMouseButtonIsPressed)
@@ -638,13 +710,21 @@ UBBoardView::mouseReleaseEvent (QMouseEvent *event)
           mWidgetMoved = false;
           movingItem = NULL;
       }
-      else if (movingItem && suspendedMousePressEvent)
-      {
-          QGraphicsView::mousePressEvent(suspendedMousePressEvent);     // suspendedMousePressEvent is deleted by old Qt event loop
-          movingItem = NULL;
-          delete suspendedMousePressEvent;
-          suspendedMousePressEvent = NULL;
-      }
+      else 
+          if (movingItem)
+          {         
+              if (suspendedMousePressEvent && !movingItem->data(UBGraphicsItemData::ItemLocked).toBool())       
+              {
+                  QGraphicsView::mousePressEvent(suspendedMousePressEvent);     // suspendedMousePressEvent is deleted by old Qt event loop
+                  movingItem = NULL;
+                  delete suspendedMousePressEvent;
+                  suspendedMousePressEvent = NULL;
+              }
+              else 
+              {
+                  movingItem->setSelected(true);
+              }
+          }
 
       if (mUBRubberBand && mUBRubberBand->isVisible()) {
           mUBRubberBand->hide();
diff --git a/src/board/UBBoardView.h b/src/board/UBBoardView.h
index 21bc12b8..faab052b 100644
--- a/src/board/UBBoardView.h
+++ b/src/board/UBBoardView.h
@@ -51,6 +51,10 @@ class UBBoardView : public QGraphicsView
 
     protected:
 
+        bool hasToolBarAsParent(QGraphicsItem *item);
+        bool itemIsLocked(QGraphicsItem *item);
+        bool itemHaveType(QGraphicsItem *item, int type);
+
         virtual bool event (QEvent * e);
 
         virtual void keyPressEvent(QKeyEvent *event);
diff --git a/src/domain/UBGraphicsDelegateFrame.cpp b/src/domain/UBGraphicsDelegateFrame.cpp
index 3270abf9..0f98e689 100644
--- a/src/domain/UBGraphicsDelegateFrame.cpp
+++ b/src/domain/UBGraphicsDelegateFrame.cpp
@@ -249,8 +249,8 @@ bool UBGraphicsDelegateFrame::canResizeBottomRight(qreal width, qreal height, qr
 
 void UBGraphicsDelegateFrame::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
 {
-    if (mDelegate->delegated()->data(UBGraphicsItemData::ItemLocked).toBool())
-        return;
+    if (None == mCurrentTool)
+        return; 
 
     QLineF move(mStartingPoint, event->scenePos());
     qreal moveX = move.length() * cos((move.angle() - mAngle) * PI / 180);
@@ -713,8 +713,8 @@ QGraphicsItem* UBGraphicsDelegateFrame::delegated()
 
 UBGraphicsDelegateFrame::FrameTool UBGraphicsDelegateFrame::toolFromPos(QPointF pos)
 {
-        if(mDelegate->isLocked())
-                return None;
+    if(mDelegate->isLocked())
+        return None;
     else if (bottomRightResizeGripRect().contains(pos))
         return ResizeBottomRight;
     else if (bottomResizeGripRect().contains(pos)){
diff --git a/src/domain/UBGraphicsItemDelegate.cpp b/src/domain/UBGraphicsItemDelegate.cpp
index ab92ab16..318a9b89 100644
--- a/src/domain/UBGraphicsItemDelegate.cpp
+++ b/src/domain/UBGraphicsItemDelegate.cpp
@@ -328,7 +328,7 @@ void UBGraphicsItemDelegate::positionHandles()
     if (mDelegated->isSelected()) {
         bool shownOnDisplay = mDelegated->data(UBGraphicsItemData::ItemLayerType).toInt() != UBItemLayerType::Control;
         showHide(shownOnDisplay);
-        lock(isLocked());
+        mDelegated->setData(UBGraphicsItemData::ItemLocked, QVariant(isLocked()));
         updateFrame();
 
         if (UBStylusTool::Play != UBDrawingController::drawingController()->stylusTool())
@@ -453,6 +453,7 @@ void UBGraphicsItemDelegate::lock(bool locked)
     }
 
     mDelegated->update();
+    positionHandles();
     mFrame->positionHandles();
 }
 
@@ -678,7 +679,7 @@ UBGraphicsToolBarItem::UBGraphicsToolBarItem(QGraphicsItem * parent) :
     rect.setWidth(parent->boundingRect().width());
     this->setRect(rect);
 
-    setBrush(QColor(UBSettings::paletteColor));             
+  //  setBrush(QColor(UBSettings::paletteColor));             
     setPen(Qt::NoPen);
     hide();
 
@@ -710,6 +711,17 @@ void UBGraphicsToolBarItem::paint(QPainter *painter, const QStyleOptionGraphicsI
     QPainterPath path;
     path.addRoundedRect(rect(), 10, 10);  
 
+    if (parentItem() && parentItem()->data(UBGraphicsItemData::ItemLocked).toBool())
+    {
+        QColor baseColor = UBSettings::paletteColor;
+        baseColor.setAlphaF(baseColor.alphaF() / 3);
+        setBrush(QBrush(baseColor));
+    }
+    else
+    {
+        setBrush(QBrush(UBSettings::paletteColor));
+    }
+
     painter->fillPath(path, brush());
 }
 
@@ -1217,6 +1229,7 @@ void DelegateMediaControl::mousePressEvent(QGraphicsSceneMouseEvent *event)
         seekToMousePos(event->pos());
         this->update();
         event->accept();
+        emit used();
     }
 }
 
@@ -1229,6 +1242,7 @@ void DelegateMediaControl::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
         seekToMousePos(event->pos());
         this->update();
         event->accept();
+        emit used();
     }
 }
 
@@ -1253,6 +1267,7 @@ void DelegateMediaControl::seekToMousePos(QPointF mousePos)
         //OSX is a bit lazy
         updateTicker(tickPos);
     }
+    emit used();
 }
 
 void DelegateMediaControl::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
diff --git a/src/domain/UBGraphicsItemDelegate.h b/src/domain/UBGraphicsItemDelegate.h
index 79ed8d2d..8ae131d0 100644
--- a/src/domain/UBGraphicsItemDelegate.h
+++ b/src/domain/UBGraphicsItemDelegate.h
@@ -102,8 +102,10 @@ uint smallPoint : 1;
 
 };
 
-class DelegateMediaControl: public QGraphicsRectItem
+class DelegateMediaControl: public QObject, public QGraphicsRectItem
 {
+    Q_OBJECT
+
     public:
 
         DelegateMediaControl(UBGraphicsMediaItem* pDelegated, QGraphicsItem * parent = 0);
@@ -127,7 +129,10 @@ class DelegateMediaControl: public QGraphicsRectItem
         void updateTicker(qint64 time);
         void totalTimeChanged(qint64 newTotalTime);
 
-   protected:
+    signals:
+        void used();
+
+    protected:
         void seekToMousePos(QPointF mousePos);
 
         UBGraphicsMediaItem* mDelegate;
diff --git a/src/domain/UBGraphicsMediaItem.cpp b/src/domain/UBGraphicsMediaItem.cpp
index 08c8bd52..2228b87a 100644
--- a/src/domain/UBGraphicsMediaItem.cpp
+++ b/src/domain/UBGraphicsMediaItem.cpp
@@ -34,8 +34,8 @@ UBGraphicsMediaItem::UBGraphicsMediaItem(const QUrl& pMediaFileUrl, QGraphicsIte
         , mInitialPos(0)
         , mVideoWidget(NULL)
         , mAudioWidget(NULL)
+        , mLinkedImage(NULL)
 {
-    
     update();
 
     QString s = pMediaFileUrl.toLocalFile();
@@ -60,6 +60,7 @@ UBGraphicsMediaItem::UBGraphicsMediaItem(const QUrl& pMediaFileUrl, QGraphicsIte
             mVideoWidget->resize(320,240);
         }
         setWidget(mVideoWidget);
+        haveLinkedImage = true;
     }
     else    
     if (pMediaFileUrl.toLocalFile().contains("audios"))
@@ -71,6 +72,7 @@ UBGraphicsMediaItem::UBGraphicsMediaItem(const QUrl& pMediaFileUrl, QGraphicsIte
         mAudioWidget = new QWidget();
         mAudioWidget->resize(320,26);
         setWidget(mAudioWidget);
+        haveLinkedImage = false;
     }
 
     Phonon::createPath(mMediaObject, mAudioOutput);
@@ -226,7 +228,7 @@ void UBGraphicsMediaItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
     if (mDelegate)
     {
         mDelegate->mousePressEvent(event);
-        if (mDelegate && parentItem() && UBGraphicsGroupContainerItem::Type == parentItem()->type())
+        if (parentItem() && UBGraphicsGroupContainerItem::Type == parentItem()->type())
         {
             UBGraphicsGroupContainerItem *group = qgraphicsitem_cast<UBGraphicsGroupContainerItem*>(parentItem());
             if (group)
@@ -242,11 +244,6 @@ void UBGraphicsMediaItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
             }       
 
         }
-        else
-        {
-            mDelegate->getToolBarItem()->show();
-        }
-
     }
 
     if (parentItem() && parentItem()->type() == UBGraphicsGroupContainerItem::Type)
@@ -271,9 +268,6 @@ void UBGraphicsMediaItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
 
 void UBGraphicsMediaItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
 {
-    if (data(UBGraphicsItemData::ItemLocked).toBool())
-        return;
-
     if(mShouldMove && (event->buttons() & Qt::LeftButton))
     {
         QPointF offset = event->scenePos() - mMousePressPos;
diff --git a/src/domain/UBGraphicsMediaItem.h b/src/domain/UBGraphicsMediaItem.h
index 8d545944..e14e27b8 100644
--- a/src/domain/UBGraphicsMediaItem.h
+++ b/src/domain/UBGraphicsMediaItem.h
@@ -73,6 +73,8 @@ public:
         return mVideoWidget;
     }
 
+    bool hasLinkedImage(){return haveLinkedImage;}
+
     mediaType getMediaType() { return mMediaType; }
 
     virtual UBGraphicsScene* scene();
@@ -115,6 +117,8 @@ private:
     QPointF mMousePressPos;
     QPointF mMouseMovePos;
 
+    bool haveLinkedImage;
+    QGraphicsPixmapItem *mLinkedImage;
 };
 
 
diff --git a/src/domain/UBGraphicsMediaItemDelegate.cpp b/src/domain/UBGraphicsMediaItemDelegate.cpp
index f9b14639..7d164c58 100644
--- a/src/domain/UBGraphicsMediaItemDelegate.cpp
+++ b/src/domain/UBGraphicsMediaItemDelegate.cpp
@@ -33,6 +33,8 @@
 UBGraphicsMediaItemDelegate::UBGraphicsMediaItemDelegate(UBGraphicsMediaItem* pDelegated, Phonon::MediaObject* pMedia, QObject * parent)
     : UBGraphicsItemDelegate(pDelegated, parent, true, false)
     , mMedia(pMedia)
+    , mToolBarShowTimer(NULL)
+    , m_iToolBarShowingInterval(5000)
 {
     QPalette palette;
     palette.setBrush ( QPalette::Light, Qt::darkGray );
@@ -42,27 +44,47 @@ UBGraphicsMediaItemDelegate::UBGraphicsMediaItemDelegate(UBGraphicsMediaItem* pD
     connect(mMedia, SIGNAL(finished()), this, SLOT(updatePlayPauseState()));
     connect(mMedia, SIGNAL(tick(qint64)), this, SLOT(updateTicker(qint64)));
     connect(mMedia, SIGNAL(totalTimeChanged(qint64)), this, SLOT(totalTimeChanged(qint64)));
+
+    if (delegated()->hasLinkedImage())
+    {
+        mToolBarShowTimer = new QTimer();
+        connect(mToolBarShowTimer, SIGNAL(timeout()), this, SLOT(hideToolBar()));
+        mToolBarShowTimer->setInterval(m_iToolBarShowingInterval);
+    }
 }
 
 bool UBGraphicsMediaItemDelegate::mousePressEvent(QGraphicsSceneMouseEvent *event)
 {
     Q_UNUSED(event);
     mToolBarItem->show();
+
+    if (mToolBarShowTimer)
+        mToolBarShowTimer->start();
+
     return UBGraphicsItemDelegate::mousePressEvent(event);
 }
 
+void UBGraphicsMediaItemDelegate::hideToolBar()
+{
+    mToolBarItem->hide();
+}
+
 void UBGraphicsMediaItemDelegate::buildButtons()
 {
     mPlayPauseButton = new DelegateButton(":/images/play.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);
     connect(mPlayPauseButton, SIGNAL(clicked(bool)), this, SLOT(togglePlayPause()));
+    connect(mPlayPauseButton, SIGNAL(clicked(bool)), mToolBarShowTimer, SLOT(start()));
+    
 
     mStopButton = new DelegateButton(":/images/stop.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);
     connect(mStopButton, SIGNAL(clicked(bool)), mMedia, SLOT(stop()));
+    connect(mStopButton, SIGNAL(clicked(bool)), mToolBarShowTimer, SLOT(start()));
 
     mMediaControl = new DelegateMediaControl(delegated(), mToolBarItem);
     mMediaControl->setFlag(QGraphicsItem::ItemIsSelectable, true);
     UBGraphicsItem::assignZValue(mMediaControl, delegated()->zValue());
-
+    connect(mMediaControl, SIGNAL(used()), mToolBarShowTimer, SLOT(start()));
+    
     if (delegated()->isMuted())
         mMuteButton = new DelegateButton(":/images/soundOff.svg", mDelegated, mToolBarItem, Qt::TitleBarArea);
     else
@@ -70,6 +92,7 @@ void UBGraphicsMediaItemDelegate::buildButtons()
 
     connect(mMuteButton, SIGNAL(clicked(bool)), delegated(), SLOT(toggleMute())); 
     connect(mMuteButton, SIGNAL(clicked(bool)), this, SLOT(toggleMute())); // for changing button image
+    connect(mMuteButton, SIGNAL(clicked(bool)), mToolBarShowTimer, SLOT(start()));
 
     mButtons << mPlayPauseButton << mStopButton << mMuteButton;
 
@@ -89,7 +112,8 @@ void UBGraphicsMediaItemDelegate::buildButtons()
 
 UBGraphicsMediaItemDelegate::~UBGraphicsMediaItemDelegate()
 {
-    //NOOP
+    if (mToolBarShowTimer)
+        delete mToolBarShowTimer;
 }
 
 void UBGraphicsMediaItemDelegate::positionHandles()
diff --git a/src/domain/UBGraphicsMediaItemDelegate.h b/src/domain/UBGraphicsMediaItemDelegate.h
index 63c3f058..3454e1d1 100644
--- a/src/domain/UBGraphicsMediaItemDelegate.h
+++ b/src/domain/UBGraphicsMediaItemDelegate.h
@@ -18,6 +18,7 @@
 
 #include <QtGui>
 #include <phonon/MediaObject>
+#include <QTimer>
 
 #include "core/UB.h"
 #include "UBGraphicsItemDelegate.h"
@@ -54,6 +55,8 @@ class UBGraphicsMediaItemDelegate :  public UBGraphicsItemDelegate
 
         void totalTimeChanged(qint64 newTotalTime);
 
+        void hideToolBar();
+
     protected:
         virtual void buildButtons();
 
@@ -65,6 +68,9 @@ class UBGraphicsMediaItemDelegate :  public UBGraphicsItemDelegate
         DelegateMediaControl *mMediaControl;
 
         Phonon::MediaObject* mMedia;
+
+        QTimer *mToolBarShowTimer;
+        int m_iToolBarShowingInterval;
 };
 
 #endif /* UBGRAPHICSMEDIAITEMDELEGATE_H_ */
\ No newline at end of file
diff --git a/src/domain/UBGraphicsTextItemDelegate.cpp b/src/domain/UBGraphicsTextItemDelegate.cpp
index ce47c04c..a93b8c91 100644
--- a/src/domain/UBGraphicsTextItemDelegate.cpp
+++ b/src/domain/UBGraphicsTextItemDelegate.cpp
@@ -303,11 +303,14 @@ void UBGraphicsTextItemDelegate::positionHandles()
             UBGraphicsGroupContainerItem *group = qgraphicsitem_cast<UBGraphicsGroupContainerItem*>(mDelegated->parentItem());
 
             mToolBarItem->hide();
-            if (group && group->getCurrentItem() == mDelegated && group->isSelected())
-                mToolBarItem->show();
+            if (mToolBarItem->parentItem() && !mToolBarItem->parentItem()->data(UBGraphicsItemData::ItemLocked).toBool())
+            {
+                if (group && group->getCurrentItem() == mDelegated && group->isSelected())
+                    mToolBarItem->show();
 
-            if (!group)
-                 mToolBarItem->show();
+                if (!group)
+                     mToolBarItem->show();
+            }
 
         }
     }