Tulip  4.1.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
TulipItemEditorCreators.cxx
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 #include <QtCore/QSet>
20 #include <QtGui/QLineEdit>
21 #include <QtGui/QComboBox>
22 
23 #include <tulip/ForEach.h>
24 #include <tulip/VectorEditionWidget.h>
25 #include <tulip/GraphPropertiesModel.h>
26 
27 
28 namespace tlp {
29 
30 template<typename T>
31 QWidget* LineEditEditorCreator<T>::createWidget(QWidget *parent) const {
32  return new QLineEdit(parent);
33 }
34 
35 template<typename T>
36 void LineEditEditorCreator<T>::setEditorData(QWidget* editor, const QVariant &data,bool,tlp::Graph*) {
37  typename T::RealType val = data.value<typename T::RealType>();
38  static_cast<QLineEdit*>(editor)->setText(QString::fromUtf8(T::toString(val).c_str()));
39 }
40 
41 template<typename T>
42 QVariant LineEditEditorCreator<T>::editorData(QWidget* editor,tlp::Graph*) {
43  std::string strVal = std::string(static_cast<QLineEdit*>(editor)->text().toUtf8().data());
44  QVariant result;
45  typename T::RealType val;
46 
47  if (T::fromString(val,strVal))
48  result.setValue<typename T::RealType>(val);
49 
50  return result;
51 }
52 
53 template<typename PROPTYPE>
54 QWidget* PropertyEditorCreator<PROPTYPE>::createWidget(QWidget* parent) const {
55  return new QComboBox(parent);
56 }
57 
58 template<typename PROPTYPE>
59 void PropertyEditorCreator<PROPTYPE>::setEditorData(QWidget* w, const QVariant& val,bool isMandatory,tlp::Graph* g) {
60  if (g == NULL) {
61  w->setEnabled(false);
62  return;
63  }
64 
65  PROPTYPE* prop = val.value<PROPTYPE*>();
66  QComboBox* combo = static_cast<QComboBox*>(w);
67  GraphPropertiesModel<PROPTYPE>* model = NULL;
68 
69  if (isMandatory)
70  model = new GraphPropertiesModel<PROPTYPE>(g,false,combo);
71  else
72  model = new GraphPropertiesModel<PROPTYPE>(QObject::trUtf8("Select a property"),g,false,combo);
73 
74  combo->setModel(model);
75  combo->setCurrentIndex(model->rowOf(prop));
76 }
77 
78 template<typename PROPTYPE>
79 QVariant PropertyEditorCreator<PROPTYPE>::editorData(QWidget* w,tlp::Graph* g) {
80  if (g == NULL)
81  return QVariant();
82 
83  QComboBox* combo = static_cast<QComboBox*>(w);
84  GraphPropertiesModel<PROPTYPE>* model = static_cast<GraphPropertiesModel<PROPTYPE> *>(combo->model());
85  QVariant var = model->data(model->index(combo->currentIndex(),0),TulipModel::PropertyRole);
87  PROPTYPE* prop = (PROPTYPE*)(pi);
88  return QVariant::fromValue<PROPTYPE*>(prop);
89 }
90 
91 template<typename PROPTYPE>
92 QString PropertyEditorCreator<PROPTYPE>::displayText(const QVariant& v) const {
93  PROPTYPE *prop = v.value<PROPTYPE*>();
94 
95  if (prop==NULL)
96  return QObject::trUtf8("Select a property");
97 
98  return QString::fromUtf8(prop->getName().c_str());
99 }
100 
101 template<typename ElementType>
102 QWidget* VectorEditorCreator<ElementType>::createWidget(QWidget*) const {
103  VectorEditionWidget* w = new VectorEditionWidget(NULL);
104  w->setWindowFlags(Qt::Dialog);
105  w->setWindowModality(Qt::ApplicationModal);
106  return w;
107 
108 }
109 
110 template<typename ElementType>
111 void VectorEditorCreator<ElementType>::setEditorData(QWidget* editor, const QVariant& v,bool,tlp::Graph*) {
112  QVector<QVariant> editorData;
113  std::vector<ElementType> vect = v.value<std::vector<ElementType> >();
114 
115  for (size_t i=0; i < vect.size(); ++i) {
116  editorData.push_back(QVariant::fromValue<ElementType>(vect[i]));
117  }
118 
119  static_cast<VectorEditionWidget*>(editor)->setVector(editorData,qMetaTypeId<ElementType>());
120 
121  static_cast<VectorEditionWidget*>(editor)->move(QCursor::pos());
122 }
123 
124 template<typename ElementType>
125 QVariant VectorEditorCreator<ElementType>::editorData(QWidget* editor,tlp::Graph*) {
126  std::vector<ElementType> result;
127  QVector<QVariant> editorData = static_cast<VectorEditionWidget*>(editor)->vector();
128  foreach(QVariant v, editorData)
129  result.push_back(v.value<ElementType>());
130  return QVariant::fromValue<std::vector<ElementType> >(result);
131 }
132 
133 template<typename ElementType>
134 QString VectorEditorCreator<ElementType>::displayText(const QVariant &data) const {
135  std::vector<ElementType> v = data.value<std::vector<ElementType> >();
136  return QString::number(v.size()) + QObject::trUtf8(" elements");
137 }
138 
139 }