Kstars

yaxistool.cpp
1/*
2 SPDX-FileCopyrightText: 2015 Jasem Mutlaq <mutlaqja@ikarustech.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "yaxistool.h"
8
9#include <QColorDialog>
10#include "Options.h"
11#include <kstars_debug.h>
12
13YAxisToolUI::YAxisToolUI(QWidget *p) : QFrame(p)
14{
15 setupUi(this);
16}
17
18YAxisTool::YAxisTool(QWidget *w) : QDialog(w)
19{
20#ifdef Q_OS_OSX
21 setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
22#endif
23 ui = new YAxisToolUI(this);
24
25 ui->setStyleSheet("QPushButton:checked { background-color: red; }");
26
27 setWindowTitle(i18nc("@title:window", "Y-Axis Tool"));
28
29 QVBoxLayout *mainLayout = new QVBoxLayout;
30 mainLayout->addWidget(ui);
31 setLayout(mainLayout);
32
33 connect(ui->lowerLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
34 connect(ui->upperLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
35 connect(ui->autoLimitsCB, &QCheckBox::clicked, this, &YAxisTool::updateAutoValues);
36 connect(ui->defaultB, &QPushButton::clicked, this, &YAxisTool::updateToDefaults);
37 connect(ui->colorB, &QPushButton::clicked, this, &YAxisTool::updateColor);
38 connect(ui->leftAxisCB, &QCheckBox::clicked, this, &YAxisTool::updateLeftAxis);
39}
40
41void YAxisTool::updateValues(const double value)
42{
43 Q_UNUSED(value);
44 YAxisInfo info = m_Info;
45 info.axis->setRange(QCPRange(ui->lowerLimitSpin->value(), ui->upperLimitSpin->value()));
46 info.rescale = ui->autoLimitsCB->isChecked();
47 m_Info = info;
48 updateSpins();
49 emit axisChanged(m_Key, m_Info);
50}
51
52void YAxisTool::updateLeftAxis()
53{
54 ui->leftAxisCB->setEnabled(!ui->leftAxisCB->isChecked());
55 emit leftAxisChanged(m_Info.axis);
56}
57
58void YAxisTool::computeAutoLimits()
59{
60 if (ui->autoLimitsCB->isChecked())
61 {
62 // The user checked the auto button.
63 // Need to set the lower/upper spin boxes with the new auto limits.
64 QCPAxis *axis = m_Info.axis;
65 axis->rescale();
66 axis->scaleRange(1.1, axis->range().center());
67
68 disconnect(ui->lowerLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
69 disconnect(ui->upperLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
70 ui->lowerLimitSpin->setValue(axis->range().lower);
71 ui->upperLimitSpin->setValue(axis->range().upper);
72 connect(ui->lowerLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
73 connect(ui->upperLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
74 }
75}
76
77void YAxisTool::updateAutoValues()
78{
79 computeAutoLimits();
80 updateValues(0);
81}
82
83void YAxisTool::updateToDefaults()
84{
85 m_Info.axis->setRange(m_Info.initialRange);
86 m_Info.rescale = YAxisInfo::isRescale(m_Info.axis->range());
87 m_Info.color = m_Info.initialColor;
88 replot(ui->leftAxisCB->isChecked());
89 computeAutoLimits();
90 emit axisChanged(m_Key, m_Info);
91}
92
93void YAxisTool::replot(bool isLeftAxis)
94{
95 // I was using editingFinished, but that signaled on change of focus.
96 // ValueChanged/valueChanged can output on the outputSpins call.
97 disconnect(ui->lowerLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
98 disconnect(ui->upperLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
99 ui->reset(m_Info);
100 connect(ui->lowerLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
101 connect(ui->upperLimitSpin, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YAxisTool::updateValues);
102
103 ui->leftAxisCB->setChecked(isLeftAxis);
104 ui->leftAxisCB->setEnabled(!isLeftAxis);
105 updateSpins();
106
107 ui->colorLabel->setText("");
108 ui->colorLabel->setStyleSheet(QString("QLabel { background-color : %1; }").arg(m_Info.color.name()));
109}
110
111void YAxisTool::reset(QObject *key, const YAxisInfo &info, bool isLeftAxis)
112{
113 m_Info = info;
114 m_Key = key;
115 replot(isLeftAxis);
116}
117
118void YAxisTool::updateColor()
119{
120 QColor color = QColorDialog::getColor(m_Info.color, this);
121 if (color.isValid())
122 {
123 ui->colorLabel->setStyleSheet(QString("QLabel { background-color : %1; }").arg(color.name()));
124 m_Info.color = color;
125 emit axisColorChanged(m_Key, m_Info, color);
126 }
127 return;
128}
129
130void YAxisTool::updateSpins()
131{
132 ui->lowerLimitSpin->setEnabled(!m_Info.rescale);
133 ui->upperLimitSpin->setEnabled(!m_Info.rescale);
134 ui->lowerLimitLabel->setEnabled(!m_Info.rescale);
135 ui->upperLimitLabel->setEnabled(!m_Info.rescale);
136}
137
138void YAxisToolUI::reset(const YAxisInfo &info)
139{
140
141 statLabel->setText(info.name);
142 statShortLabel->setText(info.shortName);
143 lowerLimitSpin->setValue(info.axis->range().lower);
144 upperLimitSpin->setValue(info.axis->range().upper);
145 autoLimitsCB->setChecked(info.rescale);
146}
147
148// If the user hit's the 'X', still want to remove the live preview.
149void YAxisTool::closeEvent(QCloseEvent *event)
150{
152 slotClosed();
153}
154
155void YAxisTool::showEvent(QShowEvent *event)
156{
158}
159
160
161void YAxisTool::slotClosed()
162{
163}
164
165void YAxisTool::slotSaveChanges()
166{
167}
168
Manages a single axis inside a QCustomPlot.
void scaleRange(double factor)
void rescale(bool onlyVisiblePlottables=false)
Q_SLOT void setRange(const QCPRange &range)
Represents the range an axis is encompassing.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void clicked(bool checked)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
bool isValid() const const
QString name(NameFormat format) const const
QColor getColor(const QColor &initial, QWidget *parent, const QString &title, ColorDialogOptions options)
void valueChanged(double d)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setEnabled(bool)
virtual bool event(QEvent *event) override
void setStyleSheet(const QString &styleSheet)
Used to keep track of the various Y-axes and connect them to the QLineEdits.
Definition yaxistool.h:25
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:48:25 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.