From 6a089ea6fc66056de7b8ab9f8de4662a8ea9b376 2012-08-31 09:51:44 From: Tero Ahola Date: 2012-08-31 09:51:44 Subject: [PATCH] Added ChartView.minimumMargins to QML properties --- diff --git a/demos/qmlcustommodel/qml/qmlcustommodel/main.qml b/demos/qmlcustommodel/qml/qmlcustommodel/main.qml index 74ea2eb..89b0989 100644 --- a/demos/qmlcustommodel/qml/qmlcustommodel/main.qml +++ b/demos/qmlcustommodel/qml/qmlcustommodel/main.qml @@ -168,12 +168,13 @@ Rectangle { } } - VPieModelMapper { - model: customModel - labelsColumn: 1 - valuesColumn: 2 - firstRow: 1 - } + } + VPieModelMapper { + model: customModel + series: pieSeries + labelsColumn: 1 + valuesColumn: 2 + firstRow: 1 } //![3] } diff --git a/plugins/declarative/declarative.pro b/plugins/declarative/declarative.pro index 77e1211..fffc56e 100644 --- a/plugins/declarative/declarative.pro +++ b/plugins/declarative/declarative.pro @@ -22,7 +22,8 @@ SOURCES += \ declarativescatterseries.cpp \ declarativepieseries.cpp \ declarativebarseries.cpp \ - declarativecategoryaxis.cpp + declarativecategoryaxis.cpp \ + declarativemargins.cpp HEADERS += \ declarativechart.h \ @@ -34,7 +35,8 @@ HEADERS += \ declarativescatterseries.h \ declarativepieseries.h \ declarativebarseries.h \ - declarativecategoryaxis.h + declarativecategoryaxis.h \ + declarativemargins.h TARGETPATH = QtCommercial/Chart target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH diff --git a/plugins/declarative/declarativechart.cpp b/plugins/declarative/declarativechart.cpp index b8688e3..938a966 100644 --- a/plugins/declarative/declarativechart.cpp +++ b/plugins/declarative/declarativechart.cpp @@ -31,6 +31,7 @@ #include "qvalueaxis.h" #include "qcategoryaxis.h" #include "qabstractseries_p.h" +#include "declarativemargins.h" #ifndef QT_ON_ARM #include "qdatetimeaxis.h" @@ -117,26 +118,27 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE /*! \qmlproperty real ChartView::topMargin - The space between the top of chart view and the top of the plot area. The title (if non-empty) is drawn on top margin - area of the chart view. Top margin area is also used by legend, if aligned to top. + Deprecated. Use minimumMargins and plotArea instead. */ /*! \qmlproperty real ChartView::bottomMargin - The space between the bottom of chart view and the bottom of the plot area. The bottom margin area may be used by - legend (if aligned to bottom), x-axis, x-axis labels and x-axis tick marks. + Deprecated. Use minimumMargins and plotArea instead. */ /*! \qmlproperty real ChartView::leftMargin - The space between the left side of chart view and the left side of the plot area. The left margin area may be used by - legend (if aligned to left), y-axis, y-axis labels and y-axis tick marks. + Deprecated. Use minimumMargins and plotArea instead. */ /*! \qmlproperty real ChartView::rightMargin - The space between the right side of chart view and the right side of the plot area. The right margin area may be used - by legend (if aligned to right). + Deprecated. Use minimumMargins and plotArea instead. +*/ + +/*! + \qmlproperty Margins ChartView::minimumMargins + The minimum margins allowed between the outer bounds and the plotArea of the ChartView. */ /*! @@ -223,24 +225,17 @@ DeclarativeChart::DeclarativeChart(QDeclarativeItem *parent) m_chart(new QChart(this)) { setFlag(QGraphicsItem::ItemHasNoContents, false); - //TODO: check what should be really here margins or platArea ?! - m_chartMargins = m_chart->minimumMargins(); - connect(m_chart, SIGNAL(marginsChanged(QRectF)), this, SLOT(handleMarginsChanged(QRectF))); + m_minMargins = new DeclarativeMargins(this); + connect(m_minMargins, SIGNAL(topChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int))); + connect(m_minMargins, SIGNAL(bottomChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int))); + connect(m_minMargins, SIGNAL(leftChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int))); + connect(m_minMargins, SIGNAL(rightChanged(int, int, int, int)), this, SLOT(changeMinimumMargins(int, int, int, int))); + // TODO: connect to plotAreaChanged signal from m_chart } -void DeclarativeChart::handleMarginsChanged(QRectF newMargins) +void DeclarativeChart::changeMinimumMargins(int top, int bottom, int left, int right) { - //TODO: check what should be really here margins or platArea ?! - if (m_chartMargins.top() != newMargins.top()) - topMarginChanged(m_chart->minimumMargins().top()); - if (m_chartMargins.bottom() != newMargins.bottom()) - bottomMarginChanged(m_chart->minimumMargins().bottom()); - if (m_chartMargins.left() != newMargins.left()) - leftMarginChanged(m_chart->minimumMargins().left()); - if (m_chartMargins.right() != newMargins.right()) - rightMarginChanged(m_chart->minimumMargins().right()); - - m_chartMargins = m_chart->minimumMargins(); + m_chart->setMinimumMargins(QMargins(left, top, right, bottom)); } DeclarativeChart::~DeclarativeChart() @@ -482,22 +477,26 @@ bool DeclarativeChart::dropShadowEnabled() qreal DeclarativeChart::topMargin() { - return m_chart->minimumMargins().top(); + qWarning() << "ChartView.topMargin is deprecated. Use minimumMargins and plotArea instead."; + return m_chart->plotArea().top(); } qreal DeclarativeChart::bottomMargin() { - return m_chart->minimumMargins().bottom(); + qWarning() << "ChartView.bottomMargin is deprecated. Use minimumMargins and plotArea instead."; + return m_chart->plotArea().bottom(); } qreal DeclarativeChart::leftMargin() { - return m_chart->minimumMargins().left(); + qWarning() << "ChartView.leftMargin is deprecated. Use minimumMargins and plotArea instead."; + return m_chart->plotArea().left(); } qreal DeclarativeChart::rightMargin() { - return m_chart->minimumMargins().right(); + qWarning() << "ChartView.rightMargin is deprecated. Use minimumMargins and plotArea instead."; + return m_chart->plotArea().right(); } void DeclarativeChart::zoom(qreal factor) diff --git a/plugins/declarative/declarativechart.h b/plugins/declarative/declarativechart.h index cb0cf36..74d9ca9 100644 --- a/plugins/declarative/declarativechart.h +++ b/plugins/declarative/declarativechart.h @@ -27,6 +27,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE +class DeclarativeMargins; + class DeclarativeChart : public QDeclarativeItem { Q_OBJECT @@ -43,6 +45,7 @@ class DeclarativeChart : public QDeclarativeItem Q_PROPERTY(qreal bottomMargin READ bottomMargin NOTIFY bottomMarginChanged) Q_PROPERTY(qreal leftMargin READ leftMargin NOTIFY leftMarginChanged) Q_PROPERTY(qreal rightMargin READ rightMargin NOTIFY rightMarginChanged) + Q_PROPERTY(DeclarativeMargins *minimumMargins READ minimumMargins REVISION 1) Q_ENUMS(Animation) Q_ENUMS(Theme) Q_ENUMS(SeriesType) @@ -111,6 +114,7 @@ public: qreal leftMargin(); qreal rightMargin(); void createDefaultAxes(QAbstractSeries* series); + DeclarativeMargins *minimumMargins() { return m_minMargins; } public: Q_INVOKABLE QAbstractSeries *series(int index); @@ -139,7 +143,8 @@ Q_SIGNALS: void rightMarginChanged(qreal margin); public Q_SLOTS: - void handleMarginsChanged(QRectF newMargins); +// void handleMarginsChanged(QRectF newMargins); + void changeMinimumMargins(int top, int bottom, int left, int right); void handleAxisXSet(QAbstractAxis *axis); void handleAxisYSet(QAbstractAxis *axis); @@ -147,7 +152,8 @@ private: // Extending QChart with DeclarativeChart is not possible because QObject does not support // multi inheritance, so we now have a QChart as a member instead QChart *m_chart; - QMargins m_chartMargins; + //QMargins m_chartMargins; + DeclarativeMargins *m_minMargins; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/plugins/declarative/declarativemargins.cpp b/plugins/declarative/declarativemargins.cpp new file mode 100644 index 0000000..592a9c2 --- /dev/null +++ b/plugins/declarative/declarativemargins.cpp @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** 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 "declarativemargins.h" +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +/*! + \qmlclass Margins + Uncreatable type that is used to define top, bottom, left and right margins. +*/ + +/*! + \qmlproperty int Margins::top + The top margin. +*/ + +/*! + \qmlproperty int Margins::bottom + The bottom margin. +*/ + +/*! + \qmlproperty int Margins::left + The left margin. +*/ + +/*! + \qmlproperty int Margins::right + The right margin. +*/ + +DeclarativeMargins::DeclarativeMargins(QObject *parent) : + QObject(parent) +{ + QMargins::setTop(0); + QMargins::setBottom(0); + QMargins::setLeft(0); + QMargins::setRight(0); +} + +void DeclarativeMargins::setTop(int top) +{ + if (top < 0) { + qWarning() << "Cannot set top margin to a negative value:" << top; + } else { + if (top != QMargins::top()) { + QMargins::setTop(top); + emit topChanged(QMargins::top(), QMargins::bottom(), QMargins::left(), QMargins::right()); + } + } +} + +void DeclarativeMargins::setBottom(int bottom) +{ + if (bottom < 0) { + qWarning() << "Cannot set bottom margin to a negative value:" << bottom; + } else { + if (bottom != QMargins::bottom()) { + QMargins::setBottom(bottom); + emit bottomChanged(QMargins::top(), QMargins::bottom(), QMargins::left(), QMargins::right()); + } + } +} + +void DeclarativeMargins::setLeft(int left) +{ + if (left < 0) { + qWarning() << "Cannot set left margin to a negative value:" << left; + } else { + if (left != QMargins::left()) { + QMargins::setLeft(left); + emit leftChanged(QMargins::top(), QMargins::bottom(), QMargins::left(), QMargins::right()); + } + } +} + +void DeclarativeMargins::setRight(int right) +{ + if (right < 0) { + qWarning() << "Cannot set left margin to a negative value:" << right; + } else { + if (right != QMargins::right()) { + QMargins::setRight(right); + emit rightChanged(QMargins::top(), QMargins::bottom(), QMargins::left(), QMargins::right()); + } + } +} + +#include "moc_declarativemargins.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/plugins/declarative/declarativemargins.h b/plugins/declarative/declarativemargins.h new file mode 100644 index 0000000..0e0d118 --- /dev/null +++ b/plugins/declarative/declarativemargins.h @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** 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 DECLARATIVE_MARGINS_H +#define DECLARATIVE_MARGINS_H + +#include "qchartglobal.h" +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class DeclarativeMargins : public QObject, public QMargins +{ + Q_OBJECT + Q_PROPERTY(int top READ top WRITE setTop NOTIFY topChanged) + Q_PROPERTY(int bottom READ bottom WRITE setBottom NOTIFY bottomChanged) + Q_PROPERTY(int left READ left WRITE setLeft NOTIFY leftChanged) + Q_PROPERTY(int right READ right WRITE setRight NOTIFY rightChanged) + +public: + explicit DeclarativeMargins(QObject *parent = 0); + void setTop(int top); + void setBottom(int bottom); + void setLeft(int left); + void setRight(int right); + +Q_SIGNALS: + void topChanged(int top, int bottom, int left, int right); + void bottomChanged(int top, int bottom, int left, int right); + void leftChanged(int top, int bottom, int left, int right); + void rightChanged(int top, int bottom, int left, int right); +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // DECLARATIVE_MARGINS_H diff --git a/plugins/declarative/plugin.cpp b/plugins/declarative/plugin.cpp index 6e23b10..a7aa2f7 100644 --- a/plugins/declarative/plugin.cpp +++ b/plugins/declarative/plugin.cpp @@ -40,6 +40,7 @@ #include "qvpiemodelmapper.h" #include "qhbarmodelmapper.h" #include "qvbarmodelmapper.h" +#include "declarativemargins.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -97,7 +98,7 @@ public: QLatin1String("Trying to create uncreatable: QPieSeries. Use PieSeries instead.")); // QtCommercial.Chart 1.1 - qmlRegisterType(uri, 1, 1, "ChartView"); + qmlRegisterType(uri, 1, 1, "ChartView"); qmlRegisterType(uri, 1, 1, "XYPoint"); qmlRegisterType(uri, 1, 1, "ScatterSeries"); qmlRegisterType(uri, 1, 1, "LineSeries"); @@ -145,6 +146,8 @@ public: QLatin1String("Trying to create uncreatable: BarsetBase.")); qmlRegisterUncreatableType(uri, 1, 1, "QPieSeries", QLatin1String("Trying to create uncreatable: QPieSeries. Use PieSeries instead.")); + qmlRegisterUncreatableType(uri, 1, 1, "Margins", + QLatin1String("Trying to create uncreatable: Margins.")); } }; diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml b/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml index 1172d7c..c9a6bfe 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/Chart.qml @@ -64,7 +64,10 @@ ChartView { legend.onColorChanged: console.log("legend.onColorChanged: " + color); legend.onBorderColorChanged: console.log("legend.onBorderColorChanged: " + color); legend.onLabelColorChanged: console.log("legend.onLabelColorChanged: " + color); - + minimumMargins.onTopChanged: console.log("chart.minimumMargins.onTopChanged: " + top); + minimumMargins.onBottomChanged: console.log("chart.minimumMargins.onBottomChanged: " + bottom); + minimumMargins.onLeftChanged: console.log("chart.minimumMargins.onLeftChanged: " + left); + minimumMargins.onRightChanged: console.log("chart.minimumMargins.onRightChanged: " + right); ValueAxis{ onColorChanged: console.log("axisX.onColorChanged: " + color); diff --git a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor1.qml b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor1.qml index 6f77da1..285561d 100644 --- a/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor1.qml +++ b/tests/qmlchartproperties/qml/qmlchartproperties/ChartEditor1.qml @@ -84,6 +84,42 @@ Row { text: "title color" onClicked: chart.titleColor = main.nextColor(); } + Button { + text: "zoom -" + onClicked: chart.zoom(0.5); + } + Button { + text: "top min margin +" + onClicked: chart.minimumMargins.top += 5; + } + Button { + text: "top min margin -" + onClicked: chart.minimumMargins.top -= 5; + } + Button { + text: "bottom min margin +" + onClicked: chart.minimumMargins.bottom += 5; + } + Button { + text: "bottom min margin -" + onClicked: chart.minimumMargins.bottom -= 5; + } + Button { + text: "left min margin +" + onClicked: chart.minimumMargins.left += 5; + } + Button { + text: "left min margin -" + onClicked: chart.minimumMargins.left -= 5; + } + Button { + text: "right min margin +" + onClicked: chart.minimumMargins.right += 5; + } + Button { + text: "right min margin -" + onClicked: chart.minimumMargins.right -= 5; + } } FontEditor {