Showing the changes to interface/src/ImportDialog.cpp (1 of 12 files). Show all
Unified · Split
| @@ -9,8 +9,10 @@ · 8 unmodified lines | ||
| 9 | 9 | |
| 10 | 10 | #include <QStandardPaths> |
| 11 | 11 | #include <QGridLayout> |
| 12 | +#include <QMouseEvent> | |
| 12 | 13 | |
| 13 | 14 | const QString WINDOW_NAME = QObject::tr("Import Voxels"); |
| 15 | +const QString IMPORT_BUTTON_NAME = QObject::tr("Import"); | |
| 14 | 16 | const QString IMPORT_TO_CLIPBOARD_CHECKBOX_STRING = QObject::tr("Import into clipboard"); |
| 15 | 17 | const QString PREVIEW_CHECKBOX_STRING = QObject::tr("Load preview"); |
| 16 | 18 | const QString IMPORT_FILE_TYPES = QObject::tr("Sparse Voxel Octree Files, " |
| @@ -20,8 +22,14 @@ const QString IMPORT_FILE_TYPES = QObject::tr("Sparse Voxel Oc · 3 unmodified lines | ||
| 20 | 22 | |
| 21 | 23 | const QString DESKTOP_LOCATION = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); |
| 22 | 24 | |
| 25 | + | |
| 23 | 26 | const glm::vec3 UP = glm::vec3(0, 1, 0); |
| 24 | 27 | const float ANGULAR_RATE = 0.02f; |
| 28 | +const float VERTICAL_ANGLE = M_PI_4 / 2.0f; | |
| 29 | +const float RETURN_RATE = 0.02f; | |
| 30 | +const float NEAR_CLIP = 0.5f; | |
| 31 | +const float FAR_CLIP = 10.0f; | |
| 32 | +const float FIELD_OF_VIEW = 60.0f; | |
| 25 | 33 | |
| 26 | 34 | class GLWidget : public QGLWidget { |
| 27 | 35 | public: |
| @@ -31,17 +39,25 @@ public: · 3 unmodified lines | ||
| 31 | 39 | |
| 32 | 40 | protected: |
| 33 | 41 | virtual void initializeGL(); |
| 34 | − virtual void resizeGL(int w, int h); | |
| 42 | + virtual void resizeGL(int width, int height); | |
| 35 | 43 | virtual void paintGL(); |
| 36 | 44 | |
| 45 | + void mousePressEvent(QMouseEvent* event); | |
| 46 | + void mouseMoveEvent(QMouseEvent* event); | |
| 47 | + void mouseReleaseEvent(QMouseEvent* event); | |
| 48 | + | |
| 37 | 49 | private: |
| 38 | 50 | VoxelSystem* _voxelSystem; |
| 39 | 51 | |
| 40 | 52 | bool _draw; |
| 41 | 53 | |
| 42 | − float _a; | |
| 43 | − float _h; | |
| 54 | + double _a; // horizontal angle of the camera to the center of the object | |
| 55 | + double _h; // vertical angle of the camera to the center of the object | |
| 44 | 56 | glm::vec3 _targetCenter; |
| 57 | + | |
| 58 | + bool _pressed; | |
| 59 | + int _mouseX; | |
| 60 | + int _mouseY; | |
| 45 | 61 | }; |
| 46 | 62 | |
| 47 | 63 | GLWidget::GLWidget(QWidget *parent, VoxelSystem *voxelSystem) |
| @@ -49,8 +65,11 @@ GLWidget::GLWidget(QWidget *parent, VoxelSystem *voxelSystem) · 1 unmodified line | ||
| 49 | 65 | _voxelSystem(voxelSystem), |
| 50 | 66 | _draw(false), |
| 51 | 67 | _a(0.0f), |
| 52 | − _h(0.0f), | |
| 53 | − _targetCenter(0.5f, 0.5f, 0.5f) { | |
| 68 | + _h(VERTICAL_ANGLE), | |
| 69 | + _targetCenter(0.5f, 0.5f, 0.5f), | |
| 70 | + _pressed(false), | |
| 71 | + _mouseX(0), | |
| 72 | + _mouseY(0) { | |
| 54 | 73 | } |
| 55 | 74 | |
| 56 | 75 | void GLWidget::initializeGL() { |
| @@ -65,12 +84,15 @@ void GLWidget::initializeGL() { · 8 unmodified lines | ||
| 65 | 84 | |
| 66 | 85 | } |
| 67 | 86 | |
| 68 | −void GLWidget::resizeGL(int w, int h) { | |
| 69 | − glViewport(0, 0, w, h); | |
| 87 | +void GLWidget::resizeGL(int width, int height) { | |
| 88 | + glViewport(0, 0, width, height); | |
| 70 | 89 | |
| 71 | 90 | glMatrixMode(GL_PROJECTION); |
| 72 | 91 | glLoadIdentity(); |
| 73 | − gluPerspective(60, (float) w / h, 0.25f, 500); | |
| 92 | + gluPerspective(FIELD_OF_VIEW, | |
| 93 | + (float) width / height, | |
| 94 | + NEAR_CLIP, | |
| 95 | + FAR_CLIP); | |
| 74 | 96 | } |
| 75 | 97 | |
| 76 | 98 | void GLWidget::paintGL() { |
| @@ -80,10 +102,14 @@ void GLWidget::paintGL() { · 3 unmodified lines | ||
| 80 | 102 | glMatrixMode(GL_MODELVIEW); |
| 81 | 103 | glLoadIdentity(); |
| 82 | 104 | |
| 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), | |
| 105 | + if (!_pressed) { | |
| 106 | + _a += ANGULAR_RATE; | |
| 107 | + _h = (1.0f - RETURN_RATE) * _h + RETURN_RATE * VERTICAL_ANGLE; | |
| 108 | + } | |
| 109 | + | |
| 110 | + gluLookAt(_targetCenter.x + (glm::length(_targetCenter) + NEAR_CLIP) * cos(_a), | |
| 111 | + _targetCenter.y + (glm::length(_targetCenter) + NEAR_CLIP) * sin(_h), | |
| 112 | + _targetCenter.z + (glm::length(_targetCenter) + NEAR_CLIP) * sin(_a), | |
| 87 | 113 | _targetCenter.x, _targetCenter.y, _targetCenter.z, |
| 88 | 114 | UP.x, UP.y, UP.z); |
| 89 | 115 | |
| @@ -102,7 +128,7 @@ void GLWidget::paintGL() { · 12 unmodified lines | ||
| 102 | 128 | |
| 103 | 129 | |
| 104 | 130 | glColor3d(0.4f, 0.4f ,0.4f); |
| 105 | − glVertex3d(2 * _targetCenter.x, 2 * _targetCenter.y,2 * _targetCenter.z); | |
| 131 | + glVertex3d(2 * _targetCenter.x, 2 * _targetCenter.y, 2 * _targetCenter.z); | |
| 106 | 132 | glVertex3d(0 , 2 * _targetCenter.y, 2 * _targetCenter.z); |
| 107 | 133 | |
| 108 | 134 | glVertex3d(2 * _targetCenter.x, 2 * _targetCenter.y, 2 * _targetCenter.z); |
| @@ -116,18 +142,40 @@ void GLWidget::paintGL() { · 7 unmodified lines | ||
| 116 | 142 | } |
| 117 | 143 | } |
| 118 | 144 | |
| 145 | + | |
| 146 | +void GLWidget::mousePressEvent(QMouseEvent* event) { | |
| 147 | + _pressed = true; | |
| 148 | + _mouseX = event->globalX(); | |
| 149 | + _mouseY = event->globalY(); | |
| 150 | +} | |
| 151 | + | |
| 152 | +void GLWidget::mouseMoveEvent(QMouseEvent* event) { | |
| 153 | + _a += (M_PI * (event->globalX() - _mouseX)) / height(); | |
| 154 | + _h += (M_PI * (event->globalY() - _mouseY)) / height(); | |
| 155 | + _h = glm::clamp(_h, -M_PI_4, M_PI_4); | |
| 156 | + | |
| 157 | + _mouseX = event->globalX(); | |
| 158 | + _mouseY = event->globalY(); | |
| 159 | +} | |
| 160 | + | |
| 161 | +void GLWidget::mouseReleaseEvent(QMouseEvent* event) { | |
| 162 | + _pressed = false; | |
| 163 | +} | |
| 164 | + | |
| 119 | 165 | ImportDialog::ImportDialog(QWidget *parent, VoxelSystem* voxelSystem) |
| 120 | 166 | : 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)) { | |
| 167 | + _importButton (IMPORT_BUTTON_NAME, this), | |
| 168 | + _clipboardImportBox(IMPORT_TO_CLIPBOARD_CHECKBOX_STRING, this), | |
| 169 | + _previewBox (PREVIEW_CHECKBOX_STRING, this), | |
| 170 | + _previewBar (this), | |
| 171 | + _glPreview (new GLWidget(this, voxelSystem)) { | |
| 125 | 172 | |
| 126 | 173 | setOption(QFileDialog::DontUseNativeDialog, true); |
| 127 | 174 | setFileMode(QFileDialog::ExistingFile); |
| 128 | 175 | setViewMode(QFileDialog::Detail); |
| 129 | 176 | |
| 130 | 177 | QGridLayout* gridLayout = (QGridLayout*) layout(); |
| 178 | + gridLayout->addWidget(&_importButton , 2, 2); | |
| 131 | 179 | gridLayout->addWidget(&_clipboardImportBox, 2, 3); |
| 132 | 180 | gridLayout->addWidget(&_previewBox , 3, 3); |
| 133 | 181 | gridLayout->addWidget(&_previewBar , 0, 3); |
| @@ -138,43 +186,71 @@ ImportDialog::ImportDialog(QWidget *parent, VoxelSystem* voxelSystem) · 4 unmodified lines | ||
| 138 | 186 | _previewBar.setRange(0, 100); |
| 139 | 187 | _previewBar.setValue(0); |
| 140 | 188 | |
| 189 | + connect(&_importButton, SIGNAL(pressed()), SLOT(import())); | |
| 190 | + connect(&_previewBox, SIGNAL(toggled(bool)), SIGNAL(previewToggled(bool))); | |
| 141 | 191 | connect(&_previewBox, SIGNAL(toggled(bool)), SLOT(preview(bool))); |
| 192 | + | |
| 142 | 193 | connect(this, SIGNAL(currentChanged(QString)), SLOT(saveCurrentFile(QString))); |
| 143 | 194 | connect(&_glTimer, SIGNAL(timeout()), SLOT(timer())); |
| 144 | 195 | |
| 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))); | |
| 196 | + connect(voxelSystem, SIGNAL(importSize(float,float,float)), SLOT(setGLCamera(float, float, float))); | |
| 197 | + connect(voxelSystem, SIGNAL(importProgress(int)), &_previewBar, SLOT(setValue(int))); | |
| 147 | 198 | } |
| 148 | 199 | |
| 149 | −int ImportDialog::exec() { | |
| 150 | − _previewBox.setChecked(false); | |
| 151 | − _previewBar.setVisible(false); | |
| 200 | +ImportDialog::~ImportDialog() { | |
| 201 | + delete _glPreview; | |
| 202 | +} | |
| 152 | 203 | |
| 153 | − int ret = QFileDialog::exec(); | |
| 204 | +void ImportDialog::import() { | |
| 205 | + _importButton.setDisabled(true); | |
| 206 | + _clipboardImportBox.setDisabled(true); | |
| 207 | + _previewBox.setDisabled(true); | |
| 154 | 208 | |
| 209 | + _previewBar.setValue(0); | |
| 210 | + _previewBar.setVisible(true); | |
| 155 | 211 | |
| 156 | − ((GLWidget*) _glPreview)->setDraw(false); | |
| 157 | − _glTimer.stop(); | |
| 158 | − _glPreview->updateGL(); | |
| 212 | + emit accepted(); | |
| 213 | +} | |
| 214 | + | |
| 215 | +void ImportDialog::accept() { | |
| 216 | + QFileDialog::accept(); | |
| 217 | +} | |
| 218 | + | |
| 219 | +void ImportDialog::reject() { | |
| 220 | + QFileDialog::reject(); | |
| 221 | +} | |
| 222 | + | |
| 223 | +int ImportDialog::exec() { | |
| 224 | + reset(); | |
| 225 | + int ret = QFileDialog::exec(); | |
| 226 | + preview(false); | |
| 159 | 227 | |
| 160 | 228 | return ret; |
| 161 | 229 | } |
| 162 | 230 | |
| 163 | 231 | void ImportDialog::setGLCamera(float x, float y, float z) { |
| 164 | − ((GLWidget*) _glPreview)->setTargetCenter(glm::vec3(x, y, z) / 2.0f); | |
| 232 | + _glPreview->setTargetCenter(glm::vec3(x, y, z) / 2.0f); | |
| 165 | 233 | } |
| 166 | 234 | |
| 167 | 235 | void ImportDialog::reset() { |
| 236 | + _previewBox.setChecked(false); | |
| 237 | + _previewBar.setVisible(false); | |
| 168 | 238 | _previewBar.setValue(0); |
| 239 | + _importButton.setEnabled(true); | |
| 240 | + _clipboardImportBox.setEnabled(true); | |
| 241 | + _previewBox.setEnabled(true); | |
| 242 | + | |
| 243 | + _glTimer.stop(); | |
| 244 | + _glPreview->setDraw(false); | |
| 245 | + _glPreview->updateGL(); | |
| 169 | 246 | } |
| 170 | 247 | |
| 171 | 248 | void ImportDialog::preview(bool wantPreview) { |
| 172 | 249 | _previewBar.setValue(0); |
| 173 | 250 | _previewBar.setVisible(wantPreview); |
| 174 | − ((GLWidget*) _glPreview)->setDraw(wantPreview); | |
| 251 | + _glPreview->setDraw(wantPreview); | |
| 175 | 252 | |
| 176 | 253 | if (wantPreview) { |
| 177 | − emit previewActivated(_currentFile); | |
| 178 | 254 | _glTimer.start(); |
| 179 | 255 | } else { |
| 180 | 256 | _glTimer.stop(); |