Skip to content

Commit

Permalink
[Example Project] Backend class
Browse files Browse the repository at this point in the history
  • Loading branch information
the-mixtape committed Sep 7, 2022
1 parent cd506a1 commit f9b6596
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 12 deletions.
60 changes: 60 additions & 0 deletions ExampleProject/Backend/ExampleBackend.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "ExampleBackend.h"

#include <QThread>
#include <QDebug>


ExampleBackend::ExampleBackend(QObject* parent)
:QThread(parent)
{
connect(this, &QThread::finished, [=]() { deleteLater(); });
}

ExampleBackend::~ExampleBackend()
{
}

void ExampleBackend::stopAndClear()
{
quit();
wait();
}

void ExampleBackend::quit()
{
bIsQuit = true;
generateNewData.unlock();

QThread::quit();
}

void ExampleBackend::copyingCompleted()
{
generateNewData.unlock();
}

void ExampleBackend::run()
{
int size = 512;
double* data = new double[size];
double d = 0;
bIsQuit = false;

while(!bIsQuit)
{
generateNewData.lock();

if (bIsQuit) break;

d = (rand() % 4 + 2);
for(int i = 0; i < size; i++)
{
data[i] = sin(i / (size/d)) * 100.0;
}

emit generatedNewData(data, size);
}

generateNewData.unlock();
delete data;
}
30 changes: 30 additions & 0 deletions ExampleProject/Backend/ExampleBackend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include <QMutex>
#include <QThread>

class ExampleBackend : public QThread
{
Q_OBJECT

public:
ExampleBackend(QObject* parent = nullptr);
~ExampleBackend();

void stopAndClear();
void quit();

signals:
void generatedNewData(double* data, int size);

public slots:
void copyingCompleted();

protected:
void run() override;

private:
QMutex generateNewData;
bool bIsQuit;

};

4 changes: 4 additions & 0 deletions ExampleProject/ExampleProject.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Backend\ExampleBackend.cpp" />
<ClCompile Include="\\192.168.132.15\share\QtPlot\ExampleProject\Frontend\MainWindow\MainWindow.cpp" />
<QtRcc Include="ExampleProject.qrc" />
<ClCompile Include="main.cpp" />
Expand All @@ -121,6 +122,9 @@
<ItemGroup>
<QtUic Include="\\192.168.132.15\share\QtPlot\ExampleProject\Frontend\MainWindow\MainWindow.ui" />
</ItemGroup>
<ItemGroup>
<QtMoc Include="Backend\ExampleBackend.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">
<Import Project="$(QtMsBuild)\qt.targets" />
Expand Down
12 changes: 12 additions & 0 deletions ExampleProject/ExampleProject.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
<Filter Include="Header Files\Frontend">
<UniqueIdentifier>{c35943ba-525b-4101-b762-ae4109c92804}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Backend">
<UniqueIdentifier>{0f712421-3941-42c2-b5d3-7ca2aec510a1}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Backend">
<UniqueIdentifier>{1a055f30-a1ef-4d4e-83f6-9ee2afa1c182}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<QtRcc Include="ExampleProject.qrc">
Expand All @@ -40,6 +46,9 @@
<ClCompile Include="\\192.168.132.15\share\QtPlot\ExampleProject\Frontend\MainWindow\MainWindow.cpp">
<Filter>Source Files\Frontend</Filter>
</ClCompile>
<ClCompile Include="Backend\ExampleBackend.cpp">
<Filter>Source Files\Backend</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<QtUic Include="\\192.168.132.15\share\QtPlot\ExampleProject\Frontend\MainWindow\MainWindow.ui">
Expand All @@ -50,5 +59,8 @@
<QtMoc Include="\\192.168.132.15\share\QtPlot\ExampleProject\Frontend\MainWindow\MainWindow.h">
<Filter>Header Files\Frontend</Filter>
</QtMoc>
<QtMoc Include="Backend\ExampleBackend.h">
<Filter>Header Files\Backend</Filter>
</QtMoc>
</ItemGroup>
</Project>
15 changes: 13 additions & 2 deletions ExampleProject/Frontend/MainWindow/MainWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "MainWindow.h"

#include "Plot/QtPlot.h"
#include "Waterfall/WaterfallContent.h"


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
Expand All @@ -9,10 +11,16 @@ MainWindow::MainWindow(QWidget *parent)

initializePlot();
initializeWaterfall();

backend.start();

connect(&backend, &ExampleBackend::generatedNewData, [=](double* data, int size) { ui.waterfallPlot->appendData(data, size); });
connect(ui.waterfallPlot, &WaterfallPlot::copyingCompleted, &backend, &ExampleBackend::copyingCompleted);
}

MainWindow::~MainWindow()
{}
{
}

void MainWindow::initializePlot()
{
Expand Down Expand Up @@ -118,7 +126,10 @@ void MainWindow::initializeWaterfall()

ui.waterfallPlot->setBackground(Qt::black);
ui.waterfallPlot->setFillColor(Qt::black);
ui.waterfallPlot->setAppendHeight(5);
ui.waterfallPlot->setAppendHeight(3);
ui.waterfallPlot->setAppendSide(EAS_Top);
ui.waterfallPlot->setResolution(512, 512);
ui.waterfallPlot->setFPSLimit(30);

QCPTextElement* chartSpectrRuntimeText = new QCPTextElement(ui.waterfallPlot);
chartSpectrRuntimeText->setText("Waterfall");
Expand Down
8 changes: 8 additions & 0 deletions ExampleProject/Frontend/MainWindow/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

#include <QtWidgets/QMainWindow>
#include "ui_MainWindow.h"
#include "Backend/ExampleBackend.h"


class ExampleBackend;


class MainWindow : public QMainWindow
{
Expand All @@ -15,6 +20,9 @@ class MainWindow : public QMainWindow
void initializePlot();
void initializeWaterfall();

signals:

private:
Ui::MainWindowClass ui;
ExampleBackend backend;
};
12 changes: 8 additions & 4 deletions ExampleProject/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@

#include <QApplication>
#include "Frontend/MainWindow/MainWindow.h"
#include "Backend/ExampleBackend.h"


int main(int argc, char *argv[])
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
QApplication app(argc, argv);
MainWindow w;

w.setWindowTitle("QtPlot Example");
w.show();
return a.exec();

return app.exec();
}
2 changes: 1 addition & 1 deletion QtPlot/QtPlot.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<ClInclude Include="WfColorMap.h" />
<ClInclude Include="Interval.h" />
<ClInclude Include="Library\EnumLibrary.h" />
<ClInclude Include="Waterfall\Waterfall.h" />
<QtMoc Include="Waterfall\Waterfall.h" />
<QtMoc Include="Waterfall\WaterfallContent.h" />
<QtMoc Include="Waterfall\WaterfallLayer.h" />
<QtMoc Include="Waterfall\WaterfallThread.h" />
Expand Down
6 changes: 3 additions & 3 deletions QtPlot/QtPlot.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@
<ClInclude Include="Library\QtPlotMathLibrary.h">
<Filter>Header Files\Library</Filter>
</ClInclude>
<ClInclude Include="Waterfall\Waterfall.h">
<Filter>Header Files\Waterfall</Filter>
</ClInclude>
<ClInclude Include="WfColorMap.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down Expand Up @@ -135,5 +132,8 @@
<QtMoc Include="Waterfall\WaterfallLayer.h">
<Filter>Header Files\Waterfall</Filter>
</QtMoc>
<QtMoc Include="Waterfall\Waterfall.h">
<Filter>Header Files\Waterfall</Filter>
</QtMoc>
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions QtPlot/Waterfall/Waterfall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ WaterfallPlot::WaterfallPlot(QWidget* parent)
loadThread->stopAndClear();
loadThread->setWaterfallContent(content);
loadThread->start();

connect(loadThread, &WaterfallThread::copyingCompleted, [=]() { emit copyingCompleted(); });
}

WaterfallPlot::~WaterfallPlot()
Expand Down
5 changes: 5 additions & 0 deletions QtPlot/Waterfall/Waterfall.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class WfColorMap;

class QTPLOT_EXPORT WaterfallPlot : public QtPlot
{
Q_OBJECT

public:
explicit WaterfallPlot(QWidget* parent);
~WaterfallPlot() override;
Expand All @@ -27,6 +29,9 @@ class QTPLOT_EXPORT WaterfallPlot : public QtPlot
void setInterval(int minval, int maxval) const;
void setFillColor(const QColor& fillColor) const;

signals:
void copyingCompleted();

private:
WaterfallThread* loadThread;
WaterfallContent* content;
Expand Down
4 changes: 2 additions & 2 deletions QtPlot/Waterfall/WaterfallThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void WaterfallThread::run()

if (bIsQuit) return;

if (content)
if (content)
{
content->append(data, size);
emit update();
Expand Down Expand Up @@ -99,7 +99,7 @@ void WaterfallThread::addData(double* inData, int inSize)

if(data)
{
memcpy(inData, data, size);
memcpy(data, inData, size * sizeof(double));
}

emit copyingCompleted();
Expand Down

0 comments on commit f9b6596

Please sign in to comment.