added all files
[ffqwlibrary] / libffqw-n810-1.0 / sources / ffchartlegend.cpp
1 /*
2           GNU GENERAL PUBLIC LICENSE
3                        Version 3, 29 June 2007
4
5  Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
6  Everyone is permitted to copy and distribute verbatim copies
7  of this license document, but changing it is not allowed.
8
9                             Preamble
10
11   The GNU General Public License is a free, copyleft license for
12 software and other kinds of works.
13
14   The licenses for most software and other practical works are designed
15 to take away your freedom to share and change the works.  By contrast,
16 the GNU General Public License is intended to guarantee your freedom to
17 share and change all versions of a program--to make sure it remains free
18 software for all its users.  We, the Free Software Foundation, use the
19 GNU General Public License for most of our software; it applies also to
20 any other work released this way by its authors.  You can apply it to
21 your programs, too.
22
23   When we speak of free software, we are referring to freedom, not
24 price.  Our General Public Licenses are designed to make sure that you
25 have the freedom to distribute copies of free software (and charge for
26 them if you wish), that you receive source code or can get it if you
27 want it, that you can change the software or use pieces of it in new
28 free programs, and that you know you can do these things.
29
30   To protect your rights, we need to prevent others from denying you
31 these rights or asking you to surrender the rights.  Therefore, you have
32 certain responsibilities if you distribute copies of the software, or if
33 you modify it: responsibilities to respect the freedom of others.
34
35   For example, if you distribute copies of such a program, whether
36 gratis or for a fee, you must pass on to the recipients the same
37 freedoms that you received.  You must make sure that they, too, receive
38 or can get the source code.  And you must show them these terms so they
39 know their rights.
40
41   Developers that use the GNU GPL protect your rights with two steps:
42 (1) assert copyright on the software, and (2) offer you this License
43 giving you legal permission to copy, distribute and/or modify it.
44
45   For the developers' and authors' protection, the GPL clearly explains
46 that there is no warranty for this free software.  For both users' and
47 authors' sake, the GPL requires that modified versions be marked as
48 changed, so that their problems will not be attributed erroneously to
49 authors of previous versions.
50
51   Some devices are designed to deny users access to install or run
52 modified versions of the software inside them, although the manufacturer
53 can do so.  This is fundamentally incompatible with the aim of
54 protecting users' freedom to change the software.  The systematic
55 pattern of such abuse occurs in the area of products for individuals to
56 use, which is precisely where it is most unacceptable.  Therefore, we
57 have designed this version of the GPL to prohibit the practice for those
58 products.  If such problems arise substantially in other domains, we
59 stand ready to extend this provision to those domains in future versions
60 of the GPL, as needed to protect the freedom of users.
61
62   Finally, every program is threatened constantly by software patents.
63 States should not allow patents to restrict development and use of
64 software on general-purpose computers, but in those that do, we wish to
65 avoid the special danger that patents applied to a free program could
66 make it effectively proprietary.  To prevent this, the GPL assures that
67 patents cannot be used to render the program non-free.
68
69   The precise terms and conditions for copying, distribution and
70 modification follow.
71
72 http://www.gnu.org/licenses/gpl-3.0.txt
73 */
74 /**
75  * @file ffchartlegend.cpp
76  * @brief Implementation of the FFChartLegend class
77  *
78  * @author ComArch S.A.
79  * @date 2009.08.20
80  * @version 1.0
81  */
82 #include "ffchartlegend.h"
83
84 /**
85  * Constructs a FFChartLegend with a parent.
86  */
87 FFChartLegend::FFChartLegend(QWidget* parent) :
88         FFAbstractWidget(parent)
89 {
90         this->parent = parent;
91         init();
92 }
93
94 /**
95  * A virtual destructor.
96  */
97 FFChartLegend::~FFChartLegend()
98 {
99         int num = legendSeriesTable.size();
100         for(int i = 0; i < num; ++i)
101         {
102                 delete legendSeriesTable.at(0);
103                 legendSeriesTable.remove(0);
104         }
105 }
106
107 /**
108  * Initiates all needed fields and functionality
109  */
110 void FFChartLegend::init()
111 {
112         //sets default values
113         path = LEGEND_PATH;
114
115         backgroundColor = DEFAULT_LEGEND_BACKGROUND_COLOR;
116         backgroundOpacity = DEFAULT_LEGEND_BACKGROUND_OPACITY;
117
118         actualChangingSeries = NULL;
119
120         //set style
121         setStyleSheet("background-color: transparent");
122
123         //sets layout
124         legendLayout = new QGridLayout(this);
125         legendLayout ->setMargin(0);
126         this->setLayout(legendLayout);
127
128         legendScrollArea = new FFScrollArea(this);
129         legendScrollArea->setFrameStyle(0);
130         legendScrollAreaItem = new QWidget(legendScrollArea);
131         legendScrollAreaLayout = new QGridLayout(legendScrollAreaItem);
132         legendScrollAreaItem->setLayout(legendScrollAreaLayout);
133         legendScrollAreaLayout->setMargin(0);
134         legendScrollAreaLayout->setSpacing(5);
135
136         legendScrollAreaItem->setStyleSheet("background-color: transparent");
137         legendScrollArea->setWidget(legendScrollAreaItem);
138         legendScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
139         legendScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
140         legendScrollArea->setFrameStyle(0);
141
142         legendLayout->addWidget(legendScrollArea);
143
144         legendControlPanel = new FFChartLegendControlPanel(this);
145         legendControlPanel->hide();
146
147         //connects
148         connect(legendControlPanel,
149                 SIGNAL(penChanged(QPen)),
150                 this,
151                 SLOT(changeSeriesPen(QPen)));
152 }
153
154 /**
155  * Supports click event. Shows or hide chart's legend
156  */
157 void FFChartLegend::popUp()
158 {
159         if(isVisible())
160         {
161                 this->hide();
162         }
163         else
164         {
165                 this->show();
166         }
167
168 }
169
170 /**
171  * It updates size of FFChartLegend's elements
172  * Calls changeSize() method
173  */
174 void FFChartLegend::resizeEvent(QResizeEvent* event)
175 {
176         Q_UNUSED(event)
177         if(this->isVisible())
178         {
179                 changeSize();
180         }
181
182 }
183 /**
184  * Fits size of FFChartLegend's elements chart when show event appeared.
185  * Calls changeSize() method.
186  */
187 void FFChartLegend::showEvent(QShowEvent* event)
188 {
189         Q_UNUSED(event)
190
191         changeSize();
192 }
193
194 /**
195  * Add series to FFChartLegend.
196  *
197  * @param series is a pointer to series on a FFChart
198  */
199 void FFChartLegend::addSeries(FFChartSeries* series)
200 {
201         //creates buttons representing series
202         FFScrollingCheckBox* visibleCheckbox =
203                         new FFScrollingCheckBox(legendScrollAreaItem);
204         FFLineButton* modifyButton = new FFLineButton(legendScrollAreaItem);
205
206         //sets series name in buttons
207         visibleCheckbox->setDescription(series->name());
208         visibleCheckbox->setChecked(series->isVisible());
209         visibleCheckbox->disableTitle(true);
210         visibleCheckbox->setTopMargin(8);
211         visibleCheckbox->setBottomMargin(8);
212         visibleCheckbox->setRightMargin(2);
213         visibleCheckbox->setLeftMargin(2);
214         visibleCheckbox->setAttribute(Qt::WA_DeleteOnClose);
215
216         modifyButton->setPen(*series->pen());
217         modifyButton->setAttribute(Qt::WA_DeleteOnClose);
218
219         SeriesContainer* tempSeriesPointer =
220                         new SeriesContainer(series,
221                                             visibleCheckbox,
222                                             modifyButton);
223         //adds seriesContener to series table
224         legendSeriesTable.append(tempSeriesPointer);
225
226         //adds buttons to layout
227         legendScrollAreaLayout->addWidget(legendSeriesTable.at(legendSeriesTable.size()
228                                                           - 1)->visibleCheckBox_,
229                                           legendSeriesTable.size() - 1,
230                                           0,
231                                           1,
232                                           2);
233         legendScrollAreaLayout->addWidget(legendSeriesTable.at(legendSeriesTable.size()
234                                                           - 1)->controlPanelButton_,
235                                           legendSeriesTable.size() - 1,
236                                           2,
237                                           1,
238                                           1);
239
240         //connecting buttons to legend
241         connect(modifyButton,
242                 SIGNAL(clicked()),
243                 this,
244                 SLOT(chooseChangingSeries()));
245         connect(visibleCheckbox, SIGNAL(clicked()), this, SLOT(changeVisible()));
246         changeSize();
247 }
248
249 /**
250  *  Fits size of FFChartLegend's elements
251  */
252 void FFChartLegend::changeSize()
253 {
254         // sets geometry for legend elements
255         legendControlPanel->setGeometry(0, 0, size().width(), size().height());
256
257         legendScrollAreaItem->setGeometry(0,
258                                           0,
259                                           legendScrollArea->width(),
260                                           legendSeriesTable.size()
261                                                           * (DEFAULT_BUTTON_HEIGHT
262                                                                           + 5));
263         //update legendscrollarea
264         legendScrollArea->setWidget(NULL);
265         legendScrollArea->setWidget(legendScrollAreaItem);
266 }
267
268 /**
269  * Assign pointer on actual modified series to the temporary pointer.
270  */
271 void FFChartLegend::chooseChangingSeries()
272 {
273         //check which series was chosen to modification
274         for(int i = 0; i < legendSeriesTable.size(); i++)
275         {
276                 if(legendSeriesTable.at(i)->controlPanelButton_
277                                 == (FFLineButton*)sender())
278                         actualChangingSeries = legendSeriesTable.at(i);
279         }
280
281         //sets and shows legendCotnrolPanel
282         legendControlPanel->setPen(*actualChangingSeries->series_->pen());
283         legendControlPanel->show();
284         if(NULL != parent)
285                 parent->repaint();
286 }
287
288 /**
289  * Updates a pen of button representing actual modified series and emits
290  * signal to chart.
291  *
292  * @param pen represents setting series pen
293  */
294
295 void FFChartLegend::changeSeriesPen(QPen pen)
296 {
297         actualChangingSeries->series_->setPen(pen);
298         actualChangingSeries->controlPanelButton_->setPen(pen);
299         emit updateSeries();
300 }
301
302 /**
303  * Updates series visibility
304  */
305 void FFChartLegend::changeVisible()
306 {
307         //looks for pointer to changing series and change it visibility and
308         //state of visibleCheckbox
309
310         for(int i = 0; i < legendSeriesTable.size(); i++)
311         {
312                 if(legendSeriesTable.at(i)->visibleCheckBox_
313                                 == (FFScrollingCheckBox*)sender())
314                 {
315                         bool
316                              tempVisible =
317                                            !(legendSeriesTable.at(i)->series_->isVisible());
318
319                         legendSeriesTable.at(i)->series_->setVisible(tempVisible);
320                         legendSeriesTable.at(i)->visibleCheckBox_->setChecked(tempVisible);
321                 }
322         }
323
324         emit updateSeries();
325 }
326
327 /**
328  * Deletes series from series contener
329  *
330  * @param series is a pointer to series that will be removed.
331  */
332 void FFChartLegend::deleteSeries(FFChartSeries* series)
333 {
334         //looks for given series in legendSeriesTable and deletes it.
335         for(int i = 0; i < legendSeriesTable.size(); i++)
336         {
337                 if(series == legendSeriesTable.at(i)->series_)
338                 {
339                         legendControlPanel->hide();
340
341                         delete legendSeriesTable.at(i);
342                         legendSeriesTable.remove(i);
343                 }
344
345         }
346         changeSize();
347 }
348
349 /**
350  * Draw FFChartLegend background
351  * @param event Contains all informations about event.
352  */
353 void FFChartLegend::paintEvent(QPaintEvent *event)
354 {
355         Q_UNUSED(event)
356         QPainter paint;
357         paint.begin(this);
358         paint.setOpacity(backgroundOpacity);
359         paint.setBrush(backgroundColor);
360         paint.setPen(Qt::transparent);
361         paint.drawRect(0, 0, width(), height());
362         paint.end();
363 }
364
365 /**
366  \fn void FFChartLegend::updateSeries()
367
368  This signal is emitted when the series is/are changed
369  */
370
371 /**
372  * Constructs a FFChartLegend with pointers to series, and buttons .
373  */
374 SeriesContainer::SeriesContainer(FFChartSeries* series,
375                                  FFScrollingCheckBox* visibleCheckBox,
376                                  FFLineButton* controlPanelButton)
377 {
378         series_ = series;
379         visibleCheckBox_ = visibleCheckBox;
380         controlPanelButton_ = controlPanelButton;
381 }
382
383 /**
384  * A virtual destructor
385  */
386 SeriesContainer::~SeriesContainer()
387 {
388         visibleCheckBox_->close();
389         controlPanelButton_->close();
390 }