File: ViewControl_TableItemDelegate.cxx

package info (click to toggle)
opencascade 7.9.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 301,932 kB
  • sloc: cpp: 1,523,264; tcl: 10,159; cs: 5,173; java: 1,554; sh: 1,342; ansic: 827; xml: 699; makefile: 31; javascript: 22
file content (220 lines) | stat: -rw-r--r-- 8,327 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
// Created on: 2021-04-27
// Created by: Natalia ERMOLAEVA
// Copyright (c) 2021 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.

#include <inspector/ViewControl_TableItemDelegate.hxx>
#include <inspector/ViewControl_ColorSelector.hxx>
#include <inspector/ViewControl_TableModelValues.hxx>
#include <inspector/ViewControl_EditType.hxx>
#include <inspector/TreeModel_ItemProperties.hxx>

#include <Standard_WarningsDisable.hxx>
#include <QFont>
#include <QCheckBox>
#include <QComboBox>
#include <QDoubleValidator>
#include <QLineEdit>
#include <QPushButton>
#include <QSpinBox>
#include <Standard_WarningsRestore.hxx>

// =======================================================================
// function : Constructor
// purpose :
// =======================================================================
ViewControl_TableItemDelegate::ViewControl_TableItemDelegate(QObject* theParent)
    : QItemDelegate(theParent),
      myModelValues(0)
{
}

// =======================================================================
// function : createEditor
// purpose :
// =======================================================================
QWidget* ViewControl_TableItemDelegate::createEditor(QWidget* theParent,
                                                     const QStyleOptionViewItem&,
                                                     const QModelIndex& theIndex) const
{
  if (!myModelValues)
    return 0;

  int                  aRow       = theIndex.row();
  int                  aColumn    = theIndex.column();
  ViewControl_EditType anEditType = myModelValues->EditType(aRow, aColumn);

  QWidget* anEditor = createEditorControl(theParent, anEditType);
  initEditorParameters(anEditor, anEditType, myModelValues, aRow, aColumn);
  return anEditor;
}

// =======================================================================
// function : setEditorData
// purpose :
// =======================================================================
void ViewControl_TableItemDelegate::setEditorData(QWidget*           theEditor,
                                                  const QModelIndex& theIndex) const
{
  if (!myModelValues)
    return;

  int                  aRow       = theIndex.row();
  int                  aColumn    = theIndex.column();
  ViewControl_EditType anEditType = myModelValues->EditType(aRow, aColumn);

  setEditorValue(theEditor, anEditType, theIndex.model()->data(theIndex));
}

// =======================================================================
// function : setModelData
// purpose :
// =======================================================================
void ViewControl_TableItemDelegate::setModelData(QWidget*            theEditor,
                                                 QAbstractItemModel* theModel,
                                                 const QModelIndex&  theIndex) const
{
  if (!myModelValues)
    return;

  int                  aRow       = theIndex.row();
  int                  aColumn    = theIndex.column();
  ViewControl_EditType anEditType = myModelValues->EditType(aRow, aColumn);
  theModel->setData(theIndex, getEditorValue(theEditor, anEditType));
}

// =======================================================================
// function : createEditorControl
// purpose :
// =======================================================================
QWidget* ViewControl_TableItemDelegate::createEditorControl(QWidget*                   theParent,
                                                            const ViewControl_EditType theEditType)
{
  switch (theEditType)
  {
    case ViewControl_EditType_None:
      return 0;
    case ViewControl_EditType_Bool:
      return new QComboBox(theParent);
    case ViewControl_EditType_Color:
      return new ViewControl_ColorSelector(theParent);
    case ViewControl_EditType_Double: {
      QLineEdit* aLineEdit = new QLineEdit(theParent);
      aLineEdit->setValidator(new QDoubleValidator(theParent));
      return aLineEdit;
    }
    case ViewControl_EditType_Line:
      return new QLineEdit(theParent);
    case ViewControl_EditType_Spin: {
      QSpinBox* aSpinBox = new QSpinBox(theParent);
      aSpinBox->setRange(IntegerFirst(), IntegerLast());
      return aSpinBox;
    }
    case ViewControl_EditType_DoAction:
      return new QPushButton(theParent);

    default:
      return 0;
  }
}

// =======================================================================
// function : initEditorParameters
// purpose :
// =======================================================================
void ViewControl_TableItemDelegate::initEditorParameters(QWidget*                   theEditor,
                                                         const ViewControl_EditType theEditType,
                                                         ViewControl_TableModelValues*,
                                                         const int,
                                                         const int)
{
  switch (theEditType)
  {
    case ViewControl_EditType_Bool: {
      (qobject_cast<QComboBox*>(theEditor))->insertItem(0, "true");
      (qobject_cast<QComboBox*>(theEditor))->insertItem(1, "false");
      break;
    }
    case ViewControl_EditType_Double: {
    }
    default:
      break;
  }
}

// =======================================================================
// function : setEditorValue
// purpose :
// =======================================================================
void ViewControl_TableItemDelegate::setEditorValue(QWidget*                   theEditor,
                                                   const ViewControl_EditType theEditType,
                                                   const QVariant&            theValue) const
{
  switch (theEditType)
  {
    case ViewControl_EditType_None:
      break;
    case ViewControl_EditType_Bool:
      (qobject_cast<QComboBox*>(theEditor))->setCurrentIndex(theValue.toBool() ? 0 : 1);
      break;
    case ViewControl_EditType_Color: {
      if (!myModelValues)
        break;

      const TCollection_AsciiString& aStreamValue = myModelValues->Properties()->StreamValue();
      (qobject_cast<ViewControl_ColorSelector*>(theEditor))
        ->SetStreamValue(aStreamValue.ToCString());
      break;
    }
    case ViewControl_EditType_Double:
    case ViewControl_EditType_Line:
      (qobject_cast<QLineEdit*>(theEditor))->setText(theValue.toString());
      break;
    case ViewControl_EditType_Spin:
      (qobject_cast<QSpinBox*>(theEditor))->setValue(theValue.toInt());
      break;
    case ViewControl_EditType_DoAction:
      (qobject_cast<QPushButton*>(theEditor))->setText("UnSelect");
      break;
    default:
      break;
  }
}

// =======================================================================
// function : getEditorValue
// purpose :
// =======================================================================

QVariant ViewControl_TableItemDelegate::getEditorValue(QWidget*                   theEditor,
                                                       const ViewControl_EditType theEditType)
{
  switch (theEditType)
  {
    case ViewControl_EditType_None:
      return QVariant();
    case ViewControl_EditType_Bool:
      return (qobject_cast<QComboBox*>(theEditor))->currentIndex() == 0 ? true : false;
    case ViewControl_EditType_Color:
      return (qobject_cast<ViewControl_ColorSelector*>(theEditor))->GetStreamValue();
    case ViewControl_EditType_Double:
    case ViewControl_EditType_Line:
      return (qobject_cast<QLineEdit*>(theEditor))->text();
    case ViewControl_EditType_Spin:
      return (qobject_cast<QSpinBox*>(theEditor))->value();
    case ViewControl_EditType_DoAction:
      return QVariant("Clicked");
    default:
      return QVariant();
  }
}