diff options
author | Michal Klocek <michal.klocek@digia.com> | 2012-05-21 10:58:11 +0300 |
---|---|---|
committer | Michal Klocek <michal.klocek@digia.com> | 2012-05-21 11:47:32 +0300 |
commit | 49edf2d3495fa615f76866f5976e14073c498ccc (patch) | |
tree | 8a16aff0be9e3e91db89eaa2a5347622ebb8f687 /tests/chartdesigner | |
parent | 84c5353058e3c0ee9b4fa60e218dc0fa42ace007 (diff) |
Qt name convetion: move test to tests
Diffstat (limited to 'tests/chartdesigner')
-rw-r--r-- | tests/chartdesigner/brushwidget.cpp | 33 | ||||
-rw-r--r-- | tests/chartdesigner/brushwidget.h | 39 | ||||
-rw-r--r-- | tests/chartdesigner/chartdesigner.pro | 24 | ||||
-rw-r--r-- | tests/chartdesigner/engine.cpp | 347 | ||||
-rw-r--r-- | tests/chartdesigner/engine.h | 76 | ||||
-rw-r--r-- | tests/chartdesigner/main.cpp | 32 | ||||
-rw-r--r-- | tests/chartdesigner/mainwindow.cpp | 423 | ||||
-rw-r--r-- | tests/chartdesigner/mainwindow.h | 97 | ||||
-rw-r--r-- | tests/chartdesigner/objectinspectorwidget.cpp | 51 | ||||
-rw-r--r-- | tests/chartdesigner/objectinspectorwidget.h | 37 | ||||
-rw-r--r-- | tests/chartdesigner/penwidget.cpp | 56 | ||||
-rw-r--r-- | tests/chartdesigner/penwidget.h | 48 |
12 files changed, 1263 insertions, 0 deletions
diff --git a/tests/chartdesigner/brushwidget.cpp b/tests/chartdesigner/brushwidget.cpp new file mode 100644 index 00000000..99ae2007 --- /dev/null +++ b/tests/chartdesigner/brushwidget.cpp @@ -0,0 +1,33 @@ +/**************************************************************************** +** +** 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 "brushwidget.h" + +BrushWidget::BrushWidget(QWidget *parent):QWidget(parent) +{ + + +} + +BrushWidget::~BrushWidget() +{ + +} + diff --git a/tests/chartdesigner/brushwidget.h b/tests/chartdesigner/brushwidget.h new file mode 100644 index 00000000..4cb487c7 --- /dev/null +++ b/tests/chartdesigner/brushwidget.h @@ -0,0 +1,39 @@ +/**************************************************************************** +** +** 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 BRUSHWIDGET_H +#define BRUSHWIDGET_H + +#include <QWidget> + +class BrushWidget: public QWidget +{ +public: + explicit BrushWidget(QWidget *parent = 0); + ~BrushWidget(); + + QBrush brush() const { return m_brush; } + void setBrush(const QBrush &brush); + +private: + QBrush m_brush; +}; + +#endif /* BRUSHWIDGET_H */ diff --git a/tests/chartdesigner/chartdesigner.pro b/tests/chartdesigner/chartdesigner.pro new file mode 100644 index 00000000..0594b37f --- /dev/null +++ b/tests/chartdesigner/chartdesigner.pro @@ -0,0 +1,24 @@ +!include( ../tests.pri ) { + error( "Couldn't find the test.pri file!" ) +} + +TEMPLATE = app +QT += core gui + +SOURCES += \ + brushwidget.cpp \ + main.cpp \ + mainwindow.cpp \ + objectinspectorwidget.cpp \ + penwidget.cpp \ + engine.cpp + + +HEADERS += \ + brushwidget.h \ + mainwindow.h \ + objectinspectorwidget.h \ + penwidget.h \ + engine.h + +!system_build:mac: QMAKE_POST_LINK += "$$MAC_POST_LINK_PREFIX $$MAC_TESTS_BIN_DIR" diff --git a/tests/chartdesigner/engine.cpp b/tests/chartdesigner/engine.cpp new file mode 100644 index 00000000..6ba25340 --- /dev/null +++ b/tests/chartdesigner/engine.cpp @@ -0,0 +1,347 @@ +/**************************************************************************** + ** + ** 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 "engine.h" +#include <QItemSelectionModel> +#include <QStandardItemModel> +#include <QXYModelMapper> +#include <QBarModelMapper> +#include <QPieModelMapper> +#include <QLineSeries> +#include <QSplineSeries> +#include <QScatterSeries> +#include <QBarSeries> +#include <QPercentBarSeries> +#include <QStackedBarSeries> +#include <QAreaSeries> +#include <QPieSeries> +#include <QChart> +#include <QBarSet> + + +const qint32 MAGIC_NUMBER = 0x66666666; + +Engine::Engine(QObject* parent) : + QObject(parent), m_count(10), m_chart(new QChart()), m_model(0), m_selection(0) +{ + createModels(); +} + +Engine::~Engine() +{ + delete m_chart; + delete m_selection; + delete m_model; + +} + +void Engine::createModels() +{ + m_model = new QStandardItemModel(m_count, m_count); + m_model->setHorizontalHeaderLabels( + QStringList() << "A" << "B" << "C" << "D" << "E" << "F" << "G" << "H" << "I" << "J"); + m_model->setVerticalHeaderLabels( + QStringList() << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "10"); + m_selection = new QItemSelectionModel(m_model); + +} + +QList<QAbstractSeries*> Engine::addSeries(QAbstractSeries::SeriesType type) +{ + const QModelIndexList& list = m_selection->selectedIndexes(); + + QMap<int, QModelIndex> columns; + + foreach(const QModelIndex& index, list) { + columns.insertMulti(index.column(), index); + } + + QList<int> keys = columns.uniqueKeys(); + + QModelIndexList rows = columns.values(keys.first()); + + int minRow = m_count + 1; + int maxRow = -1; + + foreach(const QModelIndex& index, rows) { + minRow = qMin(index.row(), minRow); + maxRow = qMax(index.row(), maxRow); + } + + QList<QAbstractSeries*> result; + QColor color; + + switch (type) { + + case QAbstractSeries::SeriesTypeLine: + { + for (int i = 1; i < keys.count(); ++i) { + QLineSeries *line = new QLineSeries(); + setupXYSeries(line, keys, i, minRow, maxRow); + result << line; + } + break; + } + case QAbstractSeries::SeriesTypeSpline: + { + for (int i = 1; i < keys.count(); ++i) { + QSplineSeries *line = new QSplineSeries(); + setupXYSeries(line, keys, i, minRow, maxRow); + result << line; + } + break; + } + case QAbstractSeries::SeriesTypeScatter: + { + for (int i = 1; i < keys.count(); ++i) { + QScatterSeries *line = new QScatterSeries(); + setupXYSeries(line, keys, i, minRow, maxRow); + result << line; + } + break; + } + case QAbstractSeries::SeriesTypeBar: + { + //TODO: fix me + QBarSeries *bar = new QBarSeries(); + setupBarSeries(bar,keys,minRow,maxRow); + result << bar; + break; + } + case QAbstractSeries::SeriesTypePercentBar: + { + QPercentBarSeries *bar = new QPercentBarSeries(); + setupBarSeries(bar,keys,minRow,maxRow); + result << bar; + break; + } + case QAbstractSeries::SeriesTypeStackedBar: + { + QStackedBarSeries *bar = new QStackedBarSeries(); + setupBarSeries(bar,keys,minRow,maxRow); + result << bar; + break; + } + case QAbstractSeries::SeriesTypePie: + { + + QPieSeries *pie = new QPieSeries(); + setupPieSeries(pie,keys,minRow,maxRow); + result << pie; + break; + } + case QAbstractSeries::SeriesTypeArea: + { + QAreaSeries *area = new QAreaSeries( new QLineSeries(), new QLineSeries()); + setupAreaSeries(area,keys,minRow,maxRow); + result << area; + break; + } + } + return result; +} + +void Engine::removeSeries(QAbstractSeries* series) +{ + m_chart->removeSeries(series); + + foreach(const QModelIndex& index, m_seriesModelIndex.value(series)) { + m_model->setData(index, Qt::white, Qt::BackgroundRole); + } +} + +void Engine::clearModels() +{ + delete m_selection; + m_selection = 0; + delete m_model; + m_model = 0; + createModels(); +} + +bool Engine::save(const QString &filename) const +{ + if (filename.isEmpty()) + return false; + + QFile file(filename); + + if (!file.open(QIODevice::WriteOnly)) { + return false; + } + + QDataStream out(&file); + out << MAGIC_NUMBER; + out.setVersion(QDataStream::Qt_4_8); + out << m_model->rowCount(); + out << m_model->columnCount(); + + for (int row = 0; row < m_model->rowCount(); ++row) { + for (int column = 0; column < m_model->columnCount(); ++column) { + QStandardItem *item = m_model->item(row, column); + if (item) { + out << row; + out << column; + out << item->data(Qt::EditRole).toString(); + } + } + } + return true; +} + +bool Engine::load(const QString &filename) +{ + clearModels(); + + if (filename.isEmpty()) + return false; + + QFile file(filename); + + if (!file.open(QIODevice::ReadOnly)) { + return false; + } + + QDataStream in(&file); + + qint32 magicNumber; + in >> magicNumber; + + if (magicNumber != MAGIC_NUMBER) + return false; + + in.setVersion(QDataStream::Qt_4_8); + + int rowCount; + in >> rowCount; + + int columnCount; + in >> columnCount; + + while (!in.atEnd()) { + int row; + int column; + QString value; + in >> row >> column >> value; + QStandardItem *item = new QStandardItem(); + bool ok; + double result = value.toDouble(&ok); + if(ok) + item->setData(result, Qt::EditRole); + else + item->setData(value, Qt::EditRole); + m_model->setItem(row, column, item); + } + + return true; +} + +void Engine::setupXYSeries(QXYSeries *xyseries, const QList<int>& columns, int column, int minRow, int maxRow) +{ + xyseries->setModel(m_model); + QXYModelMapper* mapper = new QXYModelMapper(xyseries); + xyseries->setModelMapper(mapper); + mapper->setMapX(columns.first()); + mapper->setMapY(columns.at(column)); + mapper->setOrientation(Qt::Vertical); + mapper->setFirst(minRow); + mapper->setCount(maxRow - minRow + 1); + m_chart->addSeries(xyseries); + xyseries->setName(QString("Series %1").arg(m_chart->series().count())); + QObject::connect(xyseries,SIGNAL(clicked(const QPointF&)),this,SIGNAL(selected())); + const QModelIndexList& list = m_selection->selectedIndexes(); + QModelIndexList result; + foreach(const QModelIndex& index, list) { + if (index.column() ==columns.at(column)){ + m_model->setData(index, xyseries->pen().color(), Qt::BackgroundRole); + result << index; + } + } + m_seriesModelIndex.insert(xyseries,result); +} + +void Engine::setupBarSeries(QBarSeries *bar, const QList<int>& columns, int minRow, int maxRow) +{ + bar->setModel(m_model); + QBarModelMapper* mapper = new QBarModelMapper(bar); + bar->setModelMapper(mapper); + mapper->setMapCategories(columns.first()); + mapper->setMapBarTop(columns.last()); + mapper->setMapBarBottom(columns.at(1)); + mapper->setOrientation(Qt::Vertical); + mapper->setFirst(minRow); + mapper->setCount(maxRow - minRow + 1); + m_chart->addSeries(bar); + bar->setName(QString("Series %1").arg(m_chart->series().count())); + + const QModelIndexList& list = m_selection->selectedIndexes(); + foreach(const QModelIndex& index, list) { + if (index.column() >= columns.at(1) && index.column()<= columns.last()) { + //m_model->setData(index, bar->barSets().at(index.column())->brush().color(), Qt::BackgroundRole); + } + } +} + +void Engine::setupPieSeries(QPieSeries *pie, const QList<int>& columns, int minRow, int maxRow) +{ + pie->setModel(m_model); + QPieModelMapper* mapper = new QPieModelMapper(pie); + pie->setModelMapper(mapper); + mapper->setMapValues(columns.at(1)); + mapper->setMapLabels(columns.first()); + mapper->setOrientation(Qt::Vertical); + mapper->setFirst(minRow); + mapper->setCount(maxRow - minRow + 1); + m_chart->addSeries(pie); + pie->setName(QString("Series %1").arg(m_chart->series().count())); + + const QModelIndexList& list = m_selection->selectedIndexes(); + foreach(const QModelIndex& index, list) { + // m_model->setData(index, bar->barSets()pen().color(), Qt::BackgroundRole); + } +} + +void Engine::setupAreaSeries(QAreaSeries *series, const QList<int>& columns, int minRow, int maxRow) +{ + series->lowerSeries()->setModel(m_model); + series->upperSeries()->setModel(m_model); + QXYModelMapper* umapper = new QXYModelMapper(series); + umapper->setMapX(columns.first()); + umapper->setMapY(columns.at(1)); + umapper->setOrientation(Qt::Vertical); + umapper->setFirst(minRow); + umapper->setCount(maxRow - minRow + 1); + QXYModelMapper* lmapper = new QXYModelMapper(series); + lmapper->setMapX(columns.first()); + lmapper->setMapY(columns.at(2)); + lmapper->setOrientation(Qt::Vertical); + lmapper->setFirst(minRow); + lmapper->setCount(maxRow - minRow + 1); + series->upperSeries()->setModelMapper(umapper); + series->lowerSeries()->setModelMapper(lmapper); + m_chart->addSeries(series); + series->setName(QString("Series %1").arg(m_chart->series().count())); + + const QModelIndexList& list = m_selection->selectedIndexes(); + foreach(const QModelIndex& index, list) { + //if (index.column() ==columns.at(column)) + // m_model->setData(index, xyseries->pen().color(), Qt::BackgroundRole); + } +} diff --git a/tests/chartdesigner/engine.h b/tests/chartdesigner/engine.h new file mode 100644 index 00000000..26e64f02 --- /dev/null +++ b/tests/chartdesigner/engine.h @@ -0,0 +1,76 @@ +/**************************************************************************** + ** + ** 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 ENGINE_H_ +#define ENGINE_H_ + +#include <QObject> +#include <QAbstractSeries> +#include <QModelIndex> + +class QStandardItemModel; +class QItemSelectionModel; + + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +class QChart; +class QXYSeries; +class QBarSeries; +class QPieSeries; +class QAreaSeries; +QTCOMMERCIALCHART_END_NAMESPACE + +QTCOMMERCIALCHART_USE_NAMESPACE + +class Engine : public QObject +{ + Q_OBJECT +public: + explicit Engine(QObject *parent = 0); + ~Engine(); + + int modelCount() { return m_count; } + QStandardItemModel *model() const { return m_model; } + QItemSelectionModel *selectionModel() const { return m_selection; } + QChart* chart() const { return m_chart; } + void clearModels(); + QList<QAbstractSeries*> addSeries(QAbstractSeries::SeriesType type); + void removeSeries(QAbstractSeries* series); + bool save(const QString &filename) const; + bool load(const QString &filename); +signals: + void selected(); + +private: + void createModels(); + void setupXYSeries(QXYSeries *xyseries, const QList<int>& columns, int column, int minRow, int maxRow); + void setupBarSeries(QBarSeries *series, const QList<int>& columns, int minRow, int maxRow); + void setupPieSeries(QPieSeries *pie, const QList<int>& columns, int minRow, int maxRow); + void setupAreaSeries(QAreaSeries *series, const QList<int>& columns, int minRow, int maxRow); + +private: + int m_count; + QChart *m_chart; + QStandardItemModel *m_model; + QItemSelectionModel *m_selection; + QMap<QAbstractSeries*, QList<QModelIndex> > m_seriesModelIndex; +}; + +#endif /* ENGINE_H_ */ diff --git a/tests/chartdesigner/main.cpp b/tests/chartdesigner/main.cpp new file mode 100644 index 00000000..ab7a03d9 --- /dev/null +++ b/tests/chartdesigner/main.cpp @@ -0,0 +1,32 @@ +/**************************************************************************** +** +** 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 "mainwindow.h" +#include <QApplication> + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + MainWindow window; + window.resize(1000,600); + window.show(); + return a.exec(); +} diff --git a/tests/chartdesigner/mainwindow.cpp b/tests/chartdesigner/mainwindow.cpp new file mode 100644 index 00000000..b0e93e22 --- /dev/null +++ b/tests/chartdesigner/mainwindow.cpp @@ -0,0 +1,423 @@ +/**************************************************************************** + ** + ** 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 "mainwindow.h" +#include "objectinspectorwidget.h" +#include "penwidget.h" +#include "brushwidget.h" +#include "engine.h" +#include <QSettings> +#include <QChartView> +#include <QDockWidget> +#include <QMenuBar> +#include <QTableWidget> +#include <QSplitter> +#include <QMetaEnum> +#include <QStandardItemModel> +#include <QHeaderView> +#include <QDebug> +#include <QMessageBox> + +static const QString FILENAME_SETTING("filename"); +static const QString GEOMETRY_SETTING("geometry"); + +MainWindow::MainWindow() : + m_engine(new Engine(this)), + m_view(new QChartView(m_engine->chart())), + m_scene(m_view->scene()), + m_table(new QTableView()), + m_addSeriesMenu(0), + m_seriesMenu(0), + m_chartMenu(0), + m_themeMenu(0), + m_animationMenu(0), + m_zoomMenu(0), + m_removeAllAction(0), + m_legendAction(0), + m_antialiasingAction(0) +{ + createMenus(); + createDockWidgets(); + createTable(); + createLayout(); + + QSettings settings; + restoreGeometry(settings.value(GEOMETRY_SETTING).toByteArray()); + m_filename = settings.value(FILENAME_SETTING).toString(); + if (m_filename.isEmpty()) + m_filename = "untitled"; + + setWindowTitle(m_filename); + updateUI(); +} + +MainWindow::~MainWindow() +{ + delete m_engine; +} + +void MainWindow::createMenus() +{ + QMenu *file = menuBar()->addMenu(tr("File")); + QMenu *edit = menuBar()->addMenu(tr("Edit")); + m_seriesMenu = menuBar()->addMenu(tr("Series")); + m_chartMenu = menuBar()->addMenu(tr("Chart")); + + m_addSeriesMenu = new QMenu(tr("Add series")); + m_themeMenu = new QMenu(tr("Apply theme")); + m_animationMenu = new QMenu(tr("Animations")); + m_zoomMenu = new QMenu(tr("Zoom")); + + file->addAction(tr("New"), this, SLOT(handleNewAction())); + file->addAction(tr("Load"), this, SLOT(handleLoadAction())); + file->addAction(tr("Save"), this, SLOT(handleSaveAction())); + file->addAction(tr("Save As"), this, SLOT(handleSaveAsAction())); + + //seriesMenu + m_seriesMenu->addMenu(m_addSeriesMenu); + m_removeAllAction = new QAction(tr("Remove all series"), this); + QObject::connect(m_removeAllAction, SIGNAL(triggered()), this, SLOT(handleRemoveAllSeriesAction())); + m_seriesMenu->addAction(m_removeAllAction); + m_seriesMenu->addSeparator(); + + //seriesMenu /addSeriesMenu + { + int index = QAbstractSeries::staticMetaObject.indexOfEnumerator("SeriesType"); + QMetaEnum metaEnum = QAbstractSeries::staticMetaObject.enumerator(index); + + int count = metaEnum.keyCount(); + + for (int i = 0; i < count; ++i) { + QAction* action = new QAction(metaEnum.key(i), this); + action->setData(metaEnum.value(i)); + m_addSeriesMenu->addAction(action); + QObject::connect(action, SIGNAL(triggered()), this, SLOT(handleAddSeriesMenu())); + } + } + + //chartMenu / themeMenu + { + m_chartMenu->addMenu(m_themeMenu); + int index = QChart::staticMetaObject.indexOfEnumerator("ChartTheme"); + QMetaEnum metaEnum = QChart::staticMetaObject.enumerator(index); + + int count = metaEnum.keyCount(); + + for (int i = 0; i < count; ++i) { + QAction* action = new QAction(metaEnum.key(i), this); + action->setData(metaEnum.value(i)); + action->setCheckable(true); + m_themeMenu->addAction(action); + QObject::connect(action, SIGNAL(triggered()), this, SLOT(handleThemeMenu())); + } + } + + //chartMenu / animationMenu + { + m_chartMenu->addMenu(m_animationMenu); + int index = QChart::staticMetaObject.indexOfEnumerator("AnimationOption"); + QMetaEnum metaEnum = QChart::staticMetaObject.enumerator(index); + + int count = metaEnum.keyCount(); + + for (int i = 0; i < count; ++i) { + QAction* action = new QAction(metaEnum.key(i), this); + action->setData(metaEnum.value(i)); + action->setCheckable(true); + m_animationMenu->addAction(action); + QObject::connect(action, SIGNAL(triggered()), this, SLOT(handleAnimationMenu())); + } + } + + //chartMenu / zoomMenu + { + m_chartMenu->addMenu(m_zoomMenu); + int index = QChartView::staticMetaObject.indexOfEnumerator("RubberBand"); + QMetaEnum metaEnum = QChartView::staticMetaObject.enumerator(index); + + int count = metaEnum.keyCount(); + + for (int i = 0; i < count; ++i) { + QAction* action = new QAction(metaEnum.key(i), this); + action->setData(metaEnum.value(i)); + action->setCheckable(true); + m_zoomMenu->addAction(action); + QObject::connect(action, SIGNAL(triggered()), this, SLOT(handleZoomMenu())); + } + } + + //chartMenu / legend + m_legendAction = new QAction(tr("Legend"), this); + m_legendAction->setCheckable(true); + m_chartMenu->addAction(m_legendAction); + QObject::connect(m_legendAction, SIGNAL(triggered()), this, SLOT(handleLegendAction())); + + //chartMenu / Anti-aliasing + m_antialiasingAction = new QAction(tr("Anti-aliasing"), this); + m_antialiasingAction->setCheckable(true); + m_chartMenu->addAction(m_antialiasingAction); + QObject::connect(m_antialiasingAction, SIGNAL(triggered()), this, SLOT(handleAntialiasingAction())); + +} + +void MainWindow::createDockWidgets() +{ + m_brushWidget = new BrushWidget(); + QDockWidget *brushDockWidget = new QDockWidget(tr("Brush"), this); + brushDockWidget->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable); + brushDockWidget->setWidget(m_brushWidget); + addDockWidget(Qt::RightDockWidgetArea, brushDockWidget); + + m_penWidget = new PenWidget(); + QDockWidget *penDockWidget = new QDockWidget(tr("Pen"), this); + penDockWidget->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable); + penDockWidget->setWidget(m_penWidget); + addDockWidget(Qt::RightDockWidgetArea, penDockWidget); + + m_inspectorWidget = new InspectorWidget(); + QDockWidget *inspectorDockWidget = new QDockWidget(tr("Object Inspector"), this); + inspectorDockWidget->setFeatures( + QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable); + inspectorDockWidget->setWidget(m_inspectorWidget); + addDockWidget(Qt::RightDockWidgetArea, inspectorDockWidget); + + setDockOptions(QMainWindow::AnimatedDocks); +} + +void MainWindow::createLayout() +{ + m_table->horizontalHeader()->setResizeMode(QHeaderView::Stretch); + + int rowHeight = m_table->rowHeight(0); + int tableHeight = (m_engine->modelCount() * rowHeight) + m_table->horizontalHeader()->height() + 2 * m_table->frameWidth(); + + m_table->setMinimumHeight(tableHeight); + m_table->setMaximumHeight(tableHeight); + + QSplitter *splitter = new QSplitter(this); + splitter->setOrientation(Qt::Vertical); + splitter->addWidget(m_table); + splitter->addWidget(m_view); + setCentralWidget(splitter); + m_view->hide(); +} + +void MainWindow::createTable() +{ + m_table->setModel(m_engine->model()); + m_table->setSelectionModel(m_engine->selectionModel()); + QObject::connect(m_table->selectionModel(),SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this,SLOT(updateUI())); +} + +void MainWindow::updateUI() +{ + + QItemSelectionModel* selection = m_table->selectionModel(); + const QModelIndexList& list = selection->selectedIndexes(); + + QMap<int, QModelIndex> columns; + + foreach(const QModelIndex& index, list) { + columns.insertMulti(index.column(), index); + } + + QList<int> keys = columns.uniqueKeys(); + + bool seriesEnabled = false; + + foreach(QAction* action, m_addSeriesMenu->actions()) { + switch (action->data().toInt()) { + case QAbstractSeries::SeriesTypeLine: + case QAbstractSeries::SeriesTypeSpline: + case QAbstractSeries::SeriesTypeScatter: + action->setEnabled(list.count() > 0 && keys.count() >= 2); + seriesEnabled |= action->isEnabled(); + break; + case QAbstractSeries::SeriesTypeBar: + case QAbstractSeries::SeriesTypePercentBar: + case QAbstractSeries::SeriesTypeStackedBar: + action->setEnabled(list.count() > 0 && keys.count() >= 2); + seriesEnabled |= action->isEnabled(); + break; + case QAbstractSeries::SeriesTypePie: + action->setEnabled(list.count() > 0 && keys.count() == 2); + seriesEnabled |= action->isEnabled(); + break; + case QAbstractSeries::SeriesTypeArea: + action->setEnabled(list.count() > 0 && keys.count() == 3); + seriesEnabled |= action->isEnabled(); + break; + } + } + + m_chartMenu->setEnabled(m_engine->chart()->series().count() > 0); + m_seriesMenu->setEnabled(seriesEnabled || m_engine->chart()->series().count() > 0); + m_removeAllAction->setEnabled(m_engine->chart()->series().count() > 0); + + int theme = m_engine->chart()->theme(); + foreach(QAction* action, m_themeMenu->actions()) { + action->setChecked(action->data().toInt() == theme); + } + + int animation = m_engine->chart()->animationOptions(); + foreach(QAction* action, m_animationMenu->actions()) { + action->setChecked(action->data().toInt() == animation); + } + + int zoom = m_view->rubberBand(); + foreach(QAction* action, m_zoomMenu->actions()) { + action->setChecked(action->data().toInt() == zoom); + } + + m_legendAction->setChecked(m_engine->chart()->legend()->isVisible()); + m_antialiasingAction->setChecked(m_view->renderHints().testFlag(QPainter::Antialiasing)); + + foreach(QAction *action, m_seriesMenu->actions()) { + //TODO: visibility handling + //if (m_series.value(action->text())) + // ; + //action->setChecked(false); + } +} + +void MainWindow::closeEvent(QCloseEvent *event) +{ + QSettings settings; + settings.setValue(GEOMETRY_SETTING, saveGeometry()); + settings.setValue(FILENAME_SETTING, m_filename); + QMainWindow::closeEvent(event); +} + +//handlers + +void MainWindow::handleNewAction() +{ + m_engine->chart()->removeAllSeries(); + m_view->hide(); + m_engine->clearModels(); + createTable(); + m_filename = "untitled"; + setWindowTitle(m_filename); + updateUI(); +} + +void MainWindow::handleAddSeriesMenu() +{ + m_view->show(); + QAction* action = qobject_cast<QAction*>(sender()); + QList<QAbstractSeries*> series = m_engine->addSeries(QAbstractSeries::SeriesType(action->data().toInt())); + + foreach(QAbstractSeries* s , series) + { + QAction *newAction = new QAction(s->name(),this); + //newAction->setCheckable(true); + m_series.insert(s->name(),s); + m_seriesMenu->addAction(newAction); + } + + updateUI(); +} + +void MainWindow::handleRemoveAllSeriesAction() +{ + + foreach(QAction* action, m_seriesMenu->actions()){ + if(m_series.contains(action->text())){ + m_seriesMenu->removeAction(action); + m_engine->removeSeries(m_series.value(action->text())); + delete action; + } + } + + m_series.clear(); + + m_view->hide(); + updateUI(); +} + +void MainWindow::handleThemeMenu() +{ + QAction* action = qobject_cast<QAction*>(sender()); + m_engine->chart()->setTheme(QChart::ChartTheme(action->data().toInt())); + updateUI(); +} + +void MainWindow::handleAnimationMenu() +{ + QAction* action = qobject_cast<QAction*>(sender()); + m_engine->chart()->setAnimationOptions(QChart::AnimationOption(action->data().toInt())); + updateUI(); +} + +void MainWindow::handleZoomMenu() +{ + QAction* action = qobject_cast<QAction*>(sender()); + m_view->setRubberBand(QChartView::RubberBand(action->data().toInt())); + updateUI(); +} + +void MainWindow::handleAntialiasingAction() +{ + QAction* action = qobject_cast<QAction*>(sender()); + if (action->isChecked()) { + m_view->setRenderHint(QPainter::Antialiasing, true); + } + else { + m_view->setRenderHint(QPainter::Antialiasing, false); + } +} + +void MainWindow::handleLegendAction() +{ + QAction* action = qobject_cast<QAction*>(sender()); + if (action->isChecked()) { + m_engine->chart()->legend()->setVisible(true); + } + else { + m_engine->chart()->legend()->setVisible(false); + } +} + +void MainWindow::handleSaveAction() +{ + if(!m_engine->save(m_filename)) { + + QScopedPointer<QMessageBox> messageBox(new QMessageBox(this)); + messageBox->setIcon(QMessageBox::Warning); + messageBox->setWindowModality(Qt::WindowModal); + messageBox->setWindowTitle(QString(tr("Error"))); + messageBox->setText(tr("Could not write to ") + m_filename); + messageBox->exec(); + } +} + +void MainWindow::handleLoadAction() +{ + if(!m_engine->load(m_filename)) { + + QScopedPointer<QMessageBox> messageBox(new QMessageBox(this)); + messageBox->setIcon(QMessageBox::Warning); + messageBox->setWindowModality(Qt::WindowModal); + messageBox->setWindowTitle(QString(tr("Error"))); + messageBox->setText(tr("Could not open ") + m_filename); + messageBox->exec(); + + }else createTable(); +} diff --git a/tests/chartdesigner/mainwindow.h b/tests/chartdesigner/mainwindow.h new file mode 100644 index 00000000..3b120fcf --- /dev/null +++ b/tests/chartdesigner/mainwindow.h @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** 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 MAINWINDOW_H +#define MAINWINDOW_H + +#include <QMainWindow> +#include <QChartGlobal> +#include <QMap> + + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +class QChartView; +class QAbstractSeries; +QTCOMMERCIALCHART_END_NAMESPACE + +class QGraphicsScene; +class InspectorWidget; +class BrushWidget; +class PenWidget; +class QTableView; +class Engine; + +QTCOMMERCIALCHART_USE_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT +public: + MainWindow(); + ~MainWindow(); + +private slots: + void handleNewAction(); + void handleAddSeriesMenu(); + void handleRemoveAllSeriesAction(); + void updateUI(); + void handleThemeMenu(); + void handleAnimationMenu(); + void handleZoomMenu(); + void handleAntialiasingAction(); + void handleLegendAction(); + void handleSaveAction(); + void handleLoadAction(); + +protected: + void closeEvent(QCloseEvent *event); + +private: + void createMenus(); + void createDockWidgets(); + void createModel(); + void createLayout(); + void createTable(); + +private: + Engine *m_engine; + QChartView *m_view; + QGraphicsScene *m_scene; + QTableView *m_table; + InspectorWidget *m_inspectorWidget; + PenWidget *m_penWidget; + BrushWidget *m_brushWidget; + QMenu *m_addSeriesMenu; + QMenu *m_seriesMenu; + QMenu *m_chartMenu; + QMenu *m_themeMenu; + QMenu *m_animationMenu; + QMenu *m_zoomMenu; + + QAction *m_removeAllAction; + QAction *m_legendAction; + QAction *m_antialiasingAction; + QString m_filename; + + QMap<QString,QAbstractSeries*> m_series; + +}; + +#endif /* MAINWINDOW_H */ diff --git a/tests/chartdesigner/objectinspectorwidget.cpp b/tests/chartdesigner/objectinspectorwidget.cpp new file mode 100644 index 00000000..82c9dd68 --- /dev/null +++ b/tests/chartdesigner/objectinspectorwidget.cpp @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** 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 "objectinspectorwidget.h" +#include <QIcon> +#include <QPainter> +#include <QPixmapCache> + +InspectorWidget::InspectorWidget() +{ + +} + +InspectorWidget::~InspectorWidget() +{ + +} + +QIcon InspectorWidget::getColorIcon(const QColor& color,const QSize &size) const +{ + + QString key = QString("COLOR_ICON=%1:%2x%3").arg(color.name()).arg(size.width()).arg(size.height()); + + QPixmap pixmap(size); + + if (!QPixmapCache::find(key, &pixmap)) { + pixmap.fill(Qt::white); + QPainter painter(&pixmap); + painter.fillRect(2, 2, 46, 46, color); + QPixmapCache::insert(key, pixmap); + } + + return QIcon(pixmap); +} diff --git a/tests/chartdesigner/objectinspectorwidget.h b/tests/chartdesigner/objectinspectorwidget.h new file mode 100644 index 00000000..b454f31d --- /dev/null +++ b/tests/chartdesigner/objectinspectorwidget.h @@ -0,0 +1,37 @@ +/**************************************************************************** +** +** 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 INSPECTORWIDGET_H +#define INSPECTORWIDGET_H + +#include <QWidget> + +class InspectorWidget : public QWidget +{ +public: + InspectorWidget(); + ~InspectorWidget(); + +private: + QIcon getColorIcon(const QColor& color,const QSize &size) const; + +}; + +#endif diff --git a/tests/chartdesigner/penwidget.cpp b/tests/chartdesigner/penwidget.cpp new file mode 100644 index 00000000..e34a6623 --- /dev/null +++ b/tests/chartdesigner/penwidget.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** 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 "penwidget.h" +#include <QFormLayout> +#include <QComboBox> +#include <QSpinBox> + +PenWidget::PenWidget(QWidget *parent):QWidget(parent), +m_colorComboBox(0), +m_lineStyleComboBox(0), +m_widthSpinBox(0) +{ + createContent(); + createLayout(); + setFixedSize(minimumSizeHint()); +} + +PenWidget::~PenWidget() +{ + +} + +void PenWidget::createContent() +{ + m_colorComboBox = new QComboBox(this); + m_lineStyleComboBox = new QComboBox(this); + m_widthSpinBox = new QSpinBox(this); +} + +void PenWidget::createLayout() +{ + QFormLayout *layout = new QFormLayout(); + layout->addRow(tr("Color"), m_colorComboBox); + layout->addRow(tr("Style"), m_lineStyleComboBox); + layout->addRow(tr("Width"), m_widthSpinBox); + setLayout(layout); +} + diff --git a/tests/chartdesigner/penwidget.h b/tests/chartdesigner/penwidget.h new file mode 100644 index 00000000..1cef9b9a --- /dev/null +++ b/tests/chartdesigner/penwidget.h @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** 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 PENWIDGET_H +#define PENWIDGET_H + +#include <QWidget> +#include <QPen> + +class QComboBox; +class QSpinBox; + +class PenWidget : public QWidget +{ +public: + explicit PenWidget(QWidget* parent = 0); + ~PenWidget(); + + QPen pen() const { return m_pen; } +private: + void createContent(); + void createLayout(); + +private: + QPen m_pen; + QComboBox *m_colorComboBox; + QComboBox *m_lineStyleComboBox; + QSpinBox *m_widthSpinBox; +}; + +#endif /* PENWIDGET_H */ |