From e9885cf48ceb0972d9cc361f69c0d2a05e939fc8 Mon Sep 17 00:00:00 2001
From: Aleksei Kanash <sc.kanash.aleksei@gmail.com>
Date: Fri, 27 Jul 2012 16:12:46 +0300
Subject: [PATCH] UBMedia items improved: toolBar size depends on zoom. audio
 items has title and borders.

---
 src/domain/UBGraphicsItemDelegate.cpp      |  7 ++--
 src/domain/UBGraphicsItemDelegate.h        |  3 ++
 src/domain/UBGraphicsMediaItem.cpp         | 15 +++++---
 src/domain/UBGraphicsMediaItem.h           | 34 ++++++++++++++++-
 src/domain/UBGraphicsMediaItemDelegate.cpp | 44 ++++++++++++++++++----
 src/domain/UBGraphicsMediaItemDelegate.h   |  3 ++
 6 files changed, 88 insertions(+), 18 deletions(-)

diff --git a/src/domain/UBGraphicsItemDelegate.cpp b/src/domain/UBGraphicsItemDelegate.cpp
index 8234a02f..d2ab2f37 100644
--- a/src/domain/UBGraphicsItemDelegate.cpp
+++ b/src/domain/UBGraphicsItemDelegate.cpp
@@ -672,7 +672,8 @@ UBGraphicsToolBarItem::UBGraphicsToolBarItem(QGraphicsItem * parent) :
     mShifting(true),
     mVisible(false),
     mMinWidth(200),
-    mInitialHeight(26)
+    mInitialHeight(26),
+    mElementsPadding(2)
 {
     QRectF rect = this->rect();
     rect.setHeight(mInitialHeight);
@@ -693,7 +694,7 @@ void UBGraphicsToolBarItem::positionHandles()
     foreach (QGraphicsItem* item, mItemsOnToolBar)
     {
         item->setPos(itemXOffset, 0);
-        itemXOffset += (item->boundingRect().width());
+        itemXOffset += (item->boundingRect().width()+mElementsPadding);
         item->show();
     }
 }
@@ -1150,7 +1151,7 @@ void DelegateMediaControl::paint(QPainter *painter,
     mLCDTimerArea.setWidth(rect().height());
 
     mSeecArea = rect();
-    mSeecArea.setWidth(rect().width()-mLCDTimerArea.width());
+    mSeecArea.setWidth(rect().width()-mLCDTimerArea.width()-2);
 
     path.addRoundedRect(mSeecArea, mSeecArea.height()/2, mSeecArea.height()/2);
     painter->fillPath(path, brush());
diff --git a/src/domain/UBGraphicsItemDelegate.h b/src/domain/UBGraphicsItemDelegate.h
index 78ab50db..4d9c7c34 100644
--- a/src/domain/UBGraphicsItemDelegate.h
+++ b/src/domain/UBGraphicsItemDelegate.h
@@ -128,6 +128,7 @@ class DelegateMediaControl: public QObject, public QGraphicsRectItem
         void positionHandles();
         void updateTicker(qint64 time);
         void totalTimeChanged(qint64 newTotalTime);
+        QSizeF lcdAreaSize(){return mLCDTimerArea.size();}
 
     signals:
         void used();
@@ -165,6 +166,7 @@ class UBGraphicsToolBarItem : public QGraphicsRectItem, public QObject
         int minWidth() { return mMinWidth; }
         void positionHandles();
         void update();
+        int getElementsPadding(){return mElementsPadding;}
 
     private:
         void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
@@ -175,6 +177,7 @@ class UBGraphicsToolBarItem : public QGraphicsRectItem, public QObject
         bool mVisible;
         int mMinWidth;
         int mInitialHeight;
+        int mElementsPadding;
         QList<QGraphicsItem*> mItemsOnToolBar;
 };
 
diff --git a/src/domain/UBGraphicsMediaItem.cpp b/src/domain/UBGraphicsMediaItem.cpp
index 4339426e..f5e9e6b0 100644
--- a/src/domain/UBGraphicsMediaItem.cpp
+++ b/src/domain/UBGraphicsMediaItem.cpp
@@ -21,7 +21,6 @@
 #include "document/UBDocumentProxy.h"
 #include "core/UBApplication.h"
 #include "board/UBBoardController.h"
-#include "frameworks/UBFileSystemUtils.h"
 #include "core/memcheck.h"
 
 bool UBGraphicsMediaItem::sIsMutedByDefault = false;
@@ -56,7 +55,7 @@ UBGraphicsMediaItem::UBGraphicsMediaItem(const QUrl& pMediaFileUrl, QGraphicsIte
 
         if(mVideoWidget->sizeHint() == QSize(1,1)){
             mVideoWidget->resize(320,240);
-            mVideoWidget->setMinimumSize(131,26);
+            mVideoWidget->setMinimumSize(140,26);
         }
         setWidget(mVideoWidget);
         haveLinkedImage = true;
@@ -69,9 +68,15 @@ UBGraphicsMediaItem::UBGraphicsMediaItem(const QUrl& pMediaFileUrl, QGraphicsIte
 
         mMediaObject->setTickInterval(1000);
         mAudioWidget = new UBGraphicsMediaItem::UBAudioPresentationWidget();
-        int borderSize = dynamic_cast<UBGraphicsMediaItem::UBAudioPresentationWidget*>(mAudioWidget)->borderSize();
-        mAudioWidget->resize(320+2*borderSize,26+2*borderSize);
-        mAudioWidget->setMinimumSize(147,26+2*borderSize);
+        int borderSize = 0;
+        UBGraphicsMediaItem::UBAudioPresentationWidget* pAudioWidget = dynamic_cast<UBGraphicsMediaItem::UBAudioPresentationWidget*>(mAudioWidget);
+        if (pAudioWidget)
+        {
+            borderSize = pAudioWidget->borderSize();
+        }
+
+        mAudioWidget->resize(320,26+3*borderSize);
+        mAudioWidget->setMinimumSize(150,26+borderSize);
         setWidget(mAudioWidget);
         haveLinkedImage = false;
     }
diff --git a/src/domain/UBGraphicsMediaItem.h b/src/domain/UBGraphicsMediaItem.h
index 889f6e04..cdd95c1a 100644
--- a/src/domain/UBGraphicsMediaItem.h
+++ b/src/domain/UBGraphicsMediaItem.h
@@ -21,6 +21,7 @@
 #include <phonon/VideoWidget>
 #include "core/UBApplication.h"
 #include "board/UBBoardController.h"
+#include "frameworks/UBFileSystemUtils.h"
 
 
 class UBGraphicsMediaItem : public UBGraphicsProxyWidget
@@ -33,10 +34,16 @@ public:
         public:
             UBAudioPresentationWidget::UBAudioPresentationWidget(QWidget *parent = NULL)
                 :QWidget(parent)
-                , mBorderSize(7)
+                , mBorderSize(10)
+                , mTitleSize(10)
             {}
 
-            int borderSize(){return mBorderSize;}
+            int borderSize()
+            {
+                return mBorderSize;
+            }
+            void setTitle(QString title = QString()){mTitle = title;}
+            QString getTitle(){return mTitle;}
 
         private:
             virtual void paintEvent(QPaintEvent *event)
@@ -44,10 +51,22 @@ public:
                 QPainter painter(this);
                 painter.fillRect(rect(), QBrush(Qt::black));
 
+                if (QString() != mTitle)
+                {
+                    painter.setPen(QPen(Qt::white));                 
+                    QRect titleRect = rect();
+                    titleRect.setX(mBorderSize);
+                    titleRect.setY(2);
+                    titleRect.setHeight(15);
+                    painter.drawText(titleRect, mTitle);
+                }
+
                 QWidget::paintEvent(event);
             }
 
         int mBorderSize;
+        int mTitleSize;
+        QString mTitle;
     };
 
 public:
@@ -104,6 +123,17 @@ public:
 
     virtual UBItem* deepCopy() const;
 
+    virtual void setSourceUrl(const QUrl &pSourceUrl)
+    {
+        UBGraphicsMediaItem::UBAudioPresentationWidget* pAudioWidget = dynamic_cast<UBGraphicsMediaItem::UBAudioPresentationWidget*>(mAudioWidget);
+        if (pAudioWidget)
+        {
+            pAudioWidget->setTitle(UBFileSystemUtils::lastPathComponent(pSourceUrl.toLocalFile()));
+        }
+
+        UBItem::setSourceUrl(pSourceUrl);
+    }
+
 public slots:
 
     void toggleMute();
diff --git a/src/domain/UBGraphicsMediaItemDelegate.cpp b/src/domain/UBGraphicsMediaItemDelegate.cpp
index 5be79c44..aad52490 100644
--- a/src/domain/UBGraphicsMediaItemDelegate.cpp
+++ b/src/domain/UBGraphicsMediaItemDelegate.cpp
@@ -36,6 +36,7 @@ UBGraphicsMediaItemDelegate::UBGraphicsMediaItemDelegate(UBGraphicsMediaItem* pD
     , mMedia(pMedia)
     , mToolBarShowTimer(NULL)
     , m_iToolBarShowingInterval(5000)
+    , mToolBarIsShown(false)
 {
     QPalette palette;
     palette.setBrush ( QPalette::Light, Qt::darkGray );
@@ -52,12 +53,18 @@ UBGraphicsMediaItemDelegate::UBGraphicsMediaItemDelegate(UBGraphicsMediaItem* pD
         connect(mToolBarShowTimer, SIGNAL(timeout()), this, SLOT(hideToolBar()));
         mToolBarShowTimer->setInterval(m_iToolBarShowingInterval);
     }
+
+    if (delegated()->getMediaType() == UBGraphicsMediaItem::mediaType_Video)
+    {
+        connect(UBApplication::boardController, SIGNAL(zoomChanged(qreal)), this, SLOT(onZoomChanged()));
+    }
 }
 
 bool UBGraphicsMediaItemDelegate::mousePressEvent(QGraphicsSceneMouseEvent *event)
 {
     Q_UNUSED(event);
     mToolBarItem->show();
+    mToolBarIsShown = true;
 
     if (mToolBarShowTimer)
         mToolBarShowTimer->start();
@@ -68,6 +75,14 @@ bool UBGraphicsMediaItemDelegate::mousePressEvent(QGraphicsSceneMouseEvent *even
 void UBGraphicsMediaItemDelegate::hideToolBar()
 {
     mToolBarItem->hide();
+    mToolBarIsShown = false;
+}
+
+void UBGraphicsMediaItemDelegate::onZoomChanged()
+{
+    positionHandles();
+    if (!mToolBarIsShown)
+        hideToolBar();
 }
 
 void UBGraphicsMediaItemDelegate::buildButtons()
@@ -126,32 +141,45 @@ void UBGraphicsMediaItemDelegate::positionHandles()
     UBGraphicsMediaItem *mediaItem = dynamic_cast<UBGraphicsMediaItem*>(mDelegated);
     if (mediaItem)
     {
-       
-        mToolBarItem->setPos(0, delegated()->boundingRect().height()-mToolBarItem->rect().height()*AntiScaleRatio);
-        mToolBarItem->setScale(AntiScaleRatio);
         QRectF toolBarRect = mToolBarItem->rect();
-        toolBarRect.setWidth(delegated()->boundingRect().width()/AntiScaleRatio);
+        if (mediaItem->getMediaType() == UBGraphicsMediaItem::mediaType_Video)
+        {      
+            mToolBarItem->setPos(0, delegated()->boundingRect().height()-mToolBarItem->rect().height());//*AntiScaleRatio);
+           // mToolBarItem->setScale(AntiScaleRatio);
 
-        if (mediaItem->getMediaType() == UBGraphicsMediaItem::mediaType_Audio)
+            toolBarRect.setWidth(delegated()->boundingRect().width());//AntiScaleRatio);
+        }
+        else if (mediaItem->getMediaType() == UBGraphicsMediaItem::mediaType_Audio)
         {
             int borderSize = 0;
             UBGraphicsMediaItem::UBAudioPresentationWidget *audioWidget = dynamic_cast<UBGraphicsMediaItem::UBAudioPresentationWidget*>(delegated()->widget());
             if (audioWidget)
                 borderSize = audioWidget->borderSize();
 
-            toolBarRect.setWidth(delegated()->boundingRect().width()/AntiScaleRatio-2*borderSize);
-            mToolBarItem->setPos(borderSize,borderSize);
+            mToolBarItem->setPos(borderSize,delegated()->boundingRect().height()-(mToolBarItem->rect().height() + borderSize));//*AntiScaleRatio);
+            toolBarRect.setWidth((delegated()->boundingRect().width()-2*borderSize));///AntiScaleRatio);
             mToolBarItem->show();
         }
 
         mToolBarItem->setRect(toolBarRect);
     }
 
+    int toolBarMinimumWidth = 0;
     int mediaItemWidth = mToolBarItem->boundingRect().width();
     foreach (DelegateButton* button, mButtons)
     {
         if (button->getSection() == Qt::TitleBarArea)
-            mediaItemWidth -= button->boundingRect().width();
+        {
+            mediaItemWidth -= button->boundingRect().width() + mToolBarItem->getElementsPadding();
+            toolBarMinimumWidth += button->boundingRect().width() + mToolBarItem->getElementsPadding();
+        }
+    }
+    toolBarMinimumWidth += mToolBarItem->boundingRect().height();
+
+    UBGraphicsMediaItem::UBAudioPresentationWidget* pAudioWidget = dynamic_cast<UBGraphicsMediaItem::UBAudioPresentationWidget*>(delegated()->widget());
+    if (pAudioWidget)
+    {
+       pAudioWidget->setMinimumSize(toolBarMinimumWidth + (int)mMediaControl->lcdAreaSize().width() + (int)mMediaControl->rect().height(),26+pAudioWidget->borderSize());
     }
 
     QRectF mediaItemRect = mMediaControl->rect();
diff --git a/src/domain/UBGraphicsMediaItemDelegate.h b/src/domain/UBGraphicsMediaItemDelegate.h
index 28a71b37..a1c68346 100644
--- a/src/domain/UBGraphicsMediaItemDelegate.h
+++ b/src/domain/UBGraphicsMediaItemDelegate.h
@@ -57,6 +57,8 @@ class UBGraphicsMediaItemDelegate :  public UBGraphicsItemDelegate
 
         void hideToolBar();
 
+        void onZoomChanged();
+
     protected:
         virtual void buildButtons();
 
@@ -71,6 +73,7 @@ class UBGraphicsMediaItemDelegate :  public UBGraphicsItemDelegate
 
         QTimer *mToolBarShowTimer;
         int m_iToolBarShowingInterval;
+        bool mToolBarIsShown;
 };
 
 #endif /* UBGRAPHICSMEDIAITEMDELEGATE_H_ */
\ No newline at end of file
-- 
2.18.1