Added mask calculation to panel.
[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(pos().x(), pos().y());
68     m_stateHidden->assignProperty(this, "pos", hiddenPosition);
69
70     QPoint visiblePosition(pos().x(), 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::move(int x, int y)
104 {
105     qDebug() << __PRETTY_FUNCTION__;
106
107     if (m_visible)
108         QWidget::move(x, y - height());
109     else
110         QWidget::move(x, y);
111
112     QPoint hiddenPosition(x, y);
113     m_stateHidden->assignProperty(this, "pos", hiddenPosition);
114
115     QPoint visiblePosition(x, y - height());
116     m_stateVisible->assignProperty(this, "pos", visiblePosition);
117 }
118
119 void PanelContextButtonBar::paintEvent(QPaintEvent *event)
120 {
121     qDebug() << __PRETTY_FUNCTION__;
122
123     Q_UNUSED(event);
124
125     const int CONTEXT_BUTTON_BAR_RECT_X = 0;
126     const int CONTEXT_BUTTON_BAR_TOP_HEIGHT = 32;
127     const int CONTEXT_BUTTON_BAR_TOP_X = 0;
128     const int CONTEXT_BUTTON_BAR_TOP_Y = 0;
129
130     QPainter painter(this);
131
132     m_barRect.setRect(CONTEXT_BUTTON_BAR_RECT_X, CONTEXT_BUTTON_BAR_TOP_HEIGHT,
133                       PANEL_CONTEXT_BUTTON_BAR_WIDTH, height() - CONTEXT_BUTTON_BAR_TOP_HEIGHT);
134
135     painter.drawPixmap(CONTEXT_BUTTON_BAR_TOP_X, CONTEXT_BUTTON_BAR_TOP_Y, m_barTop);
136     painter.drawTiledPixmap(m_barRect, m_barTile);
137 }
138
139 void PanelContextButtonBar::setContextButtons(QWidget *contextButtons)
140 {
141     qDebug() << __PRETTY_FUNCTION__;
142
143     // Hide previous buttons (if any)
144     if (m_contextButtons)
145         m_contextButtons->setParent(0);
146
147     m_contextButtons = contextButtons;
148
149     m_contextButtons->setParent(this);
150     m_contextButtons->setVisible(true);
151
152     setFixedHeight(m_contextButtons->height());
153
154     emit positionChangeRequested();
155 }
156
157 void PanelContextButtonBar::showContextButtonBar()
158 {
159     qDebug() << __PRETTY_FUNCTION__;
160
161     if (!m_visible) {
162         m_visible = true;
163         emit toggleState();
164     }
165 }