EditWidget.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // Include own headers
  2. #include "EditWidget.hpp"
  3. #include "../gui/ui_EditWidget.h"
  4. // Include modules
  5. #include "OSGWidget.hpp"
  6. #include "PickHandler.hpp"
  7. #include "TrackPointRenderer.hpp"
  8. #include "OpenScadRenderer.hpp"
  9. // Include dependencies
  10. #include <QFileDialog>
  11. EditWidget::EditWidget(QWidget* parent): QWidget(parent), ui(new Ui::EditWidget) {
  12. ui->setupUi(this);
  13. ui->actionPointDoubleIdentifier->setVisible(false);
  14. ui->insertionToolButton->setCheckable(true);
  15. ui->insertionToolButton->setChecked(true);
  16. QObject::connect(ui->insertionToolButton, &QToolButton::clicked, this, [=](){ this->selectTool(InsertionTool); });
  17. ui->selectionToolButton->setCheckable(true);
  18. QObject::connect(ui->selectionToolButton, &QToolButton::clicked, this, [=](){ this->selectTool(SelectionTool); });
  19. QObject::connect(ui->tabWidget, &QTabWidget::currentChanged, this, &EditWidget::tabChanged);
  20. // Modifiers
  21. QObject::connect(ui->normalModX, &QDoubleSpinBox::valueChanged, this, &EditWidget::updateNormalModifier);
  22. QObject::connect(ui->normalModY, &QDoubleSpinBox::valueChanged, this, &EditWidget::updateNormalModifier);
  23. QObject::connect(ui->normalModZ, &QDoubleSpinBox::valueChanged, this, &EditWidget::updateNormalModifier);
  24. QObject::connect(ui->modifierReset, &QPushButton::clicked, this, &EditWidget::resetNormalModifier);
  25. // OptiTrack settings
  26. QObject::connect(ui->optiTrackLength, &QDoubleSpinBox::valueChanged, this, &EditWidget::updateOptiTrackSettings);
  27. QObject::connect(ui->optiTrackRadius, &QDoubleSpinBox::valueChanged, this, &EditWidget::updateOptiTrackSettings);
  28. QObject::connect(ui->optiTrackLoadDefaults, &QPushButton::clicked, this, &EditWidget::resetOptiTrackSettings);
  29. // StramVRTrack settings
  30. QObject::connect(ui->steamVrTrackLength, &QDoubleSpinBox::valueChanged, this, &EditWidget::updateSteamVRTrackSettings);
  31. QObject::connect(ui->steamVrTrackLoadDefaults, &QPushButton::clicked, this, &EditWidget::resetSteamVRTrackSettings);
  32. // Action point settings
  33. QObject::connect(ui->actionPointIdentifier, &QLineEdit::textChanged, this, &EditWidget::updateActionPointSettings);
  34. QObject::connect(ui->actionPointLoadDefaults, &QPushButton::clicked, this, &EditWidget::resetActionPointSettings);
  35. // Delete button
  36. QObject::connect(ui->deleteTrackPoint, &QPushButton::clicked, this, &EditWidget::deleteCurrentTrackPoint);
  37. // Export button
  38. QObject::connect(ui->exportButton, &QPushButton::clicked, this, &EditWidget::exportProject);
  39. if (!OpenScadRenderer::openScadAvailable()) {
  40. ui->exportGroup->setEnabled(false);
  41. }
  42. }
  43. EditWidget::~EditWidget() {
  44. delete ui;
  45. }
  46. void EditWidget::updatePositions(osg::Vec3 point) {
  47. ui->anchorX->setText(QString::number(point.x()));
  48. ui->anchorY->setText(QString::number(point.y()));
  49. ui->anchorZ->setText(QString::number(point.z()));
  50. }
  51. void EditWidget::updateNormals(osg::Vec3 normal) {
  52. ui->normalX->setText(QString::number(normal.x()));
  53. ui->normalY->setText(QString::number(normal.y()));
  54. ui->normalZ->setText(QString::number(normal.z()));
  55. }
  56. void EditWidget::invalidatePositions() {
  57. ui->deleteTrackPoint->setEnabled(false);
  58. ui->anchorX->setText("-");
  59. ui->anchorY->setText("-");
  60. ui->anchorZ->setText("-");
  61. ui->normalX->setText("-");
  62. ui->normalY->setText("-");
  63. ui->normalZ->setText("-");
  64. }
  65. ActiveTrackingSystem EditWidget::getSelectedTrackingSystem() {
  66. switch(ui->tabWidget->currentIndex()) {
  67. case 0: {
  68. return OptiTrack;
  69. }
  70. case 1: {
  71. return EMFTrack;
  72. }
  73. case 2: {
  74. return SteamVRTrack;
  75. }
  76. default: {
  77. return ActionPoints;
  78. }
  79. }
  80. }
  81. void EditWidget::setSelection(int id) {
  82. selectedPoint = id;
  83. ui->deleteTrackPoint->setEnabled(true);
  84. switch(ui->tabWidget->currentIndex()) {
  85. case 0: {
  86. OptiTrackPoint* point = MainWindow::getInstance()->getStore()->getOptiTrackPoints()[id];
  87. updatePositions(point->getTranslation());
  88. updateNormals(point->getNormal());
  89. setNormalModifier(point->getNormalModifier());
  90. setOptiTrackSettings(point->getLength(), point->getRadius());
  91. break;
  92. }
  93. case 1: {
  94. break;
  95. }
  96. case 2: {
  97. SteamVRTrackPoint* point = MainWindow::getInstance()->getStore()->getSteamVRTrackPoints()[id];
  98. updatePositions(point->getTranslation());
  99. updateNormals(point->getNormal());
  100. setNormalModifier(point->getNormalModifier());
  101. setSteamVRTrackSettings(point->getLength());
  102. break;
  103. }
  104. default: {
  105. ActionPoint* point = MainWindow::getInstance()->getStore()->getActionPoints()[id];
  106. updatePositions(point->getTranslation());
  107. updateNormals(point->getNormal());
  108. setNormalModifier(point->getNormalModifier());
  109. setActionPointSettings(point->getIdentifier());
  110. break;
  111. }
  112. }
  113. }
  114. int EditWidget::getSelectedPoint() {
  115. return selectedPoint;
  116. }
  117. void EditWidget::updateTrackpointCount() {
  118. int count;
  119. count = MainWindow::getInstance()->getStore()->getCount(OptiTrack);
  120. {
  121. QString countString("TRACKPOINTS SET: ");
  122. countString.append(QString::number(count));
  123. ui->optiTrackCount->setText(countString);
  124. }
  125. // TODO: EMF Track
  126. count = MainWindow::getInstance()->getStore()->getCount(SteamVRTrack);
  127. {
  128. QString countString("TRACKPOINTS SET: ");
  129. countString.append(QString::number(count));
  130. ui->steamVrTrackCount->setText(countString);
  131. }
  132. count = MainWindow::getInstance()->getStore()->getCount(ActionPoints);
  133. {
  134. QString countString("ACTION POINTS SET: ");
  135. countString.append(QString::number(count));
  136. ui->actionPointCount->setText(countString);
  137. }
  138. }
  139. void EditWidget::showEvent(QShowEvent* event) {
  140. QWidget::showEvent(event);
  141. resetAllSettings();
  142. }
  143. void EditWidget::resetAllSettings() {
  144. resetNormalModifier();
  145. resetOptiTrackSettings();
  146. resetSteamVRTrackSettings();
  147. resetActionPointSettings();
  148. }
  149. void EditWidget::selectTool(Tool tool) {
  150. switch(tool) {
  151. case InsertionTool: {
  152. ui->insertionToolButton->setChecked(true);
  153. ui->selectionToolButton->setChecked(false);
  154. MainWindow::getInstance()->getOsgWidget()->getPicker()->setSelection(true);
  155. invalidatePositions();
  156. resetAllSettings();
  157. selectedPoint = -1;
  158. break;
  159. }
  160. case SelectionTool: {
  161. ui->insertionToolButton->setChecked(false);
  162. ui->selectionToolButton->setChecked(true);
  163. MainWindow::getInstance()->getOsgWidget()->getPicker()->setSelection(false);
  164. break;
  165. }
  166. }
  167. }
  168. void EditWidget::tabChanged(int index) {
  169. if (selectedPoint < 0) {
  170. MainWindow::getInstance()->getOsgWidget()->getPicker()->updateRenderer();
  171. }
  172. selectedPoint = -1;
  173. ActiveTrackingSystem activeTrackingSystem = getSelectedTrackingSystem();
  174. MainWindow::getInstance()->getOsgWidget()->getPointRenderer()->render(activeTrackingSystem);
  175. }
  176. void EditWidget::updateNormalModifier() {
  177. osg::Vec3 modifier = osg::Vec3(ui->normalModX->value(), ui->normalModY->value(), ui->normalModZ->value());
  178. if (selectedPoint < 0) {
  179. MainWindow::getInstance()->getStore()->updateNormalModifier(modifier);
  180. MainWindow::getInstance()->getOsgWidget()->getPicker()->updateRenderer();
  181. } else {
  182. ActiveTrackingSystem activeTrackingSystem = getSelectedTrackingSystem();
  183. MainWindow::getInstance()->getStore()->getTrackPointById(selectedPoint, activeTrackingSystem)->updateNormalModifier(modifier);
  184. MainWindow::getInstance()->getOsgWidget()->getPointRenderer()->render(activeTrackingSystem);
  185. }
  186. }
  187. void EditWidget::resetNormalModifier() {
  188. osg::Vec3 modifier = osg::Vec3(0.0f, 0.0f, 0.0f);
  189. ui->normalModX->setValue(0.0f);
  190. ui->normalModY->setValue(0.0f);
  191. ui->normalModZ->setValue(0.0f);
  192. if (selectedPoint < 0) {
  193. MainWindow::getInstance()->getStore()->updateNormalModifier(modifier);
  194. MainWindow::getInstance()->getOsgWidget()->getPicker()->updateRenderer();
  195. } else {
  196. ActiveTrackingSystem activeTrackingSystem = getSelectedTrackingSystem();
  197. MainWindow::getInstance()->getStore()->getTrackPointById(selectedPoint, activeTrackingSystem)->updateNormalModifier(modifier);
  198. MainWindow::getInstance()->getOsgWidget()->getPointRenderer()->render(activeTrackingSystem);
  199. }
  200. }
  201. void EditWidget::setNormalModifier(osg::Vec3 normalModifier) {
  202. ui->normalModX->setValue(normalModifier.x());
  203. ui->normalModY->setValue(normalModifier.y());
  204. ui->normalModZ->setValue(normalModifier.z());
  205. }
  206. void EditWidget::updateOptiTrackSettings() {
  207. OptiTrackSettings settings = {ui->optiTrackLength->value(), ui->optiTrackRadius->value()};
  208. if (selectedPoint < 0) {
  209. MainWindow::getInstance()->getStore()->updateOptiTrackSettings(settings);
  210. MainWindow::getInstance()->getOsgWidget()->getPicker()->updateRenderer();
  211. } else {
  212. MainWindow::getInstance()->getStore()->getOptiTrackPoints()[selectedPoint]->updateOptiTrackSettings(settings);
  213. MainWindow::getInstance()->getOsgWidget()->getPointRenderer()->render(OptiTrack);
  214. }
  215. }
  216. void EditWidget::resetOptiTrackSettings() {
  217. OptiTrackSettings settings = {OPTITRACK_DEFAULT_LENGTH, OPTITRACK_DEFAULT_RADIUS};
  218. ui->optiTrackLength->setValue(OPTITRACK_DEFAULT_LENGTH);
  219. ui->optiTrackRadius->setValue(OPTITRACK_DEFAULT_RADIUS);
  220. if (selectedPoint < 0) {
  221. MainWindow::getInstance()->getStore()->updateOptiTrackSettings(settings);
  222. MainWindow::getInstance()->getOsgWidget()->getPicker()->updateRenderer();
  223. } else {
  224. MainWindow::getInstance()->getStore()->getOptiTrackPoints()[selectedPoint]->updateOptiTrackSettings(settings);
  225. MainWindow::getInstance()->getOsgWidget()->getPointRenderer()->render(OptiTrack);
  226. }
  227. }
  228. void EditWidget::setOptiTrackSettings(double length, double radius) {
  229. ui->optiTrackLength->setValue(length);
  230. ui->optiTrackRadius->setValue(radius);
  231. }
  232. void EditWidget::updateSteamVRTrackSettings() {
  233. SteamVRTrackSettings settings = {ui->steamVrTrackLength->value()};
  234. if (selectedPoint < 0) {
  235. MainWindow::getInstance()->getStore()->updateSteamVRTrackSettings(settings);
  236. MainWindow::getInstance()->getOsgWidget()->getPicker()->updateRenderer();
  237. } else {
  238. MainWindow::getInstance()->getStore()->getSteamVRTrackPoints()[selectedPoint]->updateSteamVRTrackSettings(settings);
  239. MainWindow::getInstance()->getOsgWidget()->getPointRenderer()->render(SteamVRTrack);
  240. }
  241. }
  242. void EditWidget::resetSteamVRTrackSettings() {
  243. SteamVRTrackSettings settings = {STEAMVR_DEFAULT_LENGTH};
  244. ui->steamVrTrackLength->setValue(STEAMVR_DEFAULT_LENGTH);
  245. if (selectedPoint < 0) {
  246. MainWindow::getInstance()->getStore()->updateSteamVRTrackSettings(settings);
  247. MainWindow::getInstance()->getOsgWidget()->getPicker()->updateRenderer();
  248. } else {
  249. MainWindow::getInstance()->getStore()->getSteamVRTrackPoints()[selectedPoint]->updateSteamVRTrackSettings(settings);
  250. MainWindow::getInstance()->getOsgWidget()->getPointRenderer()->render(SteamVRTrack);
  251. }
  252. }
  253. void EditWidget::setSteamVRTrackSettings(double length) {
  254. ui->steamVrTrackLength->setValue(length);
  255. }
  256. void EditWidget::updateActionPointSettings(QString qinput) {
  257. std::string input = qinput.toUtf8().constData();
  258. if (MainWindow::getInstance()->getStore()->actionPointIdentifierInUse(input)) {
  259. ui->actionPointDoubleIdentifier->setVisible(true);
  260. } else {
  261. ui->actionPointDoubleIdentifier->setVisible(false);
  262. QString qtext = ui->actionPointIdentifier->text();
  263. ActionPointSettings settings = {qtext.toUtf8().constData()};
  264. if (selectedPoint < 0) {
  265. MainWindow::getInstance()->getStore()->updateActionPointSettings(settings);
  266. } else {
  267. MainWindow::getInstance()->getStore()->getActionPoints()[selectedPoint]->updateActionPointSettings(settings);
  268. }
  269. }
  270. }
  271. void EditWidget::resetActionPointSettings() {
  272. int iterator = 0;
  273. ActionPointSettings settings;
  274. std::string candidate;
  275. while (true) {
  276. candidate = "point";
  277. candidate += std::to_string(iterator);
  278. bool result = MainWindow::getInstance()->getStore()->actionPointIdentifierInUse(candidate);
  279. if (!result) {
  280. settings = {candidate};
  281. break;
  282. }
  283. }
  284. ui->actionPointIdentifier->setText(QString::fromStdString(candidate));
  285. if (selectedPoint < 0) {
  286. MainWindow::getInstance()->getStore()->updateActionPointSettings(settings);
  287. } else {
  288. MainWindow::getInstance()->getStore()->getActionPoints()[selectedPoint]->updateActionPointSettings(settings);
  289. }
  290. }
  291. void EditWidget::setActionPointSettings(std::string identifier) {
  292. if (MainWindow::getInstance()->getStore()->actionPointIdentifierInUse(identifier)) {
  293. ui->actionPointDoubleIdentifier->setVisible(true);
  294. } else {
  295. ui->actionPointDoubleIdentifier->setVisible(false);
  296. ui->actionPointIdentifier->setText(QString::fromStdString(identifier));
  297. }
  298. }
  299. void EditWidget::deleteCurrentTrackPoint() {
  300. ActiveTrackingSystem activeTrackingSystem = getSelectedTrackingSystem();
  301. MainWindow::getInstance()->getStore()->removeTrackPoint(selectedPoint, activeTrackingSystem);
  302. selectedPoint = -1;
  303. switch(activeTrackingSystem) {
  304. case OptiTrack: {
  305. resetOptiTrackSettings();
  306. break;
  307. }
  308. case EMFTrack: {
  309. break;
  310. }
  311. case SteamVRTrack: {
  312. resetSteamVRTrackSettings();
  313. break;
  314. }
  315. case ActionPoints: {
  316. break;
  317. }
  318. }
  319. resetNormalModifier();
  320. invalidatePositions();
  321. MainWindow::getInstance()->getOsgWidget()->getPointRenderer()->render(OptiTrack);
  322. }
  323. void EditWidget::exportProject() {
  324. Qt::CheckState optiTrackSelected = ui->optiTrackCkeckbox->checkState();
  325. Qt::CheckState emfTrackSelected = ui->emfTrackCheckbox->checkState();
  326. Qt::CheckState steamVrTrackSelected = ui->steamVrCheckbox->checkState();
  327. ExportSettings settings = {optiTrackSelected == Qt::Checked, emfTrackSelected == Qt::Checked, steamVrTrackSelected == Qt::Checked};
  328. QString fileName = QFileDialog::getSaveFileName(this, tr("Export your TrackpointApp Project"), "", tr("3MF Files (*.3mf)"));
  329. std::string exportFile = fileName.toUtf8().constData();
  330. MainWindow::getInstance()->getStore()->exportProject(exportFile, settings);
  331. }