b9ef6621ee6ac7dee837788a1e3ce3661c4d64d4
[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 <QPropertyAnimation>
24 #include <QStateMachine>
25
26 #include "panelcommon.h"
27
28 #include "listitemcontextbuttonbar.h"
29
30 ListItemContextButtonBar::ListItemContextButtonBar(QWidget *parent) :
31     QWidget(parent),
32     m_waitForOpen(false),
33     m_contextButtons(0),
34     m_newContextButtons(0),
35     m_state(StateHidden)
36 {
37     qDebug() << __PRETTY_FUNCTION__;
38
39     const int ANIMATION_DURATION_MS = 150;
40
41     m_animation = new QPropertyAnimation(this, "pos", this);
42     m_animation->setDuration(ANIMATION_DURATION_MS);
43
44     connect(m_animation, SIGNAL(finished()),
45             this, SLOT(onAnimationFinished()));
46 }
47
48 void ListItemContextButtonBar::changeButtons()
49 {
50     qDebug() << __PRETTY_FUNCTION__;
51
52     Q_ASSERT(m_animation->state() == QAbstractAnimation::Stopped);
53
54     // Hide previous buttons (if any)
55     if (m_contextButtons)
56         m_contextButtons->setParent(0);
57
58     m_contextButtons = m_newContextButtons;
59     m_newContextButtons = 0;
60     m_contextButtons->setParent(this);
61     m_contextButtons->show();
62     setFixedSize(m_contextButtons->size());
63
64     // center this widget horizontally to middle of the panel contents area and set outside of
65     // the view
66     const int FROM_PANEL_CONTENTS_LEFT = PANEL_WIDTH / 2 - m_contextButtons->width() / 2;
67     move(PANEL_TAB_BAR_WIDTH + PANEL_BAR_WIDTH + FROM_PANEL_CONTENTS_LEFT, -size().height());
68
69     // update new target values for animations
70     m_animation->setStartValue(pos());
71     const int Y = 0;
72     m_animation->setEndValue(QPoint(pos().x(), Y));
73 }
74
75 void ListItemContextButtonBar::hideContextButtonBar()
76 {
77     qDebug() << __PRETTY_FUNCTION__;
78
79     m_state = StateClosing;
80     m_animation->setDirection(QAbstractAnimation::Backward);
81     m_animation->start();
82 }
83
84 void ListItemContextButtonBar::onAnimationFinished()
85 {
86     qDebug() << __PRETTY_FUNCTION__;
87
88     if (m_animation->direction() == QAbstractAnimation::Backward) {
89         m_state = StateHidden;
90         if (m_newContextButtons) {
91             changeButtons();
92             if (m_waitForOpen) {
93                 m_waitForOpen = false;
94                 showContextButtonBar();
95             }
96         }
97     } else {
98         m_state = StateVisible;
99     }
100 }
101
102 void ListItemContextButtonBar::onListItemSelectionChanged(bool itemIsSelected)
103 {
104     qDebug() << __PRETTY_FUNCTION__;
105
106     if (itemIsSelected) {
107         if (m_newContextButtons)
108             m_waitForOpen = true;
109         else if (m_state != StateVisible)
110             showContextButtonBar();
111     } else {
112         if (m_newContextButtons)
113             m_waitForOpen = false;
114         else if (m_state != StateHidden)
115             hideContextButtonBar();
116     }
117 }
118
119 void ListItemContextButtonBar::setContextButtons(QWidget *contextButtons)
120 {
121     qDebug() << __PRETTY_FUNCTION__;
122
123     m_newContextButtons = contextButtons;
124     m_waitForOpen = false;
125
126     if (m_state != StateHidden)
127         hideContextButtonBar();
128     else
129         changeButtons();
130 }
131
132 void ListItemContextButtonBar::showContextButtonBar()
133 {
134     qDebug() << __PRETTY_FUNCTION__;
135
136     m_state = StateOpening;
137     m_animation->setDirection(QAbstractAnimation::Forward);
138     m_animation->start();
139 }