added all files
[ffqwlibrary] / libffqw-1.0 / sources / ffchart.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 ffchart.cpp
76  * @brief Implementation of the FFChartScene class.
77  *
78  * @author ComArch S.A.
79  * @date 2009.09.01
80  * @version 1.1
81  */
82
83 #include "ffchart.h"
84
85
86 /**
87  * Constructs a FFChart with a parent.
88  */
89 FFChart::FFChart(QWidget* parent) :
90         FFAbstractWidget(parent)
91 {
92         init();
93 }
94
95 /**
96  * A virtual destructor.
97  */
98 FFChart::~FFChart()
99 {
100
101 }
102
103 /**
104  * Initiates an object of FFChart. Sets all needed fields and connections.
105  * It is called by all constructors.
106  */
107 void FFChart::init()
108 {
109         //sets size policy
110         setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
111
112
113         // creates needed objects
114         view = new QGraphicsView(this);
115         scene = new FFChartScene(this);
116         legend = new FFChartLegend(this);
117
118         //sets default values
119         autoSort_ = true;
120         autoValidate_ = true;
121
122         //sets view
123         view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
124         view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
125         view->setScene(scene);
126         //necessary with our librray in Qt 4.6
127 #ifndef DIABLO_COMP
128         view->setOptimizationFlag(QGraphicsView::IndirectPainting);
129 #endif
130  
131         //sets legend size
132         legend->setGeometry(width() / 2, 0, width() / 2, height());
133         //hides legend
134         legend->hide();
135
136
137
138         view->installEventFilter(this);
139
140         //connects
141         connect(this,SIGNAL(seriesChanged(QList<FFChartSeries*>)),scene,SLOT(seriesChanged(QList<FFChartSeries*>)));
142         connect(legend, SIGNAL(updateSeries()), scene, SLOT(update()));
143 }
144
145 /**
146  * Adds new series that will be drawn on the chart.
147  */
148 void FFChart::addSeries(FFChartSeries* series)
149 {
150
151         //series validation
152         if(autoValidate_)
153         {
154                 series->validate();
155         }
156         //series' point sorting
157         if(autoSort_)
158         {
159                 series->sort();
160         }
161
162
163         series->pen()->setCosmetic(true);
164
165         //add series to sies list and legend
166         series_.append(series);
167
168         legend->addSeries(series);
169
170         //updates scene
171         scene->update();
172
173
174         emit seriesChanged(series_);
175 }
176
177 /**
178  * Removes series indicated by given pointer.
179  */
180 void FFChart::removeSeries(FFChartSeries* series)
181 {
182         //searches for series and remove it
183         for(int i = 0; i < series_.size(); ++i)
184         {
185                 if(series_[i] == series)
186                 {
187                         legend->deleteSeries(series);
188                         series_.removeAt(i);
189                 }
190         }
191
192         //update scene
193         scene->update();
194
195         emit seriesChanged(series_);
196 }
197
198 /**
199  * Removes all series.
200  */
201 void FFChart::removeSeries()
202 {
203         //removes series
204         int num = series_.size();
205         for(int i = 0; i < num; ++i)
206         {
207                 legend->deleteSeries(series_.at(0));
208                 series_.removeAt(0);
209         }
210         //updates scene
211         scene->update();
212
213         emit seriesChanged(series_);
214 }
215
216 /**
217  * Returns a list with pointers to all series contained in the chart.
218  */
219 QList<FFChartSeries*> FFChart::series()
220 {
221         return series_;
222 }
223
224 /**
225  * Returns pointer to series with given name.
226  * @return pointer to the looking series or NULL if series is not found
227  */
228 FFChartSeries* FFChart::series(QString name)
229 {
230         //return pointer to first added series with given name
231
232         for(int i=0; i<series_.size();++i)
233         {
234                 if(series_.at(i)->name() == name)
235                 {
236                         return series_[i];
237                 }
238         }
239         //If found nothing NULL is returned
240         return NULL;
241 }
242
243 /**
244  * Turns on/off auto-sorting on series. If it is on then each newly added series
245  * will be sorted by 'x' values.
246  */
247 void FFChart::setAutoSort(const bool& autoSort)
248 {
249         autoSort_ = autoSort;
250 }
251
252 /**
253  * Turns on/off auto-validating on series. If it is on then each newly added
254  * series will be cleaned with the same points.
255  */
256 void FFChart::setAutoValidate(const bool& autoValidate)
257 {
258         autoValidate_ = autoValidate;
259 }
260
261 /**
262  * Returns true if auto-sorting is enabled, otherwise false.
263  */
264 bool FFChart::isAutoSortEnabled() const
265 {
266         return autoSort_;
267 }
268
269 /**
270  * Returns true if auto-validating is enabled, otherwise false.
271  */
272 bool FFChart::isAutoValidateEnabled() const
273 {
274         return autoValidate_;
275 }
276
277 /**
278  * Sets object that will be responsible for managing with events sent to
279  *  the FFChart.
280  */
281 void FFChart::installEventFilter(QObject* object)
282 {
283         view->installEventFilter(object);
284 }
285
286 /**
287  * Slot that zooms in the current chart into a rectangle which is smaller by
288  * a given ratio.
289  */
290 void FFChart::zoomIn(qreal ratio)
291 {
292         scene->zoomIn(ratio);
293 }
294
295 /**
296  * Slot that zooms out the current chart into a rectangle which is larger by
297  * a given ratio.
298  */
299 void FFChart::zoomOut(qreal ratio)
300 {
301         scene->zoomOut(ratio);
302 }
303
304 /**
305  * Moves the chart by given vector.
306  */
307 void FFChart::moveBy(QPointF point)
308 {
309         scene->moveBy(point.toPoint());
310 }
311
312 /**
313  * Overridden virtual method. It is responsible for managing with resize events.
314  */
315 void FFChart::resizeEvent(QResizeEvent* event)
316 {
317         Q_UNUSED(event)
318         view->resize(size());
319         scene->setSceneRect(QRectF(0,0,event->size().width(),event->size().height()));
320         legend->setGeometry(width() / 2, 0, width() / 2, height());
321
322         QWidget::resizeEvent(event);
323 }
324
325 /**
326  * Toggles the legend. If it is visible, this method makes it hidden and
327  * vice versa.
328  */
329 void FFChart::showLegend()
330 {
331         if(legend->isVisible())
332         {
333                 legend->hide();
334                 this->repaint();
335         }
336         else
337         {
338                 legend->show();
339                 this->repaint();
340         }
341 }
342
343 /*!
344  * \fn void FFChart::seriesChanged(QList<FFChartSeries*>);
345  *
346  * This Signal  is emitted when the set of series was changed.
347  */