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