File indexing completed on 2024-04-21 14:44:45

0001 /*
0002     SPDX-FileCopyrightText: 2015 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "yaxistool.h"
0008 
0009 #include <QColorDialog>
0010 #include "Options.h"
0011 #include <kstars_debug.h>
0012 
0013 YAxisToolUI::YAxisToolUI(QWidget *p) : QFrame(p)
0014 {
0015     setupUi(this);
0016 }
0017 
0018 YAxisTool::YAxisTool(QWidget *w) : QDialog(w)
0019 {
0020 #ifdef Q_OS_OSX
0021     setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
0022 #endif
0023     ui = new YAxisToolUI(this);
0024 
0025     ui->setStyleSheet("QPushButton:checked { background-color: red; }");
0026 
0027     setWindowTitle(i18nc("@title:window", "Y-Axis Tool"));
0028 
0029     QVBoxLayout *mainLayout = new QVBoxLayout;
0030     mainLayout->addWidget(ui);
0031     setLayout(mainLayout);
0032 
0033     connect(ui->lowerLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
0034     connect(ui->upperLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
0035     connect(ui->autoLimitsCB, &QCheckBox::clicked, this, &YAxisTool::updateAutoValues);
0036     connect(ui->defaultB, &QPushButton::clicked, this, &YAxisTool::updateToDefaults);
0037     connect(ui->colorB, &QPushButton::clicked, this, &YAxisTool::updateColor);
0038     connect(ui->leftAxisCB, &QCheckBox::clicked, this, &YAxisTool::updateLeftAxis);
0039 }
0040 
0041 void YAxisTool::updateValues(const double value)
0042 {
0043     Q_UNUSED(value);
0044     YAxisInfo info = m_Info;
0045     info.axis->setRange(QCPRange(ui->lowerLimitSpin->value(), ui->upperLimitSpin->value()));
0046     info.rescale = ui->autoLimitsCB->isChecked();
0047     m_Info = info;
0048     updateSpins();
0049     emit axisChanged(m_Key, m_Info);
0050 }
0051 
0052 void YAxisTool::updateLeftAxis()
0053 {
0054     ui->leftAxisCB->setEnabled(!ui->leftAxisCB->isChecked());
0055     emit leftAxisChanged(m_Info.axis);
0056 }
0057 
0058 void YAxisTool::computeAutoLimits()
0059 {
0060     if (ui->autoLimitsCB->isChecked())
0061     {
0062         // The user checked the auto button.
0063         // Need to set the lower/upper spin boxes with the new auto limits.
0064         QCPAxis *axis = m_Info.axis;
0065         axis->rescale();
0066         axis->scaleRange(1.1, axis->range().center());
0067 
0068         disconnect(ui->lowerLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
0069         disconnect(ui->upperLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
0070         ui->lowerLimitSpin->setValue(axis->range().lower);
0071         ui->upperLimitSpin->setValue(axis->range().upper);
0072         connect(ui->lowerLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
0073         connect(ui->upperLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
0074     }
0075 }
0076 
0077 void YAxisTool::updateAutoValues()
0078 {
0079     computeAutoLimits();
0080     updateValues(0);
0081 }
0082 
0083 void YAxisTool::updateToDefaults()
0084 {
0085     m_Info.axis->setRange(m_Info.initialRange);
0086     m_Info.rescale = YAxisInfo::isRescale(m_Info.axis->range());
0087     m_Info.color = m_Info.initialColor;
0088     replot(ui->leftAxisCB->isChecked());
0089     computeAutoLimits();
0090     emit axisChanged(m_Key, m_Info);
0091 }
0092 
0093 void YAxisTool::replot(bool isLeftAxis)
0094 {
0095     // I was using editingFinished, but that signaled on change of focus.
0096     // ValueChanged/valueChanged can output on the outputSpins call.
0097     disconnect(ui->lowerLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
0098     disconnect(ui->upperLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
0099     ui->reset(m_Info);
0100     connect(ui->lowerLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
0101     connect(ui->upperLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
0102 
0103     ui->leftAxisCB->setChecked(isLeftAxis);
0104     ui->leftAxisCB->setEnabled(!isLeftAxis);
0105     updateSpins();
0106 
0107     ui->colorLabel->setText("");
0108     ui->colorLabel->setStyleSheet(QString("QLabel { background-color : %1; }").arg(m_Info.color.name()));
0109 }
0110 
0111 void YAxisTool::reset(QObject *key, const YAxisInfo &info, bool isLeftAxis)
0112 {
0113     m_Info = info;
0114     m_Key = key;
0115     replot(isLeftAxis);
0116 }
0117 
0118 void YAxisTool::updateColor()
0119 {
0120     QColor color = QColorDialog::getColor(m_Info.color, this);
0121     if (color.isValid())
0122     {
0123         ui->colorLabel->setStyleSheet(QString("QLabel { background-color : %1; }").arg(color.name()));
0124         m_Info.color = color;
0125         emit axisColorChanged(m_Key, m_Info, color);
0126     }
0127     return;
0128 }
0129 
0130 void YAxisTool::updateSpins()
0131 {
0132     ui->lowerLimitSpin->setEnabled(!m_Info.rescale);
0133     ui->upperLimitSpin->setEnabled(!m_Info.rescale);
0134     ui->lowerLimitLabel->setEnabled(!m_Info.rescale);
0135     ui->upperLimitLabel->setEnabled(!m_Info.rescale);
0136 }
0137 
0138 void YAxisToolUI::reset(const YAxisInfo &info)
0139 {
0140 
0141     statLabel->setText(info.name);
0142     statShortLabel->setText(info.shortName);
0143     lowerLimitSpin->setValue(info.axis->range().lower);
0144     upperLimitSpin->setValue(info.axis->range().upper);
0145     autoLimitsCB->setChecked(info.rescale);
0146 }
0147 
0148 // If the user hit's the 'X', still want to remove the live preview.
0149 void YAxisTool::closeEvent(QCloseEvent *event)
0150 {
0151     Q_UNUSED(event);
0152     slotClosed();
0153 }
0154 
0155 void YAxisTool::showEvent(QShowEvent *event)
0156 {
0157     Q_UNUSED(event);
0158 }
0159 
0160 
0161 void YAxisTool::slotClosed()
0162 {
0163 }
0164 
0165 void YAxisTool::slotSaveChanges()
0166 {
0167 }
0168