25 #include <tulip/ForEach.h>
26 #include <tulip/VectorEditionWidget.h>
27 #include <tulip/GraphPropertiesModel.h>
33 QWidget* LineEditEditorCreator<T>::createWidget(QWidget *parent)
const {
34 return new QLineEdit(parent);
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();
45 QVariant LineEditEditorCreator<T>::editorData(QWidget* editor,
tlp::Graph*) {
46 std::string strVal = std::string(static_cast<QLineEdit*>(editor)->text().toUtf8().data());
48 typename T::RealType val;
50 if (T::fromString(val,strVal))
51 result.setValue<
typename T::RealType>(val);
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);
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();
73 QVariant MultiLinesEditEditorCreator<T>::editorData(QWidget* editor,
tlp::Graph*) {
74 std::string strVal = std::string(static_cast<QTextEdit*>(editor)->toPlainText().toUtf8().data());
76 typename T::RealType val;
78 if (T::fromString(val,strVal))
79 result.setValue<
typename T::RealType>(val);
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);
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());
100 return QSize(width+15, height+5);
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'));
111 if (option.state.testFlag(QStyle::State_Selected) && option.showDecorationSelected) {
112 painter->setPen(option.palette.highlightedText().color());
113 painter->setBrush(option.palette.highlightedText());
116 painter->setPen(option.palette.text().color());
117 painter->setBrush(option.palette.text());
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));
127 template<
typename PROPTYPE>
128 QWidget* PropertyEditorCreator<PROPTYPE>::createWidget(QWidget* parent)
const {
129 return new QComboBox(parent);
132 template<
typename PROPTYPE>
133 void PropertyEditorCreator<PROPTYPE>::setEditorData(QWidget* w,
const QVariant& val,
bool isMandatory,
tlp::Graph* g) {
135 w->setEnabled(
false);
139 PROPTYPE* prop = val.value<PROPTYPE*>();
140 QComboBox* combo =
static_cast<QComboBox*
>(w);
141 GraphPropertiesModel<PROPTYPE>* model = NULL;
144 model =
new GraphPropertiesModel<PROPTYPE>(g,
false,combo);
146 model =
new GraphPropertiesModel<PROPTYPE>(QObject::trUtf8(
"Select a property"),g,
false,combo);
148 combo->setModel(model);
149 combo->setCurrentIndex(model->rowOf(prop));
152 template<
typename PROPTYPE>
153 QVariant PropertyEditorCreator<PROPTYPE>::editorData(QWidget* w,
tlp::Graph* g) {
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);
165 template<
typename PROPTYPE>
166 QString PropertyEditorCreator<PROPTYPE>::displayText(
const QVariant& v)
const {
167 PROPTYPE *prop = v.value<PROPTYPE*>();
170 return QObject::trUtf8(
"Select a property");
172 return QString::fromUtf8(prop->getName().c_str());
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);
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> >();
189 for (
size_t i=0; i < vect.size(); ++i) {
190 editorData.push_back(QVariant::fromValue<ElementType>(vect[i]));
193 static_cast<VectorEditionWidget*
>(editor)->setVector(editorData,qMetaTypeId<ElementType>());
195 static_cast<VectorEditionWidget*
>(editor)->move(QCursor::pos());
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)
204 return QVariant::fromValue<std::vector<ElementType> >(result);
207 template<
typename ElementType>
208 QString VectorEditorCreator<ElementType>::displayText(
const QVariant &data)
const {
209 std::vector<ElementType> v = data.value<std::vector<ElementType> >();
215 return QString(
"1 element");
217 return QString::number(v.size()) + QObject::trUtf8(
" elements");