Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: demo for menu spinbox in datalogger menu #1826

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
95 changes: 2 additions & 93 deletions gui/include/gui/widgets/menuspinbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "plot_utils.hpp"
#include "utils.h"
#include "mousewheelwidgetguard.h"
#include "scale.h"
#include <cmath>
#include <scopy-gui_export.h>
#include <QWidget>
Expand All @@ -33,103 +34,11 @@
#include <QPushButton>
#include <QLabel>
#include <QBoxLayout>
#include <incrementstrategy.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; };
~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 Down
98 changes: 98 additions & 0 deletions gui/include/gui/widgets/scale.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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 SCALE_H
#define SCALE_H

#include <QComboBox>
#include <QList>
#include <QString>
#include <scopy-gui_export.h>
#include <QObject>

namespace scopy {
namespace gui {

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

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

class SCOPY_GUI_EXPORT Scale : public QObject
{
Q_OBJECT
public:
Scale(QString unit, double min, double max, bool hasPrefix = true);
~Scale();

double getScaleForPrefix(QString prefix, Qt::CaseSensitivity s);
double getScaleForUnit(QString unit, Qt::CaseSensitivity s);
double getScaleForSymbol(QString symbol);

QList<UnitPrefix> scalePrefixes() const;
void setScalePrefixes(const QList<UnitPrefix> &newScalePrefixes);

QList<ScaleOption> scaleOptions() const;
void setScaleOptions(const QList<ScaleOption> &newScaleOptions);

bool scalingEnabled() const;
void setScalingEnabled(bool newScalingEnabled);

QComboBox *scaleCb() const;

QString unit() const;
void setUnit(const QString &newUnit);

void computeScale(double val);

bool hasPrefix() const;
void setHasPrefix(bool newHasPrefix);

Q_SIGNALS:
void scaleUpdated();
void unitChanged(QString unit);
void scaleDown(int newScaleIndex);

private:
bool m_hasPrefix;
bool m_scalingEnabled = true;
double m_min, m_max;
QString m_unit;
QList<ScaleOption> m_scaleOptions;
QList<UnitPrefix> m_scalePrefixes;
QComboBox *m_scaleCb;
void populateScaleCb();
};

} // namespace gui
} // namespace scopy
#endif // SCALE_H
Loading
Loading