PyramidChart3D Example

#include "mainwindow.h"

#include <QGroupBox>
#include <QComboBox>
#include <QFormLayout>
#include <QCheckBox>

/* MainWindow */
MainWindow::MainWindow()
    : DemoChartWindow(tr("Pyramid Chart 3D"))
{
    setChart(new Chart);
    createSeriesParametrs();
    createPyramidSeries();
    updateValueParameters();
}

void MainWindow::createSeriesParametrs()
{
    QGroupBox* seriesTypeGroup = createGroupParameters(tr("Series"));
    QFormLayout* localLayout = (QFormLayout*)seriesTypeGroup->layout();

    m_circularSwitcher = new QCheckBox(tr("Circular"), seriesTypeGroup);
    connect(m_circularSwitcher, SIGNAL(stateChanged(int)), this, SLOT(circularChanged(int)));
    localLayout->addRow(m_circularSwitcher);

    m_transparency = new QSlider(Qt::Horizontal, seriesTypeGroup);
    m_transparency->setRange(0, 255);
    m_transparency->setSliderPosition(255);
    m_transparency->setSingleStep(5);
    connect(m_transparency, SIGNAL(valueChanged(int)), this, SLOT(transparencyChanged(int)));
    localLayout->addRow(new QLabel(QObject::tr("Transparency:"), seriesTypeGroup));
    localLayout->addRow(m_transparency);

    m_gapSize = new QSlider(Qt::Horizontal, seriesTypeGroup);
    m_gapSize->setRange(0, 20);
    m_gapSize->setSliderPosition(4);
    m_gapSize->setSingleStep(1);
    connect(m_gapSize, SIGNAL(valueChanged(int)), this, SLOT(pointGapChanged(int)));
    localLayout->addRow(new QLabel(QObject::tr("Point Gap:"), seriesTypeGroup));
    localLayout->addRow(m_gapSize);

    m_rotation = new QSlider(Qt::Horizontal, seriesTypeGroup);
    m_rotation->setRange(-180, 180);
    m_rotation->setSliderPosition(1);
    m_rotation->setSingleStep(1);
    connect(m_rotation, SIGNAL(valueChanged(int)), this, SLOT(rotationChanged(int)));
    m_labelRotation = new QLabel(QObject::tr("Rotation:"));
    localLayout->addRow(m_labelRotation);
    localLayout->addRow(m_rotation);

    m_depthPercent = new QSlider(Qt::Horizontal, seriesTypeGroup);
    m_depthPercent->setRange(0, 45);
    m_depthPercent->setSliderPosition(10);
    m_depthPercent->setSingleStep(1);
    connect(m_depthPercent, SIGNAL(valueChanged(int)), this, SLOT(depthPercentChanged(int)));
    m_labeldepthPercen = new QLabel(QObject::tr("Depth:"));
    localLayout->addRow(m_labeldepthPercen);
    localLayout->addRow(m_depthPercent);

    // Option Labels
    m_dataLabelsGroup = createGroupParameters(tr("Show Data Labels"), true);
    localLayout = (QFormLayout*)m_dataLabelsGroup->layout();

    connect(m_dataLabelsGroup, SIGNAL(toggled(bool)), this, SLOT(showDataLabels(bool)));
    m_angleDataLabelsSwitcher = new QComboBox(m_dataLabelsGroup);
    m_angleDataLabelsSwitcher->addItem(tr("Left"), ChartPyramidSeriesLabel::Left);
    m_angleDataLabelsSwitcher->addItem(tr("Right"), ChartPyramidSeriesLabel::Right);
    m_angleDataLabelsSwitcher->addItem(tr("Center"), ChartPyramidSeriesLabel::Center);
    m_angleDataLabelsSwitcher->setCurrentIndex(1);
    connect(m_angleDataLabelsSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(labelsPositionChanged(int)));
    // Layouts Labels
    localLayout->addRow(tr("Position:"), m_angleDataLabelsSwitcher);

}

void MainWindow::createPyramidSeries()
{
    createTitle(tr("Web Site Visitor Trend"));
    m_chart->legend()->setVisible(true);

    ChartPyramidSeries3D* series = new ChartPyramidSeries3D();

    ChartDataPoint* pnt = series->add(9152, tr("Visited a Web Site: 9152"));
    pnt->setLegendText(tr("Visited a Web Site: 100%"));

    pnt = series->add(6870.0, tr("Downloaded a Trial: 6870"));
    pnt->setLegendText(tr("Downloaded a Trial: 75%"));

    pnt = series->add(5121.0, tr("Contacted to Support: 5121"));
    pnt->setLegendText(tr("Contacted to Support: 56%"));

    pnt = series->add(2224.0,  tr("Subscribed: 2224"));
    pnt->setLegendText(tr("Subscribed: 24%"));

    pnt = series->add(1670.0,  tr("Renewed: 1670"));
    pnt->setLegendText(tr("Renewed: 18%"));

//    series->setEqualHeight(true);
    m_chart->appendSeries(series);
}

void MainWindow::updateValueParameters()
{
    DemoChartWindow::updateValueParameters();
    transparencyChanged(m_transparency->value());
    pointGapChanged(m_gapSize->value());
    labelsPositionChanged(m_angleDataLabelsSwitcher->currentIndex());
    depthPercentChanged(m_depthPercent->value());
    rotationChanged(m_rotation->value());
}

void MainWindow::transparencyChanged(int value)
{
    const SeriesList& listSeries = m_chart->series();

    for (int i = 0; i < listSeries.count(); i++)
    {
        if (ChartPyramidSeries3D* series = qobject_cast<ChartPyramidSeries3D*>(listSeries.at(i)))
            series->setTransparency(value);
    }
}

void MainWindow::pointGapChanged(int value)
{
    const SeriesList& listSeries = m_chart->series();

    for (int i = 0; i < listSeries.count(); i++)
    {
        if (ChartPyramidSeries3D* series = qobject_cast<ChartPyramidSeries3D*>(listSeries.at(i)))
            series->setGapSize(value);
    }
}

void MainWindow::rotationChanged(int value)
{
    const SeriesList& listSeries = m_chart->series();

    for (int i = 0; i < listSeries.count(); i++)
    {
        if (ChartPyramidSeries3D* series = qobject_cast<ChartPyramidSeries3D*>(listSeries.at(i)))
            series->setRotation(value);
    }
}

void MainWindow::depthPercentChanged(int value)
{
    const SeriesList& listSeries = m_chart->series();

    for (int i = 0; i < listSeries.count(); i++)
    {
        if (ChartPyramidSeries3D* series = qobject_cast<ChartPyramidSeries3D*>(listSeries.at(i)))
            series->setDepthPercent(value);
    }
}

void MainWindow::labelsPositionChanged(int index)
{
    QVariant var = m_angleDataLabelsSwitcher->itemData(index);

    const SeriesList& listSeries = m_chart->series();
    for (int i = 0, count = listSeries.count(); i < count; i++)
    {
        ChartPyramidSeries3D* series = (ChartPyramidSeries3D*)listSeries.at(i);
        if (ChartPyramidSeriesLabel* label = (ChartPyramidSeriesLabel*)series->label())
            label->setPosition((ChartPyramidSeriesLabel::Position)var.toInt());
    }
}

void MainWindow::circularChanged(int check)
{
    bool circular = check == Qt::Checked;
    const SeriesList& listSeries = m_chart->series();
    for (int i = 0, count = listSeries.count(); i < count; i++)
    {
        if (ChartPyramidSeries3D* series = qobject_cast<ChartPyramidSeries3D*>(listSeries.at(i)))
        {
            series->setCircular(circular);

            bool enabled = !circular;
            if (circular)
                m_depthPercent->setSliderPosition(10);

            m_labeldepthPercen->setEnabled(enabled);
            m_depthPercent->setEnabled(enabled);
            m_labelRotation->setEnabled(enabled);
            m_rotation->setEnabled(enabled);
        }
    }
}