Kstars

timespinbox.cpp
1 /*
2  SPDX-FileCopyrightText: 2002 Jason Harris <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "timespinbox.h"
8 
9 #include "kstars_debug.h"
10 
11 #include <KLocalizedString>
12 
13 #include <QDebug>
14 #include <QLineEdit>
15 
16 #include <cmath>
17 
18 #define SECS_PER_DAY 86400.
19 #define SIDEREAL_YEAR 31558149.77
20 
21 //Time steps:
22 //0-9: 0 sec, 0.1, 0.25, 0.5, 1, 2, 5, 10, 20, 30 sec
23 //10-14: 1 minute, 2, 5, 10, 15, 30 min
24 //15-19: 1 hour, 2, 4, 6, 12 hours
25 //20-23: 1 day, 2, 3, 5 days
26 //24-26: 1 week, 2, 3 weeks
27 //27-32: 1 month, 2, 3, 4, 6, 9 months
28 //33-41: 1 year, 2, 3, 4, 5, 10, 25, 50, 100 years
29 
30 TimeSpinBox::TimeSpinBox(QWidget *parent, bool _daysonly) : QSpinBox(parent)
31 {
32  setDaysOnly(_daysonly);
33 
34  setMinimum(-41);
35  setMaximum(41);
36  setSingleStep(1);
37 
38  setButtonSymbols(QSpinBox::PlusMinus);
39  lineEdit()->setReadOnly(true);
40  setValue(4); //1 second (real time)
41 
42  //Set width:
43  QFontMetrics fm(font());
44  int extra = width() - lineEdit()->width();
45  uint wmax = 0;
46  for (int i = 0; i < maximum(); ++i)
47  {
48  #if QT_VERSION >= QT_VERSION_CHECK(5,11,0)
49  uint w = fm.horizontalAdvance('-' + TimeString[i]);
50  #else
51  uint w = fm.width('-' + TimeString[i]);
52  #endif
53 
54  if (w > wmax)
55  wmax = w;
56  }
57  setFixedWidth(wmax + extra);
58 
59  connect(this, SIGNAL(valueChanged(int)), this, SLOT(reportChange()));
60  // updateDisplay();
61 }
62 
63 void TimeSpinBox::setDaysOnly(bool daysonly)
64 {
65  DaysOnly = daysonly;
66 
67  int i = 0; //index for TimeScale values
68  TimeScale[0] = 0.0; // 0.0 sec
69  if (!daysOnly())
70  {
71  TimeScale[1] = 0.1; // 0.1 sec
72  TimeScale[2] = 0.25; // 0.25 sec
73  TimeScale[3] = 0.5; // 0.5 sec
74  TimeScale[4] = 1.0; // 1 sec (real-time)
75  TimeScale[5] = 2.0; // 2 sec
76  TimeScale[6] = 5.0; // 5 sec
77  TimeScale[7] = 10.0; // 10 sec
78  TimeScale[8] = 20.0; // 20 sec
79  TimeScale[9] = 30.0; // 30 sec
80  TimeScale[10] = 60.0; // 1 min
81  TimeScale[11] = 120.0; // 2 min
82  TimeScale[12] = 300.0; // 5 min
83  TimeScale[13] = 600.0; // 10 min
84  TimeScale[14] = 900.0; // 15 min
85  TimeScale[15] = 1800.0; // 30 min
86  TimeScale[16] = 3600.0; // 1 hr
87  TimeScale[17] = 7200.0; // 2 hr
88  TimeScale[18] = 10800.0; // 3 hr
89  TimeScale[19] = 21600.0; // 6 hr
90  TimeScale[20] = 43200.0; // 12 hr
91  i = 20;
92  }
93  TimeScale[i + 1] = 86164.1; // 23 hr 56 min
94  TimeScale[i + 2] = SECS_PER_DAY; // 1 day
95  TimeScale[i + 3] = 2. * SECS_PER_DAY; // 2 days
96  TimeScale[i + 4] = 3. * SECS_PER_DAY; // 3 days
97  TimeScale[i + 5] = 5. * SECS_PER_DAY; // 5 days
98  TimeScale[i + 6] = 7. * SECS_PER_DAY; // 1 week
99  TimeScale[i + 7] = 14. * SECS_PER_DAY; //2 weeks
100  TimeScale[i + 8] = 21. * SECS_PER_DAY; //3 weeks
101  //Months aren't a simple measurement of time; I'll just use fractions of a year
102  TimeScale[i + 9] = SIDEREAL_YEAR / 12.0; // 1 month
103  TimeScale[i + 10] = SIDEREAL_YEAR / 6.0; // 2 months
104  TimeScale[i + 11] = 0.25 * SIDEREAL_YEAR; // 3 months
105  TimeScale[i + 12] = SIDEREAL_YEAR / 3.0; // 4 months
106  TimeScale[i + 13] = 0.5 * SIDEREAL_YEAR; // 6 months
107  TimeScale[i + 14] = 0.75 * SIDEREAL_YEAR; // 9 months
108  TimeScale[i + 15] = SIDEREAL_YEAR; // 1 year
109  TimeScale[i + 16] = 2.0 * SIDEREAL_YEAR; // 2 years
110  TimeScale[i + 17] = 3.0 * SIDEREAL_YEAR; // 3 years
111  TimeScale[i + 18] = 5.0 * SIDEREAL_YEAR; // 5 years
112  TimeScale[i + 19] = 10.0 * SIDEREAL_YEAR; // 10 years
113  TimeScale[i + 20] = 25.0 * SIDEREAL_YEAR; // 25 years
114  TimeScale[i + 21] = 50.0 * SIDEREAL_YEAR; // 50 years
115  TimeScale[i + 22] = 100.0 * SIDEREAL_YEAR; // 100 years
116 
117  TimeString.clear();
118  if (!daysOnly())
119  {
120  TimeString.append("0 " + i18nc("seconds", "secs"));
121  TimeString.append("0.1 " + i18nc("seconds", "secs"));
122  TimeString.append("0.25 " + i18nc("seconds", "secs"));
123  TimeString.append("0.5 " + i18nc("seconds", "secs"));
124  TimeString.append("1 " + i18nc("second", "sec"));
125  TimeString.append("2 " + i18nc("seconds", "secs"));
126  TimeString.append("5 " + i18nc("seconds", "secs"));
127  TimeString.append("10 " + i18nc("seconds", "secs"));
128  TimeString.append("20 " + i18nc("seconds", "secs"));
129  TimeString.append("30 " + i18nc("seconds", "secs"));
130  TimeString.append("1 " + i18nc("minute", "min"));
131  TimeString.append("2 " + i18nc("minutes", "mins"));
132  TimeString.append("5 " + i18nc("minutes", "mins"));
133  TimeString.append("10 " + i18nc("minutes", "mins"));
134  TimeString.append("15 " + i18nc("minutes", "mins"));
135  TimeString.append("30 " + i18nc("minutes", "mins"));
136  TimeString.append("1 " + i18n("hour"));
137  TimeString.append("2 " + i18nc("hours", "hrs"));
138  TimeString.append("3 " + i18nc("hours", "hrs"));
139  TimeString.append("6 " + i18nc("hours", "hrs"));
140  TimeString.append("12 " + i18nc("hours", "hrs"));
141  }
142  else
143  {
144  TimeString.append("0 " + i18n("days"));
145  }
146  TimeString.append("1 " + i18nc("sidereal day", "sid day"));
147  TimeString.append("1 " + i18n("day"));
148  TimeString.append("2 " + i18n("days"));
149  TimeString.append("3 " + i18n("days"));
150  TimeString.append("5 " + i18n("days"));
151  TimeString.append("1 " + i18n("week"));
152  TimeString.append("2 " + i18nc("weeks", "wks"));
153  TimeString.append("3 " + i18nc("weeks", "wks"));
154  TimeString.append("1 " + i18n("month"));
155  TimeString.append("2 " + i18nc("months", "mths"));
156  TimeString.append("3 " + i18nc("months", "mths"));
157  TimeString.append("4 " + i18nc("months", "mths"));
158  TimeString.append("6 " + i18nc("months", "mths"));
159  TimeString.append("9 " + i18nc("months", "mths"));
160  TimeString.append("1 " + i18n("year"));
161  TimeString.append("2 " + i18nc("years", "yrs"));
162  TimeString.append("3 " + i18nc("years", "yrs"));
163  TimeString.append("5 " + i18nc("years", "yrs"));
164  TimeString.append("10 " + i18nc("years", "yrs"));
165  TimeString.append("25 " + i18nc("years", "yrs"));
166  TimeString.append("50 " + i18nc("years", "yrs"));
167  TimeString.append("100 " + i18nc("years", "yrs"));
168 
169  if (!daysOnly())
170  {
171  setMinimum(-41);
172  setMaximum(41);
173  }
174  else
175  {
176  setMinimum(-21);
177  setMaximum(21);
178  }
179 }
180 
181 int TimeSpinBox::valueFromText(const QString &text) const
182 {
183  for (int i = 0; i < TimeString.size(); i++)
184  {
185  if (text == TimeString[i])
186  {
187  return i;
188  }
189  if (text.mid(1, text.length()) == TimeString[i])
190  {
191  return -1 * i;
192  }
193  }
194 
195  return 0;
196 }
197 
198 QString TimeSpinBox::textFromValue(int value) const
199 {
200  QString result;
201  int posval(abs(value));
202 
203  if (posval > TimeString.size() - 1)
204  posval = 4;
205 
206  result = TimeString[posval];
207 
208  if (value < 0)
209  {
210  result = '-' + result;
211  }
212  return result;
213 }
214 
215 void TimeSpinBox::changeScale(float x)
216 {
217  //Pick the closest value
218  int imin = 0;
219  float dxlast(10000000000.0), dxmin(10000000000.0);
220 
221  for (unsigned int i = 0; i < 42; ++i)
222  {
223  float dx = fabs(TimeScale[i] - fabs(x));
224 
225  if (dx < dxmin)
226  {
227  imin = i;
228  dxmin = dx;
229  }
230  if (dx > dxlast)
231  break; //we have passed the minimum dx
232 
233  dxlast = dx;
234  }
235 
236  if (x < 0.0)
237  imin *= -1;
238  setValue(imin);
239 }
240 
241 float TimeSpinBox::timeScale(void) const
242 {
243  return value() > 0 ? TimeScale[value()] : -1. * TimeScale[abs(value())];
244 }
245 
246 void TimeSpinBox::reportChange()
247 {
248  qCDebug(KSTARS) << "Reporting new timestep value: " << timeScale();
249  emit scaleChanged(timeScale());
250 }
void append(const T &value)
int width(const QString &text, int len) const const
int horizontalAdvance(const QString &text, int len) const const
int size() const const
QString i18n(const char *text, const TYPE &arg...)
int length() const const
void clear()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
TimeSpinBox(QWidget *parent, bool daysOnly=false)
Constructor.
Definition: timespinbox.cpp:30
QString mid(int position, int n) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Aug 11 2022 04:00:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.