COPASI API  4.16.103
CQTSSATimeScaleWidget.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 #include "CQTSSATimeScaleWidget.h"
12 
13 #include <cmath>
14 #include <QtGui/QBitmap>
15 #include <QtGui/QColor>
16 #include <QtGui/QToolTip>
17 #include <QtGui/QVBoxLayout>
18 #include <QtGui/QPaintEvent>
19 
20 /*
21  * Constructs a CScanWidgetRepeat as a child of 'parent', with the
22  * name 'name' and widget flags set to 'f'.
23  */
24 CQTSSATimeScaleWidget::CQTSSATimeScaleWidget(QWidget* parent, const char* name, Qt::WFlags fl)
25  : QWidget(parent, fl)
26 {
27  setObjectName(QString::fromUtf8(name));
28 
29  if (!name)
30  setObjectName("CQTSSATimeScaleWidget");
31 
32  mpVLayout = new QVBoxLayout(this);
33  mpPaintWidget = new PaintWidget(this, "PaintWidget");
34  mpPaintWidget->setMinimumHeight(200);
35  mpSlider = new QSlider(Qt::Horizontal, this);
36  mpSlider->setDisabled(true);
37 
38  mpVLayout->addWidget(mpPaintWidget);
39  mpVLayout->addWidget(mpSlider);
40 
41  mpPaintWidget->setStyleSheet(QString(mpPaintWidget->metaObject()->className()) + " {background-color:" + QColor(Qt::white).name() + ";}");
42  mpPaintWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
43  mpSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
44 
45  mpSlider->setToolTip("move Slider to set focus on prefered time scale"); // <- helpful
46  connect(mpSlider, SIGNAL(valueChanged(int)), this, SLOT(changedInterval()));
47 }
48 
49 /*
50  * Destroys the object and frees any allocated resources
51  */
53 {
54  // no need to delete child widgets, Qt does it all for us
55 }
56 
58 {
59 
60  if (vector.size() > 0)
61  {
62  mpPaintWidget->mClear = false;
63  mpSlider->setDisabled(false);
64 
65  if (vector.size() != mpPaintWidget->mVector.size())
66  {
67  mpSlider->setRange(0, (int)(vector.size() - 1));
68  mpSlider->setValue(mpSlider->minimum());
69  }
70 
71  mpPaintWidget->mVector = vector;
73  mpPaintWidget->repaint();
74  }
75 }
76 
78 {
79  if (mpPaintWidget->mVector.size() == 0)
80  return;
81 
83 }
84 
86 {
87  mpPaintWidget->mClear = true;
88  mpPaintWidget->repaint();
89  mpSlider->setDisabled(true);
90 }
91 
92 //******************************************************************
93 
94 /*
95  * Constructs a CScanWidgetRepeat as a child of 'parent', with the
96  * name 'name' and widget flags set to 'f'.
97  */
98 PaintWidget::PaintWidget(QWidget* parent, const char* name, Qt::WFlags fl)
99  : QWidget(parent, fl),
100  mSelection(0)
101 {
102  setObjectName(QString::fromUtf8(name));
103 
104  if (!name)
105  setObjectName("PaintWidget");
106 }
107 
108 /*
109  * Destroys the object and frees any allocated resources
110  */
112 {
113  // no need to delete child widgets, Qt does it all for us
114 }
115 
117 {
118  mSelection = select;
119  repaint();
120 }
121 
122 void PaintWidget::paintEvent(QPaintEvent *)
123 {
124 
125  if (mVector.size() == 0) return;
126 
127  if (mClear) return;
128 
129  int position;
130  int space = 35;
131 
132  int maxScaleValue = -1000;
133  int minScaleValue = 1000;
134  unsigned C_INT32 j;
135 
136  for (j = 0; j < mVector.size(); ++j)
137  {
138  if (fabs(mVector[j]) == std::numeric_limits<C_FLOAT64>::infinity())
139  continue;
140 
141  if ((int)(log10(fabs(mVector[j])) + 1) > maxScaleValue)
142  maxScaleValue = (int)(log10(fabs(mVector[j])) + 1);
143 
144  if ((int)(log10(fabs(mVector[j])) - 1) < minScaleValue)
145  minScaleValue = (int)(log10(fabs(mVector[j])) - 1);
146  }
147 
148  int scaleValueRange = maxScaleValue - minScaleValue;
149 
150  QPainter paint(this);
151  paint.save();
152 
153  //paint.setWindow(0, 0, 1000, 1000);
154  QRect v = paint.viewport();
155 
156  //paint.setFont(QFont("Courier", 15));
157 
158  //Legend
159  paint.setPen(QPen(QColor(180, 0, 0), 5));
160  paint.drawLine(space, space, space, space + 10);
161  paint.setPen(QPen(QColor(0, 0, 0), 1));
162  paint.drawText(space + 5, space + 10, " - negative time scale values");
163 
164  paint.setPen(QPen(QColor(0, 180, 0), 5));
165  paint.drawLine(space, space + 20, space, space + 30);
166  paint.setPen(QPen(QColor(0, 0, 0), 1));
167  paint.drawText(space + 5, space + 30, " - positive time scale values");
168 
169  //axis
170  paint.setPen(QPen(QColor(100, 100, 100), 1));
171  int yCentre = v.bottom() - space - 30;
172  int scaleBegin = space;
173  int scaleEnd = v.right() - space; //(maxScaleValue - minScaleValue) * (1000 - 2 * space) / scaleValueRange + space;
174  int axisLength = scaleEnd - scaleBegin;
175  paint.drawLine(scaleBegin, yCentre, scaleEnd, yCentre);
176 
177  //tics
178  C_INT32 i;
179 
180  for (i = 0; i <= scaleValueRange; i++)
181  {
182  position = scaleBegin + (int)(i * axisLength / scaleValueRange);
183  paint.drawLine(position, yCentre + 10, position, yCentre);
184  paint.drawText(position - 5, yCentre + 30, QString::number(minScaleValue + i));
185  }
186 
187  paint.setPen(QPen(QColor(0, 180, 0), 1));
188 
189  //values
190  int yTop = yCentre - 50;
191  int xText = v.right() - space - 230;
192  int yText = space + 30;
193 
194  for (j = 0; j < mVector.size(); j++)
195  {
196  position = scaleBegin + (int)((log10(fabs(mVector[j])) - minScaleValue) * axisLength / scaleValueRange);
197 
198  if ((uint)mSelection == (uint)(mVector.size() - 1 - j))
199  {
200  if (mVector[j] < 0)
201  paint.setPen(QPen(QColor(180, 20, 20), 4));
202  else
203  paint.setPen(QPen(QColor(20, 180, 20), 4));
204 
205  paint.drawLine(position, yCentre - 1, position, yTop);
206  paint.setPen(QPen(QColor(200, 200, 200), 1));
207  // paint.setFont(QFont("Courier", 20));
208  paint.drawLine(xText, yText, position + 1, yTop - 1);
209  paint.drawLine(xText, yText, scaleEnd, yText);
210  paint.setPen(QPen(QColor(0, 0, 0), 1));
211 
212  if (mVector[j] < 0)
213  paint.drawText(xText + 1, yText - 15, " log10 (|" + QString::number(mVector[j]) + "|) = " + QString::number(log10(fabs(mVector[j]))));
214  else
215  paint.drawText(xText + 1, yText - 15, " log10 (" + QString::number(fabs(mVector[j])) + ") = " + QString::number(log10(fabs(mVector[j]))));
216 
217  paint.drawText(scaleBegin + scaleEnd / 2 - 50, yCentre + 50, " log timescale");
218  }
219  else
220  {
221  if (mVector[j] < 0)
222  paint.setPen(QPen(QColor(180, 20, 20), 2));
223  else
224  paint.setPen(QPen(QColor(20, 180, 20), 2));
225 
226  paint.drawLine(position, yCentre - 1, position, yTop);
227  }
228  }
229 
230  paint.restore();
231 }
#define C_INT32
Definition: copasi.h:90
PaintWidget(QWidget *parent=0, const char *name=0, Qt::WFlags fl=0)
void paintEvent(QPaintEvent *)
void paintTimeScale(CVector< C_FLOAT64 > vector)
CQTSSATimeScaleWidget(QWidget *parent=0, const char *name=0, Qt::WFlags fl=0)
size_t size() const
Definition: CVector.h:100
bool fl(const C_FLOAT64 &d1, const C_FLOAT64 &d2)
void paintTimeScale(int select)
CVector< C_FLOAT64 > mVector