4a8edb59dd637285268f3be35b98e36056853af8
[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     // Hide previous buttons (if any)
79     if (m_contextButtons)
80         m_contextButtons->setParent(0);
81
82     m_contextButtons = m_newContextButtons;
83     m_newContextButtons = 0;
84     m_contextButtons->setParent(this);
85     m_contextButtons->show();
86     setFixedSize(m_contextButtons->size());
87
88     // center this widget horizontally to middle of the panel contents area and set outside of
89     // the view
90     const int FROM_PANEL_CONTENTS_LEFT = PANEL_WIDTH / 2 - m_contextButtons->width() / 2;
91     move(PANEL_TAB_BAR_WIDTH + PANEL_BAR_WIDTH + FROM_PANEL_CONTENTS_LEFT, -size().height());
92
93     // update new target values for animations
94     m_animation->setStartValue(pos());
95     const int Y = 0;
96     m_animation->setEndValue(QPoint(pos().x(), Y));
97 }
98
99 void ListItemContextButtonBar::hideContextButtonBar()
100 {
101     qDebug() << __PRETTY_FUNCTION__;
102
103     m_state = StateClosing;
104     m_animation->setDirection(QAbstractAnimation::Backward);
105     m_animation->start();
106 }
107
108 void ListItemContextButtonBar::onAnimationFinished()
109 {
110     qDebug() << __PRETTY_FUNCTION__;
111
112     if (m_animation->direction() == QAbstractAnimation::Backward) {
113         m_state = StateHidden;
114         if (m_newContextButtons) {
115             changeButtons();
116             if (m_waitForOpen) {
117                 m_waitForOpen = false;
118                 showContextButtonBar();
119             }
120         }
121     } else {
122         m_state = StateVisible;
123     }
124 }
125
126 void ListItemContextButtonBar::onListItemSelectionChanged(bool itemIsSelected)
127 {
128     qDebug() << __PRETTY_FUNCTION__;
129
130     if (itemIsSelected) {
131         if (m_newContextButtons)
132             m_waitForOpen = true;
133         else if (m_state != StateVisible)
134             showContextButtonBar();
135     } else {
136         if (m_newContextButtons)
137             m_waitForOpen = false;
138         else if (m_state != StateHidden)
139             hideContextButtonBar();
140     }
141 }
142
143 void ListItemContextButtonBar::paintEvent(QPaintEvent *event)
144 {
145     qDebug() << __PRETTY_FUNCTION__;
146
147     Q_UNUSED(event);
148
149     QPainter painter(this);
150
151     painter.drawPixmap(0, 0, *m_backgroundLeft);
152     painter.drawTiledPixmap(24, 0, width() - 2 * 24, 78, *m_backgroundMiddle);
153     painter.drawPixmap(width() - 24, 0, *m_backgroundRight);
154 }
155
156 void ListItemContextButtonBar::setContextButtons(QWidget *contextButtons)
157 {
158     qDebug() << __PRETTY_FUNCTION__;
159
160     m_newContextButtons = contextButtons;
161     m_waitForOpen = false;
162
163     if (m_state != StateHidden)
164         hideContextButtonBar();
165     else
166         changeButtons();
167 }
168
169 void ListItemContextButtonBar::showContextButtonBar()
170 {
171     qDebug() << __PRETTY_FUNCTION__;
172
173     m_state = StateOpening;
174     m_animation->setDirection(QAbstractAnimation::Forward);
175     m_animation->start();
176 }