Refactored panel context button bar animation and now it is implemented in a lot...
[situare] / src / ui / panelcontextbuttonbar.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Pekka Nissinen - pekka.nissinen@ixonos.com
6
7     Situare is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     version 2 as published by the Free Software Foundation.
10
11     Situare is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with Situare; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19     USA.
20 */
21
22 #include <QDebug>
23 #include <QPainter>
24 #include <QPropertyAnimation>
25 #include <QSignalTransition>
26 #include <QStateMachine>
27
28 #include "panelcontextbuttonbar.h"
29
30 const int PANEL_CONTEXT_BUTTON_BAR_WIDTH = 78;
31
32 PanelContextButtonBar::PanelContextButtonBar(QWidget *parent)
33     : QWidget(parent),
34       m_visible(false),
35       m_contextButtons(0)
36 {
37     qDebug() << __PRETTY_FUNCTION__;
38
39     setFixedWidth(PANEL_CONTEXT_BUTTON_BAR_WIDTH);
40
41     m_barTile.load(":/res/images/panel_context_button_bar_tile.png");
42     m_barTop.load(":/res/images/panel_context_button_bar_top.png");
43
44     // --- BAR ANIMATION ---
45     QStateMachine *stateMachine = new QStateMachine(this);
46
47     m_stateHidden = new QState(stateMachine);
48     m_stateVisible = new QState(stateMachine);
49
50     QPropertyAnimation *animation = new QPropertyAnimation(this, "pos", this);
51
52     stateMachine->setInitialState(m_stateHidden);
53
54     QSignalTransition *showSignalTransition = m_stateHidden->addTransition(this,
55                                                                            SIGNAL(toggleState()),
56                                                                            m_stateVisible);
57     showSignalTransition->addAnimation(animation);
58
59     QSignalTransition *hideSignalTransition = m_stateVisible->addTransition(this,
60                                                                             SIGNAL(toggleState()),
61                                                                             m_stateHidden);
62     hideSignalTransition->addAnimation(animation);
63
64     connect(animation, SIGNAL(finished()),
65             this, SLOT(contextButtonBarStateChanged()));
66
67     QPoint hiddenPosition(this->pos().x(), this->pos().y());
68     m_stateHidden->assignProperty(this, "pos", hiddenPosition);
69
70     QPoint visiblePosition(this->pos().x(), this->pos().y() - height());
71     m_stateVisible->assignProperty(this, "pos", visiblePosition);
72
73     stateMachine->start();
74 }
75
76 void PanelContextButtonBar::contextButtonBarStateChanged()
77 {
78     qDebug() << __PRETTY_FUNCTION__;
79
80     if (m_visible)
81         emit barVisible();
82     else
83         emit barHidden();
84 }
85
86 void PanelContextButtonBar::hideContextButtonBar()
87 {
88     qDebug() << __PRETTY_FUNCTION__;
89
90     if (m_visible) {
91         m_visible = false;
92         emit toggleState();
93     }
94 }
95
96 bool PanelContextButtonBar::isBarVisible() const
97 {
98     qDebug() << __PRETTY_FUNCTION__;
99
100     return m_visible;
101 }
102
103 void PanelContextButtonBar::paintEvent(QPaintEvent *event)
104 {
105     qDebug() << __PRETTY_FUNCTION__;
106
107     Q_UNUSED(event);
108
109     const int CONTEXT_BUTTON_BAR_RECT_X = 0;
110     const int CONTEXT_BUTTON_BAR_TOP_HEIGHT = 32;
111     const int CONTEXT_BUTTON_BAR_TOP_X = 0;
112     const int CONTEXT_BUTTON_BAR_TOP_Y = 0;
113
114     QPainter painter(this);
115
116     m_barRect.setRect(CONTEXT_BUTTON_BAR_RECT_X, CONTEXT_BUTTON_BAR_TOP_HEIGHT,
117                       PANEL_CONTEXT_BUTTON_BAR_WIDTH, height() - CONTEXT_BUTTON_BAR_TOP_HEIGHT);
118
119     painter.drawPixmap(CONTEXT_BUTTON_BAR_TOP_X, CONTEXT_BUTTON_BAR_TOP_Y, m_barTop);
120     painter.drawTiledPixmap(m_barRect, m_barTile);
121
122 }
123
124 void PanelContextButtonBar::move(int x, int y)
125 {
126     qDebug() << __PRETTY_FUNCTION__;
127
128     if (m_visible)
129         QWidget::move(x, y - height());
130     else
131         QWidget::move(x, y);
132
133     QPoint hiddenPosition(x, y);
134     m_stateHidden->assignProperty(this, "pos", hiddenPosition);
135
136     QPoint visiblePosition(x, y - height());
137     m_stateVisible->assignProperty(this, "pos", visiblePosition);
138 }
139
140 void PanelContextButtonBar::resizeEvent(QResizeEvent *event)
141 {
142     qDebug() << __PRETTY_FUNCTION__;
143
144     QWidget::resizeEvent(event);
145
146     QPoint visiblePosition(pos().x(), pos().y() - height());
147     m_stateVisible->assignProperty(this, "pos", visiblePosition);
148 }
149
150 void PanelContextButtonBar::setContextButtons(QWidget *contextButtons)
151 {
152     qDebug() << __PRETTY_FUNCTION__;
153
154     // Hide previous buttons (if any)
155     if (m_contextButtons)
156         m_contextButtons->setParent(0);
157
158     m_contextButtons = contextButtons;
159
160     m_contextButtons->setParent(this);
161     m_contextButtons->setVisible(true);
162
163     setFixedHeight(m_contextButtons->height());
164 }
165
166 void PanelContextButtonBar::showContextButtonBar()
167 {
168     qDebug() << __PRETTY_FUNCTION__;
169
170     if (!m_visible) {
171         m_visible = true;
172         emit toggleState();
173     }
174 }