From abb4622b2dab821b9139ef024b6e4a4917762dd2 2012-10-17 07:46:13 From: sauimone Date: 2012-10-17 07:46:13 Subject: [PATCH] new legend example for testing new api. Currently using still the old one. --- diff --git a/examples/examples.pro b/examples/examples.pro index 185b842..c62cf9e 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -32,7 +32,8 @@ SUBDIRS += \ temperaturerecords \ donutchart \ multiaxis \ - callout + callout \ + newlegend !linux-arm*: { SUBDIRS += \ diff --git a/examples/newlegend/main.cpp b/examples/newlegend/main.cpp new file mode 100644 index 0000000..61d5fa3 --- /dev/null +++ b/examples/newlegend/main.cpp @@ -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$ +** +****************************************************************************/ + +#include "mainwidget.h" + +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + MainWidget w; + w.resize(720, 480); + w.show(); + + return a.exec(); +} diff --git a/examples/newlegend/mainwidget.cpp b/examples/newlegend/mainwidget.cpp new file mode 100644 index 0000000..9f40373 --- /dev/null +++ b/examples/newlegend/mainwidget.cpp @@ -0,0 +1,246 @@ +/**************************************************************************** +** +** 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 "mainwidget.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +MainWidget::MainWidget(QWidget *parent) : + QWidget(parent) +{ + // Create buttons for ui + m_buttonLayout = new QGridLayout(); + QPushButton *detachLegendButton = new QPushButton("Toggle attached"); + connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(toggleAttached())); + m_buttonLayout->addWidget(detachLegendButton, 0, 0); + + QPushButton *addSliceButton = new QPushButton("add slice"); + connect(addSliceButton, SIGNAL(clicked()), this, SLOT(addSlice())); + m_buttonLayout->addWidget(addSliceButton, 2, 0); + QPushButton *removeSliceButton = new QPushButton("remove slice"); + connect(removeSliceButton, SIGNAL(clicked()), this, SLOT(removeSlice())); + m_buttonLayout->addWidget(removeSliceButton, 3, 0); + + QPushButton *alignButton = new QPushButton("Align (Bottom)"); + connect(alignButton, SIGNAL(clicked()), this, SLOT(setLegendAlignment())); + m_buttonLayout->addWidget(alignButton, 4, 0); + + QPushButton *boldButton = new QPushButton("Toggle bold"); + connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold())); + m_buttonLayout->addWidget(boldButton, 8, 0); + + QPushButton *italicButton = new QPushButton("Toggle italic"); + connect(italicButton, SIGNAL(clicked()), this, SLOT(toggleItalic())); + m_buttonLayout->addWidget(italicButton, 9, 0); + + m_legendPosX = new QDoubleSpinBox(); + m_legendPosY = new QDoubleSpinBox(); + m_legendWidth = new QDoubleSpinBox(); + m_legendHeight = new QDoubleSpinBox(); + + connect(m_legendPosX, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout())); + connect(m_legendPosY, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout())); + connect(m_legendWidth, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout())); + connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout())); + + QFormLayout* legendLayout = new QFormLayout(); + legendLayout->addRow("HPos", m_legendPosX); + legendLayout->addRow("VPos", m_legendPosY); + legendLayout->addRow("Width", m_legendWidth); + legendLayout->addRow("Height", m_legendHeight); + m_legendSettings = new QGroupBox("Detached legend"); + m_legendSettings->setLayout(legendLayout); + m_buttonLayout->addWidget(m_legendSettings); + m_legendSettings->setVisible(false); + + // Create chart view with the chart + m_chart = new QChart(); + m_chartView = new QChartView(m_chart, this); + + // Create spinbox to modify font size + m_fontSize = new QDoubleSpinBox(); + m_fontSize->setValue(m_chart->legend()->font().pointSizeF()); + connect(m_fontSize, SIGNAL(valueChanged(double)), this, SLOT(fontSizeChanged())); + + QFormLayout* fontLayout = new QFormLayout(); + fontLayout->addRow("Legend font size", m_fontSize); + + // Create layout for grid and detached legend + m_mainLayout = new QGridLayout(); + m_mainLayout->addLayout(m_buttonLayout, 0, 0); + m_mainLayout->addLayout(fontLayout,1,0); + m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1); + setLayout(m_mainLayout); + + createSeries(); +} + +void MainWidget::createSeries() +{ + m_series = new QPieSeries(); + addSlice(); + addSlice(); + addSlice(); + addSlice(); + + m_chart->addSeries(m_series); + m_chart->setTitle("Legend detach example"); + m_chart->createDefaultAxes(); +//![1] + m_chart->legend()->setVisible(true); + m_chart->legend()->setAlignment(Qt::AlignBottom); +//![1] + + m_chartView->setRenderHint(QPainter::Antialiasing); +} + +void MainWidget::showLegendSpinbox() +{ + m_legendSettings->setVisible(true); + QRectF chartViewRect = m_chartView->rect(); + + m_legendPosX->setMinimum(0); + m_legendPosX->setMaximum(chartViewRect.width()); + m_legendPosX->setValue(150); + + m_legendPosY->setMinimum(0); + m_legendPosY->setMaximum(chartViewRect.height()); + m_legendPosY->setValue(150); + + m_legendWidth->setMinimum(0); + m_legendWidth->setMaximum(chartViewRect.width()); + m_legendWidth->setValue(150); + + m_legendHeight->setMinimum(0); + m_legendHeight->setMaximum(chartViewRect.height()); + m_legendHeight->setValue(75); +} + +void MainWidget::hideLegendSpinbox() +{ + m_legendSettings->setVisible(false); +} + + +void MainWidget::toggleAttached() +{ + QLegend *legend = m_chart->legend(); + if (legend->isAttachedToChart()) { + //![2] + legend->detachFromChart(); + m_chart->legend()->setBackgroundVisible(true); + m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128))); + m_chart->legend()->setPen(QPen(QColor(192,192,192,192))); + //![2] + showLegendSpinbox(); + updateLegendLayout(); + } else { + //![3] + legend->attachToChart(); + legend->setBackgroundVisible(false); + //![3] + hideLegendSpinbox(); + } + update(); +} + +void MainWidget::addSlice() +{ + QPieSlice* slice = new QPieSlice(QString("slice " + QString::number(m_series->count())), m_series->count()+1); +// slice->setValue(); + m_series->append(slice); +} + +void MainWidget::removeSlice() +{ + QList slices = m_series->slices(); + if (slices.count() > 0) { + m_series->remove(slices.at(slices.count()-1)); + } +} + +void MainWidget::setLegendAlignment() +{ + QPushButton *button = qobject_cast(sender()); + + switch (m_chart->legend()->alignment()) { + case Qt::AlignTop: + m_chart->legend()->setAlignment(Qt::AlignLeft); + if (button) + button->setText("Align (Left)"); + break; + case Qt::AlignLeft: + m_chart->legend()->setAlignment(Qt::AlignBottom); + if (button) + button->setText("Align (Bottom)"); + break; + case Qt::AlignBottom: + m_chart->legend()->setAlignment(Qt::AlignRight); + if (button) + button->setText("Align (Right)"); + break; + default: + if (button) + button->setText("Align (Top)"); + m_chart->legend()->setAlignment(Qt::AlignTop); + break; + } +} + +void MainWidget::toggleBold() +{ + QFont font = m_chart->legend()->font(); + font.setBold(!font.bold()); + m_chart->legend()->setFont(font); +} + +void MainWidget::toggleItalic() +{ + QFont font = m_chart->legend()->font(); + font.setItalic(!font.italic()); + m_chart->legend()->setFont(font); +} + +void MainWidget::fontSizeChanged() +{ + QFont font = m_chart->legend()->font(); + font.setPointSizeF(m_fontSize->value()); + m_chart->legend()->setFont(font); +} + +void MainWidget::updateLegendLayout() +{ +//![4] + m_chart->legend()->setGeometry(QRectF(m_legendPosX->value() + ,m_legendPosY->value() + ,m_legendWidth->value() + ,m_legendHeight->value())); + m_chart->legend()->update(); +//![4] +} diff --git a/examples/newlegend/mainwidget.h b/examples/newlegend/mainwidget.h new file mode 100644 index 0000000..549eac3 --- /dev/null +++ b/examples/newlegend/mainwidget.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** 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 MAINWIDGET_H +#define MAINWIDGET_H + +#include "qchartglobal.h" +#include "qchart.h" +#include "qchartview.h" +#include +#include +#include +#include +#include +#include +#include +#include + +QTCOMMERCIALCHART_USE_NAMESPACE + +class MainWidget : public QWidget +{ + Q_OBJECT +public: + explicit MainWidget(QWidget *parent = 0); + void createSeries(); + void showLegendSpinbox(); + void hideLegendSpinbox(); + +signals: + +public slots: + void toggleAttached(); + void addSlice(); + void removeSlice(); + + void setLegendAlignment(); + + void toggleBold(); + void toggleItalic(); + void fontSizeChanged(); + + void updateLegendLayout(); + + +private: + + QChart *m_chart; + QPieSeries *m_series; + + QChartView *m_chartView; + QGridLayout *m_mainLayout; + QGridLayout *m_buttonLayout; + QGridLayout *m_fontLayout; + + QDoubleSpinBox *m_fontSize; + + // For detached layout + QGroupBox* m_legendSettings; + QDoubleSpinBox *m_legendPosX; + QDoubleSpinBox *m_legendPosY; + QDoubleSpinBox *m_legendWidth; + QDoubleSpinBox *m_legendHeight; +}; + +#endif // MAINWIDGET_H diff --git a/examples/newlegend/newlegend.pro b/examples/newlegend/newlegend.pro new file mode 100644 index 0000000..4b31516 --- /dev/null +++ b/examples/newlegend/newlegend.pro @@ -0,0 +1,10 @@ +!include( ../examples.pri ) { + error( "Couldn't find the examples.pri file!" ) +} + +TARGET = newlegend +SOURCES += main.cpp \ + mainwidget.cpp + +HEADERS += \ + mainwidget.h diff --git a/src/legend/legend.pri b/src/legend/legend.pri index 8859df4..a22b5c2 100644 --- a/src/legend/legend.pri +++ b/src/legend/legend.pri @@ -5,7 +5,8 @@ SOURCES += \ $$PWD/qlegend.cpp \ $$PWD/legendmarker.cpp \ $$PWD/legendlayout.cpp \ - $$PWD/qlegendmarker.cpp + $$PWD/qlegendmarker.cpp \ + $$PWD/qpielegendmarker.cpp PRIVATE_HEADERS += \ $$PWD/legendmarker_p.h \ @@ -16,4 +17,5 @@ PRIVATE_HEADERS += \ PUBLIC_HEADERS += \ $$PWD/qlegend.h \ - $$PWD/qlegendmarker.h + $$PWD/qlegendmarker.h \ + $$PWD/qpielegendmarker.h diff --git a/src/legend/qlegendmarker.cpp b/src/legend/qlegendmarker.cpp index 2fc6459..f1d2baa 100644 --- a/src/legend/qlegendmarker.cpp +++ b/src/legend/qlegendmarker.cpp @@ -24,8 +24,9 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -QLegendMarker::QLegendMarker(QObject *parent) : - QObject(parent)//, +QLegendMarker::QLegendMarker(QAbstractSeries* series, QObject *parent) : + QObject(parent), + m_series(series) // d_ptr(new QLegendMarkerPrivate(this)) { } diff --git a/src/legend/qlegendmarker.h b/src/legend/qlegendmarker.h index 43d22fe..44d78e5 100644 --- a/src/legend/qlegendmarker.h +++ b/src/legend/qlegendmarker.h @@ -30,6 +30,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE // TODO: //class QLegendMarkerPrivate; +class QAbstractSeries; // TODO: should this be abstract? class QTCOMMERCIALCHART_EXPORT QLegendMarker : public QObject @@ -42,7 +43,7 @@ class QTCOMMERCIALCHART_EXPORT QLegendMarker : public QObject // Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged); public: - explicit QLegendMarker(QObject *parent = 0); + explicit QLegendMarker(QAbstractSeries* series, QObject *parent = 0); virtual ~QLegendMarker(); QString label() const; @@ -57,6 +58,10 @@ public: bool isVisible() const; void setVisible(bool visible); +// virtual QAbstractSeries::SeriesType type() = 0; + virtual QAbstractSeries* series() { return m_series; } + virtual QObject* peerObject() = 0; + Q_SIGNALS: void clicked(); void hovered(bool status); @@ -70,6 +75,7 @@ public: Q_DISABLE_COPY(QLegendMarker) // TODO: move to PIMPL + QAbstractSeries* m_series; QString m_label; QPen m_pen; QBrush m_brush; diff --git a/src/legend/qpielegendmarker.cpp b/src/legend/qpielegendmarker.cpp new file mode 100644 index 0000000..9652405 --- /dev/null +++ b/src/legend/qpielegendmarker.cpp @@ -0,0 +1,36 @@ +/**************************************************************************** +** +** 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 "qpielegendmarker.h" +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +QPieLegendMarker::QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent) : + QLegendMarker(series, parent), + m_slice(slice) +{ +} + + +#include "moc_qpielegendmarker.cpp" +//#include "moc_qpielegendmarker_p.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/legend/qpielegendmarker.h b/src/legend/qpielegendmarker.h new file mode 100644 index 0000000..fe0d2c3 --- /dev/null +++ b/src/legend/qpielegendmarker.h @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** 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 QPIELEGENDMARKER_H +#define QPIELEGENDMARKER_H + +#include +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QTCOMMERCIALCHART_EXPORT QPieLegendMarker : public QLegendMarker +{ + Q_OBJECT + +public: + explicit QPieLegendMarker(QPieSeries* series, QPieSlice* slice, QObject *parent = 0); + + virtual QPieSlice* peerObject() { return m_slice; } + +//Q_SIGNALS: + +//public Q_SLOTS: + +private: +// TODO: +// QScopedPointer d_ptr; + Q_DISABLE_COPY(QPieLegendMarker) + + QPieSlice* m_slice; + +}; + +QTCOMMERCIALCHART_END_NAMESPACE +#endif // QPIELEGENDMARKER_H