added all files
[ffqwlibrary] / libffqw-n810-1.0 / sources / ffchartlegend.cpp
diff --git a/libffqw-n810-1.0/sources/ffchartlegend.cpp b/libffqw-n810-1.0/sources/ffchartlegend.cpp
new file mode 100644 (file)
index 0000000..81c7c0f
--- /dev/null
@@ -0,0 +1,390 @@
+/*
+         GNU GENERAL PUBLIC LICENSE
+                       Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+                            Preamble
+
+  The GNU General Public License is a free, copyleft license for
+software and other kinds of works.
+
+  The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works.  By contrast,
+the GNU General Public License is intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users.  We, the Free Software Foundation, use the
+GNU General Public License for most of our software; it applies also to
+any other work released this way by its authors.  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+  To protect your rights, we need to prevent others from denying you
+these rights or asking you to surrender the rights.  Therefore, you have
+certain responsibilities if you distribute copies of the software, or if
+you modify it: responsibilities to respect the freedom of others.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must pass on to the recipients the same
+freedoms that you received.  You must make sure that they, too, receive
+or can get the source code.  And you must show them these terms so they
+know their rights.
+
+  Developers that use the GNU GPL protect your rights with two steps:
+(1) assert copyright on the software, and (2) offer you this License
+giving you legal permission to copy, distribute and/or modify it.
+
+  For the developers' and authors' protection, the GPL clearly explains
+that there is no warranty for this free software.  For both users' and
+authors' sake, the GPL requires that modified versions be marked as
+changed, so that their problems will not be attributed erroneously to
+authors of previous versions.
+
+  Some devices are designed to deny users access to install or run
+modified versions of the software inside them, although the manufacturer
+can do so.  This is fundamentally incompatible with the aim of
+protecting users' freedom to change the software.  The systematic
+pattern of such abuse occurs in the area of products for individuals to
+use, which is precisely where it is most unacceptable.  Therefore, we
+have designed this version of the GPL to prohibit the practice for those
+products.  If such problems arise substantially in other domains, we
+stand ready to extend this provision to those domains in future versions
+of the GPL, as needed to protect the freedom of users.
+
+  Finally, every program is threatened constantly by software patents.
+States should not allow patents to restrict development and use of
+software on general-purpose computers, but in those that do, we wish to
+avoid the special danger that patents applied to a free program could
+make it effectively proprietary.  To prevent this, the GPL assures that
+patents cannot be used to render the program non-free.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+http://www.gnu.org/licenses/gpl-3.0.txt
+*/
+/**
+ * @file ffchartlegend.cpp
+ * @brief Implementation of the FFChartLegend class
+ *
+ * @author ComArch S.A.
+ * @date 2009.08.20
+ * @version 1.0
+ */
+#include "ffchartlegend.h"
+
+/**
+ * Constructs a FFChartLegend with a parent.
+ */
+FFChartLegend::FFChartLegend(QWidget* parent) :
+       FFAbstractWidget(parent)
+{
+       this->parent = parent;
+       init();
+}
+
+/**
+ * A virtual destructor.
+ */
+FFChartLegend::~FFChartLegend()
+{
+       int num = legendSeriesTable.size();
+       for(int i = 0; i < num; ++i)
+       {
+               delete legendSeriesTable.at(0);
+               legendSeriesTable.remove(0);
+       }
+}
+
+/**
+ * Initiates all needed fields and functionality
+ */
+void FFChartLegend::init()
+{
+       //sets default values
+       path = LEGEND_PATH;
+
+       backgroundColor = DEFAULT_LEGEND_BACKGROUND_COLOR;
+       backgroundOpacity = DEFAULT_LEGEND_BACKGROUND_OPACITY;
+
+       actualChangingSeries = NULL;
+
+       //set style
+       setStyleSheet("background-color: transparent");
+
+       //sets layout
+       legendLayout = new QGridLayout(this);
+       legendLayout ->setMargin(0);
+       this->setLayout(legendLayout);
+
+       legendScrollArea = new FFScrollArea(this);
+       legendScrollArea->setFrameStyle(0);
+       legendScrollAreaItem = new QWidget(legendScrollArea);
+       legendScrollAreaLayout = new QGridLayout(legendScrollAreaItem);
+       legendScrollAreaItem->setLayout(legendScrollAreaLayout);
+       legendScrollAreaLayout->setMargin(0);
+       legendScrollAreaLayout->setSpacing(5);
+
+       legendScrollAreaItem->setStyleSheet("background-color: transparent");
+       legendScrollArea->setWidget(legendScrollAreaItem);
+       legendScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+       legendScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+       legendScrollArea->setFrameStyle(0);
+
+       legendLayout->addWidget(legendScrollArea);
+
+       legendControlPanel = new FFChartLegendControlPanel(this);
+       legendControlPanel->hide();
+
+       //connects
+       connect(legendControlPanel,
+               SIGNAL(penChanged(QPen)),
+               this,
+               SLOT(changeSeriesPen(QPen)));
+}
+
+/**
+ * Supports click event. Shows or hide chart's legend
+ */
+void FFChartLegend::popUp()
+{
+       if(isVisible())
+       {
+               this->hide();
+       }
+       else
+       {
+               this->show();
+       }
+
+}
+
+/**
+ * It updates size of FFChartLegend's elements
+ * Calls changeSize() method
+ */
+void FFChartLegend::resizeEvent(QResizeEvent* event)
+{
+       Q_UNUSED(event)
+       if(this->isVisible())
+       {
+               changeSize();
+       }
+
+}
+/**
+ * Fits size of FFChartLegend's elements chart when show event appeared.
+ * Calls changeSize() method.
+ */
+void FFChartLegend::showEvent(QShowEvent* event)
+{
+       Q_UNUSED(event)
+
+       changeSize();
+}
+
+/**
+ * Add series to FFChartLegend.
+ *
+ * @param series is a pointer to series on a FFChart
+ */
+void FFChartLegend::addSeries(FFChartSeries* series)
+{
+       //creates buttons representing series
+       FFScrollingCheckBox* visibleCheckbox =
+                       new FFScrollingCheckBox(legendScrollAreaItem);
+       FFLineButton* modifyButton = new FFLineButton(legendScrollAreaItem);
+
+       //sets series name in buttons
+       visibleCheckbox->setDescription(series->name());
+       visibleCheckbox->setChecked(series->isVisible());
+       visibleCheckbox->disableTitle(true);
+       visibleCheckbox->setTopMargin(8);
+       visibleCheckbox->setBottomMargin(8);
+       visibleCheckbox->setRightMargin(2);
+       visibleCheckbox->setLeftMargin(2);
+       visibleCheckbox->setAttribute(Qt::WA_DeleteOnClose);
+
+       modifyButton->setPen(*series->pen());
+       modifyButton->setAttribute(Qt::WA_DeleteOnClose);
+
+       SeriesContainer* tempSeriesPointer =
+                       new SeriesContainer(series,
+                                           visibleCheckbox,
+                                           modifyButton);
+       //adds seriesContener to series table
+       legendSeriesTable.append(tempSeriesPointer);
+
+       //adds buttons to layout
+       legendScrollAreaLayout->addWidget(legendSeriesTable.at(legendSeriesTable.size()
+                                                         - 1)->visibleCheckBox_,
+                                         legendSeriesTable.size() - 1,
+                                         0,
+                                         1,
+                                         2);
+       legendScrollAreaLayout->addWidget(legendSeriesTable.at(legendSeriesTable.size()
+                                                         - 1)->controlPanelButton_,
+                                         legendSeriesTable.size() - 1,
+                                         2,
+                                         1,
+                                         1);
+
+       //connecting buttons to legend
+       connect(modifyButton,
+               SIGNAL(clicked()),
+               this,
+               SLOT(chooseChangingSeries()));
+       connect(visibleCheckbox, SIGNAL(clicked()), this, SLOT(changeVisible()));
+       changeSize();
+}
+
+/**
+ *  Fits size of FFChartLegend's elements
+ */
+void FFChartLegend::changeSize()
+{
+       // sets geometry for legend elements
+       legendControlPanel->setGeometry(0, 0, size().width(), size().height());
+
+       legendScrollAreaItem->setGeometry(0,
+                                         0,
+                                         legendScrollArea->width(),
+                                         legendSeriesTable.size()
+                                                         * (DEFAULT_BUTTON_HEIGHT
+                                                                         + 5));
+       //update legendscrollarea
+       legendScrollArea->setWidget(NULL);
+       legendScrollArea->setWidget(legendScrollAreaItem);
+}
+
+/**
+ * Assign pointer on actual modified series to the temporary pointer.
+ */
+void FFChartLegend::chooseChangingSeries()
+{
+       //check which series was chosen to modification
+       for(int i = 0; i < legendSeriesTable.size(); i++)
+       {
+               if(legendSeriesTable.at(i)->controlPanelButton_
+                               == (FFLineButton*)sender())
+                       actualChangingSeries = legendSeriesTable.at(i);
+       }
+
+       //sets and shows legendCotnrolPanel
+       legendControlPanel->setPen(*actualChangingSeries->series_->pen());
+       legendControlPanel->show();
+       if(NULL != parent)
+               parent->repaint();
+}
+
+/**
+ * Updates a pen of button representing actual modified series and emits
+ * signal to chart.
+ *
+ * @param pen represents setting series pen
+ */
+
+void FFChartLegend::changeSeriesPen(QPen pen)
+{
+       actualChangingSeries->series_->setPen(pen);
+       actualChangingSeries->controlPanelButton_->setPen(pen);
+       emit updateSeries();
+}
+
+/**
+ * Updates series visibility
+ */
+void FFChartLegend::changeVisible()
+{
+       //looks for pointer to changing series and change it visibility and
+       //state of visibleCheckbox
+
+       for(int i = 0; i < legendSeriesTable.size(); i++)
+       {
+               if(legendSeriesTable.at(i)->visibleCheckBox_
+                               == (FFScrollingCheckBox*)sender())
+               {
+                       bool
+                            tempVisible =
+                                          !(legendSeriesTable.at(i)->series_->isVisible());
+
+                       legendSeriesTable.at(i)->series_->setVisible(tempVisible);
+                       legendSeriesTable.at(i)->visibleCheckBox_->setChecked(tempVisible);
+               }
+       }
+
+       emit updateSeries();
+}
+
+/**
+ * Deletes series from series contener
+ *
+ * @param series is a pointer to series that will be removed.
+ */
+void FFChartLegend::deleteSeries(FFChartSeries* series)
+{
+       //looks for given series in legendSeriesTable and deletes it.
+       for(int i = 0; i < legendSeriesTable.size(); i++)
+       {
+               if(series == legendSeriesTable.at(i)->series_)
+               {
+                       legendControlPanel->hide();
+
+                       delete legendSeriesTable.at(i);
+                       legendSeriesTable.remove(i);
+               }
+
+       }
+       changeSize();
+}
+
+/**
+ * Draw FFChartLegend background
+ * @param event Contains all informations about event.
+ */
+void FFChartLegend::paintEvent(QPaintEvent *event)
+{
+       Q_UNUSED(event)
+       QPainter paint;
+       paint.begin(this);
+       paint.setOpacity(backgroundOpacity);
+       paint.setBrush(backgroundColor);
+       paint.setPen(Qt::transparent);
+       paint.drawRect(0, 0, width(), height());
+       paint.end();
+}
+
+/**
+ \fn void FFChartLegend::updateSeries()
+
+ This signal is emitted when the series is/are changed
+ */
+
+/**
+ * Constructs a FFChartLegend with pointers to series, and buttons .
+ */
+SeriesContainer::SeriesContainer(FFChartSeries* series,
+                                 FFScrollingCheckBox* visibleCheckBox,
+                                 FFLineButton* controlPanelButton)
+{
+       series_ = series;
+       visibleCheckBox_ = visibleCheckBox;
+       controlPanelButton_ = controlPanelButton;
+}
+
+/**
+ * A virtual destructor
+ */
+SeriesContainer::~SeriesContainer()
+{
+       visibleCheckBox_->close();
+       controlPanelButton_->close();
+}