File size: 5,035 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
/* -*- 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
  Created on: 2010-05-05

  (C) Copyright 2010-2017 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 <cisstVector/vctPlot2DOpenGLQtWidget.h>

#include <QMouseEvent>
#include <QMenu>
#include <QPainter>
#include <QColor>

vctPlot2DOpenGLQtWidget::vctPlot2DOpenGLQtWidget(QWidget * parent):
    vctQtOpenGLBaseWidget(parent),
    vctPlot2DOpenGL(),
    DisplayYRangeScale(0)
{
    this->setFocusPolicy(Qt::StrongFocus);
    this->setToolTip(QString("'space' to pause/restart, 'r' to reset\nright click for menu"));
    // Context menu
    this->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
            this, SLOT(ShowContextMenu(const QPoint &)));
}

void vctPlot2DOpenGLQtWidget::initializeGL(void)
{
    vctPlot2DOpenGL::RenderInitialize();
}

void vctPlot2DOpenGLQtWidget::resizeGL(int width, int height)
{
    vctPlot2DOpenGL::RenderResize(width, height);
}

void vctPlot2DOpenGLQtWidget::paintGL(void)
{
    vctPlot2DOpenGL::Render();
    if (DisplayYRangeScale) {
        const int font_size = 8;
        QPainter painter(this);
        const QColor txtColor = palette().color(QPalette::Text);
        painter.setPen(txtColor);
        painter.setFont(QFont("Helvetica", font_size));
        painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
        vct2 range = DisplayYRangeScale->GetViewingRangeY();
        painter.drawText(1, 1 + font_size,
                         QString::asprintf("%0.2f", range.at(1)));
        painter.drawText(1, this->Viewport.Y() - font_size,
                         QString::asprintf("%0.2f", range.at(0)));
        painter.end();
    }
}

void vctPlot2DOpenGLQtWidget::keyPressEvent(QKeyEvent * event)
{
    switch(event->key()) {
    case Qt::Key_Space:
        this->Freeze(!this->GetFreeze());
        break;
    case Qt::Key_R:
        this->SetContinuousExpandY(false);
        this->SetContinuousExpandY(true);
        break;
    default:
        break;
    }
}

void vctPlot2DOpenGLQtWidget::ShowContextMenu(const QPoint & pos)
{
    QPoint globalPos = this->mapToGlobal(pos);
    QMenu menu;

    QAction * freeze = new QAction("Freeze", this);
    freeze->setCheckable(true);
    freeze->setChecked(this->GetFreeze());
    menu.addAction(freeze);
    connect(freeze, SIGNAL(toggled(bool)), this, SLOT(FreezeSlot(bool)));

    menu.addSeparator();

    QAction * fitXnow = new QAction("Fit X now", this);
    QAction * fitXalways = new QAction("Fit X always", this);
    fitXalways->setCheckable(true);
    fitXalways->setChecked(this->GetContinuousFitX());
    menu.addAction(fitXnow);
    menu.addAction(fitXalways);
    connect(fitXnow, SIGNAL(triggered()), this, SLOT(FitXSlot()));
    connect(fitXalways, SIGNAL(toggled(bool)), this, SLOT(SetContinuousFitXSlot(bool)));

    menu.addSeparator();

    QAction * fitYnow = new QAction("Fit Y now", this);
    QAction * fitYalways = new QAction("Fit Y always", this);
    QAction * expandYalways = new QAction("Expand Y always", this);
    QAction * expandYreset = new QAction("Expand Y reset", this);
    fitYalways->setCheckable(true);
    fitYalways->setChecked(this->GetContinuousFitY());
    expandYalways->setCheckable(true);
    expandYalways->setChecked(this->GetContinuousExpandY());
    menu.addAction(fitYnow);
    menu.addAction(fitYalways);
    menu.addAction(expandYalways);
    menu.addAction(expandYreset);
    connect(fitYnow, SIGNAL(triggered()), this, SLOT(FitYSlot()));
    connect(fitYalways, SIGNAL(toggled(bool)), this, SLOT(SetContinuousFitYSlot(bool)));
    connect(expandYalways, SIGNAL(toggled(bool)), this, SLOT(SetContinuousExpandYSlot(bool)));
    connect(expandYreset, SIGNAL(triggered()), this, SLOT(SetContinuousExpandYResetSlot()));

    menu.exec(globalPos);
}

void vctPlot2DOpenGLQtWidget::FreezeSlot(bool checked)
{
    this->Freeze(checked);
}

void vctPlot2DOpenGLQtWidget::FitXSlot(void)
{
    this->SetContinuousFitX(false);
    this->AutoFitX();
}

void vctPlot2DOpenGLQtWidget::FitYSlot(void)
{
    this->SetContinuousFitY(false);
    this->AutoFitY();
}

void vctPlot2DOpenGLQtWidget::SetContinuousFitXSlot(bool checked)
{
    this->SetContinuousFitX(checked);
}

void vctPlot2DOpenGLQtWidget::SetContinuousFitYSlot(bool checked)
{
    this->SetContinuousFitY(checked);
}

void vctPlot2DOpenGLQtWidget::SetContinuousExpandYSlot(bool checked)
{
    this->SetContinuousExpandY(checked);
}

void vctPlot2DOpenGLQtWidget::SetContinuousExpandYResetSlot(void)
{
    this->SetContinuousExpandY(false);
    this->SetContinuousExpandY(true);
}