Showing the changes to interface/src/ImportDialog.cpp (1 of 2 files). Show all
Unified · Split
| @@ -0,0 +1,192 @@ | ||
| 1 | +// | |
| 2 | +// ImportDialog.cpp | |
| 3 | +// hifi | |
| 4 | +// | |
| 5 | +// Created by Clement Brisset on 8/12/13. | |
| 6 | +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. | |
| 7 | +// | |
| 8 | +#include "ImportDialog.h" | |
| 9 | + | |
| 10 | +#include <QStandardPaths> | |
| 11 | +#include <QGridLayout> | |
| 12 | + | |
| 13 | +const QString WINDOW_NAME = QObject::tr("Import Voxels"); | |
| 14 | +const QString IMPORT_TO_CLIPBOARD_CHECKBOX_STRING = QObject::tr("Import into clipboard"); | |
| 15 | +const QString PREVIEW_CHECKBOX_STRING = QObject::tr("Load preview"); | |
| 16 | +const QString IMPORT_FILE_TYPES = QObject::tr("Sparse Voxel Octree Files, " | |
| 17 | + "Square PNG, " | |
| 18 | + "Schematic Files " | |
| 19 | + "(*.svo *.png *.schematic)"); | |
| 20 | + | |
| 21 | +const QString DESKTOP_LOCATION = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); | |
| 22 | + | |
| 23 | +const glm::vec3 UP = glm::vec3(0, 1, 0); | |
| 24 | +const float ANGULAR_RATE = 0.02f; | |
| 25 | + | |
| 26 | +class GLWidget : public QGLWidget { | |
| 27 | +public: | |
| 28 | + GLWidget(QWidget* parent = NULL, VoxelSystem* voxelSystem = NULL); | |
| 29 | + void setDraw(bool draw) {_draw = draw;} | |
| 30 | + void setTargetCenter(glm::vec3 targetCenter) {_targetCenter = targetCenter;} | |
| 31 | + | |
| 32 | +protected: | |
| 33 | + virtual void initializeGL(); | |
| 34 | + virtual void resizeGL(int w, int h); | |
| 35 | + virtual void paintGL(); | |
| 36 | + | |
| 37 | +private: | |
| 38 | + VoxelSystem* _voxelSystem; | |
| 39 | + | |
| 40 | + bool _draw; | |
| 41 | + | |
| 42 | + float _a; | |
| 43 | + float _h; | |
| 44 | + glm::vec3 _targetCenter; | |
| 45 | +}; | |
| 46 | + | |
| 47 | +GLWidget::GLWidget(QWidget *parent, VoxelSystem *voxelSystem) | |
| 48 | + : QGLWidget(parent), | |
| 49 | + _voxelSystem(voxelSystem), | |
| 50 | + _draw(false), | |
| 51 | + _a(0.0f), | |
| 52 | + _h(0.0f), | |
| 53 | + _targetCenter(0.5f, 0.5f, 0.5f) { | |
| 54 | +} | |
| 55 | + | |
| 56 | +void GLWidget::initializeGL() { | |
| 57 | + glEnable(GL_BLEND); | |
| 58 | + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
| 59 | + glShadeModel (GL_SMOOTH); | |
| 60 | + glEnable(GL_DEPTH_TEST); | |
| 61 | + | |
| 62 | + if(_voxelSystem) { | |
| 63 | + _voxelSystem->init(); | |
| 64 | + } | |
| 65 | + | |
| 66 | +} | |
| 67 | + | |
| 68 | +void GLWidget::resizeGL(int w, int h) { | |
| 69 | + glViewport(0, 0, w, h); | |
| 70 | + | |
| 71 | + glMatrixMode(GL_PROJECTION); | |
| 72 | + glLoadIdentity(); | |
| 73 | + gluPerspective(60, (float) w / h, 0.25f, 500); | |
| 74 | +} | |
| 75 | + | |
| 76 | +void GLWidget::paintGL() { | |
| 77 | + glEnable(GL_LINE_SMOOTH); | |
| 78 | + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
| 79 | + | |
| 80 | + glMatrixMode(GL_MODELVIEW); | |
| 81 | + glLoadIdentity(); | |
| 82 | + | |
| 83 | + _a += ANGULAR_RATE; | |
| 84 | + gluLookAt(_targetCenter.x + (glm::length(_targetCenter) + 0.5f) * cos((double) _a), | |
| 85 | + _targetCenter.y * 1.25f, | |
| 86 | + _targetCenter.z + (glm::length(_targetCenter) + 0.5f) * sin((double) _a), | |
| 87 | + _targetCenter.x, _targetCenter.y, _targetCenter.z, | |
| 88 | + UP.x, UP.y, UP.z); | |
| 89 | + | |
| 90 | + | |
| 91 | + if (_draw && _voxelSystem) { | |
| 92 | + glBegin(GL_LINES); | |
| 93 | + glColor3d(1, 1 ,1); | |
| 94 | + glVertex3d(0, 0, 0); | |
| 95 | + glVertex3d(1, 0, 0); | |
| 96 | + | |
| 97 | + glVertex3d(0, 0, 0); | |
| 98 | + glVertex3d(0, 1, 0); | |
| 99 | + | |
| 100 | + glVertex3d(0, 0, 0); | |
| 101 | + glVertex3d(0, 0, 1); | |
| 102 | + | |
| 103 | + | |
| 104 | + glColor3d(0.4f, 0.4f ,0.4f); | |
| 105 | + glVertex3d(2 * _targetCenter.x, 2 * _targetCenter.y,2 * _targetCenter.z); | |
| 106 | + glVertex3d(0 , 2 * _targetCenter.y, 2 * _targetCenter.z); | |
| 107 | + | |
| 108 | + glVertex3d(2 * _targetCenter.x, 2 * _targetCenter.y, 2 * _targetCenter.z); | |
| 109 | + glVertex3d(2 * _targetCenter.x, 0 , 2 * _targetCenter.z); | |
| 110 | + | |
| 111 | + glVertex3d(2 * _targetCenter.x, 2 * _targetCenter.y, 2 * _targetCenter.z); | |
| 112 | + glVertex3d(2 * _targetCenter.x, 2 * _targetCenter.y, 0 ); | |
| 113 | + glEnd(); | |
| 114 | + | |
| 115 | + _voxelSystem->render(false); | |
| 116 | + } | |
| 117 | +} | |
| 118 | + | |
| 119 | +ImportDialog::ImportDialog(QWidget *parent, VoxelSystem* voxelSystem) | |
| 120 | + : QFileDialog(parent, WINDOW_NAME, DESKTOP_LOCATION, IMPORT_FILE_TYPES), | |
| 121 | + _clipboardImportBox (IMPORT_TO_CLIPBOARD_CHECKBOX_STRING, this), | |
| 122 | + _previewBox (PREVIEW_CHECKBOX_STRING, this), | |
| 123 | + _previewBar (this), | |
| 124 | + _glPreview (new GLWidget(this, voxelSystem)) { | |
| 125 | + | |
| 126 | + setOption(QFileDialog::DontUseNativeDialog, true); | |
| 127 | + setFileMode(QFileDialog::ExistingFile); | |
| 128 | + setViewMode(QFileDialog::Detail); | |
| 129 | + | |
| 130 | + QGridLayout* gridLayout = (QGridLayout*) layout(); | |
| 131 | + gridLayout->addWidget(&_clipboardImportBox, 2, 3); | |
| 132 | + gridLayout->addWidget(&_previewBox , 3, 3); | |
| 133 | + gridLayout->addWidget(&_previewBar , 0, 3); | |
| 134 | + gridLayout->addWidget(_glPreview , 1, 3); | |
| 135 | + gridLayout->setColumnStretch(3, 1); | |
| 136 | + | |
| 137 | + _previewBar.setVisible(false); | |
| 138 | + _previewBar.setRange(0, 100); | |
| 139 | + _previewBar.setValue(0); | |
| 140 | + | |
| 141 | + connect(&_previewBox, SIGNAL(toggled(bool)), SLOT(preview(bool))); | |
| 142 | + connect(this, SIGNAL(currentChanged(QString)), SLOT(saveCurrentFile(QString))); | |
| 143 | + connect(&_glTimer, SIGNAL(timeout()), SLOT(timer())); | |
| 144 | + | |
| 145 | + connect(voxelSystem->getVoxelTree(), SIGNAL(importSize(float,float,float)), SLOT(setGLCamera(float, float, float))); | |
| 146 | + connect(voxelSystem->getVoxelTree(), SIGNAL(importProgress(int)), &_previewBar, SLOT(setValue(int))); | |
| 147 | +} | |
| 148 | + | |
| 149 | +int ImportDialog::exec() { | |
| 150 | + _previewBox.setChecked(false); | |
| 151 | + _previewBar.setVisible(false); | |
| 152 | + | |
| 153 | + int ret = QFileDialog::exec(); | |
| 154 | + | |
| 155 | + | |
| 156 | + ((GLWidget*) _glPreview)->setDraw(false); | |
| 157 | + _glTimer.stop(); | |
| 158 | + _glPreview->updateGL(); | |
| 159 | + | |
| 160 | + return ret; | |
| 161 | +} | |
| 162 | + | |
| 163 | +void ImportDialog::setGLCamera(float x, float y, float z) { | |
| 164 | + ((GLWidget*) _glPreview)->setTargetCenter(glm::vec3(x, y, z) / 2.0f); | |
| 165 | +} | |
| 166 | + | |
| 167 | +void ImportDialog::reset() { | |
| 168 | + _previewBar.setValue(0); | |
| 169 | +} | |
| 170 | + | |
| 171 | +void ImportDialog::preview(bool wantPreview) { | |
| 172 | + _previewBar.setValue(0); | |
| 173 | + _previewBar.setVisible(wantPreview); | |
| 174 | + ((GLWidget*) _glPreview)->setDraw(wantPreview); | |
| 175 | + | |
| 176 | + if (wantPreview) { | |
| 177 | + emit previewActivated(_currentFile); | |
| 178 | + _glTimer.start(); | |
| 179 | + } else { | |
| 180 | + _glTimer.stop(); | |
| 181 | + _glPreview->updateGL(); | |
| 182 | + } | |
| 183 | +} | |
| 184 | + | |
| 185 | +void ImportDialog::saveCurrentFile(QString filename) { | |
| 186 | + _currentFile = filename; | |
| 187 | +} | |
| 188 | + | |
| 189 | +void ImportDialog::timer() { | |
| 190 | + _glPreview->updateGL(); | |
| 191 | + _glTimer.start(16); | |
| 192 | +} | |