File size: 8,730 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*

  Author(s):  Praneeth Sadda, Anton Deguet
  Created on: 2011-11-11

  (C) Copyright 2011 Johns Hopkins University (JHU), All Rights
  Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---

*/

#include <cisstMultiTask/mtsQtWidgetGenericObjectProxy.h>
#include <cisstMultiTask/mtsGenericObjectProxy.h>
#include <cisstMultiTask/mtsStateIndex.h>

#include <QHBoxLayout>
#include <QLabel>
#include <QSpinBox>
#include <QComboBox>
#include <QDoubleSpinBox>
#include <QLineEdit>
#include <QTableWidget>
#include <QHeaderView>

// -- mtsInt
mtsQtWidgetIntRead::mtsQtWidgetIntRead(void):
    mtsQtWidgetGenericObjectRead()
{
    Label = new QLabel();
    Label->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
    Layout->addWidget(Label);
    Layout->addStretch();
}

bool mtsQtWidgetIntRead::SetValue(const mtsGenericObject & value)
{
    const mtsInt * intData = dynamic_cast<const mtsInt *>(&value);
    if (intData) {
        QString s;
        s.setNum(intData->GetData());
        Label->setText(s);
        return true;
    }
    return false;
}

mtsQtWidgetIntWrite::mtsQtWidgetIntWrite(void):
    mtsQtWidgetGenericObjectWrite()
{
    SpinBox = new QSpinBox();
    Layout->addWidget(SpinBox);
    Layout->addStretch();
}

bool mtsQtWidgetIntWrite::SetValue(const mtsGenericObject & value)
{
    const mtsInt * intData = dynamic_cast<const mtsInt *>(&value);
    if (intData) {
        SpinBox->setValue(intData->GetData());
        return true;
    }
    return false;
}

bool mtsQtWidgetIntWrite::GetValue(mtsGenericObject & placeHolder) const
{
    mtsInt * intData = dynamic_cast<mtsInt *>(&placeHolder);
    if (intData) {
        intData->Data = SpinBox->value();
        return true;
    }
    return false;
}


// -- mtsBool
mtsQtWidgetBoolRead::mtsQtWidgetBoolRead(void):
    mtsQtWidgetGenericObjectRead()
{
    Label = new QLabel();
    Label->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
    Layout->addWidget(Label);
    Layout->addStretch();
}

bool mtsQtWidgetBoolRead::SetValue(const mtsGenericObject & value)
{
    const mtsBool * boolData = dynamic_cast<const mtsBool *>(&value);
    if (boolData) {
        Label->setText(boolData->GetData() ? "True" : "False");
        return true;
    }
    return false;
}

mtsQtWidgetBoolWrite::mtsQtWidgetBoolWrite(void):
    mtsQtWidgetGenericObjectWrite()
{
    ComboBox = new QComboBox();
    ComboBox->addItem("True");
    ComboBox->addItem("False");
    Layout->addWidget(ComboBox);
    Layout->addStretch();
}

bool mtsQtWidgetBoolWrite::SetValue(const mtsGenericObject & value)
{
    const mtsBool * boolData = dynamic_cast<const mtsBool *>(&value);
    if (boolData) {
        ComboBox->setCurrentIndex(boolData->GetData() ? 0 : 1);
        return true;
    }
    return false;
}

bool mtsQtWidgetBoolWrite::GetValue(mtsGenericObject & placeHolder) const
{
    mtsBool * boolData = dynamic_cast<mtsBool *>(&placeHolder);
    if (boolData) {
        boolData->Data = (ComboBox->currentIndex() == 0);
        return true;
    }
    return false;
}


// -- mtsDouble
mtsQtWidgetDoubleRead::mtsQtWidgetDoubleRead(void):
    mtsQtWidgetGenericObjectRead()
{
    Label = new QLabel();
    Label->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
    Layout->addWidget(Label);
    Layout->addStretch();
}

bool mtsQtWidgetDoubleRead::SetValue(const mtsGenericObject & value)
{
    const mtsDouble * doubleData = dynamic_cast<const mtsDouble *>(&value);
    if (doubleData) {
        QString s;
        s.setNum(doubleData->GetData());
        Label->setText(s);
        return true;
    }
    return false;
}

mtsQtWidgetDoubleWrite::mtsQtWidgetDoubleWrite(void):
    mtsQtWidgetGenericObjectWrite()
{
    DoubleSpinBox = new QDoubleSpinBox();
    DoubleSpinBox->setDecimals(5);
    Layout->addWidget(DoubleSpinBox);
    Layout->addStretch();
}

bool mtsQtWidgetDoubleWrite::SetValue(const mtsGenericObject & value)
{
    const mtsDouble * doubleData = dynamic_cast<const mtsDouble *>(&value);
    if (doubleData) {
        DoubleSpinBox->setValue(doubleData->GetData());
        return true;
    }
    return false;
}

bool mtsQtWidgetDoubleWrite::GetValue(mtsGenericObject & placeHolder) const
{
    mtsDouble * doubleData = dynamic_cast<mtsDouble *>(&placeHolder);
    if (doubleData) {
        doubleData->Data = DoubleSpinBox->value();
        return true;
    }
    return false;
}


// -- mtsStdString
mtsQtWidgetStdStringRead::mtsQtWidgetStdStringRead(void):
    mtsQtWidgetGenericObjectRead()
{
    Label = new QLabel();
    Label->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
    Layout->addWidget(Label);
    Layout->addStretch();
}

bool mtsQtWidgetStdStringRead::SetValue(const mtsGenericObject & value)
{
    const mtsStdString * stdStringData = dynamic_cast<const mtsStdString *>(&value);
    if (stdStringData) {
        Label->setText(stdStringData->GetData().c_str());
        return true;
    }
    return false;
}

mtsQtWidgetStdStringWrite::mtsQtWidgetStdStringWrite(void):
    mtsQtWidgetGenericObjectWrite()
{
    LineEdit = new QLineEdit();
    Layout->addWidget(LineEdit);
    Layout->addStretch();
}

bool mtsQtWidgetStdStringWrite::SetValue(const mtsGenericObject & value)
{
    const mtsStdString * stdStringData = dynamic_cast<const mtsStdString *>(&value);
    if (stdStringData) {
        LineEdit->setText(stdStringData->GetData().c_str());
        return true;
    }
    return false;
}

bool mtsQtWidgetStdStringWrite::GetValue(mtsGenericObject & placeHolder) const
{
    mtsStdString * stdStringData = dynamic_cast<mtsStdString *>(&placeHolder);
    if (stdStringData) {
        stdStringData->Data = std::string(LineEdit->text().toLatin1());
        return true;
    }
    return false;
}


// -- mtsStateIndex

mtsQtWidgetStateIndexRead::mtsQtWidgetStateIndexRead(void):
    mtsQtWidgetGenericObjectRead()
{
    Label = new QLabel();
    Label->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
    Layout->addWidget(Label);
    Layout->addStretch();
}

bool mtsQtWidgetStateIndexRead::SetValue(const mtsGenericObject & value)
{
    const mtsStateIndex * stateIndexData = dynamic_cast<const mtsStateIndex *>(&value);
    if (stateIndexData) {
        Label->setText(stateIndexData->ToString().c_str());
        return true;
    }
    return false;
}

mtsQtWidgetStateIndexWrite::mtsQtWidgetStateIndexWrite(void):
    mtsQtWidgetGenericObjectWrite()
{
    TableWidget = new QTableWidget();

    TableWidget->setRowCount(1);
    TableWidget->setColumnCount(3);

    QStringList members;
    members << "Index" << "Ticks" << "Length";

    TableWidget->setHorizontalHeaderLabels(members);

    IndexSpinBox = new QSpinBox();
    TicksSpinBox = new QDoubleSpinBox();
    LengthSpinBox = new QDoubleSpinBox();

    TableWidget->setCellWidget(0, 0, IndexSpinBox);
    TableWidget->setCellWidget(0, 1, TicksSpinBox);
    TableWidget->setCellWidget(0, 2, LengthSpinBox);
    TableWidget->verticalHeader()->hide();

    int verticalHeight = TableWidget->horizontalHeader()->sizeHint().height() + TableWidget->verticalHeader()->sizeHint().height();

    TableWidget->setFixedHeight(verticalHeight);

    Layout->addWidget(TableWidget);
    Layout->addStretch();
}

bool mtsQtWidgetStateIndexWrite::SetValue(const mtsGenericObject & value)
{
    const mtsStateIndex * stateIndexData = dynamic_cast<const mtsStateIndex *>(&value);
    if (stateIndexData) {
        IndexSpinBox->setValue(stateIndexData->Index());
        TicksSpinBox->setValue(stateIndexData->Ticks());
        LengthSpinBox->setValue(stateIndexData->Length());
        return true;
    }
    return false;
}

bool mtsQtWidgetStateIndexWrite::GetValue(mtsGenericObject & placeHolder) const
{
    mtsStateIndex * stateIndexData = dynamic_cast<mtsStateIndex *>(&placeHolder);
    if (stateIndexData) {
        mtsStateIndex newStateIndexData(
                                        0.0,
                                        IndexSpinBox->value(),
                                        TicksSpinBox->value(),
                                        LengthSpinBox->value()
                                        );
        stateIndexData->ReconstructFrom(newStateIndexData);
        return true;
    }
    return false;
}