Widget cleanup.

This commit is contained in:
Leonardo Zide 2020-11-14 12:47:55 -08:00
parent 0d155dd971
commit e2fb7b2ed9
10 changed files with 104 additions and 132 deletions

View file

@ -2,6 +2,13 @@
#include "lc_context.h" #include "lc_context.h"
enum class lcDragState
{
None,
Piece,
Color
};
enum class lcCursor enum class lcCursor
{ {
Default, Default,
@ -85,6 +92,9 @@ public:
virtual void OnForwardButtonUp() { } virtual void OnForwardButtonUp() { }
virtual void OnMouseMove() { } virtual void OnMouseMove() { }
virtual void OnMouseWheel(float Direction) { Q_UNUSED(Direction); } virtual void OnMouseWheel(float Direction) { Q_UNUSED(Direction); }
virtual void BeginDrag(lcDragState DragState) { Q_UNUSED(DragState); };
virtual void EndDrag(bool Accept) { Q_UNUSED(Accept); };
lcInputState mInputState; lcInputState mInputState;
int mWidth; int mWidth;

View file

@ -1601,7 +1601,7 @@ void lcMainWindow::SetCurrentModelTab(lcModel* Model)
CentralLayout->setContentsMargins(0, 0, 0, 0); CentralLayout->setContentsMargins(0, 0, 0, 0);
NewView = new View(Model); NewView = new View(Model);
ViewWidget = new lcQGLWidget(TabWidget, NewView, true); ViewWidget = new lcQGLWidget(TabWidget, NewView);
CentralLayout->addWidget(ViewWidget, 0, 0, 1, 1); CentralLayout->addWidget(ViewWidget, 0, 0, 1, 1);
mModelTabWidget->setCurrentWidget(TabWidget); mModelTabWidget->setCurrentWidget(TabWidget);
@ -1824,7 +1824,7 @@ void lcMainWindow::SplitView(Qt::Orientation Orientation)
Splitter = new QSplitter(Orientation, Parent); Splitter = new QSplitter(Orientation, Parent);
Parent->layout()->addWidget(Splitter); Parent->layout()->addWidget(Splitter);
Splitter->addWidget(Focus); Splitter->addWidget(Focus);
Splitter->addWidget(new lcQGLWidget(mModelTabWidget->currentWidget(), new View(GetCurrentTabModel()), true)); Splitter->addWidget(new lcQGLWidget(mModelTabWidget->currentWidget(), new View(GetCurrentTabModel())));
} }
else else
{ {
@ -1835,7 +1835,7 @@ void lcMainWindow::SplitView(Qt::Orientation Orientation)
Splitter = new QSplitter(Orientation, Parent); Splitter = new QSplitter(Orientation, Parent);
ParentSplitter->insertWidget(FocusIndex, Splitter); ParentSplitter->insertWidget(FocusIndex, Splitter);
Splitter->addWidget(Focus); Splitter->addWidget(Focus);
Splitter->addWidget(new lcQGLWidget(mModelTabWidget->currentWidget(), new View(GetCurrentTabModel()), true)); Splitter->addWidget(new lcQGLWidget(mModelTabWidget->currentWidget(), new View(GetCurrentTabModel())));
ParentSplitter->setSizes(Sizes); ParentSplitter->setSizes(Sizes);
} }

View file

@ -712,7 +712,7 @@ void lcPartSelectionListView::PreviewSelection(int InfoIndex)
lcPreviewWidget* Preview = new lcPreviewWidget(); lcPreviewWidget* Preview = new lcPreviewWidget();
lcQGLWidget* ViewWidget = new lcQGLWidget(nullptr, Preview, true/*isView*/, true/*isPreview*/); lcQGLWidget* ViewWidget = new lcQGLWidget(nullptr, Preview);
if (Preview && ViewWidget) if (Preview && ViewWidget)
{ {

View file

@ -15,7 +15,7 @@ lcPreviewDockWidget::lcPreviewDockWidget(QMainWindow* Parent)
: QMainWindow(Parent) : QMainWindow(Parent)
{ {
mPreview = new lcPreviewWidget(); mPreview = new lcPreviewWidget();
mViewWidget = new lcQGLWidget(nullptr, mPreview, true/*IsView*/, true/*IsPreview*/); mViewWidget = new lcQGLWidget(nullptr, mPreview);
setCentralWidget(mViewWidget); setCentralWidget(mViewWidget);
setMinimumSize(200, 200); setMinimumSize(200, 200);

View file

@ -536,7 +536,7 @@ void lcTimelineWidget::PreviewSelection(QTreeWidgetItem* Current)
lcPreviewWidget *Preview = new lcPreviewWidget(); lcPreviewWidget *Preview = new lcPreviewWidget();
lcQGLWidget *ViewWidget = new lcQGLWidget(nullptr, Preview, true/*isView*/, true/*isPreview*/); lcQGLWidget *ViewWidget = new lcQGLWidget(nullptr, Preview);
if (Preview && ViewWidget) if (Preview && ViewWidget)
{ {

View file

@ -49,13 +49,6 @@ enum lcTrackTool
LC_TRACKTOOL_COUNT LC_TRACKTOOL_COUNT
}; };
enum class lcDragState
{
None,
Piece,
Color
};
class View : public lcGLWidget class View : public lcGLWidget
{ {
public: public:
@ -99,6 +92,8 @@ public:
void OnForwardButtonUp() override; void OnForwardButtonUp() override;
void OnMouseMove() override; void OnMouseMove() override;
void OnMouseWheel(float Direction) override; void OnMouseWheel(float Direction) override;
void BeginDrag(lcDragState DragState) override;
void EndDrag(bool Accept) override;
bool IsTracking() const bool IsTracking() const
{ {
@ -107,8 +102,6 @@ public:
void StartOrbitTracking(); void StartOrbitTracking();
void CancelTrackingOrClearSelection(); void CancelTrackingOrClearSelection();
void BeginDrag(lcDragState DragState);
void EndDrag(bool Accept);
void SetProjection(bool Ortho); void SetProjection(bool Ortho);
void LookAt(); void LookAt();

View file

@ -86,11 +86,11 @@ void lcGLWidget::SetCursor(lcCursor CursorType)
} }
} }
lcQGLWidget::lcQGLWidget(QWidget *parent, lcGLWidget *owner, bool IsView, bool IsPreview) lcQGLWidget::lcQGLWidget(QWidget* Parent, lcGLWidget* Owner)
: QGLWidget(parent, gWidgetList.isEmpty() ? nullptr : gWidgetList.first()), mIsPreview(IsPreview) : QGLWidget(Parent, gWidgetList.isEmpty() ? nullptr : gWidgetList.first())
{ {
mWheelAccumulator = 0; mWheelAccumulator = 0;
widget = owner; widget = Owner;
widget->mWidget = this; widget->mWidget = this;
makeCurrent(); makeCurrent();
@ -109,11 +109,8 @@ lcQGLWidget::lcQGLWidget(QWidget *parent, lcGLWidget *owner, bool IsView, bool I
if (!gSupportsShaderObjects && lcGetPreferences().mShadingMode == lcShadingMode::DefaultLights) if (!gSupportsShaderObjects && lcGetPreferences().mShadingMode == lcShadingMode::DefaultLights)
lcGetPreferences().mShadingMode = lcShadingMode::Flat; lcGetPreferences().mShadingMode = lcShadingMode::Flat;
if (!mIsPreview) { if (!gSupportsFramebufferObjectARB && !gSupportsFramebufferObjectEXT)
if (!gSupportsFramebufferObjectARB && !gSupportsFramebufferObjectEXT) { gMainWindow->GetPartSelectionWidget()->DisableIconMode();
gMainWindow->GetPartSelectionWidget()->DisableIconMode();
}
}
gPlaceholderMesh = new lcMesh; gPlaceholderMesh = new lcMesh;
gPlaceholderMesh->CreateBox(); gPlaceholderMesh->CreateBox();
@ -123,15 +120,12 @@ lcQGLWidget::lcQGLWidget(QWidget *parent, lcGLWidget *owner, bool IsView, bool I
widget->OnInitialUpdate(); widget->OnInitialUpdate();
preferredSize = QSize(0, 0);
setMouseTracking(true); setMouseTracking(true);
mIsView = IsView; if (dynamic_cast<View*>(Owner))
if (mIsView)
{ {
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
if (!mIsPreview) setAcceptDrops(true);
setAcceptDrops(true);
} }
} }
@ -145,8 +139,7 @@ lcQGLWidget::~lcQGLWidget()
gTexFont.Reset(); gTexFont.Reset();
lcGetPiecesLibrary()->ReleaseBuffers(widget->mContext); lcGetPiecesLibrary()->ReleaseBuffers(widget->mContext);
if (!mIsPreview) View::DestroyResources(widget->mContext);
View::DestroyResources(widget->mContext);
lcContext::DestroyResources(); lcContext::DestroyResources();
lcViewSphere::DestroyResources(widget->mContext); lcViewSphere::DestroyResources(widget->mContext);
@ -159,10 +152,7 @@ lcQGLWidget::~lcQGLWidget()
QSize lcQGLWidget::sizeHint() const QSize lcQGLWidget::sizeHint() const
{ {
if (preferredSize.isEmpty()) return mPreferredSize.isNull() ? QGLWidget::sizeHint() : mPreferredSize;
return QGLWidget::sizeHint();
else
return preferredSize;
} }
void lcQGLWidget::SetPreviewPosition(const QRect& ParentRect) void lcQGLWidget::SetPreviewPosition(const QRect& ParentRect)
@ -177,7 +167,7 @@ void lcQGLWidget::SetPreviewPosition(const QRect& ParentRect)
{ {
Size[0] = 400; Size[1] = 300; Size[0] = 400; Size[1] = 300;
} }
preferredSize = QSize(Size[0], Size[1]); mPreferredSize = QSize(Size[0], Size[1]);
float Scale = deviceScale(); float Scale = deviceScale();
Preview->mWidth = width() * Scale; Preview->mWidth = width() * Scale;
@ -217,16 +207,10 @@ void lcQGLWidget::SetPreviewPosition(const QRect& ParentRect)
setFocus(); setFocus();
} }
void lcQGLWidget::initializeGL() void lcQGLWidget::resizeGL(int Width, int Height)
{ {
glEnable(GL_DEPTH_TEST); widget->mWidth = Width;
glDepthFunc(GL_LEQUAL); widget->mHeight = Height;
}
void lcQGLWidget::resizeGL(int width, int height)
{
widget->mWidth = width;
widget->mHeight = height;
} }
void lcQGLWidget::paintGL() void lcQGLWidget::paintGL()
@ -234,37 +218,37 @@ void lcQGLWidget::paintGL()
widget->OnDraw(); widget->OnDraw();
} }
void lcQGLWidget::keyPressEvent(QKeyEvent *event) void lcQGLWidget::keyPressEvent(QKeyEvent* KeyEvent)
{ {
if (!mIsPreview && mIsView && (event->key() == Qt::Key_Control || event->key() == Qt::Key_Shift)) if (KeyEvent->key() == Qt::Key_Control || KeyEvent->key() == Qt::Key_Shift)
{ {
widget->mInputState.Modifiers = event->modifiers(); widget->mInputState.Modifiers = KeyEvent->modifiers();
widget->OnUpdateCursor(); widget->OnUpdateCursor();
} }
QGLWidget::keyPressEvent(event); QGLWidget::keyPressEvent(KeyEvent);
} }
void lcQGLWidget::keyReleaseEvent(QKeyEvent *event) void lcQGLWidget::keyReleaseEvent(QKeyEvent* KeyEvent)
{ {
if (!mIsPreview && mIsView && (event->key() == Qt::Key_Control || event->key() == Qt::Key_Shift)) if (KeyEvent->key() == Qt::Key_Control || KeyEvent->key() == Qt::Key_Shift)
{ {
widget->mInputState.Modifiers = event->modifiers(); widget->mInputState.Modifiers = KeyEvent->modifiers();
widget->OnUpdateCursor(); widget->OnUpdateCursor();
} }
QGLWidget::keyReleaseEvent(event); QGLWidget::keyReleaseEvent(KeyEvent);
} }
void lcQGLWidget::mousePressEvent(QMouseEvent *event) void lcQGLWidget::mousePressEvent(QMouseEvent* MouseEvent)
{ {
float scale = deviceScale(); float scale = deviceScale();
widget->mInputState.x = event->x() * scale; widget->mInputState.x = MouseEvent->x() * scale;
widget->mInputState.y = widget->mHeight - event->y() * scale - 1; widget->mInputState.y = widget->mHeight - MouseEvent->y() * scale - 1;
widget->mInputState.Modifiers = event->modifiers(); widget->mInputState.Modifiers = MouseEvent->modifiers();
switch (event->button()) switch (MouseEvent->button())
{ {
case Qt::LeftButton: case Qt::LeftButton:
widget->OnLeftButtonDown(); widget->OnLeftButtonDown();
@ -293,15 +277,15 @@ void lcQGLWidget::mousePressEvent(QMouseEvent *event)
} }
} }
void lcQGLWidget::mouseReleaseEvent(QMouseEvent *event) void lcQGLWidget::mouseReleaseEvent(QMouseEvent* MouseEvent)
{ {
float scale = deviceScale(); float scale = deviceScale();
widget->mInputState.x = event->x() * scale; widget->mInputState.x = MouseEvent->x() * scale;
widget->mInputState.y = widget->mHeight - event->y() * scale - 1; widget->mInputState.y = widget->mHeight - MouseEvent->y() * scale - 1;
widget->mInputState.Modifiers = event->modifiers(); widget->mInputState.Modifiers = MouseEvent->modifiers();
switch (event->button()) switch (MouseEvent->button())
{ {
case Qt::LeftButton: case Qt::LeftButton:
widget->OnLeftButtonUp(); widget->OnLeftButtonUp();
@ -330,15 +314,15 @@ void lcQGLWidget::mouseReleaseEvent(QMouseEvent *event)
} }
} }
void lcQGLWidget::mouseDoubleClickEvent(QMouseEvent *event) void lcQGLWidget::mouseDoubleClickEvent(QMouseEvent* MouseEvent)
{ {
float scale = deviceScale(); float scale = deviceScale();
widget->mInputState.x = event->x() * scale; widget->mInputState.x = MouseEvent->x() * scale;
widget->mInputState.y = widget->mHeight - event->y() * scale - 1; widget->mInputState.y = widget->mHeight - MouseEvent->y() * scale - 1;
widget->mInputState.Modifiers = event->modifiers(); widget->mInputState.Modifiers = MouseEvent->modifiers();
switch (event->button()) switch (MouseEvent->button())
{ {
case Qt::LeftButton: case Qt::LeftButton:
widget->OnLeftButtonDoubleClick(); widget->OnLeftButtonDoubleClick();
@ -348,33 +332,33 @@ void lcQGLWidget::mouseDoubleClickEvent(QMouseEvent *event)
} }
} }
void lcQGLWidget::mouseMoveEvent(QMouseEvent *event) void lcQGLWidget::mouseMoveEvent(QMouseEvent* MouseEvent)
{ {
float scale = deviceScale(); float scale = deviceScale();
widget->mInputState.x = event->x() * scale; widget->mInputState.x = MouseEvent->x() * scale;
widget->mInputState.y = widget->mHeight - event->y() * scale - 1; widget->mInputState.y = widget->mHeight - MouseEvent->y() * scale - 1;
widget->mInputState.Modifiers = event->modifiers(); widget->mInputState.Modifiers = MouseEvent->modifiers();
widget->OnMouseMove(); widget->OnMouseMove();
} }
void lcQGLWidget::wheelEvent(QWheelEvent *event) void lcQGLWidget::wheelEvent(QWheelEvent* WheelEvent)
{ {
if ((event->orientation() & Qt::Vertical) == 0) if ((WheelEvent->orientation() & Qt::Vertical) == 0)
{ {
event->ignore(); WheelEvent->ignore();
return; return;
} }
float scale = deviceScale(); float scale = deviceScale();
widget->mInputState.x = event->x() * scale; widget->mInputState.x = WheelEvent->x() * scale;
widget->mInputState.y = widget->mHeight - event->y() * scale - 1; widget->mInputState.y = widget->mHeight - WheelEvent->y() * scale - 1;
widget->mInputState.Modifiers = event->modifiers(); widget->mInputState.Modifiers = WheelEvent->modifiers();
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
mWheelAccumulator += event->angleDelta().y() / 8; mWheelAccumulator += WheelEvent->angleDelta().y() / 8;
#else #else
mWheelAccumulator += event->delta() / 8; mWheelAccumulator += event->delta() / 8;
#endif #endif
@ -386,60 +370,51 @@ void lcQGLWidget::wheelEvent(QWheelEvent *event)
mWheelAccumulator -= numSteps * 15; mWheelAccumulator -= numSteps * 15;
} }
event->accept(); WheelEvent->accept();
} }
void lcQGLWidget::dragEnterEvent(QDragEnterEvent* DragEnterEvent) void lcQGLWidget::dragEnterEvent(QDragEnterEvent* DragEnterEvent)
{ {
if (!mIsPreview && mIsView) const QMimeData* MimeData = DragEnterEvent->mimeData();
if (MimeData->hasFormat("application/vnd.leocad-part"))
{ {
const QMimeData* MimeData = DragEnterEvent->mimeData(); DragEnterEvent->acceptProposedAction();
widget->BeginDrag(lcDragState::Piece);
if (MimeData->hasFormat("application/vnd.leocad-part")) return;
{
DragEnterEvent->acceptProposedAction();
((View*)widget)->BeginDrag(lcDragState::Piece);
}
else if (MimeData->hasFormat("application/vnd.leocad-color"))
{
DragEnterEvent->acceptProposedAction();
((View*)widget)->BeginDrag(lcDragState::Color);
}
} }
else else if (MimeData->hasFormat("application/vnd.leocad-color"))
DragEnterEvent->ignore(); {
} DragEnterEvent->acceptProposedAction();
widget->BeginDrag(lcDragState::Color);
void lcQGLWidget::dragLeaveEvent(QDragLeaveEvent *event)
{
if (!mIsView || mIsPreview) {
return; return;
} }
((View*)widget)->EndDrag(false); DragEnterEvent->ignore();
}
event->accept(); void lcQGLWidget::dragLeaveEvent(QDragLeaveEvent* DragLeaveEvent)
{
widget->EndDrag(false);
DragLeaveEvent->accept();
} }
void lcQGLWidget::dragMoveEvent(QDragMoveEvent* DragMoveEvent) void lcQGLWidget::dragMoveEvent(QDragMoveEvent* DragMoveEvent)
{ {
if (!mIsPreview && mIsView) const QMimeData* MimeData = DragMoveEvent->mimeData();
if (MimeData->hasFormat("application/vnd.leocad-part") || MimeData->hasFormat("application/vnd.leocad-color"))
{ {
const QMimeData* MimeData = DragMoveEvent->mimeData(); float scale = deviceScale();
if (MimeData->hasFormat("application/vnd.leocad-part") || MimeData->hasFormat("application/vnd.leocad-color")) widget->mInputState.x = DragMoveEvent->pos().x() * scale;
{ widget->mInputState.y = widget->mHeight - DragMoveEvent->pos().y() * scale - 1;
float scale = deviceScale(); widget->mInputState.Modifiers = DragMoveEvent->keyboardModifiers();
widget->mInputState.x = DragMoveEvent->pos().x() * scale; widget->OnMouseMove();
widget->mInputState.y = widget->mHeight - DragMoveEvent->pos().y() * scale - 1;
widget->mInputState.Modifiers = DragMoveEvent->keyboardModifiers();
widget->OnMouseMove(); DragMoveEvent->accept();
return;
DragMoveEvent->accept();
return;
}
} }
QGLWidget::dragMoveEvent(DragMoveEvent); QGLWidget::dragMoveEvent(DragMoveEvent);
@ -447,18 +422,15 @@ void lcQGLWidget::dragMoveEvent(QDragMoveEvent* DragMoveEvent)
void lcQGLWidget::dropEvent(QDropEvent* DropEvent) void lcQGLWidget::dropEvent(QDropEvent* DropEvent)
{ {
if (!mIsPreview && mIsView) const QMimeData* MimeData = DropEvent->mimeData();
if (MimeData->hasFormat("application/vnd.leocad-part") || MimeData->hasFormat("application/vnd.leocad-color"))
{ {
const QMimeData* MimeData = DropEvent->mimeData(); widget->EndDrag(true);
setFocus(Qt::MouseFocusReason);
if (MimeData->hasFormat("application/vnd.leocad-part") || MimeData->hasFormat("application/vnd.leocad-color")) DropEvent->accept();
{ return;
((View*)widget)->EndDrag(true);
setFocus(Qt::MouseFocusReason);
DropEvent->accept();
return;
}
} }
QGLWidget::dropEvent(DropEvent); QGLWidget::dropEvent(DropEvent);

View file

@ -6,15 +6,12 @@ class lcGLWidget;
class lcQGLWidget : public QGLWidget class lcQGLWidget : public QGLWidget
{ {
public: public:
lcQGLWidget(QWidget* Parent, lcGLWidget* Owner, bool IsView, bool IsPreview = false); lcQGLWidget(QWidget* Parent, lcGLWidget* Owner);
~lcQGLWidget(); ~lcQGLWidget();
QSize sizeHint() const override; QSize sizeHint() const override;
lcGLWidget *widget; lcGLWidget* widget;
QSize preferredSize;
bool mIsView;
bool mIsPreview;
float deviceScale() float deviceScale()
{ {
@ -28,7 +25,6 @@ public:
void SetPreviewPosition(const QRect& ParentRect); void SetPreviewPosition(const QRect& ParentRect);
protected: protected:
void initializeGL() override;
void resizeGL(int Width, int Height) override; void resizeGL(int Width, int Height) override;
void paintGL() override; void paintGL() override;
void keyPressEvent(QKeyEvent* KeyEvent) override; void keyPressEvent(QKeyEvent* KeyEvent) override;
@ -43,6 +39,7 @@ protected:
void dragMoveEvent(QDragMoveEvent* DragMoveEvent) override; void dragMoveEvent(QDragMoveEvent* DragMoveEvent) override;
void dropEvent(QDropEvent* DropEvent) override; void dropEvent(QDropEvent* DropEvent) override;
QSize mPreferredSize;
int mWheelAccumulator; int mWheelAccumulator;
}; };

View file

@ -19,7 +19,7 @@ lcQMinifigDialog::lcQMinifigDialog(QWidget *parent) :
mMinifigWidget = new MinifigWizard(); mMinifigWidget = new MinifigWizard();
lcQGLWidget *minifigWidget = new lcQGLWidget(nullptr, mMinifigWidget, false); lcQGLWidget *minifigWidget = new lcQGLWidget(nullptr, mMinifigWidget);
minifigWidget->setMinimumWidth(100); minifigWidget->setMinimumWidth(100);
previewLayout->addWidget(minifigWidget); previewLayout->addWidget(minifigWidget);

View file

@ -1141,7 +1141,7 @@ void lcQPropertiesTree::PreviewSelection(const QString &PartType, int ColorCode)
lcPreviewWidget *Preview = new lcPreviewWidget(); lcPreviewWidget *Preview = new lcPreviewWidget();
lcQGLWidget *ViewWidget = new lcQGLWidget(nullptr, Preview, true/*isView*/, true/*isPreview*/); lcQGLWidget *ViewWidget = new lcQGLWidget(nullptr, Preview);
if (Preview && ViewWidget) if (Preview && ViewWidget)
{ {