26 #include <tulip/ForEach.h> 27 #include <tulip/DataSet.h> 28 #include <tulip/VectorEditor.h> 29 #include <tulip/GraphPropertiesModel.h> 30 #include <tulip/TlpQtTools.h> 36 QWidget* LineEditEditorCreator<T>::createWidget(QWidget *parent)
const {
37 return new QLineEdit(parent);
41 void LineEditEditorCreator<T>::setEditorData(QWidget* editor,
const QVariant &data,
bool,
tlp::Graph*) {
42 typename T::RealType val = data.value<
typename T::RealType>();
44 static_cast<QLineEdit*
>(editor)->selectAll();
48 QVariant LineEditEditorCreator<T>::editorData(QWidget* editor,
tlp::Graph*) {
51 typename T::RealType val;
53 if (T::fromString(val,strVal))
54 result.setValue<
typename T::RealType>(val);
60 QWidget* MultiLinesEditEditorCreator<T>::createWidget(QWidget *parent)
const {
61 QTextEdit *edit =
new QTextEdit(parent);
62 edit->setFocusPolicy(Qt::StrongFocus);
63 edit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
64 edit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
69 void MultiLinesEditEditorCreator<T>::setEditorData(QWidget* editor,
const QVariant &data,
bool,
tlp::Graph*) {
70 typename T::RealType val = data.value<
typename T::RealType>();
72 static_cast<QTextEdit*
>(editor)->selectAll();
76 QVariant MultiLinesEditEditorCreator<T>::editorData(QWidget* editor,
tlp::Graph*) {
77 std::string strVal =
QStringToTlpString(static_cast<QTextEdit*>(editor)->toPlainText());
79 typename T::RealType val;
81 if (T::fromString(val,strVal))
82 result.setValue<
typename T::RealType>(val);
88 QSize MultiLinesEditEditorCreator<T>::sizeHint(
const QStyleOptionViewItem & option,
const QModelIndex &index)
const {
89 QVariant data = index.model()->data(index);
90 typename T::RealType val = data.value<
typename T::RealType>();
92 QStringList lines = valS.split(QLatin1Char(
'\n'));
93 QFontMetrics fontMetrics(option.font);
97 for (
int i = 0 ; i < lines.count() ; ++i) {
98 QRect textBB = fontMetrics.boundingRect(lines.at(i));
99 height += fontMetrics.boundingRect(
"|").height();
100 width = std::max(width, textBB.width());
104 width = std::min(width, 500);
106 return QSize(width+15, height+5);
110 bool MultiLinesEditEditorCreator<T>::paint(QPainter* painter,
const QStyleOptionViewItem &option,
const QVariant &data)
const {
111 TulipItemEditorCreator::paint(painter,option,data);
112 QRect rect = option.rect;
113 typename T::RealType val = data.value<
typename T::RealType>();
115 QStringList lines = valS.split(QLatin1Char(
'\n'));
117 if (option.state.testFlag(QStyle::State_Selected) && option.showDecorationSelected) {
118 painter->setPen(option.palette.highlightedText().color());
119 painter->setBrush(option.palette.highlightedText());
122 painter->setPen(option.palette.text().color());
123 painter->setBrush(option.palette.text());
126 for (
int i = 0 ; i < lines.count() ; ++i) {
127 painter->drawText(rect.x(), rect.y() + i * rect.height()/lines.count(), rect.width(), rect.height()/lines.count(),Qt::AlignLeft | Qt::AlignVCenter, lines.at(i));
133 template<
typename PROPTYPE>
134 QWidget* PropertyEditorCreator<PROPTYPE>::createWidget(QWidget* parent)
const {
135 return new QComboBox(parent);
138 template<
typename PROPTYPE>
139 void PropertyEditorCreator<PROPTYPE>::setEditorData(QWidget* w,
const QVariant& val,
bool isMandatory,
tlp::Graph* g) {
141 w->setEnabled(
false);
145 PROPTYPE* prop = val.value<PROPTYPE*>();
146 QComboBox* combo =
static_cast<QComboBox*
>(w);
147 GraphPropertiesModel<PROPTYPE>* model = NULL;
150 model =
new GraphPropertiesModel<PROPTYPE>(g,
false,combo);
152 model =
new GraphPropertiesModel<PROPTYPE>(QObject::trUtf8(
"Select a property"),g,
false,combo);
154 combo->setModel(model);
155 combo->setCurrentIndex(model->rowOf(prop));
158 template<
typename PROPTYPE>
159 QVariant PropertyEditorCreator<PROPTYPE>::editorData(QWidget* w,
tlp::Graph* g) {
163 QComboBox* combo =
static_cast<QComboBox*
>(w);
164 GraphPropertiesModel<PROPTYPE>* model =
static_cast<GraphPropertiesModel<PROPTYPE> *
>(combo->model());
165 QVariant var = model->data(model->index(combo->currentIndex(),0),TulipModel::PropertyRole);
167 PROPTYPE* prop = (PROPTYPE*)(pi);
168 return QVariant::fromValue<PROPTYPE*>(prop);
171 template<
typename PROPTYPE>
172 QString PropertyEditorCreator<PROPTYPE>::displayText(
const QVariant& v)
const {
173 PROPTYPE *prop = v.value<PROPTYPE*>();
176 return QObject::trUtf8(
"Select a property");
181 template<
typename ElementType>
182 QWidget* VectorEditorCreator<ElementType>::createWidget(QWidget*)
const {
183 VectorEditor* w =
new VectorEditor(NULL);
184 w->setWindowFlags(Qt::Dialog);
185 w->setWindowModality(Qt::ApplicationModal);
190 template<
typename ElementType>
191 void VectorEditorCreator<ElementType>::setEditorData(QWidget* editor,
const QVariant& v,
bool,
tlp::Graph*) {
192 QVector<QVariant> editorData;
193 std::vector<ElementType> vect = v.value<std::vector<ElementType> >();
195 for (
size_t i=0; i < vect.size(); ++i) {
196 editorData.push_back(QVariant::fromValue<ElementType>(vect[i]));
199 static_cast<VectorEditor*
>(editor)->setVector(editorData,qMetaTypeId<ElementType>());
201 static_cast<VectorEditor*
>(editor)->move(QCursor::pos());
204 template<
typename ElementType>
205 QVariant VectorEditorCreator<ElementType>::editorData(QWidget* editor,
tlp::Graph*) {
206 std::vector<ElementType> result;
207 QVector<QVariant> editorData =
static_cast<VectorEditor*
>(editor)->vector();
209 foreach(QVariant v, editorData)
212 return QVariant::fromValue<std::vector<ElementType> >(result);
217 struct DisplayVectorDataType :
public DataType {
218 DisplayVectorDataType(
void *value) :DataType(value) {}
219 ~DisplayVectorDataType() {
221 DataType* clone()
const {
225 std::string getTypeName()
const {
226 return std::string(
typeid(std::vector<T>).name());
231 template<
typename ElementType>
232 QString VectorEditorCreator<ElementType>::displayText(
const QVariant &data)
const {
233 std::vector<ElementType> v = data.value<std::vector<ElementType> >();
239 DataTypeSerializer* dts =
243 DisplayVectorDataType<ElementType> dt(&v);
245 std::stringstream sstr;
246 dts->writeData(sstr, &dt);
248 std::string str = sstr.str();
252 return truncateText(qstr);
256 return QString(
"1 element");
258 return QString::number(v.size()) + QObject::trUtf8(
" elements");
PropertyInterface describes the interface of a graph property.
std::string QStringToTlpString(const QString &toConvert)
Convert a QString to a Tulip UTF-8 encoded std::string.
static DataTypeSerializer * typenameToSerializer(const std::string &name)
QString tlpStringToQString(const std::string &toConvert)
Convert a Tulip UTF-8 encoded std::string to a QString.