File size: 6,484 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
/* -*- 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):  Anton Deguet, Dorothy Hu
  Created on: 2017-01-20

  (C) Copyright 2017-2021 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 ---
*/


// system include
#include <iostream>

// Qt includes
#include <QMessageBox>
#include <QCloseEvent>
#include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QSpacerItem>
#include <QComboBox>
#include <QCheckBox>
#include <QSignalMapper>

#include <cisstVector/vctForceTorque2DQtWidget.h>
#include <cisstVector/vctPlot2DOpenGLQtWidget.h>

vctForceTorque2DQtWidget::vctForceTorque2DQtWidget(void):
    mScaleIndex(0)
{
    setupUi();
}

void vctForceTorque2DQtWidget::closeEvent(QCloseEvent * event)
{
    int answer = QMessageBox::warning(this, tr("vctForceTorque2DQtWidget"),
                                      tr("Do you really want to quit this application?"),
                                      QMessageBox::No | QMessageBox::Yes);
    if (answer == QMessageBox::Yes) {
        event->accept();
        QCoreApplication::exit();
    } else {
        event->ignore();
    }
}

void vctForceTorque2DQtWidget::setupUi(void)
{

    QHBoxLayout * mainLayout = new QHBoxLayout;
    mainLayout->setContentsMargins(0, 0, 0, 0);

    // left side, selector and legend
    QVBoxLayout * leftLayout = new QVBoxLayout;
    leftLayout->setContentsMargins(2, 2, 2, 2);
    mainLayout->addLayout(leftLayout);

    // combo box to select the plot item
    QComboBox * QPlotSelectItem = new QComboBox;
    QPlotSelectItem->addItem("Forces");
    QPlotSelectItem->addItem("Torques");
    QPlotSelectItem->setCurrentIndex(mScaleIndex);
    leftLayout->addWidget(QPlotSelectItem);

    // constants
    const QColor textColor = palette().color(QPalette::Text);
    const QColor baseColor = palette().color(QPalette::Base);
    const vct3 _colors[5] = {vct3(1.0, 0.0, 0.0),
                             vct3(0.0, 1.0, 0.0),
                             vct3(0.0, 0.0, 1.0),
                             vct3(textColor.redF(), textColor.greenF(), textColor.blueF()),
                             vct3(0.5)};
    const std::string _signals[5] = {"Axis X", "Axis Y", "Axis Z", "Norm", "Zero"};
    const std::string _scales[2] = {"F", "T"};

    // mapping
    mSignalMapper = new QSignalMapper();

    // legends
    QLabel * label;
    QPalette palette;

    for (size_t signal = 0;
         signal < 5;
         ++signal) {
        QHBoxLayout * signalLayout = new QHBoxLayout();
        signalLayout->setContentsMargins(0, 0, 0, 0);
        leftLayout->addLayout(signalLayout);
        // label
        label = new QLabel(_signals[signal].c_str());
        label->setAutoFillBackground(true);
        palette.setColor(QPalette::WindowText, QColor(_colors[signal].X() * 255,
                                                      _colors[signal].Y() * 255,
                                                      _colors[signal].Z() * 255));
        label->setPalette(palette);
        signalLayout->addWidget(label);
        // checkbox
        mCheckBoxes[signal] = new QCheckBox("");
        mCheckBoxes[signal]->setChecked(true);
        signalLayout->addWidget(mCheckBoxes[signal]);

        mSignalMapper->setMapping(mCheckBoxes[signal], signal);
        connect(mCheckBoxes[signal], SIGNAL(released()), mSignalMapper, SLOT(map()));
    }

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
    connect(mSignalMapper, &QSignalMapper::mappedInt,
            this, &vctForceTorque2DQtWidget::SlotVisibleSignal);
#else
    connect(mSignalMapper, SIGNAL(mapped(int)),
            this, SLOT(SlotVisibleSignal(int)));
#endif

    leftLayout->addStretch();

    // plot area
    QFTPlot = new vctPlot2DOpenGLQtWidget();
    QFTPlot->SetBackgroundColor(vct3(baseColor.redF(), baseColor.greenF(), baseColor.blueF()));
    QFTPlot->resize(QFTPlot->sizeHint());
    QFTPlot->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
    mainLayout->addWidget(QFTPlot);

    // create scales and signals
    for (size_t scale = 0;
         scale < 2;
         ++scale) {
        mScales[scale] = QFTPlot->AddScale(_scales[scale]);
        for (size_t signal = 0;
             signal < 5;
             ++signal) {
            mSignals[scale][signal] = mScales[scale]->AddSignal(_signals[signal]);
            mSignals[scale][signal]->SetColor(_colors[signal]);
            mVisibleSignals[scale][signal] = true;
        }
    }

    this->setLayout(mainLayout);

    setWindowTitle("");
    resize(sizeHint());

    // setup Qt Connection
    connect(QPlotSelectItem, SIGNAL(currentIndexChanged(int)), this, SLOT(SlotScaleIndex(int)));

    SlotScaleIndex(mScaleIndex);
}

void vctForceTorque2DQtWidget::SetValue(const double & time, const vct3 & force, const vct3 & torque)
{
    // make sure we should update the display
    if (this->isHidden()) {
        return;
    }

    // plot
    vct3 toPlot;
    if (mScaleIndex == 0) {
        toPlot = force;
    } else {
        toPlot = torque;
    }

    for (size_t i = 0; i < 3; ++i){
        mSignals[mScaleIndex][i]->AppendPoint(vctDouble2(time, toPlot[i]));
    }
    mSignals[mScaleIndex][3]->AppendPoint(vctDouble2(time, toPlot.Norm()));
    mSignals[mScaleIndex][4]->AppendPoint(vctDouble2(time, 0.0));

    QFTPlot->SetDisplayYRangeScale(mScales[mScaleIndex]);

    QFTPlot->update();
}


void vctForceTorque2DQtWidget::SlotScaleIndex(int newAxis)
{
    mScaleIndex = newAxis;

    for (size_t i = 0; i < 5; ++i) {
        // hide other scale
        mSignals[(mScaleIndex + 1) % 2][i]->SetVisible(false);
        // show current scales and user preferences
        mSignals[mScaleIndex][i]->SetVisible(mVisibleSignals[mScaleIndex][i]);
        mCheckBoxes[i]->setChecked(mVisibleSignals[mScaleIndex][i]);
    }
    mScales[mScaleIndex]->AutoFitXY();
    QFTPlot->SetContinuousExpandYResetSlot();
}


void vctForceTorque2DQtWidget::SlotVisibleSignal(int index)
{
    const bool checked = mCheckBoxes[index]->isChecked();
    mSignals[mScaleIndex][index]->SetVisible(checked);
    mVisibleSignals[mScaleIndex][index] = checked;
}