AnalyzerBase: Refactor code

This commit is contained in:
Jonas Kvinge 2022-04-01 22:30:00 +02:00
parent 58eec8df1e
commit f471462a84
3 changed files with 33 additions and 26 deletions

View File

@ -32,10 +32,11 @@
#include <QVector>
#include <QPainter>
#include <QPalette>
#include <QBasicTimer>
#include <QShowEvent>
#include <QHideEvent>
#include <QTimerEvent>
#include <QtEvents>
#include "core/logging.h"
#include "engine/enginebase.h"
// INSTRUCTIONS Base2D
@ -51,16 +52,34 @@
Analyzer::Base::Base(QWidget *parent, const uint scopeSize)
: QWidget(parent),
timeout_(40),
fht_(new FHT(scopeSize)),
engine_(nullptr),
lastscope_(512),
new_frame_(false),
is_playing_(false) {}
is_playing_(false),
timeout_(40) {}
void Analyzer::Base::hideEvent(QHideEvent*) { timer_.stop(); }
Analyzer::Base::~Base() {
delete fht_;
}
void Analyzer::Base::showEvent(QShowEvent*) { timer_.start(timeout(), this); }
void Analyzer::Base::showEvent(QShowEvent*) {
timer_.start(timeout(), this);
}
void Analyzer::Base::hideEvent(QHideEvent*) {
timer_.stop();
}
void Analyzer::Base::ChangeTimeout(const int timeout) {
timeout_ = timeout;
if (timer_.isActive()) {
timer_.stop();
timer_.start(timeout_, this);
}
}
void Analyzer::Base::transform(Scope &scope) {
@ -186,10 +205,6 @@ void Analyzer::Base::demo(QPainter &p) {
}
void Analyzer::Base::polishEvent() {
init();
}
void Analyzer::interpolate(const Scope &inVec, Scope &outVec) {
double pos = 0.0;

View File

@ -44,8 +44,8 @@
class QHideEvent;
class QShowEvent;
class QTimerEvent;
class QPaintEvent;
class QTimerEvent;
namespace Analyzer {
@ -55,19 +55,13 @@ class Base : public QWidget {
Q_OBJECT
public:
~Base() override { delete fht_; }
~Base() override;
int timeout() const { return timeout_; }
void set_engine(EngineBase *engine) { engine_ = engine; }
void changeTimeout(int newTimeout) {
timeout_ = newTimeout;
if (timer_.isActive()) {
timer_.stop();
timer_.start(timeout_, this);
}
}
void ChangeTimeout(const int timeout);
virtual void framerateChanged() {}
@ -76,10 +70,8 @@ class Base : public QWidget {
void hideEvent(QHideEvent*) override;
void showEvent(QShowEvent*) override;
void paintEvent(QPaintEvent*) override;
void timerEvent(QTimerEvent*) override;
void polishEvent();
void paintEvent(QPaintEvent *e) override;
void timerEvent(QTimerEvent *e) override;
int resizeExponent(int);
int resizeForBands(const int);
@ -90,13 +82,13 @@ class Base : public QWidget {
protected:
QBasicTimer timer_;
int timeout_;
FHT *fht_;
EngineBase *engine_;
Scope lastscope_;
bool new_frame_;
bool is_playing_;
int timeout_;
};
void interpolate(const Scope&, Scope&);

View File

@ -153,7 +153,7 @@ void AnalyzerContainer::ChangeAnalyzer(const int id) {
current_analyzer_->set_engine(engine_);
// Even if it is not supposed to happen, I don't want to get a dbz error
current_framerate_ = current_framerate_ == 0 ? kMediumFramerate : current_framerate_;
current_analyzer_->changeTimeout(1000 / current_framerate_);
current_analyzer_->ChangeTimeout(1000 / current_framerate_);
layout()->addWidget(current_analyzer_);
@ -166,7 +166,7 @@ void AnalyzerContainer::ChangeFramerate(int new_framerate) {
if (current_analyzer_) {
// Even if it is not supposed to happen, I don't want to get a dbz error
new_framerate = new_framerate == 0 ? kMediumFramerate : new_framerate;
current_analyzer_->changeTimeout(1000 / new_framerate);
current_analyzer_->ChangeTimeout(1000 / new_framerate);
// notify the current analyzer that the framerate has changed
current_analyzer_->framerateChanged();