File: TulipItemDelegate.cpp

package info (click to toggle)
tulip 3.7.0dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 39,428 kB
  • sloc: cpp: 231,403; php: 11,023; python: 1,128; sh: 671; yacc: 522; makefile: 315; xml: 63; lex: 55
file content (273 lines) | stat: -rwxr-xr-x 11,733 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
 *
 * This file is part of Tulip (www.tulip-software.org)
 *
 * Authors: David Auber and the Tulip development Team
 * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
 *
 * Tulip is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Tulip is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 */
#include "TulipItemDelegate.h"

#include <tulip/Color.h>

#include "tulip/ColorButton.h"
#include "tulip/TlpQtTools.h"
#include "tulip/FileNameEditorWidget.h"
#include "tulip/CoordWidget.h"
#include "tulip/SizeWidget.h"
#include "tulip/ColorButton.h"

#include "VectorEditionWidget.h"
#include "ElementCollection.h"
#include "TulipQVariantBuilder.h"

#include <QtGui/QLineEdit>
#include <QtGui/QComboBox>
#include <QtGui/QPainter>
#include <QtGui/QApplication>
#include <QtGui/QTextLayout>

using namespace tlp;
using namespace std;

QWidget* TulipItemDelegate::createEditor(QWidget* p, const QStyleOptionViewItem& option,
    const QModelIndex& index) const {
  QVariant data = index.data(Qt::EditRole);

  //Create the right editor in function of the data type.
  //If the data are int check if there is Glyph, Edge Extremity or Edge shape
  if(data.userType() == QVariant::Double) {
    QLineEdit *lineEdit = new QLineEdit(p);
    lineEdit->setValidator(new QDoubleValidator(lineEdit));
    lineEdit->setText(QString::number(data.toDouble()));
    return lineEdit;
  }
  else

    //If data are QString check if there is file.
    if(data.userType() == QVariant::String) {
      return QStyledItemDelegate::createEditor(p, option, index);
    }
    else

      //Create editor for user defined type.
      if(data.userType() == qMetaTypeId< Color >()) {
        ColorButton* button =  new ColorButton(p);
        button->setColor(colorToQColor(data.value<Color>()));
        button->setFocusPolicy(Qt::StrongFocus);
        return button;
      }
      else if(data.userType() == qMetaTypeId< Coord >()) {
        CoordWidget *editor = new CoordWidget(p);
        editor->setCoord(data.value<Coord>());
        editor->setAutoFillBackground(true);
        editor->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
        editor->setFocusPolicy(Qt::StrongFocus);
        return editor;
      }
      else if(data.userType() == qMetaTypeId< Size >()) {
        SizeWidget * editor = new  SizeWidget(p);
        editor->setSize(data.value<Size>());
        editor->setAutoFillBackground(true);
        editor->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
        editor->setFocusPolicy(Qt::StrongFocus);
        return editor;
      }
      else if(data.userType() == qMetaTypeId< vector<bool > >()) {
        VectorEditionWidget *vectorWidget = new VectorEditionWidget(p);
        vectorWidget->setInterface(BOOLEAN_PROPERTY_RTTI,new ListPropertyWidgetTypeManger<BooleanType>(data.value<vector<bool> >()));
        return vectorWidget;
      }
      else if(data.userType() == qMetaTypeId< vector<Color > >()) {
        VectorEditionWidget *vectorWidget = new VectorEditionWidget(p);
        vectorWidget->setInterface(COLORPROPERTY_RTTI,new ListPropertyWidgetTypeManger<ColorType>(data.value<vector<Color> >()));
        return vectorWidget;
      }
      else if(data.userType() == qMetaTypeId< vector<Coord > >()) {
        VectorEditionWidget *vectorWidget = new VectorEditionWidget(p);
        vectorWidget->setInterface(LAYOUTPROPERTY_RTTI,new ListPropertyWidgetTypeManger<PointType>(data.value<vector<Coord> >()));
        return vectorWidget;
      }
      else if(data.userType() == qMetaTypeId< vector<double > >()) {
        VectorEditionWidget *vectorWidget = new VectorEditionWidget(p);
        vectorWidget->setInterface(DOUBLEPROPERTY_RTTI,new ListPropertyWidgetTypeManger<DoubleType>(data.value<vector<double> >()));
        return vectorWidget;
      }
      else if(data.userType() == qMetaTypeId< vector<int > >()) {
        VectorEditionWidget *vectorWidget = new VectorEditionWidget(p);
        vectorWidget->setInterface(INTEGER_PROPERTY_RTTI,new ListPropertyWidgetTypeManger<IntegerType>(data.value<vector<int> >()));
        return vectorWidget;
      }
      else if(data.userType() == qMetaTypeId< vector<Size > >()) {
        VectorEditionWidget *vectorWidget = new VectorEditionWidget(p);
        vectorWidget->setInterface(SIZEPROPERTY_RTTI,new ListPropertyWidgetTypeManger<SizeType>(data.value<vector<Size> >()));
        return vectorWidget;
      }
      else if(data.userType() == qMetaTypeId< vector<string > >()) {
        VectorEditionWidget *vectorWidget = new VectorEditionWidget(p);
        vectorWidget->setInterface(STRINGPROPERTY_RTTI,new ListPropertyWidgetTypeManger<StringType>(data.value<vector<string> >()));
        return vectorWidget;
      }
      else if(data.userType() == qMetaTypeId< ElementCollection >()) {
        ElementCollection *collection = new ElementCollection(data.value< ElementCollection >());
        QComboBox *comboBox = new QComboBox(p);
        comboBox->setModel(collection);
        collection->setParent(comboBox);
        QList<int> selection = collection->selectedElement();

        if(!selection.empty()) {
          comboBox->setCurrentIndex(selection.front());
        }

        return comboBox;
      }
      else if(data.userType() == qMetaTypeId< FilteredUrl >()) {
        FilteredUrl url = data.value<FilteredUrl>();
        return createFileNameEditor(p,url.path(),url.extensionsFilters());
      }
      else {
        return QStyledItemDelegate::createEditor(p, option, index);
      }

  return QStyledItemDelegate::createEditor(p, option, index);
}

void TulipItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
                                     const QModelIndex& index) const {
  QVariant data = index.data(Qt::EditRole);

  if(data.userType() == QVariant::Double) {
    QLineEdit* lineEdit = static_cast<QLineEdit*>(editor);
    model->setData(index,QVariant(lineEdit->text().toDouble()));
  }
  else if(data.userType() == qMetaTypeId< Color >()) {
    QVariant v;
    v.setValue<Color>(QColorToColor(((ColorButton*)editor)->color()));
    model->setData(index,v);
  }
  else if(data.userType() == qMetaTypeId< Coord >()) {
    QVariant v;
    v.setValue<Coord>(((CoordWidget*)editor)->coord());
    model->setData(index,v);
  }
  else if(data.userType() == qMetaTypeId< Size >()) {
    QVariant v;
    v.setValue<Size>(((SizeWidget*)editor)->size());
    model->setData(index,v);
  }
  else if(data.userType() == qMetaTypeId< vector<bool > >()) {
    QVariant v;
    v.setValue<vector<bool > >(((ListPropertyWidgetTypeManger<BooleanType>*)((VectorEditionWidget*)editor)->getInterface())->getResultValue());
    model->setData(index, v);
  }
  else if(data.userType() == qMetaTypeId< vector<Color > >()) {
    QVariant v;
    v.setValue<vector<Color > >(((ListPropertyWidgetTypeManger<ColorType>*)((VectorEditionWidget*)editor)->getInterface())->getResultValue());
    model->setData(index, v);
  }
  else if(data.userType() == qMetaTypeId< vector<Coord > >()) {
    QVariant v;
    v.setValue<vector<Coord > >(((ListPropertyWidgetTypeManger<PointType>*)((VectorEditionWidget*)editor)->getInterface())->getResultValue());
    model->setData(index, v);
  }
  else if(data.userType() == qMetaTypeId< vector<double > >()) {
    QVariant v;
    v.setValue<vector<double > >(((ListPropertyWidgetTypeManger<DoubleType>*)((VectorEditionWidget*)editor)->getInterface())->getResultValue());
    model->setData(index, v);
  }
  else if(data.userType() == qMetaTypeId< vector<int > >()) {
    QVariant v;
    v.setValue<vector<int > >(((ListPropertyWidgetTypeManger<IntegerType>*)((VectorEditionWidget*)editor)->getInterface())->getResultValue());
    model->setData(index, v);
  }
  else if(data.userType() == qMetaTypeId< vector<Size > >()) {
    QVariant v;
    v.setValue<vector<Size > >(((ListPropertyWidgetTypeManger<SizeType>*)((VectorEditionWidget*)editor)->getInterface())->getResultValue());
    model->setData(index, v);
  }
  else if(data.userType() == qMetaTypeId< vector<string > >()) {
    QVariant v;
    v.setValue<vector<string > >(((ListPropertyWidgetTypeManger<StringType>*)((VectorEditionWidget*)editor)->getInterface())->getResultValue());
    model->setData(index, v);
  }
  else if(data.userType() == qMetaTypeId< ElementCollection >()) {
    QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
    ElementCollection collection(*qobject_cast<ElementCollection*>(comboBox->model()));
    collection.setAllElementSelection(false);
    collection.setElementSelection(comboBox->currentIndex(),true);
    model->setData(index,QVariant::fromValue<ElementCollection>(collection));
  }
  else if(data.userType() == qMetaTypeId< FilteredUrl >()) {
    FileNameEditorWidget *fileEditor = qobject_cast<FileNameEditorWidget*>(editor);
    FilteredUrl url(fileEditor->fileName());
    model->setData(index,QVariant::fromValue<FilteredUrl>(url));
  }
  else {
    QStyledItemDelegate::setModelData(editor, model, index);
  }
}

QWidget* TulipItemDelegate::createFileNameEditor(QWidget* parent, const QString& defaultFileName, const QString& filenameFilter) const {
  FileNameEditorWidget *editor = new FileNameEditorWidget(parent);
  editor->setFileName(defaultFileName);
  editor->setFilter(filenameFilter);
  editor->setAutoFillBackground(true);
  return editor;
}

void TulipItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const {
  QVariant data = index.data();
  //Get the normalized value of the element.
  QVariant normalizedValue = index.data(NormalizedValueRole);

  //If we have a normalized value represent it as an histogram.
  if(normalizedValue.isValid() && normalizedValue.type()==QVariant::Double) {
    QStyleOptionViewItemV4 opt = static_cast<QStyleOptionViewItemV4>(option);
    initStyleOption(&opt,index);
    painter->save();

    //Change the color of the background if the element is selected
    if (opt.state & QStyle::State_Selected)
      painter->fillRect(opt.rect, opt.palette.highlight());

    //Draw histogram
    double normalization = normalizedValue.toDouble();
    QRect histogramFrame = opt.rect;
    QRect histogramRect(histogramFrame.topLeft(),QSize(histogramFrame.width()*normalization,histogramFrame.height()));
    painter->fillRect(histogramRect,QBrush(Qt::lightGray));
    //Draw value
    QString text = data.toString();
    const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
    QRect boundingRect = opt.rect;
    boundingRect.adjust(textMargin, 0, -textMargin, 0);
    //Change the color of the text if the element is selected
    QPen pen = painter->pen();

    if (opt.state & QStyle::State_Selected)
      pen.setColor(opt.palette.color(QPalette::HighlightedText));
    else
      pen.setColor(opt.palette.color(QPalette::Text));

    painter->setPen(pen);
    QTextOption textOption;
    const bool wrapText = opt.features & QStyleOptionViewItemV2::WrapText;
    textOption.setWrapMode(wrapText ? QTextOption::WordWrap : QTextOption::ManualWrap);
    textOption.setTextDirection(opt.direction);
    textOption.setAlignment(QStyle::visualAlignment(opt.direction, opt.displayAlignment));
    painter->drawText(boundingRect,text,textOption);
    painter->restore();
  }
  else {
    QStyledItemDelegate::paint(painter,option,index);
  }
}