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