Skip to content

Commit

Permalink
wip: demo for menu spinbox in datalogger menu
Browse files Browse the repository at this point in the history
Signed-off-by: IonutMuthi <[email protected]>
  • Loading branch information
IonutMuthi committed Nov 15, 2024
1 parent e504773 commit 88f660c
Show file tree
Hide file tree
Showing 8 changed files with 789 additions and 102 deletions.
123 changes: 123 additions & 0 deletions gui/include/gui/widgets/incrementstrategy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#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
77 changes: 77 additions & 0 deletions gui/include/gui/widgets/scale.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#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
74 changes: 74 additions & 0 deletions gui/include/gui/widgets/testspinbox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#ifndef TESTSPINBOX_H
#define TESTSPINBOX_H

#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QWidget>
#include <incrementstrategy.h>
#include <mousewheelwidgetguard.h>
#include <scale.h>
#include <scopy-gui_export.h>

namespace scopy {
namespace gui {

class SCOPY_GUI_EXPORT TestSpinbox : public QWidget
{
Q_OBJECT
public:
typedef enum
{
IS_POW2,
IS_125,
IS_FIXED

} IncrementMode;

explicit TestSpinbox(QString name, double val, QString unit, double min, double max, bool vertical = 0,
bool left = 0, QWidget *parent = nullptr);

IncrementStrategy *incrementStrategy() const;

double value() const;

Scale *scale() const;
void setScale(Scale *newScale);
void setScalingEnabled(bool en);
bool scallingEnabled();

public Q_SLOTS:
void setIncrementMode(IncrementMode im);
void setValue(double newValue);
void setValueForce(double newValue, bool force = true);

Q_SIGNALS:
void nameChanged(QString);
void valueChanged(double);
void unitChanged(QString);

private:
void layoutVertically(bool left);
void layoutHorizontally(bool left);
void updateWidgetsVal();
double clamp(double val, double min, double max);

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

double m_value, m_min, m_max;

IncrementStrategy *m_incrementStrategy;
IncrementMode m_im;

Scale *m_scale;

int findLastDigit(QString str);
double userInput(QString s);
};
} // namespace gui
} // namespace scopy
#endif // TESTSPINBOX_H
Loading

0 comments on commit 88f660c

Please sign in to comment.