@@ -1,146 +1,146 | |||||
1 | #include "customtablemodel.h" |
|
1 | #include "customtablemodel.h" | |
2 |
|
2 | |||
3 | CustomTableModel::CustomTableModel(QObject *parent) : |
|
3 | CustomTableModel::CustomTableModel(QObject *parent) : | |
4 | QAbstractTableModel(parent) |
|
4 | QAbstractTableModel(parent) | |
5 | { |
|
5 | { | |
6 | m_points.append(QPointF(10, 50)); |
|
6 | m_points.append(QPointF(10, 50)); | |
7 | m_labels.append("Apples"); |
|
7 | m_labels.append("Apples"); | |
8 | m_points.append(QPointF(60, 70)); |
|
8 | m_points.append(QPointF(60, 70)); | |
9 | m_labels.append("Oranges"); |
|
9 | m_labels.append("Oranges"); | |
10 | m_points.append(QPointF(110, 50)); |
|
10 | m_points.append(QPointF(110, 50)); | |
11 | m_labels.append("Bananas"); |
|
11 | m_labels.append("Bananas"); | |
12 | m_points.append(QPointF(140, 40)); |
|
12 | m_points.append(QPointF(140, 40)); | |
13 | m_labels.append("Lemons"); |
|
13 | m_labels.append("Lemons"); | |
14 | m_points.append(QPointF(200, 150)); |
|
14 | m_points.append(QPointF(200, 150)); | |
15 | m_labels.append("Plums"); |
|
15 | m_labels.append("Plums"); | |
16 | m_points.append(QPointF(225, 75)); |
|
16 | m_points.append(QPointF(225, 75)); | |
17 | m_labels.append("Pearls"); |
|
17 | m_labels.append("Pearls"); | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | int CustomTableModel::rowCount(const QModelIndex & parent) const |
|
20 | int CustomTableModel::rowCount(const QModelIndex & parent) const | |
21 | { |
|
21 | { | |
22 | return m_points.count(); |
|
22 | return m_points.count(); | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | int CustomTableModel::columnCount(const QModelIndex & parent) const |
|
25 | int CustomTableModel::columnCount(const QModelIndex & parent) const | |
26 | { |
|
26 | { | |
27 | return 3; |
|
27 | return 3; | |
28 | } |
|
28 | } | |
29 |
|
29 | |||
30 | QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const |
|
30 | QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const | |
31 | { |
|
31 | { | |
32 | if (role != Qt::DisplayRole) |
|
32 | if (role != Qt::DisplayRole) | |
33 | return QVariant(); |
|
33 | return QVariant(); | |
34 |
|
34 | |||
35 | if (orientation == Qt::Horizontal) |
|
35 | if (orientation == Qt::Horizontal) | |
36 | { |
|
36 | { | |
37 | switch(section) |
|
37 | switch(section) | |
38 | { |
|
38 | { | |
39 | case 0: |
|
39 | case 0: | |
40 | return "x"; |
|
40 | return "x"; | |
41 | case 1: |
|
41 | case 1: | |
42 | return "y"; |
|
42 | return "y"; | |
43 | case 2: |
|
43 | case 2: | |
44 | return "Fruit"; |
|
44 | return "Fruit"; | |
45 | default: |
|
45 | default: | |
46 | return "What?"; |
|
46 | return "What?"; | |
47 | } |
|
47 | } | |
48 | } |
|
48 | } | |
49 | else |
|
49 | else | |
50 | return QString("%1").arg(section + 1); |
|
50 | return QString("%1").arg(section + 1); | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | QVariant CustomTableModel::data(const QModelIndex & index, int role) const |
|
53 | QVariant CustomTableModel::data(const QModelIndex & index, int role) const | |
54 | { |
|
54 | { | |
55 | if (role == Qt::DisplayRole) |
|
55 | if (role == Qt::DisplayRole) | |
56 | { |
|
56 | { | |
57 | switch(index.column()) |
|
57 | switch(index.column()) | |
58 | { |
|
58 | { | |
59 | case 0: |
|
59 | case 0: | |
60 | return m_points[index.row()].x(); |
|
60 | return m_points[index.row()].x(); | |
61 | case 1: |
|
61 | case 1: | |
62 | return m_points[index.row()].y(); |
|
62 | return m_points[index.row()].y(); | |
63 | case 2: |
|
63 | case 2: | |
64 | return m_labels[index.row()]; |
|
64 | return m_labels[index.row()]; | |
65 | default: |
|
65 | default: | |
66 | return QVariant(); |
|
66 | return QVariant(); | |
67 | } |
|
67 | } | |
68 | } |
|
68 | } | |
69 | else if (role == Qt::EditRole) |
|
69 | else if (role == Qt::EditRole) | |
70 | { |
|
70 | { | |
71 | switch(index.column()) |
|
71 | switch(index.column()) | |
72 | { |
|
72 | { | |
73 | case 0: |
|
73 | case 0: | |
74 | return m_points[index.row()].x(); |
|
74 | return m_points[index.row()].x(); | |
75 | case 1: |
|
75 | case 1: | |
76 | return m_points[index.row()].y(); |
|
76 | return m_points[index.row()].y(); | |
77 | case 2: |
|
77 | case 2: | |
78 | return m_labels[index.row()]; |
|
78 | return m_labels[index.row()]; | |
79 | default: |
|
79 | default: | |
80 | return QVariant(); |
|
80 | return QVariant(); | |
81 | } |
|
81 | } | |
82 | } |
|
82 | } | |
83 | } |
|
83 | } | |
84 |
|
84 | |||
85 | bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role) |
|
85 | bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role) | |
86 | { |
|
86 | { | |
87 | if (index.isValid() && role == Qt::EditRole) |
|
87 | if (index.isValid() && role == Qt::EditRole) | |
88 | { |
|
88 | { | |
89 | switch(index.column()) |
|
89 | switch(index.column()) | |
90 | { |
|
90 | { | |
91 | case 0: |
|
91 | case 0: | |
92 | m_points[index.row()].setX(value.toDouble()); |
|
92 | m_points[index.row()].setX(value.toDouble()); | |
93 | break; |
|
93 | break; | |
94 | case 1: |
|
94 | case 1: | |
95 | m_points[index.row()].setY(value.toDouble()); |
|
95 | m_points[index.row()].setY(value.toDouble()); | |
96 | break; |
|
96 | break; | |
97 | case 2: |
|
97 | case 2: | |
98 | m_labels.replace(index.row(), value.toString()); |
|
98 | m_labels.replace(index.row(), value.toString()); | |
99 | break; |
|
99 | break; | |
100 | default: |
|
100 | default: | |
101 | return false; |
|
101 | return false; | |
102 | } |
|
102 | } | |
103 | emit dataChanged(index, index); |
|
103 | emit dataChanged(index, index); | |
104 | return true; |
|
104 | return true; | |
105 | } |
|
105 | } | |
106 | return false; |
|
106 | return false; | |
107 | } |
|
107 | } | |
108 |
|
108 | |||
109 | Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const |
|
109 | Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const | |
110 | { |
|
110 | { | |
111 | // if (!index.isValid()) |
|
111 | // if (!index.isValid()) | |
112 | // return Qt::ItemIsEnabled; |
|
112 | // return Qt::ItemIsEnabled; | |
113 | return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; |
|
113 | return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; | |
114 | } |
|
114 | } | |
115 |
|
115 | |||
116 | bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent) |
|
116 | bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent) | |
117 | { |
|
117 | { | |
118 | if (row < 0) |
|
118 | if (row < 0) | |
119 | row = 0; |
|
119 | row = 0; | |
120 | beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1); |
|
120 | beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1); | |
121 | for (int i = row; i < row + count; i++) |
|
121 | for (int i = row; i < row + count; i++) | |
122 | { |
|
122 | { | |
123 | m_points.insert(row, QPointF()); |
|
123 | m_points.insert(row, QPointF(10,20)); | |
124 | m_labels.insert(row,("")); |
|
124 | m_labels.insert(row,("a")); | |
125 | } |
|
125 | } | |
126 | endInsertRows(); |
|
126 | endInsertRows(); | |
127 | return true; |
|
127 | return true; | |
128 | } |
|
128 | } | |
129 |
|
129 | |||
130 | bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent) |
|
130 | bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent) | |
131 | { |
|
131 | { | |
132 | if (row > this->rowCount() - 1) |
|
132 | if (row > this->rowCount() - 1) | |
133 | return false; |
|
133 | return false; | |
134 | if (row < 0) |
|
134 | if (row < 0) | |
135 | row = 0; |
|
135 | row = 0; | |
136 | if (row + count > rowCount()) |
|
136 | if (row + count > rowCount()) | |
137 | return false; |
|
137 | return false; | |
138 | beginRemoveRows(parent, row, row + count - 1); |
|
138 | beginRemoveRows(parent, row, row + count - 1); | |
139 | for (int i = row; i < row + count; i++) |
|
139 | for (int i = row; i < row + count; i++) | |
140 | { |
|
140 | { | |
141 | m_points.removeAt(row); |
|
141 | m_points.removeAt(row); | |
142 | m_labels.removeAt(row); |
|
142 | m_labels.removeAt(row); | |
143 | } |
|
143 | } | |
144 | endRemoveRows(); |
|
144 | endRemoveRows(); | |
145 | return true; |
|
145 | return true; | |
146 | } |
|
146 | } |
@@ -1,601 +1,634 | |||||
1 | #include "qpieseries.h" |
|
1 | #include "qpieseries.h" | |
2 | #include "qpieslice.h" |
|
2 | #include "qpieslice.h" | |
3 | #include <QDebug> |
|
3 | #include <QDebug> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 |
|
7 | |||
8 | /*! |
|
8 | /*! | |
9 | \class QPieSeries::ChangeSet |
|
9 | \class QPieSeries::ChangeSet | |
10 | \brief Defines the changes in the series. |
|
10 | \brief Defines the changes in the series. | |
11 |
|
11 | |||
12 | Contains the changes that have occurred in the series. Lists of added, changed and removed slices. |
|
12 | Contains the changes that have occurred in the series. Lists of added, changed and removed slices. | |
13 |
|
13 | |||
14 | \sa QPieSeries::changed() |
|
14 | \sa QPieSeries::changed() | |
15 | */ |
|
15 | */ | |
16 |
|
16 | |||
17 | /*! |
|
17 | /*! | |
18 | \internal |
|
18 | \internal | |
19 | */ |
|
19 | */ | |
20 | void QPieSeries::ChangeSet::appendAdded(QPieSlice* slice) |
|
20 | void QPieSeries::ChangeSet::appendAdded(QPieSlice* slice) | |
21 | { |
|
21 | { | |
22 | if (!m_added.contains(slice)) |
|
22 | if (!m_added.contains(slice)) | |
23 | m_added << slice; |
|
23 | m_added << slice; | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
26 | /*! |
|
26 | /*! | |
27 | \internal |
|
27 | \internal | |
28 | */ |
|
28 | */ | |
29 | void QPieSeries::ChangeSet::appendAdded(QList<QPieSlice*> slices) |
|
29 | void QPieSeries::ChangeSet::appendAdded(QList<QPieSlice*> slices) | |
30 | { |
|
30 | { | |
31 | foreach (QPieSlice* s, slices) |
|
31 | foreach (QPieSlice* s, slices) | |
32 | appendAdded(s); |
|
32 | appendAdded(s); | |
33 | } |
|
33 | } | |
34 |
|
34 | |||
35 | /*! |
|
35 | /*! | |
36 | \internal |
|
36 | \internal | |
37 | */ |
|
37 | */ | |
38 | void QPieSeries::ChangeSet::appendChanged(QPieSlice* slice) |
|
38 | void QPieSeries::ChangeSet::appendChanged(QPieSlice* slice) | |
39 | { |
|
39 | { | |
40 | if (!m_changed.contains(slice)) |
|
40 | if (!m_changed.contains(slice)) | |
41 | m_changed << slice; |
|
41 | m_changed << slice; | |
42 | } |
|
42 | } | |
43 |
|
43 | |||
44 | /*! |
|
44 | /*! | |
45 | \internal |
|
45 | \internal | |
46 | */ |
|
46 | */ | |
47 | void QPieSeries::ChangeSet::appendRemoved(QPieSlice* slice) |
|
47 | void QPieSeries::ChangeSet::appendRemoved(QPieSlice* slice) | |
48 | { |
|
48 | { | |
49 | if (!m_removed.contains(slice)) |
|
49 | if (!m_removed.contains(slice)) | |
50 | m_removed << slice; |
|
50 | m_removed << slice; | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | /*! |
|
53 | /*! | |
54 | Returns a list of slices that have been added to the series. |
|
54 | Returns a list of slices that have been added to the series. | |
55 | \sa QPieSeries::changed() |
|
55 | \sa QPieSeries::changed() | |
56 | */ |
|
56 | */ | |
57 | QList<QPieSlice*> QPieSeries::ChangeSet::added() const |
|
57 | QList<QPieSlice*> QPieSeries::ChangeSet::added() const | |
58 | { |
|
58 | { | |
59 | return m_added; |
|
59 | return m_added; | |
60 | } |
|
60 | } | |
61 |
|
61 | |||
62 | /*! |
|
62 | /*! | |
63 | Returns a list of slices that have been changed in the series. |
|
63 | Returns a list of slices that have been changed in the series. | |
64 | \sa QPieSeries::changed() |
|
64 | \sa QPieSeries::changed() | |
65 | */ |
|
65 | */ | |
66 | QList<QPieSlice*> QPieSeries::ChangeSet::changed() const |
|
66 | QList<QPieSlice*> QPieSeries::ChangeSet::changed() const | |
67 | { |
|
67 | { | |
68 | return m_changed; |
|
68 | return m_changed; | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | /*! |
|
71 | /*! | |
72 | Returns a list of slices that have been removed from the series. |
|
72 | Returns a list of slices that have been removed from the series. | |
73 | \sa QPieSeries::changed() |
|
73 | \sa QPieSeries::changed() | |
74 | */ |
|
74 | */ | |
75 | QList<QPieSlice*> QPieSeries::ChangeSet::removed() const |
|
75 | QList<QPieSlice*> QPieSeries::ChangeSet::removed() const | |
76 | { |
|
76 | { | |
77 | return m_removed; |
|
77 | return m_removed; | |
78 | } |
|
78 | } | |
79 |
|
79 | |||
80 |
|
80 | |||
81 | /*! |
|
81 | /*! | |
82 | Returns true if there are no added/changed or removed slices in the change set. |
|
82 | Returns true if there are no added/changed or removed slices in the change set. | |
83 | */ |
|
83 | */ | |
84 | bool QPieSeries::ChangeSet::isEmpty() const |
|
84 | bool QPieSeries::ChangeSet::isEmpty() const | |
85 | { |
|
85 | { | |
86 | if (m_added.count() || m_changed.count() || m_removed.count()) |
|
86 | if (m_added.count() || m_changed.count() || m_removed.count()) | |
87 | return false; |
|
87 | return false; | |
88 | return true; |
|
88 | return true; | |
89 | } |
|
89 | } | |
90 |
|
90 | |||
91 | /*! |
|
91 | /*! | |
92 | \class QPieSeries |
|
92 | \class QPieSeries | |
93 | \brief Pie series API for QtCommercial Charts |
|
93 | \brief Pie series API for QtCommercial Charts | |
94 |
|
94 | |||
95 | The pie series defines a pie chart which consists of pie slices which are QPieSlice objects. |
|
95 | The pie series defines a pie chart which consists of pie slices which are QPieSlice objects. | |
96 | The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices. |
|
96 | The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices. | |
97 | The actual slice size is determined by that relative value. |
|
97 | The actual slice size is determined by that relative value. | |
98 |
|
98 | |||
99 | By default the pie is defined as a full pie but it can be a partial pie. |
|
99 | By default the pie is defined as a full pie but it can be a partial pie. | |
100 | This can be done by setting a starting angle and angle span to the series. |
|
100 | This can be done by setting a starting angle and angle span to the series. | |
101 | */ |
|
101 | */ | |
102 |
|
102 | |||
103 | /*! |
|
103 | /*! | |
104 | Constructs a series object which is a child of \a parent. |
|
104 | Constructs a series object which is a child of \a parent. | |
105 | */ |
|
105 | */ | |
106 | QPieSeries::QPieSeries(QObject *parent) : |
|
106 | QPieSeries::QPieSeries(QObject *parent) : | |
107 | QSeries(parent), |
|
107 | QSeries(parent), | |
108 | m_pieRelativeHorPos(0.5), |
|
108 | m_pieRelativeHorPos(0.5), | |
109 | m_pieRelativeVerPos(0.5), |
|
109 | m_pieRelativeVerPos(0.5), | |
110 | m_pieRelativeSize(0.7), |
|
110 | m_pieRelativeSize(0.7), | |
111 | m_pieStartAngle(0), |
|
111 | m_pieStartAngle(0), | |
112 | m_pieEndAngle(360), |
|
112 | m_pieEndAngle(360), | |
113 | m_total(0) |
|
113 | m_total(0) | |
114 | { |
|
114 | { | |
115 |
|
115 | |||
116 | } |
|
116 | } | |
117 |
|
117 | |||
118 | /*! |
|
118 | /*! | |
119 | Destroys the object. Note that adding series to QChart transfers the ownership to the chart. |
|
119 | Destroys the object. Note that adding series to QChart transfers the ownership to the chart. | |
120 | */ |
|
120 | */ | |
121 | QPieSeries::~QPieSeries() |
|
121 | QPieSeries::~QPieSeries() | |
122 | { |
|
122 | { | |
123 |
|
123 | |||
124 | } |
|
124 | } | |
125 |
|
125 | |||
126 | /*! |
|
126 | /*! | |
127 | Returns QChartSeries::SeriesTypePie. |
|
127 | Returns QChartSeries::SeriesTypePie. | |
128 | */ |
|
128 | */ | |
129 | QSeries::QSeriesType QPieSeries::type() const |
|
129 | QSeries::QSeriesType QPieSeries::type() const | |
130 | { |
|
130 | { | |
131 | return QSeries::SeriesTypePie; |
|
131 | return QSeries::SeriesTypePie; | |
132 | } |
|
132 | } | |
133 |
|
133 | |||
134 | /*! |
|
134 | /*! | |
135 | Sets an array of \a slices to the series replacing the existing slices. |
|
135 | Sets an array of \a slices to the series replacing the existing slices. | |
136 | Slice ownership is passed to the series. |
|
136 | Slice ownership is passed to the series. | |
137 | */ |
|
137 | */ | |
138 | void QPieSeries::replace(QList<QPieSlice*> slices) |
|
138 | void QPieSeries::replace(QList<QPieSlice*> slices) | |
139 | { |
|
139 | { | |
140 | clear(); |
|
140 | clear(); | |
141 | add(slices); |
|
141 | add(slices); | |
142 | } |
|
142 | } | |
143 |
|
143 | |||
144 | /*! |
|
144 | /*! | |
145 | Adds an array of \a slices to the series. |
|
145 | Adds an array of \a slices to the series. | |
146 | Slice ownership is passed to the series. |
|
146 | Slice ownership is passed to the series. | |
147 | */ |
|
147 | */ | |
148 | void QPieSeries::add(QList<QPieSlice*> slices) |
|
148 | void QPieSeries::add(QList<QPieSlice*> slices) | |
149 | { |
|
149 | { | |
150 | foreach (QPieSlice* s, slices) { |
|
150 | foreach (QPieSlice* s, slices) { | |
151 | s->setParent(this); |
|
151 | s->setParent(this); | |
152 | m_slices << s; |
|
152 | m_slices << s; | |
153 | } |
|
153 | } | |
154 |
|
154 | |||
155 | updateDerivativeData(); |
|
155 | updateDerivativeData(); | |
156 |
|
156 | |||
157 | foreach (QPieSlice* s, slices) { |
|
157 | foreach (QPieSlice* s, slices) { | |
158 | connect(s, SIGNAL(changed()), this, SLOT(sliceChanged())); |
|
158 | connect(s, SIGNAL(changed()), this, SLOT(sliceChanged())); | |
159 | connect(s, SIGNAL(clicked()), this, SLOT(sliceClicked())); |
|
159 | connect(s, SIGNAL(clicked()), this, SLOT(sliceClicked())); | |
160 | connect(s, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter())); |
|
160 | connect(s, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter())); | |
161 | connect(s, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave())); |
|
161 | connect(s, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave())); | |
162 | } |
|
162 | } | |
163 |
|
163 | |||
164 | emit changed(); |
|
164 | emit changed(); | |
165 | } |
|
165 | } | |
166 |
|
166 | |||
167 | /*! |
|
167 | /*! | |
168 | Adds a single \a slice to the series. |
|
168 | Adds a single \a slice to the series. | |
169 | Slice ownership is passed to the series. |
|
169 | Slice ownership is passed to the series. | |
170 | */ |
|
170 | */ | |
171 | void QPieSeries::add(QPieSlice* slice) |
|
171 | void QPieSeries::add(QPieSlice* slice) | |
172 | { |
|
172 | { | |
173 | add(QList<QPieSlice*>() << slice); |
|
173 | add(QList<QPieSlice*>() << slice); | |
174 | } |
|
174 | } | |
175 |
|
175 | |||
176 | /*! |
|
176 | /*! | |
177 | Adds a single \a slice to the series and returns a reference to the series. |
|
177 | Adds a single \a slice to the series and returns a reference to the series. | |
178 | Slice ownership is passed to the series. |
|
178 | Slice ownership is passed to the series. | |
179 | */ |
|
179 | */ | |
180 | QPieSeries& QPieSeries::operator << (QPieSlice* slice) |
|
180 | QPieSeries& QPieSeries::operator << (QPieSlice* slice) | |
181 | { |
|
181 | { | |
182 | add(slice); |
|
182 | add(slice); | |
183 | return *this; |
|
183 | return *this; | |
184 | } |
|
184 | } | |
185 |
|
185 | |||
186 |
|
186 | |||
187 | /*! |
|
187 | /*! | |
188 | Adds a single slice to the series with give \a value and \a name. |
|
188 | Adds a single slice to the series with give \a value and \a name. | |
189 | Slice ownership is passed to the series. |
|
189 | Slice ownership is passed to the series. | |
190 | */ |
|
190 | */ | |
191 | QPieSlice* QPieSeries::add(qreal value, QString name) |
|
191 | QPieSlice* QPieSeries::add(qreal value, QString name) | |
192 | { |
|
192 | { | |
193 | QPieSlice* slice = new QPieSlice(value, name); |
|
193 | QPieSlice* slice = new QPieSlice(value, name); | |
194 | add(slice); |
|
194 | add(slice); | |
195 | return slice; |
|
195 | return slice; | |
196 | } |
|
196 | } | |
197 |
|
197 | |||
|
198 | void QPieSeries::insert(int i, QPieSlice* slice) | |||
|
199 | { | |||
|
200 | Q_ASSERT(i <= m_slices.count()); | |||
|
201 | slice->setParent(this); | |||
|
202 | m_slices.insert(i, slice); | |||
|
203 | ||||
|
204 | updateDerivativeData(); | |||
|
205 | ||||
|
206 | connect(slice, SIGNAL(changed()), this, SLOT(sliceChanged())); | |||
|
207 | connect(slice, SIGNAL(clicked()), this, SLOT(sliceClicked())); | |||
|
208 | connect(slice, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter())); | |||
|
209 | connect(slice, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave())); | |||
|
210 | ||||
|
211 | emit changed(); | |||
|
212 | } | |||
|
213 | ||||
198 | /*! |
|
214 | /*! | |
199 | Removes a single \a slice from the series and deletes the slice. |
|
215 | Removes a single \a slice from the series and deletes the slice. | |
200 |
|
216 | |||
201 | Do not reference this pointer after this call. |
|
217 | Do not reference this pointer after this call. | |
202 | */ |
|
218 | */ | |
203 | void QPieSeries::remove(QPieSlice* slice) |
|
219 | void QPieSeries::remove(QPieSlice* slice) | |
204 | { |
|
220 | { | |
205 | if (!m_slices.removeOne(slice)) { |
|
221 | if (!m_slices.removeOne(slice)) { | |
206 | Q_ASSERT(0); // TODO: how should this be reported? |
|
222 | Q_ASSERT(0); // TODO: how should this be reported? | |
207 | return; |
|
223 | return; | |
208 | } |
|
224 | } | |
209 | emit changed(); |
|
225 | emit changed(); | |
210 |
|
226 | |||
211 | updateDerivativeData(); |
|
227 | updateDerivativeData(); | |
212 |
|
228 | |||
213 | delete slice; |
|
229 | delete slice; | |
214 | slice = NULL; |
|
230 | slice = NULL; | |
215 | } |
|
231 | } | |
216 |
|
232 | |||
217 | /*! |
|
233 | /*! | |
218 | Clears all slices from the series. |
|
234 | Clears all slices from the series. | |
219 | */ |
|
235 | */ | |
220 | void QPieSeries::clear() |
|
236 | void QPieSeries::clear() | |
221 | { |
|
237 | { | |
222 | if (m_slices.count() == 0) |
|
238 | if (m_slices.count() == 0) | |
223 | return; |
|
239 | return; | |
224 |
|
240 | |||
225 | foreach (QPieSlice* s, m_slices) { |
|
241 | foreach (QPieSlice* s, m_slices) { | |
226 | m_slices.removeOne(s); |
|
242 | m_slices.removeOne(s); | |
227 | delete s; |
|
243 | delete s; | |
228 | } |
|
244 | } | |
229 |
|
245 | |||
230 | emit changed(); |
|
246 | emit changed(); | |
231 |
|
247 | |||
232 | updateDerivativeData(); |
|
248 | updateDerivativeData(); | |
233 | } |
|
249 | } | |
234 |
|
250 | |||
235 | /*! |
|
251 | /*! | |
236 | Counts the number of the slices in this series. |
|
252 | Counts the number of the slices in this series. | |
237 | */ |
|
253 | */ | |
238 | int QPieSeries::count() const |
|
254 | int QPieSeries::count() const | |
239 | { |
|
255 | { | |
240 | return m_slices.count(); |
|
256 | return m_slices.count(); | |
241 | } |
|
257 | } | |
242 |
|
258 | |||
243 | /*! |
|
259 | /*! | |
244 | Returns a list of slices that belong to this series. |
|
260 | Returns a list of slices that belong to this series. | |
245 | */ |
|
261 | */ | |
246 | QList<QPieSlice*> QPieSeries::slices() const |
|
262 | QList<QPieSlice*> QPieSeries::slices() const | |
247 | { |
|
263 | { | |
248 | return m_slices; |
|
264 | return m_slices; | |
249 | } |
|
265 | } | |
250 |
|
266 | |||
251 | /*! |
|
267 | /*! | |
252 | Sets the center position of the pie by \a relativeHorizontalPosition and \a relativeVerticalPosition. |
|
268 | Sets the center position of the pie by \a relativeHorizontalPosition and \a relativeVerticalPosition. | |
253 |
|
269 | |||
254 | The factors are relative to the chart rectangle where: |
|
270 | The factors are relative to the chart rectangle where: | |
255 |
|
271 | |||
256 | \a relativeHorizontalPosition 0.0 means the absolute left. |
|
272 | \a relativeHorizontalPosition 0.0 means the absolute left. | |
257 | \a relativeHorizontalPosition 1.0 means the absolute right. |
|
273 | \a relativeHorizontalPosition 1.0 means the absolute right. | |
258 | \a relativeVerticalPosition 0.0 means the absolute top. |
|
274 | \a relativeVerticalPosition 0.0 means the absolute top. | |
259 | \a relativeVerticalPosition 1.0 means the absolute bottom. |
|
275 | \a relativeVerticalPosition 1.0 means the absolute bottom. | |
260 |
|
276 | |||
261 | By default both values are 0.5 which puts the pie in the middle of the chart rectangle. |
|
277 | By default both values are 0.5 which puts the pie in the middle of the chart rectangle. | |
262 |
|
278 | |||
263 | \sa pieHorizontalPosition(), pieVerticalPosition(), setPieSize() |
|
279 | \sa pieHorizontalPosition(), pieVerticalPosition(), setPieSize() | |
264 | */ |
|
280 | */ | |
265 | void QPieSeries::setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition) |
|
281 | void QPieSeries::setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition) | |
266 | { |
|
282 | { | |
267 | if (relativeHorizontalPosition < 0.0 || relativeHorizontalPosition > 1.0 || |
|
283 | if (relativeHorizontalPosition < 0.0 || relativeHorizontalPosition > 1.0 || | |
268 | relativeVerticalPosition < 0.0 || relativeVerticalPosition > 1.0) |
|
284 | relativeVerticalPosition < 0.0 || relativeVerticalPosition > 1.0) | |
269 | return; |
|
285 | return; | |
270 |
|
286 | |||
271 | if (m_pieRelativeHorPos != relativeHorizontalPosition || m_pieRelativeVerPos != relativeVerticalPosition) { |
|
287 | if (m_pieRelativeHorPos != relativeHorizontalPosition || m_pieRelativeVerPos != relativeVerticalPosition) { | |
272 | m_pieRelativeHorPos = relativeHorizontalPosition; |
|
288 | m_pieRelativeHorPos = relativeHorizontalPosition; | |
273 | m_pieRelativeVerPos = relativeVerticalPosition; |
|
289 | m_pieRelativeVerPos = relativeVerticalPosition; | |
274 | emit changed(); |
|
290 | emit changed(); | |
275 | } |
|
291 | } | |
276 | } |
|
292 | } | |
277 |
|
293 | |||
278 | /*! |
|
294 | /*! | |
279 | Gets the horizontal position of the pie. |
|
295 | Gets the horizontal position of the pie. | |
280 |
|
296 | |||
281 | The returned value is relative to the chart rectangle where: |
|
297 | The returned value is relative to the chart rectangle where: | |
282 |
|
298 | |||
283 | 0.0 means the absolute left. |
|
299 | 0.0 means the absolute left. | |
284 | 1.0 means the absolute right. |
|
300 | 1.0 means the absolute right. | |
285 |
|
301 | |||
286 | By default it is 0.5 which puts the pie in the horizontal middle of the chart rectangle. |
|
302 | By default it is 0.5 which puts the pie in the horizontal middle of the chart rectangle. | |
287 |
|
303 | |||
288 | \sa setPiePosition(), pieVerticalPosition(), setPieSize() |
|
304 | \sa setPiePosition(), pieVerticalPosition(), setPieSize() | |
289 | */ |
|
305 | */ | |
290 | qreal QPieSeries::pieHorizontalPosition() const |
|
306 | qreal QPieSeries::pieHorizontalPosition() const | |
291 | { |
|
307 | { | |
292 | return m_pieRelativeHorPos; |
|
308 | return m_pieRelativeHorPos; | |
293 | } |
|
309 | } | |
294 |
|
310 | |||
295 | /*! |
|
311 | /*! | |
296 | Gets the vertical position position of the pie. |
|
312 | Gets the vertical position position of the pie. | |
297 |
|
313 | |||
298 | The returned value is relative to the chart rectangle where: |
|
314 | The returned value is relative to the chart rectangle where: | |
299 |
|
315 | |||
300 | 0.0 means the absolute top. |
|
316 | 0.0 means the absolute top. | |
301 | 1.0 means the absolute bottom. |
|
317 | 1.0 means the absolute bottom. | |
302 |
|
318 | |||
303 | By default it is 0.5 which puts the pie in the vertical middle of the chart rectangle. |
|
319 | By default it is 0.5 which puts the pie in the vertical middle of the chart rectangle. | |
304 |
|
320 | |||
305 | \sa setPiePosition(), pieHorizontalPosition(), setPieSize() |
|
321 | \sa setPiePosition(), pieHorizontalPosition(), setPieSize() | |
306 | */ |
|
322 | */ | |
307 | qreal QPieSeries::pieVerticalPosition() const |
|
323 | qreal QPieSeries::pieVerticalPosition() const | |
308 | { |
|
324 | { | |
309 | return m_pieRelativeVerPos; |
|
325 | return m_pieRelativeVerPos; | |
310 | } |
|
326 | } | |
311 |
|
327 | |||
312 | /*! |
|
328 | /*! | |
313 | Sets the relative size of the pie. |
|
329 | Sets the relative size of the pie. | |
314 |
|
330 | |||
315 | The \a relativeSize is defined so that the 1.0 is the maximum that can fit the given chart rectangle. |
|
331 | The \a relativeSize is defined so that the 1.0 is the maximum that can fit the given chart rectangle. | |
316 |
|
332 | |||
317 | Default value is 0.7. |
|
333 | Default value is 0.7. | |
318 |
|
334 | |||
319 | \sa pieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition() |
|
335 | \sa pieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition() | |
320 | */ |
|
336 | */ | |
321 | void QPieSeries::setPieSize(qreal relativeSize) |
|
337 | void QPieSeries::setPieSize(qreal relativeSize) | |
322 | { |
|
338 | { | |
323 | if (relativeSize < 0.0 || relativeSize > 1.0) |
|
339 | if (relativeSize < 0.0 || relativeSize > 1.0) | |
324 | return; |
|
340 | return; | |
325 |
|
341 | |||
326 | if (m_pieRelativeSize != relativeSize) { |
|
342 | if (m_pieRelativeSize != relativeSize) { | |
327 | m_pieRelativeSize = relativeSize; |
|
343 | m_pieRelativeSize = relativeSize; | |
328 | emit changed(); |
|
344 | emit changed(); | |
329 | } |
|
345 | } | |
330 | } |
|
346 | } | |
331 |
|
347 | |||
332 | /*! |
|
348 | /*! | |
333 | Gets the relative size of the pie. |
|
349 | Gets the relative size of the pie. | |
334 |
|
350 | |||
335 | The size is defined so that the 1.0 is the maximum that can fit the given chart rectangle. |
|
351 | The size is defined so that the 1.0 is the maximum that can fit the given chart rectangle. | |
336 |
|
352 | |||
337 | Default value is 0.7. |
|
353 | Default value is 0.7. | |
338 |
|
354 | |||
339 | \sa setPieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition() |
|
355 | \sa setPieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition() | |
340 | */ |
|
356 | */ | |
341 | qreal QPieSeries::pieSize() const |
|
357 | qreal QPieSeries::pieSize() const | |
342 | { |
|
358 | { | |
343 | return m_pieRelativeSize; |
|
359 | return m_pieRelativeSize; | |
344 | } |
|
360 | } | |
345 |
|
361 | |||
346 |
|
362 | |||
347 | /*! |
|
363 | /*! | |
348 | Sets the end angle of the pie. |
|
364 | Sets the end angle of the pie. | |
349 |
|
365 | |||
350 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. |
|
366 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. | |
351 |
|
367 | |||
352 | \a angle must be less than pie end angle. Default value is 0. |
|
368 | \a angle must be less than pie end angle. Default value is 0. | |
353 |
|
369 | |||
354 | \sa pieStartAngle(), pieEndAngle(), setPieEndAngle() |
|
370 | \sa pieStartAngle(), pieEndAngle(), setPieEndAngle() | |
355 | */ |
|
371 | */ | |
356 | void QPieSeries::setPieStartAngle(qreal angle) |
|
372 | void QPieSeries::setPieStartAngle(qreal angle) | |
357 | { |
|
373 | { | |
358 | if (angle >= 0 && angle <= 360 && angle != m_pieStartAngle && angle <= m_pieEndAngle) { |
|
374 | if (angle >= 0 && angle <= 360 && angle != m_pieStartAngle && angle <= m_pieEndAngle) { | |
359 | m_pieStartAngle = angle; |
|
375 | m_pieStartAngle = angle; | |
360 | updateDerivativeData(); |
|
376 | updateDerivativeData(); | |
361 | } |
|
377 | } | |
362 | } |
|
378 | } | |
363 |
|
379 | |||
364 | /*! |
|
380 | /*! | |
365 | Gets the start angle of the pie. |
|
381 | Gets the start angle of the pie. | |
366 |
|
382 | |||
367 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. Default value is 360. |
|
383 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. Default value is 360. | |
368 |
|
384 | |||
369 | \sa setPieStartAngle(), pieEndAngle(), setPieEndAngle() |
|
385 | \sa setPieStartAngle(), pieEndAngle(), setPieEndAngle() | |
370 | */ |
|
386 | */ | |
371 | qreal QPieSeries::pieStartAngle() const |
|
387 | qreal QPieSeries::pieStartAngle() const | |
372 | { |
|
388 | { | |
373 | return m_pieStartAngle; |
|
389 | return m_pieStartAngle; | |
374 | } |
|
390 | } | |
375 |
|
391 | |||
376 | /*! |
|
392 | /*! | |
377 | Sets the end angle of the pie. |
|
393 | Sets the end angle of the pie. | |
378 |
|
394 | |||
379 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. |
|
395 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. | |
380 |
|
396 | |||
381 | \a angle must be greater than start angle. |
|
397 | \a angle must be greater than start angle. | |
382 |
|
398 | |||
383 | \sa pieEndAngle(), pieStartAngle(), setPieStartAngle() |
|
399 | \sa pieEndAngle(), pieStartAngle(), setPieStartAngle() | |
384 | */ |
|
400 | */ | |
385 | void QPieSeries::setPieEndAngle(qreal angle) |
|
401 | void QPieSeries::setPieEndAngle(qreal angle) | |
386 | { |
|
402 | { | |
387 | if (angle >= 0 && angle <= 360 && angle != m_pieEndAngle && angle >= m_pieStartAngle) { |
|
403 | if (angle >= 0 && angle <= 360 && angle != m_pieEndAngle && angle >= m_pieStartAngle) { | |
388 | m_pieEndAngle = angle; |
|
404 | m_pieEndAngle = angle; | |
389 | updateDerivativeData(); |
|
405 | updateDerivativeData(); | |
390 | } |
|
406 | } | |
391 | } |
|
407 | } | |
392 |
|
408 | |||
393 | /*! |
|
409 | /*! | |
394 | Returns the end angle of the pie. |
|
410 | Returns the end angle of the pie. | |
395 |
|
411 | |||
396 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. |
|
412 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. | |
397 |
|
413 | |||
398 | \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle() |
|
414 | \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle() | |
399 | */ |
|
415 | */ | |
400 | qreal QPieSeries::pieEndAngle() const |
|
416 | qreal QPieSeries::pieEndAngle() const | |
401 | { |
|
417 | { | |
402 | return m_pieEndAngle; |
|
418 | return m_pieEndAngle; | |
403 | } |
|
419 | } | |
404 |
|
420 | |||
405 | /*! |
|
421 | /*! | |
406 | Sets the all the slice labels \a visible or invisible. |
|
422 | Sets the all the slice labels \a visible or invisible. | |
407 |
|
423 | |||
408 | \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible() |
|
424 | \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible() | |
409 | */ |
|
425 | */ | |
410 | void QPieSeries::setLabelsVisible(bool visible) |
|
426 | void QPieSeries::setLabelsVisible(bool visible) | |
411 | { |
|
427 | { | |
412 | foreach (QPieSlice* s, m_slices) |
|
428 | foreach (QPieSlice* s, m_slices) | |
413 | s->setLabelVisible(visible); |
|
429 | s->setLabelVisible(visible); | |
414 | } |
|
430 | } | |
415 |
|
431 | |||
416 | /*! |
|
432 | /*! | |
417 | Returns the sum of all slice values in this series. |
|
433 | Returns the sum of all slice values in this series. | |
418 |
|
434 | |||
419 | \sa QPieSlice::value(), QPieSlice::setValue() |
|
435 | \sa QPieSlice::value(), QPieSlice::setValue() | |
420 | */ |
|
436 | */ | |
421 | qreal QPieSeries::total() const |
|
437 | qreal QPieSeries::total() const | |
422 | { |
|
438 | { | |
423 | return m_total; |
|
439 | return m_total; | |
424 | } |
|
440 | } | |
425 |
|
441 | |||
426 | /*! |
|
442 | /*! | |
427 | \fn void QPieSeries::changed() |
|
443 | \fn void QPieSeries::changed() | |
428 |
|
444 | |||
429 | This signal emitted when something has changed in the series. |
|
445 | This signal emitted when something has changed in the series. | |
430 |
|
446 | |||
431 | \sa QPieSeries::ChangeSet, QPieSlice::changed() |
|
447 | \sa QPieSeries::ChangeSet, QPieSlice::changed() | |
432 | */ |
|
448 | */ | |
433 |
|
449 | |||
434 | /*! |
|
450 | /*! | |
435 | \fn void QPieSeries::clicked(QPieSlice* slice) |
|
451 | \fn void QPieSeries::clicked(QPieSlice* slice) | |
436 |
|
452 | |||
437 | This signal is emitted when a \a slice has been clicked. |
|
453 | This signal is emitted when a \a slice has been clicked. | |
438 |
|
454 | |||
439 | \sa QPieSlice::clicked() |
|
455 | \sa QPieSlice::clicked() | |
440 | */ |
|
456 | */ | |
441 |
|
457 | |||
442 | /*! |
|
458 | /*! | |
443 | \fn void QPieSeries::hoverEnter(QPieSlice* slice) |
|
459 | \fn void QPieSeries::hoverEnter(QPieSlice* slice) | |
444 |
|
460 | |||
445 | This signal is emitted when user has hovered over a \a slice. |
|
461 | This signal is emitted when user has hovered over a \a slice. | |
446 |
|
462 | |||
447 | \sa QPieSlice::hoverEnter() |
|
463 | \sa QPieSlice::hoverEnter() | |
448 | */ |
|
464 | */ | |
449 |
|
465 | |||
450 | /*! |
|
466 | /*! | |
451 | \fn void QPieSeries::hoverLeave(QPieSlice* slice) |
|
467 | \fn void QPieSeries::hoverLeave(QPieSlice* slice) | |
452 |
|
468 | |||
453 | This signal is emitted when user has hovered away from a \a slice. |
|
469 | This signal is emitted when user has hovered away from a \a slice. | |
454 |
|
470 | |||
455 | \sa QPieSlice::hoverLeave() |
|
471 | \sa QPieSlice::hoverLeave() | |
456 | */ |
|
472 | */ | |
457 |
|
473 | |||
458 | void QPieSeries::sliceChanged() |
|
474 | void QPieSeries::sliceChanged() | |
459 | { |
|
475 | { | |
460 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
476 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
461 | Q_ASSERT(m_slices.contains(slice)); |
|
477 | Q_ASSERT(m_slices.contains(slice)); | |
462 | updateDerivativeData(); |
|
478 | updateDerivativeData(); | |
463 | } |
|
479 | } | |
464 |
|
480 | |||
465 | void QPieSeries::sliceClicked() |
|
481 | void QPieSeries::sliceClicked() | |
466 | { |
|
482 | { | |
467 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
483 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
468 | Q_ASSERT(m_slices.contains(slice)); |
|
484 | Q_ASSERT(m_slices.contains(slice)); | |
469 | emit clicked(slice); |
|
485 | emit clicked(slice); | |
470 | } |
|
486 | } | |
471 |
|
487 | |||
472 | void QPieSeries::sliceHoverEnter() |
|
488 | void QPieSeries::sliceHoverEnter() | |
473 | { |
|
489 | { | |
474 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
490 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
475 | Q_ASSERT(m_slices.contains(slice)); |
|
491 | Q_ASSERT(m_slices.contains(slice)); | |
476 | emit hoverEnter(slice); |
|
492 | emit hoverEnter(slice); | |
477 | } |
|
493 | } | |
478 |
|
494 | |||
479 | void QPieSeries::sliceHoverLeave() |
|
495 | void QPieSeries::sliceHoverLeave() | |
480 | { |
|
496 | { | |
481 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
497 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
482 | Q_ASSERT(m_slices.contains(slice)); |
|
498 | Q_ASSERT(m_slices.contains(slice)); | |
483 | emit hoverLeave(slice); |
|
499 | emit hoverLeave(slice); | |
484 | } |
|
500 | } | |
485 |
|
501 | |||
486 | void QPieSeries::updateDerivativeData() |
|
502 | void QPieSeries::updateDerivativeData() | |
487 | { |
|
503 | { | |
488 | m_total = 0; |
|
504 | m_total = 0; | |
489 |
|
505 | |||
490 | // nothing to do? |
|
506 | // nothing to do? | |
491 | if (m_slices.count() == 0) |
|
507 | if (m_slices.count() == 0) | |
492 | return; |
|
508 | return; | |
493 |
|
509 | |||
494 | // calculate total |
|
510 | // calculate total | |
495 | foreach (QPieSlice* s, m_slices) |
|
511 | foreach (QPieSlice* s, m_slices) | |
496 | m_total += s->value(); |
|
512 | m_total += s->value(); | |
497 |
|
513 | |||
498 | // we must have some values |
|
514 | // we must have some values | |
499 | if (m_total == 0) { |
|
515 | if (m_total == 0) { | |
500 | qDebug() << "QPieSeries::updateDerivativeData() total == 0"; |
|
516 | qDebug() << "QPieSeries::updateDerivativeData() total == 0"; | |
501 | Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this? |
|
517 | Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this? | |
502 | } |
|
518 | } | |
503 |
|
519 | |||
504 | // update slice attributes |
|
520 | // update slice attributes | |
505 | qreal sliceAngle = m_pieStartAngle; |
|
521 | qreal sliceAngle = m_pieStartAngle; | |
506 | qreal pieSpan = m_pieEndAngle - m_pieStartAngle; |
|
522 | qreal pieSpan = m_pieEndAngle - m_pieStartAngle; | |
507 | foreach (QPieSlice* s, m_slices) { |
|
523 | foreach (QPieSlice* s, m_slices) { | |
508 |
|
524 | |||
509 | bool changed = false; |
|
525 | bool changed = false; | |
510 |
|
526 | |||
511 | qreal percentage = s->value() / m_total; |
|
527 | qreal percentage = s->value() / m_total; | |
512 | if (s->m_percentage != percentage) { |
|
528 | if (s->m_percentage != percentage) { | |
513 | s->m_percentage = percentage; |
|
529 | s->m_percentage = percentage; | |
514 | changed = true; |
|
530 | changed = true; | |
515 | } |
|
531 | } | |
516 |
|
532 | |||
517 | qreal sliceSpan = pieSpan * percentage; |
|
533 | qreal sliceSpan = pieSpan * percentage; | |
518 | if (s->m_angleSpan != sliceSpan) { |
|
534 | if (s->m_angleSpan != sliceSpan) { | |
519 | s->m_angleSpan = sliceSpan; |
|
535 | s->m_angleSpan = sliceSpan; | |
520 | changed = true; |
|
536 | changed = true; | |
521 | } |
|
537 | } | |
522 |
|
538 | |||
523 | if (s->m_startAngle != sliceAngle) { |
|
539 | if (s->m_startAngle != sliceAngle) { | |
524 | s->m_startAngle = sliceAngle; |
|
540 | s->m_startAngle = sliceAngle; | |
525 | changed = true; |
|
541 | changed = true; | |
526 | } |
|
542 | } | |
527 | sliceAngle += sliceSpan; |
|
543 | sliceAngle += sliceSpan; | |
528 |
|
544 | |||
529 | if (changed) |
|
545 | if (changed) | |
530 | emit s->changed(); |
|
546 | emit s->changed(); | |
531 | } |
|
547 | } | |
532 | } |
|
548 | } | |
533 |
|
549 | |||
534 | bool QPieSeries::setModel(QAbstractItemModel* model) |
|
550 | bool QPieSeries::setModel(QAbstractItemModel* model) | |
535 | { |
|
551 | { | |
536 | // disconnect signals from old model |
|
552 | // disconnect signals from old model | |
537 | if(m_model) |
|
553 | if(m_model) | |
538 | { |
|
554 | { | |
539 | disconnect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), 0, 0); |
|
555 | disconnect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), 0, 0); | |
540 | disconnect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), 0, 0); |
|
556 | disconnect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), 0, 0); | |
541 | disconnect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), 0, 0); |
|
557 | disconnect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), 0, 0); | |
542 | } |
|
558 | } | |
543 |
|
559 | |||
544 | // set new model if not NULL and connect necessary signals from it |
|
560 | // set new model if not NULL and connect necessary signals from it | |
545 | if(model) |
|
561 | if(model) | |
546 | { |
|
562 | { | |
547 | m_model = model; |
|
563 | m_model = model; | |
548 | connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); |
|
564 | connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); | |
549 | connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); |
|
565 | connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); | |
550 | connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); |
|
566 | connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); | |
551 | } |
|
567 | } | |
552 | } |
|
568 | } | |
553 |
|
569 | |||
554 | void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation) |
|
570 | void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation) | |
555 | { |
|
571 | { | |
556 | m_mapValues = modelValuesLine; |
|
572 | m_mapValues = modelValuesLine; | |
557 | m_mapLabels = modelLabelsLine; |
|
573 | m_mapLabels = modelLabelsLine; | |
558 | m_mapOrientation = orientation; |
|
574 | m_mapOrientation = orientation; | |
559 |
|
575 | |||
560 | if (m_model == NULL) |
|
576 | if (m_model == NULL) | |
561 | return; |
|
577 | return; | |
562 |
|
578 | |||
563 | if (m_mapOrientation == Qt::Vertical) |
|
579 | if (m_mapOrientation == Qt::Vertical) | |
564 | for (int i = 0; i < m_model->rowCount(); i++) |
|
580 | for (int i = 0; i < m_model->rowCount(); i++) | |
565 | add(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString()); |
|
581 | add(m_model->data(m_model->index(i, m_mapValues), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(i, m_mapLabels), Qt::DisplayRole).toString()); | |
566 | else |
|
582 | else | |
567 | for (int i = 0; i < m_model->columnCount(); i++) |
|
583 | for (int i = 0; i < m_model->columnCount(); i++) | |
568 | add(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString()); |
|
584 | add(m_model->data(m_model->index(m_mapValues, i), Qt::DisplayRole).toDouble(), m_model->data(m_model->index(m_mapLabels, i), Qt::DisplayRole).toString()); | |
569 |
|
585 | |||
570 |
|
586 | |||
571 | } |
|
587 | } | |
572 |
|
588 | |||
573 | void QPieSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) |
|
589 | void QPieSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) | |
574 | { |
|
590 | { | |
575 | if (m_mapOrientation == Qt::Vertical) |
|
591 | if (m_mapOrientation == Qt::Vertical) | |
|
592 | { | |||
576 | // slices().at(topLeft.row())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble()); |
|
593 | // slices().at(topLeft.row())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble()); | |
577 | if (topLeft.column() == m_mapValues) |
|
594 | if (topLeft.column() == m_mapValues) | |
578 | slices().at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); |
|
595 | slices().at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); | |
579 | else if (topLeft.column() == m_mapLabels) |
|
596 | else if (topLeft.column() == m_mapLabels) | |
580 | slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); |
|
597 | slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | |
581 | else |
|
598 | } | |
582 | // slices().at(topLeft.column())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble()); |
|
599 | else | |
583 | if (topLeft.column() == m_mapValues) |
|
600 | { | |
584 | slices().at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); |
|
601 | // slices().at(topLeft.column())->setValue(m_model->data(m_model->index(topLeft.row(), topLeft.column()), Qt::DisplayRole).toDouble()); | |
585 |
|
|
602 | if (topLeft.column() == m_mapValues) | |
586 |
|
|
603 | slices().at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble()); | |
|
604 | else if (topLeft.column() == m_mapLabels) | |||
|
605 | slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | |||
|
606 | } | |||
587 | } |
|
607 | } | |
588 |
|
608 | |||
589 | void QPieSeries::modelDataAdded(QModelIndex parent, int start, int end) |
|
609 | void QPieSeries::modelDataAdded(QModelIndex parent, int start, int end) | |
590 | { |
|
610 | { | |
591 | // |
|
611 | QPieSlice* newSlice = new QPieSlice; | |
|
612 | newSlice->setLabelVisible(true); | |||
|
613 | if (m_mapOrientation == Qt::Vertical) | |||
|
614 | { | |||
|
615 | newSlice->setValue(m_model->data(m_model->index(start, m_mapValues), Qt::DisplayRole).toDouble()); | |||
|
616 | newSlice->setLabel(m_model->data(m_model->index(start, m_mapLabels), Qt::DisplayRole).toString()); | |||
|
617 | } | |||
|
618 | else | |||
|
619 | { | |||
|
620 | newSlice->setValue(m_model->data(m_model->index(m_mapValues, start), Qt::DisplayRole).toDouble()); | |||
|
621 | newSlice->setLabel(m_model->data(m_model->index(m_mapLabels, start), Qt::DisplayRole).toString()); | |||
|
622 | } | |||
|
623 | ||||
|
624 | insert(start, newSlice); | |||
592 | } |
|
625 | } | |
593 |
|
626 | |||
594 | void QPieSeries::modelDataRemoved(QModelIndex parent, int start, int end) |
|
627 | void QPieSeries::modelDataRemoved(QModelIndex parent, int start, int end) | |
595 | { |
|
628 | { | |
596 | remove(slices().at(start)); |
|
629 | remove(slices().at(start)); | |
597 | } |
|
630 | } | |
598 |
|
631 | |||
599 | #include "moc_qpieseries.cpp" |
|
632 | #include "moc_qpieseries.cpp" | |
600 |
|
633 | |||
601 | QTCOMMERCIALCHART_END_NAMESPACE |
|
634 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,143 +1,144 | |||||
1 | #ifndef PIESERIES_H |
|
1 | #ifndef PIESERIES_H | |
2 | #define PIESERIES_H |
|
2 | #define PIESERIES_H | |
3 |
|
3 | |||
4 | #include "qseries.h" |
|
4 | #include "qseries.h" | |
5 | #include <QObject> |
|
5 | #include <QObject> | |
6 | #include <QRectF> |
|
6 | #include <QRectF> | |
7 | #include <QColor> |
|
7 | #include <QColor> | |
8 | #include <QPen> |
|
8 | #include <QPen> | |
9 | #include <QBrush> |
|
9 | #include <QBrush> | |
10 | #include <QSignalMapper> |
|
10 | #include <QSignalMapper> | |
11 |
|
11 | |||
12 | class QGraphicsObject; |
|
12 | class QGraphicsObject; | |
13 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
13 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
14 | class PieChartItem; |
|
14 | class PieChartItem; | |
15 | class PieSlice; |
|
15 | class PieSlice; | |
16 | class QPieSlice; |
|
16 | class QPieSlice; | |
17 |
|
17 | |||
18 | class QTCOMMERCIALCHART_EXPORT QPieSeries : public QSeries |
|
18 | class QTCOMMERCIALCHART_EXPORT QPieSeries : public QSeries | |
19 | { |
|
19 | { | |
20 | Q_OBJECT |
|
20 | Q_OBJECT | |
21 |
|
21 | |||
22 | public: |
|
22 | public: | |
23 |
|
23 | |||
24 | class ChangeSet |
|
24 | class ChangeSet | |
25 | { |
|
25 | { | |
26 | public: |
|
26 | public: | |
27 |
|
27 | |||
28 | // TODO: these should not really be exposed to the public API |
|
28 | // TODO: these should not really be exposed to the public API | |
29 | void appendAdded(QPieSlice* slice); |
|
29 | void appendAdded(QPieSlice* slice); | |
30 | void appendAdded(QList<QPieSlice*> slices); |
|
30 | void appendAdded(QList<QPieSlice*> slices); | |
31 | void appendChanged(QPieSlice* slice); |
|
31 | void appendChanged(QPieSlice* slice); | |
32 | void appendRemoved(QPieSlice* slice); |
|
32 | void appendRemoved(QPieSlice* slice); | |
33 |
|
33 | |||
34 | QList<QPieSlice*> added() const; |
|
34 | QList<QPieSlice*> added() const; | |
35 | QList<QPieSlice*> changed() const; |
|
35 | QList<QPieSlice*> changed() const; | |
36 | QList<QPieSlice*> removed() const; |
|
36 | QList<QPieSlice*> removed() const; | |
37 |
|
37 | |||
38 | bool isEmpty() const; |
|
38 | bool isEmpty() const; | |
39 |
|
39 | |||
40 | private: |
|
40 | private: | |
41 | QList<QPieSlice*> m_added; |
|
41 | QList<QPieSlice*> m_added; | |
42 | QList<QPieSlice*> m_changed; |
|
42 | QList<QPieSlice*> m_changed; | |
43 | QList<QPieSlice*> m_removed; |
|
43 | QList<QPieSlice*> m_removed; | |
44 | }; |
|
44 | }; | |
45 |
|
45 | |||
46 | public: |
|
46 | public: | |
47 | QPieSeries(QObject *parent = 0); |
|
47 | QPieSeries(QObject *parent = 0); | |
48 | virtual ~QPieSeries(); |
|
48 | virtual ~QPieSeries(); | |
49 |
|
49 | |||
50 | public: // from QChartSeries |
|
50 | public: // from QChartSeries | |
51 | QSeriesType type() const; |
|
51 | QSeriesType type() const; | |
52 |
|
52 | |||
53 | public: |
|
53 | public: | |
54 |
|
54 | |||
55 | // slice setters |
|
55 | // slice setters | |
56 | void add(QPieSlice* slice); |
|
56 | void add(QPieSlice* slice); | |
57 | void add(QList<QPieSlice*> slices); |
|
57 | void add(QList<QPieSlice*> slices); | |
|
58 | void insert(int i, QPieSlice* slice); | |||
58 | void replace(QList<QPieSlice*> slices); |
|
59 | void replace(QList<QPieSlice*> slices); | |
59 | void remove(QPieSlice* slice); |
|
60 | void remove(QPieSlice* slice); | |
60 | void clear(); |
|
61 | void clear(); | |
61 |
|
62 | |||
62 | // sluce getters |
|
63 | // sluce getters | |
63 | QList<QPieSlice*> slices() const; |
|
64 | QList<QPieSlice*> slices() const; | |
64 |
|
65 | |||
65 | // calculated data |
|
66 | // calculated data | |
66 | int count() const; |
|
67 | int count() const; | |
67 | qreal total() const; |
|
68 | qreal total() const; | |
68 |
|
69 | |||
69 | // pie customization |
|
70 | // pie customization | |
70 | void setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition); |
|
71 | void setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition); | |
71 | qreal pieHorizontalPosition() const; |
|
72 | qreal pieHorizontalPosition() const; | |
72 | qreal pieVerticalPosition() const; |
|
73 | qreal pieVerticalPosition() const; | |
73 | void setPieSize(qreal relativeSize); |
|
74 | void setPieSize(qreal relativeSize); | |
74 | qreal pieSize() const; |
|
75 | qreal pieSize() const; | |
75 | void setPieStartAngle(qreal startAngle); |
|
76 | void setPieStartAngle(qreal startAngle); | |
76 | qreal pieStartAngle() const; |
|
77 | qreal pieStartAngle() const; | |
77 | void setPieEndAngle(qreal endAngle); |
|
78 | void setPieEndAngle(qreal endAngle); | |
78 | qreal pieEndAngle() const; |
|
79 | qreal pieEndAngle() const; | |
79 |
|
80 | |||
80 | // convenience function |
|
81 | // convenience function | |
81 | QPieSeries& operator << (QPieSlice* slice); |
|
82 | QPieSeries& operator << (QPieSlice* slice); | |
82 | QPieSlice* add(qreal value, QString name); |
|
83 | QPieSlice* add(qreal value, QString name); | |
83 | void setLabelsVisible(bool visible = true); |
|
84 | void setLabelsVisible(bool visible = true); | |
84 |
|
85 | |||
85 | // data from model |
|
86 | // data from model | |
86 | bool setModel(QAbstractItemModel* model); |
|
87 | bool setModel(QAbstractItemModel* model); | |
87 | void setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation = Qt::Vertical); |
|
88 | void setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation = Qt::Vertical); | |
88 |
|
89 | |||
89 | // TODO: find slices? |
|
90 | // TODO: find slices? | |
90 | // QList<QPieSlice*> findByValue(qreal value); |
|
91 | // QList<QPieSlice*> findByValue(qreal value); | |
91 | // ... |
|
92 | // ... | |
92 |
|
93 | |||
93 | // TODO: sorting slices? |
|
94 | // TODO: sorting slices? | |
94 | // void sort(QPieSeries::SortByValue|label|??) |
|
95 | // void sort(QPieSeries::SortByValue|label|??) | |
95 |
|
96 | |||
96 | // TODO: general graphics customization |
|
97 | // TODO: general graphics customization | |
97 | // setDrawStyle(2d|3d) |
|
98 | // setDrawStyle(2d|3d) | |
98 | // setDropShadows |
|
99 | // setDropShadows | |
99 |
|
100 | |||
100 | Q_SIGNALS: |
|
101 | Q_SIGNALS: | |
101 | void clicked(QPieSlice* slice); |
|
102 | void clicked(QPieSlice* slice); | |
102 | void hoverEnter(QPieSlice* slice); |
|
103 | void hoverEnter(QPieSlice* slice); | |
103 | void hoverLeave(QPieSlice* slice); |
|
104 | void hoverLeave(QPieSlice* slice); | |
104 | void changed(); // TODO: hide this in PIMPL |
|
105 | void changed(); // TODO: hide this in PIMPL | |
105 |
|
106 | |||
106 | private Q_SLOTS: // TODO: should be private and not visible in the interface at all |
|
107 | private Q_SLOTS: // TODO: should be private and not visible in the interface at all | |
107 | void sliceChanged(); |
|
108 | void sliceChanged(); | |
108 | void sliceClicked(); |
|
109 | void sliceClicked(); | |
109 | void sliceHoverEnter(); |
|
110 | void sliceHoverEnter(); | |
110 | void sliceHoverLeave(); |
|
111 | void sliceHoverLeave(); | |
111 |
|
112 | |||
112 | // slots for updating pie when data in model changes |
|
113 | // slots for updating pie when data in model changes | |
113 | void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); |
|
114 | void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); | |
114 | void modelDataAdded(QModelIndex parent, int start, int end); |
|
115 | void modelDataAdded(QModelIndex parent, int start, int end); | |
115 | void modelDataRemoved(QModelIndex parent, int start, int end); |
|
116 | void modelDataRemoved(QModelIndex parent, int start, int end); | |
116 |
|
117 | |||
117 | private: |
|
118 | private: | |
118 | void updateDerivativeData(); |
|
119 | void updateDerivativeData(); | |
119 |
|
120 | |||
120 | private: |
|
121 | private: | |
121 | Q_DISABLE_COPY(QPieSeries) |
|
122 | Q_DISABLE_COPY(QPieSeries) | |
122 |
|
123 | |||
123 | // TODO: use PIML |
|
124 | // TODO: use PIML | |
124 | friend class PieChartItem; |
|
125 | friend class PieChartItem; | |
125 | friend class PieSlice; |
|
126 | friend class PieSlice; | |
126 |
|
127 | |||
127 | QList<QPieSlice*> m_slices; |
|
128 | QList<QPieSlice*> m_slices; | |
128 | qreal m_pieRelativeHorPos; |
|
129 | qreal m_pieRelativeHorPos; | |
129 | qreal m_pieRelativeVerPos; |
|
130 | qreal m_pieRelativeVerPos; | |
130 | qreal m_pieRelativeSize; |
|
131 | qreal m_pieRelativeSize; | |
131 | qreal m_pieStartAngle; |
|
132 | qreal m_pieStartAngle; | |
132 | qreal m_pieEndAngle; |
|
133 | qreal m_pieEndAngle; | |
133 | qreal m_total; |
|
134 | qreal m_total; | |
134 |
|
135 | |||
135 | // model map |
|
136 | // model map | |
136 | int m_mapValues; |
|
137 | int m_mapValues; | |
137 | int m_mapLabels; |
|
138 | int m_mapLabels; | |
138 | Qt::Orientation m_mapOrientation; |
|
139 | Qt::Orientation m_mapOrientation; | |
139 | }; |
|
140 | }; | |
140 |
|
141 | |||
141 | QTCOMMERCIALCHART_END_NAMESPACE |
|
142 | QTCOMMERCIALCHART_END_NAMESPACE | |
142 |
|
143 | |||
143 | #endif // PIESERIES_H |
|
144 | #endif // PIESERIES_H |
General Comments 0
You need to be logged in to leave comments.
Login now