19 #include <QtCore/QSet>
20 #include <QtGui/QLineEdit>
21 #include <QtGui/QTextEdit>
22 #include <QtGui/QComboBox>
24 #include <tulip/ForEach.h>
25 #include <tulip/VectorEditionWidget.h>
26 #include <tulip/GraphPropertiesModel.h>
32 QWidget* LineEditEditorCreator<T>::createWidget(QWidget *parent)
const {
33 return new QLineEdit(parent);
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();
44 QVariant LineEditEditorCreator<T>::editorData(QWidget* editor,
tlp::Graph*) {
45 std::string strVal = std::string(static_cast<QLineEdit*>(editor)->text().toUtf8().data());
47 typename T::RealType val;
49 if (T::fromString(val,strVal))
50 result.setValue<
typename T::RealType>(val);
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);
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();
72 QVariant MultiLinesEditEditorCreator<T>::editorData(QWidget* editor,
tlp::Graph*) {
73 std::string strVal = std::string(static_cast<QTextEdit*>(editor)->toPlainText().toUtf8().data());
75 typename T::RealType val;
77 if (T::fromString(val,strVal))
78 result.setValue<
typename T::RealType>(val);
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);
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());
99 return QSize(width+15, height+5);
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'));
110 if (option.state.testFlag(QStyle::State_Selected) && option.showDecorationSelected) {
111 painter->setPen(option.palette.highlightedText().color());
112 painter->setBrush(option.palette.highlightedText());
115 painter->setPen(option.palette.text().color());
116 painter->setBrush(option.palette.text());
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));
126 template<
typename PROPTYPE>
127 QWidget* PropertyEditorCreator<PROPTYPE>::createWidget(QWidget* parent)
const {
128 return new QComboBox(parent);
131 template<
typename PROPTYPE>
132 void PropertyEditorCreator<PROPTYPE>::setEditorData(QWidget* w,
const QVariant& val,
bool isMandatory,
tlp::Graph* g) {
134 w->setEnabled(
false);
138 PROPTYPE* prop = val.value<PROPTYPE*>();
139 QComboBox* combo =
static_cast<QComboBox*
>(w);
140 GraphPropertiesModel<PROPTYPE>* model = NULL;
143 model =
new GraphPropertiesModel<PROPTYPE>(g,
false,combo);
145 model =
new GraphPropertiesModel<PROPTYPE>(QObject::trUtf8(
"Select a property"),g,
false,combo);
147 combo->setModel(model);
148 combo->setCurrentIndex(model->rowOf(prop));
151 template<
typename PROPTYPE>
152 QVariant PropertyEditorCreator<PROPTYPE>::editorData(QWidget* w,
tlp::Graph* g) {
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);
164 template<
typename PROPTYPE>
165 QString PropertyEditorCreator<PROPTYPE>::displayText(
const QVariant& v)
const {
166 PROPTYPE *prop = v.value<PROPTYPE*>();
169 return QObject::trUtf8(
"Select a property");
171 return QString::fromUtf8(prop->getName().c_str());
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);
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> >();
188 for (
size_t i=0; i < vect.size(); ++i) {
189 editorData.push_back(QVariant::fromValue<ElementType>(vect[i]));
192 static_cast<VectorEditionWidget*
>(editor)->setVector(editorData,qMetaTypeId<ElementType>());
194 static_cast<VectorEditionWidget*
>(editor)->move(QCursor::pos());
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)
203 return QVariant::fromValue<std::vector<ElementType> >(result);
206 template<
typename ElementType>
207 QString VectorEditorCreator<ElementType>::displayText(
const QVariant &data)
const {
208 std::vector<ElementType> v = data.value<std::vector<ElementType> >();
214 return QString(
"1 element");
216 return QString::number(v.size()) + QObject::trUtf8(
" elements");