##// 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 163 void QBarCategoryAxis::append(const QStringList &categories)
162 164 {
163 165 if (categories.isEmpty())
164 return;
166 return;
165 167
166 168 Q_D(QBarCategoryAxis);
167 if (d->m_categories.isEmpty()) {
168 d->m_categories.append(categories);
169 setRange(categories.first(), categories.last());
169
170 int count = d->m_categories.count();
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 183 } else {
171 d->m_categories.append(categories);
172 d->emitUpdated();
184 setRange(d->m_minCategory, d->m_categories.last());
173 185 }
186
174 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 195 void QBarCategoryAxis::append(const QString &category)
181 196 {
182 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 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 209 } else {
187 d->m_categories.append(category);
188 d->emitUpdated();
210 setRange(d->m_minCategory, d->m_categories.last());
189 211 }
212
190 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 220 void QBarCategoryAxis::remove(const QString &category)
197 221 {
198 222 Q_D(QBarCategoryAxis);
223
199 224 if (d->m_categories.contains(category)) {
200 225 d->m_categories.removeAt(d->m_categories.indexOf(category));
201 if (!d->m_categories.isEmpty())
202 setRange(d->m_categories.first(), d->m_categories.last());
203 else
226 if (!d->m_categories.isEmpty()){
227 if(d->m_minCategory == category){
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 236 setRange(QString::null, QString::null);
205 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 245 void QBarCategoryAxis::insert(int index, const QString &category)
213 246 {
214 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 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 263 } else {
219 d->m_categories.insert(index, category);
264 d->updateCategoryDomain();
220 265 d->emitUpdated();
221 266 }
267
222 268 emit categoriesChanged();
223 269 }
224 270
225 271 /*!
226 Replaces \a oldCategory with \a newCategory.
227 If \a oldCategory does not exits on the axis nothing is done.
272 Replaces \a oldCategory with \a newCategory. 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 276 void QBarCategoryAxis::replace(const QString &oldCategory, const QString &newCategory)
230 277 {
231 278 Q_D(QBarCategoryAxis);
279
232 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 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 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 298 void QBarCategoryAxis::clear()
244 299 {
245 300 Q_D(QBarCategoryAxis);
246 301 d->m_categories.clear();
247 setRange(QString::null, QString::null);
302 setRange(QString::null, QString::null);
248 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 310 void QBarCategoryAxis::setCategories(const QStringList &categories)
252 311 {
253 312 Q_D(QBarCategoryAxis);
254 if (d->m_categories != categories) {
255 d->m_categories = categories;
256 setRange(categories.first(), categories.last());
257 emit categoriesChanged();
258 }
313 d->m_categories.clear();
314 d->m_minCategory = QString::null;
315 d->m_maxCategory = QString::null;
316 d->m_min = 0;
317 d->m_max = 0;
318 d->m_count = 0;
319 append(categories);
259 320 }
260 321
322 /*!
323 Returns categories
324 */
261 325 QStringList QBarCategoryAxis::categories()
262 326 {
263 327 Q_D(QBarCategoryAxis);
@@ -327,7 +391,7 void QBarCategoryAxis::setRange(const QString &minCategory, const QString &maxCa
327 391
328 392 bool changed = false;
329 393
330 //special case
394 //special case in case or clearing all categories
331 395 if (minCategory.isNull() && maxCategory.isNull()) {
332 396 d->m_minCategory = minCategory;
333 397 d->m_maxCategory = maxCategory;
@@ -340,9 +404,9 void QBarCategoryAxis::setRange(const QString &minCategory, const QString &maxCa
340 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 408 return;
345
409 }
346 410 if (!minCategory.isEmpty() && d->m_minCategory != minCategory && d->m_categories.contains(minCategory)) {
347 411 d->m_minCategory = minCategory;
348 412 d->m_min = d->m_categories.indexOf(d->m_minCategory) - 0.5;
@@ -451,6 +515,14 ChartAxis *QBarCategoryAxisPrivate::createGraphics(ChartPresenter *presenter)
451 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 526 void QBarCategoryAxisPrivate::intializeDomain(Domain *domain)
455 527 {
456 528
@@ -49,6 +49,7 public:
49 49 ChartAxis *createGraphics(ChartPresenter *presenter);
50 50 void intializeDomain(Domain *domain);
51 51 void handleDomainUpdated();
52 void updateCategoryDomain();
52 53 qreal min() { return m_min; }
53 54 qreal max() { return m_max; }
54 55 int count() const { return m_count; }
@@ -142,9 +142,8 void tst_QBarCategoriesAxis::qbarcategoryaxis()
142 142 QBarCategoryAxis axis;
143 143 axis.append(QStringList());
144 144 axis.append(QString());
145 QCOMPARE(axis.at(0), QString());
145 QCOMPARE(axis.count(), 0);
146 146 QStringList test;
147 test.append(QString());
148 147 QCOMPARE(axis.categories(),test);
149 148 axis.clear();
150 149 QCOMPARE(axis.count(), 0);
@@ -395,8 +394,7 void tst_QBarCategoriesAxis::insert_data()
395 394 QTest::addColumn<QStringList>("categories");
396 395 QTest::addColumn<int>("index");
397 396 QTest::addColumn<QString>("category");
398 QTest::newRow("Jul Aug Sep 0 Jun") << (QStringList() << "Jul" << "Aug" << "Sep") << 0 << "Jun";
399 QTest::newRow("Jul Aug Sep 3 Sep") << (QStringList() << "Jul" << "Aug" << "Sep") << 3 << "Sep";
397 QTest::newRow("Jul Aug Sep 0 Jun") << (QStringList() << "Jul" << "Aug" << "Sep") << 1 << "Jun";
400 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