Tulip  4.4.0
Better Visualization Through Research
 All Classes Namespaces 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 <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/VectorEditionWidget.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  return QSize(width+15, height+5);
103 }
104 
105 template<typename T>
106 bool MultiLinesEditEditorCreator<T>::paint(QPainter* painter, const QStyleOptionViewItem &option, const QVariant &data) const {
107  TulipItemEditorCreator::paint(painter,option,data);
108  QRect rect = option.rect;
109  typename T::RealType val = data.value<typename T::RealType>();
110  QString valS = QString::fromUtf8(T::toString(val).c_str());
111  QStringList lines = valS.split(QLatin1Char('\n'));
112 
113  if (option.state.testFlag(QStyle::State_Selected) && option.showDecorationSelected) {
114  painter->setPen(option.palette.highlightedText().color());
115  painter->setBrush(option.palette.highlightedText());
116  }
117  else {
118  painter->setPen(option.palette.text().color());
119  painter->setBrush(option.palette.text());
120  }
121 
122  for (int i = 0 ; i < lines.count() ; ++i) {
123  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));
124  }
125 
126  return true;
127 }
128 
129 template<typename PROPTYPE>
130 QWidget* PropertyEditorCreator<PROPTYPE>::createWidget(QWidget* parent) const {
131  return new QComboBox(parent);
132 }
133 
134 template<typename PROPTYPE>
135 void PropertyEditorCreator<PROPTYPE>::setEditorData(QWidget* w, const QVariant& val,bool isMandatory,tlp::Graph* g) {
136  if (g == NULL) {
137  w->setEnabled(false);
138  return;
139  }
140 
141  PROPTYPE* prop = val.value<PROPTYPE*>();
142  QComboBox* combo = static_cast<QComboBox*>(w);
143  GraphPropertiesModel<PROPTYPE>* model = NULL;
144 
145  if (isMandatory)
146  model = new GraphPropertiesModel<PROPTYPE>(g,false,combo);
147  else
148  model = new GraphPropertiesModel<PROPTYPE>(QObject::trUtf8("Select a property"),g,false,combo);
149 
150  combo->setModel(model);
151  combo->setCurrentIndex(model->rowOf(prop));
152 }
153 
154 template<typename PROPTYPE>
155 QVariant PropertyEditorCreator<PROPTYPE>::editorData(QWidget* w,tlp::Graph* g) {
156  if (g == NULL)
157  return QVariant();
158 
159  QComboBox* combo = static_cast<QComboBox*>(w);
160  GraphPropertiesModel<PROPTYPE>* model = static_cast<GraphPropertiesModel<PROPTYPE> *>(combo->model());
161  QVariant var = model->data(model->index(combo->currentIndex(),0),TulipModel::PropertyRole);
163  PROPTYPE* prop = (PROPTYPE*)(pi);
164  return QVariant::fromValue<PROPTYPE*>(prop);
165 }
166 
167 template<typename PROPTYPE>
168 QString PropertyEditorCreator<PROPTYPE>::displayText(const QVariant& v) const {
169  PROPTYPE *prop = v.value<PROPTYPE*>();
170 
171  if (prop==NULL)
172  return QObject::trUtf8("Select a property");
173 
174  return QString::fromUtf8(prop->getName().c_str());
175 }
176 
177 template<typename ElementType>
178 QWidget* VectorEditorCreator<ElementType>::createWidget(QWidget*) const {
179  VectorEditionWidget* w = new VectorEditionWidget(NULL);
180  w->setWindowFlags(Qt::Dialog);
181  w->setWindowModality(Qt::ApplicationModal);
182  return w;
183 
184 }
185 
186 template<typename ElementType>
187 void VectorEditorCreator<ElementType>::setEditorData(QWidget* editor, const QVariant& v,bool,tlp::Graph*) {
188  QVector<QVariant> editorData;
189  std::vector<ElementType> vect = v.value<std::vector<ElementType> >();
190 
191  for (size_t i=0; i < vect.size(); ++i) {
192  editorData.push_back(QVariant::fromValue<ElementType>(vect[i]));
193  }
194 
195  static_cast<VectorEditionWidget*>(editor)->setVector(editorData,qMetaTypeId<ElementType>());
196 
197  static_cast<VectorEditionWidget*>(editor)->move(QCursor::pos());
198 }
199 
200 template<typename ElementType>
201 QVariant VectorEditorCreator<ElementType>::editorData(QWidget* editor,tlp::Graph*) {
202  std::vector<ElementType> result;
203  QVector<QVariant> editorData = static_cast<VectorEditionWidget*>(editor)->vector();
204  foreach(QVariant v, editorData)
205  result.push_back(v.value<ElementType>());
206  return QVariant::fromValue<std::vector<ElementType> >(result);
207 }
208 
209 // the template below is only used for displayText method implementation
210 template<typename T>
211 struct DisplayVectorDataType :public DataType {
212  DisplayVectorDataType(void *value) :DataType(value) {}
213  ~DisplayVectorDataType() {
214  }
215  DataType* clone() const {
216  return NULL;
217  }
218 
219  std::string getTypeName() const {
220  return std::string(typeid(std::vector<T>).name());
221  }
222 };
223 
224 
225 template<typename ElementType>
226 QString VectorEditorCreator<ElementType>::displayText(const QVariant &data) const {
227  std::vector<ElementType> v = data.value<std::vector<ElementType> >();
228 
229  if (v.empty())
230  return QString();
231 
232  // use a DataTypeSerializer if any
233  DataTypeSerializer* dts =
234  DataSet::typenameToSerializer(std::string(typeid(v).name()));
235 
236  if (dts) {
237  DisplayVectorDataType<ElementType> dt(&v);
238 
239  std::stringstream sstr;
240  dts->writeData(sstr, &dt);
241 
242  std::string str = sstr.str();
243 
244  if (str.size() > 45)
245  str.replace(str.begin() + 41, str.end(), " ...)");
246 
247  return QString::fromUtf8(str.c_str());
248  }
249 
250  if (v.size() == 1)
251  return QString("1 element");
252 
253  return QString::number(v.size()) + QObject::trUtf8(" elements");
254 }
255 
256 }