From 1ce80b8c2c98bc8aead2f03a4f6394dc54ec66c6 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Sun, 13 Dec 2020 10:33:42 -0800 Subject: [PATCH] Have the view use signals to pass camera changes to the main window. --- common/lc_global.h | 1 + common/lc_glwidget.cpp | 146 ++++++++++++++++++++++++++++++++++-- common/lc_glwidget.h | 25 +++++- common/lc_mainwindow.cpp | 37 ++++++--- common/lc_mainwindow.h | 2 + common/lc_previewwidget.cpp | 42 ----------- common/lc_previewwidget.h | 19 ++--- common/lc_viewsphere.cpp | 2 +- common/view.cpp | 138 +--------------------------------- common/view.h | 11 +-- 10 files changed, 206 insertions(+), 217 deletions(-) diff --git a/common/lc_global.h b/common/lc_global.h index ba7eb3d8..260c347f 100644 --- a/common/lc_global.h +++ b/common/lc_global.h @@ -67,6 +67,7 @@ class PieceInfo; typedef std::map> lcPartsList; struct lcModelPartsEntry; struct lcMinifig; +enum class lcViewpoint; class lcVector2; class lcVector3; diff --git a/common/lc_glwidget.cpp b/common/lc_glwidget.cpp index 3589ad4b..b700e788 100644 --- a/common/lc_glwidget.cpp +++ b/common/lc_glwidget.cpp @@ -9,7 +9,7 @@ #include "lc_model.h" #include "lc_scene.h" -lcGLWidget* lcGLWidget::mLastFocusView; +lcGLWidget* lcGLWidget::mLastFocusedView; lcGLWidget::lcGLWidget(lcModel* Model) : mModel(Model), mScene(new lcScene()) @@ -19,8 +19,8 @@ lcGLWidget::lcGLWidget(lcModel* Model) lcGLWidget::~lcGLWidget() { - if (mLastFocusView == this) - mLastFocusView = nullptr; + if (mLastFocusedView == this) + mLastFocusedView = nullptr; if (mDeleteContext) delete mContext; @@ -34,7 +34,7 @@ lcModel* lcGLWidget::GetActiveModel() const void lcGLWidget::SetFocus(bool Focus) { if (Focus) - mLastFocusView = this; + mLastFocusedView = this; } void lcGLWidget::SetMousePosition(int MouseX, int MouseY) @@ -261,6 +261,142 @@ void lcGLWidget::UnprojectPoints(lcVector3* Points, int NumPoints) const lcUnprojectPoints(Points, NumPoints, mCamera->mWorldView, GetProjectionMatrix(), Viewport); } +void lcGLWidget::ZoomExtents() +{ + lcModel* ActiveModel = GetActiveModel(); + if (ActiveModel) + ActiveModel->ZoomExtents(mCamera, (float)mWidth / (float)mHeight); +} + +void lcGLWidget::SetViewpoint(lcViewpoint Viewpoint) +{ + if (!mCamera || !mCamera->IsSimple()) + { + lcCamera* OldCamera = mCamera; + + mCamera = new lcCamera(true); + + if (OldCamera) + mCamera->CopySettings(OldCamera); + } + + mCamera->SetViewpoint(Viewpoint); + ZoomExtents(); + Redraw(); + + emit CameraChanged(); +} + +void lcGLWidget::SetViewpoint(const lcVector3& Position) +{ + if (!mCamera || !mCamera->IsSimple()) + { + lcCamera* OldCamera = mCamera; + + mCamera = new lcCamera(true); + + if (OldCamera) + mCamera->CopySettings(OldCamera); + } + + mCamera->SetViewpoint(Position); + ZoomExtents(); + Redraw(); + + emit CameraChanged(); +} + +void lcGLWidget::SetViewpoint(const lcVector3& Position, const lcVector3& Target, const lcVector3& Up) +{ + if (!mCamera || !mCamera->IsSimple()) + { + lcCamera* OldCamera = mCamera; + + mCamera = new lcCamera(true); + + if (OldCamera) + mCamera->CopySettings(OldCamera); + } + + mCamera->SetViewpoint(Position, Target, Up); + Redraw(); + + emit CameraChanged(); +} + +void lcGLWidget::SetCameraAngles(float Latitude, float Longitude) +{ + if (!mCamera || !mCamera->IsSimple()) + { + lcCamera* OldCamera = mCamera; + + mCamera = new lcCamera(true); + + if (OldCamera) + mCamera->CopySettings(OldCamera); + } + + mCamera->SetAngles(Latitude, Longitude, 1.0f); + ZoomExtents(); + Redraw(); +} + +void lcGLWidget::SetDefaultCamera() +{ + if (!mCamera || !mCamera->IsSimple()) + mCamera = new lcCamera(true); + + mCamera->SetViewpoint(lcViewpoint::Home); + + emit CameraChanged(); +} + +void lcGLWidget::SetCamera(lcCamera* Camera, bool ForceCopy) +{ + if (Camera->IsSimple() || ForceCopy) + { + if (!mCamera || !mCamera->IsSimple()) + mCamera = new lcCamera(true); + + mCamera->CopyPosition(Camera); + } + else + { + if (mCamera && mCamera->IsSimple()) + delete mCamera; + + mCamera = Camera; + } +} + +void lcGLWidget::SetCamera(const char* CameraName) +{ + const lcArray& Cameras = mModel->GetCameras(); + + for (int CameraIdx = 0; CameraIdx < Cameras.GetSize(); CameraIdx++) + { + if (qstricmp(CameraName, Cameras[CameraIdx]->m_strName) == 0) + { + SetCameraIndex(CameraIdx); + return; + } + } +} + +void lcGLWidget::SetCameraIndex(int Index) +{ + const lcArray& Cameras = mModel->GetCameras(); + + if (Index >= Cameras.GetSize()) + return; + + lcCamera* Camera = Cameras[Index]; + SetCamera(Camera, false); + + emit CameraChanged(); + Redraw(); +} + void lcGLWidget::StartTracking(lcTrackButton TrackButton) { mTrackButton = TrackButton; @@ -410,7 +546,7 @@ void lcGLWidget::DrawViewport() const mContext->SetMaterial(lcMaterialType::UnlitColor); - if (mLastFocusView == this) + if (mLastFocusedView == this) mContext->SetColor(lcVector4FromColor(lcGetPreferences().mActiveViewColor)); else mContext->SetColor(lcVector4FromColor(lcGetPreferences().mInactiveViewColor)); diff --git a/common/lc_glwidget.h b/common/lc_glwidget.h index 13916255..108041e4 100644 --- a/common/lc_glwidget.h +++ b/common/lc_glwidget.h @@ -80,8 +80,10 @@ enum class lcTrackTool Count }; -class lcGLWidget +class lcGLWidget : public QObject { + Q_OBJECT + public: lcGLWidget(lcModel* Model); virtual ~lcGLWidget(); @@ -96,6 +98,11 @@ public: return mCamera; } + bool IsLastFocused() const + { + return mLastFocusedView == this; + } + bool IsTracking() const { return mTrackButton != lcTrackButton::None; @@ -124,6 +131,17 @@ public: void UnprojectPoints(lcVector3* Points, int NumPoints) const; lcMatrix44 GetProjectionMatrix() const; + void ZoomExtents(); + + void SetViewpoint(lcViewpoint Viewpoint); + void SetViewpoint(const lcVector3& Position); + void SetViewpoint(const lcVector3& Position, const lcVector3& Target, const lcVector3& Up); + void SetCameraAngles(float Latitude, float Longitude); + void SetDefaultCamera(); + void SetCamera(lcCamera* Camera, bool ForceCopy); + void SetCamera(const char* CameraName); + void SetCameraIndex(int Index); + void DrawBackground() const; void DrawViewport() const; void DrawAxes() const; @@ -151,6 +169,9 @@ public: QGLWidget* mWidget = nullptr; lcContext* mContext = nullptr; +signals: + void CameraChanged(); + protected: lcCursor GetCursor() const; void SetCursor(lcCursor Cursor); @@ -178,5 +199,5 @@ protected: lcCamera* mCamera = nullptr; bool mDeleteContext = true; - static lcGLWidget* mLastFocusView; + static lcGLWidget* mLastFocusedView; }; diff --git a/common/lc_mainwindow.cpp b/common/lc_mainwindow.cpp index f6c6ea06..e2a25466 100644 --- a/common/lc_mainwindow.cpp +++ b/common/lc_mainwindow.cpp @@ -760,14 +760,23 @@ void lcMainWindow::CreateToolBars() tabifyDockWidget(mPartsToolBar, mPropertiesToolBar); tabifyDockWidget(mPropertiesToolBar, mTimelineToolBar); - connect(mPropertiesToolBar, SIGNAL (topLevelChanged(bool)), this, SLOT (EnableWindowFlags(bool))); - connect(mTimelineToolBar, SIGNAL (topLevelChanged(bool)), this, SLOT (EnableWindowFlags(bool))); - connect(mPartsToolBar, SIGNAL (topLevelChanged(bool)), this, SLOT (EnableWindowFlags(bool))); - connect(mColorsToolBar, SIGNAL (topLevelChanged(bool)), this, SLOT (EnableWindowFlags(bool))); + connect(mPropertiesToolBar, SIGNAL(topLevelChanged(bool)), this, SLOT(EnableWindowFlags(bool))); + connect(mTimelineToolBar, SIGNAL(topLevelChanged(bool)), this, SLOT(EnableWindowFlags(bool))); + connect(mPartsToolBar, SIGNAL(topLevelChanged(bool)), this, SLOT(EnableWindowFlags(bool))); + connect(mColorsToolBar, SIGNAL(topLevelChanged(bool)), this, SLOT(EnableWindowFlags(bool))); mPartsToolBar->raise(); } +View* lcMainWindow::CreateView(lcModel* Model) +{ + View* NewView = new View(Model); + + connect(NewView, SIGNAL(CameraChanged()), this, SLOT(ViewCameraChanged())); + + return NewView; +} + void lcMainWindow::PreviewPiece(const QString &PartType, int ColorCode) { if (mPreviewWidget) { @@ -789,7 +798,7 @@ void lcMainWindow::CreatePreviewWidget() tabifyDockWidget(mTimelineToolBar, mPreviewToolBar); - connect(mPreviewToolBar, SIGNAL (topLevelChanged(bool)), this, SLOT (EnableWindowFlags(bool))); + connect(mPreviewToolBar, SIGNAL(topLevelChanged(bool)), this, SLOT(EnableWindowFlags(bool))); } void lcMainWindow::TogglePreviewWidget(bool visible) @@ -1599,7 +1608,7 @@ void lcMainWindow::SetCurrentModelTab(lcModel* Model) QGridLayout* CentralLayout = new QGridLayout(TabWidget); CentralLayout->setContentsMargins(0, 0, 0, 0); - NewView = new View(Model); + NewView = CreateView(Model); ViewWidget = new lcQGLWidget(TabWidget, NewView); CentralLayout->addWidget(ViewWidget, 0, 0, 1, 1); @@ -1610,7 +1619,7 @@ void lcMainWindow::SetCurrentModelTab(lcModel* Model) TabWidget = EmptyWidget; TabWidget->SetModel(Model); - NewView = new View(Model); + NewView = CreateView(Model); ViewWidget = (lcQGLWidget*)TabWidget->layout()->itemAt(0)->widget(); ViewWidget->mWidget = NewView; NewView->mWidget = ViewWidget; @@ -1825,7 +1834,7 @@ void lcMainWindow::SplitView(Qt::Orientation Orientation) Splitter = new QSplitter(Orientation, Parent); Parent->layout()->addWidget(Splitter); Splitter->addWidget(Focus); - Splitter->addWidget(new lcQGLWidget(mModelTabWidget->currentWidget(), new View(GetCurrentTabModel()))); + Splitter->addWidget(new lcQGLWidget(mModelTabWidget->currentWidget(), CreateView(GetCurrentTabModel()))); } else { @@ -1836,7 +1845,7 @@ void lcMainWindow::SplitView(Qt::Orientation Orientation) Splitter = new QSplitter(Orientation, Parent); ParentSplitter->insertWidget(FocusIndex, Splitter); Splitter->addWidget(Focus); - Splitter->addWidget(new lcQGLWidget(mModelTabWidget->currentWidget(), new View(GetCurrentTabModel()))); + Splitter->addWidget(new lcQGLWidget(mModelTabWidget->currentWidget(), CreateView(GetCurrentTabModel()))); ParentSplitter->setSizes(Sizes); } @@ -2154,6 +2163,16 @@ void lcMainWindow::UpdateUndoRedo(const QString& UndoText, const QString& RedoTe } } +void lcMainWindow::ViewCameraChanged() +{ + lcGLWidget* View = dynamic_cast(sender()); + + if (!View || !View->IsLastFocused()) + return; + + UpdateCameraMenu(); +} + void lcMainWindow::UpdateCameraMenu() { const lcArray& Cameras = lcGetActiveModel()->GetCameras(); diff --git a/common/lc_mainwindow.h b/common/lc_mainwindow.h index e6a2bc1e..62d3a9e2 100644 --- a/common/lc_mainwindow.h +++ b/common/lc_mainwindow.h @@ -362,6 +362,7 @@ public slots: void TogglePreviewWidget(bool); protected slots: + void ViewCameraChanged(); void UpdateDockWidgetActions(); void UpdateGamepads(); void ModelTabContextMenuRequested(const QPoint& Point); @@ -384,6 +385,7 @@ protected: void CreateMenus(); void CreateToolBars(); void CreateStatusBar(); + View* CreateView(lcModel* Model); void ToggleDockWidget(QWidget* DockWidget); void SplitView(Qt::Orientation Orientation); void ShowSearchDialog(); diff --git a/common/lc_previewwidget.cpp b/common/lc_previewwidget.cpp index 6eaa842f..fb6794ce 100644 --- a/common/lc_previewwidget.cpp +++ b/common/lc_previewwidget.cpp @@ -188,31 +188,6 @@ void lcPreviewWidget::UpdatePreview() SetCurrentPiece(PartType, ColorCode); } -void lcPreviewWidget::SetDefaultCamera() -{ - if (!mCamera || !mCamera->IsSimple()) - mCamera = new lcCamera(true); - mCamera->SetViewpoint(lcViewpoint::Home); -} - -void lcPreviewWidget::SetCamera(lcCamera* Camera) // called by lcModel::DeleteModel() -{ - if (!mCamera || !mCamera->IsSimple()) - mCamera = new lcCamera(true); - - mCamera->CopyPosition(Camera); -} - -void lcPreviewWidget::ZoomExtents() -{ - lcModel* ActiveModel = GetActiveModel(); - if (ActiveModel) - { - ActiveModel->ZoomExtents(mCamera, float(mWidth) / float(mHeight)); - Redraw(); - } -} - void lcPreviewWidget::StartOrbitTracking() // called by viewSphere { mTrackTool = lcTrackTool::OrbitXY; @@ -222,23 +197,6 @@ void lcPreviewWidget::StartOrbitTracking() // called by viewSphere OnButtonDown(lcTrackButton::Left); } -void lcPreviewWidget::SetViewpoint(const lcVector3& Position) -{ - if (!mCamera || !mCamera->IsSimple()) - { - lcCamera* OldCamera = mCamera; - - mCamera = new lcCamera(true); - - if (OldCamera) - mCamera->CopySettings(OldCamera); - } - - mCamera->SetViewpoint(Position); - - Redraw(); -} - void lcPreviewWidget::StopTracking(bool Accept) { if (mTrackButton == lcTrackButton::None) diff --git a/common/lc_previewwidget.h b/common/lc_previewwidget.h index aca01fd3..099b61ca 100644 --- a/common/lc_previewwidget.h +++ b/common/lc_previewwidget.h @@ -40,22 +40,17 @@ public: return mDescription; } - void ClearPreview(); - void UpdatePreview(); - bool SetCurrentPiece(const QString& PartType, int ColorCode); - void SetCamera(lcCamera* Camera); - void SetDefaultCamera(); - void ZoomExtents(); - - // exclusively called from viewSphere - void SetViewpoint(const lcVector3& Position); - void StartOrbitTracking(); - bool IsModel() const { return mIsModel; } - + + void ClearPreview(); + void UpdatePreview(); + bool SetCurrentPiece(const QString& PartType, int ColorCode); + + void StartOrbitTracking(); + void OnDraw() override; void OnLeftButtonDown() override; void OnLeftButtonUp() override; diff --git a/common/lc_viewsphere.cpp b/common/lc_viewsphere.cpp index ad2f1bb6..f2ff15c1 100644 --- a/common/lc_viewsphere.cpp +++ b/common/lc_viewsphere.cpp @@ -283,7 +283,7 @@ bool lcViewSphere::OnLeftButtonUp() Position[AxisIdx] = -1250.0f; } - mIsPreview ? mPreview->SetViewpoint(Position) : mView->SetViewpoint(Position); + mWidget->SetViewpoint(Position); return true; } diff --git a/common/view.cpp b/common/view.cpp index aa5c682f..57f4436a 100644 --- a/common/view.cpp +++ b/common/view.cpp @@ -257,139 +257,10 @@ void View::RemoveCamera() else mCamera->SetViewpoint(lcViewpoint::Home); - gMainWindow->UpdateCurrentCamera(-1); + emit CameraChanged(); Redraw(); } -void View::SetCamera(lcCamera* Camera, bool ForceCopy) -{ - if (Camera->IsSimple() || ForceCopy) - { - if (!mCamera || !mCamera->IsSimple()) - mCamera = new lcCamera(true); - - mCamera->CopyPosition(Camera); - } - else - { - if (mCamera && mCamera->IsSimple()) - delete mCamera; - - mCamera = Camera; - } -} - -void View::SetCamera(const char* CameraName) -{ - const lcArray& Cameras = mModel->GetCameras(); - - for (int CameraIdx = 0; CameraIdx < Cameras.GetSize(); CameraIdx++) - { - if (qstricmp(CameraName, Cameras[CameraIdx]->m_strName) == 0) - { - SetCameraIndex(CameraIdx); - return; - } - } -} - -void View::SetCameraIndex(int Index) -{ - const lcArray& Cameras = mModel->GetCameras(); - - if (Index >= Cameras.GetSize()) - return; - - lcCamera* Camera = Cameras[Index]; - SetCamera(Camera, false); - - gMainWindow->UpdateCurrentCamera(Index); - Redraw(); -} - -void View::SetViewpoint(lcViewpoint Viewpoint) -{ - if (!mCamera || !mCamera->IsSimple()) - { - lcCamera* OldCamera = mCamera; - - mCamera = new lcCamera(true); - - if (OldCamera) - mCamera->CopySettings(OldCamera); - } - - mCamera->SetViewpoint(Viewpoint); - ZoomExtents(); - Redraw(); - - gMainWindow->UpdateCurrentCamera(-1); -} - -void View::SetViewpoint(const lcVector3& Position) -{ - if (!mCamera || !mCamera->IsSimple()) - { - lcCamera* OldCamera = mCamera; - - mCamera = new lcCamera(true); - - if (OldCamera) - mCamera->CopySettings(OldCamera); - } - - mCamera->SetViewpoint(Position); - ZoomExtents(); - Redraw(); - - gMainWindow->UpdateCurrentCamera(-1); -} - -void View::SetViewpoint(const lcVector3& Position, const lcVector3& Target, const lcVector3& Up) -{ - if (!mCamera || !mCamera->IsSimple()) - { - lcCamera* OldCamera = mCamera; - - mCamera = new lcCamera(true); - - if (OldCamera) - mCamera->CopySettings(OldCamera); - } - - mCamera->SetViewpoint(Position, Target, Up); - Redraw(); - - gMainWindow->UpdateCurrentCamera(-1); -} - -void View::SetCameraAngles(float Latitude, float Longitude) -{ - if (!mCamera || !mCamera->IsSimple()) - { - lcCamera* OldCamera = mCamera; - - mCamera = new lcCamera(true); - - if (OldCamera) - mCamera->CopySettings(OldCamera); - } - - mCamera->SetAngles(Latitude, Longitude, 1.0f); - ZoomExtents(); - Redraw(); -} - -void View::SetDefaultCamera() -{ - if (!mCamera || !mCamera->IsSimple()) - mCamera = new lcCamera(true); - - mCamera->SetViewpoint(lcViewpoint::Home); - - gMainWindow->UpdateCurrentCamera(-1); -} - lcMatrix44 View::GetTileProjectionMatrix(int CurrentRow, int CurrentColumn, int CurrentTileWidth, int CurrentTileHeight) const { int ImageWidth = mRenderImage.width(); @@ -1791,13 +1662,6 @@ void View::LookAt() ActiveModel->LookAt(mCamera); } -void View::ZoomExtents() -{ - lcModel* ActiveModel = GetActiveModel(); - if (ActiveModel) - ActiveModel->ZoomExtents(mCamera, (float)mWidth / (float)mHeight); -} - void View::MoveCamera(const lcVector3& Direction) { lcModel* ActiveModel = GetActiveModel(); diff --git a/common/view.h b/common/view.h index a47a3fd1..b3afe5f7 100644 --- a/common/view.h +++ b/common/view.h @@ -6,6 +6,8 @@ class View : public lcGLWidget { + Q_OBJECT + public: View(lcModel* Model); ~View(); @@ -53,19 +55,10 @@ public: void SetProjection(bool Ortho); void LookAt(); - void ZoomExtents(); void MoveCamera(const lcVector3& Direction); void Zoom(float Amount); void RemoveCamera(); - void SetCamera(lcCamera* Camera, bool ForceCopy); - void SetCamera(const char* CameraName); - void SetCameraIndex(int Index); - void SetViewpoint(lcViewpoint Viewpoint); - void SetViewpoint(const lcVector3& Position); - void SetViewpoint(const lcVector3& Position, const lcVector3& Target, const lcVector3& Up); - void SetCameraAngles(float Latitude, float Longitude); - void SetDefaultCamera(); void ShowContextMenu() const; lcVector3 GetMoveDirection(const lcVector3& Direction) const;