main.cpp
236 lines
| 7.9 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r437 | #include <QtGui/QApplication> | ||
#include <QMainWindow> | ||||
#include <qchartglobal.h> | ||||
#include <qchartview.h> | ||||
#include <qpieseries.h> | ||||
#include <qpieslice.h> | ||||
#include <QGridLayout> | ||||
#include <QFormLayout> | ||||
#include <QComboBox> | ||||
#include <QSpinBox> | ||||
#include <QCheckBox> | ||||
#include <QGroupBox> | ||||
#include <QLabel> | ||||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
class CustomSlice : public QPieSlice | ||||
{ | ||||
Q_OBJECT | ||||
public: | ||||
CustomSlice(qreal value, QString label) | ||||
:QPieSlice(value, label) | ||||
{ | ||||
connect(this, SIGNAL(hoverEnter()), this, SLOT(handleHoverEnter())); | ||||
connect(this, SIGNAL(hoverLeave()), this, SLOT(handleHoverLeave())); | ||||
} | ||||
public Q_SLOTS: | ||||
void handleHoverEnter() | ||||
{ | ||||
QBrush brush = this->brush(); | ||||
m_originalBrush = brush; | ||||
brush.setColor(brush.color().lighter()); | ||||
setBrush(brush); | ||||
} | ||||
void handleHoverLeave() | ||||
{ | ||||
setBrush(m_originalBrush); | ||||
} | ||||
private: | ||||
QBrush m_originalBrush; | ||||
}; | ||||
class MainWidget : public QWidget | ||||
{ | ||||
Q_OBJECT | ||||
public: | ||||
explicit MainWidget(QWidget* parent = 0) | ||||
Jani Honkonen
|
r454 | :QWidget(parent), | ||
m_slice(0) | ||||
Jani Honkonen
|
r437 | { | ||
m_chartView = new QChartView(); | ||||
m_chartView->setChartTitle("Piechart customization"); | ||||
Jani Honkonen
|
r454 | m_chartView->setRenderHint(QPainter::Antialiasing); | ||
m_chartView->setChartTheme(QChart::ChartThemeIcy); | ||||
Jani Honkonen
|
r437 | |||
m_series = new QPieSeries(); | ||||
*m_series << new CustomSlice(10.0, "Slice 1"); | ||||
*m_series << new CustomSlice(20.0, "Slice 2"); | ||||
*m_series << new CustomSlice(30.0, "Slice 3"); | ||||
*m_series << new CustomSlice(40.0, "Slice 4"); | ||||
*m_series << new CustomSlice(50.0, "Slice 5"); | ||||
m_chartView->addSeries(m_series); | ||||
Jani Honkonen
|
r454 | m_hPosition = new QDoubleSpinBox(); | ||
m_hPosition->setMinimum(0.0); | ||||
m_hPosition->setMaximum(1.0); | ||||
m_hPosition->setSingleStep(0.1); | ||||
m_hPosition->setValue(m_series->horizontalPositionFactor()); | ||||
Jani Honkonen
|
r437 | |||
Jani Honkonen
|
r454 | m_vPosition = new QDoubleSpinBox(); | ||
m_vPosition->setMinimum(0.0); | ||||
m_vPosition->setMaximum(1.0); | ||||
m_vPosition->setSingleStep(0.1); | ||||
m_vPosition->setValue(m_series->verticalPositionFactor()); | ||||
Jani Honkonen
|
r437 | |||
m_sizeFactor = new QDoubleSpinBox(); | ||||
m_sizeFactor->setMinimum(0.0); | ||||
m_sizeFactor->setMaximum(1.0); | ||||
m_sizeFactor->setSingleStep(0.1); | ||||
Jani Honkonen
|
r454 | m_sizeFactor->setValue(m_series->sizeFactor()); | ||
Jani Honkonen
|
r437 | |||
m_startAngle = new QDoubleSpinBox(); | ||||
m_startAngle->setMinimum(0.0); | ||||
m_startAngle->setMaximum(360); | ||||
m_startAngle->setValue(m_series->startAngle()); | ||||
m_startAngle->setSingleStep(1); | ||||
m_endAngle = new QDoubleSpinBox(); | ||||
m_endAngle->setMinimum(0.0); | ||||
m_endAngle->setMaximum(360); | ||||
m_endAngle->setValue(m_series->endAngle()); | ||||
m_endAngle->setSingleStep(1); | ||||
QFormLayout* seriesSettingsLayout = new QFormLayout(); | ||||
seriesSettingsLayout->addRow("Horizontal position", m_hPosition); | ||||
Jani Honkonen
|
r454 | seriesSettingsLayout->addRow("Vertical position", m_vPosition); | ||
Jani Honkonen
|
r437 | seriesSettingsLayout->addRow("Size factor", m_sizeFactor); | ||
seriesSettingsLayout->addRow("Start angle", m_startAngle); | ||||
seriesSettingsLayout->addRow("End angle", m_endAngle); | ||||
QGroupBox* seriesSettings = new QGroupBox("Series"); | ||||
seriesSettings->setLayout(seriesSettingsLayout); | ||||
m_sliceName = new QLabel("<click a slice>"); | ||||
m_sliceValue = new QDoubleSpinBox(); | ||||
m_sliceValue->setMaximum(1000); | ||||
m_sliceLabelVisible = new QCheckBox(); | ||||
Jani Honkonen
|
r454 | m_sliceLabelArmFactor = new QDoubleSpinBox(); | ||
m_sliceLabelArmFactor->setSingleStep(0.01); | ||||
Jani Honkonen
|
r437 | m_sliceExploded = new QCheckBox(); | ||
Jani Honkonen
|
r454 | m_sliceExplodedFactor = new QDoubleSpinBox(); | ||
m_sliceExplodedFactor->setSingleStep(0.01); | ||||
Jani Honkonen
|
r437 | |||
QFormLayout* sliceSettingsLayout = new QFormLayout(); | ||||
sliceSettingsLayout->addRow("Selected", m_sliceName); | ||||
sliceSettingsLayout->addRow("Value", m_sliceValue); | ||||
sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible); | ||||
Jani Honkonen
|
r454 | sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor); | ||
Jani Honkonen
|
r437 | sliceSettingsLayout->addRow("Exploded", m_sliceExploded); | ||
Jani Honkonen
|
r454 | sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor); | ||
Jani Honkonen
|
r437 | QGroupBox* sliceSettings = new QGroupBox("Slice"); | ||
sliceSettings->setLayout(sliceSettingsLayout); | ||||
QGridLayout* baseLayout = new QGridLayout(); | ||||
baseLayout->addWidget(seriesSettings, 0, 0); | ||||
baseLayout->addWidget(sliceSettings, 1, 0); | ||||
baseLayout->addWidget(m_chartView, 0, 1, 2, 1); | ||||
setLayout(baseLayout); | ||||
Jani Honkonen
|
r454 | connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); | ||
connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); | ||||
Jani Honkonen
|
r437 | connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); | ||
connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); | ||||
connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); | ||||
connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); | ||||
connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings())); | ||||
Jani Honkonen
|
r454 | connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); | ||
Jani Honkonen
|
r437 | connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings())); | ||
Jani Honkonen
|
r454 | connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); | ||
Jani Honkonen
|
r437 | |||
connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*))); | ||||
updateSerieSettings(); | ||||
} | ||||
public Q_SLOTS: | ||||
void updateSerieSettings() | ||||
{ | ||||
Jani Honkonen
|
r454 | m_series->setPositionFactors(m_vPosition->value(), m_hPosition->value()); | ||
Jani Honkonen
|
r437 | |||
m_series->setSizeFactor(m_sizeFactor->value()); | ||||
m_series->setStartAngle(m_startAngle->value()); | ||||
m_series->setEndAngle(m_endAngle->value()); | ||||
} | ||||
void updateSliceSettings() | ||||
{ | ||||
Jani Honkonen
|
r454 | if (!m_slice) | ||
return; | ||||
Jani Honkonen
|
r437 | m_slice->setValue(m_sliceValue->value()); | ||
m_slice->setLabelVisible(m_sliceLabelVisible->isChecked()); | ||||
Jani Honkonen
|
r454 | m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value()); | ||
Jani Honkonen
|
r437 | m_slice->setExploded(m_sliceExploded->isChecked()); | ||
Jani Honkonen
|
r454 | m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value()); | ||
Jani Honkonen
|
r437 | } | ||
void handleSliceClicked(QPieSlice* slice) | ||||
{ | ||||
m_slice = slice; | ||||
m_sliceName->setText(slice->label()); | ||||
m_sliceValue->blockSignals(true); | ||||
m_sliceValue->setValue(slice->value()); | ||||
m_sliceValue->blockSignals(false); | ||||
m_sliceLabelVisible->blockSignals(true); | ||||
m_sliceLabelVisible->setChecked(slice->isLabelVisible()); | ||||
m_sliceLabelVisible->blockSignals(false); | ||||
Jani Honkonen
|
r454 | m_sliceLabelArmFactor->blockSignals(true); | ||
m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor()); | ||||
m_sliceLabelArmFactor->blockSignals(false); | ||||
Jani Honkonen
|
r437 | m_sliceExploded->blockSignals(true); | ||
m_sliceExploded->setChecked(slice->isExploded()); | ||||
m_sliceExploded->blockSignals(false); | ||||
Jani Honkonen
|
r454 | |||
m_sliceExplodedFactor->blockSignals(true); | ||||
m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor()); | ||||
m_sliceExplodedFactor->blockSignals(false); | ||||
Jani Honkonen
|
r437 | } | ||
private: | ||||
QChartView* m_chartView; | ||||
QPieSeries* m_series; | ||||
QPieSlice* m_slice; | ||||
Jani Honkonen
|
r454 | QDoubleSpinBox* m_hPosition; | ||
QDoubleSpinBox* m_vPosition; | ||||
Jani Honkonen
|
r437 | QDoubleSpinBox* m_sizeFactor; | ||
QDoubleSpinBox* m_startAngle; | ||||
QDoubleSpinBox* m_endAngle; | ||||
QLabel* m_sliceName; | ||||
QDoubleSpinBox* m_sliceValue; | ||||
QCheckBox* m_sliceLabelVisible; | ||||
Jani Honkonen
|
r454 | QDoubleSpinBox* m_sliceLabelArmFactor; | ||
Jani Honkonen
|
r437 | QCheckBox* m_sliceExploded; | ||
Jani Honkonen
|
r454 | QDoubleSpinBox* m_sliceExplodedFactor; | ||
Jani Honkonen
|
r437 | }; | ||
int main(int argc, char *argv[]) | ||||
{ | ||||
QApplication a(argc, argv); | ||||
QMainWindow window; | ||||
MainWidget* widget = new MainWidget(); | ||||
window.setCentralWidget(widget); | ||||
window.resize(900, 600); | ||||
window.show(); | ||||
return a.exec(); | ||||
} | ||||
#include "main.moc" | ||||