Some minor cosmetic changes and removed an unnecessary constant from panelcommon.h
[situare] / src / ui / tabbedpanel.cpp
index f534a86..44a3fd0 100644 (file)
 
 #include <QDebug>
 #include <QPropertyAnimation>
+#include <QRegion>
 #include <QSignalTransition>
 #include <QStackedWidget>
 #include <QStateMachine>
 
 #include "panelbar.h"
-#include "panelcontent.h"
+#include "panelbase.h"
+#include "panelcontentstack.h"
+#include "panelcontextbuttonbar.h"
 #include "paneltabbar.h"
 
 #include "tabbedpanel.h"
 
+const int PANEL_CONTEXT_BUTTON_BAR_LEFT_X = 1;
+const int PANEL_TAB_BAR_TOP_SPACING = 8;
+
 TabbedPanel::TabbedPanel(QWidget *parent)
     : QWidget(parent),
-      m_isOpen(false)
+      m_open(false),
+      m_closeRequestPending(false)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    ///< @todo Do not use this, REMOVE ALL OCCURENCES IN ALL FILES!
-    this->resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH, PANEL_HEIGHT);
-    this->move(PANEL_CLOSED_X, PANEL_TOP_PADDING);
+    const int PANEL_LEFT_X = 0;
+    const int PANEL_TOP_Y = 0;
 
-    // --- TABS ---
-    m_panelWidgetStack = new QStackedWidget(this);
+    resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH, PANEL_HEIGHT);
+    move(PANEL_CLOSED_X, PANEL_TOP_PADDING);
 
+    // --- TABS ---
     m_panelTabBar = new PanelTabBar(this);
+    m_panelTabBar->move(PANEL_LEFT_X, PANEL_TAB_BAR_TOP_SPACING);
 
     connect(m_panelTabBar, SIGNAL(currentChanged(int)),
-            this, SLOT(showTab(int)));
+            this, SLOT(setCurrentIndex(int)));
+
+    connect(m_panelTabBar, SIGNAL(sizeChangeRequested()),
+            this, SLOT(calculateMask()));
 
     connect(m_panelTabBar, SIGNAL(tabCloseRequested(int)),
              this, SLOT(closePanel()));
@@ -58,138 +69,203 @@ TabbedPanel::TabbedPanel(QWidget *parent)
 
     // --- BAR ---
     m_panelBar = new PanelBar(this);
-    m_panelBar->move(PANEL_TAB_WIDTH, 0);
+    m_panelBar->move(PANEL_TAB_WIDTH, PANEL_TOP_Y);
+
+    // --- CONTEXT BUTTON BAR ---
+    m_panelContextButtonBar = new PanelContextButtonBar(this);
+    m_panelContextButtonBar->move(PANEL_CONTEXT_BUTTON_BAR_LEFT_X, PANEL_HEIGHT);
+
+    connect(m_panelContextButtonBar, SIGNAL(barHidden()),
+            this, SLOT(closePanel()));
+
+    connect(m_panelContextButtonBar, SIGNAL(positionChangeRequested()),
+            this, SLOT(repositionContextButtonBar()));
 
     // --- PANEL CONTENT ---
-    m_panelContent = new PanelContent(this);
-    m_panelContent->setContentWidget(m_panelWidgetStack);
-    m_panelContent->move(PANEL_TAB_WIDTH + PANEL_BAR_WIDTH, 0);
+    m_panelContentStack = new PanelContentStack(this);
+    m_panelContentStack->move(PANEL_TAB_WIDTH + PANEL_BAR_WIDTH, PANEL_TOP_Y);
 
     // --- PANEL ANIMATION ---
-    m_panelStateMachine = new QStateMachine(this);
+    QStateMachine *panelStateMachine = new QStateMachine(this);
 
-    m_panelStateClosed = new QState(m_panelStateMachine);
-    m_panelStateOpened = new QState(m_panelStateMachine);
+    m_stateClosed = new QState(panelStateMachine);
+    m_stateOpened = new QState(panelStateMachine);
 
-    m_panelAnimation = new QPropertyAnimation(this, "pos", this);
+    QPropertyAnimation *panelAnimation = new QPropertyAnimation(this, "pos", this);
 
-    m_panelStateMachine->setInitialState(m_panelStateClosed);
+    panelStateMachine->setInitialState(m_stateClosed);
 
-    m_panelTransitionOpen = m_panelStateClosed->addTransition(this, SIGNAL(toggleState()),
-                                                              m_panelStateOpened);
-    m_panelTransitionOpen->addAnimation(m_panelAnimation);
+    QSignalTransition *panelTransitionOpen = m_stateClosed->addTransition(this,
+                                                                          SIGNAL(toggleState()),
+                                                                          m_stateOpened);
+    panelTransitionOpen->addAnimation(panelAnimation);
 
-    m_panelTransitionClose = m_panelStateOpened->addTransition(this, SIGNAL(toggleState()),
-                                                               m_panelStateClosed);
-    m_panelTransitionClose->addAnimation(m_panelAnimation);
+    QSignalTransition *panelTransitionClose = m_stateOpened->addTransition(this,
+                                                                           SIGNAL(toggleState()),
+                                                                           m_stateClosed);
+    panelTransitionClose->addAnimation(panelAnimation);
 
-    connect(m_panelAnimation, SIGNAL(finished()),
+    connect(panelAnimation, SIGNAL(finished()),
             this, SLOT(stateChanged()));
 
-    m_panelStateClosed->assignProperty(this, "pos",
-                                       QPoint(PANEL_CLOSED_X, PANEL_TOP_PADDING));
-    m_panelStateOpened->assignProperty(this, "pos",
-                                       QPoint(PANEL_OPENED_X, PANEL_TOP_PADDING));
+    QPoint closedPosition(PANEL_CLOSED_X, PANEL_TOP_PADDING);
+    m_stateClosed->assignProperty(this, "pos", closedPosition);
 
-    m_panelStateMachine->start();
+    QPoint openedPosition(PANEL_OPENED_X, PANEL_TOP_PADDING);
+    m_stateOpened->assignProperty(this, "pos", openedPosition);
+
+    panelStateMachine->start();
 }
 
 int TabbedPanel::addTab(QWidget *widget, const QIcon& icon)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    ///< @todo magic
-    return insertTab(-1, widget, icon);
+    const int APPEND_INDEX = -1;
+
+    return insertTab(APPEND_INDEX, widget, icon);
 }
 
-int TabbedPanel::insertTab(int index, QWidget *widget, const QIcon& icon)
+void TabbedPanel::calculateMask()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    ///< @todo callers responsibility to call with right parameters
-    if(!widget)
-        return -1;
+    QRect panelTabBarRect = m_panelTabBar->rect();
+    QRect panelContextButtonBarRect = m_panelContextButtonBar->rect();
+    int panelContextButtonBarY = height() - panelContextButtonBarRect.height();
 
-    index = m_panelWidgetStack->insertWidget(index, widget);
-    m_panelTabBar->insertTab(index, icon);
+    if (!m_open)
+        panelContextButtonBarY = height();
 
-    return index;
+    QRegion panelTabBarRegion(0, PANEL_TAB_BAR_TOP_SPACING,
+                         panelTabBarRect.width(), panelTabBarRect.height());
+    QRegion panelContextButtonBarRegion(0, panelContextButtonBarY,
+                                      panelContextButtonBarRect.width(),
+                                      panelContextButtonBarRect.height());
+    QRegion panelContentRegion(panelTabBarRect.right() + 1, 0,
+                               PANEL_WIDTH, height());
+    QRegion panelRegion = panelTabBarRegion + panelContentRegion + panelContextButtonBarRegion;
+
+    setMask(panelRegion);
 }
 
-void TabbedPanel::removeTab(int index)
+void TabbedPanel::closePanel()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(QWidget *widget = m_panelWidgetStack->widget(index)) {
-        m_panelWidgetStack->removeWidget(widget);
-        m_panelTabBar->removeTab(index);
+    if (m_open) {
+        m_open = false;
+
+        if (m_panelContextButtonBar->isBarVisible()) {
+            m_closeRequestPending = true;
+            m_panelContextButtonBar->hideContextButtonBar();
+        } else {
+            emit toggleState();
+        }
+    } else if (m_closeRequestPending) {
+        m_closeRequestPending = false;
+        emit toggleState();
     }
 }
 
-///< @todo sort alphabetically (other methods too)
-void TabbedPanel::closePanel()
+int TabbedPanel::insertTab(int index, QWidget *widget, const QIcon& icon)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(m_isOpen)
-        emit toggleState();
+    index = m_panelContentStack->insertWidget(index, widget);
+    m_panelTabBar->insertTab(index, icon);
+
+    return index;
 }
 
-void TabbedPanel::openPanel()
+void TabbedPanel::openPanel(QWidget *widget)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(!m_isOpen)
-        emit toggleState();
+    if (widget) {
+        m_panelTabBar->selectTab(m_panelContentStack->indexOf(widget));
+    } else if (!m_open) {
+        if (!m_closeRequestPending) {
+            m_open = true;
+            emit toggleState();
+        }
+    }
 }
 
-void TabbedPanel::showTab(int index)
+void TabbedPanel::removeTab(int index)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-///< @todo if ((first) && (second)) {
-    if (index < m_panelWidgetStack->count() && index >= 0) {
-        m_panelWidgetStack->setCurrentIndex(index);
-        openPanel();
-        emit currentChanged(index);
+    if (QWidget *widget = m_panelContentStack->widget(index)) {
+        m_panelContentStack->removeWidget(widget);
+        m_panelTabBar->removeTab(index);
     }
 }
 
+void TabbedPanel::repositionContextButtonBar()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_panelContextButtonBar->move(PANEL_CONTEXT_BUTTON_BAR_LEFT_X, height());
+    
+    calculateMask();
+}
+
 void TabbedPanel::resizePanel(const QSize &size)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    this->resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH,
-                 size.height() - PANEL_TOP_PADDING - PANEL_BOTTOM_PADDING);
+    resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH,
+           size.height() - PANEL_TOP_PADDING - PANEL_BOTTOM_PADDING);
 
-    if (!m_isOpen) {
-        this->move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING);
-    } else {
-        this->move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
-                   PANEL_TOP_PADDING);
-    }
+    if (!m_open)
+        move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING);
+    else
+        move(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH, PANEL_TOP_PADDING);
 
     m_panelBar->resizeBar(size);
 
-    m_panelContent->resizePanelContent(size);
+    m_panelContextButtonBar->move(PANEL_CONTEXT_BUTTON_BAR_LEFT_X, size.height());
+
+    m_panelContentStack->resizeContentStack(size);
+
+    QPoint closedPosition(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING);
+    m_stateClosed->assignProperty(this, "pos", closedPosition);
 
-    ///< @todo alignment
-    m_panelStateClosed->assignProperty(this, "pos",
-                        QPoint(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH, PANEL_TOP_PADDING));
-    m_panelStateOpened->assignProperty(this, "pos",
-                        QPoint(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
-                               PANEL_TOP_PADDING));
+    QPoint openedPosition(size.width() - PANEL_TAB_WIDTH - PANEL_BAR_WIDTH - PANEL_WIDTH,
+                          PANEL_TOP_PADDING);
+    m_stateOpened->assignProperty(this, "pos", openedPosition);
+
+    calculateMask();
+}
+
+void TabbedPanel::setCurrentIndex(int index)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if ((index < m_panelContentStack->count()) && (index >= 0)) {
+        m_panelContentStack->setCurrentIndex(index);
+
+        if (!m_open)
+            openPanel();
+
+        m_panelContextButtonBar->setContextButtons(
+                static_cast<PanelBase *>(m_panelContentStack->widget(index))->contextButtons());
+
+        emit currentChanged(index);
+    }
 }
 
 void TabbedPanel::stateChanged()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(!m_isOpen) {
-        m_isOpen = true;
+    calculateMask();
+
+    if (m_open) {
+        m_panelContextButtonBar->showContextButtonBar();
         emit panelOpened();
     } else {
-        m_isOpen = false;
         emit panelClosed();
     }
 }