Kstars

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

KDE's Doxygen guidelines are available online.