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