Debian Codegraph @

src/Gui/PreferencePages/DlgSettingsNavigation.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2002 Jürgen Riegel <[email protected]> *
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
24 #include <QApplication>
25 #include <QDoubleSpinBox>
26 #include <QGridLayout>
27 #include <memory>
28
29
30 #include <App/Application.h>
31 #include <Base/Parameter.h>
32 #include <Base/Tools.h>
33 #include <Gui/MainWindow.h>
34 #include <Gui/View3DSettings.h>
35 #include <Gui/Navigation/NavigationStyle.h>
36 #include <Gui/View3DInventor.h>
37 #include <Gui/View3DInventorViewer.h>
38
39 #include "DlgSettingsNavigation.h"
40 #include "ui_DlgSettingsNavigation.h"
41
42 #include "../ui_MouseButtons.h"
43
44 using namespace Gui::Dialog;
45
46 /* TRANSLATOR Gui::Dialog::DlgSettingsNavigation */
47
48 /**
49 * Constructs a DlgSettingsNavigation which is a child of 'parent', with the
50 * name 'name' and widget flags set to 'f'
51 */
52 DlgSettingsNavigation::DlgSettingsNavigation(QWidget* parent)
53 : PreferencePage(parent)
54 , ui(new Ui_DlgSettingsNavigation)
55 , q0(0)
56 , q1(0)
57 , q2(0)
58 , q3(1)
59 {
60 ui->setupUi(this);
61 ui->naviCubeBaseColor->setAllowTransparency(true);
62 ui->rotationCenterColor->setAllowTransparency(true);
63 retranslate();
64 #if !defined(_USE_3DCONNEXION_SDK) && !defined(SPNAV_FOUND)
65 ui->legacySpaceMouseDevices->setDisabled(true);
66 #elif !defined(USE_3DCONNEXION_NAVLIB)
67 ui->spaceMouseDevice->setHidden(true);
68 ui->legacySpaceMouseDevices->setHidden(true);
69 #endif
70 }
71
72 /**
73 * Destroys the object and frees any allocated resources
74 */
75 DlgSettingsNavigation::~DlgSettingsNavigation() = default;
76
77 void DlgSettingsNavigation::saveSettings()
78 {
79 // must be done as very first because we create a new instance of NavigatorStyle
80 // where we set some attributes afterwards
81 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
82 "User parameter:BaseApp/Preferences/View"
83 );
84 QVariant data
85 = ui->comboNavigationStyle->itemData(ui->comboNavigationStyle->currentIndex(), Qt::UserRole);
86 hGrp->SetASCII("NavigationStyle", (const char*)data.toByteArray());
87
88 int index = ui->comboOrbitStyle->currentIndex();
89 hGrp->SetInt("OrbitStyle", index);
90 index = ui->comboRotationMode->currentIndex();
91 hGrp->SetInt("RotationMode", index);
92
93 ui->checkBoxZoomAtCursor->onSave();
94 ui->checkBoxInvertZoom->onSave();
95 ui->checkBoxDisableTilt->onSave();
96 ui->rotationCenterSize->onSave();
97 ui->rotationCenterColor->onSave();
98 ui->spinBoxZoomStep->onSave();
99 ui->spinBoxAnimationDuration->onSave();
100 ui->checkBoxSpinningAnimations->onSave();
101 ui->checkBoxEnableLongPressClarifySelection->onSave();
102 ui->spinBoxLongPressTimeout->onSave();
103 ui->qspinNewDocScale->onSave();
104 ui->prefStepByTurn->onSave();
105 ui->naviCubeCorner->onSave();
106 ui->naviCubeToNearest->onSave();
107 ui->prefCubeSize->onSave();
108 ui->naviCubeBaseColor->onSave();
109 ui->naviCubeInactiveOpacity->onSave();
110 ui->legacySpaceMouseDevices->onSave();
111 if (property("LegacySpaceMouse").toBool() != ui->legacySpaceMouseDevices->isChecked()) {
112 requireRestart();
113 }
114
115 bool showNaviCube = ui->groupBoxNaviCube->isChecked();
116 hGrp->SetBool("ShowNaviCube", showNaviCube);
117
118 bool showRotationCenter = ui->groupBoxRotationCenter->isChecked();
119 hGrp->SetBool("ShowRotationCenter", showRotationCenter);
120
121 bool useNavigationAnimations = ui->groupBoxAnimations->isChecked();
122 hGrp->SetBool("UseNavigationAnimations", useNavigationAnimations);
123
124 QVariant camera = ui->comboNewDocView->itemData(ui->comboNewDocView->currentIndex(), Qt::UserRole);
125 hGrp->SetASCII("NewDocumentCameraOrientation", (const char*)camera.toByteArray());
126 if (camera == QByteArray("Custom")) {
127 ParameterGrp::handle hCustom = hGrp->GetGroup("Custom");
128 hCustom->SetFloat("Q0", q0);
129 hCustom->SetFloat("Q1", q1);
130 hCustom->SetFloat("Q2", q2);
131 hCustom->SetFloat("Q3", q3);
132 }
133 hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/NaviCube");
134 if (ui->naviCubeFontName->currentIndex()) {
135 hGrp->SetASCII("FontString", ui->naviCubeFontName->currentText().toLatin1());
136 }
137 else {
138 hGrp->RemoveASCII("FontString");
139 }
140 }
141
142 void DlgSettingsNavigation::loadSettings()
143 {
144 ui->checkBoxZoomAtCursor->onRestore();
145 ui->checkBoxInvertZoom->onRestore();
146 ui->checkBoxDisableTilt->onRestore();
147 ui->rotationCenterSize->onRestore();
148 ui->rotationCenterColor->onRestore();
149 ui->spinBoxZoomStep->onRestore();
150 ui->spinBoxAnimationDuration->onRestore();
151 ui->checkBoxSpinningAnimations->onRestore();
152 ui->checkBoxEnableLongPressClarifySelection->onRestore();
153 ui->spinBoxLongPressTimeout->onRestore();
154 ui->qspinNewDocScale->onRestore();
155 ui->prefStepByTurn->onRestore();
156 ui->naviCubeCorner->onRestore();
157 ui->naviCubeToNearest->onRestore();
158 ui->prefCubeSize->onRestore();
159 ui->naviCubeBaseColor->onRestore();
160 ui->naviCubeInactiveOpacity->onRestore();
161 ui->legacySpaceMouseDevices->onRestore();
162 setProperty("LegacySpaceMouse", ui->legacySpaceMouseDevices->isChecked());
163
164 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
165 "User parameter:BaseApp/Preferences/View"
166 );
167 std::string model
168 = hGrp->GetASCII("NavigationStyle", CADNavigationStyle::getClassTypeId().getName());
169 int index = ui->comboNavigationStyle->findData(QByteArray(model.c_str()));
170 if (index > -1) {
171 ui->comboNavigationStyle->setCurrentIndex(index);
172 }
173
174 index = hGrp->GetInt("OrbitStyle", int(NavigationStyle::RoundedArcball));
175 index = Base::clamp(index, 0, ui->comboOrbitStyle->count() - 1);
176 ui->comboOrbitStyle->setCurrentIndex(index);
177
178 index = hGrp->GetInt("RotationMode", 0);
179 ui->comboRotationMode->setCurrentIndex(index);
180
181 bool showNaviCube = hGrp->GetBool("ShowNaviCube", true);
182 ui->groupBoxNaviCube->setChecked(showNaviCube);
183
184 bool showRotationCenter = hGrp->GetBool("ShowRotationCenter", true);
185 ui->groupBoxRotationCenter->setChecked(showRotationCenter);
186
187 bool useNavigationAnimations = hGrp->GetBool("UseNavigationAnimations", true);
188 ui->groupBoxAnimations->setChecked(useNavigationAnimations);
189
190 addOrientations();
191
192 connect(ui->mouseButton, &QPushButton::clicked, this, &DlgSettingsNavigation::onMouseButtonClicked);
193
194 // fill up font styles
195
196 // we purposely allow all available fonts on the system
197 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
198 QStringList familyNames = QFontDatabase().families(QFontDatabase::Any);
199 #else
200 QStringList familyNames = QFontDatabase::families(QFontDatabase::Any);
201 #endif
202 ui->naviCubeFontName->addItems(familyNames);
203
204 hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/NaviCube");
205 int indexFamilyNames = familyNames.indexOf(QString::fromStdString(hGrp->GetASCII("FontString")));
206 ui->naviCubeFontName->setCurrentIndex(indexFamilyNames + 1);
207 }
208
209 void DlgSettingsNavigation::addOrientations()
210 {
211 ui->comboNewDocView->addItem(tr("Isometric"), QByteArray("Isometric"));
212 ui->comboNewDocView->addItem(tr("Dimetric"), QByteArray("Dimetric"));
213 ui->comboNewDocView->addItem(tr("Trimetric"), QByteArray("Trimetric"));
214 ui->comboNewDocView->addItem(tr("Top"), QByteArray("Top"));
215 ui->comboNewDocView->addItem(tr("Front"), QByteArray("Front"));
216 ui->comboNewDocView->addItem(tr("Left"), QByteArray("Left"));
217 ui->comboNewDocView->addItem(tr("Right"), QByteArray("Right"));
218 ui->comboNewDocView->addItem(tr("Rear"), QByteArray("Rear"));
219 ui->comboNewDocView->addItem(tr("Bottom"), QByteArray("Bottom"));
220 ui->comboNewDocView->addItem(tr("Custom"), QByteArray("Custom"));
221
222 ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath(
223 "User parameter:BaseApp/Preferences/View"
224 );
225 std::string camera = hGrp->GetASCII("NewDocumentCameraOrientation", "Trimetric");
226 int index = ui->comboNewDocView->findData(QByteArray(camera.c_str()));
227 if (index > -1) {
228 ui->comboNewDocView->setCurrentIndex(index);
229 }
230 if (camera == "Custom") {
231 ParameterGrp::handle hCustom = hGrp->GetGroup("Custom");
232 q0 = hCustom->GetFloat("Q0", q0);
233 q1 = hCustom->GetFloat("Q1", q1);
234 q2 = hCustom->GetFloat("Q2", q2);
235 q3 = hCustom->GetFloat("Q3", q3);
236 }
237
238 connect(
239 ui->comboNewDocView,
240 qOverload<int>(&QComboBox::currentIndexChanged),
241 this,
242 &DlgSettingsNavigation::onNewDocViewChanged
243 );
244 }
245
246 void DlgSettingsNavigation::translateOrientations()
247 {
248 ui->comboNewDocView->setItemText(0, tr("Isometric"));
249 ui->comboNewDocView->setItemText(1, tr("Dimetric"));
250 ui->comboNewDocView->setItemText(2, tr("Trimetric"));
251 ui->comboNewDocView->setItemText(3, tr("Top"));
252 ui->comboNewDocView->setItemText(4, tr("Front"));
253 ui->comboNewDocView->setItemText(5, tr("Left"));
254 ui->comboNewDocView->setItemText(6, tr("Right"));
255 ui->comboNewDocView->setItemText(7, tr("Rear"));
256 ui->comboNewDocView->setItemText(8, tr("Bottom"));
257 ui->comboNewDocView->setItemText(9, tr("Custom"));
258 }
259
260 void DlgSettingsNavigation::resetSettingsToDefaults()
261 {
262 ParameterGrp::handle hGrp;
263 hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/View");
264 // reset "NavigationStyle" parameter
265 hGrp->RemoveASCII("NavigationStyle");
266 // reset "OrbitStyle" parameter
267 hGrp->RemoveInt("OrbitStyle");
268 // reset "RotationMode" parameter
269 hGrp->RemoveInt("RotationMode");
270 // reset "ShowNaviCube" parameter
271 hGrp->RemoveBool("ShowNaviCube");
272 // reset "ShowRotationCenter" parameter
273 hGrp->RemoveBool("ShowRotationCenter");
274 // reset "UseNavigationAnimations" parameter
275 hGrp->RemoveBool("UseNavigationAnimations");
276 // reset "NewDocumentCameraOrientation" parameter
277 hGrp->RemoveASCII("NewDocumentCameraOrientation");
278
279 hGrp = hGrp->GetGroup("Custom");
280 // reset "Q0" parameter
281 hGrp->RemoveFloat("Q0");
282 // reset "Q1" parameter
283 hGrp->RemoveFloat("Q1");
284 // reset "Q2" parameter
285 hGrp->RemoveFloat("Q2");
286 // reset "Q3" parameter
287 hGrp->RemoveFloat("Q3");
288
289 // finally reset all the parameters associated to Gui::Pref* widgets
290 PreferencePage::resetSettingsToDefaults();
291 }
292
293 void DlgSettingsNavigation::onMouseButtonClicked()
294 {
295 QDialog dlg(this);
296 Ui_MouseButtons uimb;
297 uimb.setupUi(&dlg);
298
299 QVariant data
300 = ui->comboNavigationStyle->itemData(ui->comboNavigationStyle->currentIndex(), Qt::UserRole);
301 void* instance = Base::Type::createInstanceByName((const char*)data.toByteArray());
302 std::unique_ptr<UserNavigationStyle> ns(static_cast<UserNavigationStyle*>(instance));
303 uimb.groupBox->setTitle(
304 uimb.groupBox->title() + QStringLiteral(" ") + ui->comboNavigationStyle->currentText()
305 );
306 QString descr;
307 descr = qApp->translate(
308 (const char*)data.toByteArray(),
309 ns->mouseButtons(NavigationStyle::SELECTION)
310 );
311 descr.replace(QLatin1String("\n"), QLatin1String("<p>"));
312 uimb.selectionLabel->setText(QStringLiteral("<b>%1</b>").arg(descr));
313 descr = qApp->translate((const char*)data.toByteArray(), ns->mouseButtons(NavigationStyle::PANNING));
314 descr.replace(QLatin1String("\n"), QLatin1String("<p>"));
315 uimb.panningLabel->setText(QStringLiteral("<b>%1</b>").arg(descr));
316 descr = qApp->translate(
317 (const char*)data.toByteArray(),
318 ns->mouseButtons(NavigationStyle::DRAGGING)
319 );
320 descr.replace(QLatin1String("\n"), QLatin1String("<p>"));
321 uimb.rotationLabel->setText(QStringLiteral("<b>%1</b>").arg(descr));
322 descr = qApp->translate((const char*)data.toByteArray(), ns->mouseButtons(NavigationStyle::ZOOMING));
323 descr.replace(QLatin1String("\n"), QLatin1String("<p>"));
324 uimb.zoomingLabel->setText(QStringLiteral("<b>%1</b>").arg(descr));
325 dlg.exec();
326 }
327
328 /**
329 * Sets the strings of the subwidgets using the current language.
330 */
331 void DlgSettingsNavigation::changeEvent(QEvent* e)
332 {
333 if (e->type() == QEvent::LanguageChange) {
334 int navigation = ui->comboNavigationStyle->currentIndex();
335 int orbit = ui->comboOrbitStyle->currentIndex();
336 int corner = ui->naviCubeCorner->currentIndex();
337 ui->retranslateUi(this);
338 retranslate();
339 translateOrientations();
340 ui->comboNavigationStyle->setCurrentIndex(navigation);
341 ui->comboOrbitStyle->setCurrentIndex(orbit);
342 ui->naviCubeCorner->setCurrentIndex(corner);
343 }
344 else {
345 QWidget::changeEvent(e);
346 }
347 }
348
349 void DlgSettingsNavigation::retranslate()
350 {
351 ui->comboNavigationStyle->clear();
352
353 // add submenu at the end to select navigation style
354 std::map<Base::Type, std::string> styles = UserNavigationStyle::getUserFriendlyNames();
355 for (const auto& style : styles) {
356 QByteArray data(style.first.getName());
357 QString name = QApplication::translate(style.first.getName(), style.second.c_str());
358
359 ui->comboNavigationStyle->addItem(name, data);
360 }
361 }
362
363 void DlgSettingsNavigation::onNewDocViewChanged(int index)
364 {
365 QVariant camera = ui->comboNewDocView->itemData(index, Qt::UserRole);
366 if (camera == QByteArray("Custom")) {
367 CameraDialog dlg(this);
368 dlg.setValues(q0, q1, q2, q3);
369 if (dlg.exec()) {
370 dlg.getValues(q0, q1, q2, q3);
371 }
372 }
373 }
374
375 // ----------------------------------------------------------------------------
376
377 CameraDialog::CameraDialog(QWidget* parent)
378 : QDialog(parent)
379 {
380 this->setWindowTitle(tr("Camera Settings"));
381
382 QGridLayout* gridLayout;
383 gridLayout = new QGridLayout(this);
384
385 QGroupBox* groupBox;
386 groupBox = new QGroupBox(this);
387 groupBox->setTitle(tr("Orientation"));
388 gridLayout->addWidget(groupBox, 0, 0, 1, 1);
389
390 QDialogButtonBox* buttonBox;
391 buttonBox = new QDialogButtonBox(this);
392 buttonBox->setOrientation(Qt::Horizontal);
393 buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
394 gridLayout->addWidget(buttonBox, 3, 0, 1, 1);
395
396 QGridLayout* layout;
397 layout = new QGridLayout(groupBox);
398
399 // Q0
400 auto label0 = new QLabel(groupBox);
401 label0->setText(tr("Q0"));
402 layout->addWidget(label0, 0, 0, 1, 1);
403
404 sb0 = new QDoubleSpinBox(groupBox);
405 sb0->setRange(-1, 1);
406 sb0->setSingleStep(0.1);
407 layout->addWidget(sb0, 0, 1, 1, 1);
408
409 // Q1
410 auto label1 = new QLabel(groupBox);
411 label1->setText(tr("Q1"));
412 layout->addWidget(label1, 1, 0, 1, 1);
413
414 sb1 = new QDoubleSpinBox(groupBox);
415 sb1->setRange(-1, 1);
416 sb1->setSingleStep(0.1);
417 layout->addWidget(sb1, 1, 1, 1, 1);
418
419 // Q2
420 auto label2 = new QLabel(groupBox);
421 label2->setText(tr("Q2"));
422 layout->addWidget(label2, 2, 0, 1, 1);
423
424 sb2 = new QDoubleSpinBox(groupBox);
425 sb2->setRange(-1, 1);
426 sb2->setSingleStep(0.1);
427 layout->addWidget(sb2, 2, 1, 1, 1);
428
429 // Q3
430 auto label3 = new QLabel(groupBox);
431 label3->setText(tr("Q3"));
432 layout->addWidget(label3, 3, 0, 1, 1);
433
434 sb3 = new QDoubleSpinBox(groupBox);
435 sb3->setRange(-1, 1);
436 sb3->setSingleStep(0.1);
437 layout->addWidget(sb3, 3, 1, 1, 1);
438
439 auto currentViewButton = new QPushButton(this);
440 currentViewButton->setText(tr("Current View"));
441 currentViewButton->setObjectName(QStringLiteral("currentView"));
442 layout->addWidget(currentViewButton, 4, 1, 2, 1);
443
444 connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
445 connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
446 connect(currentViewButton, &QPushButton::clicked, this, &CameraDialog::onCurrentViewClicked);
447 }
448
449 CameraDialog::~CameraDialog() = default;
450
451 void CameraDialog::setValues(double q0, double q1, double q2, double q3)
452 {
453 sb0->setValue(q0);
454 sb1->setValue(q1);
455 sb2->setValue(q2);
456 sb3->setValue(q3);
457 }
458
459 void CameraDialog::getValues(double& q0, double& q1, double& q2, double& q3) const
460 {
461 q0 = sb0->value();
462 q1 = sb1->value();
463 q2 = sb2->value();
464 q3 = sb3->value();
465 }
466
467 void CameraDialog::onCurrentViewClicked()
468 {
469 auto mdi = qobject_cast<View3DInventor*>(getMainWindow()->activeWindow());
470 if (mdi) {
471 SbRotation rot = mdi->getViewer()->getCameraOrientation();
472 const float* q = rot.getValue();
473 sb0->setValue(q[0]);
474 sb1->setValue(q[1]);
475 sb2->setValue(q[2]);
476 sb3->setValue(q[3]);
477 }
478 }
479
480 #include "moc_DlgSettingsNavigation.cpp"