File: VectorEditionWidget.h

package info (click to toggle)
tulip 3.7.0dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 39,428 kB
  • sloc: cpp: 231,403; php: 11,023; python: 1,128; sh: 671; yacc: 522; makefile: 315; xml: 63; lex: 55
file content (177 lines) | stat: -rwxr-xr-x 4,909 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef VECTOREDITIONWIDGET_H
#define VECTOREDITIONWIDGET_H

#include <QWidget>
#include <tulip/tulipconf.h>
#include <tulip/PropertyTypes.h>
#include "TulipQVariantBuilder.h"
#include <QtCore/QVariant>
#include <QtCore/QAbstractListModel>


namespace Ui {
class VectorEditionWidget;
}


class ListPropertyWidgetTypeMangerInterface {
public:
  virtual ~ListPropertyWidgetTypeMangerInterface() {}
  virtual std::string getDefaultStringValue() const=0;
  virtual QVariant getValue(unsigned int i) = 0;
  virtual QVariant getStringValue(unsigned int i) = 0;
  virtual int getElementNumber()const = 0;
  virtual bool setValue(unsigned int i,QVariant v) = 0;

  virtual void insertRow() = 0;
  virtual void deleteRow(unsigned int i) = 0;
};

template<typename TYPECLASS>
class ListPropertyWidgetTypeManger : public ListPropertyWidgetTypeMangerInterface {
public:
  ListPropertyWidgetTypeManger<TYPECLASS>(const std::vector<typename TYPECLASS::RealType>& elements) : elements(elements) {
  }

  virtual ~ListPropertyWidgetTypeManger() {
  }

  std::string getDefaultStringValue() const {
    return TYPECLASS::toString(TYPECLASS::defaultValue());
  }

  virtual int getElementNumber() const {
    return elements.size();
  }

  virtual QVariant getStringValue(unsigned int i) {
    assert(i < elements.size());
    return QVariant(QString::fromUtf8(TYPECLASS::toString( elements[i]).c_str()));
  }

  virtual  QVariant getValue(unsigned int i) {
    assert(i < elements.size());
    QVariant v;
    v.setValue<typename TYPECLASS::RealType>(elements[i]);
    return v;
  }

  virtual bool setValue(unsigned int i, QVariant v) {
    assert(i < elements.size());

    if(v.isValid()) {
      elements[i]=v.value<typename TYPECLASS::RealType>();
      return true;
    }
    else {
      return false;
    }
  }

  virtual void insertRow() {
    elements.push_back(TYPECLASS::defaultValue());
  }
  virtual void deleteRow(unsigned int row) {
    assert(row < elements.size());
    elements.erase(elements.begin()+row);
  }

  const std::vector<typename TYPECLASS::RealType>&  getResultValue() {
    return elements;
  }

private:
  std::vector<typename TYPECLASS::RealType> elements;
};

template<>
class ListPropertyWidgetTypeManger<tlp::StringType> : public ListPropertyWidgetTypeMangerInterface {
public:
  ListPropertyWidgetTypeManger<tlp::StringType>(const std::vector<std::string>& elements) : elements(elements) {
  }
  std::string getDefaultStringValue() const {
    return std::string();
  }

  virtual int getElementNumber() const {
    return elements.size();
  }

  virtual QVariant getStringValue(unsigned int i) {
    return getValue(i);
  }

  virtual  QVariant getValue(unsigned int i) {
    assert(i < elements.size());
    QVariant v(QString::fromUtf8(elements[i].c_str()));
    return v;
  }

  virtual bool setValue(unsigned int i, QVariant data) {
    assert(i < elements.size());

    if(data.isValid()) {
      elements[i]=data.toString().toStdString();
      return true;
    }
    else {
      return false;
    }
  }

  virtual void insertRow() {
    elements.push_back(std::string());
  }
  virtual void deleteRow(unsigned int row) {
    assert(row < elements.size());
    elements.erase(elements.begin()+row);
  }

  const std::vector<std::string>&  getResultValue() {
    return elements;
  }

private:
  std::vector<std::string> elements;
};

class ListPropertyWidgetModel : public QAbstractListModel {

public :
  ListPropertyWidgetModel(TulipPropertyType elementsType,ListPropertyWidgetTypeMangerInterface *typeManager, QWidget* parent = 0);
  virtual ~ListPropertyWidgetModel();
  virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
  virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
  virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
  virtual Qt::ItemFlags flags(const QModelIndex& index) const;
  virtual bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex());
  virtual bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex());

  ListPropertyWidgetTypeMangerInterface *getInterface() {
    return elements;
  }
private:
  ListPropertyWidgetTypeMangerInterface *elements;
  TulipPropertyType elementsType;
};


class VectorEditionWidget : public QWidget {
  Q_OBJECT

public:
  explicit VectorEditionWidget(QWidget *parent = 0);
  ~VectorEditionWidget();
  ListPropertyWidgetTypeMangerInterface *getInterface();
  void setInterface(TulipPropertyType elementsType,ListPropertyWidgetTypeMangerInterface *interf);

private slots:
  void addRow();
  void removeRows();
  void setAll();

private:
  Ui::VectorEditionWidget *ui;
};

#endif // VECTOREDITIONWIDGET_H