Tulip  4.2.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 <QtCore/QSet>
20 #include <QtGui/QLineEdit>
21 #include <QtGui/QTextEdit>
22 #include <QtGui/QComboBox>
23 
24 #include <tulip/ForEach.h>
25 #include <tulip/VectorEditionWidget.h>
26 #include <tulip/GraphPropertiesModel.h>
27 
28 
29 namespace tlp {
30 
31 template<typename T>
32 QWidget* LineEditEditorCreator<T>::createWidget(QWidget *parent) const {
33  return new QLineEdit(parent);
34 }
35 
36 template<typename T>
37 void LineEditEditorCreator<T>::setEditorData(QWidget* editor, const QVariant &data,bool,tlp::Graph*) {
38  typename T::RealType val = data.value<typename T::RealType>();
39  static_cast<QLineEdit*>(editor)->setText(QString::fromUtf8(T::toString(val).c_str()));
40  static_cast<QLineEdit*>(editor)->selectAll();
41 }
42 
43 template<typename T>
44 QVariant LineEditEditorCreator<T>::editorData(QWidget* editor,tlp::Graph*) {
45  std::string strVal = std::string(static_cast<QLineEdit*>(editor)->text().toUtf8().data());
46  QVariant result;
47  typename T::RealType val;
48 
49  if (T::fromString(val,strVal))
50  result.setValue<typename T::RealType>(val);
51 
52  return result;
53 }
54 
55 template<typename T>
56 QWidget* MultiLinesEditEditorCreator<T>::createWidget(QWidget *parent) const {
57  QTextEdit *edit = new QTextEdit(parent);
58  edit->setFocusPolicy(Qt::StrongFocus);
59  edit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
60  edit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
61  return edit;
62 }
63 
64 template<typename T>
65 void MultiLinesEditEditorCreator<T>::setEditorData(QWidget* editor, const QVariant &data,bool,tlp::Graph*) {
66  typename T::RealType val = data.value<typename T::RealType>();
67  static_cast<QTextEdit*>(editor)->setPlainText(QString::fromUtf8(T::toString(val).c_str()));
68  static_cast<QTextEdit*>(editor)->selectAll();
69 }
70 
71 template<typename T>
72 QVariant MultiLinesEditEditorCreator<T>::editorData(QWidget* editor,tlp::Graph*) {
73  std::string strVal = std::string(static_cast<QTextEdit*>(editor)->toPlainText().toUtf8().data());
74  QVariant result;
75  typename T::RealType val;
76 
77  if (T::fromString(val,strVal))
78  result.setValue<typename T::RealType>(val);
79 
80  return result;
81 }
82 
83 template<typename T>
84 QSize MultiLinesEditEditorCreator<T>::sizeHint(const QStyleOptionViewItem & option, const QModelIndex &index) const {
85  QVariant data = index.model()->data(index);
86  typename T::RealType val = data.value<typename T::RealType>();
87  QString valS = QString::fromUtf8(T::toString(val).c_str());
88  QStringList lines = valS.split(QLatin1Char('\n'));
89  QFontMetrics fontMetrics(option.font);
90  int height = 0;
91  int width = 0;
92 
93  for (int i = 0 ; i < lines.count() ; ++i) {
94  QRect textBB = fontMetrics.boundingRect(lines.at(i));
95  height += textBB.height();
96  width = std::max(width, textBB.width());
97  }
98 
99  return QSize(width+15, height+5);
100 }
101 
102 template<typename T>
103 bool MultiLinesEditEditorCreator<T>::paint(QPainter* painter, const QStyleOptionViewItem &option, const QVariant &data) const {
104  TulipItemEditorCreator::paint(painter,option,data);
105  QRect rect = option.rect;
106  typename T::RealType val = data.value<typename T::RealType>();
107  QString valS = QString::fromUtf8(T::toString(val).c_str());
108  QStringList lines = valS.split(QLatin1Char('\n'));
109 
110  if (option.state.testFlag(QStyle::State_Selected) && option.showDecorationSelected) {
111  painter->setPen(option.palette.highlightedText().color());
112  painter->setBrush(option.palette.highlightedText());
113  }
114  else {
115  painter->setPen(option.palette.text().color());
116  painter->setBrush(option.palette.text());
117  }
118 
119  for (int i = 0 ; i < lines.count() ; ++i) {
120  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));
121  }
122 
123  return true;
124 }
125 
126 template<typename PROPTYPE>
127 QWidget* PropertyEditorCreator<PROPTYPE>::createWidget(QWidget* parent) const {
128  return new QComboBox(parent);
129 }
130 
131 template<typename PROPTYPE>
132 void PropertyEditorCreator<PROPTYPE>::setEditorData(QWidget* w, const QVariant& val,bool isMandatory,tlp::Graph* g) {
133  if (g == NULL) {
134  w->setEnabled(false);
135  return;
136  }
137 
138  PROPTYPE* prop = val.value<PROPTYPE*>();
139  QComboBox* combo = static_cast<QComboBox*>(w);
140  GraphPropertiesModel<PROPTYPE>* model = NULL;
141 
142  if (isMandatory)
143  model = new GraphPropertiesModel<PROPTYPE>(g,false,combo);
144  else
145  model = new GraphPropertiesModel<PROPTYPE>(QObject::trUtf8("Select a property"),g,false,combo);
146 
147  combo->setModel(model);
148  combo->setCurrentIndex(model->rowOf(prop));
149 }
150 
151 template<typename PROPTYPE>
152 QVariant PropertyEditorCreator<PROPTYPE>::editorData(QWidget* w,tlp::Graph* g) {
153  if (g == NULL)
154  return QVariant();
155 
156  QComboBox* combo = static_cast<QComboBox*>(w);
157  GraphPropertiesModel<PROPTYPE>* model = static_cast<GraphPropertiesModel<PROPTYPE> *>(combo->model());
158  QVariant var = model->data(model->index(combo->currentIndex(),0),TulipModel::PropertyRole);
160  PROPTYPE* prop = (PROPTYPE*)(pi);
161  return QVariant::fromValue<PROPTYPE*>(prop);
162 }
163 
164 template<typename PROPTYPE>
165 QString PropertyEditorCreator<PROPTYPE>::displayText(const QVariant& v) const {
166  PROPTYPE *prop = v.value<PROPTYPE*>();
167 
168  if (prop==NULL)
169  return QObject::trUtf8("Select a property");
170 
171  return QString::fromUtf8(prop->getName().c_str());
172 }
173 
174 template<typename ElementType>
175 QWidget* VectorEditorCreator<ElementType>::createWidget(QWidget*) const {
176  VectorEditionWidget* w = new VectorEditionWidget(NULL);
177  w->setWindowFlags(Qt::Dialog);
178  w->setWindowModality(Qt::ApplicationModal);
179  return w;
180 
181 }
182 
183 template<typename ElementType>
184 void VectorEditorCreator<ElementType>::setEditorData(QWidget* editor, const QVariant& v,bool,tlp::Graph*) {
185  QVector<QVariant> editorData;
186  std::vector<ElementType> vect = v.value<std::vector<ElementType> >();
187 
188  for (size_t i=0; i < vect.size(); ++i) {
189  editorData.push_back(QVariant::fromValue<ElementType>(vect[i]));
190  }
191 
192  static_cast<VectorEditionWidget*>(editor)->setVector(editorData,qMetaTypeId<ElementType>());
193 
194  static_cast<VectorEditionWidget*>(editor)->move(QCursor::pos());
195 }
196 
197 template<typename ElementType>
198 QVariant VectorEditorCreator<ElementType>::editorData(QWidget* editor,tlp::Graph*) {
199  std::vector<ElementType> result;
200  QVector<QVariant> editorData = static_cast<VectorEditionWidget*>(editor)->vector();
201  foreach(QVariant v, editorData)
202  result.push_back(v.value<ElementType>());
203  return QVariant::fromValue<std::vector<ElementType> >(result);
204 }
205 
206 template<typename ElementType>
207 QString VectorEditorCreator<ElementType>::displayText(const QVariant &data) const {
208  std::vector<ElementType> v = data.value<std::vector<ElementType> >();
209 
210  if (v.empty())
211  return QString();
212 
213  if (v.size() == 1)
214  return QString("1 element");
215 
216  return QString::number(v.size()) + QObject::trUtf8(" elements");
217 }
218 
219 }