From c8d5b77f686a8aac29001d550c7ebd4f88753de0 2012-10-08 14:56:30 From: Michal Klocek Date: 2012-10-08 14:56:30 Subject: [PATCH] Adds chart parser to cherviewer options --- diff --git a/demos/chartviewer/grid.cpp b/demos/chartviewer/grid.cpp index bd659e4..3a2f879 100644 --- a/demos/chartviewer/grid.cpp +++ b/demos/chartviewer/grid.cpp @@ -57,15 +57,11 @@ void Grid::createCharts(const QString &category) if (category.isEmpty()) { for (int i = 0; i < m_size * m_size; ++i) { - QChart *chart = 0; - if (i < list.size()) { - chart = list.at(i)->createChart(m_dataTable); - } else { - chart = new QChart(); - chart->setTitle(QObject::tr("Empty")); - } + QChart *chart = new QChart(); + chart->setTitle(QObject::tr("Empty")); m_gridLayout->addItem(chart, i / m_size, i % m_size); m_chartHash[chart] = i; + m_chartHashRev[i] = chart; } } else { int j = 0; @@ -75,6 +71,7 @@ void Grid::createCharts(const QString &category) qchart = list.at(i)->createChart(m_dataTable); m_gridLayout->addItem(qchart, j / m_size, j % m_size); m_chartHash[qchart] = j; + m_chartHashRev[j] = qchart; j++; } } @@ -83,19 +80,58 @@ void Grid::createCharts(const QString &category) qchart->setTitle(QObject::tr("Empty")); m_gridLayout->addItem(qchart, j / m_size, j % m_size); m_chartHash[qchart] = j; + m_chartHashRev[j] = qchart; } } - m_category = category; + m_gridLayout->activate(); +} + +void Grid::createCharts(const QString &category, const QString &subcategory, const QString &name) +{ + clear(); + + QChart *qchart(0); + Charts::ChartList list = Charts::chartList(); + Chart *chart; + + //find chart + for (int i = 0; i < list.size(); ++i) { + + chart = list.at(i); + if (chart->category() == category && + chart->subCategory() == subcategory && + chart->name() == name) { + break; + } + chart = 0; + } + + //create charts + for (int j = 0; j < m_size * m_size; ++j) { + + if(!chart){ + qchart = new QChart(); + }else{ + qchart = chart->createChart(m_dataTable); + } + qchart->setTitle(QObject::tr("Empty")); + m_gridLayout->addItem(qchart, j / m_size, j % m_size); + m_chartHash[qchart] = j; + m_chartHashRev[j] = qchart; + } + m_gridLayout->activate(); } void Grid::clear() { - for (int i = 0; i < m_gridLayout->count(); ++i) - m_gridLayout->removeAt(i); + int count = m_gridLayout->count(); + for (int i = 0; i < count; ++i) + m_gridLayout->removeAt(0); qDeleteAll(m_chartHash.keys()); m_chartHash.clear(); + m_chartHashRev.clear(); } QList Grid::charts() @@ -111,8 +147,40 @@ void Grid::setState(State state) void Grid::setSize(int size) { if (m_size != size) { + + //remove old; + int count = m_gridLayout->count(); + for (int i = 0; i < count; ++i) { + m_gridLayout->removeAt(0); + } + + + QChart* qchart = 0; + int j = 0; + + for (; j < size * size; ++j) { + + qchart = m_chartHashRev[j]; + + if (!qchart){ + qchart = new QChart(); + qchart->setTitle(QObject::tr("Empty")); + } + + m_chartHash[qchart] = j; + m_chartHashRev[j] = qchart; + m_gridLayout->addItem(qchart, j / size, j % size); + } + + //delete rest + while (j < m_size * m_size) { + QChart* qchart = m_chartHashRev.take(j); + delete(qchart); + m_chartHash.remove(qchart); + j++; + } + m_size = size; - createCharts(m_category); } } @@ -132,9 +200,11 @@ void Grid::replaceChart(QChart *oldChart, Chart *newChart) } } m_chartHash.remove(oldChart); + m_chartHashRev.remove(index); QChart *chart = newChart->createChart(m_dataTable); m_gridLayout->addItem(chart, index / m_size, index % m_size); m_chartHash[chart] = index; + m_chartHashRev[index] = chart; delete oldChart; } diff --git a/demos/chartviewer/grid.h b/demos/chartviewer/grid.h index 8af7a1f..1682d30 100644 --- a/demos/chartviewer/grid.h +++ b/demos/chartviewer/grid.h @@ -45,6 +45,7 @@ public: QList charts(); void createCharts(const QString &category = QString()); + void createCharts(const QString &category, const QString &subcategory, const QString &name); void replaceChart(QChart *oldChart, Chart *newChart); void setState(State state); State state() const { return m_state; }; @@ -70,12 +71,12 @@ private: int m_size; DataTable m_dataTable; QHash m_chartHash; + QHash m_chartHashRev; State m_state; State m_currentState; QPointF m_origin; QGraphicsRectItem *m_rubberBand; QGraphicsGridLayout *m_gridLayout; - QString m_category; }; #endif /* GRID_H_ */ diff --git a/demos/chartviewer/window.cpp b/demos/chartviewer/window.cpp index 876ab7a..9e50538 100644 --- a/demos/chartviewer/window.cpp +++ b/demos/chartviewer/window.cpp @@ -58,7 +58,7 @@ Window::Window(const QVariantHash ¶meters, QWidget *parent) m_baseLayout(new QGraphicsLinearLayout()), m_menu(createMenu()), m_template(0), - m_grid(new Grid(1)) + m_grid(new Grid(-1)) { createProxyWidgets(); // create layout @@ -82,8 +82,6 @@ Window::Window(const QVariantHash ¶meters, QWidget *parent) settingsLayout->addItem(m_widgetHash["zoomCheckBox"]); settingsLayout->addStretch(); - m_grid->createCharts(); - m_baseLayout->setOrientation(Qt::Horizontal); m_baseLayout->addItem(m_grid); m_baseLayout->addItem(settingsLayout); @@ -99,6 +97,10 @@ Window::Window(const QVariantHash ¶meters, QWidget *parent) m_antialiasCheckBox->setChecked(true); initializeFromParamaters(parameters); updateUI(); + if(!m_category.isEmpty() && !m_subcategory.isEmpty() && !m_name.isEmpty()) + m_grid->createCharts(m_category,m_subcategory,m_name); + + handleGeometryChanged(); setCentralWidget(m_view); @@ -214,12 +216,34 @@ QComboBox *Window::createTempleteBox() void Window::initializeFromParamaters(const QVariantHash ¶meters) { + if (parameters.contains("view")) { + int t = parameters["view"].toInt(); + for (int i = 0; i < m_viewComboBox->count(); ++i) { + if (m_viewComboBox->itemData(i).toInt() == t) { + m_viewComboBox->setCurrentIndex(i); + break; + } + } + } + if (parameters.contains("chart")) { QString t = parameters["chart"].toString(); - for (int i = 0; i < m_templateComboBox->count(); ++i) { - if (m_templateComboBox->itemText(i) == t) { - m_templateComboBox->setCurrentIndex(i); - break; + + QRegExp rx("([a-zA-Z0-9_]*)::([a-zA-Z0-9_]*)::([a-zA-Z0-9_]*)"); + int pos = rx.indexIn(t); + + if (pos > -1) { + m_category = rx.cap(1); + m_subcategory = rx.cap(2); + m_name = rx.cap(3); + m_templateComboBox->setCurrentIndex(0); + } + else { + for (int i = 0; i < m_templateComboBox->count(); ++i) { + if (m_templateComboBox->itemText(i) == t) { + m_templateComboBox->setCurrentIndex(i); + break; + } } } } @@ -254,21 +278,12 @@ void Window::initializeFromParamaters(const QVariantHash ¶meters) } } } - if (parameters.contains("view")) { - int t = parameters["view"].toInt(); - for (int i = 0; i < m_viewComboBox->count(); ++i) { - if (m_viewComboBox->itemData(i).toInt() == t) { - m_viewComboBox->setCurrentIndex(i); - break; - } - } - } } void Window::updateUI() { - checkTemplate(); checkView(); + checkTemplate(); checkOpenGL(); checkTheme(); checkAnimationOptions(); @@ -279,7 +294,10 @@ void Window::updateUI() void Window::checkView() { int count(m_viewComboBox->itemData(m_viewComboBox->currentIndex()).toInt()); - m_grid->setSize(count); + if(m_grid->size()!=count){ + m_grid->setSize(count); + m_template = 0; + } } void Window::checkLegend() diff --git a/demos/chartviewer/window.h b/demos/chartviewer/window.h index 4be8cac..bfc914d 100644 --- a/demos/chartviewer/window.h +++ b/demos/chartviewer/window.h @@ -92,6 +92,9 @@ private: QMenu *m_menu; int m_template; Grid *m_grid; + QString m_category; + QString m_subcategory; + QString m_name; friend class ComboBox; };