28 #include <QDoubleSpinBox>
30 #include <tulip/DataSet.h>
31 #include <tulip/VectorEditor.h>
32 #include <tulip/GraphPropertiesModel.h>
33 #include <tulip/TlpQtTools.h>
34 #include <tulip/TulipMetaTypes.h>
35 #include <tulip/ScientificDoubleSpinBox.h>
36 #include <tulip/Perspective.h>
41 QWidget *NumberEditorCreator<T>::createWidget(QWidget *parent)
const {
42 QDoubleSpinBox *dsb =
nullptr;
45 if (
typeid(T).name() ==
typeid(tlp::IntegerType).name() ||
46 typeid(T).name() ==
typeid(tlp::UnsignedIntegerType).name() ||
47 typeid(T).name() ==
typeid(tlp::LongType).name()) {
48 dsb =
new QDoubleSpinBox(parent);
53 dsb =
new tlp::ScientificDoubleSpinBox(parent);
55 dsb->setLocale(QLocale(QLocale::C));
59 if (
typeid(T).name() ==
typeid(tlp::IntegerType).name()) {
60 dsb->setRange(-INT_MAX, INT_MAX);
61 }
else if (
typeid(T).name() ==
typeid(tlp::UnsignedIntegerType).name()) {
62 dsb->setRange(0, UINT_MAX);
63 }
else if (
typeid(T).name() ==
typeid(tlp::LongType).name()) {
64 dsb->setRange(
static_cast<double>(-LONG_MAX),
static_cast<double>(LONG_MAX));
65 }
else if (
typeid(T).name() ==
typeid(tlp::FloatType).name()) {
66 dsb->setRange(-FLT_MAX, FLT_MAX);
68 dsb->setRange(-DBL_MAX, DBL_MAX);
75 void NumberEditorCreator<T>::setEditorData(QWidget *editor,
const QVariant &data,
bool,
77 static_cast<QDoubleSpinBox *
>(editor)->setValue(data.value<
typename T::RealType>());
81 QVariant NumberEditorCreator<T>::editorData(QWidget *editor,
tlp::Graph *) {
84 static_cast<typename T::RealType
>(
static_cast<QDoubleSpinBox *
>(editor)->value()));
89 QWidget *LineEditEditorCreator<T>::createWidget(QWidget *parent)
const {
90 return new QLineEdit(parent);
94 void LineEditEditorCreator<T>::setEditorData(QWidget *editor,
const QVariant &data,
bool,
96 typename T::RealType val = data.value<
typename T::RealType>();
98 static_cast<QLineEdit *
>(editor)->selectAll();
101 template <
typename T>
102 QVariant LineEditEditorCreator<T>::editorData(QWidget *editor,
tlp::Graph *) {
105 typename T::RealType val;
107 if (T::fromString(val, strVal))
108 result.setValue(val);
113 template <
typename T>
114 QWidget *MultiLinesEditEditorCreator<T>::createWidget(QWidget *parent)
const {
115 QTextEdit *edit =
new QTextEdit(parent);
116 edit->setFocusPolicy(Qt::StrongFocus);
117 edit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
118 edit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
122 template <
typename T>
123 void MultiLinesEditEditorCreator<T>::setEditorData(QWidget *editor,
const QVariant &data,
bool,
125 typename T::RealType val = data.value<
typename T::RealType>();
127 static_cast<QTextEdit *
>(editor)->selectAll();
130 template <
typename T>
131 QVariant MultiLinesEditEditorCreator<T>::editorData(QWidget *editor,
tlp::Graph *) {
132 std::string strVal =
QStringToTlpString(
static_cast<QTextEdit *
>(editor)->toPlainText());
134 typename T::RealType val;
136 if (T::fromString(val, strVal))
137 result.setValue(val);
142 template <
typename T>
143 QSize MultiLinesEditEditorCreator<T>::sizeHint(
const QStyleOptionViewItem &option,
144 const QModelIndex &index)
const {
145 QVariant data = index.model()->data(index);
146 typename T::RealType val = data.value<
typename T::RealType>();
148 QStringList lines = valS.split(QLatin1Char(
'\n'));
149 QFontMetrics fontMetrics(option.font);
153 for (
int i = 0; i < lines.count(); ++i) {
154 QRect textBB = fontMetrics.boundingRect(lines.at(i));
155 height += fontMetrics.boundingRect(
"|").height();
156 width = std::max(width, textBB.width());
160 width = std::min(width, 500);
162 return QSize(width + 15, height + 5);
165 template <
typename T>
166 bool MultiLinesEditEditorCreator<T>::paint(QPainter *painter,
const QStyleOptionViewItem &option,
167 const QVariant &data,
const QModelIndex &index)
const {
168 TulipItemEditorCreator::paint(painter, option, data, index);
169 QRect rect = option.rect;
170 typename T::RealType val = data.value<
typename T::RealType>();
172 QStringList lines = valS.split(QLatin1Char(
'\n'));
174 if (option.state.testFlag(QStyle::State_Selected) && option.showDecorationSelected) {
175 painter->setPen(option.palette.highlightedText().color());
176 painter->setBrush(option.palette.highlightedText());
178 painter->setPen(option.palette.text().color());
179 painter->setBrush(option.palette.text());
182 for (
int i = 0; i < lines.count(); ++i) {
183 painter->drawText(rect.x(), rect.y() + i * rect.height() / lines.count(), rect.width(),
184 rect.height() / lines.count(), Qt::AlignLeft | Qt::AlignVCenter, lines.at(i));
190 template <
typename PROPTYPE>
191 QWidget *PropertyEditorCreator<PROPTYPE>::createWidget(QWidget *parent)
const {
192 return new QComboBox(parent);
195 template <
typename PROPTYPE>
196 void PropertyEditorCreator<PROPTYPE>::setEditorData(QWidget *w,
const QVariant &val,
199 w->setEnabled(
false);
203 PROPTYPE *prop = val.value<PROPTYPE *>();
204 QComboBox *combo =
static_cast<QComboBox *
>(w);
205 GraphPropertiesModel<PROPTYPE> *model =
nullptr;
208 model =
new GraphPropertiesModel<PROPTYPE>(g,
false, combo);
210 model =
new GraphPropertiesModel<PROPTYPE>(QObject::tr(
"Select a property"), g,
false, combo);
212 combo->setModel(model);
213 combo->setCurrentIndex(model->rowOf(prop));
216 template <
typename PROPTYPE>
217 QVariant PropertyEditorCreator<PROPTYPE>::editorData(QWidget *w,
tlp::Graph *g) {
221 QComboBox *combo =
static_cast<QComboBox *
>(w);
222 GraphPropertiesModel<PROPTYPE> *model =
223 static_cast<GraphPropertiesModel<PROPTYPE> *
>(combo->model());
224 QVariant var = model->data(model->index(combo->currentIndex(), 0), TulipModel::PropertyRole);
226 PROPTYPE *prop =
static_cast<PROPTYPE *
>(pi);
227 return QVariant::fromValue<PROPTYPE *>(prop);
230 template <
typename PROPTYPE>
231 QString PropertyEditorCreator<PROPTYPE>::displayText(
const QVariant &v)
const {
232 PROPTYPE *prop = v.value<PROPTYPE *>();
235 return QObject::tr(
"Select a property");
240 template <
typename ElementType>
241 QWidget *VectorEditorCreator<ElementType>::createWidget(QWidget *)
const {
242 VectorEditor *w =
new VectorEditor(
245 w->setWindowFlags(Qt::Dialog);
246 w->setWindowModality(Qt::ApplicationModal);
250 template <
typename ElementType>
251 void VectorEditorCreator<ElementType>::setEditorData(QWidget *editor,
const QVariant &v,
bool,
253 QVector<QVariant> editorData;
254 std::vector<ElementType> vect = v.value<std::vector<ElementType>>();
256 for (
size_t i = 0; i < vect.size(); ++i) {
257 editorData.push_back(QVariant::fromValue<ElementType>(vect[i]));
260 static_cast<VectorEditor *
>(editor)->setVector(editorData, qMetaTypeId<ElementType>());
262 static_cast<VectorEditor *
>(editor)->move(QCursor::pos());
265 template <
typename ElementType>
266 QVariant VectorEditorCreator<ElementType>::editorData(QWidget *editor,
tlp::Graph *) {
267 std::vector<ElementType> result;
268 QVector<QVariant> editorData =
static_cast<VectorEditor *
>(editor)->vector();
270 for (
const QVariant &v : editorData)
273 return QVariant::fromValue<std::vector<ElementType>>(result);
277 template <
typename T>
278 struct DisplayVectorDataType :
public DataType {
279 DisplayVectorDataType(
void *value) : DataType(value) {}
280 ~DisplayVectorDataType()
override {}
281 DataType *clone()
const override {
285 std::string getTypeName()
const override {
286 return std::string(
typeid(std::vector<T>).name());
290 template <
typename ElementType>
291 QString VectorEditorCreator<ElementType>::displayText(
const QVariant &data)
const {
292 std::vector<ElementType> v = data.value<std::vector<ElementType>>();
301 DisplayVectorDataType<ElementType> dt(&v);
303 std::stringstream sstr;
304 dts->writeData(sstr, &dt);
306 std::string str = sstr.str();
310 return truncateText(qstr);
314 return QString(
"1 element");
316 return QString::number(v.size()) + QObject::tr(
" elements");
static DataTypeSerializer * typenameToSerializer(const std::string &name)
static tlp::Perspective * instance()
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.
QString tlpStringToQString(const std::string &toConvert)
Convert a Tulip UTF-8 encoded std::string to a QString.