@@ -36,21 +36,61 @@ void caf::PdmField<DataType>::setValueFromUi(const QVariant& uiValue)
36
36
{
37
37
QVariant oldValue = PdmFieldTypeSpecialization<DataType>::convert (m_fieldValue);
38
38
39
+ // Check whether we are handling selections of values or actual values
39
40
if (m_optionEntryCache.size ())
40
41
{
41
- // Check if we got an index into the option list
42
+ // This has an option based GUI, the uiValue is only indexes into the m_optionEntryCache
42
43
if (uiValue.type () == QVariant::UInt)
43
44
{
44
45
assert (uiValue.toUInt () < static_cast <unsigned int >(m_optionEntryCache.size ()));
45
46
PdmFieldTypeSpecialization<DataType>::setFromVariant (m_optionEntryCache[uiValue.toUInt ()].value , m_fieldValue);
46
47
}
47
- else
48
+ else if (uiValue. type () == QVariant::List)
48
49
{
50
+ QList<QVariant> selectedIndexes = uiValue.toList ();
51
+ QList<QVariant> valuesToSetInField;
52
+
53
+ if (selectedIndexes.isEmpty ())
54
+ {
55
+ PdmFieldTypeSpecialization<DataType>::setFromVariant (valuesToSetInField, m_fieldValue);
56
+ }
57
+ else
58
+ {
59
+ if (selectedIndexes.front ().type () == QVariant::UInt)
60
+ {
61
+ for (int i = 0 ; i < selectedIndexes.size (); ++i)
62
+ {
63
+ unsigned int opIdx = selectedIndexes[i].toUInt ();
64
+ if (opIdx < static_cast <unsigned int >(m_optionEntryCache.size ()))
65
+ {
66
+ valuesToSetInField.push_back (m_optionEntryCache[opIdx].value );
67
+ }
68
+ }
69
+
70
+ PdmFieldTypeSpecialization<DataType>::setFromVariant (valuesToSetInField, m_fieldValue);
71
+ }
72
+ else
73
+ {
74
+ // We are not getting indexes as expected from the UI. For now assert, to catch this condition
75
+ // but it should possibly be handled as setting the values explicitly. The code for that is below the assert
76
+ assert (false );
77
+ PdmFieldTypeSpecialization<DataType>::setFromVariant (uiValue, m_fieldValue);
78
+ m_optionEntryCache.clear ();
79
+ }
80
+ }
81
+
82
+ }
83
+ else
84
+ {
85
+ // We are not getting indexes as expected from the UI. For now assert, to catch this condition
86
+ // but it should possibly be handled as setting the values explicitly. The code for that is below the assert
87
+ assert (false );
88
+ PdmFieldTypeSpecialization<DataType>::setFromVariant (uiValue, m_fieldValue);
49
89
m_optionEntryCache.clear ();
50
90
}
51
91
}
52
92
else
53
- {
93
+ { // Not an option based GUI, the uiValue is a real field value
54
94
PdmFieldTypeSpecialization<DataType>::setFromVariant (uiValue, m_fieldValue);
55
95
}
56
96
@@ -68,51 +108,109 @@ void caf::PdmField<DataType>::setValueFromUi(const QVariant& uiValue)
68
108
}
69
109
70
110
// --------------------------------------------------------------------------------------------------
71
- // /
111
+ // / Returns the option values that is to be displayed in the UI for this field.
112
+ // / This method calls the virtual PdmObject::calculateValueOptions to get the list provided from the
113
+ // / application, then possibly adds the current field value(s) to the list, to
114
+ // / make sure the actual values are shown
72
115
// --------------------------------------------------------------------------------------------------
73
116
template <typename DataType >
74
117
QList<PdmOptionItemInfo> caf::PdmField<DataType>::valueOptions(bool * useOptionsOnly)
75
118
{
119
+ // First check if the owner PdmObject has a value options specification.
120
+ // if it has, use it.
76
121
if (m_ownerObject)
77
122
{
78
123
m_optionEntryCache = m_ownerObject->calculateValueOptions (this , useOptionsOnly);
79
124
if (m_optionEntryCache.size ())
80
125
{
81
- // Find this field value in the list if present
126
+ // Make sure the options contain the field values, event though they not necessarily
127
+ // is supplied as possible options by the application. This is a convenience making sure
128
+ // the actual data in the pdmObject is shown correctly in the UI, and to prevent accidental
129
+ // changes of the field values
130
+
131
+ // Find the field value(s) in the list if present
132
+
82
133
QVariant convertedFieldValue = PdmFieldTypeSpecialization<DataType>::convert (m_fieldValue);
83
- unsigned int index ;
84
- bool foundFieldValue = PdmOptionItemInfo::findValue (m_optionEntryCache, convertedFieldValue, &index );
85
134
86
- // If not found, we have to add it to the list, to be able to show it
135
+ std::vector<unsigned int > foundIndexes;
136
+ bool foundAllFieldValues = PdmOptionItemInfo::findValues (m_optionEntryCache, convertedFieldValue, foundIndexes);
137
+
138
+ // If not all are found, we have to add the missing to the list, to be able to show it
87
139
88
- if (!foundFieldValue )
140
+ if (!foundAllFieldValues )
89
141
{
90
- m_optionEntryCache.push_front (PdmOptionItemInfo (convertedFieldValue.toString (), convertedFieldValue, true , QIcon ()));
142
+ if (convertedFieldValue.type () != QVariant::List) // Single value field
143
+ {
144
+ m_optionEntryCache.push_front (PdmOptionItemInfo (convertedFieldValue.toString (), convertedFieldValue, true , QIcon ()));
145
+ }
146
+ else // The field value is a list of values
147
+ {
148
+ QList<QVariant> valuesSelectedInField = convertedFieldValue.toList ();
149
+ for (int i= 0 ; i < valuesSelectedInField.size (); ++i)
150
+ {
151
+ bool isFound = false ;
152
+ for (unsigned int opIdx = 0 ; opIdx < static_cast <unsigned int >(m_optionEntryCache.size ()); ++opIdx)
153
+ {
154
+ if (valuesSelectedInField[i] == m_optionEntryCache[opIdx].value ) isFound = true ;
155
+ }
156
+
157
+ if (!isFound)
158
+ {
159
+ m_optionEntryCache.push_front (PdmOptionItemInfo (valuesSelectedInField[i].toString (), valuesSelectedInField[i], true , QIcon ()));
160
+ }
161
+ }
162
+ }
91
163
}
92
164
93
- if (m_optionEntryCache. size ()) return m_optionEntryCache;
165
+ return m_optionEntryCache;
94
166
}
95
167
}
96
168
169
+ // If we have no options, use the options defined by the type. Normally only caf::AppEnum type
170
+
97
171
return PdmFieldTypeSpecialization<DataType>::valueOptions (useOptionsOnly, m_fieldValue);
98
172
}
99
173
100
174
// --------------------------------------------------------------------------------------------------
101
- // /
175
+ // / Extracts a QVariant representation of the data in the field to be used in the UI.
176
+ // / Note that for fields with a none empty valueOptions list the returned QVariant contains the
177
+ // / indexes to the selected options rather than the actual values, if they can be found.
178
+ // / If we cant find them, the method asserts, forcing the valueOptions to always contain the field values
102
179
// --------------------------------------------------------------------------------------------------
103
180
template <typename DataType >
104
181
QVariant caf::PdmField<DataType>::uiValue() const
105
182
{
106
183
if (m_optionEntryCache.size ())
107
184
{
108
185
QVariant convertedFieldValue = PdmFieldTypeSpecialization<DataType>::convert (m_fieldValue);
109
- unsigned int index ;
110
- bool foundFieldValue = PdmOptionItemInfo::findValue (m_optionEntryCache, convertedFieldValue, &index );
111
- assert (foundFieldValue);
112
- return QVariant (index );
113
- }
186
+ std::vector<unsigned int > indexes;
187
+ PdmOptionItemInfo::findValues (m_optionEntryCache, convertedFieldValue, indexes);
188
+ if (convertedFieldValue.type () == QVariant::List)
189
+ {
190
+ if (indexes.size () == convertedFieldValue.toList ().size ())
191
+ {
192
+ QList<QVariant> returnList;
193
+ for (size_t i = 0 ; i < indexes.size (); ++i)
194
+ {
195
+ returnList.push_back (QVariant (indexes[i]));
196
+ }
197
+ return QVariant (returnList);
198
+ }
199
+ assert (false ); // Did not find all the field values among the options available.
200
+ }
201
+ else
202
+ {
203
+ if (indexes.size () == 1 ) return QVariant (indexes.front ());
204
+ }
114
205
115
- return PdmFieldTypeSpecialization<DataType>::convert (m_fieldValue);
206
+ assert (false );
207
+ return convertedFieldValue;
208
+ }
209
+ else
210
+ {
211
+ return PdmFieldTypeSpecialization<DataType>::convert (m_fieldValue);
212
+ }
213
+
116
214
}
117
215
118
216
// ==================================================================================================
0 commit comments