Skip to content

Commit 23d03db

Browse files
committed
add: files
1 parent 2881e48 commit 23d03db

22 files changed

+1078
-0
lines changed

src/Ex1/iplot.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef IPLOT_H
2+
#define IPLOT_H
3+
4+
#include <QWidget>
5+
6+
class IPlotRandomWidget : public QWidget
7+
{
8+
public:
9+
IPlotRandomWidget(QWidget *parent)
10+
:QWidget(parent){}
11+
12+
//virtual ~IPlotRandomWidget(){}
13+
virtual void addPoint(double y) = 0;
14+
virtual void clear() = 0 ;
15+
};
16+
17+
#endif // IPLOT_H

src/Ex1/plotrandomwidget.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "plotrandomwidget.h"
2+
3+
PlotRandomWidget::PlotRandomWidget(QWidget* parent)
4+
:IPlotRandomWidget (parent)
5+
{
6+
QChartView *chartView = new QChartView(&_chart);
7+
_chart.addSeries(&_series);
8+
9+
QValueAxis *axisX = new QValueAxis;
10+
axisX->setRange(0, _xDefaultAxsisMaximum);
11+
QValueAxis *axisY = new QValueAxis;
12+
axisY->setRange(0, 4);
13+
//_chart.addAxis(axisX, Qt::AlignBottom);
14+
//_chart.addAxis(axisY, Qt::AlignLeft);
15+
_chart.setAxisX(axisX, &_series);
16+
_chart.setAxisY(axisY, &_series);
17+
_chart.legend()->hide();
18+
19+
QHBoxLayout *mainLayout = new QHBoxLayout(parent);
20+
mainLayout->addWidget(chartView);
21+
}
22+
23+
void PlotRandomWidget::addPoint(double y)
24+
{
25+
_series.append(_xCurrent, y);
26+
//если текущее значение по оси X стало больше, чем Xscale перерисуем шкалу
27+
if(_xCurrent >= _xCurrentAxsisMaximum)
28+
{
29+
_xCurrentAxsisMaximum = _xCurrent + _xDefaultAxsisMaximum;
30+
rescaleAxsisX(_xCurrentAxsisMaximum);
31+
}
32+
33+
const double xStep = 1;
34+
_xCurrent +=xStep;
35+
}
36+
37+
void PlotRandomWidget::clear()
38+
{
39+
_series.clear();
40+
_xCurrent = 0;
41+
42+
43+
_xCurrentAxsisMaximum = _xDefaultAxsisMaximum;
44+
rescaleAxsisX(_xCurrentAxsisMaximum);
45+
46+
}
47+
48+
void PlotRandomWidget::rescaleAxsisX(double value)
49+
{
50+
_chart.axisX()->setRange(0., value);
51+
}

src/Ex1/plotrandomwidget.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef PLOTRANDOMWIDGET_H
2+
#define PLOTRANDOMWIDGET_H
3+
4+
#include "iplot.h"
5+
#include <QtCharts>
6+
#include <QWidget>
7+
8+
9+
class PlotRandomWidget: public IPlotRandomWidget
10+
{
11+
public:
12+
PlotRandomWidget(QWidget* parent);
13+
void addPoint(double y) override;
14+
void clear() override;
15+
16+
private:
17+
void rescaleAxsisX(double value);
18+
19+
QtCharts::QLineSeries _series;
20+
QtCharts::QChart _chart;
21+
22+
double _xCurrent = 0;
23+
const double _xDefaultAxsisMaximum = 100;
24+
double _xCurrentAxsisMaximum = _xDefaultAxsisMaximum;
25+
};
26+
27+
#endif // PLOTRANDOMWIDGET_H

src/Ex1/plotrandomworker.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "plotrandomworker.h"
2+
#include <QThread>
3+
#include <QRandomGenerator>
4+
#include "random"
5+
6+
PlotRandomWorker::PlotRandomWorker(QPointer<IPlotRandomWidget> plot)
7+
:_plot(plot)
8+
{
9+
10+
}
11+
12+
void PlotRandomWorker::run()
13+
{
14+
forever
15+
{
16+
if (QThread::currentThread()->isInterruptionRequested())
17+
return;
18+
19+
if(!_plot.isNull())
20+
_plot->addPoint(generateRandom());
21+
22+
QThread::msleep(100);
23+
}
24+
}
25+
26+
27+
double PlotRandomWorker::generateRandom()
28+
{
29+
30+
std::uniform_real_distribution<> dist(1, 2.5);
31+
return dist(*QRandomGenerator::global());
32+
}

src/Ex1/plotrandomworker.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef PLOTRANDOMWORKER_H
2+
#define PLOTRANDOMWORKER_H
3+
4+
#include <QThread>
5+
#include <QPointer>
6+
#include "iplot.h"
7+
8+
class PlotRandomWorker : public QThread
9+
{
10+
Q_OBJECT
11+
12+
public:
13+
PlotRandomWorker(QPointer<IPlotRandomWidget> plot);
14+
void run() override;
15+
16+
private:
17+
static double generateRandom();
18+
QPointer<IPlotRandomWidget> _plot;
19+
};
20+
21+
#endif // PLOTRANDOMWORKER_H

src/Ex1/threadexercises.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "threadexercises.h"
2+
#include <QDebug>
3+
#include "plotrandomwidget.h"
4+
#include "plotrandomworker.h"
5+
6+
ThreadExercises::ThreadExercises(ThreadExercisesWidgets widgets, QObject *parent)
7+
:QObject(parent)
8+
{
9+
plot =new PlotRandomWidget(widgets.groupBox);
10+
11+
connect(widgets.playButton, &QPushButton::clicked, this, &ThreadExercises::play);
12+
connect(widgets.pauseButton, &QPushButton::clicked, this, &ThreadExercises::pause);
13+
connect(widgets.stopButton, &QPushButton::clicked, this, &ThreadExercises::stop);
14+
15+
}
16+
17+
ThreadExercises::~ThreadExercises()
18+
{
19+
if(!worker.isNull())
20+
{
21+
worker->requestInterruption();
22+
worker->wait();
23+
}
24+
qDebug()<<"ThreadExercises will be deleted";
25+
}
26+
27+
void ThreadExercises::play()
28+
{
29+
if(!worker.isNull())
30+
return;
31+
32+
worker = new PlotRandomWorker(plot);
33+
connect(worker, &PlotRandomWorker::finished, worker, &QObject::deleteLater);
34+
worker->start();
35+
}
36+
37+
void ThreadExercises::pause()
38+
{
39+
if(worker.isNull())
40+
return;
41+
42+
worker->requestInterruption();
43+
}
44+
45+
void ThreadExercises::stop()
46+
{
47+
if(!worker.isNull())
48+
pause();
49+
50+
plot->clear();
51+
}

src/Ex1/threadexercises.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef THREADEXERCISES_H
2+
#define THREADEXERCISES_H
3+
4+
#include <QObject>
5+
#include <QPushButton>
6+
#include <QGroupBox>
7+
#include <QPointer>
8+
#include "iplot.h"
9+
#include "plotrandomworker.h"
10+
11+
12+
struct ThreadExercisesWidgets
13+
{
14+
QPushButton* playButton = nullptr;
15+
QPushButton* pauseButton = nullptr;
16+
QPushButton* stopButton = nullptr;
17+
QGroupBox* groupBox = nullptr;
18+
};
19+
20+
class ThreadExercises: public QObject
21+
{
22+
Q_OBJECT
23+
24+
public:
25+
ThreadExercises(ThreadExercisesWidgets widgets, QObject *parent = nullptr);
26+
~ThreadExercises();
27+
28+
private:
29+
QPointer<IPlotRandomWidget> plot = nullptr;
30+
QPointer<PlotRandomWorker> worker = nullptr;
31+
32+
private slots:
33+
void play();
34+
void pause();
35+
void stop();
36+
};
37+
38+
#endif // THREADEXERCISES_H

src/Ex2/comboboxdelegate.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "comboboxdelegate.h"
2+
#include "customcell.h"
3+
#include <QComboBox>
4+
5+
ComboBoxDelegate::ComboBoxDelegate(const QList<CustomCell*>& cells, QObject *parent)
6+
:QStyledItemDelegate(parent),
7+
_cells(cells)
8+
{
9+
10+
}
11+
12+
QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
13+
{
14+
QComboBox* combobox = new QComboBox(parent);
15+
combobox->setEditable(false);
16+
return combobox;
17+
}
18+
19+
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
20+
{
21+
QComboBox* combobox = static_cast<QComboBox* >(editor);
22+
for(const auto& cell: _cells)
23+
combobox->addItem(cell->getName());
24+
25+
}
26+
27+
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
28+
{
29+
QComboBox* combobox = static_cast<QComboBox* >(editor);
30+
model->setData(index, combobox->currentText());
31+
}
32+
33+
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
34+
{
35+
editor->setGeometry(option.rect);
36+
}

src/Ex2/comboboxdelegate.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef COMBOBOXDELEGATE_H
2+
#define COMBOBOXDELEGATE_H
3+
4+
#include <QObject>
5+
#include <QStyledItemDelegate>
6+
7+
class CustomCell;
8+
9+
class ComboBoxDelegate: public QStyledItemDelegate
10+
{
11+
public:
12+
ComboBoxDelegate(const QList<CustomCell*>& cells, QObject* parent = nullptr);
13+
14+
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
15+
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
16+
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
17+
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
18+
19+
private:
20+
QList<CustomCell*> _cells;
21+
};
22+
23+
#endif // COMBOBOXDELEGATE_H

src/Ex2/customcell.cpp

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "customcell.h"
2+
3+
CustomCell::CustomCell(const QUuid& id,const QString& name, QObject *parent)
4+
:QObject(parent),
5+
_id(id),
6+
_name(name)
7+
{
8+
9+
}
10+
11+
QUuid CustomCell::getId() const
12+
{
13+
return _id;
14+
}
15+
16+
QString CustomCell::getName() const
17+
{
18+
return _name;
19+
}
20+
21+
void CustomCell::setId(const QUuid& id)
22+
{
23+
_id = id;
24+
}
25+
26+
void CustomCell::setName(const QString& name)
27+
{
28+
_name = name;
29+
}
30+
31+
32+
CustomRow::CustomRow(const QUuid &id, CustomCell *xCell, CustomCell *yCell, CustomCell *zCell, QObject *parent)
33+
:QObject(parent),
34+
_id(id),
35+
_xCell(xCell),
36+
_yCell(yCell),
37+
_zCell(zCell)
38+
{
39+
40+
}
41+
42+
QUuid CustomRow::getId() const
43+
{
44+
return _id;
45+
}
46+
47+
CustomCell *CustomRow::getCellX()
48+
{
49+
return _xCell;
50+
}
51+
52+
CustomCell *CustomRow::getCellY()
53+
{
54+
return _yCell;
55+
}
56+
57+
CustomCell *CustomRow::getCellZ()
58+
{
59+
return _zCell;
60+
}
61+
62+
void CustomRow::setId(const QUuid &id)
63+
{
64+
_id = id;
65+
}
66+
67+
void CustomRow::setCellX(CustomCell *xCell)
68+
{
69+
_xCell = xCell;
70+
}
71+
72+
void CustomRow::setCellY(CustomCell *yCell)
73+
{
74+
_yCell = yCell;
75+
}
76+
77+
void CustomRow::setCellZ(CustomCell *zCell)
78+
{
79+
_zCell = zCell;
80+
}

0 commit comments

Comments
 (0)