BarChartDataModel Example
#include "mainwindow.h"
#include <QGroupBox>
#include <QComboBox>
#include <QHeaderView>
#include <QDoubleSpinBox>
#include <QTableWidget>
#include <QToolBar>
#include <QAction>
#include <QFormLayout>
MainWindow::MainWindow()
: DemoChartWindow(tr("Spline Chart with Model"))
{
m_columnCount = 3;
m_rowCount = 12;
m_orientation = Qt::Vertical;
setChart(createChart(), createDataBindingWidget());
createSeriesParametrs();
seriesChanged(m_seriesSwitcher->currentIndex());
}
Chart* MainWindow::createChart()
{
return new Qtitan::Chart(this);
}
QWidget* MainWindow::createDataBindingWidget()
{
m_tableWidget = new QTableWidget(this);
m_tableWidget->setMaximumWidth(250);
m_tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
return m_tableWidget;
}
void MainWindow::createSeriesParametrs()
{
QGroupBox* dataBindingGroup = createGroupParameters(tr("Data Binding"));
QFormLayout* formLayout = (QFormLayout*)dataBindingGroup->layout();
m_seriesSwitcher = new QComboBox(dataBindingGroup);
m_seriesSwitcher->addItem(tr("Selection Model"), QVariant(SelectionDataModel));
m_seriesSwitcher->addItem(tr("Edit Model"), QVariant(EditDataModel));
connect(m_seriesSwitcher, SIGNAL(currentIndexChanged(int)), this, SLOT(seriesChanged(int)));
formLayout->addRow(m_seriesSwitcher);
m_toolBar = new QToolBar(this);
m_actAppendRow = m_toolBar->addAction(QIcon(QStringLiteral(":\\shared\\res\\appendrow.png")), tr("Append Row"));
m_actAppendRow->setEnabled(false);
connect(m_actAppendRow, SIGNAL(triggered()), this, SLOT(appendRow()));
m_actDeleteRow = m_toolBar->addAction(QIcon(QStringLiteral(":\\shared\\res\\deleterow.png")), tr("Delete Row"));
m_actDeleteRow->setEnabled(false);
connect(m_actDeleteRow, SIGNAL(triggered()), this, SLOT(deleteRow()));
formLayout->addRow(m_toolBar);
}
void MainWindow::createSelectionModelSeries()
{
disconnect(m_tableWidget, SIGNAL(itemSelectionChanged()), this, SLOT(updateEnableButtons()));
connect(m_tableWidget, SIGNAL(itemSelectionChanged()), this, SLOT(setSelectionData()));
createTitle(tr("Selection Data Model"));
m_chart->legend()->setVisible(true);
ChartBarSeries2D* series = new ChartBarSeries2D();
series->setName(tr("Input Series"));
m_chart->appendSeries(series);
ChartView2D* view2D = static_cast<ChartView2D*>(m_chart->views().at(0));
ChartAxis* axisX = view2D->axisX();
axisX->label()->setAngle(45);
series->modelController()->setModel(m_tableWidget->model());
}
void MainWindow::createEditModelSeries()
{
disconnect(m_tableWidget, SIGNAL(itemSelectionChanged()), this, SLOT(setSelectionData()));
connect(m_tableWidget, SIGNAL(itemSelectionChanged()), this, SLOT(updateEnableButtons()));
createTitle(tr("Edit Data Model"));
m_chart->legend()->setVisible(true);
ChartBarSeries2D* series = new ChartBarSeries2D();
series->setName(tr("Input Series"));
m_chart->appendSeries(series);
ChartView2D* view2D = static_cast<ChartView2D*>(m_chart->views().at(0));
ChartAxis* axisX = view2D->axisX();
axisX->label()->setAngle(45);
series->modelController()->setModel(m_tableWidget->model());
}
void MainWindow::fillDataTable()
{
m_tableWidget->clear();
m_tableWidget->setColumnCount(m_columnCount);
m_tableWidget->setRowCount(m_rowCount);
for (int i = 0; i < m_rowCount; i++)
{
for (int k = 0; k < m_columnCount; k++)
{
QTableWidgetItem* item = new QTableWidgetItem();
int value = 0;
if (k == 0)
{
value = qtn_rand(10, 30);
item->setText(QStringLiteral("%1").arg(value));
m_tableWidget->setItem(i, k, item);
}
else
{
item->setText(QStringLiteral("%1 Precipitation").arg(i + 1));
m_tableWidget->setItem(i, k, item);
}
}
}
}
void MainWindow::initSelectionRange()
{
m_tableWidget->clearSelection();
QTableWidgetSelectionRange range(0, 0, m_rowCount-1, 1);
m_tableWidget->setRangeSelected(range, true);
}
void MainWindow::updateValueParameters()
{
DemoChartWindow::updateValueParameters();
}
void MainWindow::seriesChanged(int index)
{
m_chart->clearSeries();
m_chart->clearTitles();
m_chart->setBackgroundBrush(QBrush());
QVariant var = m_seriesSwitcher->itemData(index);
switch((SeriesType)var.toUInt())
{
case SelectionDataModel :
{
m_tableWidget->clearSelection();
m_tableWidget->setSelectionMode(QAbstractItemView::ContiguousSelection);
m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
m_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_toolBar->hide();
m_columnCount = 2;
m_rowCount = 12;
fillDataTable();
createSelectionModelSeries();
initSelectionRange();
}
break;
case EditDataModel :
{
m_tableWidget->clearSelection();
m_tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
m_tableWidget->setEditTriggers(QAbstractItemView::AllEditTriggers);
m_toolBar->show();
m_columnCount = 2;
m_rowCount = 12;
fillDataTable();
createEditModelSeries();
}
break;
default:
break;
}
updateValueParameters();
}
void MainWindow::setSelectionData()
{
if (m_chart->series().count() == 0)
return;
if (ChartBarSeries2D* series = qobject_cast<ChartBarSeries2D*>(m_chart->series().at(0)))
{
QList<QTableWidgetSelectionRange> selectedRanges = m_tableWidget->selectedRanges();
if (selectedRanges.count() == 1)
{
QTableWidgetSelectionRange selected = selectedRanges.at(0);
series->modelController()->setAxisXColumn(selected.leftColumn() == selected.rightColumn() ? -1 : selected.leftColumn());
series->modelController()->setAxisYColumn(selected.rightColumn());
series->modelController()->setRowRange(selected.topRow(), selected.bottomRow());
}
else
series->modelController()->setRowRange(-1, -1);
}
}
void MainWindow::appendRow()
{
if (m_chart->series().count() == 0)
return;
int numNewRow = 0;
QList<QTableWidgetSelectionRange> selectedRanges = m_tableWidget->selectedRanges();
if (selectedRanges.count() > 0)
{
QModelIndex indexEdit = m_tableWidget->currentIndex();
if (indexEdit.isValid())
{
if (QTableWidgetItem* itemEdit = m_tableWidget->item(indexEdit.row(), indexEdit.column()))
m_tableWidget->closePersistentEditor(itemEdit);
}
m_tableWidget->reset();
QTableWidgetSelectionRange selected = selectedRanges.at(selectedRanges.count()-1);
numNewRow = selected.topRow() + 1;
}
m_rowCount++;
m_tableWidget->insertRow(numNewRow);
QTableWidgetItem* itemX = new QTableWidgetItem();
itemX->setText(QStringLiteral("%1").arg(1));
m_tableWidget->setItem(numNewRow, 0, itemX);
QTableWidgetItem* itemY = new QTableWidgetItem();
itemY->setText(QStringLiteral("%1").arg(tr("Test")));
m_tableWidget->setItem(numNewRow, 1, itemY);
QTableWidgetSelectionRange range(numNewRow, 0, numNewRow, m_columnCount-1);
m_tableWidget->setRangeSelected(range, true);
QModelIndex index = m_tableWidget->model()->index(numNewRow, 0);
if (index.isValid())
{
m_tableWidget->setCurrentIndex(index);
m_tableWidget->edit(index);
}
if (ChartBarSeries2D* series = qobject_cast<ChartBarSeries2D*>(m_chart->series().at(0)))
series->modelController()->setColumnRange(0, m_rowCount-1);
}
void MainWindow::deleteRow()
{
if (m_chart->series().count() == 0)
return;
QList<QTableWidgetSelectionRange> selectedRanges = m_tableWidget->selectedRanges();
for (int i = 0; selectedRanges.count() > i; i++)
{
QTableWidgetSelectionRange selected = selectedRanges.at(i);
m_tableWidget->removeRow(selected.topRow());
m_rowCount--;
if (ChartPointSeries* series = qobject_cast<ChartPointSeries*>(m_chart->series().at(0)))
{
ChartModelController* controller = series->modelController();
controller->setColumnRange(0, m_rowCount-1);
}
}
updateEnableButtons();
}
void MainWindow::updateEnableButtons()
{
if (m_chart->series().count() == 0)
return;
bool isEmpty = !(m_orientation == Qt::Horizontal ? m_tableWidget->model()->columnCount() > 0 : m_tableWidget->model()->rowCount() > 0);
QList<QTableWidgetSelectionRange> selectedRanges = m_tableWidget->selectedRanges();
m_actDeleteRow->setEnabled(selectedRanges.count() > 0);
m_actAppendRow->setEnabled(isEmpty || selectedRanges.count() > 0);
}