@@ -1,169 +1,174 | |||||
1 | #include <limits.h> |
|
1 | #include <limits.h> | |
2 | #include <QVector> |
|
2 | #include <QVector> | |
3 | #include <QDebug> |
|
3 | #include <QDebug> | |
4 | #include "barchartmodel_p.h" |
|
4 | #include "barchartmodel_p.h" | |
5 | #include "qbarset.h" |
|
5 | #include "qbarset.h" | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | BarChartModel::BarChartModel(QStringList categories, QObject *parent) : |
|
9 | BarChartModel::BarChartModel(QStringList categories, QObject *parent) : | |
10 | QObject(parent) |
|
10 | QObject(parent) | |
11 | ,mCategory(categories) |
|
11 | ,mCategory(categories) | |
12 | { |
|
12 | { | |
13 | } |
|
13 | } | |
14 |
|
14 | |||
15 | QStringList BarChartModel::category() |
|
15 | QStringList BarChartModel::category() | |
16 | { |
|
16 | { | |
17 | return mCategory; |
|
17 | return mCategory; | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | void BarChartModel::addBarSet(QBarSet *set) |
|
20 | void BarChartModel::addBarSet(QBarSet *set) | |
21 | { |
|
21 | { | |
22 | mDataModel.append(set); |
|
22 | mDataModel.append(set); | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | void BarChartModel::removeBarSet(QBarSet *set) |
|
25 | void BarChartModel::removeBarSet(QBarSet *set) | |
26 | { |
|
26 | { | |
27 | if (mDataModel.contains(set)) { |
|
27 | if (mDataModel.contains(set)) { | |
28 | mDataModel.removeOne(set); |
|
28 | mDataModel.removeOne(set); | |
29 | } |
|
29 | } | |
30 | } |
|
30 | } | |
31 |
|
31 | |||
32 | void BarChartModel::insertBarSet(int i, QBarSet *set) |
|
32 | void BarChartModel::insertBarSet(int i, QBarSet *set) | |
33 | { |
|
33 | { | |
34 | mDataModel.insert(i, set); |
|
34 | mDataModel.insert(i, set); | |
35 | } |
|
35 | } | |
36 |
|
36 | |||
37 | void BarChartModel::insertCategory(int i, QString category) |
|
37 | void BarChartModel::insertCategory(int i, QString category) | |
38 | { |
|
38 | { | |
39 | mCategory.insert(i, category); |
|
39 | mCategory.insert(i, category); | |
40 | } |
|
40 | } | |
41 |
|
41 | |||
|
42 | void BarChartModel::removeCategory(int i) | |||
|
43 | { | |||
|
44 | mCategory.removeAt(i); | |||
|
45 | } | |||
|
46 | ||||
42 | QBarSet* BarChartModel::setAt(int index) |
|
47 | QBarSet* BarChartModel::setAt(int index) | |
43 | { |
|
48 | { | |
44 | return mDataModel.at(index); |
|
49 | return mDataModel.at(index); | |
45 | } |
|
50 | } | |
46 |
|
51 | |||
47 | QList<QBarSet*> BarChartModel::barSets() |
|
52 | QList<QBarSet*> BarChartModel::barSets() | |
48 | { |
|
53 | { | |
49 | return mDataModel; |
|
54 | return mDataModel; | |
50 | } |
|
55 | } | |
51 |
|
56 | |||
52 | int BarChartModel::barsetCount() |
|
57 | int BarChartModel::barsetCount() | |
53 | { |
|
58 | { | |
54 | return mDataModel.count(); |
|
59 | return mDataModel.count(); | |
55 | } |
|
60 | } | |
56 |
|
61 | |||
57 | int BarChartModel::categoryCount() |
|
62 | int BarChartModel::categoryCount() | |
58 | { |
|
63 | { | |
59 | return mCategory.count(); |
|
64 | return mCategory.count(); | |
60 | } |
|
65 | } | |
61 |
|
66 | |||
62 | qreal BarChartModel::min() |
|
67 | qreal BarChartModel::min() | |
63 | { |
|
68 | { | |
64 | Q_ASSERT(mDataModel.count() > 0); |
|
69 | Q_ASSERT(mDataModel.count() > 0); | |
65 | // TODO: make min and max members and update them when data changes. |
|
70 | // TODO: make min and max members and update them when data changes. | |
66 | // This is slower since they are checked every time, even if data is same since previous call. |
|
71 | // This is slower since they are checked every time, even if data is same since previous call. | |
67 | qreal min = INT_MAX; |
|
72 | qreal min = INT_MAX; | |
68 |
|
73 | |||
69 | for (int i=0; i <mDataModel.count(); i++) { |
|
74 | for (int i=0; i <mDataModel.count(); i++) { | |
70 | int itemCount = mDataModel.at(i)->count(); |
|
75 | int itemCount = mDataModel.at(i)->count(); | |
71 | for (int j=0; j<itemCount; j++) { |
|
76 | for (int j=0; j<itemCount; j++) { | |
72 | qreal temp = mDataModel.at(i)->valueAt(j); |
|
77 | qreal temp = mDataModel.at(i)->valueAt(j); | |
73 | if (temp < min) { |
|
78 | if (temp < min) { | |
74 | min = temp; |
|
79 | min = temp; | |
75 | } |
|
80 | } | |
76 | } |
|
81 | } | |
77 | } |
|
82 | } | |
78 | return min; |
|
83 | return min; | |
79 | } |
|
84 | } | |
80 |
|
85 | |||
81 | qreal BarChartModel::max() |
|
86 | qreal BarChartModel::max() | |
82 | { |
|
87 | { | |
83 | Q_ASSERT(mDataModel.count() > 0); |
|
88 | Q_ASSERT(mDataModel.count() > 0); | |
84 |
|
89 | |||
85 | // TODO: make min and max members and update them when data changes. |
|
90 | // TODO: make min and max members and update them when data changes. | |
86 | // This is slower since they are checked every time, even if data is same since previous call. |
|
91 | // This is slower since they are checked every time, even if data is same since previous call. | |
87 | qreal max = INT_MIN; |
|
92 | qreal max = INT_MIN; | |
88 |
|
93 | |||
89 | for (int i=0; i <mDataModel.count(); i++) { |
|
94 | for (int i=0; i <mDataModel.count(); i++) { | |
90 | int itemCount = mDataModel.at(i)->count(); |
|
95 | int itemCount = mDataModel.at(i)->count(); | |
91 | for (int j=0; j<itemCount; j++) { |
|
96 | for (int j=0; j<itemCount; j++) { | |
92 | qreal temp = mDataModel.at(i)->valueAt(j); |
|
97 | qreal temp = mDataModel.at(i)->valueAt(j); | |
93 | if (temp > max) { |
|
98 | if (temp > max) { | |
94 | max = temp; |
|
99 | max = temp; | |
95 | } |
|
100 | } | |
96 | } |
|
101 | } | |
97 | } |
|
102 | } | |
98 |
|
103 | |||
99 | return max; |
|
104 | return max; | |
100 | } |
|
105 | } | |
101 |
|
106 | |||
102 | qreal BarChartModel::valueAt(int set, int category) |
|
107 | qreal BarChartModel::valueAt(int set, int category) | |
103 | { |
|
108 | { | |
104 | if ((set < 0) || (set >= mDataModel.count())) { |
|
109 | if ((set < 0) || (set >= mDataModel.count())) { | |
105 | // No set, no value. |
|
110 | // No set, no value. | |
106 | return 0; |
|
111 | return 0; | |
107 | } else if ((category < 0) || (category >= mDataModel.at(set)->count())) { |
|
112 | } else if ((category < 0) || (category >= mDataModel.at(set)->count())) { | |
108 | // No category, no value. |
|
113 | // No category, no value. | |
109 | return 0; |
|
114 | return 0; | |
110 | } |
|
115 | } | |
111 |
|
116 | |||
112 | return mDataModel.at(set)->valueAt(category); |
|
117 | return mDataModel.at(set)->valueAt(category); | |
113 | } |
|
118 | } | |
114 |
|
119 | |||
115 | qreal BarChartModel::percentageAt(int set, int category) |
|
120 | qreal BarChartModel::percentageAt(int set, int category) | |
116 | { |
|
121 | { | |
117 | if ((set < 0) || (set >= mDataModel.count())) { |
|
122 | if ((set < 0) || (set >= mDataModel.count())) { | |
118 | // No set, no value. |
|
123 | // No set, no value. | |
119 | return 0; |
|
124 | return 0; | |
120 | } else if ((category < 0) || (category >= mDataModel.at(set)->count())) { |
|
125 | } else if ((category < 0) || (category >= mDataModel.at(set)->count())) { | |
121 | // No category, no value. |
|
126 | // No category, no value. | |
122 | return 0; |
|
127 | return 0; | |
123 | } |
|
128 | } | |
124 |
|
129 | |||
125 | qreal value = mDataModel.at(set)->valueAt(category); |
|
130 | qreal value = mDataModel.at(set)->valueAt(category); | |
126 | qreal total = categorySum(category); |
|
131 | qreal total = categorySum(category); | |
127 | if (0 == total) { |
|
132 | if (0 == total) { | |
128 | return 100.0; |
|
133 | return 100.0; | |
129 | } |
|
134 | } | |
130 |
|
135 | |||
131 | return value / total; |
|
136 | return value / total; | |
132 | } |
|
137 | } | |
133 |
|
138 | |||
134 |
|
139 | |||
135 | qreal BarChartModel::categorySum(int category) |
|
140 | qreal BarChartModel::categorySum(int category) | |
136 | { |
|
141 | { | |
137 | qreal sum(0); |
|
142 | qreal sum(0); | |
138 | int count = mDataModel.count(); // Count sets |
|
143 | int count = mDataModel.count(); // Count sets | |
139 |
|
144 | |||
140 | for (int set = 0; set < count; set++) { |
|
145 | for (int set = 0; set < count; set++) { | |
141 | if (category < mDataModel.at(set)->count()) { |
|
146 | if (category < mDataModel.at(set)->count()) { | |
142 | sum += mDataModel.at(set)->valueAt(category); |
|
147 | sum += mDataModel.at(set)->valueAt(category); | |
143 | } |
|
148 | } | |
144 | } |
|
149 | } | |
145 | return sum; |
|
150 | return sum; | |
146 | } |
|
151 | } | |
147 |
|
152 | |||
148 | qreal BarChartModel::maxCategorySum() |
|
153 | qreal BarChartModel::maxCategorySum() | |
149 | { |
|
154 | { | |
150 | qreal max = INT_MIN; |
|
155 | qreal max = INT_MIN; | |
151 | int count = categoryCount(); |
|
156 | int count = categoryCount(); | |
152 |
|
157 | |||
153 | for (int col=0; col<count; col++) { |
|
158 | for (int col=0; col<count; col++) { | |
154 | qreal sum = categorySum(col); |
|
159 | qreal sum = categorySum(col); | |
155 | if (sum > max) { |
|
160 | if (sum > max) { | |
156 | max = sum; |
|
161 | max = sum; | |
157 | } |
|
162 | } | |
158 | } |
|
163 | } | |
159 | return max; |
|
164 | return max; | |
160 | } |
|
165 | } | |
161 |
|
166 | |||
162 | QString BarChartModel::categoryName(int category) |
|
167 | QString BarChartModel::categoryName(int category) | |
163 | { |
|
168 | { | |
164 | return mCategory.at(category); |
|
169 | return mCategory.at(category); | |
165 | } |
|
170 | } | |
166 |
|
171 | |||
167 | #include "moc_barchartmodel_p.cpp" |
|
172 | #include "moc_barchartmodel_p.cpp" | |
168 |
|
173 | |||
169 | QTCOMMERCIALCHART_END_NAMESPACE |
|
174 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,59 +1,60 | |||||
1 | #ifndef BARCHARTMODEL_H |
|
1 | #ifndef BARCHARTMODEL_H | |
2 | #define BARCHARTMODEL_H |
|
2 | #define BARCHARTMODEL_H | |
3 |
|
3 | |||
4 | #include <QObject> |
|
4 | #include <QObject> | |
5 | #include <QStringList> |
|
5 | #include <QStringList> | |
6 | #include "qchartglobal.h" |
|
6 | #include "qchartglobal.h" | |
7 | #include <qseries.h> |
|
7 | #include <qseries.h> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 | // Model for bar chart. Internal class. |
|
11 | // Model for bar chart. Internal class. | |
12 | // TODO: Implement as QAbstractItemModel? |
|
12 | // TODO: Implement as QAbstractItemModel? | |
13 |
|
13 | |||
14 | class QBarSet; |
|
14 | class QBarSet; | |
15 |
|
15 | |||
16 | class BarChartModel : public QObject //, public QAbstractItemModel |
|
16 | class BarChartModel : public QObject //, public QAbstractItemModel | |
17 | { |
|
17 | { | |
18 | Q_OBJECT |
|
18 | Q_OBJECT | |
19 | public: |
|
19 | public: | |
20 | explicit BarChartModel(QStringList categories, QObject *parent = 0); |
|
20 | explicit BarChartModel(QStringList categories, QObject *parent = 0); | |
21 |
|
21 | |||
22 | QStringList category(); |
|
22 | QStringList category(); | |
23 | void addBarSet(QBarSet *set); |
|
23 | void addBarSet(QBarSet *set); | |
24 | void removeBarSet(QBarSet *set); |
|
24 | void removeBarSet(QBarSet *set); | |
25 | void insertBarSet(int i, QBarSet *set); |
|
25 | void insertBarSet(int i, QBarSet *set); | |
26 | void insertCategory(int i, QString category); |
|
26 | void insertCategory(int i, QString category); | |
|
27 | void removeCategory(int i); | |||
27 | QBarSet *setAt(int index); |
|
28 | QBarSet *setAt(int index); | |
28 | QList<QBarSet*> barSets(); |
|
29 | QList<QBarSet*> barSets(); | |
29 |
|
30 | |||
30 | int barsetCount(); // Number of sets in model |
|
31 | int barsetCount(); // Number of sets in model | |
31 | int categoryCount(); // Number of categories |
|
32 | int categoryCount(); // Number of categories | |
32 |
|
33 | |||
33 | qreal max(); // Maximum value of all sets |
|
34 | qreal max(); // Maximum value of all sets | |
34 | qreal min(); // Minimum value of all sets |
|
35 | qreal min(); // Minimum value of all sets | |
35 | qreal valueAt(int set, int category); |
|
36 | qreal valueAt(int set, int category); | |
36 | qreal percentageAt(int set, int category); |
|
37 | qreal percentageAt(int set, int category); | |
37 |
|
38 | |||
38 | qreal categorySum(int category); |
|
39 | qreal categorySum(int category); | |
39 | qreal maxCategorySum(); // returns maximum sum of sets in all categories. |
|
40 | qreal maxCategorySum(); // returns maximum sum of sets in all categories. | |
40 |
|
41 | |||
41 | QString categoryName(int category); |
|
42 | QString categoryName(int category); | |
42 |
|
43 | |||
43 | signals: |
|
44 | signals: | |
44 | void modelUpdated(); |
|
45 | void modelUpdated(); | |
45 |
|
46 | |||
46 | public slots: |
|
47 | public slots: | |
47 |
|
48 | |||
48 | private: |
|
49 | private: | |
49 |
|
50 | |||
50 | QList<QBarSet*> mDataModel; |
|
51 | QList<QBarSet*> mDataModel; | |
51 | QStringList mCategory; |
|
52 | QStringList mCategory; | |
52 |
|
53 | |||
53 | int mCurrentSet; |
|
54 | int mCurrentSet; | |
54 |
|
55 | |||
55 | }; |
|
56 | }; | |
56 |
|
57 | |||
57 | QTCOMMERCIALCHART_END_NAMESPACE |
|
58 | QTCOMMERCIALCHART_END_NAMESPACE | |
58 |
|
59 | |||
59 | #endif // BARCHARTMODEL_H |
|
60 | #endif // BARCHARTMODEL_H |
@@ -1,358 +1,368 | |||||
1 | #include <QDebug> |
|
1 | #include <QDebug> | |
2 | #include "qbarseries.h" |
|
2 | #include "qbarseries.h" | |
3 | #include "qbarset.h" |
|
3 | #include "qbarset.h" | |
4 | #include "barchartmodel_p.h" |
|
4 | #include "barchartmodel_p.h" | |
5 |
|
5 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
7 | |||
8 | /*! |
|
8 | /*! | |
9 | \class QBarSeries |
|
9 | \class QBarSeries | |
10 | \brief part of QtCommercial chart API. |
|
10 | \brief part of QtCommercial chart API. | |
11 |
|
11 | |||
12 | QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible |
|
12 | QBarSeries represents a series of data shown as bars. One QBarSeries can contain multible | |
13 | QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined |
|
13 | QBarSet data sets. QBarSeries groups the data from sets to categories, which are defined | |
14 | by QStringList. |
|
14 | by QStringList. | |
15 |
|
15 | |||
16 | \mainclass |
|
16 | \mainclass | |
17 |
|
17 | |||
18 | \sa QBarSet, QStackedBarSeries, QPercentBarSeries |
|
18 | \sa QBarSet, QStackedBarSeries, QPercentBarSeries | |
19 | */ |
|
19 | */ | |
20 |
|
20 | |||
21 | /*! |
|
21 | /*! | |
22 | \fn virtual QSeriesType QBarSeries::type() const |
|
22 | \fn virtual QSeriesType QBarSeries::type() const | |
23 | \brief Returns type of series. |
|
23 | \brief Returns type of series. | |
24 | \sa QSeries, QSeriesType |
|
24 | \sa QSeries, QSeriesType | |
25 | */ |
|
25 | */ | |
26 |
|
26 | |||
27 | /*! |
|
27 | /*! | |
28 | \fn void QBarSeries::showToolTip(QPoint pos, QString tip) |
|
28 | \fn void QBarSeries::showToolTip(QPoint pos, QString tip) | |
29 | \brief \internal \a pos \a tip |
|
29 | \brief \internal \a pos \a tip | |
30 | */ |
|
30 | */ | |
31 |
|
31 | |||
32 | /*! |
|
32 | /*! | |
33 | Constructs empty QBarSeries. Parameter \a categories defines the categories for chart. |
|
33 | Constructs empty QBarSeries. Parameter \a categories defines the categories for chart. | |
34 | QBarSeries is QObject which is a child of a \a parent. |
|
34 | QBarSeries is QObject which is a child of a \a parent. | |
35 | */ |
|
35 | */ | |
36 | QBarSeries::QBarSeries(QStringList categories, QObject *parent) |
|
36 | QBarSeries::QBarSeries(QStringList categories, QObject *parent) | |
37 | : QSeries(parent) |
|
37 | : QSeries(parent) | |
38 | ,mModel(new BarChartModel(categories, this)) |
|
38 | ,mModel(new BarChartModel(categories, this)) | |
39 | { |
|
39 | { | |
40 | m_model = NULL; |
|
40 | m_model = NULL; | |
41 | m_mapCategories = -1; |
|
41 | m_mapCategories = -1; | |
42 | m_mapBarBottom = -1; |
|
42 | m_mapBarBottom = -1; | |
43 | m_mapBarTop = -1; |
|
43 | m_mapBarTop = -1; | |
44 | m_mapOrientation = Qt::Vertical; |
|
44 | m_mapOrientation = Qt::Vertical; | |
45 | } |
|
45 | } | |
46 |
|
46 | |||
47 | /*! |
|
47 | /*! | |
48 | Adds a set of bars to series. Takes ownership of \a set. |
|
48 | Adds a set of bars to series. Takes ownership of \a set. | |
49 | Connects the clicked(QString) and rightClicked(QString) signals |
|
49 | Connects the clicked(QString) and rightClicked(QString) signals | |
50 | of \a set to this series |
|
50 | of \a set to this series | |
51 | */ |
|
51 | */ | |
52 | void QBarSeries::addBarSet(QBarSet *set) |
|
52 | void QBarSeries::addBarSet(QBarSet *set) | |
53 | { |
|
53 | { | |
54 | mModel->addBarSet(set); |
|
54 | mModel->addBarSet(set); | |
55 | connect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString))); |
|
55 | connect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString))); | |
56 | connect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString))); |
|
56 | connect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString))); | |
57 | connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged())); |
|
57 | connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged())); | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 | /*! |
|
60 | /*! | |
61 | Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set. |
|
61 | Removes a set of bars from series. Releases ownership of \a set. Doesnt delete \a set. | |
62 | Disconnects the clicked(QString) and rightClicked(QString) signals |
|
62 | Disconnects the clicked(QString) and rightClicked(QString) signals | |
63 | of \a set from this series |
|
63 | of \a set from this series | |
64 | */ |
|
64 | */ | |
65 | void QBarSeries::removeBarSet(QBarSet *set) |
|
65 | void QBarSeries::removeBarSet(QBarSet *set) | |
66 | { |
|
66 | { | |
67 | disconnect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString))); |
|
67 | disconnect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString))); | |
68 | disconnect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString))); |
|
68 | disconnect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString))); | |
69 | mModel->removeBarSet(set); |
|
69 | mModel->removeBarSet(set); | |
70 | } |
|
70 | } | |
71 |
|
71 | |||
72 | void QBarSeries::insertBarSet(int i, QBarSet *set) |
|
72 | void QBarSeries::insertBarSet(int i, QBarSet *set) | |
73 | { |
|
73 | { | |
74 | mModel->insertBarSet(i, set); |
|
74 | mModel->insertBarSet(i, set); | |
75 | // emit barsetChanged(); |
|
75 | // emit barsetChanged(); | |
76 | } |
|
76 | } | |
77 |
|
77 | |||
78 | void QBarSeries::insertCategory(int i, QString category) |
|
78 | void QBarSeries::insertCategory(int i, QString category) | |
79 | { |
|
79 | { | |
80 | mModel->insertCategory(i, category); |
|
80 | mModel->insertCategory(i, category); | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
|
83 | void QBarSeries::removeCategory(int i) | |||
|
84 | { | |||
|
85 | mModel->removeCategory(i); | |||
|
86 | } | |||
|
87 | ||||
83 | /*! |
|
88 | /*! | |
84 | Returns number of sets in series. |
|
89 | Returns number of sets in series. | |
85 | */ |
|
90 | */ | |
86 | int QBarSeries::barsetCount() |
|
91 | int QBarSeries::barsetCount() | |
87 | { |
|
92 | { | |
88 | // if(m_model) |
|
93 | // if(m_model) | |
89 | // return m_mapBarTop - m_mapBarBottom; |
|
94 | // return m_mapBarTop - m_mapBarBottom; | |
90 | // else |
|
95 | // else | |
91 | return mModel->barsetCount(); |
|
96 | return mModel->barsetCount(); | |
92 | } |
|
97 | } | |
93 |
|
98 | |||
94 | /*! |
|
99 | /*! | |
95 | Returns number of categories in series |
|
100 | Returns number of categories in series | |
96 | */ |
|
101 | */ | |
97 | int QBarSeries::categoryCount() |
|
102 | int QBarSeries::categoryCount() | |
98 | { |
|
103 | { | |
99 | return mModel->categoryCount(); |
|
104 | return mModel->categoryCount(); | |
100 | } |
|
105 | } | |
101 |
|
106 | |||
102 | /*! |
|
107 | /*! | |
103 | Returns a list of sets in series. Keeps ownership of sets. |
|
108 | Returns a list of sets in series. Keeps ownership of sets. | |
104 | */ |
|
109 | */ | |
105 | QList<QBarSet*> QBarSeries::barSets() |
|
110 | QList<QBarSet*> QBarSeries::barSets() | |
106 | { |
|
111 | { | |
107 | return mModel->barSets(); |
|
112 | return mModel->barSets(); | |
108 | } |
|
113 | } | |
109 |
|
114 | |||
110 | /*! |
|
115 | /*! | |
111 | \internal \a index |
|
116 | \internal \a index | |
112 | */ |
|
117 | */ | |
113 | QBarSet* QBarSeries::barsetAt(int index) |
|
118 | QBarSet* QBarSeries::barsetAt(int index) | |
114 | { |
|
119 | { | |
115 | return mModel->setAt(index); |
|
120 | return mModel->setAt(index); | |
116 | } |
|
121 | } | |
117 |
|
122 | |||
118 | /*! |
|
123 | /*! | |
119 | \internal \a category |
|
124 | \internal \a category | |
120 | */ |
|
125 | */ | |
121 | QString QBarSeries::categoryName(int category) |
|
126 | QString QBarSeries::categoryName(int category) | |
122 | { |
|
127 | { | |
123 | return mModel->categoryName(category); |
|
128 | return mModel->categoryName(category); | |
124 | } |
|
129 | } | |
125 |
|
130 | |||
126 | /*! |
|
131 | /*! | |
127 | Enables or disables tooltip depending on parameter \a enabled. |
|
132 | Enables or disables tooltip depending on parameter \a enabled. | |
128 | Tooltip shows the name of set, when mouse is hovering on top of bar. |
|
133 | Tooltip shows the name of set, when mouse is hovering on top of bar. | |
129 | Calling without parameter \a enabled, enables the tooltip |
|
134 | Calling without parameter \a enabled, enables the tooltip | |
130 | */ |
|
135 | */ | |
131 | void QBarSeries::setToolTipEnabled(bool enabled) |
|
136 | void QBarSeries::setToolTipEnabled(bool enabled) | |
132 | { |
|
137 | { | |
133 | // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled. |
|
138 | // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled. | |
134 | if (enabled) { |
|
139 | if (enabled) { | |
135 | for (int i=0; i<mModel->barsetCount(); i++) { |
|
140 | for (int i=0; i<mModel->barsetCount(); i++) { | |
136 | QBarSet *set = mModel->setAt(i); |
|
141 | QBarSet *set = mModel->setAt(i); | |
137 | connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); |
|
142 | connect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); | |
138 | } |
|
143 | } | |
139 | } else { |
|
144 | } else { | |
140 | for (int i=0; i<mModel->barsetCount(); i++) { |
|
145 | for (int i=0; i<mModel->barsetCount(); i++) { | |
141 | QBarSet *set = mModel->setAt(i); |
|
146 | QBarSet *set = mModel->setAt(i); | |
142 | disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); |
|
147 | disconnect(set,SIGNAL(showToolTip(QPoint,QString)),this,SIGNAL(showToolTip(QPoint,QString))); | |
143 | } |
|
148 | } | |
144 | } |
|
149 | } | |
145 | } |
|
150 | } | |
146 |
|
151 | |||
147 |
|
152 | |||
148 | /*! |
|
153 | /*! | |
149 | \internal \a category |
|
154 | \internal \a category | |
150 | */ |
|
155 | */ | |
151 | void QBarSeries::barsetClicked(QString category) |
|
156 | void QBarSeries::barsetClicked(QString category) | |
152 | { |
|
157 | { | |
153 | emit clicked(qobject_cast<QBarSet*>(sender()), category); |
|
158 | emit clicked(qobject_cast<QBarSet*>(sender()), category); | |
154 | } |
|
159 | } | |
155 |
|
160 | |||
156 | /*! |
|
161 | /*! | |
157 | \internal \a category |
|
162 | \internal \a category | |
158 | */ |
|
163 | */ | |
159 | void QBarSeries::barsetRightClicked(QString category) |
|
164 | void QBarSeries::barsetRightClicked(QString category) | |
160 | { |
|
165 | { | |
161 | emit rightClicked(qobject_cast<QBarSet*>(sender()), category); |
|
166 | emit rightClicked(qobject_cast<QBarSet*>(sender()), category); | |
162 | } |
|
167 | } | |
163 |
|
168 | |||
164 |
|
169 | |||
165 | /*! |
|
170 | /*! | |
166 | \internal |
|
171 | \internal | |
167 | */ |
|
172 | */ | |
168 | qreal QBarSeries::min() |
|
173 | qreal QBarSeries::min() | |
169 | { |
|
174 | { | |
170 | return mModel->min(); |
|
175 | return mModel->min(); | |
171 | } |
|
176 | } | |
172 |
|
177 | |||
173 | /*! |
|
178 | /*! | |
174 | \internal |
|
179 | \internal | |
175 | */ |
|
180 | */ | |
176 | qreal QBarSeries::max() |
|
181 | qreal QBarSeries::max() | |
177 | { |
|
182 | { | |
178 | return mModel->max(); |
|
183 | return mModel->max(); | |
179 | } |
|
184 | } | |
180 |
|
185 | |||
181 | /*! |
|
186 | /*! | |
182 | \internal \a set \a category |
|
187 | \internal \a set \a category | |
183 | */ |
|
188 | */ | |
184 | qreal QBarSeries::valueAt(int set, int category) |
|
189 | qreal QBarSeries::valueAt(int set, int category) | |
185 | { |
|
190 | { | |
186 | return mModel->valueAt(set,category); |
|
191 | return mModel->valueAt(set,category); | |
187 | } |
|
192 | } | |
188 |
|
193 | |||
189 | /*! |
|
194 | /*! | |
190 | \internal \a set \a category |
|
195 | \internal \a set \a category | |
191 | */ |
|
196 | */ | |
192 | qreal QBarSeries::percentageAt(int set, int category) |
|
197 | qreal QBarSeries::percentageAt(int set, int category) | |
193 | { |
|
198 | { | |
194 | return mModel->percentageAt(set,category); |
|
199 | return mModel->percentageAt(set,category); | |
195 | } |
|
200 | } | |
196 |
|
201 | |||
197 | /*! |
|
202 | /*! | |
198 | \internal \a category |
|
203 | \internal \a category | |
199 | */ |
|
204 | */ | |
200 | qreal QBarSeries::categorySum(int category) |
|
205 | qreal QBarSeries::categorySum(int category) | |
201 | { |
|
206 | { | |
202 | return mModel->categorySum(category); |
|
207 | return mModel->categorySum(category); | |
203 | } |
|
208 | } | |
204 |
|
209 | |||
205 | /*! |
|
210 | /*! | |
206 | \internal |
|
211 | \internal | |
207 | */ |
|
212 | */ | |
208 | qreal QBarSeries::maxCategorySum() |
|
213 | qreal QBarSeries::maxCategorySum() | |
209 | { |
|
214 | { | |
210 | return mModel->maxCategorySum(); |
|
215 | return mModel->maxCategorySum(); | |
211 | } |
|
216 | } | |
212 |
|
217 | |||
213 | /*! |
|
218 | /*! | |
214 | \internal |
|
219 | \internal | |
215 | */ |
|
220 | */ | |
216 | BarChartModel& QBarSeries::model() |
|
221 | BarChartModel& QBarSeries::model() | |
217 | { |
|
222 | { | |
218 | return *mModel; |
|
223 | return *mModel; | |
219 | } |
|
224 | } | |
220 |
|
225 | |||
221 | bool QBarSeries::setModel(QAbstractItemModel* model) |
|
226 | bool QBarSeries::setModel(QAbstractItemModel* model) | |
222 | { |
|
227 | { | |
223 | // disconnect signals from old model |
|
228 | // disconnect signals from old model | |
224 | if(m_model) |
|
229 | if(m_model) | |
225 | { |
|
230 | { | |
226 | disconnect(m_model, 0, this, 0); |
|
231 | disconnect(m_model, 0, this, 0); | |
227 | m_mapCategories = -1; |
|
232 | m_mapCategories = -1; | |
228 | m_mapBarBottom = -1; |
|
233 | m_mapBarBottom = -1; | |
229 | m_mapBarTop = -1; |
|
234 | m_mapBarTop = -1; | |
230 | m_mapOrientation = Qt::Vertical; |
|
235 | m_mapOrientation = Qt::Vertical; | |
231 | } |
|
236 | } | |
232 |
|
237 | |||
233 | // set new model |
|
238 | // set new model | |
234 | if(model) |
|
239 | if(model) | |
235 | { |
|
240 | { | |
236 | m_model = model; |
|
241 | m_model = model; | |
237 | return true; |
|
242 | return true; | |
238 | } |
|
243 | } | |
239 | else |
|
244 | else | |
240 | { |
|
245 | { | |
241 | m_model = NULL; |
|
246 | m_model = NULL; | |
242 | return false; |
|
247 | return false; | |
243 | } |
|
248 | } | |
244 | } |
|
249 | } | |
245 |
|
250 | |||
246 | // TODO |
|
251 | // TODO | |
247 | void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation) |
|
252 | void QBarSeries::setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation) | |
248 | { |
|
253 | { | |
249 | if (m_model == NULL) |
|
254 | if (m_model == NULL) | |
250 | return; |
|
255 | return; | |
251 | m_mapCategories = categories; |
|
256 | m_mapCategories = categories; | |
252 | m_mapBarBottom = bottomBoundry; |
|
257 | m_mapBarBottom = bottomBoundry; | |
253 | m_mapBarTop = topBoundry; |
|
258 | m_mapBarTop = topBoundry; | |
254 | m_mapOrientation = orientation; |
|
259 | m_mapOrientation = orientation; | |
255 |
|
260 | |||
256 | // connect the signals |
|
261 | // connect the signals | |
257 | if (m_mapOrientation == Qt::Vertical) |
|
262 | if (m_mapOrientation == Qt::Vertical) | |
258 | { |
|
263 | { | |
259 | connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); |
|
264 | connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); | |
260 | connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); |
|
265 | connect(m_model,SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); | |
261 | connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); |
|
266 | connect(m_model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); | |
262 | } |
|
267 | } | |
263 | else |
|
268 | else | |
264 | { |
|
269 | { | |
265 | connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); |
|
270 | connect(m_model,SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(modelUpdated(QModelIndex, QModelIndex))); | |
266 | connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); |
|
271 | connect(m_model,SIGNAL(columnsInserted(QModelIndex, int, int)), this, SLOT(modelDataAdded(QModelIndex,int,int))); | |
267 | connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); |
|
272 | connect(m_model, SIGNAL(columnsRemoved(QModelIndex, int, int)), this, SLOT(modelDataRemoved(QModelIndex,int,int))); | |
268 | } |
|
273 | } | |
269 |
|
274 | |||
270 |
|
275 | |||
271 | // create the initial bars |
|
276 | // create the initial bars | |
272 | delete mModel; |
|
277 | delete mModel; | |
273 | if (m_mapOrientation == Qt::Vertical) |
|
278 | if (m_mapOrientation == Qt::Vertical) | |
274 | { |
|
279 | { | |
275 | QStringList categories; |
|
280 | QStringList categories; | |
276 | for (int k = 0; k < m_model->rowCount(); k++) |
|
281 | for (int k = 0; k < m_model->rowCount(); k++) | |
277 | categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString(); |
|
282 | categories << m_model->data(m_model->index(k, m_mapCategories), Qt::DisplayRole).toString(); | |
278 | mModel = new BarChartModel(categories, this); |
|
283 | mModel = new BarChartModel(categories, this); | |
279 |
|
284 | |||
280 | for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) |
|
285 | for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) | |
281 | { |
|
286 | { | |
282 | QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1)); |
|
287 | QBarSet* barSet = new QBarSet(QString("Column: %1").arg(i + 1)); | |
283 | for(int m = 0; m < m_model->rowCount(); m++) |
|
288 | for(int m = 0; m < m_model->rowCount(); m++) | |
284 | *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble(); |
|
289 | *barSet << m_model->data(m_model->index(m, i), Qt::DisplayRole).toDouble(); | |
285 | addBarSet(barSet); |
|
290 | addBarSet(barSet); | |
286 | } |
|
291 | } | |
287 | } |
|
292 | } | |
288 | else |
|
293 | else | |
289 | { |
|
294 | { | |
290 | QStringList categories; |
|
295 | QStringList categories; | |
291 | for (int k = 0; k < m_model->columnCount(); k++) |
|
296 | for (int k = 0; k < m_model->columnCount(); k++) | |
292 | categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString(); |
|
297 | categories << m_model->data(m_model->index(m_mapCategories, k), Qt::DisplayRole).toString(); | |
293 | mModel = new BarChartModel(categories, this); |
|
298 | mModel = new BarChartModel(categories, this); | |
294 |
|
299 | |||
295 | for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) |
|
300 | for (int i = m_mapBarBottom; i <= m_mapBarTop; i++) | |
296 | { |
|
301 | { | |
297 | QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1)); |
|
302 | QBarSet* barSet = new QBarSet(QString("Row: %1").arg(i + 1)); | |
298 | for(int m = 0; m < m_model->columnCount(); m++) |
|
303 | for(int m = 0; m < m_model->columnCount(); m++) | |
299 | *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble(); |
|
304 | *barSet << m_model->data(m_model->index(i, m), Qt::DisplayRole).toDouble(); | |
300 | addBarSet(barSet); |
|
305 | addBarSet(barSet); | |
301 | } |
|
306 | } | |
302 | } |
|
307 | } | |
303 | } |
|
308 | } | |
304 |
|
309 | |||
305 | void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) |
|
310 | void QBarSeries::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight) | |
306 | { |
|
311 | { | |
307 | Q_UNUSED(bottomRight) |
|
312 | Q_UNUSED(bottomRight) | |
308 |
|
313 | |||
309 | if (m_mapOrientation == Qt::Vertical) |
|
314 | if (m_mapOrientation == Qt::Vertical) | |
310 | { |
|
315 | { | |
311 | if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop) |
|
316 | if (topLeft.column() >= m_mapBarBottom && topLeft.column() <= m_mapBarTop) | |
312 | barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); |
|
317 | barsetAt(topLeft.column() - m_mapBarBottom)->setValue(topLeft.row(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); | |
313 | // else if (topLeft.column() == m_mapCategories) |
|
318 | // else if (topLeft.column() == m_mapCategories) | |
314 | // slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); |
|
319 | // slices().at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | |
315 | } |
|
320 | } | |
316 | else |
|
321 | else | |
317 | { |
|
322 | { | |
318 | if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop) |
|
323 | if (topLeft.row() >= m_mapBarBottom && topLeft.row() <= m_mapBarTop) | |
319 | barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); |
|
324 | barsetAt(topLeft.row() - m_mapBarBottom)->setValue(topLeft.column(), m_model->data(topLeft, Qt::DisplayRole).toDouble()); | |
320 | // else if (topLeft.row() == m_mapCategories) |
|
325 | // else if (topLeft.row() == m_mapCategories) | |
321 | // slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); |
|
326 | // slices().at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString()); | |
322 | } |
|
327 | } | |
323 | } |
|
328 | } | |
324 |
|
329 | |||
325 | void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/) |
|
330 | void QBarSeries::modelDataAdded(QModelIndex /*parent*/, int start, int /*end*/) | |
326 | { |
|
331 | { | |
327 | if (m_mapOrientation == Qt::Vertical) |
|
332 | if (m_mapOrientation == Qt::Vertical) | |
328 | { |
|
333 | { | |
329 | insertCategory(start, QString("Row: %1").arg(start + 1)); |
|
334 | insertCategory(start, QString("Row: %1").arg(start + 1)); | |
330 | for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) |
|
335 | for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) | |
331 | { |
|
336 | { | |
332 | barsetAt(i)->insertValue(start, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble()); |
|
337 | barsetAt(i)->insertValue(start, m_model->data(m_model->index(start, i), Qt::DisplayRole).toDouble()); | |
333 | } |
|
338 | } | |
334 | } |
|
339 | } | |
335 | else |
|
340 | else | |
336 | { |
|
341 | { | |
337 | insertCategory(start, QString("Column: %1").arg(start + 1)); |
|
342 | insertCategory(start, QString("Column: %1").arg(start + 1)); | |
338 | for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) |
|
343 | for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) | |
339 | { |
|
344 | { | |
340 | barsetAt(i)->insertValue(start, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble()); |
|
345 | barsetAt(i)->insertValue(start, m_model->data(m_model->index(i, start), Qt::DisplayRole).toDouble()); | |
341 | } |
|
346 | } | |
342 | } |
|
347 | } | |
343 | emit restructuredBar(1); |
|
348 | emit restructuredBar(1); | |
344 | } |
|
349 | } | |
345 |
|
350 | |||
346 |
void QBarSeries::modelDataRemoved(QModelIndex /*parent*/, int |
|
351 | void QBarSeries::modelDataRemoved(QModelIndex /*parent*/, int start, int /*end*/) | |
347 | { |
|
352 | { | |
348 | // |
|
353 | removeCategory(start); | |
|
354 | for (int i = 0; i <= m_mapBarTop - m_mapBarBottom; i++) | |||
|
355 | { | |||
|
356 | barsetAt(i)->removeValue(start); | |||
|
357 | } | |||
|
358 | emit restructuredBar(1); | |||
349 | } |
|
359 | } | |
350 |
|
360 | |||
351 | void QBarSeries::barsetChanged() |
|
361 | void QBarSeries::barsetChanged() | |
352 | { |
|
362 | { | |
353 | emit updatedBars(); |
|
363 | emit updatedBars(); | |
354 | } |
|
364 | } | |
355 |
|
365 | |||
356 | #include "moc_qbarseries.cpp" |
|
366 | #include "moc_qbarseries.cpp" | |
357 |
|
367 | |||
358 | QTCOMMERCIALCHART_END_NAMESPACE |
|
368 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,90 +1,91 | |||||
1 | #ifndef BARSERIES_H |
|
1 | #ifndef BARSERIES_H | |
2 | #define BARSERIES_H |
|
2 | #define BARSERIES_H | |
3 |
|
3 | |||
4 | #include "qseries.h" |
|
4 | #include "qseries.h" | |
5 | #include <QStringList> |
|
5 | #include <QStringList> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | class QBarSet; |
|
9 | class QBarSet; | |
10 | class BarChartModel; |
|
10 | class BarChartModel; | |
11 | class BarCategory; |
|
11 | class BarCategory; | |
12 |
|
12 | |||
13 | // Container for series |
|
13 | // Container for series | |
14 | class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries |
|
14 | class QTCOMMERCIALCHART_EXPORT QBarSeries : public QSeries | |
15 | { |
|
15 | { | |
16 | Q_OBJECT |
|
16 | Q_OBJECT | |
17 | public: |
|
17 | public: | |
18 | QBarSeries(QStringList categories, QObject* parent=0); |
|
18 | QBarSeries(QStringList categories, QObject* parent=0); | |
19 |
|
19 | |||
20 | virtual QSeriesType type() const { return QSeries::SeriesTypeBar; } |
|
20 | virtual QSeriesType type() const { return QSeries::SeriesTypeBar; } | |
21 |
|
21 | |||
22 | void addBarSet(QBarSet *set); // Takes ownership of set |
|
22 | void addBarSet(QBarSet *set); // Takes ownership of set | |
23 | void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set |
|
23 | void removeBarSet(QBarSet *set); // Releases ownership, doesn't delete set | |
24 | void insertBarSet(int i, QBarSet *set); |
|
24 | void insertBarSet(int i, QBarSet *set); | |
25 | void insertCategory(int i, QString category); |
|
25 | void insertCategory(int i, QString category); | |
|
26 | void removeCategory(int i); | |||
26 | int barsetCount(); |
|
27 | int barsetCount(); | |
27 | int categoryCount(); |
|
28 | int categoryCount(); | |
28 | QList<QBarSet*> barSets(); |
|
29 | QList<QBarSet*> barSets(); | |
29 |
|
30 | |||
30 | bool setModel(QAbstractItemModel* model); |
|
31 | bool setModel(QAbstractItemModel* model); | |
31 | QAbstractItemModel* modelExt() {return m_model;} |
|
32 | QAbstractItemModel* modelExt() {return m_model;} | |
32 | void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical); |
|
33 | void setModelMapping(int categories, int bottomBoundry, int topBoundry, Qt::Orientation orientation = Qt::Vertical); | |
33 |
|
34 | |||
34 | public: |
|
35 | public: | |
35 | // TODO: Functions below this are not part of api and will be moved |
|
36 | // TODO: Functions below this are not part of api and will be moved | |
36 | // to private implementation, when we start using it |
|
37 | // to private implementation, when we start using it | |
37 | // TODO: TO PIMPL ---> |
|
38 | // TODO: TO PIMPL ---> | |
38 | QBarSet* barsetAt(int index); |
|
39 | QBarSet* barsetAt(int index); | |
39 | QString categoryName(int category); |
|
40 | QString categoryName(int category); | |
40 | qreal min(); |
|
41 | qreal min(); | |
41 | qreal max(); |
|
42 | qreal max(); | |
42 | qreal valueAt(int set, int category); |
|
43 | qreal valueAt(int set, int category); | |
43 | qreal percentageAt(int set, int category); |
|
44 | qreal percentageAt(int set, int category); | |
44 | qreal categorySum(int category); |
|
45 | qreal categorySum(int category); | |
45 | qreal maxCategorySum(); |
|
46 | qreal maxCategorySum(); | |
46 | BarChartModel& model(); |
|
47 | BarChartModel& model(); | |
47 | // <--- TO PIMPL |
|
48 | // <--- TO PIMPL | |
48 |
|
49 | |||
49 | signals: |
|
50 | signals: | |
50 | //void changed(int index); |
|
51 | //void changed(int index); | |
51 | void clicked(QBarSet* barset, QString category); // Up to user of api, what to do with these signals |
|
52 | void clicked(QBarSet* barset, QString category); // Up to user of api, what to do with these signals | |
52 | void rightClicked(QBarSet* barset, QString category); |
|
53 | void rightClicked(QBarSet* barset, QString category); | |
53 |
|
54 | |||
54 | // |
|
55 | // | |
55 | void updatedBars(); |
|
56 | void updatedBars(); | |
56 | void restructuredBar(int); |
|
57 | void restructuredBar(int); | |
57 |
|
58 | |||
58 | // TODO: internal signals, these to private implementation. |
|
59 | // TODO: internal signals, these to private implementation. | |
59 | // TODO: TO PIMPL ---> |
|
60 | // TODO: TO PIMPL ---> | |
60 | void showToolTip(QPoint pos, QString tip); |
|
61 | void showToolTip(QPoint pos, QString tip); | |
61 | // <--- TO PIMPL |
|
62 | // <--- TO PIMPL | |
62 |
|
63 | |||
63 | public Q_SLOTS: |
|
64 | public Q_SLOTS: | |
64 | void setToolTipEnabled(bool enabled=true); // enables tooltips |
|
65 | void setToolTipEnabled(bool enabled=true); // enables tooltips | |
65 |
|
66 | |||
66 | // TODO: TO PIMPL ---> |
|
67 | // TODO: TO PIMPL ---> | |
67 | void barsetClicked(QString category); |
|
68 | void barsetClicked(QString category); | |
68 | void barsetRightClicked(QString category); |
|
69 | void barsetRightClicked(QString category); | |
69 | // <--- TO PIMPL |
|
70 | // <--- TO PIMPL | |
70 |
|
71 | |||
71 | private Q_SLOTS: |
|
72 | private Q_SLOTS: | |
72 | // slots for updating bars when data in model changes |
|
73 | // slots for updating bars when data in model changes | |
73 | void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); |
|
74 | void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight); | |
74 | void modelDataAdded(QModelIndex parent, int start, int end); |
|
75 | void modelDataAdded(QModelIndex parent, int start, int end); | |
75 | void modelDataRemoved(QModelIndex parent, int start, int end); |
|
76 | void modelDataRemoved(QModelIndex parent, int start, int end); | |
76 | void barsetChanged(); |
|
77 | void barsetChanged(); | |
77 |
|
78 | |||
78 | protected: |
|
79 | protected: | |
79 | BarChartModel* mModel; |
|
80 | BarChartModel* mModel; | |
80 |
|
81 | |||
81 | // QAbstractItemModel* m_model; |
|
82 | // QAbstractItemModel* m_model; | |
82 | int m_mapCategories; |
|
83 | int m_mapCategories; | |
83 | int m_mapBarBottom; |
|
84 | int m_mapBarBottom; | |
84 | int m_mapBarTop; |
|
85 | int m_mapBarTop; | |
85 | Qt::Orientation m_mapOrientation; |
|
86 | Qt::Orientation m_mapOrientation; | |
86 | }; |
|
87 | }; | |
87 |
|
88 | |||
88 | QTCOMMERCIALCHART_END_NAMESPACE |
|
89 | QTCOMMERCIALCHART_END_NAMESPACE | |
89 |
|
90 | |||
90 | #endif // BARSERIES_H |
|
91 | #endif // BARSERIES_H |
@@ -1,200 +1,205 | |||||
1 | #include "qbarset.h" |
|
1 | #include "qbarset.h" | |
2 | #include <QDebug> |
|
2 | #include <QDebug> | |
3 | #include <QToolTip> |
|
3 | #include <QToolTip> | |
4 |
|
4 | |||
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
6 |
|
6 | |||
7 | /*! |
|
7 | /*! | |
8 | \class QBarSet |
|
8 | \class QBarSet | |
9 | \brief part of QtCommercial chart API. |
|
9 | \brief part of QtCommercial chart API. | |
10 |
|
10 | |||
11 | QBarSet represents one set of bars. Set of bars contains one data value for each category. |
|
11 | QBarSet represents one set of bars. Set of bars contains one data value for each category. | |
12 | First value of set is assumed to belong to first category, second to second category and so on. |
|
12 | First value of set is assumed to belong to first category, second to second category and so on. | |
13 | If set has fewer values than there are categories, then the missing values are assumed to be |
|
13 | If set has fewer values than there are categories, then the missing values are assumed to be | |
14 | at the end of set. For missing values in middle of a set, numerical value of zero is used. |
|
14 | at the end of set. For missing values in middle of a set, numerical value of zero is used. | |
15 |
|
15 | |||
16 | \mainclass |
|
16 | \mainclass | |
17 |
|
17 | |||
18 | \sa QBarSeries, QStackedBarSeries, QPercentBarSeries |
|
18 | \sa QBarSeries, QStackedBarSeries, QPercentBarSeries | |
19 | */ |
|
19 | */ | |
20 |
|
20 | |||
21 | /*! |
|
21 | /*! | |
22 | \fn void QBarSet::clicked(QString category) |
|
22 | \fn void QBarSet::clicked(QString category) | |
23 | \brief signals that set has been clicked |
|
23 | \brief signals that set has been clicked | |
24 | Parameter \a category describes on which category was clicked |
|
24 | Parameter \a category describes on which category was clicked | |
25 | */ |
|
25 | */ | |
26 |
|
26 | |||
27 | /*! |
|
27 | /*! | |
28 | \fn void QBarSet::rightClicked(QString category) |
|
28 | \fn void QBarSet::rightClicked(QString category) | |
29 | \brief signals that set has been clicked with right mouse button |
|
29 | \brief signals that set has been clicked with right mouse button | |
30 | Parameter \a category describes on which category was clicked |
|
30 | Parameter \a category describes on which category was clicked | |
31 | */ |
|
31 | */ | |
32 |
|
32 | |||
33 | /*! |
|
33 | /*! | |
34 | \fn void QBarSet::hoverEnter(QPoint pos) |
|
34 | \fn void QBarSet::hoverEnter(QPoint pos) | |
35 | \brief signals that mouse has entered over the set at position \a pos. |
|
35 | \brief signals that mouse has entered over the set at position \a pos. | |
36 | */ |
|
36 | */ | |
37 |
|
37 | |||
38 | /*! |
|
38 | /*! | |
39 | \fn void QBarSet::hoverLeave() |
|
39 | \fn void QBarSet::hoverLeave() | |
40 | \brief signals that mouse has left from the set. |
|
40 | \brief signals that mouse has left from the set. | |
41 | */ |
|
41 | */ | |
42 |
|
42 | |||
43 | /*! |
|
43 | /*! | |
44 | \fn void QBarSet::toggleFloatingValues() |
|
44 | \fn void QBarSet::toggleFloatingValues() | |
45 | \brief \internal |
|
45 | \brief \internal | |
46 | */ |
|
46 | */ | |
47 |
|
47 | |||
48 | /*! |
|
48 | /*! | |
49 | \fn void QBarSet::showToolTip(QPoint pos, QString tip) |
|
49 | \fn void QBarSet::showToolTip(QPoint pos, QString tip) | |
50 | \brief \internal \a pos \a tip |
|
50 | \brief \internal \a pos \a tip | |
51 | */ |
|
51 | */ | |
52 |
|
52 | |||
53 |
|
53 | |||
54 | /*! |
|
54 | /*! | |
55 | Constructs QBarSet with a name of \a name and with parent of \a parent |
|
55 | Constructs QBarSet with a name of \a name and with parent of \a parent | |
56 | */ |
|
56 | */ | |
57 | QBarSet::QBarSet(QString name, QObject *parent) |
|
57 | QBarSet::QBarSet(QString name, QObject *parent) | |
58 | : QObject(parent) |
|
58 | : QObject(parent) | |
59 | ,mName(name) |
|
59 | ,mName(name) | |
60 | { |
|
60 | { | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | /*! |
|
63 | /*! | |
64 | Sets new \a name for set. |
|
64 | Sets new \a name for set. | |
65 | */ |
|
65 | */ | |
66 | void QBarSet::setName(QString name) |
|
66 | void QBarSet::setName(QString name) | |
67 | { |
|
67 | { | |
68 | mName = name; |
|
68 | mName = name; | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | /*! |
|
71 | /*! | |
72 | Returns name of the set. |
|
72 | Returns name of the set. | |
73 | */ |
|
73 | */ | |
74 | QString QBarSet::name() |
|
74 | QString QBarSet::name() | |
75 | { |
|
75 | { | |
76 | return mName; |
|
76 | return mName; | |
77 | } |
|
77 | } | |
78 |
|
78 | |||
79 | /*! |
|
79 | /*! | |
80 | Appends new value \a value to the end of set. |
|
80 | Appends new value \a value to the end of set. | |
81 | */ |
|
81 | */ | |
82 | QBarSet& QBarSet::operator << (const qreal &value) |
|
82 | QBarSet& QBarSet::operator << (const qreal &value) | |
83 | { |
|
83 | { | |
84 | mValues.append(value); |
|
84 | mValues.append(value); | |
85 | emit structureChanged(); |
|
85 | emit structureChanged(); | |
86 | return *this; |
|
86 | return *this; | |
87 | } |
|
87 | } | |
88 |
|
88 | |||
89 | void QBarSet::insertValue(int i, qreal value) |
|
89 | void QBarSet::insertValue(int i, qreal value) | |
90 | { |
|
90 | { | |
91 | mValues.insert(i, value); |
|
91 | mValues.insert(i, value); | |
92 | } |
|
92 | } | |
93 |
|
93 | |||
|
94 | void QBarSet::removeValue(int i) | |||
|
95 | { | |||
|
96 | mValues.removeAt(i); | |||
|
97 | } | |||
|
98 | ||||
94 | /*! |
|
99 | /*! | |
95 | Returns count of values in set. |
|
100 | Returns count of values in set. | |
96 | */ |
|
101 | */ | |
97 | int QBarSet::count() |
|
102 | int QBarSet::count() | |
98 | { |
|
103 | { | |
99 | return mValues.count(); |
|
104 | return mValues.count(); | |
100 | } |
|
105 | } | |
101 |
|
106 | |||
102 | /*! |
|
107 | /*! | |
103 | Returns value of set indexed by \a index |
|
108 | Returns value of set indexed by \a index | |
104 | */ |
|
109 | */ | |
105 | qreal QBarSet::valueAt(int index) |
|
110 | qreal QBarSet::valueAt(int index) | |
106 | { |
|
111 | { | |
107 | return mValues.at(index); |
|
112 | return mValues.at(index); | |
108 | } |
|
113 | } | |
109 |
|
114 | |||
110 | /*! |
|
115 | /*! | |
111 | Sets a new value \a value to set, indexed by \a index |
|
116 | Sets a new value \a value to set, indexed by \a index | |
112 | */ |
|
117 | */ | |
113 | void QBarSet::setValue(int index, qreal value) |
|
118 | void QBarSet::setValue(int index, qreal value) | |
114 | { |
|
119 | { | |
115 | mValues.replace(index,value); |
|
120 | mValues.replace(index,value); | |
116 | emit valueChanged(); |
|
121 | emit valueChanged(); | |
117 | } |
|
122 | } | |
118 |
|
123 | |||
119 | /*! |
|
124 | /*! | |
120 | Returns total sum of all values in barset. |
|
125 | Returns total sum of all values in barset. | |
121 | */ |
|
126 | */ | |
122 | qreal QBarSet::total() |
|
127 | qreal QBarSet::total() | |
123 | { |
|
128 | { | |
124 | qreal total(0); |
|
129 | qreal total(0); | |
125 | for (int i=0; i<mValues.count(); i++) { |
|
130 | for (int i=0; i<mValues.count(); i++) { | |
126 | total += mValues.at(i); |
|
131 | total += mValues.at(i); | |
127 | } |
|
132 | } | |
128 | return total; |
|
133 | return total; | |
129 | } |
|
134 | } | |
130 |
|
135 | |||
131 | /*! |
|
136 | /*! | |
132 | Sets pen for set. Bars of this set are drawn using \a pen |
|
137 | Sets pen for set. Bars of this set are drawn using \a pen | |
133 | */ |
|
138 | */ | |
134 | void QBarSet::setPen(const QPen pen) |
|
139 | void QBarSet::setPen(const QPen pen) | |
135 | { |
|
140 | { | |
136 | mPen = pen; |
|
141 | mPen = pen; | |
137 | emit valueChanged(); |
|
142 | emit valueChanged(); | |
138 | } |
|
143 | } | |
139 |
|
144 | |||
140 | /*! |
|
145 | /*! | |
141 | Returns pen of the set. |
|
146 | Returns pen of the set. | |
142 | */ |
|
147 | */ | |
143 | QPen QBarSet::pen() const |
|
148 | QPen QBarSet::pen() const | |
144 | { |
|
149 | { | |
145 | return mPen; |
|
150 | return mPen; | |
146 | } |
|
151 | } | |
147 |
|
152 | |||
148 | /*! |
|
153 | /*! | |
149 | Sets brush for the set. Bars of this set are drawn using \a brush |
|
154 | Sets brush for the set. Bars of this set are drawn using \a brush | |
150 | */ |
|
155 | */ | |
151 | void QBarSet::setBrush(const QBrush brush) |
|
156 | void QBarSet::setBrush(const QBrush brush) | |
152 | { |
|
157 | { | |
153 | mBrush = brush; |
|
158 | mBrush = brush; | |
154 | emit valueChanged(); |
|
159 | emit valueChanged(); | |
155 | } |
|
160 | } | |
156 |
|
161 | |||
157 | /*! |
|
162 | /*! | |
158 | Returns brush of the set. |
|
163 | Returns brush of the set. | |
159 | */ |
|
164 | */ | |
160 | QBrush QBarSet::brush() const |
|
165 | QBrush QBarSet::brush() const | |
161 | { |
|
166 | { | |
162 | return mBrush; |
|
167 | return mBrush; | |
163 | } |
|
168 | } | |
164 |
|
169 | |||
165 | /*! |
|
170 | /*! | |
166 | Sets the pen for floating values that are drawn on top of this set |
|
171 | Sets the pen for floating values that are drawn on top of this set | |
167 | */ |
|
172 | */ | |
168 | void QBarSet::setFloatingValuePen(const QPen pen) |
|
173 | void QBarSet::setFloatingValuePen(const QPen pen) | |
169 | { |
|
174 | { | |
170 | mFloatingValuePen = pen; |
|
175 | mFloatingValuePen = pen; | |
171 | } |
|
176 | } | |
172 |
|
177 | |||
173 | /*! |
|
178 | /*! | |
174 | Returns the pen for floating values that are drawn on top of this set |
|
179 | Returns the pen for floating values that are drawn on top of this set | |
175 | */ |
|
180 | */ | |
176 | QPen QBarSet::floatingValuePen() const |
|
181 | QPen QBarSet::floatingValuePen() const | |
177 | { |
|
182 | { | |
178 | return mFloatingValuePen; |
|
183 | return mFloatingValuePen; | |
179 | } |
|
184 | } | |
180 |
|
185 | |||
181 | /*! |
|
186 | /*! | |
182 | \internal \a pos |
|
187 | \internal \a pos | |
183 | */ |
|
188 | */ | |
184 | void QBarSet::barHoverEnterEvent(QPoint pos) |
|
189 | void QBarSet::barHoverEnterEvent(QPoint pos) | |
185 | { |
|
190 | { | |
186 | emit showToolTip(pos, mName); |
|
191 | emit showToolTip(pos, mName); | |
187 | emit hoverEnter(pos); |
|
192 | emit hoverEnter(pos); | |
188 | } |
|
193 | } | |
189 |
|
194 | |||
190 | /*! |
|
195 | /*! | |
191 | \internal |
|
196 | \internal | |
192 | */ |
|
197 | */ | |
193 | void QBarSet::barHoverLeaveEvent() |
|
198 | void QBarSet::barHoverLeaveEvent() | |
194 | { |
|
199 | { | |
195 | // Emit signal to user of charts |
|
200 | // Emit signal to user of charts | |
196 | emit hoverLeave(); |
|
201 | emit hoverLeave(); | |
197 | } |
|
202 | } | |
198 |
|
203 | |||
199 | #include "moc_qbarset.cpp" |
|
204 | #include "moc_qbarset.cpp" | |
200 | QTCOMMERCIALCHART_END_NAMESPACE |
|
205 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,73 +1,74 | |||||
1 | #ifndef QBARSET_H |
|
1 | #ifndef QBARSET_H | |
2 | #define QBARSET_H |
|
2 | #define QBARSET_H | |
3 |
|
3 | |||
4 | #include <qchartglobal.h> |
|
4 | #include <qchartglobal.h> | |
5 | #include <QPen> |
|
5 | #include <QPen> | |
6 | #include <QBrush> |
|
6 | #include <QBrush> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject |
|
10 | class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject | |
11 | { |
|
11 | { | |
12 | Q_OBJECT |
|
12 | Q_OBJECT | |
13 | public: |
|
13 | public: | |
14 | QBarSet(QString name, QObject *parent = 0); |
|
14 | QBarSet(QString name, QObject *parent = 0); | |
15 |
|
15 | |||
16 | void setName(QString name); |
|
16 | void setName(QString name); | |
17 | QString name(); |
|
17 | QString name(); | |
18 | QBarSet& operator << (const qreal &value); // appends new value to set |
|
18 | QBarSet& operator << (const qreal &value); // appends new value to set | |
19 | void insertValue(int i, qreal value); |
|
19 | void insertValue(int i, qreal value); | |
|
20 | void removeValue(int i); | |||
20 |
|
21 | |||
21 | // TODO: remove indices eventually. Use as internal? |
|
22 | // TODO: remove indices eventually. Use as internal? | |
22 | int count(); // count of values in set |
|
23 | int count(); // count of values in set | |
23 | qreal valueAt(int index); // for modifying individual values |
|
24 | qreal valueAt(int index); // for modifying individual values | |
24 | void setValue(int index, qreal value); // setter for individual value |
|
25 | void setValue(int index, qreal value); // setter for individual value | |
25 | qreal total(); // total values in the set |
|
26 | qreal total(); // total values in the set | |
26 |
|
27 | |||
27 | // TODO: |
|
28 | // TODO: | |
28 | //qreal value(QString category); |
|
29 | //qreal value(QString category); | |
29 | //void setValue(QString category, qreal value); |
|
30 | //void setValue(QString category, qreal value); | |
30 |
|
31 | |||
31 | void setPen(const QPen pen); |
|
32 | void setPen(const QPen pen); | |
32 | QPen pen() const; |
|
33 | QPen pen() const; | |
33 |
|
34 | |||
34 | void setBrush(const QBrush brush); |
|
35 | void setBrush(const QBrush brush); | |
35 | QBrush brush() const; |
|
36 | QBrush brush() const; | |
36 |
|
37 | |||
37 | void setFloatingValuePen(const QPen pen); |
|
38 | void setFloatingValuePen(const QPen pen); | |
38 | QPen floatingValuePen() const; |
|
39 | QPen floatingValuePen() const; | |
39 |
|
40 | |||
40 | Q_SIGNALS: |
|
41 | Q_SIGNALS: | |
41 | void clicked(QString category); // Clicked and hover signals exposed to user |
|
42 | void clicked(QString category); // Clicked and hover signals exposed to user | |
42 | void rightClicked(QString category); |
|
43 | void rightClicked(QString category); | |
43 | void toggleFloatingValues(); |
|
44 | void toggleFloatingValues(); | |
44 |
|
45 | |||
45 | // TODO: Expose this to user or not? |
|
46 | // TODO: Expose this to user or not? | |
46 | // TODO: TO PIMPL ---> |
|
47 | // TODO: TO PIMPL ---> | |
47 | void structureChanged(); |
|
48 | void structureChanged(); | |
48 | void valueChanged(); |
|
49 | void valueChanged(); | |
49 | void hoverEnter(QPoint pos); |
|
50 | void hoverEnter(QPoint pos); | |
50 | void hoverLeave(); |
|
51 | void hoverLeave(); | |
51 | void showToolTip(QPoint pos, QString tip); // Private signal |
|
52 | void showToolTip(QPoint pos, QString tip); // Private signal | |
52 | // <--- TO PIMPL |
|
53 | // <--- TO PIMPL | |
53 |
|
54 | |||
54 | public Q_SLOTS: |
|
55 | public Q_SLOTS: | |
55 | // These are for internal communication |
|
56 | // These are for internal communication | |
56 | // TODO: TO PIMPL ---> |
|
57 | // TODO: TO PIMPL ---> | |
57 | void barHoverEnterEvent(QPoint pos); |
|
58 | void barHoverEnterEvent(QPoint pos); | |
58 | void barHoverLeaveEvent(); |
|
59 | void barHoverLeaveEvent(); | |
59 | // <--- TO PIMPL |
|
60 | // <--- TO PIMPL | |
60 |
|
61 | |||
61 | private: |
|
62 | private: | |
62 |
|
63 | |||
63 | QString mName; |
|
64 | QString mName; | |
64 | QList<qreal> mValues; // TODO: replace with map (category, value) |
|
65 | QList<qreal> mValues; // TODO: replace with map (category, value) | |
65 | QMap<QString,qreal> mMappedValues; |
|
66 | QMap<QString,qreal> mMappedValues; | |
66 | QPen mPen; |
|
67 | QPen mPen; | |
67 | QBrush mBrush; |
|
68 | QBrush mBrush; | |
68 | QPen mFloatingValuePen; |
|
69 | QPen mFloatingValuePen; | |
69 | }; |
|
70 | }; | |
70 |
|
71 | |||
71 | QTCOMMERCIALCHART_END_NAMESPACE |
|
72 | QTCOMMERCIALCHART_END_NAMESPACE | |
72 |
|
73 | |||
73 | #endif // QBARSET_H |
|
74 | #endif // QBARSET_H |
General Comments 0
You need to be logged in to leave comments.
Login now