From e27b2359361684ee622be6749cf5a381baa63497 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Thu, 24 Dec 2020 11:32:56 -0800 Subject: [PATCH] Derive preview from view. --- common/lc_mainwindow.cpp | 2 +- common/lc_model.cpp | 1 + common/lc_partselectionwidget.cpp | 1 + common/lc_previewwidget.cpp | 324 ++---------------------------- common/lc_previewwidget.h | 33 +-- common/lc_viewsphere.cpp | 43 ++-- common/lc_viewsphere.h | 5 - common/lc_viewwidget.cpp | 2 +- common/view.cpp | 5 +- 9 files changed, 54 insertions(+), 362 deletions(-) diff --git a/common/lc_mainwindow.cpp b/common/lc_mainwindow.cpp index 72f9cd22..cee3fd1c 100644 --- a/common/lc_mainwindow.cpp +++ b/common/lc_mainwindow.cpp @@ -792,7 +792,7 @@ void lcMainWindow::PreviewPiece(const QString& PartId, int ColorCode) } else { - lcPreviewWidget* Preview = new lcPreviewWidget(); + lcPreview* Preview = new lcPreview(); lcViewWidget* ViewWidget = new lcViewWidget(nullptr, Preview); if (Preview && ViewWidget) diff --git a/common/lc_model.cpp b/common/lc_model.cpp index f6175ac5..f39ffa53 100644 --- a/common/lc_model.cpp +++ b/common/lc_model.cpp @@ -8,6 +8,7 @@ #include "lc_mainwindow.h" #include "lc_profile.h" #include "lc_library.h" +#include "lc_scene.h" #include "lc_texture.h" #include "lc_synth.h" #include "lc_file.h" diff --git a/common/lc_partselectionwidget.cpp b/common/lc_partselectionwidget.cpp index 5cf8fd24..7925e1f8 100644 --- a/common/lc_partselectionwidget.cpp +++ b/common/lc_partselectionwidget.cpp @@ -8,6 +8,7 @@ #include "lc_model.h" #include "project.h" #include "pieceinf.h" +#include "lc_scene.h" #include "view.h" #include "lc_glextensions.h" #include "lc_viewwidget.h" diff --git a/common/lc_previewwidget.cpp b/common/lc_previewwidget.cpp index 5ac2bba5..78f75f49 100644 --- a/common/lc_previewwidget.cpp +++ b/common/lc_previewwidget.cpp @@ -4,15 +4,14 @@ #include "piece.h" #include "project.h" #include "lc_model.h" -#include "camera.h" #include "lc_library.h" -#include "lc_viewsphere.h" #include "lc_viewwidget.h" +#include "view.h" lcPreviewDockWidget::lcPreviewDockWidget(QMainWindow* Parent) : QMainWindow(Parent) { - mPreview = new lcPreviewWidget(); + mPreview = new lcPreview(); mViewWidget = new lcViewWidget(nullptr, mPreview); setCentralWidget(mViewWidget); setMinimumSize(200, 200); @@ -24,7 +23,7 @@ lcPreviewDockWidget::lcPreviewDockWidget(QMainWindow* Parent) connect(mLockAction, SIGNAL(triggered()), this, SLOT(SetPreviewLock())); SetPreviewLock(); - mLabel = new QLabel(QString()); + mLabel = new QLabel(); mToolBar = addToolBar(tr("Toolbar")); mToolBar->setObjectName("Toolbar"); @@ -58,54 +57,46 @@ void lcPreviewDockWidget::UpdatePreview() void lcPreviewDockWidget::ClearPreview() { - if (mPreview->GetActiveModel()->GetPieces().GetSize()) + if (mPreview->GetModel()->GetPieces().GetSize()) mPreview->ClearPreview(); + mLabel->setText(QString()); } void lcPreviewDockWidget::SetPreviewLock() { bool Locked = mLockAction->isChecked(); - if (Locked && mPreview->GetActiveModel()->GetPieces().IsEmpty()) + + if (Locked && mPreview->GetModel()->GetPieces().IsEmpty()) { mLockAction->setChecked(false); return; } + QIcon LockIcon(Locked ? ":/resources/action_preview_locked.png" : ":/resources/action_preview_unlocked.png"); QString State(Locked ? tr("Unlock") : tr("Lock")); QString StatusTip(tr("%1 the preview display to %2 updates").arg(State).arg(Locked ? "enable" : "disable")); + mLockAction->setToolTip(tr("%1 Preview").arg(State)); mLockAction->setIcon(LockIcon); mLockAction->setStatusTip(StatusTip); } -lcPreviewWidget::lcPreviewWidget() - : lcGLWidget(lcViewType::Preview, nullptr), mLoader(new Project(true)) +lcPreview::lcPreview() + : View(lcViewType::Preview, nullptr), mLoader(new Project(true)) { - mViewSphere = std::unique_ptr(new lcViewSphere(this)); mLoader->SetActiveModel(0); mModel = mLoader->GetActiveModel(); - - SetDefaultCamera(); } -lcPreviewWidget::~lcPreviewWidget() -{ - if (mCamera && mCamera->IsSimple()) - delete mCamera; - - delete mLoader; -} - -bool lcPreviewWidget::SetCurrentPiece(const QString& PartType, int ColorCode) +bool lcPreview::SetCurrentPiece(const QString& PartType, int ColorCode) { lcPiecesLibrary* Library = lcGetPiecesLibrary(); PieceInfo* Info = Library->FindPiece(PartType.toLatin1().constData(), nullptr, false, false); if (Info) { - lcModel* ActiveModel = GetActiveModel(); - for (lcPiece* ModelPiece : ActiveModel->GetPieces()) + for (lcPiece* ModelPiece : mModel->GetPieces()) { if (Info == ModelPiece->mPieceInfo) { @@ -118,8 +109,8 @@ bool lcPreviewWidget::SetCurrentPiece(const QString& PartType, int ColorCode) mIsModel = Info->IsModel(); mDescription = Info->m_strDescription; - ActiveModel->SelectAllPieces(); - ActiveModel->DeleteSelectedObjects(); + mModel->SelectAllPieces(); + mModel->DeleteSelectedObjects(); Library->LoadPieceInfo(Info, false, true); Library->WaitForLoadQueue(); @@ -131,7 +122,7 @@ bool lcPreviewWidget::SetCurrentPiece(const QString& PartType, int ColorCode) Piece->Initialize(lcMatrix44Identity(), CurrentStep); Piece->SetColorCode(ColorCode); - ActiveModel->SetPreviewPiece(Piece); + mModel->SetPreviewPiece(Piece); } else { @@ -158,22 +149,21 @@ bool lcPreviewWidget::SetCurrentPiece(const QString& PartType, int ColorCode) return true; } -void lcPreviewWidget::ClearPreview() +void lcPreview::ClearPreview() { - delete mLoader; - mLoader = new Project(true/*IsPreview*/); + mLoader = std::unique_ptr(new Project(true/*IsPreview*/)); mLoader->SetActiveModel(0); mModel = mLoader->GetActiveModel(); lcGetPiecesLibrary()->UnloadUnusedParts(); Redraw(); } -void lcPreviewWidget::UpdatePreview() +void lcPreview::UpdatePreview() { QString PartType; int ColorCode = -1; - lcModel* ActiveModel = GetActiveModel(); - for (lcPiece* ModelPiece : ActiveModel->GetPieces()) + + for (lcPiece* ModelPiece : mModel->GetPieces()) { if (ModelPiece->mPieceInfo) { @@ -188,275 +178,3 @@ void lcPreviewWidget::UpdatePreview() if (!PartType.isEmpty() && ColorCode > -1) SetCurrentPiece(PartType, ColorCode); } - -void lcPreviewWidget::StartOrbitTracking() // called by viewSphere -{ - mTrackTool = lcTrackTool::OrbitXY; - - UpdateCursor(); - - OnButtonDown(lcTrackButton::Left); -} - -void lcPreviewWidget::StopTracking(bool Accept) -{ - if (mTrackButton == lcTrackButton::None) - return; - - lcTool Tool = GetCurrentTool(); - lcModel* ActiveModel = GetActiveModel(); - - switch (Tool) - { - case lcTool::Select: - break; - - case lcTool::Pan: - case lcTool::RotateView: - ActiveModel->EndMouseTool(Tool, Accept); - break; - - case lcTool::Count: - default: - break; - } - - mTrackButton = lcTrackButton::None; - - mTrackTool = lcTrackTool::None; - - UpdateCursor(); -} - -void lcPreviewWidget::OnButtonDown(lcTrackButton TrackButton) -{ - switch (mTrackTool) - { - case lcTrackTool::None: - break; - - case lcTrackTool::Pan: - StartTracking(TrackButton); - break; - - case lcTrackTool::OrbitXY: - StartTracking(TrackButton); - break; - - default: - break; - } -} - -void lcPreviewWidget::OnDraw() -{ - if (!mModel) - return; - - lcPreferences& Preferences = lcGetPreferences(); - const bool DrawInterface = mWidget != nullptr; - - mScene->SetAllowLOD(Preferences.mAllowLOD && mWidget != nullptr); - mScene->SetLODDistance(Preferences.mMeshLODDistance); - - mScene->Begin(mCamera->mWorldView); - - mScene->SetDrawInterface(DrawInterface); - - mModel->GetScene(mScene.get(), mCamera, false /*HighlightNewParts*/, false/*mFadeSteps*/); - - mScene->End(); - - mContext->SetDefaultState(); - - mContext->SetViewport(0, 0, mWidth, mHeight); - - DrawBackground(); - - mContext->SetProjectionMatrix(GetProjectionMatrix()); - - mContext->SetLineWidth(Preferences.mLineWidth); - - mScene->Draw(mContext); - - if (DrawInterface) - { - mContext->SetLineWidth(1.0f); - - if (Preferences.mDrawPreviewAxis) - DrawAxes(); - - if (Preferences.mDrawPreviewViewSphere) - mViewSphere->Draw(); - DrawViewport(); - } - - mContext->ClearResources(); -} - -void lcPreviewWidget::OnLeftButtonDown() -{ - if (mTrackButton != lcTrackButton::None) - { - StopTracking(false); - return; - } - - if (mViewSphere->OnLeftButtonDown()) - return; - - lcTrackTool OverrideTool = lcTrackTool::OrbitXY; - - if (OverrideTool != lcTrackTool::None) - { - mTrackTool = OverrideTool; - UpdateCursor(); - } - - OnButtonDown(lcTrackButton::Left); -} - -void lcPreviewWidget::OnLeftButtonUp() -{ - StopTracking(mTrackButton == lcTrackButton::Left); - - if (mViewSphere->OnLeftButtonUp()) - { - ZoomExtents(); - return; - } -} - -void lcPreviewWidget::OnMiddleButtonDown() -{ - if (mTrackButton != lcTrackButton::None) - { - StopTracking(false); - return; - } - -#if (QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)) - lcTrackTool OverrideTool = lcTrackTool::None; - - if (OverrideTool != lcTrackTool::None) - { - mTrackTool = OverrideTool; - UpdateCursor(); - } -#endif - OnButtonDown(lcTrackButton::Middle); -} - -void lcPreviewWidget::OnMiddleButtonUp() -{ - StopTracking(mTrackButton == lcTrackButton::Middle); -} - -void lcPreviewWidget::OnLeftButtonDoubleClick() -{ - ZoomExtents(); - Redraw(); -} - -void lcPreviewWidget::OnRightButtonDown() -{ - if (mTrackButton != lcTrackButton::None) - { - StopTracking(false); - return; - } - - lcTrackTool OverrideTool = lcTrackTool::Pan; - - if (OverrideTool != lcTrackTool::None) - { - mTrackTool = OverrideTool; - UpdateCursor(); - } - - OnButtonDown(lcTrackButton::Middle); -} - -void lcPreviewWidget::OnRightButtonUp() -{ - if (mTrackButton != lcTrackButton::None) - StopTracking(mTrackButton == lcTrackButton::Right); -} - -void lcPreviewWidget::OnMouseMove() -{ - lcModel* ActiveModel = GetActiveModel(); - - if (!ActiveModel) - return; - - if (mTrackButton == lcTrackButton::None) - { - if (mViewSphere->OnMouseMove()) - { - lcTrackTool NewTrackTool = mViewSphere->IsDragging() ? lcTrackTool::OrbitXY : lcTrackTool::None; - - if (NewTrackTool != mTrackTool) - { - mTrackTool = NewTrackTool; - UpdateCursor(); - } - - return; - } - - return; - } - - const float MouseSensitivity = 0.5f / (21.0f - lcGetPreferences().mMouseSensitivity); - - switch (mTrackTool) - { - case lcTrackTool::None: - break; - - case lcTrackTool::Pan: - { - lcVector3 Points[4] = - { - lcVector3((float)mMouseX, (float)mMouseY, 0.0f), - lcVector3((float)mMouseX, (float)mMouseY, 1.0f), - lcVector3(mMouseDownX, mMouseDownY, 0.0f), - lcVector3(mMouseDownX, mMouseDownY, 1.0f) - }; - - UnprojectPoints(Points, 4); - - const lcVector3& CurrentStart = Points[0]; - const lcVector3& CurrentEnd = Points[1]; - const lcVector3& MouseDownStart = Points[2]; - const lcVector3& MouseDownEnd = Points[3]; - lcVector3 Center = ActiveModel->GetSelectionOrModelCenter(); - - lcVector3 PlaneNormal(mCamera->mPosition - mCamera->mTargetPosition); - lcVector4 Plane(PlaneNormal, -lcDot(PlaneNormal, Center)); - lcVector3 Intersection, MoveStart; - - if (!lcLineSegmentPlaneIntersection(&Intersection, CurrentStart, CurrentEnd, Plane) || !lcLineSegmentPlaneIntersection(&MoveStart, MouseDownStart, MouseDownEnd, Plane)) - { - Center = MouseDownStart + lcNormalize(MouseDownEnd - MouseDownStart) * 10.0f; - Plane = lcVector4(PlaneNormal, -lcDot(PlaneNormal, Center)); - - if (!lcLineSegmentPlaneIntersection(&Intersection, CurrentStart, CurrentEnd, Plane) || !lcLineSegmentPlaneIntersection(&MoveStart, MouseDownStart, MouseDownEnd, Plane)) - break; - } - - ActiveModel->UpdatePanTool(mCamera, MoveStart - Intersection); - Redraw(); - } - break; - - case lcTrackTool::OrbitXY: - ActiveModel->UpdateOrbitTool(mCamera, 0.1f * MouseSensitivity * (mMouseX - mMouseDownX), 0.1f * MouseSensitivity * (mMouseY - mMouseDownY)); - Redraw(); - break; - - default: - break; - } -} diff --git a/common/lc_previewwidget.h b/common/lc_previewwidget.h index 0ffcf282..d3f4bc9b 100644 --- a/common/lc_previewwidget.h +++ b/common/lc_previewwidget.h @@ -1,10 +1,8 @@ #pragma once -#include "lc_glwidget.h" -#include "lc_scene.h" -#include "lc_commands.h" +#include "view.h" -class lcPreviewWidget; +class lcPreview; class lcPreviewDockWidget : public QMainWindow { @@ -12,6 +10,7 @@ class lcPreviewDockWidget : public QMainWindow public: explicit lcPreviewDockWidget(QMainWindow* Parent = nullptr); + bool SetCurrentPiece(const QString& PartType, int ColorCode); void ClearPreview(); void UpdatePreview(); @@ -23,15 +22,14 @@ protected: QAction* mLockAction; QToolBar* mToolBar; QLabel* mLabel; - lcPreviewWidget* mPreview; + lcPreview* mPreview; lcViewWidget* mViewWidget; }; -class lcPreviewWidget : public lcGLWidget +class lcPreview : public View { public: - lcPreviewWidget(); - ~lcPreviewWidget(); + lcPreview(); QString GetDescription() const { @@ -47,24 +45,9 @@ public: void UpdatePreview(); bool SetCurrentPiece(const QString& PartType, int ColorCode); - void StartOrbitTracking(); - - void OnDraw() override; - void OnLeftButtonDown() override; - void OnLeftButtonUp() override; - void OnLeftButtonDoubleClick() override; - void OnMiddleButtonDown() override; - void OnMiddleButtonUp() override; - void OnRightButtonDown() override; - void OnRightButtonUp() override; - void OnMouseMove() override; - protected: - void StopTracking(bool Accept); - void OnButtonDown(lcTrackButton TrackButton); - - Project* mLoader; + std::unique_ptr mLoader; QString mDescription; - bool mIsModel; + bool mIsModel = false; }; diff --git a/common/lc_viewsphere.cpp b/common/lc_viewsphere.cpp index 332a17c9..aae40ead 100644 --- a/common/lc_viewsphere.cpp +++ b/common/lc_viewsphere.cpp @@ -1,7 +1,6 @@ #include "lc_global.h" #include "lc_viewsphere.h" #include "view.h" -#include "lc_previewwidget.h" #include "camera.h" #include "lc_context.h" #include "lc_stringcache.h" @@ -17,13 +16,7 @@ const float lcViewSphere::mHighlightRadius = 0.35f; const int lcViewSphere::mSubdivisions = 7; lcViewSphere::lcViewSphere(View* View) - : mWidget(View), mPreview(nullptr), mView(View), mIsPreview(false) -{ - UpdateSettings(); -} - -lcViewSphere::lcViewSphere(lcPreviewWidget* Preview) - : mWidget(Preview), mPreview(Preview), mView(nullptr), mIsPreview(true) + : mView(View) { UpdateSettings(); } @@ -32,7 +25,7 @@ void lcViewSphere::UpdateSettings() { const lcPreferences& Preferences = lcGetPreferences(); - switch (mWidget->GetViewType()) + switch (mView->GetViewType()) { case lcViewType::View: mSize = Preferences.mViewSphereSize; @@ -54,7 +47,7 @@ void lcViewSphere::UpdateSettings() lcMatrix44 lcViewSphere::GetViewMatrix() const { - lcMatrix44 ViewMatrix = mWidget->GetCamera()->mWorldView; + lcMatrix44 ViewMatrix = mView->GetCamera()->mWorldView; ViewMatrix.SetTranslation(lcVector3(0, 0, 0)); return ViewMatrix; } @@ -188,9 +181,9 @@ void lcViewSphere::Draw() if (!mSize || !mEnabled) return; - lcContext* Context = mWidget->mContext; - const int Width = mWidget->GetWidth(); - const int Height = mWidget->GetHeight(); + lcContext* Context = mView->mContext; + const int Width = mView->GetWidth(); + const int Height = mView->GetHeight(); const int ViewportSize = mSize; const int Left = (mLocation == lcViewSphereLocation::BottomLeft || mLocation == lcViewSphereLocation::TopLeft) ? 0 : Width - ViewportSize; const int Bottom = (mLocation == lcViewSphereLocation::BottomLeft || mLocation == lcViewSphereLocation::BottomRight) ? 0 : Height - ViewportSize; @@ -259,8 +252,8 @@ bool lcViewSphere::OnLeftButtonDown() if (!mIntersectionFlags.any()) return false; - mMouseDownX = mWidget->GetMouseX(); - mMouseDownY = mWidget->GetMouseY(); + mMouseDownX = mView->GetMouseX(); + mMouseDownY = mView->GetMouseY(); mMouseDown = true; return true; @@ -289,7 +282,7 @@ bool lcViewSphere::OnLeftButtonUp() Position[AxisIdx] = -1250.0f; } - mWidget->SetViewpoint(Position); + mView->SetViewpoint(Position); return true; } @@ -302,11 +295,11 @@ bool lcViewSphere::OnMouseMove() if (IsDragging()) { mIntersectionFlags.reset(); - mIsPreview ? mPreview->StartOrbitTracking() : mView->StartOrbitTracking(); + mView->StartOrbitTracking(); return true; } - if (mWidget->IsTracking()) + if (mView->IsTracking()) return false; std::bitset<6> IntersectionFlags = GetIntersectionFlags(mIntersection); @@ -314,7 +307,7 @@ bool lcViewSphere::OnMouseMove() if (IntersectionFlags != mIntersectionFlags) { mIntersectionFlags = IntersectionFlags; - mWidget->Redraw(); + mView->Redraw(); } return mIntersectionFlags.any(); @@ -322,20 +315,20 @@ bool lcViewSphere::OnMouseMove() bool lcViewSphere::IsDragging() const { - int InputStateX = mWidget->GetMouseX(); - int InputStateY = mWidget->GetMouseY(); + int InputStateX = mView->GetMouseX(); + int InputStateY = mView->GetMouseY(); return mMouseDown && (qAbs(mMouseDownX - InputStateX) > 3 || qAbs(mMouseDownY - InputStateY) > 3); } std::bitset<6> lcViewSphere::GetIntersectionFlags(lcVector3& Intersection) const { - const int Width = mWidget->GetWidth(); - const int Height = mWidget->GetHeight(); + const int Width = mView->GetWidth(); + const int Height = mView->GetHeight(); const int ViewportSize = mSize; const int Left = (mLocation == lcViewSphereLocation::BottomLeft || mLocation == lcViewSphereLocation::TopLeft) ? 0 : Width - ViewportSize; const int Bottom = (mLocation == lcViewSphereLocation::BottomLeft || mLocation == lcViewSphereLocation::BottomRight) ? 0 : Height - ViewportSize; - const int x = mWidget->GetMouseX() - Left; - const int y = mWidget->GetMouseY() - Bottom; + const int x = mView->GetMouseX() - Left; + const int y = mView->GetMouseY() - Bottom; std::bitset<6> IntersectionFlags; if (x < 0 || x > Width || y < 0 || y > Height) diff --git a/common/lc_viewsphere.h b/common/lc_viewsphere.h index f139c7e0..9befdf23 100644 --- a/common/lc_viewsphere.h +++ b/common/lc_viewsphere.h @@ -5,7 +5,6 @@ #include class View; -class lcPreviewWidget; enum class lcViewSphereLocation { @@ -19,7 +18,6 @@ class lcViewSphere { public: lcViewSphere(View* View); - lcViewSphere(lcPreviewWidget* Preview); void Draw(); bool OnMouseMove(); @@ -36,10 +34,7 @@ protected: lcMatrix44 GetProjectionMatrix() const; std::bitset<6> GetIntersectionFlags(lcVector3& Intersection) const; - lcGLWidget* const mWidget = nullptr; - lcPreviewWidget* const mPreview = nullptr; View* const mView = nullptr; - bool mIsPreview = false; int mSize = 1; bool mEnabled = false; diff --git a/common/lc_viewwidget.cpp b/common/lc_viewwidget.cpp index e642bea8..1453cdec 100644 --- a/common/lc_viewwidget.cpp +++ b/common/lc_viewwidget.cpp @@ -106,7 +106,7 @@ void lcViewWidget::SetView(lcGLWidget* View) void lcViewWidget::SetPreviewPosition(const QRect& ParentRect) { lcPreferences& Preferences = lcGetPreferences(); - lcPreviewWidget* Preview = reinterpret_cast(mView); + lcPreview* Preview = reinterpret_cast(mView); setWindowTitle(tr("%1 Preview").arg(Preview->IsModel() ? "Submodel" : "Part")); diff --git a/common/view.cpp b/common/view.cpp index e3c31ab3..863a53bb 100644 --- a/common/view.cpp +++ b/common/view.cpp @@ -35,7 +35,7 @@ View::~View() { mContext->DestroyVertexBuffer(mGridBuffer); - if (gMainWindow) + if (gMainWindow && mViewType == lcViewType::View) gMainWindow->RemoveView(this); if (mCamera && mCamera->IsSimple()) @@ -1572,7 +1572,8 @@ void View::DrawGrid() void View::OnInitialUpdate() { - gMainWindow->AddView(this); + if (mViewType == lcViewType::View) + gMainWindow->AddView(this); } lcTrackTool View::GetOverrideTrackTool(Qt::MouseButton Button) const