diff --git a/demos/chartviewer/chartviewer.pro b/demos/chartviewer/chartviewer.pro index 3509ecb..a816e4d 100644 --- a/demos/chartviewer/chartviewer.pro +++ b/demos/chartviewer/chartviewer.pro @@ -3,6 +3,6 @@ include(charts/charts.pri) TARGET = chartviewer QT += opengl INCLUDEPATH += . -SOURCES += main.cpp window.cpp view.cpp -HEADERS += window.h view.h charts.h model.h +SOURCES += main.cpp window.cpp view.cpp grid.cpp +HEADERS += window.h view.h charts.h model.h grid.h diff --git a/demos/chartviewer/grid.cpp b/demos/chartviewer/grid.cpp new file mode 100644 index 0000000..5236e21 --- /dev/null +++ b/demos/chartviewer/grid.cpp @@ -0,0 +1,238 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the Qt Commercial Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "grid.h" +#include "charts.h" +#include +#include +#include +#include + +Grid::Grid(int size,QGraphicsItem *parent):QGraphicsWidget(parent), + m_listCount(3), + m_valueMax(10), + m_valueCount(7), + m_size(size), + m_dataTable(Model::generateRandomData(m_listCount, m_valueMax, m_valueCount)), + m_rubberBand(new QGraphicsRectItem()), + m_gridLayout(new QGraphicsGridLayout()) +{ + setLayout(m_gridLayout); + m_rubberBand->setParentItem(this); + m_rubberBand->setVisible(false); + m_rubberBand->setZValue(2); +} + +Grid::~Grid() +{ + +} + +void Grid::createCharts() +{ + clear(); + + Charts::ChartList list = Charts::chartList(); + + 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")); + } + + m_gridLayout->addItem(chart, i / m_size, i % m_size); + m_chartHash[chart] = i; + } + +} + +void Grid::createCharts(const QString& category) +{ + clear(); + + QChart *qchart(0); + Charts::ChartList list = Charts::chartList(); + + int j = 0; + for (int i = 0; i < list.size(); ++i) { + Chart *chart = list.at(i); + if (chart->category() == category && j < m_size * m_size) { + qchart = list.at(i)->createChart(m_dataTable); + m_gridLayout->addItem(qchart, j / m_size, j % m_size); + m_chartHash[qchart] = j; + j++; + } + } + for (; j < m_size * m_size; ++j) { + qchart = new QChart(); + qchart->setTitle(QObject::tr("Empty")); + m_gridLayout->addItem(qchart, j / m_size, j % m_size); + m_chartHash[qchart] = j; + } + + m_gridLayout->activate(); +} + +void Grid::clear() +{ + for (int i = 0; i < m_gridLayout->count(); ++i) { + m_gridLayout->removeAt(i); + } + + qDeleteAll(m_chartHash.keys()); + m_chartHash.clear(); +} + +QList Grid::charts() +{ + return m_chartHash.keys(); +} + +void Grid::setState(State state) +{ + m_state = state; +} + +void Grid::setRubberPen(const QPen& pen) +{ + m_rubberBand->setPen(pen); +} + +void Grid::replaceChart(QChart* oldChart, Chart* newChart) +{ + int index = m_chartHash[oldChart]; + //not in 4.7.2 m_baseLayout->removeItem(qchart); + for (int i = 0; i < m_gridLayout->count(); ++i) { + if (m_gridLayout->itemAt(i) == oldChart) { + m_gridLayout->removeAt(i); + break; + } + } + m_chartHash.remove(oldChart); + QChart *chart = newChart->createChart(m_dataTable); + m_gridLayout->addItem(chart, index / m_size, index % m_size); + m_chartHash[chart] = index; + delete oldChart; +} + +void Grid::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + + m_origin = event->pos(); + m_currentState = NoState; + + foreach (QChart *chart, charts()) { + + QRectF geometryRect = chart->geometry(); + QRectF plotArea = chart->plotArea(); + plotArea.translate(geometryRect.topLeft()); + if (plotArea.contains(m_origin)) { + m_currentState = m_state; + if (m_currentState == NoState) emit chartSelected(chart); + break; + } + } + if (m_currentState == ZoomState) { + m_rubberBand->setRect(QRectF(m_origin, QSize())); + m_rubberBand->setVisible(true); + } + + event->accept(); + } + + if (event->button() == Qt::RightButton) { + m_origin = event->pos(); + m_currentState = m_state; + } +} + +void Grid::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + if (m_currentState != NoState) { + + foreach (QChart *chart, charts()) { + + QRectF geometryRect = chart->geometry(); + QRectF plotArea = chart->plotArea(); + plotArea.translate(geometryRect.topLeft()); + + if (plotArea.contains(m_origin)) { + if (m_currentState == ScrollState) { + QPointF delta = m_origin - event->pos(); + chart->scroll(delta.x(), -delta.y()); + } + if (m_currentState == ZoomState && plotArea.contains(event->pos())) + m_rubberBand->setRect(QRectF(m_origin, event->pos()).normalized()); + break; + } + } + if (m_currentState == ScrollState) + m_origin = event->pos(); + event->accept(); + } +} + +void Grid::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + if (m_currentState == ZoomState) { + m_rubberBand->setVisible(false); + + foreach (QChart *chart, charts()) { + + QRectF geometryRect = chart->geometry(); + QRectF plotArea = chart->plotArea(); + plotArea.translate(geometryRect.topLeft()); + + if (plotArea.contains(m_origin)) { + QRectF rect = m_rubberBand->rect(); + rect.translate(-geometryRect.topLeft()); + chart->zoomIn(rect); + break; + } + } + } + + m_currentState = NoState; + event->accept(); + } + + if (event->button() == Qt::RightButton) { + + if (m_currentState == ZoomState) { + foreach (QChart *chart, charts()) { + + QRectF geometryRect = chart->geometry(); + QRectF plotArea = chart->plotArea(); + plotArea.translate(geometryRect.topLeft()); + + if (plotArea.contains(m_origin)) { + chart->zoomOut(); + break; + } + } + } + } +} diff --git a/demos/chartviewer/grid.h b/demos/chartviewer/grid.h new file mode 100644 index 0000000..2c0ffa2 --- /dev/null +++ b/demos/chartviewer/grid.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the Qt Commercial Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef GRID_H_ +#define GRID_H_ + +#include "model.h" +#include +#include + +class QGraphicsGridLayout; +class GridControl; +class Chart; + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +class QChart; +QTCOMMERCIALCHART_END_NAMESPACE + +QTCOMMERCIALCHART_USE_NAMESPACE + +class Grid : public QGraphicsWidget +{ + Q_OBJECT +public: + enum State { NoState = 0, ZoomState, ScrollState}; + Grid(int size , QGraphicsItem *parent = 0 ); + ~Grid(); + QList charts(); + void createCharts(); + void createCharts(const QString& category); + void replaceChart(QChart* oldChart, Chart* newChart); + void setState(State state); + State state(){ return m_state; }; + void setRubberPen(const QPen& pen); +Q_SIGNAL + void chartSelected(QChart* chart); +protected: + void mousePressEvent(QGraphicsSceneMouseEvent *event); + void mouseMoveEvent(QGraphicsSceneMouseEvent *event); + void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); +private: + void clear(); +private: + int m_listCount; + int m_valueMax; + int m_valueCount; + int m_size; + DataTable m_dataTable; + QHash m_chartHash; + GridControl* m_control; + State m_state; + State m_currentState; + QPointF m_origin; + QGraphicsRectItem *m_rubberBand; + QGraphicsGridLayout* m_gridLayout; +}; + +#endif /* GRID_H_ */ diff --git a/demos/chartviewer/window.cpp b/demos/chartviewer/window.cpp index 008f008..245acee 100644 --- a/demos/chartviewer/window.cpp +++ b/demos/chartviewer/window.cpp @@ -20,6 +20,7 @@ #include "window.h" #include "view.h" +#include "grid.h" #include "charts.h" #include #include @@ -32,7 +33,6 @@ #include #include #include -#include #include #include #include @@ -42,12 +42,8 @@ Window::Window(const QVariantHash& parameters,QWidget *parent) : QMainWindow(parent), - m_listCount(3), - m_valueMax(10), - m_valueCount(7), m_scene(new QGraphicsScene(this)), m_view(0), - m_dataTable(Model::generateRandomData(m_listCount, m_valueMax, m_valueCount)), m_form(0), m_themeComboBox(0), m_antialiasCheckBox(0), @@ -57,12 +53,10 @@ Window::Window(const QVariantHash& parameters,QWidget *parent) : m_openGLCheckBox(0), m_zoomCheckBox(0), m_scrollCheckBox(0), - m_rubberBand(new QGraphicsRectItem()), - m_baseLayout(new QGraphicsGridLayout()), + m_baseLayout(new QGraphicsLinearLayout()), m_menu(createMenu()), - m_state(NoState), - m_currentState(NoState), - m_template(0) + m_template(0), + m_grid(new Grid(3)) { createProxyWidgets(); // create layout @@ -82,29 +76,16 @@ Window::Window(const QVariantHash& parameters,QWidget *parent) : settingsLayout->addItem(m_widgetHash["scrollCheckBox"]); settingsLayout->addItem(m_widgetHash["zoomCheckBox"]); settingsLayout->addStretch(); - m_baseLayout->addItem(settingsLayout, 0, 3, 2, 1); - //create charts - Charts::ChartList list = Charts::chartList(); + m_grid->createCharts(); - for (int i = 0; i < 9; ++i) { - QChart *chart = 0; - if (i < list.size()) { - chart = list.at(i)->createChart(m_dataTable); - } else { - chart = new QChart(); - chart->setTitle(tr("Empty")); - } - - m_baseLayout->addItem(chart, i / 3, i % 3); - m_chartHash[chart] = i; - } + m_baseLayout->setOrientation(Qt::Horizontal); + m_baseLayout->addItem(m_grid); + m_baseLayout->addItem(settingsLayout); m_form = new QGraphicsWidget(); m_form->setLayout(m_baseLayout); m_scene->addItem(m_form); - m_scene->addItem(m_rubberBand); - m_rubberBand->setVisible(false); m_view = new View(m_scene, m_form); m_view->setMinimumSize(m_form->minimumSize().toSize()); @@ -133,6 +114,7 @@ void Window::connectSignals() 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())); + QObject::connect(m_grid, SIGNAL(chartSelected(QChart*)),this,SLOT(handleChartSelected(QChart*))); } void Window::createProxyWidgets() @@ -270,10 +252,10 @@ void Window::checkLegend() Qt::Alignment alignment(m_legendComboBox->itemData(m_legendComboBox->currentIndex()).toInt()); if (!alignment) { - foreach (QChart *chart, m_chartHash.keys()) + foreach (QChart *chart, m_grid->charts()) chart->legend()->hide(); } else { - foreach (QChart *chart, m_chartHash.keys()) { + foreach (QChart *chart, m_grid->charts()) { chart->legend()->setAlignment(alignment); chart->legend()->show(); } @@ -304,8 +286,10 @@ void Window::checkAnimationOptions() QChart::AnimationOptions options( m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt()); - if (!m_chartHash.isEmpty() && m_chartHash.keys().at(0)->animationOptions() != options) { - foreach (QChart *chart, m_chartHash.keys()) + QList charts = m_grid->charts(); + + if (!charts.isEmpty() && charts.at(0)->animationOptions() != options) { + foreach (QChart *chart, charts) chart->setAnimationOptions(options); } } @@ -314,68 +298,33 @@ void Window::checkState() { bool scroll = m_scrollCheckBox->isChecked(); - if (m_state != ScrollState && scroll) { - m_state = ScrollState; + + if (m_grid->state() != Grid::ScrollState && scroll) { + m_grid->setState(Grid::ScrollState); m_zoomCheckBox->setChecked(false); - } else if (!scroll && m_state == ScrollState) { - m_state = NoState; + } else if (!scroll && m_grid->state() == Grid::ScrollState) { + m_grid->setState(Grid::NoState); } bool zoom = m_zoomCheckBox->isChecked(); - if (m_state != ZoomState && zoom) { - m_state = ZoomState; + if (m_grid->state() != Grid::ZoomState && zoom) { + m_grid->setState(Grid::ZoomState); m_scrollCheckBox->setChecked(false); - } else if (!zoom && m_state == ZoomState) { - m_state = NoState; + } else if (!zoom && m_grid->state() == Grid::ZoomState) { + m_grid->setState(Grid::NoState); } } void Window::checkTemplate() { - int index = m_templateComboBox->currentIndex(); if (m_template == index || index == 0) return; m_template = index; - 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; - } - m_baseLayout->activate(); + m_grid->createCharts(category); } void Window::checkTheme() @@ -383,7 +332,7 @@ void Window::checkTheme() QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData( m_themeComboBox->currentIndex()).toInt(); - foreach (QChart *chart, m_chartHash.keys()) + foreach (QChart *chart, m_grid->charts()) chart->setTheme(theme); QPalette pal = window()->palette(); @@ -415,110 +364,7 @@ void Window::checkTheme() foreach (QGraphicsProxyWidget *widget, m_widgetHash) widget->setPalette(pal); m_view->setBackgroundBrush(pal.color((QPalette::Window))); - m_rubberBand->setPen(pal.color((QPalette::WindowText))); -} - -void Window::mousePressEvent(QMouseEvent *event) -{ - if (event->button() == Qt::LeftButton) { - - m_origin = event->pos(); - m_currentState = NoState; - - foreach (QChart *chart, m_chartHash.keys()) { - - QRectF geometryRect = chart->geometry(); - QRectF plotArea = chart->plotArea(); - plotArea.translate(geometryRect.topLeft()); - if (plotArea.contains(m_origin)) { - m_currentState = m_state; - if (m_currentState == NoState && m_templateComboBox->currentIndex() == 0) - handleMenu(chart); - break; - } - } - - if (m_currentState == ZoomState) { - m_rubberBand->setRect(QRectF(m_origin, QSize())); - m_rubberBand->setVisible(true); - } - - event->accept(); - } - - if (event->button() == Qt::RightButton) { - m_origin = event->pos(); - m_currentState = m_state; - } -} - -void Window::mouseMoveEvent(QMouseEvent *event) -{ - if (m_currentState != NoState) { - - foreach (QChart *chart, m_chartHash.keys()) { - - QRectF geometryRect = chart->geometry(); - QRectF plotArea = chart->plotArea(); - plotArea.translate(geometryRect.topLeft()); - - if (plotArea.contains(m_origin)) { - if (m_currentState == ScrollState) { - QPointF delta = m_origin - event->pos(); - chart->scroll(delta.x(), -delta.y()); - } - if (m_currentState == ZoomState && plotArea.contains(event->pos())) - m_rubberBand->setRect(QRectF(m_origin, event->pos()).normalized()); - break; - } - } - if (m_currentState == ScrollState) - m_origin = event->pos(); - event->accept(); - } -} - -void Window::mouseReleaseEvent(QMouseEvent *event) -{ - if (event->button() == Qt::LeftButton) { - if (m_currentState == ZoomState) { - m_rubberBand->setVisible(false); - - foreach (QChart *chart, m_chartHash.keys()) { - - QRectF geometryRect = chart->geometry(); - QRectF plotArea = chart->plotArea(); - plotArea.translate(geometryRect.topLeft()); - - if (plotArea.contains(m_origin)) { - QRectF rect = m_rubberBand->rect(); - rect.translate(-geometryRect.topLeft()); - chart->zoomIn(rect); - break; - } - } - } - - m_currentState = NoState; - event->accept(); - } - - if (event->button() == Qt::RightButton) { - - if (m_currentState == ZoomState) { - foreach (QChart *chart, m_chartHash.keys()) { - - QRectF geometryRect = chart->geometry(); - QRectF plotArea = chart->plotArea(); - plotArea.translate(geometryRect.topLeft()); - - if (plotArea.contains(m_origin)) { - chart->zoomOut(); - break; - } - } - } - } + m_grid->setRubberPen(pal.color((QPalette::WindowText))); } void Window::comboBoxFocused(QComboBox *combobox) @@ -531,29 +377,17 @@ void Window::comboBoxFocused(QComboBox *combobox) } } -void Window::handleMenu(QChart *qchart) +void Window::handleChartSelected(QChart *qchart) { + if(m_templateComboBox->currentIndex() != 0) return; + QAction *chosen = m_menu->exec(QCursor::pos()); if (chosen) { Chart *chart = (Chart *) chosen->data().value(); - int index = m_chartHash[qchart]; - //not in 4.7.2 m_baseLayout->removeItem(qchart); - for (int i = 0 ; i < m_baseLayout->count(); ++i) { - if (m_baseLayout->itemAt(i) == qchart) { - m_baseLayout->removeAt(i); - break; - } - } - - m_chartHash.remove(qchart); - QChart *newChart = chart->createChart(m_dataTable); - m_baseLayout->addItem(newChart, index / 3, index % 3); - m_chartHash[newChart] = index; - delete qchart; + m_grid->replaceChart(qchart,chart); updateUI(); } - } QMenu *Window::createMenu() @@ -594,7 +428,6 @@ QMenu *Window::createMenu() } } } - return result; } diff --git a/demos/chartviewer/window.h b/demos/chartviewer/window.h index 14eecf1..a9220e7 100644 --- a/demos/chartviewer/window.h +++ b/demos/chartviewer/window.h @@ -20,7 +20,6 @@ #ifndef WINDOW_H #define WINDOW_H -#include "model.h" #include #include #include @@ -31,8 +30,9 @@ class QGraphicsRectItem; class QGraphicsScene; class QGraphicsWidget; class View; -class QGraphicsGridLayout; +class QGraphicsLinearLayout; class Chart; +class Grid; QTCOMMERCIALCHART_BEGIN_NAMESPACE class QChart; @@ -44,7 +44,6 @@ QTCOMMERCIALCHART_USE_NAMESPACE class Window: public QMainWindow { Q_OBJECT - enum State { NoState = 0, ZoomState, ScrollState}; public: explicit Window(const QVariantHash& parameters, QWidget *parent = 0); ~Window(); @@ -52,6 +51,7 @@ public: private Q_SLOTS: void updateUI(); void handleGeometryChanged(); + void handleChartSelected(QChart *chart); private: QComboBox *createThemeBox(); QComboBox *createAnimationBox(); @@ -67,24 +67,18 @@ private: inline void checkState(); inline void checkTemplate(); QMenu *createMenu(); - void handleMenu(QChart *chart); QAction *createMenuAction(QMenu *menu, const QIcon &icon, const QString &text, const QVariant &data); void initializeFromParamaters(const QVariantHash& parameters); protected: - void mousePressEvent(QMouseEvent *event); - void mouseMoveEvent(QMouseEvent *event); - void mouseReleaseEvent(QMouseEvent *event); + //void mousePressEvent(QMouseEvent *event); + //void mouseMoveEvent(QMouseEvent *event); + //void mouseReleaseEvent(QMouseEvent *event); private: - int m_listCount; - int m_valueMax; - int m_valueCount; QGraphicsScene *m_scene; View *m_view; QHash m_widgetHash; - QHash m_chartHash; - DataTable m_dataTable; QGraphicsWidget *m_form; QComboBox *m_themeComboBox; @@ -95,13 +89,10 @@ private: QCheckBox *m_openGLCheckBox; QCheckBox *m_zoomCheckBox; QCheckBox *m_scrollCheckBox; - QPoint m_origin; - QGraphicsRectItem *m_rubberBand; - QGraphicsGridLayout *m_baseLayout; + QGraphicsLinearLayout *m_baseLayout; QMenu *m_menu; - State m_state; - State m_currentState; int m_template; + Grid* m_grid; friend class ComboBox; };