Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions OpticalExperiment.pro
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ SOURCES += \
ui/commandhandlerchart.cpp \
ui/chartview.cpp \
ui/parametersmanager.cpp \
utility/constructorserializer.cpp
utility/constructorserializer.cpp \
ui/commandviewmanager.cpp

HEADERS += \
command/command.h \
Expand Down Expand Up @@ -94,7 +95,8 @@ HEADERS += \
ui/commandhandlerchart.h \
ui/chartview.h \
ui/parametersmanager.h \
utility/constructorserializer.h
utility/constructorserializer.h \
ui/commandviewmanager.h

FORMS += \
ui/mainwindow.ui
Expand Down
1 change: 0 additions & 1 deletion deviceconfigs/commandhadlermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class CommandHadlerModel : CommandHandler, public Singleton<CommandHadlerModel>
bool deleteConnection(pCommand cmnd);
bool changeVariables(pCommand cmnd);
bool refreshItem(pCommand cmnd);

};

#endif // COMANDHADLERMODEL_H
62 changes: 13 additions & 49 deletions ui/commandhandlerview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
#include "genericitem.h"
#include "connectionitem.h"

#include "ui/commandviewmanager.h"

CommandHandlerView::CommandHandlerView()
: CommandHandler(nullptr),
Singleton<CommandHandlerView>(*this) {}

void CommandHandlerView::setScene(QGraphicsScene *scene) {
_scene = scene;
}

//ToDo: Вполне возможно, что нужно перенести этот метод в CommandHandler
bool CommandHandlerView::handle(std::shared_ptr<Command> cmnd) {
Expand All @@ -34,16 +33,6 @@ bool CommandHandlerView::handle(std::shared_ptr<Command> cmnd) {
}
}

QPointF CommandHandlerView::getDevicePos(int id) {
auto item = findItemWithId(id);

if (!item) {
throw std::logic_error("can't find device with id");
}

return item->pos();
}

bool CommandHandlerView::addItem(std::shared_ptr<Command> cmnd) {
ConstructorItem *newDevice = nullptr;

Expand All @@ -56,69 +45,51 @@ bool CommandHandlerView::addItem(std::shared_ptr<Command> cmnd) {
cmnd->data.ad.typeItemId);
}

_scene->addItem(newDevice);

_devices.push_back(newDevice);
CommandViewManager::i()->addItemToScene(newDevice);
CommandViewManager::i()->addDevice(newDevice);

return true;
}

bool CommandHandlerView::addConnection(std::shared_ptr<Command> cmnd) {
auto source = findItemWithId(cmnd->data.ac.sourceId);
auto dest = findItemWithId(cmnd->data.ac.destId);
auto source = CommandViewManager::i()->findItemWithId(cmnd->data.ac.sourceId);
auto dest = CommandViewManager::i()->findItemWithId(cmnd->data.ac.destId);

if (!source || !dest) {
if (source == nullptr || dest == nullptr) {
return false;
}

auto v1 = source->getOutput(cmnd->data.ac.sourceNum);
auto v2 = dest->getInput(cmnd->data.ac.destNum);

_scene->addItem(new ConnectionItem(v1, v2));
CommandViewManager::i()->addItemToScene(new ConnectionItem(v1, v2));

/* ToDo: Точно ли так надо возвращать?*/
return true;
}

bool CommandHandlerView::removeItem(std::shared_ptr<Command> cmnd) {
auto iter = std::find_if(_devices.begin(), _devices.end(),
[cmnd](ConstructorItem *item)
{ return item->getId() == cmnd->data.dd.id; });

if (iter == _devices.end()) {
return false;
}

delete *iter;
_devices.erase(iter);

return true;
return CommandViewManager::i()->removeDevice(cmnd->data.dd.id);
}

bool CommandHandlerView::removeConnection(std::shared_ptr<Command> cmnd) {
auto vertex = findItemWithId(cmnd->data.dc.sourceId);
auto vertex = CommandViewManager::i()->findItemWithId(cmnd->data.dc.sourceId);

if (!vertex) {
// что-то поломалось
if (vertex == nullptr)
return false;
}

auto out = vertex->getOutput(cmnd->data.dc.sourceNum);

delete out->getConnection();

return true;
}

bool CommandHandlerView::changeVariables(std::shared_ptr<Command> cmnd) {
auto device = findItemWithId(cmnd->data.cv.id);
auto device = CommandViewManager::i()->findItemWithId(cmnd->data.cv.id);

if (!device) {
if (device == nullptr)
return false;
}

QMap<QString, double> properties;

for (const auto &i: cmnd->varList) {
properties[i.first.c_str()] = i.second;
}
Expand All @@ -127,10 +98,3 @@ bool CommandHandlerView::changeVariables(std::shared_ptr<Command> cmnd) {

return true;
}

ConstructorItem *CommandHandlerView::findItemWithId(int id) {
auto iter = std::find_if(_devices.begin(), _devices.end(),
[id](ConstructorItem *item){ return item->getId() == id; });

return iter != _devices.end() ? *iter : nullptr;
}
20 changes: 3 additions & 17 deletions ui/commandhandlerview.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#ifndef COMANDHANDLERVIEW_H
#define COMANDHANDLERVIEW_H
#include <memory>
#include <QGraphicsScene>
#include <QPointF>
#include <QList>

#include "command/commandhandler.h"
#include "utility/singleton.hpp"
Expand All @@ -12,29 +9,18 @@

class ConstructorItem;

class CommandHandlerView :CommandHandler, public Singleton<CommandHandlerView> {
class CommandHandlerView : CommandHandler, public Singleton<CommandHandlerView> {
public:
CommandHandlerView();
CommandHandlerView();

void setScene(QGraphicsScene *scene);
bool handle(std::shared_ptr<Command> cmnd) override;

// FIXME: этот метод должен быть в другом классе
QPointF getDevicePos(int id);


private:
bool addItem(std::shared_ptr<Command> cmnd);
bool addConnection(std::shared_ptr<Command> cmnd);
bool removeItem(std::shared_ptr<Command> cmnd);
bool removeConnection(std::shared_ptr<Command> cmnd);
bool changeVariables(std::shared_ptr<Command> cmnd);

ConstructorItem *findItemWithId(int id);

private:
QGraphicsScene *_scene;

QList<ConstructorItem *> _devices;
};

#endif // COMANDHANDLERVIEW_H
54 changes: 54 additions & 0 deletions ui/commandviewmanager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "commandviewmanager.h"

CommandViewManager::CommandViewManager() : Singleton<CommandViewManager> (*this) { }

QPointF CommandViewManager::getDevicePos(int id) {
auto item = findItemWithId(id);

if (item == nullptr) {
throw std::logic_error("can't find device with id");
}

return item->pos();
}

void CommandViewManager::setScene(QGraphicsScene *scene) {
Q_ASSERT(_scene != nullptr);
_scene = scene;
}

bool CommandViewManager::addItemToScene(QGraphicsItem * const item) {
if (item == nullptr)
return false;

_scene->addItem(item);
return true;
}

bool CommandViewManager::addDevice(ConstructorItem *dev) {
if (dev == nullptr)
return false;

_devices.push_back(dev);
return true;
}

bool CommandViewManager::removeDevice(int id) {
auto iter = std::find_if(_devices.begin(), _devices.end(),
[id](ConstructorItem *item){ return item->getId() == id; });

if (iter == _devices.end())
return false;

delete *iter;
_devices.erase(iter);

return true;
}

ConstructorItem *CommandViewManager::findItemWithId(int id) {
auto iter = std::find_if(_devices.begin(), _devices.end(),
[id](ConstructorItem *item){ return item->getId() == id; });

return iter != _devices.end() ? *iter : nullptr;
}
27 changes: 27 additions & 0 deletions ui/commandviewmanager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef COMMANDVIEWMANAGER_H
#define COMMANDVIEWMANAGER_H
#include <QPointF>
#include <QGraphicsScene>

#include "constructoritem.h"
#include "utility/singleton.hpp"

class CommandViewManager : public Singleton<CommandViewManager> {
public:
CommandViewManager();

void setScene(QGraphicsScene *scene);
bool addItemToScene(QGraphicsItem * const item);

bool addDevice(ConstructorItem *dev);
bool removeDevice(int id);

QPointF getDevicePos(int id);
ConstructorItem * findItemWithId(int id);

private:
QList<ConstructorItem *> _devices;
QGraphicsScene *_scene;
};

#endif // COMMANDVIEWMANAGER_H
3 changes: 2 additions & 1 deletion ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "commandhandlerview.h"
#include "command/commandhanlerglobal.h"
#include "ui/commandhandlerchart.h"
#include "ui/commandviewmanager.h"
#include "utility/constructorserializer.h"

MainWindow::MainWindow(QWidget *parent) :
Expand Down Expand Up @@ -46,7 +47,7 @@ MainWindow::MainWindow(QWidget *parent) :
ui->graphicsView->resize(this->height() * 4 / 5, 0);

// ToDo: Скорее всего эту строчку куда-то нужно перенести
CommandHandlerView::i()->setScene(ui->graphicsView->scene());
CommandViewManager::i()->setScene(ui->graphicsView->scene());
CommandHandlerChart::i()->setTabWidget(ui->tabWidget);
}

Expand Down
3 changes: 2 additions & 1 deletion utility/constructorserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "deviceconfigs/device.h"
#include "devicemanager.h"
#include "ui/commandhandlerview.h"
#include "ui/commandviewmanager.h"
#include "deviceconfigs/deviceconfiglist.h"
#include "command/commandhanlerglobal.h"

Expand All @@ -30,7 +31,7 @@ QByteArray ConstructorSerializer::serialize() {
jsonDevice["id"] = device->getId();
jsonDevice["type"]= device->getType();

QPointF pos = CommandHandlerView::i()->getDevicePos(device->getId());
QPointF pos = CommandViewManager::i()->getDevicePos(device->getId());

jsonDevice["pos"] = QJsonArray{ pos.x(), pos.y() };

Expand Down