##// END OF EJS Templates
Adds more logic and documentation to qbarcategoryaxis for append, insert, replace, clear
Michal Klocek -
r2121:95b5b5ea3834
parent child
Show More
@@ -156,108 +156,172 QBarCategoryAxis::QBarCategoryAxis(QBarCategoryAxisPrivate &d, QObject *parent)
156 }
156 }
157
157
158 /*!
158 /*!
159 Appends \a categories to axis
159 Appends \a categories to axis. A maximum of the axis will be changed to last category in \a categories.
160 If there were no categories previously defined, minimum of axis will be also changed to first category in \a categories.
161 A category has to be valid QStrings and can not be duplicated. Duplicated categories will not be appended.
160 */
162 */
161 void QBarCategoryAxis::append(const QStringList &categories)
163 void QBarCategoryAxis::append(const QStringList &categories)
162 {
164 {
163 if (categories.isEmpty())
165 if (categories.isEmpty())
164 return;
166 return;
165
167
166 Q_D(QBarCategoryAxis);
168 Q_D(QBarCategoryAxis);
167 if (d->m_categories.isEmpty()) {
169
168 d->m_categories.append(categories);
170 int count = d->m_categories.count();
169 setRange(categories.first(), categories.last());
171
172 foreach(QString category, categories)
173 {
174 if(!d->m_categories.contains(category) && !category.isNull()) {
175 d->m_categories.append(category);
176 }
177 }
178
179 if(d->m_categories.count() == count) return;
180
181 if (count == 0) {
182 setRange(d->m_categories.first(), d->m_categories.last());
170 } else {
183 } else {
171 d->m_categories.append(categories);
184 setRange(d->m_minCategory, d->m_categories.last());
172 d->emitUpdated();
173 }
185 }
186
174 emit categoriesChanged();
187 emit categoriesChanged();
175 }
188 }
176
189
177 /*!
190 /*!
178 Appends \a category to axis
191 Appends \a category to axis. A maximum of the axis will be changed to last \a category.
192 If there were no categories previously defined, minimum of axis will be also changed to \a category.
193 A \a category has to be valid QStrings and can not be duplicated. Duplicated categories will not be appended.
179 */
194 */
180 void QBarCategoryAxis::append(const QString &category)
195 void QBarCategoryAxis::append(const QString &category)
181 {
196 {
182 Q_D(QBarCategoryAxis);
197 Q_D(QBarCategoryAxis);
183 if (d->m_categories.isEmpty()) {
198
199 int count = d->m_categories.count();
200
201 if(!d->m_categories.contains(category) && !category.isNull()){
184 d->m_categories.append(category);
202 d->m_categories.append(category);
185 setRange(category, category);
203 }
204
205 if(d->m_categories.count() == count) return;
206
207 if (count == 0) {
208 setRange(d->m_categories.last(), d->m_categories.last());
186 } else {
209 } else {
187 d->m_categories.append(category);
210 setRange(d->m_minCategory, d->m_categories.last());
188 d->emitUpdated();
189 }
211 }
212
190 emit categoriesChanged();
213 emit categoriesChanged();
191 }
214 }
192
215
193 /*!
216 /*!
194 Removes \a category from axis
217 Removes \a category from axis. Removing category which is currently maximum or minimum
218 will affect the axis range.
195 */
219 */
196 void QBarCategoryAxis::remove(const QString &category)
220 void QBarCategoryAxis::remove(const QString &category)
197 {
221 {
198 Q_D(QBarCategoryAxis);
222 Q_D(QBarCategoryAxis);
223
199 if (d->m_categories.contains(category)) {
224 if (d->m_categories.contains(category)) {
200 d->m_categories.removeAt(d->m_categories.indexOf(category));
225 d->m_categories.removeAt(d->m_categories.indexOf(category));
201 if (!d->m_categories.isEmpty())
226 if (!d->m_categories.isEmpty()){
202 setRange(d->m_categories.first(), d->m_categories.last());
227 if(d->m_minCategory == category){
203 else
228 setRange(d->m_categories.first(), d->m_maxCategory);
229 } else if(d->m_maxCategory == category){
230 setRange(d->m_minCategory, d->m_categories.last());
231 } else {
232 d->updateCategoryDomain();
233 d->emitUpdated();
234 }
235 }else
204 setRange(QString::null, QString::null);
236 setRange(QString::null, QString::null);
205 emit categoriesChanged();
237 emit categoriesChanged();
206 }
238 }
207 }
239 }
208
240
209 /*!
241 /*!
210 Inserts \a category to axis at \a index
242 Inserts \a category to axis at \a index. A \a category has to be valid QStrings and can not be duplicated.
243 If \a category is prepended or appended to categories, minimum and maximum of axis is updated accordingly.
211 */
244 */
212 void QBarCategoryAxis::insert(int index, const QString &category)
245 void QBarCategoryAxis::insert(int index, const QString &category)
213 {
246 {
214 Q_D(QBarCategoryAxis);
247 Q_D(QBarCategoryAxis);
215 if (d->m_categories.isEmpty()) {
248
249 int count = d->m_categories.count();
250
251 if(!d->m_categories.contains(category) && !category.isNull()){
216 d->m_categories.insert(index, category);
252 d->m_categories.insert(index, category);
217 setRange(category, category);
253 }
254
255 if(d->m_categories.count() == count) return;
256
257 if (count == 0) {
258 setRange(d->m_categories.first(), d->m_categories.first());
259 } else if (index == 0) {
260 setRange(d->m_categories.first(), d->m_maxCategory);
261 } else if (index == count){
262 setRange(d->m_minCategory, d->m_categories.last());
218 } else {
263 } else {
219 d->m_categories.insert(index, category);
264 d->updateCategoryDomain();
220 d->emitUpdated();
265 d->emitUpdated();
221 }
266 }
267
222 emit categoriesChanged();
268 emit categoriesChanged();
223 }
269 }
224
270
225 /*!
271 /*!
226 Replaces \a oldCategory with \a newCategory.
272 Replaces \a oldCategory with \a newCategory. If \a oldCategory does not exits on the axis nothing is done.
227 If \a oldCategory does not exits on the axis nothing is done.
273 A \a newVategory has to be valid QStrings and can not be duplicated. In case of replacing minimum or maximum category,
274 minimum and maximum of axis is updated accordingly.
228 */
275 */
229 void QBarCategoryAxis::replace(const QString &oldCategory, const QString &newCategory)
276 void QBarCategoryAxis::replace(const QString &oldCategory, const QString &newCategory)
230 {
277 {
231 Q_D(QBarCategoryAxis);
278 Q_D(QBarCategoryAxis);
279
232 int pos = d->m_categories.indexOf(oldCategory);
280 int pos = d->m_categories.indexOf(oldCategory);
233 if (pos != -1) {
281
282 if (pos != -1 && !d->m_categories.contains(newCategory) && !newCategory.isNull()) {
234 d->m_categories.replace(pos, newCategory);
283 d->m_categories.replace(pos, newCategory);
235 d->emitUpdated();
284 if(d->m_minCategory == oldCategory) {
285 setRange(newCategory, d->m_maxCategory);
286 } else if(d->m_maxCategory == oldCategory) {
287 setRange(d->m_minCategory, newCategory);
288 } else {
289 d->emitUpdated();
290 }
236 emit categoriesChanged();
291 emit categoriesChanged();
237 }
292 }
238 }
293 }
239
294
240 /*!
295 /*!
241 Removes all categories.
296 Removes all categories. Sets the maximum and minimum of the axis's range to QString::null.
242 */
297 */
243 void QBarCategoryAxis::clear()
298 void QBarCategoryAxis::clear()
244 {
299 {
245 Q_D(QBarCategoryAxis);
300 Q_D(QBarCategoryAxis);
246 d->m_categories.clear();
301 d->m_categories.clear();
247 setRange(QString::null, QString::null);
302 setRange(QString::null, QString::null);
248 emit categoriesChanged();
303 emit categoriesChanged();
249 }
304 }
250
305
306 /*!
307 Set \a categories and discards the old ones, range of axis is adjusted to match first and last category in \a categories.
308 A category has to be valid QStrings and can not be duplicated.
309 */
251 void QBarCategoryAxis::setCategories(const QStringList &categories)
310 void QBarCategoryAxis::setCategories(const QStringList &categories)
252 {
311 {
253 Q_D(QBarCategoryAxis);
312 Q_D(QBarCategoryAxis);
254 if (d->m_categories != categories) {
313 d->m_categories.clear();
255 d->m_categories = categories;
314 d->m_minCategory = QString::null;
256 setRange(categories.first(), categories.last());
315 d->m_maxCategory = QString::null;
257 emit categoriesChanged();
316 d->m_min = 0;
258 }
317 d->m_max = 0;
318 d->m_count = 0;
319 append(categories);
259 }
320 }
260
321
322 /*!
323 Returns categories
324 */
261 QStringList QBarCategoryAxis::categories()
325 QStringList QBarCategoryAxis::categories()
262 {
326 {
263 Q_D(QBarCategoryAxis);
327 Q_D(QBarCategoryAxis);
@@ -327,7 +391,7 void QBarCategoryAxis::setRange(const QString &minCategory, const QString &maxCa
327
391
328 bool changed = false;
392 bool changed = false;
329
393
330 //special case
394 //special case in case or clearing all categories
331 if (minCategory.isNull() && maxCategory.isNull()) {
395 if (minCategory.isNull() && maxCategory.isNull()) {
332 d->m_minCategory = minCategory;
396 d->m_minCategory = minCategory;
333 d->m_maxCategory = maxCategory;
397 d->m_maxCategory = maxCategory;
@@ -340,9 +404,9 void QBarCategoryAxis::setRange(const QString &minCategory, const QString &maxCa
340 d->emitUpdated();
404 d->emitUpdated();
341 }
405 }
342
406
343 if (d->m_categories.indexOf(d->m_maxCategory) < d->m_categories.indexOf(d->m_minCategory))
407 if (d->m_categories.indexOf(maxCategory) < d->m_categories.indexOf(minCategory)){
344 return;
408 return;
345
409 }
346 if (!minCategory.isEmpty() && d->m_minCategory != minCategory && d->m_categories.contains(minCategory)) {
410 if (!minCategory.isEmpty() && d->m_minCategory != minCategory && d->m_categories.contains(minCategory)) {
347 d->m_minCategory = minCategory;
411 d->m_minCategory = minCategory;
348 d->m_min = d->m_categories.indexOf(d->m_minCategory) - 0.5;
412 d->m_min = d->m_categories.indexOf(d->m_minCategory) - 0.5;
@@ -451,6 +515,14 ChartAxis *QBarCategoryAxisPrivate::createGraphics(ChartPresenter *presenter)
451 return new ChartBarCategoryAxisX(q, presenter);
515 return new ChartBarCategoryAxisX(q, presenter);
452 }
516 }
453
517
518 void QBarCategoryAxisPrivate::updateCategoryDomain()
519 {
520 m_min = m_categories.indexOf(m_minCategory) - 0.5;
521 m_max = m_categories.indexOf(m_maxCategory) + 0.5;
522 m_count = m_max - m_min;
523 }
524
525
454 void QBarCategoryAxisPrivate::intializeDomain(Domain *domain)
526 void QBarCategoryAxisPrivate::intializeDomain(Domain *domain)
455 {
527 {
456
528
@@ -49,6 +49,7 public:
49 ChartAxis *createGraphics(ChartPresenter *presenter);
49 ChartAxis *createGraphics(ChartPresenter *presenter);
50 void intializeDomain(Domain *domain);
50 void intializeDomain(Domain *domain);
51 void handleDomainUpdated();
51 void handleDomainUpdated();
52 void updateCategoryDomain();
52 qreal min() { return m_min; }
53 qreal min() { return m_min; }
53 qreal max() { return m_max; }
54 qreal max() { return m_max; }
54 int count() const { return m_count; }
55 int count() const { return m_count; }
@@ -142,9 +142,8 void tst_QBarCategoriesAxis::qbarcategoryaxis()
142 QBarCategoryAxis axis;
142 QBarCategoryAxis axis;
143 axis.append(QStringList());
143 axis.append(QStringList());
144 axis.append(QString());
144 axis.append(QString());
145 QCOMPARE(axis.at(0), QString());
145 QCOMPARE(axis.count(), 0);
146 QStringList test;
146 QStringList test;
147 test.append(QString());
148 QCOMPARE(axis.categories(),test);
147 QCOMPARE(axis.categories(),test);
149 axis.clear();
148 axis.clear();
150 QCOMPARE(axis.count(), 0);
149 QCOMPARE(axis.count(), 0);
@@ -395,8 +394,7 void tst_QBarCategoriesAxis::insert_data()
395 QTest::addColumn<QStringList>("categories");
394 QTest::addColumn<QStringList>("categories");
396 QTest::addColumn<int>("index");
395 QTest::addColumn<int>("index");
397 QTest::addColumn<QString>("category");
396 QTest::addColumn<QString>("category");
398 QTest::newRow("Jul Aug Sep 0 Jun") << (QStringList() << "Jul" << "Aug" << "Sep") << 0 << "Jun";
397 QTest::newRow("Jul Aug Sep 0 Jun") << (QStringList() << "Jul" << "Aug" << "Sep") << 1 << "Jun";
399 QTest::newRow("Jul Aug Sep 3 Sep") << (QStringList() << "Jul" << "Aug" << "Sep") << 3 << "Sep";
400 QTest::newRow("Jul Aug Sep 2 Summer") << (QStringList() << "Jul" << "Aug" << "Sep") << 2 << "Summer";
398 QTest::newRow("Jul Aug Sep 2 Summer") << (QStringList() << "Jul" << "Aug" << "Sep") << 2 << "Summer";
401 }
399 }
402
400
General Comments 0
You need to be logged in to leave comments. Login now