Centering, re-factoring
[situare] / src / ui / listitemcontextbuttonbar.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Sami Rämö - sami.ramo@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 <QLayout>
24 #include <QPainter>
25 #include <QPropertyAnimation>
26 #include <QStateMachine>
27
28 #include "panelcommon.h"
29
30 #include "listitemcontextbuttonbar.h"
31
32 ListItemContextButtonBar::ListItemContextButtonBar(QWidget *parent) :
33     QWidget(parent),
34     m_waitForOpen(false),
35     m_contextButtons(0),
36     m_newContextButtons(0),
37     m_state(StateHidden)
38 {
39     qDebug() << __PRETTY_FUNCTION__;
40
41     // --- BAR LOOK ---
42     const int BAR_HEIGHT = 78;
43     setFixedHeight(BAR_HEIGHT);
44
45     const int BUTTON_WIDTH = 74;
46     const int BUTTON_COUNT = 3;
47     const int HORIZONTAL_MARGIN = 10;
48     setFixedWidth(BUTTON_COUNT * BUTTON_WIDTH + 2 * HORIZONTAL_MARGIN);
49
50     m_backgroundLeft = new QPixmap(":/res/images/list_item_context_button_bar_left.png");
51     m_backgroundMiddle = new QPixmap(":/res/images/list_item_context_button_bar_tile.png");
52     m_backgroundRight = new QPixmap(":/res/images/list_item_context_button_bar_right.png");
53
54     // --- LAYOUT ---
55     m_barLayout = new QHBoxLayout();
56     setLayout(m_barLayout);
57     m_barLayout->setContentsMargins(0, 0, 0, 0);
58
59     // --- SET POSITION ---
60     // center this widget horizontally to middle of the panel contents area and set outside of
61     // the view
62     const int FROM_PANEL_CONTENTS_LEFT = PANEL_WIDTH / 2 - width() / 2;
63     move(PANEL_TAB_BAR_WIDTH + PANEL_BAR_WIDTH + FROM_PANEL_CONTENTS_LEFT, -size().height());
64
65     // --- ANIMATION ---
66     const int ANIMATION_DURATION_MS = 150;
67
68     m_animation = new QPropertyAnimation(this, "pos", this);
69     m_animation->setDuration(ANIMATION_DURATION_MS);
70
71     connect(m_animation, SIGNAL(finished()),
72             this, SLOT(onAnimationFinished()));
73
74     // target values for animations
75     m_animation->setStartValue(pos());
76     const int Y = 0;
77     m_animation->setEndValue(QPoint(pos().x(), Y));
78 }
79
80 ListItemContextButtonBar::~ListItemContextButtonBar()
81 {
82     qDebug() << __PRETTY_FUNCTION__;
83
84     if (m_backgroundLeft)
85         delete m_backgroundLeft;
86
87     if (m_backgroundMiddle)
88         delete m_backgroundMiddle;
89
90     if (m_backgroundRight)
91         delete m_backgroundRight;
92 }
93
94 void ListItemContextButtonBar::changeButtons()
95 {
96     qDebug() << __PRETTY_FUNCTION__;
97
98     Q_ASSERT(m_animation->state() == QAbstractAnimation::Stopped);
99
100     if (!isVisible())
101         show();
102
103     // Remove old buttons (if any)
104     if (m_contextButtons) {
105         m_barLayout->removeWidget(m_contextButtons);
106         m_contextButtons->setParent(0);
107     }
108
109     m_contextButtons = m_newContextButtons;
110     m_newContextButtons = 0;
111
112     m_barLayout->addWidget(m_contextButtons, 0, Qt::AlignHCenter);
113 }
114
115 void ListItemContextButtonBar::hideContextButtonBar()
116 {
117     qDebug() << __PRETTY_FUNCTION__;
118
119     m_state = StateClosing;
120     m_animation->setDirection(QAbstractAnimation::Backward);
121     m_animation->start();
122 }
123
124 void ListItemContextButtonBar::onAnimationFinished()
125 {
126     qDebug() << __PRETTY_FUNCTION__;
127
128     if (m_animation->direction() == QAbstractAnimation::Backward) {
129         m_state = StateHidden;
130         if (m_newContextButtons) {
131             changeButtons();
132             if (m_waitForOpen) {
133                 m_waitForOpen = false;
134                 showContextButtonBar();
135             }
136         }
137     } else {
138         m_state = StateVisible;
139     }
140 }
141
142 void ListItemContextButtonBar::onListItemSelectionChanged(bool itemIsSelected)
143 {
144     qDebug() << __PRETTY_FUNCTION__;
145
146     if (itemIsSelected) {
147         if (m_newContextButtons)
148             m_waitForOpen = true;
149         else if (m_state != StateVisible)
150             showContextButtonBar();
151     } else {
152         if (m_newContextButtons)
153             m_waitForOpen = false;
154         else if (m_state != StateHidden)
155             hideContextButtonBar();
156     }
157 }
158
159 void ListItemContextButtonBar::paintEvent(QPaintEvent *event)
160 {
161     qDebug() << __PRETTY_FUNCTION__;
162
163     Q_UNUSED(event);
164
165     QPainter painter(this);
166
167     const int TOP = 0;
168     const int LEFT = 0;
169     painter.drawPixmap(TOP, LEFT, *m_backgroundLeft);
170     painter.drawTiledPixmap(m_backgroundLeft->width(), TOP,
171                             width() - m_backgroundLeft->width() - m_backgroundRight->width(),
172                             height(),
173                             *m_backgroundMiddle);
174     painter.drawPixmap(width() - m_backgroundRight->width(), TOP, *m_backgroundRight);
175 }
176
177 void ListItemContextButtonBar::setContextButtons(QWidget *contextButtons)
178 {
179     qDebug() << __PRETTY_FUNCTION__;
180
181     m_newContextButtons = contextButtons;
182     m_waitForOpen = false;
183
184     if (m_state != StateHidden)
185         hideContextButtonBar();
186     else
187         changeButtons();
188 }
189
190 void ListItemContextButtonBar::showContextButtonBar()
191 {
192     qDebug() << __PRETTY_FUNCTION__;
193
194     if (m_contextButtons->layout()->count() > 0) {
195         m_state = StateOpening;
196         m_animation->setDirection(QAbstractAnimation::Forward);
197         m_animation->start();
198     }
199 }