From 011a4bf2614e97cfc91c17e3b54f2a65183c5d66 2012-08-27 14:15:04 From: Michal Klocek Date: 2012-08-27 14:15:04 Subject: [PATCH] Adds charts templates to chartviewer --- diff --git a/demos/chartviewer/charts/horizontalbarchart.cpp b/demos/chartviewer/charts/horizontalbarchart.cpp index 665779a..5ebe997 100644 --- a/demos/chartviewer/charts/horizontalbarchart.cpp +++ b/demos/chartviewer/charts/horizontalbarchart.cpp @@ -28,7 +28,7 @@ class HorizontalBarChart: public Chart public: QString name() { return QObject::tr("HorizontalBarChart"); } QString category() { return QObject::tr("BarSeries"); } - QString subCategory() { return QObject::tr("Vertical"); } + QString subCategory() { return QObject::tr("Horizontal"); } QChart* createChart(const DataTable& table) { diff --git a/demos/chartviewer/charts/horizontalstackedbarchart.cpp b/demos/chartviewer/charts/horizontalstackedbarchart.cpp index c9f73e7..e8ba4e3 100644 --- a/demos/chartviewer/charts/horizontalstackedbarchart.cpp +++ b/demos/chartviewer/charts/horizontalstackedbarchart.cpp @@ -28,7 +28,7 @@ class HorizontalStackedBarChart: public Chart public: QString name() { return QObject::tr("HorizontalStackedBarChart"); } QString category() { return QObject::tr("BarSeries"); } - QString subCategory() { return QObject::tr("Vertical"); } + QString subCategory() { return QObject::tr("Horizontal"); } QChart* createChart(const DataTable& table) { diff --git a/demos/chartviewer/charts/piechart.cpp b/demos/chartviewer/charts/piechart.cpp index 9436fb9..9fc2d5f 100644 --- a/demos/chartviewer/charts/piechart.cpp +++ b/demos/chartviewer/charts/piechart.cpp @@ -26,7 +26,7 @@ class PieChart: public Chart { public: QString name() { return QObject::tr("PieChart"); } - QString category() { return QString::null; } + QString category() { return QObject::tr("PieSeries"); } QString subCategory() { return QString::null; } QChart* createChart(const DataTable& table) diff --git a/demos/chartviewer/window.cpp b/demos/chartviewer/window.cpp index cdbfc62..976c645 100644 --- a/demos/chartviewer/window.cpp +++ b/demos/chartviewer/window.cpp @@ -53,14 +53,16 @@ Window::Window(QWidget* parent) : m_antialiasCheckBox(0), m_animatedComboBox(0), m_legendComboBox(0), + m_templateComboBox(0), m_openGLCheckBox(0), m_zoomCheckBox(0), m_scrollCheckBox(0), m_rubberBand(new QGraphicsRectItem()), m_baseLayout(new QGraphicsGridLayout()), - m_menu(new QMenu(this)), + m_menu(createMenu()), m_state(NoState), - m_currentState(NoState) + m_currentState(NoState), + m_template(0) { createProxyWidgets(); connectSignals(); @@ -77,6 +79,8 @@ Window::Window(QWidget* parent) : settingsLayout->addItem(m_widgetHash["animatedComboBox"]); settingsLayout->addItem(m_widgetHash["legendLabel"]); settingsLayout->addItem(m_widgetHash["legendComboBox"]); + settingsLayout->addItem(m_widgetHash["templateLabel"]); + settingsLayout->addItem(m_widgetHash["templateComboBox"]); settingsLayout->addItem(m_widgetHash["scrollCheckBox"]); settingsLayout->addItem(m_widgetHash["zoomCheckBox"]); settingsLayout->addStretch(); @@ -85,16 +89,19 @@ Window::Window(QWidget* parent) : //create charts Charts::ChartList list = Charts::chartList(); - for (int i = 0; i < 9 && i < list.size(); ++i) { - QChart* chart = list.at(i)->createChart(m_dataTable); + for (int i = 0; i < 9; ++i) { + QChart* chart = 0; + if(icreateChart(m_dataTable); + }else{ + chart = new QChart(); + chart->setTitle(tr("Empty")); + } + m_baseLayout->addItem(chart, i / 3, i % 3); m_chartHash[chart] = i; } - foreach(Chart* chart, list) { - createMenuAction(m_menu, QIcon(), chart->name(), qVariantFromValue((void *) chart)); - } - m_form = new QGraphicsWidget(); m_form->setLayout(m_baseLayout); m_scene->addItem(m_form); @@ -123,6 +130,7 @@ void Window::connectSignals() QObject::connect(m_scrollCheckBox, SIGNAL(toggled(bool)), this, SLOT(updateUI())); QObject::connect(m_animatedComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); QObject::connect(m_legendComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); + QObject::connect(m_templateComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUI())); } void Window::createProxyWidgets() @@ -134,6 +142,7 @@ void Window::createProxyWidgets() m_openGLCheckBox = new QCheckBox(tr("OpenGL")); m_zoomCheckBox = new QCheckBox(tr("Zoom")); m_scrollCheckBox = new QCheckBox(tr("Scroll")); + m_templateComboBox= createTempleteBox(); m_widgetHash["themeComboBox"] = m_scene->addWidget(m_themeComboBox); m_widgetHash["antialiasCheckBox"] = m_scene->addWidget(m_antialiasCheckBox); m_widgetHash["animatedComboBox"] = m_scene->addWidget(m_animatedComboBox); @@ -142,8 +151,11 @@ void Window::createProxyWidgets() m_widgetHash["themeLabel"] = m_scene->addWidget(new QLabel("Theme")); m_widgetHash["animationsLabel"] = m_scene->addWidget(new QLabel("Animations")); m_widgetHash["legendLabel"] = m_scene->addWidget(new QLabel("Legend")); + m_widgetHash["templateLabel"] = m_scene->addWidget(new QLabel("Chart template")); + m_widgetHash["templateComboBox"] = m_scene->addWidget(m_templateComboBox); m_widgetHash["zoomCheckBox"] = m_scene->addWidget(m_zoomCheckBox); m_widgetHash["scrollCheckBox"] = m_scene->addWidget(m_scrollCheckBox); + } QComboBox* Window::createThemeBox() @@ -180,8 +192,27 @@ QComboBox* Window::createLegendBox() return legendComboBox; } +QComboBox* Window::createTempleteBox() +{ + QComboBox* templateComboBox = new ComboBox(this); + templateComboBox->addItem("No Template", 0); + + Charts::ChartList list = Charts::chartList(); + QMultiMap categoryMap; + + foreach(Chart* chart, list) { + categoryMap.insertMulti(chart->category(), chart); + } + foreach(const QString& category, categoryMap.uniqueKeys()) { + templateComboBox->addItem(category, category); + } + return templateComboBox; +} + + void Window::updateUI() { + checkTemplate(); checkOpenGL(); checkTheme(); checkAnimationOptions(); @@ -258,6 +289,51 @@ void Window::checkState() } } +void Window::checkTemplate() +{ + + int index = m_templateComboBox->currentIndex(); + if (m_template == index || index == 0) + return; + + QString category = m_templateComboBox->itemData(index).toString(); + Charts::ChartList list = Charts::chartList(); + + QList qchartList = m_chartHash.keys(); + + foreach(QChart* qchart,qchartList){ + for(int i = 0 ; i < m_baseLayout->count();++i) + { + if(m_baseLayout->itemAt(i)==qchart){ + m_baseLayout->removeAt(i); + break; + } + } + } + + m_chartHash.clear(); + qDeleteAll(qchartList); + + QChart* qchart(0); + + int j=0; + for (int i = 0; i < list.size(); ++i) { + Chart* chart = list.at(i); + if (chart->category() == category && j < 9) { + qchart = list.at(i)->createChart(m_dataTable); + m_baseLayout->addItem(qchart, j / 3, j % 3); + m_chartHash[qchart] = j; + j++; + } + } + for (; j < 9; ++j) { + qchart = new QChart(); + qchart->setTitle(tr("Empty")); + m_baseLayout->addItem(qchart, j / 3, j % 3); + m_chartHash[qchart] = j; + } +} + void Window::checkTheme() { QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData( @@ -321,8 +397,8 @@ void Window::mousePressEvent(QMouseEvent *event) if (plotArea.contains(m_origin)) { m_currentState = m_state; - if (m_currentState == NoState) { - showMenu(chart); + if (m_currentState == NoState && m_templateComboBox->currentIndex()==0) { + handleMenu(chart); } break; } @@ -422,7 +498,7 @@ void Window::comboBoxFocused(QComboBox *combobox) } } -void Window::showMenu(QChart* qchart) +void Window::handleMenu(QChart* qchart) { QAction *chosen = m_menu->exec(QCursor::pos()); @@ -448,6 +524,53 @@ void Window::showMenu(QChart* qchart) } +QMenu* Window::createMenu() +{ + Charts::ChartList list = Charts::chartList(); + QMultiMap categoryMap; + + QMenu* result = new QMenu(this); + + foreach(Chart* chart, list) { + categoryMap.insertMulti(chart->category(), chart); + } + + foreach(const QString& category, categoryMap.uniqueKeys()) { + QMenu* menu(0); + QMultiMap subCategoryMap; + if (category.isEmpty()) { + menu = result; + } + else { + menu = new QMenu(category, this); + result->addMenu(menu); + } + + foreach(Chart* chart , categoryMap.values(category)) { + subCategoryMap.insert(chart->subCategory(), chart); + } + + foreach(const QString& subCategory, subCategoryMap.uniqueKeys()) { + QMenu* subMenu(0); + if (subCategory.isEmpty()) { + subMenu = menu; + } + else { + subMenu = new QMenu(subCategory, this); + menu->addMenu(subMenu); + } + + foreach(Chart* chart , subCategoryMap.values(subCategory)) { + + createMenuAction(subMenu, QIcon(), chart->name(), + qVariantFromValue((void *) chart)); + } + } + } + + return result; +} + QAction* Window::createMenuAction(QMenu *menu, const QIcon &icon, const QString &text, const QVariant &data) { diff --git a/demos/chartviewer/window.h b/demos/chartviewer/window.h index 886c2db..5e94df5 100644 --- a/demos/chartviewer/window.h +++ b/demos/chartviewer/window.h @@ -55,6 +55,7 @@ private: QComboBox* createThemeBox(); QComboBox* createAnimationBox(); QComboBox* createLegendBox(); + QComboBox* createTempleteBox(); void connectSignals(); void createProxyWidgets(); void comboBoxFocused(QComboBox *combox); @@ -63,7 +64,9 @@ private: inline void checkOpenGL(); inline void checkTheme(); inline void checkState(); - void showMenu(QChart * chart); + inline void checkTemplate(); + QMenu* createMenu(); + void handleMenu(QChart * chart); QAction* createMenuAction(QMenu *menu, const QIcon &icon, const QString &text, const QVariant &data); protected: @@ -86,6 +89,7 @@ private: QCheckBox *m_antialiasCheckBox; QComboBox *m_animatedComboBox; QComboBox *m_legendComboBox; + QComboBox *m_templateComboBox; QCheckBox *m_openGLCheckBox; QCheckBox *m_zoomCheckBox; QCheckBox *m_scrollCheckBox; @@ -95,6 +99,7 @@ private: QMenu* m_menu; State m_state; State m_currentState; + int m_template; friend class ComboBox; };