Kstars

timeunitbox.cpp
1 /*
2  SPDX-FileCopyrightText: 2002 Jason Harris <[email protected]>
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 
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 
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 
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 
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 
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 
124 {
125  if (val >= 0)
126  return UnitStep[val];
127  else
128  return -1 * UnitStep[abs(val)];
129 }
int getUnitValue(int)
the same as unitValue, except you can get the UnitStep for any value, not just the current one.
QIcon fromTheme(const QString &name)
void setArrowType(Qt::ArrowType type)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setMaximumWidth(int maxw)
int maxValue() const
Definition: timeunitbox.h:70
QString i18n(const char *text, const TYPE &arg...)
void setMinimum(int minValue)
Set the minimum value for the internal time-unit value.
Definition: timeunitbox.h:63
void setValue(int value)
Set the value which describes which time-unit is displayed in the TimeSpinBox.
Definition: timeunitbox.h:58
void setDefaultAction(QAction *action)
void setMaximumHeight(int maxh)
void setMaximum(int maxValue)
Set the maximum value for the internal time-unit value.
Definition: timeunitbox.h:65
void setToolTip(const QString &tip)
void setEnabled(bool)
void setSpacing(int spacing)
int value() const
Definition: timeunitbox.h:60
void setContentsMargins(int left, int top, int right, int bottom)
int minValue() const
Definition: timeunitbox.h:68
TimeUnitBox(QWidget *parent=nullptr, bool daysonly=false)
Constructor.
Definition: timeunitbox.cpp:16
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Aug 15 2022 04:04:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.