Commit ecdfed7e authored by Leander Schulten's avatar Leander Schulten
Browse files

Replace std::vector to a subclass of QAbstractListModel(readable from qml)

parent e4796d0b
......@@ -3,43 +3,61 @@
QString DevicePrototype::syncServiceClassName;
void ChannelVector::beginPushBack(int length){
beginInsertRows(QModelIndex(),channels.size(),channels.size()+length);
}
void ChannelVector::endPushBack(){
endInsertRows();
}
void ChannelVector::pop_back(){
const auto pos = channels.size()-1;
beginRemoveRows(QModelIndex(),pos,pos);
channels.pop_back();
endRemoveRows();
}
void DevicePrototype::removeChannels(int newMaxIndex){
if (newMaxIndex<0||newMaxIndex>getNumberOfChannels()) {
return;
}
// Elemente ganz hinten löschen
for (int i = 0; i < getNumberOfChannels() - newMaxIndex; ++i) {
channelRemoved(channels.back());
SyncService::addRemoveMemberMessage("DevicePrototype",getID(),"channels",channels.back()->getID());
delete channels.back();
channelRemoved(channels.getChannel().back());
SyncService::addRemoveMemberMessage("DevicePrototype",getID(),"channels",channels.getChannel().back()->getID());
delete channels.getChannel().back();
channels.pop_back();
}
}
void DevicePrototype::addChannel(int channel, QString name, QString description){
// dummy Channels einfügen
if (channel>=getNumberOfChannels()) {
channels.beginPushBack(getNumberOfChannels()-channel);
for (int i = getNumberOfChannels(); i < channel; ++i) {
channels.push_back(new Channel(i));
emit channelAdded(channels.back());
channels.getChannel().push_back(new Channel(i));
emit channelAdded(channels.getChannel().back());
QJsonObject o;
channels.back()->writeJsonObject(o);
channels.getChannel().back()->writeJsonObject(o);
SyncService::addCreateMemberMessage("DevicePrototype",getID(),"channels",o);
}
channels.push_back(new Channel(channel,name,description));
emit channelAdded(channels.back());
channels.getChannel().push_back(new Channel(channel,name,description));
channels.endPushBack();
emit channelAdded(channels.getChannel().back());
QJsonObject o;
channels.back()->writeJsonObject(o);
channels.getChannel().back()->writeJsonObject(o);
SyncService::addCreateMemberMessage("DevicePrototype",getID(),"channels",o);
}else{
// eigenschaften setzten
channels[channel]->setName(name);
channels[channel]->setDescription(description);
channels.getChannel()[channel]->setName(name);
channels.getChannel()[channel]->setDescription(description);
}
}
const Channel * DevicePrototype::getChannelById(const int id) const{
for(auto c = channels.cbegin();c!=channels.cend();++c){
for(auto c = channels.getChannel().cbegin();c!=channels.getChannel().cend();++c){
if ((**c).getID().value() == id) {
return *c;
}
......@@ -48,7 +66,7 @@ const Channel * DevicePrototype::getChannelById(const int id) const{
}
const Channel * DevicePrototype::getChannelByName(const QString &name) const{
for(auto c = channels.cbegin();c!=channels.cend();++c){
for(auto c = channels.getChannel().cbegin();c!=channels.getChannel().cend();++c){
if ((**c).getName() == name) {
return *c;
}
......@@ -57,16 +75,16 @@ const Channel * DevicePrototype::getChannelByName(const QString &name) const{
}
const Channel * DevicePrototype::getChannelByIndex(const unsigned int channelIndex) const{
if (channelIndex>=channels.size()) {
if (channelIndex>=channels.getChannel().size()) {
return nullptr;
}
return channels[channelIndex];
return channels.getChannel()[channelIndex];
}
DevicePrototype::DevicePrototype(const QJsonObject &o):NamedObject(o,&syncServiceClassName),IDBase(o){
auto array = o["channels"].toArray();
for(const auto c : array){
channels.push_back(new Channel(c.toObject()));
channels.getChannel().push_back(new Channel(c.toObject()));
}
}
......@@ -74,7 +92,7 @@ void DevicePrototype::writeJsonObject(QJsonObject &o) const{
NamedObject::writeJsonObject(o);
IDBase::writeJsonObject(o);
QJsonArray channels;
for(const auto c : this->channels){
for(const auto c : this->channels.getChannel()){
QJsonObject o;
c->writeJsonObject(o);
channels.append(o);
......
......@@ -7,6 +7,62 @@
#include "channel.h"
#include "idbase.h"
class ChannelVector : public QAbstractListModel{
std::vector<Channel*> channels;
public:
enum{
NameRole = Qt::UserRole+1,IndexRole,DescriptionRole
};
const std::vector<Channel*>& getChannel()const{return channels;}
std::vector<Channel*>& getChannel(){return channels;}
virtual int rowCount(const QModelIndex &) const override{return channels.size();}
virtual QVariant data(const QModelIndex &index, int role) const override{
if(index.row()>=0&&index.row()<int(channels.size())){
switch(role){
case Qt::DisplayRole:
case NameRole:
return channels[index.row()]->getName();
case Qt::ToolTipRole:
case DescriptionRole:
return channels[index.row()]->getDescription();
case IndexRole:
return channels[index.row()]->getIndex();
}
return QVariant::fromValue(channels[index.row()]);
}
qDebug()<<"index out\n";
return QVariant();
}
virtual bool setData(const QModelIndex &index, const QVariant &value, int role) override{
if(index.row()>=0&&index.row()<int(channels.size())){
switch(role){
case Qt::DisplayRole:
case NameRole:
channels[index.row()]->setName(value.toString());
break;
case Qt::ToolTipRole:
case DescriptionRole:
channels[index.row()]->setDescription(value.toString());
break;
default:
return false;
}
return true;
}
return false;
}
QHash<int,QByteArray> roleNames()const override{
QHash<int,QByteArray> r;
r[NameRole] = "name";
r[IndexRole] = "index";
r[DescriptionRole] = "description";
return r;
}
void pop_back();
void beginPushBack(int length);
void endPushBack();
};
/** Jedes Gerät(Scanner/Laser/Lampe/...) bekommt ein Device Prototype, wo festgelegt wird, auf welchem Channel welche Daten anliegen
* @brief The DevicePrototype class
*/
......@@ -14,21 +70,27 @@ class DevicePrototype : public NamedObject, public IDBase<DevicePrototype>
{
Q_OBJECT
Q_PROPERTY(int numberOfChannels READ getNumberOfChannels NOTIFY numberOfChannelsChanged)
Q_PROPERTY(QAbstractItemModel * channel READ getChannelModel CONSTANT)
private:
/**
* @brief channels Ein vector die die einzelnen Channel Informationen hält
*/
std::vector<Channel*> channels;
ChannelVector channels;
public:
static QString syncServiceClassName;
DevicePrototype(const QJsonObject &o);
DevicePrototype(QString name):NamedObject(name,"",&syncServiceClassName){}
int getNumberOfChannels()const{return channels.size();}
DevicePrototype(QString name, QString description=""):NamedObject(name,description,&syncServiceClassName){}
int getNumberOfChannels()const{return channels.getChannel().size();}
/**
* @brief removeChannels Entfernt Channel bis zu einem bestimmten Index
* @param newMaxIndex Der neue Maximale Index (Inclusiv)
*/
void removeChannels(int newMaxIndex);
/**
* @brief popChannel Entfernt den letzten Channel
*/
Q_INVOKABLE void popChannel(){removeChannels(getNumberOfChannels()-1);}
Q_INVOKABLE void pushChannel(QString name, QString description=""){addChannel(getNumberOfChannels(),name,description);}
/**
* @brief addChannelDescription Setzt ein Channel neu oder fügt einen neuen Channel mit dem jeweiligen Channel hinzu
* Wenn eine Lücke zwischen den bisherigen maximalen Index und dem neuen Index liegt, wird diese mit Dummy Channeln aufgefüllt.
......@@ -41,9 +103,12 @@ public:
const Channel * getChannelById(const int id)const;
const Channel * getChannelByIndex(const unsigned int channelIndex)const;
const std::vector<Channel*> & getChannels()const{return channels;};
const std::vector<Channel*> & getChannels()const{return channels.getChannel();}
void writeJsonObject(QJsonObject &o)const;
ChannelVector * getChannelModel(){return &channels;}
const ChannelVector * getChannelModel()const{return &channels;}
public:
//static methods for the syncService:
static void update (const ID &id, const QString &name,const QString &value){
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment