Skip to content

Commit

Permalink
generic: menu spinbox update
Browse files Browse the repository at this point in the history
Signed-off-by: IonutMuthi <[email protected]>
  • Loading branch information
IonutMuthi committed Nov 22, 2024
1 parent 1b78de3 commit 8a8132d
Show file tree
Hide file tree
Showing 18 changed files with 639 additions and 265 deletions.
144 changes: 144 additions & 0 deletions gui/include/gui/widgets/incrementstrategy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright (c) 2024 Analog Devices Inc.
*
* This file is part of Scopy
* (see https://www.github.com/analogdevicesinc/scopy).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

#ifndef INCREMENTSTRATEGY_H
#define INCREMENTSTRATEGY_H

#include <plot_utils.hpp>
#include <scopy-gui_export.h>

namespace scopy {
namespace gui {

class SCOPY_GUI_EXPORT IncrementStrategy
{
public:
virtual ~IncrementStrategy() {}
virtual double increment(double val) = 0;
virtual double decrement(double val) = 0;
virtual void setScale(double scale) = 0;
};

class SCOPY_GUI_EXPORT IncrementStrategy125 : public IncrementStrategy
{
public:
NumberSeries m_steps;

IncrementStrategy125()
: m_steps(1e-9, 1e9, 10)
{}
~IncrementStrategy125() {}
virtual double increment(double val) override { return m_steps.getNumberAfter(val); }
virtual double decrement(double val) override { return m_steps.getNumberBefore(val); }

double m_scale;
void setScale(double scale) override { m_scale = scale; }
};

class SCOPY_GUI_EXPORT IncrementStrategyPower2 : public IncrementStrategy
{
public:
QList<double> m_steps;
IncrementStrategyPower2()
{
for(int i = 30; i >= 0; i--) {
m_steps.append(-(1 << i));
}
for(int i = 0; i < 31; i++) {
m_steps.append(1 << i);
}
}
~IncrementStrategyPower2() {}
virtual double increment(double val) override
{
int i = 0;
val = val + 1;
while(val > m_steps[i]) {
i++;
}
return m_steps[i];
}
virtual double decrement(double val) override
{
int i = m_steps.count() - 1;
val = val - 1;
while(val < m_steps[i]) {
i--;
}
return m_steps[i];
}
double m_scale;

void setScale(double scale) override { m_scale = scale; }
};
class SCOPY_GUI_EXPORT IncrementStrategyFixed : public IncrementStrategy
{
public:
IncrementStrategyFixed(double k = 1)
{
m_k = k;
m_scale = 1;
}
~IncrementStrategyFixed() {}
virtual double increment(double val) override
{
val = val + m_k * m_scale;
return val;
}
virtual double decrement(double val) override
{
val = val - m_k * m_scale;
return val;
}
void setK(double val) { m_k = val; }
double k() { return m_k; }

private:
double m_k;
double m_scale;

void setScale(double scale) override { m_scale = scale; }
};

class SCOPY_GUI_EXPORT IncrementStrategyTest : public IncrementStrategy
{
public:
IncrementStrategyTest() { m_scale = 1; }
~IncrementStrategyTest() {}
virtual double increment(double val) override
{
val = val + m_scale;
return val;
}
virtual double decrement(double val) override
{
val = val - m_scale;
return val;
}

private:
double m_scale;

void setScale(double scale) override { m_scale = scale; }
};
} // namespace gui
} // namespace scopy
#endif // INCREMENTSTRATEGY_H
116 changes: 8 additions & 108 deletions gui/include/gui/widgets/menuspinbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,112 +28,16 @@
#include <cmath>
#include <scopy-gui_export.h>
#include <QWidget>
#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QLabel>
#include <QBoxLayout>
#include <incrementstrategy.h>
#include <scale.h>

namespace scopy {
namespace gui {

class SCOPY_GUI_EXPORT IncrementStrategy
{
public:
virtual ~IncrementStrategy(){};
virtual double increment(double val) = 0;
virtual double decrement(double val) = 0;
virtual void setScale(double scale) = 0;
};

class SCOPY_GUI_EXPORT IncrementStrategy125 : public IncrementStrategy
{
public:
NumberSeries m_steps;

IncrementStrategy125()
: m_steps(1e-9, 1e9, 10){};
~IncrementStrategy125(){};
virtual double increment(double val) override { return m_steps.getNumberAfter(val); }
virtual double decrement(double val) override { return m_steps.getNumberBefore(val); }

double m_scale;
void setScale(double scale) override { m_scale = scale; }
};

class SCOPY_GUI_EXPORT IncrementStrategyPower2 : public IncrementStrategy
{
public:
QList<double> m_steps;
IncrementStrategyPower2()
{
for(int i = 30; i >= 0; i--) {
m_steps.append(-(1 << i));
}
for(int i = 0; i < 31; i++) {
m_steps.append(1 << i);
}
};
~IncrementStrategyPower2(){};
virtual double increment(double val) override
{
int i = 0;
val = val + 1;
while(val > m_steps[i]) {
i++;
}
return m_steps[i];
}
virtual double decrement(double val) override
{
int i = m_steps.count() - 1;
val = val - 1;
while(val < m_steps[i]) {
i--;
}
return m_steps[i];
}
double m_scale;

void setScale(double scale) override { m_scale = scale; }
};
class SCOPY_GUI_EXPORT IncrementStrategyFixed : public IncrementStrategy
{
public:
IncrementStrategyFixed(double k = 1)
{
m_k = k;
m_scale = 1;
};
~IncrementStrategyFixed(){};
virtual double increment(double val) override
{
val = val + m_k * m_scale;
return val;
}
virtual double decrement(double val) override
{
val = val - m_k * m_scale;
return val;
}
void setK(double val) { m_k = val; }
double k() { return m_k; }

private:
double m_k;
double m_scale;

void setScale(double scale) override { m_scale = scale; }
};

class SCOPY_GUI_EXPORT UnitPrefix
{
public:
QString prefix;
double scale;
// enum type - metric, hour, logarithmic, etc
};

class SCOPY_GUI_EXPORT MenuSpinbox : public QWidget
{
Q_OBJECT
Expand All @@ -157,7 +61,8 @@ class SCOPY_GUI_EXPORT MenuSpinbox : public QWidget
IncrementStrategy *incrementStrategy() const;
QString name() const;

void setScaleRange(double min, double max);
Scale *scale() const;
void setScale(Scale *newScale);

public Q_SLOTS:
void setName(const QString &newName);
Expand All @@ -169,6 +74,7 @@ public Q_SLOTS:
void setValue(double newValue);
void setIncrementMode(IncrementMode is);
void setScalingEnabled(bool en);
bool scallingEnabled();

Q_SIGNALS:
void nameChanged(QString);
Expand All @@ -184,30 +90,24 @@ private Q_SLOTS:
void layoutVertically(bool left);
void layoutHorizontally(bool left);
double clamp(double val, double min, double max);
void minMaxReached(double val);

QLabel *m_label;
QLineEdit *m_edit;
QComboBox *m_scaleCb;
QPushButton *m_plus;
QPushButton *m_minus;
MouseWheelWidgetGuard *m_mouseWheelGuard;

IncrementStrategy *m_incrementStrategy;
IncrementMode m_im;

Scale *m_scale;

Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(double value READ value WRITE setValue NOTIFY valueChanged)
Q_PROPERTY(QString unit READ unit WRITE setUnit NOTIFY unitChanged)

QString m_name;
double m_value, m_min, m_max;
double m_scaleMin, m_scaleMax;
QString m_unit;

QList<UnitPrefix> m_scales;
// QMap<QString, double> m_scaleMap;
double getScaleForPrefix(QString prefix, Qt::CaseSensitivity s = Qt::CaseSensitive);
bool m_scalingEnabled;
};
} // namespace gui
} // namespace scopy
Expand Down
Loading

0 comments on commit 8a8132d

Please sign in to comment.