From d39e2c5d635316123e1783f735f479ed6f3ba0f3 2012-06-01 08:59:53 From: Tero Ahola Date: 2012-06-01 08:59:53 Subject: [PATCH] Removed DeclarativePieSlice; properties in QPieSlice now --- diff --git a/demos/qmlcustommodel/customtablemodel.cpp b/demos/qmlcustommodel/customtablemodel.cpp index 741a14e..4d0feb8 100644 --- a/demos/qmlcustommodel/customtablemodel.cpp +++ b/demos/qmlcustommodel/customtablemodel.cpp @@ -77,6 +77,11 @@ bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, return false; } +QVariant CustomTableModel::at(int row, int column) +{ + return data(index(row, column)); +} + void CustomTableModel::insertColumn(int column, const QModelIndex &parent) { beginInsertColumns(parent, column, column); diff --git a/demos/qmlcustommodel/customtablemodel.h b/demos/qmlcustommodel/customtablemodel.h index f472b19..67500d1 100644 --- a/demos/qmlcustommodel/customtablemodel.h +++ b/demos/qmlcustommodel/customtablemodel.h @@ -27,7 +27,8 @@ class CustomTableModel : public QAbstractTableModel { Q_OBJECT - Q_PROPERTY(int count READ rowCount) + Q_PROPERTY(int rowCount READ rowCount) + Q_PROPERTY(int columnCount READ columnCount) public: explicit CustomTableModel(QObject *parent = 0); @@ -42,6 +43,7 @@ public: void insertRow(int row, const QModelIndex &parent = QModelIndex()); Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex()); Q_INVOKABLE bool removeRow (int row, const QModelIndex &parent = QModelIndex()); + Q_INVOKABLE QVariant at(int row, int column); private: QList * > m_data; diff --git a/demos/qmlcustommodel/qml/qmlcustommodel/main.qml b/demos/qmlcustommodel/qml/qmlcustommodel/main.qml index b7ab9d2..9a94807 100644 --- a/demos/qmlcustommodel/qml/qmlcustommodel/main.qml +++ b/demos/qmlcustommodel/qml/qmlcustommodel/main.qml @@ -28,41 +28,47 @@ Rectangle { ChartView { id: chart - title: "Custom model example" + title: "Top-5 car brand shares in Finland" anchors.fill: parent theme: ChartView.ChartThemeLight axisX.max: 10 axisX.min: 0 axisY.max: 20 axisY.min: 0 + animationOptions: ChartView.SeriesAnimations + axisXLabels: [0, "2007", 1, "2008", 2, "2009", 3, "2010", 4, "2011", 5, "2012"] // For dynamic data we use a custom data model derived from QAbstractiItemModel CustomModel { id: customModel - CustomModelElement { values: [0, "Manufacturer", 1, 2] } - CustomModelElement { values: [1, "Volkswagen", 13.5, 12.5] } - CustomModelElement { values: [2, "Toyota", 10.9, 9.9] } - CustomModelElement { values: [3, "Ford", 8.6, 7.6] } - CustomModelElement { values: [4, "Skoda", 8.2, 7.2] } - CustomModelElement { values: [5, "Volvo", 6.8, 5.8] } + CustomModelElement { values: [0, "Manufacturer", 0, 1, 2, 3, 4] } + CustomModelElement { values: [1, "Volkswagen", 10.3, 12.0, 12.8, 13.0, 13.8] } + CustomModelElement { values: [2, "Toyota", 13.8, 13.5, 16.2, 13.7, 10.7] } + CustomModelElement { values: [3, "Ford", 6.4, 7.1, 8.9, 8.2, 8.6] } + CustomModelElement { values: [4, "Skoda", 4.7, 5.8, 6.9, 8.3, 8.2] } + CustomModelElement { values: [5, "Volvo", 7.1, 6.7, 6.5, 6.3, 7.0] } + CustomModelElement { values: [6, "Others", 57.7, 54.9, 48.7, 50.5, 51.7] } } - LineSeries { - name: "Volkswagen" - HXYModelMapper { + BarSeries { + name: "Others" + barMargin: 0 + HBarModelMapper { model: customModel - xRow: 0 - yRow: 1 + firstBarSetRow: 6 + lastBarSetRow: 6 first: 2 } } LineSeries { - name: "Toyota" + id: lineSeries + name: "Volkswagen" HXYModelMapper { + id: lineSeriesMapper model: customModel xRow: 0 - yRow: 2 + yRow: 1 first: 2 } } @@ -71,64 +77,29 @@ Rectangle { id: pieSeries size: 0.4 horizontalPosition: 0.7 - verticalPosition: 0.3 - } + verticalPosition: 0.4 + onClicked: { + // Show the selection by exploding the slice + for (var i = 0; i < pieSeries.count; i++) + pieSeries.at(i).exploded = false; + slice.exploded = true; -// VPieModelMapper { -// series: pieSeries -// model: customModel -// labelsColumn: 1 -// valuesColumn: 2 -// first: 1 -// } - HPieModelMapper { - series: pieSeries - model: customModel - labelsRow: 1 - valuesRow: 2 - first: 2 - } - - AreaSeries { - name: "Ford" - upperSeries: LineSeries { - HXYModelMapper { - model: customModel - xRow: 0 - yRow: 3 - first: 2 + // Update the line series to show the yearly data for this slice + lineSeries.name = slice.label; + for (var j = 0; j < customModel.rowCount; j++) { + if (customModel.at(j, 1) == slice.label) { + lineSeriesMapper.yRow = j; + } } } } - GroupedBarSeries { - name: "Skoda and Volvo" - HBarModelMapper { - model: customModel - firstBarSetRow: 4 - lastBarSetRow: 5 - first: 2 - } + VPieModelMapper { + series: pieSeries + model: customModel + labelsColumn: 1 + valuesColumn: 2 + first: 1 } } - - - // TODO: you could also implement appending to your model, for example: -// pieSeries.model.append(["Others", 52.0]); - - // TODO: show how to use data from a list model in a chart view - // i.e. copy the data into a custom model -// ListModel { -// id: listModel -// ListElement { -// label: "Volkswagen" -// value: 13.5 -// } -// ListElement { -// label: "Toyota" -// value: 10.9 -// } -// // and so on... -// } - } diff --git a/plugins/declarative/declarativepieseries.cpp b/plugins/declarative/declarativepieseries.cpp index 2e755cd..6d47259 100644 --- a/plugins/declarative/declarativepieseries.cpp +++ b/plugins/declarative/declarativepieseries.cpp @@ -27,60 +27,6 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -DeclarativePieSlice::DeclarativePieSlice(QObject *parent) : - QPieSlice(parent) -{ -} - -QColor DeclarativePieSlice::color() -{ - return brush().color(); -} - -void DeclarativePieSlice::setColor(QColor color) -{ - QBrush b = brush(); - b.setColor(color); - setBrush(b); -} - -QColor DeclarativePieSlice::borderColor() -{ - return pen().color(); -} - -void DeclarativePieSlice::setBorderColor(QColor color) -{ - QPen p = pen(); - p.setColor(color); - setPen(p); -} - -int DeclarativePieSlice::borderWidth() -{ - return pen().width(); -} - -void DeclarativePieSlice::setBorderWidth(int width) -{ - QPen p = pen(); - p.setWidth(width); - setPen(p); -} - -QColor DeclarativePieSlice::labelColor() -{ - return labelBrush().color(); -} - -void DeclarativePieSlice::setLabelColor(QColor color) -{ - // TODO: use brush instead for label color - QBrush b = labelBrush(); - b.setColor(color); - setLabelBrush(b); -} - DeclarativePieSeries::DeclarativePieSeries(QObject *parent) : QPieSeries(parent) { @@ -93,8 +39,8 @@ void DeclarativePieSeries::classBegin() void DeclarativePieSeries::componentComplete() { foreach(QObject *child, children()) { - if (qobject_cast(child)) { - QPieSeries::append(qobject_cast(child)); + if (qobject_cast(child)) { + QPieSeries::append(qobject_cast(child)); } else if(qobject_cast(child)) { QVPieModelMapper *mapper = qobject_cast(child); mapper->setSeries(this); @@ -117,28 +63,28 @@ void DeclarativePieSeries::appendSeriesChildren(QDeclarativeListProperty sliceList = slices(); if (index < sliceList.count()) - return qobject_cast(sliceList[index]); + return sliceList[index]; return 0; } -DeclarativePieSlice* DeclarativePieSeries::find(QString label) +QPieSlice* DeclarativePieSeries::find(QString label) { foreach (QPieSlice *slice, slices()) { if (slice->label() == label) - return qobject_cast(slice); + return slice; } return 0; } -DeclarativePieSlice* DeclarativePieSeries::append(QString label, qreal value) +QPieSlice* DeclarativePieSeries::append(QString label, qreal value) { // TODO: parameter order is wrong, switch it: - DeclarativePieSlice *slice = new DeclarativePieSlice(this); + QPieSlice *slice = new QPieSlice(this); slice->setLabel(label); slice->setValue(value); QPieSeries::append(slice); diff --git a/plugins/declarative/declarativepieseries.h b/plugins/declarative/declarativepieseries.h index 9a2fe65..5b31268 100644 --- a/plugins/declarative/declarativepieseries.h +++ b/plugins/declarative/declarativepieseries.h @@ -32,26 +32,6 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QChart; -class DeclarativePieSlice: public QPieSlice -{ - Q_OBJECT - Q_PROPERTY(QColor color READ color WRITE setColor) - Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor) - Q_PROPERTY(int borderWidth READ borderWidth WRITE setBorderWidth) - Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor) - -public: - explicit DeclarativePieSlice(QObject *parent = 0); - QColor color(); - void setColor(QColor color); - QColor borderColor(); - void setBorderColor(QColor color); - int borderWidth(); - void setBorderWidth(int width); - QColor labelColor(); - void setLabelColor(QColor color); -}; - class DeclarativePieSeries : public QPieSeries, public QDeclarativeParserStatus { Q_OBJECT @@ -62,9 +42,9 @@ class DeclarativePieSeries : public QPieSeries, public QDeclarativeParserStatus public: explicit DeclarativePieSeries(QObject *parent = 0); QDeclarativeListProperty seriesChildren(); - Q_INVOKABLE DeclarativePieSlice *at(int index); - Q_INVOKABLE DeclarativePieSlice* find(QString label); - Q_INVOKABLE DeclarativePieSlice* append(QString label, qreal value); + Q_INVOKABLE QPieSlice *at(int index); + Q_INVOKABLE QPieSlice *find(QString label); + Q_INVOKABLE QPieSlice *append(QString label, qreal value); public: void classBegin(); diff --git a/plugins/declarative/plugin.cpp b/plugins/declarative/plugin.cpp index 1f7dd62..4660dd7 100644 --- a/plugins/declarative/plugin.cpp +++ b/plugins/declarative/plugin.cpp @@ -58,7 +58,7 @@ public: qmlRegisterType(uri, 1, 0, "StackedBarSeries"); qmlRegisterType(uri, 1, 0, "PercentBarSeries"); qmlRegisterType(uri, 1, 0, "PieSeries"); - qmlRegisterType(uri, 1, 0, "PieSlice"); + qmlRegisterType(uri, 1, 0, "PieSlice"); qmlRegisterType(uri, 1, 0, "BarSet"); qmlRegisterType(uri, 1, 0, "HXYModelMapper"); qmlRegisterType(uri, 1, 0, "VXYModelMapper"); @@ -71,8 +71,6 @@ public: QLatin1String("Trying to create uncreatable: QScatterSeries.")); qmlRegisterUncreatableType(uri, 1, 0, "QPieSeries", QLatin1String("Trying to create uncreatable: QPieSeries.")); - qmlRegisterUncreatableType(uri, 1, 0, "QPieSlice", - QLatin1String("Trying to create uncreatable: QPieSlice.")); qmlRegisterUncreatableType(uri, 1, 0, "AbstractItemModel", QLatin1String("Trying to create uncreatable: AbstractItemModel.")); qmlRegisterUncreatableType(uri, 1, 0, "XYModelMapper", diff --git a/src/piechart/qpieslice.cpp b/src/piechart/qpieslice.cpp index 6cb8eed..1d25743 100644 --- a/src/piechart/qpieslice.cpp +++ b/src/piechart/qpieslice.cpp @@ -452,6 +452,63 @@ qreal QPieSlice::angleSpan() const return d_ptr->m_data.m_angleSpan; } +QColor QPieSlice::color() +{ + return brush().color(); +} + +void QPieSlice::setColor(QColor color) +{ + QBrush b = brush(); + if (color != b.color()) { + b.setColor(color); + setBrush(b); + } +} + +QColor QPieSlice::borderColor() +{ + return pen().color(); +} + +void QPieSlice::setBorderColor(QColor color) +{ + QPen p = pen(); + if (color != p.color()) { + p.setColor(color); + setPen(p); + emit borderColorChanged(); + } +} + +int QPieSlice::borderWidth() +{ + return pen().width(); +} + +void QPieSlice::setBorderWidth(int width) +{ + QPen p = pen(); + if (width != p.width()) { + p.setWidth(width); + setPen(p); + } +} + +QColor QPieSlice::labelColor() +{ + return labelBrush().color(); +} + +void QPieSlice::setLabelColor(QColor color) +{ + QBrush b = labelBrush(); + if (color != b.color()) { + b.setColor(color); + setLabelBrush(b); + } +} + /*! Returns the series that this slice belongs to. @@ -483,6 +540,10 @@ QPieSlicePrivate *QPieSlicePrivate::fromSlice(QPieSlice *slice) void QPieSlicePrivate::setPen(const QPen &pen, bool themed) { if (m_data.m_slicePen != pen) { + if (m_data.m_slicePen.color() != pen.color()) + emit q_ptr->borderColorChanged(); + if (m_data.m_slicePen.width() != pen.width()) + emit q_ptr->borderWidthChanged(); m_data.m_slicePen = pen; m_data.m_slicePen.setThemed(themed); emit q_ptr->penChanged(); @@ -492,6 +553,8 @@ void QPieSlicePrivate::setPen(const QPen &pen, bool themed) void QPieSlicePrivate::setBrush(const QBrush &brush, bool themed) { if (m_data.m_sliceBrush != brush) { + if (m_data.m_sliceBrush.color() != brush.color()) + emit q_ptr->colorChanged(); m_data.m_sliceBrush = brush; m_data.m_sliceBrush.setThemed(themed); emit q_ptr->brushChanged(); @@ -501,6 +564,8 @@ void QPieSlicePrivate::setBrush(const QBrush &brush, bool themed) void QPieSlicePrivate::setLabelBrush(const QBrush &brush, bool themed) { if (m_data.m_labelBrush != brush) { + if (m_data.m_labelBrush.color() != brush.color()) + emit q_ptr->labelColorChanged(); m_data.m_labelBrush = brush; m_data.m_labelBrush.setThemed(themed); emit q_ptr->labelBrushChanged(); diff --git a/src/piechart/qpieslice.h b/src/piechart/qpieslice.h index 4cf48a8..0b330d3 100644 --- a/src/piechart/qpieslice.h +++ b/src/piechart/qpieslice.h @@ -47,6 +47,10 @@ class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject Q_PROPERTY(qreal percentage READ percentage NOTIFY percentageChanged) Q_PROPERTY(qreal startAngle READ startAngle NOTIFY startAngleChanged) Q_PROPERTY(qreal angleSpan READ angleSpan NOTIFY angleSpanChanged) + Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) + Q_PROPERTY(QColor borderColor READ borderColor WRITE setBorderColor NOTIFY borderColorChanged) + Q_PROPERTY(int borderWidth READ borderWidth WRITE setBorderWidth NOTIFY borderWidthChanged) + Q_PROPERTY(QColor labelColor READ labelColor WRITE setLabelColor NOTIFY labelColorChanged) public: explicit QPieSlice(QObject *parent = 0); @@ -87,6 +91,15 @@ public: qreal startAngle() const; qreal angleSpan() const; + QColor color(); + void setColor(QColor color); + QColor borderColor(); + void setBorderColor(QColor color); + int borderWidth(); + void setBorderWidth(int width); + QColor labelColor(); + void setLabelColor(QColor color); + QPieSeries *series() const; Q_SIGNALS: @@ -103,6 +116,10 @@ Q_SIGNALS: void percentageChanged(); void startAngleChanged(); void angleSpanChanged(); + void colorChanged(); + void borderColorChanged(); + void borderWidthChanged(); + void labelColorChanged(); void clicked(); void hovered(bool state);