Some minor cosmetic changes and removed an unnecessary constant from panelcommon.h
[situare] / src / ui / tabbedpanel.cpp
index ec2aff2..44a3fd0 100644 (file)
     USA.
 */
 
-#include <QButtonGroup>
 #include <QDebug>
 #include <QPropertyAnimation>
+#include <QRegion>
 #include <QSignalTransition>
 #include <QStackedWidget>
 #include <QStateMachine>
 
 #include "panelbar.h"
-#include "panelcontent.h"
-#include "paneltab.h"
-#include "userinfo.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_activeTab(-1)
+      m_open(false),
+      m_closeRequestPending(false)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    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;
+
+    resize(PANEL_BAR_TABBED_WIDTH + PANEL_WIDTH, PANEL_HEIGHT);
+    move(PANEL_CLOSED_X, PANEL_TOP_PADDING);
 
     // --- TABS ---
-    m_panelWidgetStack = new QStackedWidget(this);
+    m_panelTabBar = new PanelTabBar(this);
+    m_panelTabBar->move(PANEL_LEFT_X, PANEL_TAB_BAR_TOP_SPACING);
+
+    connect(m_panelTabBar, SIGNAL(currentChanged(int)),
+            this, SLOT(setCurrentIndex(int)));
 
-    m_tabButtonGroup = new QButtonGroup(this);
+    connect(m_panelTabBar, SIGNAL(sizeChangeRequested()),
+            this, SLOT(calculateMask()));
 
-    connect(m_tabButtonGroup, SIGNAL(buttonPressed(int)),
-            this, SLOT(setActiveTab(int)));
+    connect(m_panelTabBar, SIGNAL(tabCloseRequested(int)),
+             this, SLOT(closePanel()));
+
+    connect(this, SIGNAL(panelClosed()),
+            m_panelTabBar, SLOT(deselectTabs()));
 
     // --- 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);
+
+    QPoint openedPosition(PANEL_OPENED_X, PANEL_TOP_PADDING);
+    m_stateOpened->assignProperty(this, "pos", openedPosition);
 
-    m_panelStateMachine->start();
+    panelStateMachine->start();
 }
 
 int TabbedPanel::addTab(QWidget *widget, const QIcon& icon)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    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__;
 
-    if(!widget)
-        return -1;
+    QRect panelTabBarRect = m_panelTabBar->rect();
+    QRect panelContextButtonBarRect = m_panelContextButtonBar->rect();
+    int panelContextButtonBarY = height() - panelContextButtonBarRect.height();
 
-    int verticalStartPoint = 8;
+    if (!m_open)
+        panelContextButtonBarY = height();
 
-    index = m_panelWidgetStack->insertWidget(index, widget);
-    m_tabButtonGroup->addButton(new PanelTab(this), index);
-    m_tabButtonGroup->button(index)->setIcon(icon);
+    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;
 
-    // [BEGIN]: Purkkaa (to be removed ASAP!!!)
-    if(index > 0)
-        verticalStartPoint += 65 * index;
+    setMask(panelRegion);
+}
 
-    m_tabButtonGroup->button(index)->move(0, verticalStartPoint);
-    // [END]: Purkkaa
+void TabbedPanel::closePanel()
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-    return 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();
+    }
 }
 
-void TabbedPanel::removeTab(int index)
+int TabbedPanel::insertTab(int index, QWidget *widget, const QIcon& icon)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(QWidget *widget = m_panelWidgetStack->widget(index)) {
-        m_panelWidgetStack->removeWidget(widget);
+    index = m_panelContentStack->insertWidget(index, widget);
+    m_panelTabBar->insertTab(index, icon);
+
+    return index;
+}
+
+void TabbedPanel::openPanel(QWidget *widget)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
-        QAbstractButton *tab = m_tabButtonGroup->button(index);
-        m_tabButtonGroup->removeButton(tab);
-        delete tab;
+    if (widget) {
+        m_panelTabBar->selectTab(m_panelContentStack->indexOf(widget));
+    } else if (!m_open) {
+        if (!m_closeRequestPending) {
+            m_open = true;
+            emit toggleState();
+        }
     }
 }
 
-void TabbedPanel::closePanel()
+void TabbedPanel::removeTab(int index)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(m_isOpen)
-        emit toggleState();
+    if (QWidget *widget = m_panelContentStack->widget(index)) {
+        m_panelContentStack->removeWidget(widget);
+        m_panelTabBar->removeTab(index);
+    }
 }
 
-void TabbedPanel::openPanel()
+void TabbedPanel::repositionContextButtonBar()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(!m_isOpen)
-        emit toggleState();
+    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);
+    if (!m_open)
+        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);
+        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);
 
-    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::setActiveTab(int index)
+void TabbedPanel::setCurrentIndex(int index)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(m_activeTab == -1) {
-        m_activeTab = index;
-        m_panelWidgetStack->setCurrentIndex(index);
-        emit tabChanged();
-        emit toggleState();
-    } else if(m_activeTab == index) {
-        m_activeTab = -1;
-        emit toggleState();
-    } else {
-        m_activeTab = index;
-        m_panelWidgetStack->setCurrentIndex(index);
-        emit tabChanged();
+    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);
     }
 }
 
@@ -196,16 +260,12 @@ 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;
-
-        m_tabButtonGroup->setExclusive(false);
-        m_tabButtonGroup->button(m_tabButtonGroup->checkedId())->setChecked(false);
-        m_tabButtonGroup->setExclusive(true);
-
         emit panelClosed();
     }
 }