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