File indexing completed on 2024-04-21 14:47:03

0001 /*
0002     SPDX-FileCopyrightText: 2002 Jason Harris <kstars@30doradus.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "timeunitbox.h"
0008 
0009 #include <QToolButton>
0010 #include <QVBoxLayout>
0011 
0012 #include <KLocalizedString>
0013 
0014 #define TUB_DAYUNITS 5
0015 
0016 TimeUnitBox::TimeUnitBox(QWidget *parent, bool daysonly) : QWidget(parent)
0017 {
0018     QVBoxLayout *vlay = new QVBoxLayout(this);
0019 
0020     vlay->setContentsMargins(0, 0, 0, 0);
0021     vlay->setSpacing(0);
0022 
0023     UpButton = new QToolButton(this);
0024     UpButton->setArrowType(Qt::UpArrow);
0025     UpButton->setMaximumWidth(26);
0026     UpButton->setMaximumHeight(13);
0027 
0028     IncreaseAction = new QAction(QIcon::fromTheme("go-next-skip"),
0029                                  i18n("Increase Time Scale"));
0030     IncreaseAction->setToolTip(i18n("Increase time scale to the next largest unit"));
0031     connect(IncreaseAction, SIGNAL(triggered()), this, SLOT(increase()));
0032     UpButton->setDefaultAction(IncreaseAction);
0033 
0034     DownButton = new QToolButton(this);
0035     DownButton->setArrowType(Qt::DownArrow);
0036     DownButton->setMaximumWidth(26);
0037     DownButton->setMaximumHeight(13);
0038 
0039     DecreaseAction = new QAction(QIcon::fromTheme("go-previous-skip"),
0040                                  i18n("Decrease Time Scale"));
0041     DecreaseAction->setToolTip(i18n("Decrease time scale to the next smallest unit"));
0042     connect(DecreaseAction, SIGNAL(triggered()), this, SLOT(decrease()));
0043     DownButton->setDefaultAction(DecreaseAction);
0044 
0045     vlay->addWidget(UpButton);
0046     vlay->addWidget(DownButton);
0047 
0048     for (int &item : UnitStep)
0049         item = 0;
0050 
0051     setDaysOnly(daysonly);
0052 }
0053 
0054 void TimeUnitBox::setDaysOnly(bool daysonly)
0055 {
0056     if (daysonly)
0057     {
0058         setMinimum(1 - TUB_DAYUNITS);
0059         setMaximum(TUB_DAYUNITS - 1);
0060         setValue(1); // Start out with days units
0061 
0062         UnitStep[0] = 0;
0063         UnitStep[1] = 1;
0064         UnitStep[2] = 5;
0065         UnitStep[3] = 8;
0066         UnitStep[4] = 14;
0067     }
0068     else
0069     {
0070         setMinimum(1 - TUB_ALLUNITS);
0071         setMaximum(TUB_ALLUNITS - 1);
0072         setValue(1); // Start out with seconds units
0073 
0074         UnitStep[0] = 0;
0075         UnitStep[1] = 4;
0076         UnitStep[2] = 10;
0077         UnitStep[3] = 16;
0078         UnitStep[4] = 21;
0079         UnitStep[5] = 25;
0080         UnitStep[6] = 28;
0081         UnitStep[7] = 34;
0082     }
0083 }
0084 
0085 void TimeUnitBox::increase()
0086 {
0087     if (value() < maxValue())
0088     {
0089         setValue(value() + 1);
0090         emit valueChanged(value());
0091         DecreaseAction->setEnabled(true);
0092     }
0093     else
0094     {
0095         IncreaseAction->setEnabled(false);
0096     }
0097 }
0098 
0099 void TimeUnitBox::decrease()
0100 {
0101     if (value() > minValue())
0102     {
0103         setValue(value() - 1);
0104         emit valueChanged(value());
0105         IncreaseAction->setEnabled(true);
0106     }
0107     else
0108     {
0109         DecreaseAction->setEnabled(false);
0110     }
0111 }
0112 
0113 int TimeUnitBox::unitValue()
0114 {
0115     int uval;
0116     if (value() >= 0)
0117         uval = UnitStep[value()];
0118     else
0119         uval = -1 * UnitStep[abs(value())];
0120     return uval;
0121 }
0122 
0123 int TimeUnitBox::getUnitValue(int val)
0124 {
0125     if (val >= 0)
0126         return UnitStep[val];
0127     else
0128         return -1 * UnitStep[abs(val)];
0129 }