2 GNU GENERAL PUBLIC LICENSE
3 Version 3, 29 June 2007
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.
11 The GNU General Public License is a free, copyleft license for
12 software and other kinds of works.
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
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.
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.
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
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.
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.
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.
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.
69 The precise terms and conditions for copying, distribution and
72 http://www.gnu.org/licenses/gpl-3.0.txt
75 * @file ffchartscene.cpp
76 * @brief Implementation of the FFChartScene class.
78 * @author ComArch S.A.
83 #include "ffchartscene.h"
85 // ############################################################ CONSTRUCTORS
88 * Constructs a FFChartScene with a parent.
90 FFChartScene::FFChartScene(QObject* parent) :
91 QGraphicsScene(parent)
97 * A virtual destructor.
99 FFChartScene::~FFChartScene()
105 * Initiates an object of FFChartScene. Sets all needed fields connections.
106 * It is called by all constructors.
108 void FFChartScene::init()
110 //sets default configuration
115 accumulatedZoomFactor_ = QSize(1, 1);
116 QBrush zoomRectBrush(QColor(60, 80, 60));
119 zoomRectPen.setBrush(zoomRectBrush);
120 zoomRectPen.setColor(QColor(80, 180, 80));
121 zoomRectPen.setWidth(3);
123 zoomRect = new QGraphicsRectItem;
124 zoomRect->setPen(zoomRectPen);
125 zoomRect->setBrush(zoomRectBrush);
126 setBackgroundBrush(QBrush(QColor(30, 30, 30)));
128 timer = new QTimer(this);
129 connect(timer, SIGNAL(timeout()), this, SLOT(kineticScroll()));
131 zoomInButton.moveBy(10,10);
132 zoomOutButton.moveBy(10,90);
133 legendButton.moveBy(10, sceneRect().height() - 70);
135 subaxesSpacing_ = 40;
136 subaxesPen_.setColor(QColor(80,80,80));
137 subaxesPen_.setStyle(Qt::DashLine);
138 subaxesPen_.setCosmetic(true);
140 axesValuesPen_.setColor(QColor(240, 240, 240));
142 //adds button to chart
143 addItem(&zoomInButton);
144 addItem(&zoomOutButton);
145 addItem(&legendButton);
150 connect(&zoomInButton, SIGNAL(mouseRelease()), this, SLOT(zoomIn()));
151 connect(&zoomOutButton, SIGNAL(mouseRelease()), this, SLOT(zoomOut()));
152 connect(&legendButton, SIGNAL(mouseRelease()), parent(), SLOT(showLegend()));
154 //sets chart's buttons
155 zoomInButton.setImage(":/standard/chart_zoomin");
156 zoomOutButton.setImage(":/standard/chart_zoomout");
157 legendButton.setImage(":/standard/chart_legendON",":/standard/chart_legendOFF");
159 zoomInButton.setZoomRatio(1.4);
160 zoomOutButton.setZoomRatio(0.6);
161 legendButton.setZoomRatio(1.4);
163 zoomInButton.setSize(QSize(60, 60));
164 zoomOutButton.setSize(QSize(60, 60));
165 legendButton.setSize(QSize(60, 60));
169 // ################################################################## PUBLIC
172 * Moves given point of chart to the scene's top left corner.
174 void FFChartScene::moveTo(QPoint point)
176 translateFactor_ = point;
181 * Moves given point of chart to the scene's top left corner.
183 void FFChartScene::moveTo(float x, float y)
185 translateFactor_ = QPointF(x, y);
190 * Moves the chart by a given vector.
192 void FFChartScene::moveBy(QPoint destPoint)
194 translateFactor_ += destPoint;
199 * Moves the chart by a given vector.
201 void FFChartScene::moveBy(float x, float y)
203 translateFactor_ += QPointF(x, y);
208 * Adds set of points to chart.
210 void FFChartScene::addSeries(FFChartSeries* series)
212 this->series.append(series);
213 itemsBoundingRect_ = itemsBoundingRect(&itemsBoundingValues_);
217 emit seriesChanged(this->series);
221 * Sets a list of series.
223 void FFChartScene::setSeries(QList<FFChartSeries*> series)
225 this->series = series;
226 itemsBoundingRect_ = itemsBoundingRect(&itemsBoundingValues_);
230 emit seriesChanged(&series);
234 * Overridden method from QGraphicsScene.
236 void FFChartScene::setSceneRect(const QRectF& rect)
238 QGraphicsScene::setSceneRect(rect);
239 legendButton.setPos(10, rect.height() - 70);
241 translateFactor_.setX(rect.width() * 0.5);
242 translateFactor_.setY(rect.height() * 0.5);
248 * Sets a distance in pixels between subaxes.
250 void FFChartScene::setSubaxesSpacing(const float& subaxesSpacing)
252 subaxesSpacing_ = subaxesSpacing;
256 * Returns a distance in pixels between subaxes.
258 float FFChartScene::subaxesSpacing() const
260 return subaxesSpacing_;
264 * Sets a pen that will be used to draw subaxes.
266 void FFChartScene::setSubaxesPen(const QPen& subaxesPen)
268 subaxesPen_ = subaxesPen;
272 * Returns a pen that is used to draw subaxes.
274 QPen FFChartScene::subaxesPen() const
280 * Sets a pen that will be used to draw values next to subaxes.
282 void FFChartScene::setAxesValuesPen(const QPen& axesValuesPen)
284 axesValuesPen_ = axesValuesPen;
288 * Returns a pen that is used to draw values next to subaxes.
290 QPen FFChartScene::axesValuesPen() const
292 return axesValuesPen_;
295 // ############################################################ PUBLIC SLOTS
298 * Zooms the chart into the given rectangle.
300 void FFChartScene::zoom(QRectF zoomRect)
302 // Normalizing zoom rectangle (width and height must are greater than 0)
303 QRectF zoomRectNormalized(zoomRect);
304 if(zoomRect.width() < 0)
306 zoomRectNormalized.setX(zoomRect.x() + zoomRect.width());
307 zoomRectNormalized.setWidth(qAbs(zoomRect.width()));
309 if(zoomRect.height() < 0)
311 zoomRectNormalized.setY(zoomRect.y() + zoomRect.height());
312 zoomRectNormalized.setHeight(qAbs(zoomRect.height()));
315 // Calculating zoom factor for scale() function ( >1 zoomin, <1 zoomout )
316 QSizeF zoomFactor(sceneRec.width() / zoomRectNormalized.width(),
317 sceneRec.height() / zoomRectNormalized.height());
319 accumulatedZoomFactor_.setWidth(zoomFactor.width()
320 * accumulatedZoomFactor_.width());
321 accumulatedZoomFactor_.setHeight(zoomFactor.height()
322 * accumulatedZoomFactor_.height());
323 //checking if factor of zooming is not to high
324 if(accumulatedZoomFactor_.width() > 20 || accumulatedZoomFactor_.height() > 20)
326 accumulatedZoomFactor_.setWidth(20);
327 accumulatedZoomFactor_.setHeight(20);
333 translateFactor_ = QPointF((translateFactor_.x()
334 - zoomRectNormalized.x())
335 * zoomFactor.width(),
336 (translateFactor_.y()
337 - zoomRectNormalized.y())
338 * zoomFactor.height());
344 * Slot that changes series' set.
346 void FFChartScene::seriesChanged(QList<FFChartSeries*> series)
352 * Slot that zooms in the current chart into a rectangle which is smaller by
355 void FFChartScene::zoomIn(qreal ratio)
359 if(accumulatedZoomFactor_.width() > 15)
365 dx = ratio * sceneRec.width();
368 if(accumulatedZoomFactor_.height() > 15)
374 dy = ratio * sceneRec.height();
377 zoom(QRectF(dx, dy, sceneRec.width() - 2 * dx, sceneRec.height() - 2
382 * Slot that zooms out the current chart into a rectangle which is larger by
385 void FFChartScene::zoomOut(qreal ratio)
389 if(accumulatedZoomFactor_.width() < 0.1)
395 dx = ratio * sceneRec.width();
398 if(accumulatedZoomFactor_.height() < 0.1)
404 dy = ratio * sceneRec.height();
407 zoom(QRectF(-dx, -dy, sceneRec.width() + 2 * dx, sceneRec.height() + 2
411 // ############################################################### PROTECTED
414 * Overridden method from QGraphicsScene. Draws all items that should be seen
417 void FFChartScene::drawItems(QPainter* painter,
419 QGraphicsItem* items[],
420 const QStyleOptionGraphicsItem options[],
427 // Drawing zoom rectangle
430 zoomRect->paint(painter, new QStyleOptionGraphicsItem, widget);
433 painter->scale(1, 1);
437 drawSubAxes(painter, widget);
440 drawAxes(painter, widget);
443 drawSeries(painter, widget);
445 // Drawing axes values
446 drawValues(painter, widget);
448 // Drawing static items
449 drawStaticItems(painter, widget);
453 * Overridden virtual method from QGraphicsScene. It is responsible for
454 * handling a mouse press event.
455 * @param event Contains all informations about event.
457 void FFChartScene::mousePressEvent(QGraphicsSceneMouseEvent* event)
459 QGraphicsScene::mousePressEvent(event);
465 if(itemAt(event->scenePos()))
470 pressPos = event->scenePos();
474 if(timer->isActive() == false)
483 * Overridden virtual method from QGraphicsScene. It is responsible for
484 * handling a mouse double click event.
485 * @param event Contains all informations about event.
487 void FFChartScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
489 if(itemAt(event->scenePos()))
499 zoomRect->setRect(event->scenePos().x(), event->scenePos().y(), 1, 1);
504 * Overridden virtual method from QGraphicsScene. It is responsible for
505 * handling a mouse release event.
506 * @param event Contains all informations about event.
508 void FFChartScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
510 QGraphicsScene::mouseReleaseEvent(event);
512 if(zoomModeSw) // zooming
516 if(qAbs(zoomRect->rect().width()) <= 15
517 || qAbs(zoomRect->rect().height()) <= 15)
523 zoom(zoomRect->rect());
526 else if(moveModeSw) //moving
530 if(oldKineticVec != kineticVec && kineticCount > 2 && zoomModeSw == false)
533 kineticVec *= kineticRatio;
544 * Overridden virtual method from QGraphicsScene. It is responsible for
545 * handling a mouse move event.
546 * @param event Contains all informations about event.
548 void FFChartScene::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
551 //if button's type is not left button return
552 if(event->buttons() != Qt::LeftButton)
563 zoomRect->setRect(pressPos.x(),
565 event->scenePos().x() - pressPos.x(),
566 event->scenePos().y() - pressPos.y());
571 QPointF scrollRatio(event->scenePos().x() - pressPos.x(),
572 event->scenePos().y() - pressPos.y());
574 oldPressPos = pressPos;
575 pressPos = event->scenePos();
576 translateFactor_ += scrollRatio;
581 // ################################################################ PPRIVATE
584 * Returns a rectangle around all points in series
585 * @param boundaryValues vector of boundary values
586 * @return bounding rectangle QRectF
588 QRectF FFChartScene::itemsBoundingRect(QVector<float>* boundaryValues)
590 //if any series are available returns zero rectangle
593 return QRectF(0, 0, 0, 0);
595 //if only one series is available returns rectangle for points from one
597 if(series.size() == 1 && series.at(0)->size() == 1)
599 return QRectF(series.at(0)->at(0)->x(),
600 series.at(0)->at(0)->y(),
604 //looks for maximum points in all series
605 float minX = series.at(0)->at(0)->x();
606 float maxX = series.at(0)->at(0)->x();
607 float minY = series.at(0)->at(0)->y();
608 float maxY = series.at(0)->at(0)->y();
610 for(int i = 0; i < series.size(); ++i)
612 for(int j = 0; j < series.at(i)->size(); ++j)
614 if(series.at(i)->at(j)->x() < minX)
616 minX = series.at(i)->at(j)->x();
619 if(series.at(i)->at(j)->y() < minY)
621 minY = series.at(i)->at(j)->y();
624 if(series.at(i)->at(j)->x() > maxX)
626 maxX = series.at(i)->at(j)->x();
629 if(series.at(i)->at(j)->y() > maxY)
631 maxY = series.at(i)->at(j)->y();
638 boundaryValues->clear();
639 boundaryValues->append(minX);
640 boundaryValues->append(minY);
641 boundaryValues->append(maxX);
642 boundaryValues->append(maxY);
645 return QRectF(minX, maxY, maxX - minX, maxY - minY);
649 * Slot that zooms in by default value.
651 void FFChartScene::zoomIn()
653 zoomIn(defaultZoomInRatio);
657 * Slot that zooms out by default value.
659 void FFChartScene::zoomOut()
661 zoomOut(defaultZoomOutRatio);
666 * @param painter a tool to drawing elements
667 * @param widget points to the widget where elements will be drew
669 void FFChartScene::drawAxes(QPainter* painter, QWidget* widget)
675 painter->setPen(QColor(255, 255, 250));
678 painter->drawLine(QPointF(0,
679 translateFactor_.y()),
680 QPointF(sceneRec.width(),
681 translateFactor_.y()));
684 painter->drawLine(QPointF(translateFactor_.x(),
686 QPointF(translateFactor_.x(),
694 * @param painter a tool to drawing elements
695 * @param widget points to the widget where elements will be drew
697 void FFChartScene::drawSubAxes(QPainter* painter, QWidget* widget)
703 //draws horizontal lines
704 int subaxesNumber = (int)(sceneRec.height() / subaxesSpacing_);
705 qreal offset = translateFactor_.y() - (qFloor(translateFactor_.y() / subaxesSpacing_)) * subaxesSpacing_;
707 painter->setPen(subaxesPen_);
710 for(i = 0; i <= subaxesNumber; i++)
712 painter->drawLine(QPointF(0,
713 offset + i * subaxesSpacing_),
714 QPointF(sceneRec.width(),
715 offset + i * subaxesSpacing_));
718 //draws vertical lines
719 subaxesNumber = (int)(sceneRec.width() / subaxesSpacing_);
720 offset = translateFactor_.x() - (qFloor(translateFactor_.x() / subaxesSpacing_)) * subaxesSpacing_;
722 for(i = 0; i <= subaxesNumber; i++)
724 painter->drawLine(QPointF(offset + i * subaxesSpacing_,
726 QPointF(offset + i * subaxesSpacing_,
734 * Draws all series on the chart.
735 * @param painter a tool to drawing elements
736 * @param widget points to the widget where elements will be drew
738 void FFChartScene::drawSeries(QPainter* painter, QWidget* widget)
744 //sets painter's configuration
745 painter->translate(translateFactor_);
746 painter->scale(accumulatedZoomFactor_.width(),
747 accumulatedZoomFactor_.height());
749 transform = painter->transform();
750 transform.rotate(180, Qt::XAxis);
751 painter->setTransform(transform, false);
754 for(int i = 0; i < series.size(); ++i)
756 if(!series.at(i)->isVisible())
761 painter->setPen(*series.at(i)->pen());
762 painter->drawPolyline(series.at(i)->data(), series.at(i)->size());
769 * Draws static items (they are not moving and being scaled) on the chart,
771 * @param painter a tool to drawing elements
772 * @param widget points to the widget where elements will be drew
774 void FFChartScene::drawStaticItems(QPainter* painter, QWidget* widget)
778 painter->setMatrix(zoomInButton.sceneMatrix(), true);
779 zoomInButton.paint(painter, new QStyleOptionGraphicsItem, widget);
782 //draws zoomOutButton
784 painter->setMatrix(zoomOutButton.sceneMatrix(), true);
785 zoomOutButton.paint(painter, new QStyleOptionGraphicsItem, widget);
790 painter->setMatrix(legendButton.sceneMatrix(), true);
791 legendButton.paint(painter, new QStyleOptionGraphicsItem, widget);
796 * Draws values next to subaxes.
797 * @param painter a tool to drawing elements
798 * @param widget points to the widget where elements will be drew
800 void FFChartScene::drawValues(QPainter* painter, QWidget* widget)
807 painter->setPen(axesValuesPen_);
808 painter->setFont(QFont(FONT_TITLE_DEF, 10));
811 subaxesSpacing_ *= 2;
813 //drawing values of axes
814 int subaxesNumber = (int)(sceneRec.height() / subaxesSpacing_);
815 qreal offset = translateFactor_.y() - (qFloor(translateFactor_.y() / subaxesSpacing_)) * subaxesSpacing_;
820 if(translateFactor_.x() < 0)
822 for(i = -1; i <= subaxesNumber; i++)
824 painter->drawText(QPointF(1, offset + i * subaxesSpacing_ + 11), QString::number((-translateFactor_.y() + offset + i * subaxesSpacing_) / -accumulatedZoomFactor_.height(), 'f', 1));
827 else if(translateFactor_.x() > sceneRec.width())
829 for(i = -1; i <= subaxesNumber; i++)
831 painter->drawText(QRectF(sceneRec.width() - 305, offset + i * subaxesSpacing_ + 11, 300, 11), Qt::AlignRight | Qt::AlignVCenter, QString::number((-translateFactor_.y() + offset + i * subaxesSpacing_) / -accumulatedZoomFactor_.height(), 'f', 1));
836 for(i = -1; i <= subaxesNumber; i++)
838 painter->drawText(QPointF(translateFactor_.x() + 1, offset + i * subaxesSpacing_ + 11), QString::number((-translateFactor_.y() + offset + i * subaxesSpacing_) / -accumulatedZoomFactor_.height(), 'f', 1));
842 subaxesNumber = (int)(sceneRec.width() / subaxesSpacing_);
843 offset = translateFactor_.x() - (qFloor(translateFactor_.x() / subaxesSpacing_)) * subaxesSpacing_;
845 if(translateFactor_.y() < 0)
847 for(i = -1; i <= subaxesNumber; i++)
849 painter->drawText(QPointF(offset + i * subaxesSpacing_ + 1, 11), QString::number((-translateFactor_.x() + offset + i * subaxesSpacing_) / accumulatedZoomFactor_.width(), 'f', 1));
852 else if(translateFactor_.y() > sceneRec.height())
854 for(i = -1; i <= subaxesNumber; i++)
856 painter->drawText(QPointF(offset + i * subaxesSpacing_ + 1, sceneRec.height() - 5), QString::number((-translateFactor_.x() + offset + i * subaxesSpacing_) / accumulatedZoomFactor_.width(), 'f', 1));
861 for(i = -1; i <= subaxesNumber; i++)
863 painter->drawText(QPointF(offset + i * subaxesSpacing_ + 1, translateFactor_.y() + 11), QString::number((-translateFactor_.x() + offset + i * subaxesSpacing_) / accumulatedZoomFactor_.width(), 'f', 1));
867 subaxesSpacing_ /= 2;
873 * This method is responsible for kinetic scrolling.
875 void FFChartScene::kineticScroll()
877 //check if kinetic mode is active
880 translateFactor_ += kineticVec;
882 if(qAbs((kineticVec *= 0.8).x()) < 0.5 && qAbs((kineticVec
891 //otherwise updates kinetic's vectors
894 oldKineticVec = kineticVec;
895 kineticVec = pressPos - oldPressPos;
900 * \fn void FFChartScene::seriesChanged(QList<FFChartSeries*>*);
902 * Signal that is emitted when the set of series has changed.