diff --git a/common/lc_glwidget.h b/common/lc_glwidget.h index 108041e4..7b782fbc 100644 --- a/common/lc_glwidget.h +++ b/common/lc_glwidget.h @@ -108,6 +108,32 @@ public: return mTrackButton != lcTrackButton::None; } + int GetWidth() const + { + return mWidth; + } + + int GetHeight() const + { + return mHeight; + } + + void SetSize(int Width, int Height) + { + mWidth = Width; + mHeight = Height; + } + + QGLWidget* GetWidget() const + { + return mWidget; + } + + void SetWidget(QGLWidget* Widget) + { + mWidget = Widget; + } + int GetMouseX() const { return mMouseX; @@ -164,9 +190,6 @@ public: virtual void BeginDrag(lcDragState DragState) { Q_UNUSED(DragState); } virtual void EndDrag(bool Accept) { Q_UNUSED(Accept); } - int mWidth = 1; - int mHeight = 1; - QGLWidget* mWidget = nullptr; lcContext* mContext = nullptr; signals: @@ -197,6 +220,10 @@ protected: lcMatrix44 mActiveSubmodelTransform; lcCamera* mCamera = nullptr; + + QGLWidget* mWidget = nullptr; + int mWidth = 1; + int mHeight = 1; bool mDeleteContext = true; static lcGLWidget* mLastFocusedView; diff --git a/common/lc_mainwindow.cpp b/common/lc_mainwindow.cpp index e2a25466..5a6e2eda 100644 --- a/common/lc_mainwindow.cpp +++ b/common/lc_mainwindow.cpp @@ -72,7 +72,7 @@ void lcModelTabWidget::ResetLayout() TopWidget->deleteLater(); Widget->setFocus(); - SetActiveView((View*)((lcQGLWidget*)Widget)->mWidget); + SetActiveView((View*)((lcQGLWidget*)Widget)->GetView()); } void lcModelTabWidget::Clear() @@ -84,8 +84,8 @@ void lcModelTabWidget::Clear() mViews.RemoveAll(); mActiveView = nullptr; lcQGLWidget* Widget = (lcQGLWidget*)layout()->itemAt(0)->widget(); - delete Widget->mWidget; - Widget->mWidget = nullptr; + delete Widget->GetView(); + Widget->SetView(nullptr); } lcMainWindow::lcMainWindow() @@ -1372,7 +1372,7 @@ QByteArray lcMainWindow::GetTabLayout() { if (Widget->metaObject() == &lcQGLWidget::staticMetaObject) { - View* CurrentView = (View*)((lcQGLWidget*)Widget)->mWidget; + View* CurrentView = (View*)((lcQGLWidget*)Widget)->GetView(); DataStream << (qint32)0; DataStream << (qint32)(TabWidget->GetActiveView() == CurrentView ? 1 : 0); @@ -1471,7 +1471,7 @@ void lcMainWindow::RestoreTabLayout(const QByteArray& TabLayout) View* CurrentView = nullptr; if (ParentWidget) - CurrentView = (View*)((lcQGLWidget*)ParentWidget)->mWidget; + CurrentView = (View*)((lcQGLWidget*)ParentWidget)->GetView(); if (CameraType == 0) { @@ -1544,7 +1544,7 @@ void lcMainWindow::RestoreTabLayout(const QByteArray& TabLayout) if (ActiveWidget && TabWidget) { - View* ActiveView = (View*)((lcQGLWidget*)ActiveWidget)->mWidget; + View* ActiveView = (View*)((lcQGLWidget*)ActiveWidget)->GetView(); TabWidget->SetActiveView(ActiveView); } } @@ -1621,11 +1621,7 @@ void lcMainWindow::SetCurrentModelTab(lcModel* Model) NewView = CreateView(Model); ViewWidget = (lcQGLWidget*)TabWidget->layout()->itemAt(0)->widget(); - ViewWidget->mWidget = NewView; - NewView->mWidget = ViewWidget; - float Scale = ViewWidget->GetDeviceScale(); - NewView->mWidth = ViewWidget->width() * Scale; - NewView->mHeight = ViewWidget->height() * Scale; + ViewWidget->SetView(NewView); AddView(NewView); mModelTabWidget->setCurrentWidget(TabWidget); @@ -1914,7 +1910,7 @@ void lcMainWindow::RemoveActiveView() } OtherWidget->setFocus(); - SetActiveView((View*)((lcQGLWidget*)OtherWidget)->mWidget); + SetActiveView((View*)((lcQGLWidget*)OtherWidget)->GetView()); } void lcMainWindow::ResetViews() diff --git a/common/lc_viewsphere.cpp b/common/lc_viewsphere.cpp index f2ff15c1..69244be3 100644 --- a/common/lc_viewsphere.cpp +++ b/common/lc_viewsphere.cpp @@ -183,8 +183,8 @@ void lcViewSphere::Draw() return; lcContext* Context = mWidget->mContext; - const int Width = mWidget->mWidth; - const int Height = mWidget->mHeight; + const int Width = mWidget->GetWidth(); + const int Height = mWidget->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; @@ -323,8 +323,8 @@ bool lcViewSphere::IsDragging() const std::bitset<6> lcViewSphere::GetIntersectionFlags(lcVector3& Intersection) const { - const int Width = mWidget->mWidth; - const int Height = mWidget->mHeight; + const int Width = mWidget->GetWidth(); + const int Height = mWidget->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; diff --git a/common/view.cpp b/common/view.cpp index 57f4436a..917aa0a6 100644 --- a/common/view.cpp +++ b/common/view.cpp @@ -310,10 +310,9 @@ lcMatrix44 View::GetTileProjectionMatrix(int CurrentRow, int CurrentColumn, int void View::ShowContextMenu() const { - QGLWidget* Widget = (QGLWidget*)mWidget; QAction** Actions = gMainWindow->mActions; - QMenu* Popup = new QMenu(Widget); + QMenu* Popup = new QMenu(mWidget); Popup->addAction(Actions[LC_EDIT_CUT]); Popup->addAction(Actions[LC_EDIT_COPY]); diff --git a/qt/lc_qaboutdialog.cpp b/qt/lc_qaboutdialog.cpp index afb31f74..1dc56a68 100644 --- a/qt/lc_qaboutdialog.cpp +++ b/qt/lc_qaboutdialog.cpp @@ -17,7 +17,7 @@ lcQAboutDialog::lcQAboutDialog(QWidget *parent) : ui->version->setText(tr("LeoCAD Version %1").arg(QString::fromLatin1(LC_VERSION_TEXT))); #endif - QGLWidget* Widget = (QGLWidget*)gMainWindow->GetActiveView()->mWidget; + QGLWidget* Widget = gMainWindow->GetActiveView()->GetWidget(); QGLFormat Format = Widget->context()->format(); int ColorDepth = Format.redBufferSize() + Format.greenBufferSize() + Format.blueBufferSize() + Format.alphaBufferSize(); diff --git a/qt/lc_qglwidget.cpp b/qt/lc_qglwidget.cpp index e6c0d37b..b6e482d4 100644 --- a/qt/lc_qglwidget.cpp +++ b/qt/lc_qglwidget.cpp @@ -23,21 +23,21 @@ lcQGLWidget::lcQGLWidget(QWidget* Parent, lcGLWidget* Owner) : QGLWidget(Parent, gWidgetList.isEmpty() ? nullptr : gWidgetList.first()) { mWheelAccumulator = 0; - mWidget = Owner; - mWidget->mWidget = this; + mView = Owner; + mView->SetWidget(this); makeCurrent(); if (gWidgetList.isEmpty()) { // TODO: Find a better place for the grid texture and font - gStringCache.Initialize(mWidget->mContext); - gTexFont.Initialize(mWidget->mContext); + gStringCache.Initialize(mView->mContext); + gTexFont.Initialize(mView->mContext); lcInitializeGLExtensions(context()); lcContext::CreateResources(); - View::CreateResources(mWidget->mContext); - lcViewSphere::CreateResources(mWidget->mContext); + View::CreateResources(mView->mContext); + lcViewSphere::CreateResources(mView->mContext); if (!gSupportsShaderObjects && lcGetPreferences().mShadingMode == lcShadingMode::DefaultLights) lcGetPreferences().mShadingMode = lcShadingMode::Flat; @@ -51,7 +51,7 @@ lcQGLWidget::lcQGLWidget(QWidget* Parent, lcGLWidget* Owner) gWidgetList.append(this); - mWidget->OnInitialUpdate(); + mView->OnInitialUpdate(); setMouseTracking(true); @@ -71,16 +71,16 @@ lcQGLWidget::~lcQGLWidget() gStringCache.Reset(); gTexFont.Reset(); - lcGetPiecesLibrary()->ReleaseBuffers(mWidget->mContext); - View::DestroyResources(mWidget->mContext); + lcGetPiecesLibrary()->ReleaseBuffers(mView->mContext); + View::DestroyResources(mView->mContext); lcContext::DestroyResources(); - lcViewSphere::DestroyResources(mWidget->mContext); + lcViewSphere::DestroyResources(mView->mContext); delete gPlaceholderMesh; gPlaceholderMesh = nullptr; } - delete mWidget; + delete mView; } QSize lcQGLWidget::sizeHint() const @@ -88,10 +88,25 @@ QSize lcQGLWidget::sizeHint() const return mPreferredSize.isNull() ? QGLWidget::sizeHint() : mPreferredSize; } +void lcQGLWidget::SetView(lcGLWidget* View) +{ + mView = View; + + if (View) + { + View->SetWidget(this); + const float Scale = GetDeviceScale(); + View->SetSize(width() * Scale, height() * Scale); + + if (hasFocus()) + View->SetFocus(true); + } +} + void lcQGLWidget::SetPreviewPosition(const QRect& ParentRect) { lcPreferences& Preferences = lcGetPreferences(); - lcPreviewWidget* Preview = reinterpret_cast(mWidget); + lcPreviewWidget* Preview = reinterpret_cast(mView); setWindowTitle(tr("%1 Preview").arg(Preview->IsModel() ? "Submodel" : "Part")); @@ -102,10 +117,6 @@ void lcQGLWidget::SetPreviewPosition(const QRect& ParentRect) } mPreferredSize = QSize(Size[0], Size[1]); - float Scale = GetDeviceScale(); - Preview->mWidth = width() * Scale; - Preview->mHeight = height() * Scale; - const QRect desktop = QApplication::desktop()->geometry(); QPoint pos; @@ -141,27 +152,26 @@ void lcQGLWidget::SetPreviewPosition(const QRect& ParentRect) void lcQGLWidget::resizeGL(int Width, int Height) { - mWidget->mWidth = Width; - mWidget->mHeight = Height; + mView->SetSize(Width, Height); } void lcQGLWidget::paintGL() { - mWidget->OnDraw(); + mView->OnDraw(); } void lcQGLWidget::focusInEvent(QFocusEvent* FocusEvent) { - if (mWidget) - mWidget->SetFocus(true); + if (mView) + mView->SetFocus(true); QGLWidget::focusInEvent(FocusEvent); } void lcQGLWidget::focusOutEvent(QFocusEvent* FocusEvent) { - if (mWidget) - mWidget->SetFocus(false); + if (mView) + mView->SetFocus(false); QGLWidget::focusOutEvent(FocusEvent); } @@ -170,8 +180,8 @@ void lcQGLWidget::keyPressEvent(QKeyEvent* KeyEvent) { if (KeyEvent->key() == Qt::Key_Control || KeyEvent->key() == Qt::Key_Shift) { - mWidget->SetMouseModifiers(KeyEvent->modifiers()); - mWidget->UpdateCursor(); + mView->SetMouseModifiers(KeyEvent->modifiers()); + mView->UpdateCursor(); } QGLWidget::keyPressEvent(KeyEvent); @@ -181,8 +191,8 @@ void lcQGLWidget::keyReleaseEvent(QKeyEvent* KeyEvent) { if (KeyEvent->key() == Qt::Key_Control || KeyEvent->key() == Qt::Key_Shift) { - mWidget->SetMouseModifiers(KeyEvent->modifiers()); - mWidget->UpdateCursor(); + mView->SetMouseModifiers(KeyEvent->modifiers()); + mView->UpdateCursor(); } QGLWidget::keyReleaseEvent(KeyEvent); @@ -192,30 +202,30 @@ void lcQGLWidget::mousePressEvent(QMouseEvent* MouseEvent) { float DeviceScale = GetDeviceScale(); - mWidget->SetMousePosition(MouseEvent->x() * DeviceScale, mWidget->mHeight - MouseEvent->y() * DeviceScale - 1); - mWidget->SetMouseModifiers(MouseEvent->modifiers()); + mView->SetMousePosition(MouseEvent->x() * DeviceScale, mView->GetHeight() - MouseEvent->y() * DeviceScale - 1); + mView->SetMouseModifiers(MouseEvent->modifiers()); switch (MouseEvent->button()) { case Qt::LeftButton: - mWidget->OnLeftButtonDown(); + mView->OnLeftButtonDown(); break; case Qt::MidButton: - mWidget->OnMiddleButtonDown(); + mView->OnMiddleButtonDown(); break; case Qt::RightButton: - mWidget->OnRightButtonDown(); + mView->OnRightButtonDown(); break; #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) case Qt::BackButton: - mWidget->OnBackButtonDown(); + mView->OnBackButtonDown(); break; case Qt::ForwardButton: - mWidget->OnForwardButtonDown(); + mView->OnForwardButtonDown(); break; #endif @@ -228,30 +238,30 @@ void lcQGLWidget::mouseReleaseEvent(QMouseEvent* MouseEvent) { float DeviceScale = GetDeviceScale(); - mWidget->SetMousePosition(MouseEvent->x() * DeviceScale, mWidget->mHeight - MouseEvent->y() * DeviceScale - 1); - mWidget->SetMouseModifiers(MouseEvent->modifiers()); + mView->SetMousePosition(MouseEvent->x() * DeviceScale, mView->GetHeight() - MouseEvent->y() * DeviceScale - 1); + mView->SetMouseModifiers(MouseEvent->modifiers()); switch (MouseEvent->button()) { case Qt::LeftButton: - mWidget->OnLeftButtonUp(); + mView->OnLeftButtonUp(); break; case Qt::MidButton: - mWidget->OnMiddleButtonUp(); + mView->OnMiddleButtonUp(); break; case Qt::RightButton: - mWidget->OnRightButtonUp(); + mView->OnRightButtonUp(); break; #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) case Qt::BackButton: - mWidget->OnBackButtonUp(); + mView->OnBackButtonUp(); break; case Qt::ForwardButton: - mWidget->OnForwardButtonUp(); + mView->OnForwardButtonUp(); break; #endif @@ -264,13 +274,13 @@ void lcQGLWidget::mouseDoubleClickEvent(QMouseEvent* MouseEvent) { float DeviceScale = GetDeviceScale(); - mWidget->SetMousePosition(MouseEvent->x() * DeviceScale, mWidget->mHeight - MouseEvent->y() * DeviceScale - 1); - mWidget->SetMouseModifiers(MouseEvent->modifiers()); + mView->SetMousePosition(MouseEvent->x() * DeviceScale, mView->GetHeight() - MouseEvent->y() * DeviceScale - 1); + mView->SetMouseModifiers(MouseEvent->modifiers()); switch (MouseEvent->button()) { case Qt::LeftButton: - mWidget->OnLeftButtonDoubleClick(); + mView->OnLeftButtonDoubleClick(); break; default: break; @@ -281,10 +291,10 @@ void lcQGLWidget::mouseMoveEvent(QMouseEvent* MouseEvent) { float DeviceScale = GetDeviceScale(); - mWidget->SetMousePosition(MouseEvent->x() * DeviceScale, mWidget->mHeight - MouseEvent->y() * DeviceScale - 1); - mWidget->SetMouseModifiers(MouseEvent->modifiers()); + mView->SetMousePosition(MouseEvent->x() * DeviceScale, mView->GetHeight() - MouseEvent->y() * DeviceScale - 1); + mView->SetMouseModifiers(MouseEvent->modifiers()); - mWidget->OnMouseMove(); + mView->OnMouseMove(); } void lcQGLWidget::wheelEvent(QWheelEvent* WheelEvent) @@ -302,11 +312,11 @@ void lcQGLWidget::wheelEvent(QWheelEvent* WheelEvent) float DeviceScale = GetDeviceScale(); #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - mWidget->SetMousePosition(WheelEvent->position().x() * DeviceScale, mWidget->mHeight - WheelEvent->position().y() * DeviceScale - 1); + mView->SetMousePosition(WheelEvent->position().x() * DeviceScale, mView->GetHeight() - WheelEvent->position().y() * DeviceScale - 1); #else - mWidget->SetMousePosition(WheelEvent->x() * DeviceScale, mWidget->mHeight - WheelEvent->y() * DeviceScale - 1); + mView->SetMousePosition(WheelEvent->x() * DeviceScale, mView->mHeight - WheelEvent->y() * DeviceScale - 1); #endif - mWidget->SetMouseModifiers(WheelEvent->modifiers()); + mView->SetMouseModifiers(WheelEvent->modifiers()); #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) mWheelAccumulator += WheelEvent->angleDelta().y() / 8; @@ -317,7 +327,7 @@ void lcQGLWidget::wheelEvent(QWheelEvent* WheelEvent) if (numSteps) { - mWidget->OnMouseWheel(numSteps); + mView->OnMouseWheel(numSteps); mWheelAccumulator -= numSteps * 15; } @@ -331,13 +341,13 @@ void lcQGLWidget::dragEnterEvent(QDragEnterEvent* DragEnterEvent) if (MimeData->hasFormat("application/vnd.leocad-part")) { DragEnterEvent->acceptProposedAction(); - mWidget->BeginDrag(lcDragState::Piece); + mView->BeginDrag(lcDragState::Piece); return; } else if (MimeData->hasFormat("application/vnd.leocad-color")) { DragEnterEvent->acceptProposedAction(); - mWidget->BeginDrag(lcDragState::Color); + mView->BeginDrag(lcDragState::Color); return; } @@ -346,7 +356,7 @@ void lcQGLWidget::dragEnterEvent(QDragEnterEvent* DragEnterEvent) void lcQGLWidget::dragLeaveEvent(QDragLeaveEvent* DragLeaveEvent) { - mWidget->EndDrag(false); + mView->EndDrag(false); DragLeaveEvent->accept(); } @@ -358,10 +368,10 @@ void lcQGLWidget::dragMoveEvent(QDragMoveEvent* DragMoveEvent) { float DeviceScale = GetDeviceScale(); - mWidget->SetMousePosition(DragMoveEvent->pos().x() * DeviceScale, mWidget->mHeight - DragMoveEvent->pos().y() * DeviceScale - 1); - mWidget->SetMouseModifiers(DragMoveEvent->keyboardModifiers()); + mView->SetMousePosition(DragMoveEvent->pos().x() * DeviceScale, mView->GetHeight() - DragMoveEvent->pos().y() * DeviceScale - 1); + mView->SetMouseModifiers(DragMoveEvent->keyboardModifiers()); - mWidget->OnMouseMove(); + mView->OnMouseMove(); DragMoveEvent->accept(); return; @@ -376,7 +386,7 @@ void lcQGLWidget::dropEvent(QDropEvent* DropEvent) if (MimeData->hasFormat("application/vnd.leocad-part") || MimeData->hasFormat("application/vnd.leocad-color")) { - mWidget->EndDrag(true); + mView->EndDrag(true); setFocus(Qt::MouseFocusReason); DropEvent->accept(); diff --git a/qt/lc_qglwidget.h b/qt/lc_qglwidget.h index 9910b70e..ee8595b4 100644 --- a/qt/lc_qglwidget.h +++ b/qt/lc_qglwidget.h @@ -5,13 +5,20 @@ class lcQGLWidget : public QGLWidget { public: - lcQGLWidget(QWidget* Parent, lcGLWidget* Owner); + lcQGLWidget(QWidget* Parent, lcGLWidget* View); ~lcQGLWidget(); + lcGLWidget* GetView() const + { + return mView; + } + + void SetView(lcGLWidget* View); + void SetPreviewPosition(const QRect& ParentRect); + QSize sizeHint() const override; - lcGLWidget* mWidget; - +protected: float GetDeviceScale() const { #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) @@ -19,11 +26,8 @@ public: #else return 1.0f; #endif - } +} - void SetPreviewPosition(const QRect& ParentRect); - -protected: void resizeGL(int Width, int Height) override; void paintGL() override; void focusInEvent(QFocusEvent* FocusEvent) override; @@ -40,6 +44,7 @@ protected: void dragMoveEvent(QDragMoveEvent* DragMoveEvent) override; void dropEvent(QDropEvent* DropEvent) override; + lcGLWidget* mView; QSize mPreferredSize; int mWheelAccumulator; };