widget.cpp
108 lines
| 3.1 KiB
| text/x-c
|
CppLexer
|
r1693 | #include "widget.h" | ||
#include <QChartView> | ||||
|
r1838 | #include <QChart> | ||
#include <QLegend> | ||||
|
r1693 | #include <QPieSeries> | ||
#include <QPieSlice> | ||||
#include <QTime> | ||||
#include <QGridLayout> | ||||
#include <QTimer> | ||||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
Widget::Widget(QWidget *parent) | ||||
: QWidget(parent) | ||||
{ | ||||
setMinimumSize(800, 600); | ||||
qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | ||||
|
r1713 | //! [1] | ||
|
r1693 | QChartView *chartView = new QChartView; | ||
chartView->setRenderHint(QPainter::Antialiasing); | ||||
|
r1838 | QChart *chart = chartView->chart(); | ||
chart->setAnimationOptions(QChart::AllAnimations); | ||||
chart->legend()->setVisible(false); | ||||
|
r1713 | //! [1] | ||
|
r1693 | |||
|
r1713 | //! [2] | ||
|
r1697 | qreal minSize = 0.1; | ||
qreal maxSize = 0.9; | ||||
|
r1838 | int donutCount = 5; | ||
|
r1713 | //! [2] | ||
//! [3] | ||||
|
r1838 | for (int i = 0; i < donutCount; i++) { | ||
|
r1693 | QPieSeries *donut = new QPieSeries; | ||
|
r1697 | int sliceCount = 3 + qrand() % 3; | ||
for (int j = 0; j < sliceCount; j++) { | ||||
|
r1693 | qreal value = 100 + qrand() % 100; | ||
|
r1697 | QPieSlice *slice = new QPieSlice(QString("%1").arg(value), value); | ||
|
r1713 | slice->setLabelVisible(true); | ||
slice->setLabelColor(Qt::white); | ||||
slice->setLabelPosition(QPieSlice::LabelInsideTangential); | ||||
|
r1697 | connect(slice, SIGNAL(hovered(bool)), this, SLOT(explodeSlice(bool))); | ||
donut->append(slice); | ||||
|
r1838 | donut->setHoleSize(minSize + i * (maxSize - minSize) / donutCount); | ||
donut->setPieSize(minSize + (i + 1) * (maxSize - minSize) / donutCount); | ||||
|
r1693 | } | ||
|
r1697 | m_donuts.append(donut); | ||
|
r1693 | chartView->chart()->addSeries(donut); | ||
} | ||||
|
r1713 | //! [3] | ||
|
r1693 | |||
// create main layout | ||||
|
r1713 | //! [4] | ||
|
r1693 | QGridLayout* mainLayout = new QGridLayout; | ||
mainLayout->addWidget(chartView, 1, 1); | ||||
setLayout(mainLayout); | ||||
|
r1713 | //! [4] | ||
|
r1693 | |||
|
r1713 | //! [5] | ||
|
r1697 | updateTimer = new QTimer(this); | ||
|
r1693 | connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateRotation())); | ||
|
r1713 | updateTimer->start(1250); | ||
//! [5] | ||||
|
r1693 | } | ||
Widget::~Widget() | ||||
{ | ||||
} | ||||
|
r1713 | //! [6] | ||
|
r1693 | void Widget::updateRotation() | ||
{ | ||||
|
r1697 | for (int i = 0; i < m_donuts.count(); i++) { | ||
QPieSeries *donut = m_donuts.at(i); | ||||
|
r1693 | qreal phaseShift = -50 + qrand() % 100; | ||
donut->setPieStartAngle(donut->pieStartAngle() + phaseShift); | ||||
donut->setPieEndAngle(donut->pieEndAngle() + phaseShift); | ||||
} | ||||
} | ||||
|
r1713 | //! [6] | ||
|
r1697 | |||
|
r1713 | //! [7] | ||
|
r1697 | void Widget::explodeSlice(bool exploded) | ||
{ | ||||
QPieSlice *slice = qobject_cast<QPieSlice *>(sender()); | ||||
if (exploded) { | ||||
updateTimer->stop(); | ||||
qreal sliceStartAngle = slice->startAngle(); | ||||
qreal sliceEndAngle = slice->startAngle() + slice->angleSpan(); | ||||
QPieSeries *donut = slice->series(); | ||||
qreal seriesIndex = m_donuts.indexOf(donut); | ||||
for (int i = seriesIndex + 1; i < m_donuts.count(); i++) { | ||||
m_donuts.at(i)->setPieStartAngle(sliceEndAngle); | ||||
m_donuts.at(i)->setPieEndAngle(360 + sliceStartAngle); | ||||
} | ||||
} else { | ||||
for (int i = 0; i < m_donuts.count(); i++) { | ||||
m_donuts.at(i)->setPieStartAngle(0); | ||||
m_donuts.at(i)->setPieEndAngle(360); | ||||
} | ||||
updateTimer->start(); | ||||
} | ||||
slice->setExploded(exploded); | ||||
} | ||||
|
r1713 | //! [7] | ||