GenericPolygonArray.h 1.25 KB
Newer Older
1 2 3 4 5 6 7
#pragma once

#include "QmlObjectListModel.h"

#include <QVector>
#include <QString>

8
template <class PolygonType, template <class, class...> class ContainerType = QVector>
9
class GenericPolygonArray
10 11
{
public:
12 13
    GenericPolygonArray() {}
    GenericPolygonArray(const GenericPolygonArray &other) :
14
        _polygons(other._polygons), _dirty(true)
15
    {}
16
    ~GenericPolygonArray(){
Valentin Platzgummer's avatar
Valentin Platzgummer committed
17
        _objs.clearAndDeleteContents();
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    }

    class QmlObjectListModel *QmlObjectListModel(){
        if (_dirty)
            _updateObjects();
        _dirty = false;
        return &_objs;
    }

    const ContainerType<PolygonType> &polygons() const {
        return _polygons;
    }

    ContainerType<PolygonType> &polygons(){
        _dirty = true;
        return _polygons;
    }

36
    GenericPolygonArray &operator =(const GenericPolygonArray &other){
Valentin Platzgummer's avatar
Valentin Platzgummer committed
37 38 39 40 41
        this->_polygons = other._polygons;
        this->_dirty = true;
        return *this;
    }

42 43 44 45


private:
    void _updateObjects(void){
Valentin Platzgummer's avatar
Valentin Platzgummer committed
46
        _objs.clearAndDeleteContents();
47
        for (long i=0; i<_polygons.size(); ++i){
Valentin Platzgummer's avatar
Valentin Platzgummer committed
48
            _objs.append(new PolygonType(_polygons[i]));
49 50 51 52 53 54 55 56 57 58
        }
    }

    ContainerType<PolygonType> _polygons;
    class QmlObjectListModel   _objs;
    bool                       _dirty;

};