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