Skip to content

Commit

Permalink
plugins/dac/bufferdacaddon: initial version + read strategy for file
Browse files Browse the repository at this point in the history
Signed-off-by: AlexandraTrifan <Alexandra.Trifan@analog.com>
  • Loading branch information
AlexandraTrifan committed Jul 5, 2024
1 parent 4926651 commit 9ee73b4
Show file tree
Hide file tree
Showing 17 changed files with 725 additions and 30 deletions.
156 changes: 145 additions & 11 deletions plugins/dac/src/bufferdacaddon.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,173 @@
#include "bufferdacaddon.h"
#include "dacdatamodel.h"
#include "txnode.h"
#include "databufferbuilder.h"
#include "databuffer.h"
#include "filebrowser.h"
#include "dac_logging_categories.h"

#include <menusectionwidget.h>
#include <menucollapsesection.h>
#include <progresslineedit.h>
#include <titlespinbox.h>
#include <menulineedit.h>
#include <menuonoffswitch.h>
#include <menucontrolbutton.h>

#include <QHBoxLayout>
#include <QLabel>
#include <QFileDialog>
#include <QString>

using namespace scopy;
BufferDacAddon::BufferDacAddon(DacDataModel *model, QWidget *parent)
: DacAddon("blue", parent)
: DacAddon(parent)
, m_model(model)
, m_dataBuffer(nullptr)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
auto m_layout = new QHBoxLayout(this);
auto m_layout = new QVBoxLayout();
m_layout->setMargin(0);
m_layout->setSpacing(10);
setLayout(m_layout);

QLabel *name = new QLabel("BUFFER DAC ADDON", this);
m_layout->addWidget(name);
QWidget *bufferConfigContainer = new QWidget(this);
auto bufferConfigLay = new QHBoxLayout();
bufferConfigLay->setMargin(0);
bufferConfigLay->setSpacing(10);
bufferConfigContainer->setLayout(bufferConfigLay);

MenuSectionWidget *scaleContainer = new MenuSectionWidget(this);
TitleSpinBox *scaleSpin = new TitleSpinBox("Scale (DBFS)", this);
StyleHelper::BackgroundWidget(scaleSpin);
scaleContainer->contentLayout()->addWidget(scaleSpin);

MenuSectionWidget *buffersizeContainer = new MenuSectionWidget(this);
QLabel *buffersizeLbl = new QLabel("Buffersize");
MenuLineEdit *buffersizeEdit = new MenuLineEdit(this);
StyleHelper::MenuSmallLabel(buffersizeLbl);
StyleHelper::MenuLineEditWidget(buffersizeEdit);
buffersizeContainer->contentLayout()->addWidget(buffersizeLbl);
buffersizeContainer->contentLayout()->addWidget(buffersizeEdit);

MenuSectionWidget *cyclicContainer = new MenuSectionWidget(this);
MenuOnOffSwitch *cyclicBtn = new MenuOnOffSwitch("Cyclic", this);
cyclicContainer->contentLayout()->addWidget(cyclicBtn);

m_runBtn = new RunBtn(this);
connect(m_runBtn, &QPushButton::toggled, this, &BufferDacAddon::runBtnToggled);

bufferConfigLay->addWidget(scaleContainer);
bufferConfigLay->addWidget(buffersizeContainer);
bufferConfigLay->addWidget(cyclicContainer);
bufferConfigLay->addWidget(m_runBtn);

FileBrowser *fm = new FileBrowser(this);
connect(fm, &FileBrowser::load, this, &BufferDacAddon::load);

MenuSectionWidget *channelsSection = new MenuSectionWidget(this);
// channelsSection->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
QLabel *channelsLbl = new QLabel("Channels");
StyleHelper::MenuSmallLabel(channelsLbl);

QString color;
if (color == "") {
// is disabled
name->setText("DISABLED");
color = "grey";
QWidget *channelsContainer = new QWidget(this);
channelsContainer->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
QVBoxLayout *channelsContainerLayout = new QVBoxLayout();
channelsContainerLayout->setMargin(0);
channelsContainerLayout->setSpacing(0);
channelsContainer->setLayout(channelsContainerLayout);

QScrollArea *scrollArea = new QScrollArea(channelsSection);
// scrollArea->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
scrollArea->setWidgetResizable(true);
scrollArea->setWidget(channelsContainer);

channelsSection->contentLayout()->addWidget(channelsLbl);
channelsSection->contentLayout()->addWidget(scrollArea);

int i = 0;
auto lst = m_model->getBufferTxs();
for (TxNode *node : lst) {
MenuControlButton *chnBtn = new MenuControlButton(this);
chnBtn->setName(node->getUuid());
chnBtn->setCheckBoxStyle(MenuControlButton::CS_CIRCLE);
chnBtn->setColor(StyleHelper::getColor("CH" + QString::number(i)));
chnBtn->setDoubleClickToOpenMenu(true);
chnBtn->setOpenMenuChecksThis(true);
chnBtn->setCheckable(true);
chnBtn->setChecked(false);
channelsContainerLayout->addWidget(chnBtn);
i++;
}
for (TxNode *node : lst) {
MenuControlButton *chnBtn = new MenuControlButton(this);
chnBtn->setName(node->getUuid());
chnBtn->setCheckBoxStyle(MenuControlButton::CS_CIRCLE);
chnBtn->setColor(StyleHelper::getColor("CH" + QString::number(i)));
chnBtn->setDoubleClickToOpenMenu(true);
chnBtn->setOpenMenuChecksThis(true);
chnBtn->setCheckable(true);
chnBtn->setChecked(false);
channelsContainerLayout->addWidget(chnBtn);
i++;
}


setStyleSheet("background-color:" + color);
QWidget *topSection = new QWidget(this);
auto topLayout = new QHBoxLayout();
topSection->setLayout(topLayout);
topSection->setFixedHeight(190);
topLayout->setMargin(0);
topLayout->setSpacing(10);
topLayout->addWidget(fm, 3);
topLayout->addWidget(channelsSection, 2);

m_layout->addWidget(topSection);
m_layout->addWidget(bufferConfigContainer);
}

BufferDacAddon::~BufferDacAddon()
{

}

void BufferDacAddon::load(QString path)
{
if (m_dataBuffer) {
// handle deletion and cleanup
delete m_dataBuffer;
}
m_dataBuffer = DataBufferBuilder()
.dataStrategy(DataBufferBuilder::FileStrategy)
.file(path).ui(true).parent(this).build();


// test
loadData();
}

void BufferDacAddon::enable(bool enable)
{
m_model->enableBuffer(enable);
}

void BufferDacAddon::chooseFile()
{
QString selectedFilter;
m_filename = QFileDialog::getSaveFileName(
this, tr("Export"), "", tr("Comma-separated values files (*.csv);;All Files(*)"), &selectedFilter,
QFileDialog::Options(QFileDialog::DontUseNativeDialog));
m_fileBufferPath->getLineEdit()->setText(m_filename);
}

void BufferDacAddon::runBtnToggled(bool toggled)
{
// data buffer already configured buffersize and other stuff
m_model->push(m_dataBuffer->getDataBufferStrategy()->data());
}

void BufferDacAddon::loadData()
{
qDebug(CAT_DAC_BUFFER) << m_dataBuffer->getDataBufferStrategy()->data()[0];
}

//void BufferDacAddon::setupFileSection()
18 changes: 17 additions & 1 deletion plugins/dac/src/bufferdacaddon.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
#ifndef BUFFERDACADDON_H_
#define BUFFERDACADDON_H_

#include <progresslineedit.h>
#include <toolbuttons.h>

#include <QWidget>
#include "dacaddon.h"

namespace scopy {
class DacDataModel;
class DataBuffer;
class BufferDacAddon : public DacAddon
{
Q_OBJECT
public:
BufferDacAddon(DacDataModel *model, QWidget *parent=nullptr);
virtual ~BufferDacAddon();

virtual void enable(bool enable);
private Q_SLOTS:
void loadData();
void load(QString path);
private:
DacDataModel *m_model;
ProgressLineEdit *m_fileBufferPath;
QPushButton *m_fileBufferBrowseBtn;
QPushButton *m_fileBufferLoadBtn;
QString m_filename;
DataBuffer *m_dataBuffer;
RunBtn *m_runBtn;

void chooseFile();
void runBtnToggled(bool toggled);
};
} // namespace scopy

Expand Down
65 changes: 65 additions & 0 deletions plugins/dac/src/csvfilestrategy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "csvfilestrategy.h"
#include "dac_logging_categories.h"

#include <QString>
#include <QVector>
#include <QFile>

using namespace scopy;
CSVFileStrategy::CSVFileStrategy(QString filename, QWidget *parent)
: QWidget(parent)
{
m_filename = filename;
m_separator = ",";
loadFile();
// tbd thread?
}

QVector<QVector<double>> CSVFileStrategy::data()
{
return m_data;
}

void CSVFileStrategy::loadFile()
{
QVector<QVector<QString>> raw_data;
bool dataOk = true;
QFile file(m_filename);
if(!file.open(QIODevice::ReadOnly)) {
qDebug(CAT_DAC_DATASTRATEGY) << "Can't open selected file";
return;
}

QTextStream in(&file);

while(!in.atEnd()) {
QVector<QString> line_data;
QString line = in.readLine();
if (line.isEmpty()) {
continue;
}
QStringList list = line.split(m_separator, Qt::SkipEmptyParts);
for(const QString &list_item : qAsConst(list)) {
line_data.push_back(list_item);
}
if(line_data.size() > 0) {
raw_data.push_back(line_data);
}
}

// suppose no m_data in the file
m_data.resize(raw_data.size());
for(int i = 0; i < m_data.size(); ++i) {
m_data[i].resize(raw_data[i].size());
}

for(int i = 0; i < raw_data.size(); ++i) {
for(int j = 0; j < raw_data[i].size(); ++j) {
m_data[i][j] = raw_data[i][j].toDouble(&dataOk);
if(!dataOk) {
qDebug(CAT_DAC_DATASTRATEGY) << "File is corrupted";
return;
}
}
}
}
28 changes: 28 additions & 0 deletions plugins/dac/src/csvfilestrategy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef CSVFILESTRATEGY_H
#define CSVFILESTRATEGY_H

#include <QWidget>
#include "databufferstrategyinterface.h"
#include "scopy-dac_export.h"

namespace scopy {
class SCOPY_DAC_EXPORT CSVFileStrategy : public QWidget, public DataBufferStrategyInterface
{
Q_OBJECT
Q_INTERFACES(scopy::DataBufferStrategyInterface)
public:
explicit CSVFileStrategy(QString filename, QWidget *parent = nullptr);

QVector<QVector<double>> data() override;

Q_SIGNALS:
void loadFinished();

private:
QString m_filename;
QString m_separator;
QVector<QVector<double>> m_data;
void loadFile();
};
} //namespace scopy
#endif // CSVFILESTRATEGY_H
26 changes: 24 additions & 2 deletions plugins/dac/src/dacdatamanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ DacDataManager::DacDataManager(DacDataModel *model, QWidget *parent)
, m_model(model)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_layout = new QVBoxLayout(this);
m_layout = new QVBoxLayout();
m_layout->setMargin(0);
m_layout->setSpacing(10);
setLayout(m_layout);
Expand All @@ -27,14 +27,25 @@ DacDataManager::DacDataManager(DacDataModel *model, QWidget *parent)
headerLay->setMargin(10);
headerLay->setSpacing(0);

rightMenuStack = new MapStackedWidget(this);

dacAddonStack = new MapStackedWidget(this);

// QLabel *name = new QLabel(model->getName().toUpper(), this);
m_mode = new MenuCombo("MODE", this);
StyleHelper::IIOComboBox(m_mode->combo());
auto cb = m_mode->combo();
connect(cb, qOverload<int>(&QComboBox::currentIndexChanged), this, [=](int idx) {
auto current = dynamic_cast<DacAddon*>(dacAddonStack->currentWidget());
if (current) {
current->enable(false);
}

auto mode = cb->itemData(idx).toInt();
auto next = dynamic_cast<DacAddon*>(dacAddonStack->get(QString::number(mode)));
if (next) {
next->enable(true);
}
dacAddonStack->show(QString::number(mode));
});

Expand Down Expand Up @@ -109,10 +120,21 @@ void DacDataManager::setupDacMode(QString mode_name, unsigned int mode)
dac = new BufferDacAddon(m_model, this);
break;
default:
dac = new DacAddon("green", this);
dac = new DacAddon(this);
break;
}
m_dacAddons.append(dac);
dacAddonStack->add(QString::number(mode), dac);
m_mode->combo()->addItem(mode_name, mode);
}


MapStackedWidget *DacDataManager::getRightMenuStack() const
{
return rightMenuStack;
}

QList<QPushButton*> DacDataManager::getMenuControlBtns() const
{
return m_menuControlBtns;
}
Loading

0 comments on commit 9ee73b4

Please sign in to comment.