Tulip  4.10.0
Better Visualization Through Research
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
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 <sstream>
20 #include <QSet>
21 #include <QLineEdit>
22 #include <QTextEdit>
23 #include <QComboBox>
24 #include <QPainter>
25 
26 #include <tulip/ForEach.h>
27 #include <tulip/DataSet.h>
28 #include <tulip/VectorEditor.h>
29 #include <tulip/GraphPropertiesModel.h>
30 #include <tulip/TlpQtTools.h>
31 
32 
33 namespace tlp {
34 
35 template<typename T>
36 QWidget* LineEditEditorCreator<T>::createWidget(QWidget *parent) const {
37  return new QLineEdit(parent);
38 }
39 
40 template<typename T>
41 void LineEditEditorCreator<T>::setEditorData(QWidget* editor, const QVariant &data,bool,tlp::Graph*) {
42  typename T::RealType val = data.value<typename T::RealType>();
43  static_cast<QLineEdit*>(editor)->setText(tlpStringToQString(T::toString(val)));
44  static_cast<QLineEdit*>(editor)->selectAll();
45 }
46 
47 template<typename T>
48 QVariant LineEditEditorCreator<T>::editorData(QWidget* editor,tlp::Graph*) {
49  std::string strVal = QStringToTlpString(static_cast<QLineEdit*>(editor)->text());
50  QVariant result;
51  typename T::RealType val;
52 
53  if (T::fromString(val,strVal))
54  result.setValue<typename T::RealType>(val);
55 
56  return result;
57 }
58 
59 template<typename T>
60 QWidget* MultiLinesEditEditorCreator<T>::createWidget(QWidget *parent) const {
61  QTextEdit *edit = new QTextEdit(parent);
62  edit->setFocusPolicy(Qt::StrongFocus);
63  edit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
64  edit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
65  return edit;
66 }
67 
68 template<typename T>
69 void MultiLinesEditEditorCreator<T>::setEditorData(QWidget* editor, const QVariant &data,bool,tlp::Graph*) {
70  typename T::RealType val = data.value<typename T::RealType>();
71  static_cast<QTextEdit*>(editor)->setPlainText(tlpStringToQString(T::toString(val)));
72  static_cast<QTextEdit*>(editor)->selectAll();
73 }
74 
75 template<typename T>
76 QVariant MultiLinesEditEditorCreator<T>::editorData(QWidget* editor,tlp::Graph*) {
77  std::string strVal = QStringToTlpString(static_cast<QTextEdit*>(editor)->toPlainText());
78  QVariant result;
79  typename T::RealType val;
80 
81  if (T::fromString(val,strVal))
82  result.setValue<typename T::RealType>(val);
83 
84  return result;
85 }
86 
87 template<typename T>
88 QSize MultiLinesEditEditorCreator<T>::sizeHint(const QStyleOptionViewItem & option, const QModelIndex &index) const {
89  QVariant data = index.model()->data(index);
90  typename T::RealType val = data.value<typename T::RealType>();
91  QString valS = tlpStringToQString(T::toString(val));
92  QStringList lines = valS.split(QLatin1Char('\n'));
93  QFontMetrics fontMetrics(option.font);
94  int height = 0;
95  int width = 0;
96 
97  for (int i = 0 ; i < lines.count() ; ++i) {
98  QRect textBB = fontMetrics.boundingRect(lines.at(i));
99  height += fontMetrics.boundingRect("|").height();
100  width = std::max(width, textBB.width());
101  }
102 
103  // restrict column width in case of really large string to display
104  width = std::min(width, 500);
105 
106  return QSize(width+15, height+5);
107 }
108 
109 template<typename T>
110 bool MultiLinesEditEditorCreator<T>::paint(QPainter* painter, const QStyleOptionViewItem &option, const QVariant &data) const {
111  TulipItemEditorCreator::paint(painter,option,data);
112  QRect rect = option.rect;
113  typename T::RealType val = data.value<typename T::RealType>();
114  QString valS = tlpStringToQString(T::toString(val));
115  QStringList lines = valS.split(QLatin1Char('\n'));
116 
117  if (option.state.testFlag(QStyle::State_Selected) && option.showDecorationSelected) {
118  painter->setPen(option.palette.highlightedText().color());
119  painter->setBrush(option.palette.highlightedText());
120  }
121  else {
122  painter->setPen(option.palette.text().color());
123  painter->setBrush(option.palette.text());
124  }
125 
126  for (int i = 0 ; i < lines.count() ; ++i) {
127  painter->drawText(rect.x(), rect.y() + i * rect.height()/lines.count(), rect.width(), rect.height()/lines.count(),Qt::AlignLeft | Qt::AlignVCenter, lines.at(i));
128  }
129 
130  return true;
131 }
132 
133 template<typename PROPTYPE>
134 QWidget* PropertyEditorCreator<PROPTYPE>::createWidget(QWidget* parent) const {
135  return new QComboBox(parent);
136 }
137 
138 template<typename PROPTYPE>
139 void PropertyEditorCreator<PROPTYPE>::setEditorData(QWidget* w, const QVariant& val,bool isMandatory,tlp::Graph* g) {
140  if (g == NULL) {
141  w->setEnabled(false);
142  return;
143  }
144 
145  PROPTYPE* prop = val.value<PROPTYPE*>();
146  QComboBox* combo = static_cast<QComboBox*>(w);
147  GraphPropertiesModel<PROPTYPE>* model = NULL;
148 
149  if (isMandatory)
150  model = new GraphPropertiesModel<PROPTYPE>(g,false,combo);
151  else
152  model = new GraphPropertiesModel<PROPTYPE>(QObject::trUtf8("Select a property"),g,false,combo);
153 
154  combo->setModel(model);
155  combo->setCurrentIndex(model->rowOf(prop));
156 }
157 
158 template<typename PROPTYPE>
159 QVariant PropertyEditorCreator<PROPTYPE>::editorData(QWidget* w,tlp::Graph* g) {
160  if (g == NULL)
161  return QVariant();
162 
163  QComboBox* combo = static_cast<QComboBox*>(w);
164  GraphPropertiesModel<PROPTYPE>* model = static_cast<GraphPropertiesModel<PROPTYPE> *>(combo->model());
165  QVariant var = model->data(model->index(combo->currentIndex(),0),TulipModel::PropertyRole);
167  PROPTYPE* prop = (PROPTYPE*)(pi);
168  return QVariant::fromValue<PROPTYPE*>(prop);
169 }
170 
171 template<typename PROPTYPE>
172 QString PropertyEditorCreator<PROPTYPE>::displayText(const QVariant& v) const {
173  PROPTYPE *prop = v.value<PROPTYPE*>();
174 
175  if (prop==NULL)
176  return QObject::trUtf8("Select a property");
177 
178  return tlpStringToQString(prop->getName());
179 }
180 
181 template<typename ElementType>
182 QWidget* VectorEditorCreator<ElementType>::createWidget(QWidget*) const {
183  VectorEditor* w = new VectorEditor(NULL);
184  w->setWindowFlags(Qt::Dialog);
185  w->setWindowModality(Qt::ApplicationModal);
186  return w;
187 
188 }
189 
190 template<typename ElementType>
191 void VectorEditorCreator<ElementType>::setEditorData(QWidget* editor, const QVariant& v,bool,tlp::Graph*) {
192  QVector<QVariant> editorData;
193  std::vector<ElementType> vect = v.value<std::vector<ElementType> >();
194 
195  for (size_t i=0; i < vect.size(); ++i) {
196  editorData.push_back(QVariant::fromValue<ElementType>(vect[i]));
197  }
198 
199  static_cast<VectorEditor*>(editor)->setVector(editorData,qMetaTypeId<ElementType>());
200 
201  static_cast<VectorEditor*>(editor)->move(QCursor::pos());
202 }
203 
204 template<typename ElementType>
205 QVariant VectorEditorCreator<ElementType>::editorData(QWidget* editor,tlp::Graph*) {
206  std::vector<ElementType> result;
207  QVector<QVariant> editorData = static_cast<VectorEditor*>(editor)->vector();
208 
209  foreach(QVariant v, editorData)
210  result.push_back(v.value<ElementType>());
211 
212  return QVariant::fromValue<std::vector<ElementType> >(result);
213 }
214 
215 // the template below is only used for displayText method implementation
216 template<typename T>
217 struct DisplayVectorDataType :public DataType {
218  DisplayVectorDataType(void *value) :DataType(value) {}
219  ~DisplayVectorDataType() {
220  }
221  DataType* clone() const {
222  return NULL;
223  }
224 
225  std::string getTypeName() const {
226  return std::string(typeid(std::vector<T>).name());
227  }
228 };
229 
230 
231 template<typename ElementType>
232 QString VectorEditorCreator<ElementType>::displayText(const QVariant &data) const {
233  std::vector<ElementType> v = data.value<std::vector<ElementType> >();
234 
235  if (v.empty())
236  return QString();
237 
238  // use a DataTypeSerializer if any
239  DataTypeSerializer* dts =
240  DataSet::typenameToSerializer(std::string(typeid(v).name()));
241 
242  if (dts) {
243  DisplayVectorDataType<ElementType> dt(&v);
244 
245  std::stringstream sstr;
246  dts->writeData(sstr, &dt);
247 
248  std::string str = sstr.str();
249 
250  QString qstr = tlpStringToQString(str);
251 
252  return truncateText(qstr);
253  }
254 
255  if (v.size() == 1)
256  return QString("1 element");
257 
258  return QString::number(v.size()) + QObject::trUtf8(" elements");
259 }
260 
261 }
PropertyInterface describes the interface of a graph property.
std::string QStringToTlpString(const QString &toConvert)
Convert a QString to a Tulip UTF-8 encoded std::string.
Definition: TlpQtTools.h:48
static DataTypeSerializer * typenameToSerializer(const std::string &name)
ElementType
Definition: Graph.h:46
QString tlpStringToQString(const std::string &toConvert)
Convert a Tulip UTF-8 encoded std::string to a QString.
Definition: TlpQtTools.h:54