FreeCAD

Форк
0
/
CompassWidget.cpp 
231 строка · 8.6 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2022 WandererFan <wandererfan@gmail.com>                *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
// The CompassWidget has several children widgets - a CompassDialWidget, a fine
24
// adjustment QDoubleSpinBox and QPushButtons that increment the spin box by a
25
// (usually) larger step than the arrows on the spinbox
26

27
#include "PreCompiled.h"
28
#ifndef _PreComp_
29
#include <QApplication>
30
#include <QLabel>
31
#include <QObject>
32
#include <QPushButton>
33
#include <QtGui>
34
#include <QtWidgets/QDoubleSpinBox>
35
#include <QtWidgets/QVBoxLayout>
36
#endif
37

38
#include <Mod/TechDraw/TechDrawGlobal.h>
39

40
#include <Base/Console.h>
41
#include <Base/Tools.h>
42

43
#include "CompassDialWidget.h"
44
#include "CompassWidget.h"
45

46
using namespace TechDrawGui;
47

48
CompassWidget::CompassWidget(QWidget* parent)
49
    : QWidget(parent), m_minimumWidth(200), m_minimumHeight(200), m_defaultMargin(10), m_angle(0.0),
50
      m_advanceIncrement(10.0)
51
{
52
    setObjectName(QString::fromUtf8("Compass"));
53
    m_rect = QRect(0, 0, m_minimumWidth, m_minimumHeight);
54
    buildWidget();
55
    compassDial->setSize(m_minimumHeight - 2 * m_defaultMargin);
56

57
    dsbAngle->installEventFilter(this);
58

59
    connect(pbCWAdvance, &QPushButton::pressed, this, &CompassWidget::slotCWAdvance);
60
    connect(pbCCWAdvance, &QPushButton::pressed, this, &CompassWidget::slotCCWAdvance);
61
}
62

63
//trap Enter press in dsbAngle so as not to invoke task accept processing
64
bool CompassWidget::eventFilter(QObject* target, QEvent* event)
65
{
66
    if (target == dsbAngle) {
67
        if (event->type() == QEvent::KeyPress) {
68
            QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
69
            if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
70
                dsbAngle->interpretText();
71
                slotSpinBoxEnter(dsbAngle->value());
72
                return true;
73
            }
74
        }
75
        else if (event->type() == QEvent::FocusOut) {
76
            dsbAngle->interpretText();
77
            slotSpinBoxEnter(dsbAngle->value());
78
            return true;
79
        }
80
    }
81
    return QWidget::eventFilter(target, event);
82
}
83

84
void CompassWidget::buildWidget()
85
{
86
    resize(m_minimumWidth, m_minimumHeight);
87
    QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
88
    sizePolicy.setHorizontalStretch(0);
89
    sizePolicy.setVerticalStretch(0);
90
    sizePolicy.setHeightForWidth(sizePolicy.hasHeightForWidth());
91
    setSizePolicy(sizePolicy);
92
    setMinimumSize(QSize(m_minimumWidth, m_minimumHeight));
93
    compassLayout = new QVBoxLayout(this);
94
    compassLayout->setObjectName(QString::fromUtf8("CompassLayout"));
95

96
    compassDialLayout = new QHBoxLayout();
97
    compassDialLayout->setObjectName(QString::fromUtf8("compassDialLayout"));
98

99
    pbCWAdvance = new QPushButton(this);
100
    pbCWAdvance->setObjectName(QString::fromUtf8("pbCWAdvance"));
101
    QIcon icon1;
102
    icon1.addFile(QString::fromUtf8(":/icons/arrow-cw.svg"), QSize(), QIcon::Normal, QIcon::On);
103
    pbCWAdvance->setIcon(icon1);
104
    compassDialLayout->addWidget(pbCWAdvance);
105

106
    compassDial = new CompassDialWidget(this);
107
    compassDial->setObjectName(QString::fromUtf8("CompassDial"));
108
    compassDialLayout->addWidget(compassDial);
109

110
    pbCCWAdvance = new QPushButton(this);
111
    pbCCWAdvance->setObjectName(QString::fromUtf8("pbCCWAdvance"));
112
    QIcon icon2;
113
    icon2.addFile(QString::fromUtf8(":/icons/arrow-ccw.svg"), QSize(), QIcon::Normal, QIcon::On);
114
    pbCCWAdvance->setIcon(icon2);
115
    compassDialLayout->addWidget(pbCCWAdvance);
116

117
    compassDialLayout->setStretch(1, 2);
118
    compassLayout->addLayout(compassDialLayout);
119

120
    compassControlLayout = new QHBoxLayout();
121
    compassControlLayout->setObjectName(QString::fromUtf8("compassControlLayout"));
122
    compassControlLabel = new QLabel(this);
123
    compassControlLabel->setObjectName(QString::fromUtf8("compassControlLabel"));
124
    QSizePolicy sizePolicy2(QSizePolicy::Minimum, QSizePolicy::Minimum);
125
    sizePolicy2.setHorizontalStretch(0);
126
    sizePolicy2.setVerticalStretch(0);
127
    sizePolicy2.setHeightForWidth(compassControlLabel->sizePolicy().hasHeightForWidth());
128
    compassControlLabel->setSizePolicy(sizePolicy2);
129

130
    compassControlLayout->addWidget(compassControlLabel);
131

132
    dsbAngle = new QDoubleSpinBox(this);
133
    dsbAngle->setObjectName(QString::fromUtf8("dsbAngle"));
134
    sizePolicy2.setHeightForWidth(dsbAngle->sizePolicy().hasHeightForWidth());
135
    dsbAngle->setSizePolicy(sizePolicy2);
136
    dsbAngle->setMinimumSize(QSize(75, 26));
137
    dsbAngle->setMouseTracking(true);
138
    dsbAngle->setFocusPolicy(Qt::ClickFocus);
139
    dsbAngle->setAlignment(Qt::AlignRight | Qt::AlignTrailing | Qt::AlignVCenter);
140
    dsbAngle->setKeyboardTracking(false);
141
    dsbAngle->setSuffix(QString::fromUtf8("\302\260"));
142
    dsbAngle->setMaximum(360.000000000000000);
143
    dsbAngle->setMinimum(-360.000000000000000);
144

145
    compassControlLayout->addWidget(dsbAngle);
146

147
    compassControlLayout->setStretch(0, 3);
148
    compassControlLayout->setStretch(1, 2);
149

150
    compassLayout->addLayout(compassControlLayout);
151

152
    retranslateUi();
153
}
154

155
void CompassWidget::retranslateUi()
156
{
157
    compassControlLabel->setText(
158
        QApplication::translate("CompassWidget", "View Direction as Angle", nullptr));
159
#ifndef QT_NO_TOOLTIP
160
    dsbAngle->setToolTip(QApplication::translate(
161
        "CompassWidget", "The view direction angle relative to +X in the BaseView.", nullptr));
162
    pbCWAdvance->setToolTip(QApplication::translate(
163
        "CompassWidget", "Advance the view direction in clockwise direction.", nullptr));
164
    pbCCWAdvance->setToolTip(QApplication::translate(
165
        "CompassWidget", "Advance the view direction in anti-clockwise direction.", nullptr));
166
#endif// QT_NO_TOOLTIP
167
}
168

169
QSize CompassWidget::sizeHint() const { return m_rect.size(); }
170

171
QSize CompassWidget::minimumSizeHint() const
172
{
173
    return QRect(0, 0, m_minimumWidth, m_minimumHeight).size();
174
}
175

176
void CompassWidget::paintEvent(QPaintEvent* event)
177
{
178
    Q_UNUSED(event);
179
    QWidget::paintEvent(event);
180
}
181

182
// set the compass dial and spinbox to a new angle
183
void CompassWidget::setDialAngle(double newAngle)
184
{
185
    //    Base::Console().Message("CW::setDialAngle(%.3f)\n", newAngle);
186
    m_angle = newAngle;
187
    if (compassDial) {
188
        compassDial->setAngle(m_angle);
189
    }
190
    if (dsbAngle) {
191
        dsbAngle->setValue(m_angle);
192
    }
193
}
194

195
//slot for updates from spinbox on Enter/Return press.
196
void CompassWidget::slotSpinBoxEnter(double newAngle)
197
{
198
    //    Base::Console().Message("CW::slotSpinBoxEnter(%.3f)\n", newAngle);
199
    if (dsbAngle) {
200
        m_angle = newAngle;
201
        Q_EMIT angleChanged(m_angle);
202
        if (compassDial) {
203
            compassDial->setAngle(m_angle);
204
        }
205
    }
206
}
207

208
void CompassWidget::slotCWAdvance()
209
{
210
    double angle = m_angle - m_advanceIncrement;
211
    if (angle < -360.0) {
212
        angle = angle + 360.0;
213
    }
214
    setDialAngle(angle);
215
    Q_EMIT angleChanged(angle);
216
}
217

218
void CompassWidget::slotCCWAdvance()
219
{
220
    double angle = m_angle + m_advanceIncrement;
221
    if (angle > dsbAngle->maximum()) {
222
        angle = angle - dsbAngle->maximum();
223
    }
224
    if (angle < dsbAngle->minimum()) {
225
        angle = angle + dsbAngle->minimum();
226
    }
227
    setDialAngle(angle);
228
    Q_EMIT angleChanged(angle);
229
}
230

231
void CompassWidget::setAdvanceIncrement(double newIncrement) { m_advanceIncrement = newIncrement; }
232

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.