26 #include <tulip/ForEach.h>
27 #include <tulip/DataSet.h>
28 #include <tulip/VectorEditor.h>
29 #include <tulip/GraphPropertiesModel.h>
35 QWidget* LineEditEditorCreator<T>::createWidget(QWidget *parent)
const {
36 return new QLineEdit(parent);
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();
47 QVariant LineEditEditorCreator<T>::editorData(QWidget* editor,
tlp::Graph*) {
48 std::string strVal = std::string(static_cast<QLineEdit*>(editor)->text().toUtf8().data());
50 typename T::RealType val;
52 if (T::fromString(val,strVal))
53 result.setValue<
typename T::RealType>(val);
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);
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();
75 QVariant MultiLinesEditEditorCreator<T>::editorData(QWidget* editor,
tlp::Graph*) {
76 std::string strVal = std::string(static_cast<QTextEdit*>(editor)->toPlainText().toUtf8().data());
78 typename T::RealType val;
80 if (T::fromString(val,strVal))
81 result.setValue<
typename T::RealType>(val);
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);
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());
102 return QSize(width+15, height+5);
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'));
113 if (option.state.testFlag(QStyle::State_Selected) && option.showDecorationSelected) {
114 painter->setPen(option.palette.highlightedText().color());
115 painter->setBrush(option.palette.highlightedText());
118 painter->setPen(option.palette.text().color());
119 painter->setBrush(option.palette.text());
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));
129 template<
typename PROPTYPE>
130 QWidget* PropertyEditorCreator<PROPTYPE>::createWidget(QWidget* parent)
const {
131 return new QComboBox(parent);
134 template<
typename PROPTYPE>
135 void PropertyEditorCreator<PROPTYPE>::setEditorData(QWidget* w,
const QVariant& val,
bool isMandatory,
tlp::Graph* g) {
137 w->setEnabled(
false);
141 PROPTYPE* prop = val.value<PROPTYPE*>();
142 QComboBox* combo =
static_cast<QComboBox*
>(w);
143 GraphPropertiesModel<PROPTYPE>* model = NULL;
146 model =
new GraphPropertiesModel<PROPTYPE>(g,
false,combo);
148 model =
new GraphPropertiesModel<PROPTYPE>(QObject::trUtf8(
"Select a property"),g,
false,combo);
150 combo->setModel(model);
151 combo->setCurrentIndex(model->rowOf(prop));
154 template<
typename PROPTYPE>
155 QVariant PropertyEditorCreator<PROPTYPE>::editorData(QWidget* w,
tlp::Graph* g) {
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);
167 template<
typename PROPTYPE>
168 QString PropertyEditorCreator<PROPTYPE>::displayText(
const QVariant& v)
const {
169 PROPTYPE *prop = v.value<PROPTYPE*>();
172 return QObject::trUtf8(
"Select a property");
174 return QString::fromUtf8(prop->getName().c_str());
177 template<
typename ElementType>
178 QWidget* VectorEditorCreator<ElementType>::createWidget(QWidget*)
const {
179 VectorEditor* w =
new VectorEditor(NULL);
180 w->setWindowFlags(Qt::Dialog);
181 w->setWindowModality(Qt::ApplicationModal);
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> >();
191 for (
size_t i=0; i < vect.size(); ++i) {
192 editorData.push_back(QVariant::fromValue<ElementType>(vect[i]));
195 static_cast<VectorEditor*
>(editor)->setVector(editorData,qMetaTypeId<ElementType>());
197 static_cast<VectorEditor*
>(editor)->move(QCursor::pos());
200 template<
typename ElementType>
201 QVariant VectorEditorCreator<ElementType>::editorData(QWidget* editor,
tlp::Graph*) {
202 std::vector<ElementType> result;
203 QVector<QVariant> editorData =
static_cast<VectorEditor*
>(editor)->vector();
204 foreach(QVariant v, editorData)
206 return QVariant::fromValue<std::vector<ElementType> >(result);
211 struct DisplayVectorDataType :
public DataType {
212 DisplayVectorDataType(
void *value) :DataType(value) {}
213 ~DisplayVectorDataType() {
215 DataType* clone()
const {
219 std::string getTypeName()
const {
220 return std::string(
typeid(std::vector<T>).name());
225 template<
typename ElementType>
226 QString VectorEditorCreator<ElementType>::displayText(
const QVariant &data)
const {
227 std::vector<ElementType> v = data.value<std::vector<ElementType> >();
233 DataTypeSerializer* dts =
237 DisplayVectorDataType<ElementType> dt(&v);
239 std::stringstream sstr;
240 dts->writeData(sstr, &dt);
242 std::string str = sstr.str();
244 QString qstr = QString::fromUtf8(str.c_str());
246 if (qstr.size() > 45) {
255 return QString(
"1 element");
257 return QString::number(v.size()) + QObject::trUtf8(
" elements");
PropertyInterface describes the interface of a graph property.
static DataTypeSerializer * typenameToSerializer(const std::string &name)