##// END OF EJS Templates
Correct graphics flags for items
Correct graphics flags for items

File last commit:

r835:a25790acd0d0
r856:cabe72fb9d4b
Show More
widget.cpp
247 lines | 8.9 KiB | text/x-c | CppLexer
Jani Honkonen
Add/modify license headers
r830 /****************************************************************************
**
** Copyright (C) 2012 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the Qt Commercial Charts Add-on.
**
** $QT_BEGIN_LICENSE$
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
** $QT_END_LICENSE$
**
****************************************************************************/
Marek Rosa
Corrected the path to SQlite data
r219
Marek Rosa
Initial commit for gdpBarChart example.
r205 #include "widget.h"
Jani Honkonen
make gdpbarchart compile again
r826
#include <QChart>
#include <QScatterSeries>
#include <QChartAxis>
Marek Rosa
Fixed the include issue in gdpbarchart demo
r835 #include <QBarSet>
Jani Honkonen
make gdpbarchart compile again
r826 #include <QBarSeries>
Marek Rosa
Initial commit for gdpBarChart example.
r205 #include <QGridLayout>
#include <QPushButton>
#include <QLabel>
#include <QListWidget>
Marek Rosa
Added chart printing to pdf
r244 #include <QPrinter>
#include <QPrintDialog>
Marek Rosa
Added another chart type choice
r268 #include <QRadioButton>
sauimone
replaced qbarcategory with qstringlist
r377 #include <QStringList>
Jani Honkonen
make gdpbarchart compile again
r826 #include <QSqlQuery>
Marek Rosa
Initial commit for gdpBarChart example.
r205
QTCOMMERCIALCHART_USE_NAMESPACE
Widget::Widget(QWidget *parent)
: QWidget(parent)
Michal Klocek
Compilation fix
r232 {
Marek Rosa
Copy database to bin directory
r242 setGeometry(100, 100, 1000, 600);
Marek Rosa
Initial commit for gdpBarChart example.
r205
// right panel layout
Marek Rosa
Added another chart type choice
r268 barChartRadioButton = new QRadioButton(tr("Bar chart"));
barChartRadioButton->setChecked(true);
scatterChartRadioButton = new QRadioButton(tr("Scatter chart"));
scatterChartRadioButton->setChecked(false);
Marek Rosa
Initial commit for gdpBarChart example.
r205 countrieslist = new QListWidget;
countrieslist->setSelectionMode(QAbstractItemView::MultiSelection);
Marek Rosa
Doc update
r382 //list of years widget
Marek Rosa
Initial commit for gdpBarChart example.
r205 yearslist = new QListWidget;
yearslist->setSelectionMode(QAbstractItemView::ExtendedSelection);
for (int i = 1990; i < 2011; i++)
yearslist->addItem(QString("%1").arg(i));
QPushButton* refreshButton = new QPushButton(tr("Refresh"));
connect(refreshButton, SIGNAL(clicked()), this, SLOT(refreshChart()));
Marek Rosa
Added chart printing to pdf
r244 QPushButton* printButton = new QPushButton(tr("Print chart"));
connect(printButton, SIGNAL(clicked()), this, SLOT(printChart()));
Marek Rosa
Initial commit for gdpBarChart example.
r205 QVBoxLayout* rightPanelLayout = new QVBoxLayout;
Marek Rosa
Added another chart type choice
r268 rightPanelLayout->addWidget(barChartRadioButton);
rightPanelLayout->addWidget(scatterChartRadioButton);
Marek Rosa
Initial commit for gdpBarChart example.
r205 rightPanelLayout->addWidget(countrieslist);
rightPanelLayout->addWidget(yearslist);
rightPanelLayout->addWidget(refreshButton);
Marek Rosa
Added chart printing to pdf
r244 rightPanelLayout->addWidget(printButton);
Marek Rosa
Initial commit for gdpBarChart example.
r205 rightPanelLayout->setStretch(0, 1);
rightPanelLayout->setStretch(1, 0);
Jani Honkonen
make gdpbarchart compile again
r826 QChart *chart = new QChart();
chart->setTitle("GDP by country");
Marek Rosa
Initial commit for gdpBarChart example.
r205 // main layout
Jani Honkonen
make gdpbarchart compile again
r826 chartView = new QChartView(chart);
Marek Rosa
Initial commit for gdpBarChart example.
r205 QGridLayout* mainLayout = new QGridLayout;
Jani Honkonen
make gdpbarchart compile again
r826 mainLayout->addWidget(chartView, 0, 0);
Marek Rosa
Initial commit for gdpBarChart example.
r205 mainLayout->addLayout(rightPanelLayout, 0, 1);
mainLayout->setColumnStretch(0,1);
setLayout(mainLayout);
// connect to the database
db = QSqlDatabase::addDatabase("QSQLITE");
Marek Rosa
Copy database to bin directory
r242 db.setDatabaseName("gdpData");
Marek Rosa
Initial commit for gdpBarChart example.
r205 if(!db.open())
{
qDebug() << "could not open database. SQLite db file missing (?)";
return;
}
// get the list of all countires and regions.
QSqlQuery query;
query.exec("SELECT DISTINCT country FROM gdp2");
// add the countries to the country filter
while (query.next()) {
countrieslist->addItem(query.value(0).toString());
}
// hide axis X labels
Tero Ahola
A few more warnings removed from examples
r613 //QChartAxis* axis = chartArea->axisX();
Marek Rosa
Spline chart example added
r434 // axis->
Marek Rosa
merge fix
r383 // axis->setLabelsVisible(false);
Marek Rosa
Added chart printing to pdf
r244 // newAxis.setLabelsOrientation(QChartAxis::LabelsOrientationSlide);
Michal Klocek
Compilation fix
r232
Marek Rosa
Initial commit for gdpBarChart example.
r205 }
Widget::~Widget()
{
//
db.close();
}
Marek Rosa
Corrected the path to SQlite data
r219 /*!
refreshes the chart
*/
Marek Rosa
Initial commit for gdpBarChart example.
r205 void Widget::refreshChart()
{
Jani Honkonen
make gdpbarchart compile again
r826 chartView->chart()->removeAllSeries();
Marek Rosa
Added another chart type choice
r268
Marek Rosa
Initial commit for gdpBarChart example.
r205 // selected countries items list is not sorted. copy the values to QStringlist and sort them.
QStringList selectedCountriesStrings;
QList<QListWidgetItem*> selectedCountriesItems = countrieslist->selectedItems();
for (int i = 0; i < selectedCountriesItems.size(); i++)
selectedCountriesStrings.append(selectedCountriesItems[i]->text());
selectedCountriesStrings.sort();
QSqlQuery query;
Marek Rosa
Doc update
r382 // selected years items list is not sorted. copy the values to QList<int> and sort them.
Marek Rosa
Initial commit for gdpBarChart example.
r205 QList<int> selectedYearsInts;
QList<QListWidgetItem*> selectedYearsItems = yearslist->selectedItems();
for (int i = 0; i < selectedYearsItems.size(); i++)
selectedYearsInts.append(selectedYearsItems[i]->text().toInt());
Marek Rosa
Added another chart type choice
r268 qSort(selectedYearsInts.begin(), selectedYearsInts.end(), qGreater<int>());
Marek Rosa
Initial commit for gdpBarChart example.
r205
Marek Rosa
Added another chart type choice
r268 if (barChartRadioButton->isChecked())
Marek Rosa
Initial commit for gdpBarChart example.
r205 {
Marek Rosa
Added another chart type choice
r268 // use the sorted selected coutries list to initialize BarCategory
sauimone
replaced qbarcategory with qstringlist
r377 QStringList category;
Marek Rosa
Added another chart type choice
r268 for (int i = 0; i < selectedCountriesStrings.size(); i++)
sauimone
replaced qbarcategory with qstringlist
r377 category << selectedCountriesStrings[i];
Marek Rosa
merge fix
r385 QBarSeries* series0 = new QBarSeries(category);
sauimone
Naming convention change for barcharts. QBarChartSeries is now QBarSeries etc.
r338 series0 = new QBarSeries(category);
Marek Rosa
Added another chart type choice
r268
// prepare the selected counries SQL query
QString countriesQuery = "country IN (";
for (int i = 0; i < selectedCountriesStrings.size(); i++)
Marek Rosa
Initial commit for gdpBarChart example.
r205 {
Marek Rosa
Added another chart type choice
r268 countriesQuery.append("'" + selectedCountriesStrings[i] + "'");
if ( i < selectedCountriesStrings.size() - 1)
countriesQuery.append(",");
else
countriesQuery.append(")");
}
// perform a query for each selected year
for (int i = 0; i < selectedYearsInts.size(); i++)
{
query.exec("SELECT country,gdpvalue FROM gdp2 where year=" + QString("%1").arg(selectedYearsInts[i]) + " AND " + countriesQuery);
Tero Ahola
Fixed compilation of example apps with QBarSets
r284 QBarSet* barSet = new QBarSet("Barset" + QString::number(i));
Marek Rosa
Merge branch 'master' of https://git.it.local/repos/QtCommercialDevel-13049/charts...
r286
Marek Rosa
Added another chart type choice
r268 // while (query.next()) {
// qDebug() << query.value(0).toString() << " : " << query.value(1).toString();
// }
query.first();
// the data for some of the coutries for some years might be missing.
// QBarChart needs bars to have same size
for (int k = 0; k < selectedCountriesStrings.size(); k++)
Marek Rosa
Initial commit for gdpBarChart example.
r205 {
Marek Rosa
Added another chart type choice
r268 if (selectedCountriesStrings[k] == query.value(0).toString())
{
*barSet << query.value(1).toReal();
qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[i]);
query.next();
}
else
{
// data missing, put 0
*barSet << 0.0f;
Marek Rosa
Doc update
r382 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]);
Marek Rosa
Added another chart type choice
r268 }
Marek Rosa
Initial commit for gdpBarChart example.
r205 }
Jani Honkonen
make gdpbarchart compile again
r826 series0->appendBarSet(barSet);
Marek Rosa
Added another chart type choice
r268 }
// add the serie to the chart
Jani Honkonen
make gdpbarchart compile again
r826 chartView->chart()->addSeries(series0);
Marek Rosa
Added another chart type choice
r268 }
else if (scatterChartRadioButton->isChecked())
{
QString yearsQuery = "year IN (";
for (int i = 0; i < selectedYearsInts.size(); i++)
{
yearsQuery.append("'" + QString("%1").arg(selectedYearsInts[i]) + "'");
if ( i < selectedYearsInts.size() - 1)
yearsQuery.append(",");
Marek Rosa
Initial commit for gdpBarChart example.
r205 else
Marek Rosa
Added another chart type choice
r268 yearsQuery.append(")");
}
Marek Rosa
Doc update
r382 // perform a query for each selected country
Marek Rosa
Added another chart type choice
r268 for (int i = 0; i < selectedCountriesStrings.size(); i++)
{
query.exec("SELECT year,gdpvalue FROM gdp2 where country='" + selectedCountriesStrings[i] + "' AND " + yearsQuery);
query.first();
QScatterSeries* series = new QScatterSeries;
// the data for some of the coutries for some years might be missing.
for (int k = 0; k < selectedYearsInts.size(); k++)
Marek Rosa
Initial commit for gdpBarChart example.
r205 {
Marek Rosa
Added another chart type choice
r268 if (selectedYearsInts[k] == query.value(0).toInt())
{
*series << QPointF(query.value(0).toInt() , query.value(1).toReal());
qDebug() << query.value(0).toString() << query.value(1).toReal() << " : " << QString("%1").arg(selectedYearsInts[k]);
query.next();
}
else
{
// data missing, put 0
*series << QPointF(selectedYearsInts[k] , 0.0f);
Marek Rosa
Doc update
r382 qDebug() << "Putting 0 for the missing data" << " : " << QString("%1").arg(selectedYearsInts[i]) << " " << query.value(0).toInt();
Marek Rosa
Added another chart type choice
r268 }
Marek Rosa
Initial commit for gdpBarChart example.
r205 }
Marek Rosa
merge fix
r383 // chartArea->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] + 1, selectedYearsInts[0] - 1);
Jani Honkonen
make gdpbarchart compile again
r826 chartView->chart()->addSeries(series);
Marek Rosa
Initial commit for gdpBarChart example.
r205 }
Jani Honkonen
make gdpbarchart compile again
r826 chartView->chart()->axisX()->setRange(selectedYearsInts[selectedYearsInts.size() - 1] - 1, selectedYearsInts[0] + 1);
Marek Rosa
Initial commit for gdpBarChart example.
r205 }
}
Marek Rosa
Added chart printing to pdf
r244
void Widget::printChart()
{
QPrinter printer;
Marek Rosa
Added another chart type choice
r268 // QPrinter printer(QPrinter::HighResolution);
Marek Rosa
Added chart printing to pdf
r244 printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOrientation(QPrinter::Landscape);
printer.setOutputFileName("print.pdf");
QPainter painter;
painter.begin(&printer);
Jani Honkonen
make gdpbarchart compile again
r826 chartView->render(&painter);
Marek Rosa
Added chart printing to pdf
r244 }