1 /*
2     SPDX-FileCopyrightText: 2002 Jason Harris <kstars@30doradus.org>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "timeunitbox.h"
8 
9 #include <QToolButton>
10 #include <QVBoxLayout>
11 
12 #include <KLocalizedString>
13 
14 #define TUB_DAYUNITS 5
15 
TimeUnitBox(QWidget * parent,bool daysonly)16 TimeUnitBox::TimeUnitBox(QWidget *parent, bool daysonly) : QWidget(parent)
17 {
18     QVBoxLayout *vlay = new QVBoxLayout(this);
19 
20     vlay->setContentsMargins(0, 0, 0, 0);
21     vlay->setSpacing(0);
22 
23     UpButton = new QToolButton(this);
24     UpButton->setArrowType(Qt::UpArrow);
25     UpButton->setMaximumWidth(26);
26     UpButton->setMaximumHeight(13);
27 
28     IncreaseAction = new QAction(QIcon::fromTheme("go-next-skip"),
29                                  i18n("Increase Time Scale"));
30     IncreaseAction->setToolTip(i18n("Increase time scale to the next largest unit"));
31     connect(IncreaseAction, SIGNAL(triggered()), this, SLOT(increase()));
32     UpButton->setDefaultAction(IncreaseAction);
33 
34     DownButton = new QToolButton(this);
35     DownButton->setArrowType(Qt::DownArrow);
36     DownButton->setMaximumWidth(26);
37     DownButton->setMaximumHeight(13);
38 
39     DecreaseAction = new QAction(QIcon::fromTheme("go-previous-skip"),
40                                  i18n("Decrease Time Scale"));
41     DecreaseAction->setToolTip(i18n("Decrease time scale to the next smallest unit"));
42     connect(DecreaseAction, SIGNAL(triggered()), this, SLOT(decrease()));
43     DownButton->setDefaultAction(DecreaseAction);
44 
45     vlay->addWidget(UpButton);
46     vlay->addWidget(DownButton);
47 
48     for (int &item : UnitStep)
49         item = 0;
50 
51     setDaysOnly(daysonly);
52 }
53 
setDaysOnly(bool daysonly)54 void TimeUnitBox::setDaysOnly(bool daysonly)
55 {
56     if (daysonly)
57     {
58         setMinimum(1 - TUB_DAYUNITS);
59         setMaximum(TUB_DAYUNITS - 1);
60         setValue(1); // Start out with days units
61 
62         UnitStep[0] = 0;
63         UnitStep[1] = 1;
64         UnitStep[2] = 5;
65         UnitStep[3] = 8;
66         UnitStep[4] = 14;
67     }
68     else
69     {
70         setMinimum(1 - TUB_ALLUNITS);
71         setMaximum(TUB_ALLUNITS - 1);
72         setValue(1); // Start out with seconds units
73 
74         UnitStep[0] = 0;
75         UnitStep[1] = 4;
76         UnitStep[2] = 10;
77         UnitStep[3] = 16;
78         UnitStep[4] = 21;
79         UnitStep[5] = 25;
80         UnitStep[6] = 28;
81         UnitStep[7] = 34;
82     }
83 }
84 
increase()85 void TimeUnitBox::increase()
86 {
87     if (value() < maxValue())
88     {
89         setValue(value() + 1);
90         emit valueChanged(value());
91         DecreaseAction->setEnabled(true);
92     }
93     else
94     {
95         IncreaseAction->setEnabled(false);
96     }
97 }
98 
decrease()99 void TimeUnitBox::decrease()
100 {
101     if (value() > minValue())
102     {
103         setValue(value() - 1);
104         emit valueChanged(value());
105         IncreaseAction->setEnabled(true);
106     }
107     else
108     {
109         DecreaseAction->setEnabled(false);
110     }
111 }
112 
unitValue()113 int TimeUnitBox::unitValue()
114 {
115     int uval;
116     if (value() >= 0)
117         uval = UnitStep[value()];
118     else
119         uval = -1 * UnitStep[abs(value())];
120     return uval;
121 }
122 
getUnitValue(int val)123 int TimeUnitBox::getUnitValue(int val)
124 {
125     if (val >= 0)
126         return UnitStep[val];
127     else
128         return -1 * UnitStep[abs(val)];
129 }
130