Commit 1300e996 authored by Leander Schulten's avatar Leander Schulten
Browse files

Replace std::vector to a subclass of QAbstractList for readability from qml

parent ecdfed7e
......@@ -8,14 +8,16 @@ ProgrammPrototype::ProgrammPrototype(DevicePrototype *devicePrototype, QString n
setParent(devicePrototype);
connect(devicePrototype,&DevicePrototype::channelAdded,this,&ProgrammPrototype::channelAdded);
connect(devicePrototype,&DevicePrototype::channelRemoved,this,&ProgrammPrototype::channelRemoved);
programm.beginPushBack(devicePrototype->getChannels().size());
for(const auto channel : devicePrototype->getChannels()){
programm.push_back(new ChannelProgramm(channel));
emit channelProgrammAdded(programm.back());
programm.getChannelProgramms().push_back(new ChannelProgramm(channel));
emit channelProgrammAdded(programm.getChannelProgramms().back());
}
programm.endPushBack();
}
void ProgrammPrototype::channelProgrammDestroyed(ChannelProgramm * c){
for(auto i = programm.cbegin();i!=programm.cend();++i){
for(auto i = programm.getChannelProgramms().cbegin();i!=programm.getChannelProgramms().cend();++i){
if(*i==c){
programm.erase(i);
break;
......@@ -25,11 +27,13 @@ void ProgrammPrototype::channelProgrammDestroyed(ChannelProgramm * c){
}
void ProgrammPrototype::channelAdded(Channel *c){
programm.push_back(new ChannelProgramm(c));
emit channelProgrammAdded(programm.back());
programm.beginPushBack(1);
programm.getChannelProgramms().push_back(new ChannelProgramm(c));
programm.endPushBack();
emit channelProgrammAdded(programm.getChannelProgramms().back());
}
void ProgrammPrototype::channelRemoved(Channel *c){
for(auto i = programm.cbegin();i!=programm.cend();++i){
for(auto i = programm.getChannelProgramms().cbegin();i!=programm.getChannelProgramms().cend();++i){
if((**i).getChannel()==c){
auto cp = *i;
programm.erase(i);
......@@ -65,13 +69,14 @@ unsigned char ChannelProgramm::getValueForTime(double t)const{
auto iter = timeline.upper_bound(t);
const auto rightValue = iter->value;
const auto rightTime = iter->time;
--iter;
const TimePoint * left;
double progress;
if(iter == timeline.cend()){
if(iter == timeline.cbegin()){
left = &*timeline.crbegin();
progress = t / rightTime;
}else{
--iter;
left = &*iter;
progress = (t - left->time) / (rightTime - left->time);
}
......@@ -114,7 +119,7 @@ void ProgrammPrototype::writeJsonObject(QJsonObject &o) const{
NamedObject::writeJsonObject(o);
o.insert("devicePrototype",QString::number(devicePrototype->getID().value()));
QJsonArray array;
for(const auto p : programm){
for(const auto p : programm.getChannelProgramms()){
QJsonObject o;
p->writeJsonObject(o);
array.append(o);
......@@ -144,20 +149,20 @@ ProgrammPrototype::ProgrammPrototype(const QJsonObject &o):NamedObject(o,&syncSe
connect(devicePrototype,&DevicePrototype::channelAdded,this,&ProgrammPrototype::channelAdded);
connect(devicePrototype,&DevicePrototype::channelRemoved,this,&ProgrammPrototype::channelRemoved);
for(const auto p : o["programm"].toArray()){
programm.push_back(new ChannelProgramm(p.toObject()));
emit channelProgrammAdded(programm.back());
programm.getChannelProgramms().push_back(new ChannelProgramm(p.toObject()));
emit channelProgrammAdded(programm.getChannelProgramms().back());
}
}
ChannelProgramm * ProgrammPrototype::getChannelProgramm(const Channel * channel){
for(auto p = programm.begin() ; p != programm.end() ;++p){
for(auto p = programm.getChannelProgramms().begin() ; p != programm.getChannelProgramms().end() ;++p){
if((**p).channel == channel)
return *p;
}
return nullptr;
}
ChannelProgramm * ProgrammPrototype::getChannelProgramm(int index){
for(auto p = programm.begin() ; p != programm.end() ;++p){
for(auto p = programm.getChannelProgramms().begin() ; p != programm.getChannelProgramms().end() ;++p){
if((**p).channel->getIndex() == index)
return *p;
}
......
......@@ -31,6 +31,7 @@ class ChannelProgramm : public QObject{
Q_OBJECT
Q_ENUM(RepeatPolicy)
Q_PROPERTY(RepeatPolicy repeatPolicy MEMBER repeatPolicy NOTIFY repeatPolicyChanged)
Q_PROPERTY(Channel* channel READ getChannel CONSTANT)
private:
RepeatPolicy repeatPolicy;
public:
......@@ -46,6 +47,44 @@ signals:
void repeatPolicyChanged();
};
class ChannelProgrammVector : public QAbstractListModel{
std::vector<ChannelProgramm*> channels;
public:
enum{
ChannelRole = Qt::UserRole+1,ChannelProgrammRole
};
const std::vector<ChannelProgramm*>& getChannelProgramms()const{return channels;}
std::vector<ChannelProgramm*>& getChannelProgramms(){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 ChannelRole:
return QVariant::fromValue(channels[index.row()]->getChannel());
}
return QVariant::fromValue(channels[index.row()]);
}
qDebug()<<"index out\n";
return QVariant();
}
QHash<int,QByteArray> roleNames()const override{
QHash<int,QByteArray> r;
r[ChannelRole] = "channel";
r[ChannelProgrammRole] = "channelProgramm";
r[ChannelProgrammRole+1] = "modelData";
return r;
}
ChannelProgramm* erase(std::vector<ChannelProgramm*>::const_iterator i){
const auto pos = i-channels.begin();
beginRemoveRows(QModelIndex(),pos,pos);
auto result = *channels.erase(i);
endRemoveRows();
return result;
}
void beginPushBack(int length){beginInsertRows(QModelIndex(),channels.size(),channels.size()+length);}
void endPushBack(){endInsertRows();}
};
/**
* @brief The ProgrammPrototype class describe for a DevicePrototype for each Channel a ChannelProgramm
*/
......@@ -53,6 +92,7 @@ class ProgrammPrototype : public NamedObject, public IDBase<ProgrammPrototype>
{
Q_OBJECT
Q_PROPERTY(DevicePrototype* devicePrototype READ getDevicePrototype CONSTANT)
Q_PROPERTY(QAbstractItemModel* channelProgramms READ getChannelProgrammModel CONSTANT)
public:
static QString syncServiceClassName;
/**
......@@ -60,8 +100,8 @@ public:
*/
const DevicePrototype * devicePrototype;
DevicePrototype * getDevicePrototype()const{return const_cast<DevicePrototype *>(devicePrototype);}
public:
std::vector<ChannelProgramm*> programm;
private:
ChannelProgrammVector programm;
private slots:
void channelProgrammDestroyed(ChannelProgramm * c);
public:
......@@ -69,8 +109,10 @@ public:
ProgrammPrototype(DevicePrototype * devicePrototype, QString name, QString description="");
ChannelProgramm * getChannelProgramm(const Channel * channel);
ChannelProgramm * getChannelProgramm(int channelIndex);
const std::vector<ChannelProgramm*>& getChannelProgramms()const{return programm;}
const std::vector<ChannelProgramm*>& getChannelProgramms()const{return programm.getChannelProgramms();}
std::vector<ChannelProgramm*>& getChannelProgramms(){return programm.getChannelProgramms();}
void writeJsonObject(QJsonObject &o)const;
ChannelProgrammVector * getChannelProgrammModel(){return &programm;}
signals:
void channelProgrammAdded(ChannelProgramm *);
void channelProgrammRemoved(ChannelProgramm *);
......
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