PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/GUISupport/Qt/Chart/vtkQtChartSeriesModelRange.cxx

https://github.com/butterwaffle/VTK
C++ | 234 lines | 173 code | 26 blank | 35 comment | 55 complexity | 8c54b417b0010f816f2ac90b14fcc90a MD5 | raw file
  1. /*=========================================================================
  2. Program: Visualization Toolkit
  3. Module: $RCSfile$
  4. Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  5. All rights reserved.
  6. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even
  8. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  9. PURPOSE. See the above copyright notice for more information.
  10. =========================================================================*/
  11. /*-------------------------------------------------------------------------
  12. Copyright 2008 Sandia Corporation.
  13. Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  14. the U.S. Government retains certain rights in this software.
  15. -------------------------------------------------------------------------*/
  16. /// \file vtkQtChartSeriesModelRange.cxx
  17. /// \date February 19, 2008
  18. #ifdef _MSC_VER
  19. // Disable warnings that Qt headers give.
  20. #pragma warning(disable:4127)
  21. #endif
  22. #include "vtkQtChartSeriesModelRange.h"
  23. #include "vtkQtChartSeriesModel.h"
  24. #include <QDate>
  25. #include <QDateTime>
  26. #include <QTime>
  27. #include <math.h>
  28. #ifndef isnan
  29. // This is compiler specific not platform specific: MinGW doesn't need that.
  30. # if defined(_MSC_VER) || defined(__BORLANDC__)
  31. # include <float.h>
  32. # define isnan(x) _isnan(x)
  33. # endif
  34. #endif
  35. vtkQtChartSeriesModelRange::vtkQtChartSeriesModelRange(QObject *parentObject)
  36. : QObject(parentObject)
  37. {
  38. this->Model = 0;
  39. this->XRangeShared = false;
  40. }
  41. void vtkQtChartSeriesModelRange::setModel(vtkQtChartSeriesModel *model,
  42. bool xShared)
  43. {
  44. if(this->Model != model)
  45. {
  46. if(this->Model)
  47. {
  48. this->disconnect(this->Model, 0, this, 0);
  49. }
  50. this->Model = model;
  51. if(this->Model)
  52. {
  53. // Use the series change signals to update the ranges.
  54. this->connect(this->Model, SIGNAL(modelReset()),
  55. this, SLOT(resetSeries()));
  56. this->connect(this->Model, SIGNAL(seriesInserted(int, int)),
  57. this, SLOT(insertSeries(int, int)));
  58. this->connect(this->Model, SIGNAL(seriesRemoved(int, int)),
  59. this, SLOT(removeSeries(int, int)));
  60. }
  61. this->XRangeShared = xShared;
  62. this->resetSeries();
  63. }
  64. else if(this->XRangeShared != xShared)
  65. {
  66. this->XRangeShared = xShared;
  67. this->resetSeries();
  68. }
  69. }
  70. QList<QVariant> vtkQtChartSeriesModelRange::getSeriesRange(int series,
  71. int component) const
  72. {
  73. if(series >= 0 && series < this->Range[1].size())
  74. {
  75. if(component == 0 && this->XRangeShared)
  76. {
  77. series = 0;
  78. }
  79. return this->Range[component][series];
  80. }
  81. return QList<QVariant>();
  82. }
  83. void vtkQtChartSeriesModelRange::resetSeries()
  84. {
  85. // Clean up the range information.
  86. this->Range[0].clear();
  87. this->Range[1].clear();
  88. // Add the new model series.
  89. if(this->Model)
  90. {
  91. int total = this->Model->getNumberOfSeries();
  92. if(total > 0)
  93. {
  94. this->insertSeries(0, total - 1);
  95. }
  96. }
  97. }
  98. void vtkQtChartSeriesModelRange::insertSeries(int first, int last)
  99. {
  100. if(this->Model)
  101. {
  102. // Add range entries for the series.
  103. if(this->XRangeShared && this->Range[1].size() == 0)
  104. {
  105. this->Range[0].append(this->computeSeriesRange(0, 0));
  106. }
  107. for( ; first <= last; first++)
  108. {
  109. this->Range[1].insert(first, this->computeSeriesRange(first, 1));
  110. if(!this->XRangeShared)
  111. {
  112. this->Range[0].insert(first, this->computeSeriesRange(first, 0));
  113. }
  114. }
  115. }
  116. }
  117. void vtkQtChartSeriesModelRange::removeSeries(int first, int last)
  118. {
  119. // Remove range entries for the series.
  120. for( ; last >= first; last--)
  121. {
  122. this->Range[1].removeAt(last);
  123. if(!this->XRangeShared)
  124. {
  125. this->Range[0].removeAt(last);
  126. }
  127. }
  128. if(this->XRangeShared && this->Range[1].size() == 0)
  129. {
  130. this->Range[0].clear();
  131. }
  132. }
  133. QList<QVariant> vtkQtChartSeriesModelRange::computeSeriesRange(int series,
  134. int component)
  135. {
  136. QList<QVariant> range;
  137. if(this->Model)
  138. {
  139. int total = this->Model->getNumberOfSeriesValues(series);
  140. int i;
  141. QVariant value;
  142. QVariant::Type valueType = QVariant::Invalid;
  143. // Find the first non-NULL, non-NaN value. Use it to determine type
  144. // and initialize min/max values.
  145. for (i = 0; i < total; i++)
  146. {
  147. value = this->Model->getSeriesValue(series, i, component);
  148. valueType = value.type();
  149. // Check to see if the the value is invalid and we have to continue loop.
  150. if (value.isNull()) continue;
  151. if (!value.isValid()) continue;
  152. if ((valueType == QVariant::Double) && isnan(value.toDouble())) continue;
  153. // If we got here, we passed all the checks. Break out.
  154. break;
  155. }
  156. // If we found a valid entry that is not a string (for which range has no
  157. // meaning), then continue to compute the range.
  158. if ((i < total) && (valueType != QVariant::String))
  159. {
  160. range.append(value);
  161. range.append(value);
  162. // Continue iteration over values.
  163. for(i++; i < total; i++)
  164. {
  165. value = this->Model->getSeriesValue(series, i, component);
  166. if(value.type() != valueType)
  167. {
  168. continue;
  169. }
  170. if(valueType == QVariant::Int)
  171. {
  172. range[0] = qMin<int>(value.toInt(), range[0].toInt());
  173. range[1] = qMax<int>(value.toInt(), range[1].toInt());
  174. }
  175. else if(valueType == QVariant::Double)
  176. {
  177. if (!isnan(value.toDouble()))
  178. {
  179. range[0] = qMin<double>(value.toDouble(), range[0].toDouble());
  180. range[1] = qMax<double>(value.toDouble(), range[1].toDouble());
  181. }
  182. }
  183. else if(valueType == QVariant::Date)
  184. {
  185. range[0] = qMin<QDate>(value.toDate(), range[0].toDate());
  186. range[1] = qMax<QDate>(value.toDate(), range[1].toDate());
  187. }
  188. else if(valueType == QVariant::DateTime)
  189. {
  190. range[0] = qMin<QDateTime>(value.toDateTime(),
  191. range[0].toDateTime());
  192. range[1] = qMax<QDateTime>(value.toDateTime(),
  193. range[1].toDateTime());
  194. }
  195. else if(valueType == QVariant::Time)
  196. {
  197. range[0] = qMin<QTime>(value.toTime(), range[0].toTime());
  198. range[1] = qMax<QTime>(value.toTime(), range[1].toTime());
  199. }
  200. }
  201. }
  202. }
  203. return range;
  204. }