2013-08-09 06:57:18 +02:00
|
|
|
#include "lc_global.h"
|
2020-12-18 02:59:11 +01:00
|
|
|
#include "lc_viewwidget.h"
|
2015-05-09 21:54:29 +02:00
|
|
|
#include "lc_glextensions.h"
|
2013-08-09 06:57:18 +02:00
|
|
|
#include "project.h"
|
|
|
|
#include "lc_library.h"
|
|
|
|
#include "lc_application.h"
|
2013-08-16 03:25:51 +02:00
|
|
|
#include "lc_mainwindow.h"
|
2014-04-14 05:20:16 +02:00
|
|
|
#include "lc_context.h"
|
2020-12-25 19:43:22 +01:00
|
|
|
#include "lc_view.h"
|
2014-08-24 00:56:59 +02:00
|
|
|
#include "lc_texture.h"
|
2015-01-08 05:50:38 +01:00
|
|
|
#include "lc_mesh.h"
|
2016-12-28 22:30:31 +01:00
|
|
|
#include "lc_profile.h"
|
2020-10-07 12:07:32 +02:00
|
|
|
#include "lc_previewwidget.h"
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
static QList<lcViewWidget*> gWidgetList;
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2020-12-25 19:54:33 +01:00
|
|
|
lcViewWidget::lcViewWidget(QWidget* Parent, lcView* View)
|
2021-01-08 20:25:24 +01:00
|
|
|
: QOpenGLWidget(Parent)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2015-02-10 23:34:04 +01:00
|
|
|
mWheelAccumulator = 0;
|
2020-12-20 01:05:29 +01:00
|
|
|
mView = View;
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->SetWidget(this);
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2016-12-28 22:30:31 +01:00
|
|
|
gWidgetList.append(this);
|
2014-08-24 00:56:59 +02:00
|
|
|
|
2013-08-09 06:57:18 +02:00
|
|
|
setMouseTracking(true);
|
|
|
|
|
2020-12-20 01:05:29 +01:00
|
|
|
if (View->GetViewType() == lcViewType::View)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
|
|
|
setFocusPolicy(Qt::StrongFocus);
|
2020-11-14 21:47:55 +01:00
|
|
|
setAcceptDrops(true);
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
lcViewWidget::~lcViewWidget()
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2016-12-28 22:30:31 +01:00
|
|
|
gWidgetList.removeOne(this);
|
2020-07-11 18:17:43 +02:00
|
|
|
|
2020-12-13 21:05:54 +01:00
|
|
|
delete mView;
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
QSize lcViewWidget::sizeHint() const
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2021-01-08 20:25:24 +01:00
|
|
|
return mPreferredSize.isEmpty() ? QOpenGLWidget::sizeHint() : mPreferredSize;
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-25 19:54:33 +01:00
|
|
|
void lcViewWidget::SetView(lcView* View)
|
2020-12-13 21:05:54 +01:00
|
|
|
{
|
|
|
|
if (View)
|
|
|
|
{
|
2020-12-28 21:08:10 +01:00
|
|
|
if (context())
|
2020-12-29 23:42:59 +01:00
|
|
|
{
|
|
|
|
makeCurrent();
|
2021-01-02 03:41:28 +01:00
|
|
|
View->mContext->SetGLContext(context(), this);
|
2020-12-29 23:42:59 +01:00
|
|
|
}
|
|
|
|
|
2020-12-13 21:05:54 +01:00
|
|
|
View->SetWidget(this);
|
|
|
|
const float Scale = GetDeviceScale();
|
|
|
|
View->SetSize(width() * Scale, height() * Scale);
|
|
|
|
|
|
|
|
if (hasFocus())
|
|
|
|
View->SetFocus(true);
|
|
|
|
}
|
2020-12-29 23:42:59 +01:00
|
|
|
|
|
|
|
delete mView;
|
|
|
|
mView = View;
|
2020-12-13 21:05:54 +01:00
|
|
|
}
|
|
|
|
|
2020-12-27 22:05:55 +01:00
|
|
|
void lcViewWidget::initializeGL()
|
|
|
|
{
|
2021-01-02 03:41:28 +01:00
|
|
|
mView->mContext->SetGLContext(context(), this);
|
2020-12-27 22:05:55 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::resizeGL(int Width, int Height)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2021-01-27 02:07:51 +01:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
|
|
|
|
const float Scale = devicePixelRatioF();
|
|
|
|
#else
|
|
|
|
const int Scale = devicePixelRatio();
|
|
|
|
#endif
|
|
|
|
mView->SetSize(Width * Scale, Height * Scale);
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::paintGL()
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnDraw();
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::focusInEvent(QFocusEvent* FocusEvent)
|
2020-12-12 03:01:04 +01:00
|
|
|
{
|
2020-12-13 21:05:54 +01:00
|
|
|
if (mView)
|
|
|
|
mView->SetFocus(true);
|
2020-12-12 03:01:04 +01:00
|
|
|
|
2021-01-08 20:25:24 +01:00
|
|
|
QOpenGLWidget::focusInEvent(FocusEvent);
|
2020-12-12 03:01:04 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::focusOutEvent(QFocusEvent* FocusEvent)
|
2020-12-12 03:01:04 +01:00
|
|
|
{
|
2020-12-13 21:05:54 +01:00
|
|
|
if (mView)
|
|
|
|
mView->SetFocus(false);
|
2020-12-12 03:01:04 +01:00
|
|
|
|
2021-01-08 20:25:24 +01:00
|
|
|
QOpenGLWidget::focusOutEvent(FocusEvent);
|
2020-12-12 03:01:04 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::keyPressEvent(QKeyEvent* KeyEvent)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2020-11-14 21:47:55 +01:00
|
|
|
if (KeyEvent->key() == Qt::Key_Control || KeyEvent->key() == Qt::Key_Shift)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->SetMouseModifiers(KeyEvent->modifiers());
|
|
|
|
mView->UpdateCursor();
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
2021-01-08 20:25:24 +01:00
|
|
|
QOpenGLWidget::keyPressEvent(KeyEvent);
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::keyReleaseEvent(QKeyEvent* KeyEvent)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2020-11-14 21:47:55 +01:00
|
|
|
if (KeyEvent->key() == Qt::Key_Control || KeyEvent->key() == Qt::Key_Shift)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->SetMouseModifiers(KeyEvent->modifiers());
|
|
|
|
mView->UpdateCursor();
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
2021-01-08 20:25:24 +01:00
|
|
|
QOpenGLWidget::keyReleaseEvent(KeyEvent);
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::mousePressEvent(QMouseEvent* MouseEvent)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2020-12-05 17:45:29 +01:00
|
|
|
float DeviceScale = GetDeviceScale();
|
2014-04-06 23:44:58 +02:00
|
|
|
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->SetMousePosition(MouseEvent->x() * DeviceScale, mView->GetHeight() - MouseEvent->y() * DeviceScale - 1);
|
|
|
|
mView->SetMouseModifiers(MouseEvent->modifiers());
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2020-11-14 21:47:55 +01:00
|
|
|
switch (MouseEvent->button())
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
|
|
|
case Qt::LeftButton:
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnLeftButtonDown();
|
2013-08-09 06:57:18 +02:00
|
|
|
break;
|
2015-09-06 21:52:17 +02:00
|
|
|
|
2021-01-14 23:51:43 +01:00
|
|
|
case Qt::MiddleButton:
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnMiddleButtonDown();
|
2013-08-09 06:57:18 +02:00
|
|
|
break;
|
2015-09-06 21:52:17 +02:00
|
|
|
|
2013-08-09 06:57:18 +02:00
|
|
|
case Qt::RightButton:
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnRightButtonDown();
|
2013-08-09 06:57:18 +02:00
|
|
|
break;
|
2015-09-06 21:52:17 +02:00
|
|
|
|
|
|
|
case Qt::BackButton:
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnBackButtonDown();
|
2015-09-06 21:52:17 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Qt::ForwardButton:
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnForwardButtonDown();
|
2015-09-06 21:52:17 +02:00
|
|
|
break;
|
|
|
|
|
2013-08-09 06:57:18 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::mouseReleaseEvent(QMouseEvent* MouseEvent)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2020-12-05 17:45:29 +01:00
|
|
|
float DeviceScale = GetDeviceScale();
|
2014-04-06 23:44:58 +02:00
|
|
|
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->SetMousePosition(MouseEvent->x() * DeviceScale, mView->GetHeight() - MouseEvent->y() * DeviceScale - 1);
|
|
|
|
mView->SetMouseModifiers(MouseEvent->modifiers());
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2020-11-14 21:47:55 +01:00
|
|
|
switch (MouseEvent->button())
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
|
|
|
case Qt::LeftButton:
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnLeftButtonUp();
|
2013-08-09 06:57:18 +02:00
|
|
|
break;
|
2015-09-06 21:52:17 +02:00
|
|
|
|
2021-01-14 23:51:43 +01:00
|
|
|
case Qt::MiddleButton:
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnMiddleButtonUp();
|
2013-08-09 06:57:18 +02:00
|
|
|
break;
|
2015-09-06 21:52:17 +02:00
|
|
|
|
2013-08-09 06:57:18 +02:00
|
|
|
case Qt::RightButton:
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnRightButtonUp();
|
2013-08-09 06:57:18 +02:00
|
|
|
break;
|
2015-09-06 21:52:17 +02:00
|
|
|
|
|
|
|
case Qt::BackButton:
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnBackButtonUp();
|
2015-09-06 21:52:17 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Qt::ForwardButton:
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnForwardButtonUp();
|
2015-09-06 21:52:17 +02:00
|
|
|
break;
|
|
|
|
|
2013-08-09 06:57:18 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::mouseDoubleClickEvent(QMouseEvent* MouseEvent)
|
2014-04-26 08:23:12 +02:00
|
|
|
{
|
2020-12-05 17:45:29 +01:00
|
|
|
float DeviceScale = GetDeviceScale();
|
2014-04-26 08:23:12 +02:00
|
|
|
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->SetMousePosition(MouseEvent->x() * DeviceScale, mView->GetHeight() - MouseEvent->y() * DeviceScale - 1);
|
|
|
|
mView->SetMouseModifiers(MouseEvent->modifiers());
|
2014-04-26 08:23:12 +02:00
|
|
|
|
2020-11-14 21:47:55 +01:00
|
|
|
switch (MouseEvent->button())
|
2014-04-26 08:23:12 +02:00
|
|
|
{
|
|
|
|
case Qt::LeftButton:
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnLeftButtonDoubleClick();
|
2014-04-26 08:23:12 +02:00
|
|
|
break;
|
2021-01-08 20:25:24 +01:00
|
|
|
|
2014-04-26 08:23:12 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::mouseMoveEvent(QMouseEvent* MouseEvent)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2020-12-05 17:45:29 +01:00
|
|
|
float DeviceScale = GetDeviceScale();
|
2014-04-06 23:44:58 +02:00
|
|
|
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->SetMousePosition(MouseEvent->x() * DeviceScale, mView->GetHeight() - MouseEvent->y() * DeviceScale - 1);
|
|
|
|
mView->SetMouseModifiers(MouseEvent->modifiers());
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnMouseMove();
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::wheelEvent(QWheelEvent* WheelEvent)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2020-12-12 00:49:32 +01:00
|
|
|
if (WheelEvent->angleDelta().y() == 0)
|
2015-02-10 23:34:04 +01:00
|
|
|
{
|
2020-11-14 21:47:55 +01:00
|
|
|
WheelEvent->ignore();
|
2015-02-10 23:34:04 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-05 17:45:29 +01:00
|
|
|
float DeviceScale = GetDeviceScale();
|
2014-04-06 23:44:58 +02:00
|
|
|
|
2020-12-12 00:49:32 +01:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->SetMousePosition(WheelEvent->position().x() * DeviceScale, mView->GetHeight() - WheelEvent->position().y() * DeviceScale - 1);
|
2020-12-12 00:49:32 +01:00
|
|
|
#else
|
2020-12-14 01:53:49 +01:00
|
|
|
mView->SetMousePosition(WheelEvent->x() * DeviceScale, mView->GetHeight() - WheelEvent->y() * DeviceScale - 1);
|
2020-12-12 00:49:32 +01:00
|
|
|
#endif
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->SetMouseModifiers(WheelEvent->modifiers());
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2020-11-14 21:47:55 +01:00
|
|
|
mWheelAccumulator += WheelEvent->angleDelta().y() / 8;
|
2015-02-10 23:34:04 +01:00
|
|
|
int numSteps = mWheelAccumulator / 15;
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2015-02-10 23:34:04 +01:00
|
|
|
if (numSteps)
|
|
|
|
{
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnMouseWheel(numSteps);
|
2015-02-10 23:34:04 +01:00
|
|
|
mWheelAccumulator -= numSteps * 15;
|
|
|
|
}
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2020-11-14 21:47:55 +01:00
|
|
|
WheelEvent->accept();
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::dragEnterEvent(QDragEnterEvent* DragEnterEvent)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2020-11-14 21:47:55 +01:00
|
|
|
const QMimeData* MimeData = DragEnterEvent->mimeData();
|
|
|
|
|
|
|
|
if (MimeData->hasFormat("application/vnd.leocad-part"))
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2020-11-14 21:47:55 +01:00
|
|
|
DragEnterEvent->acceptProposedAction();
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->BeginDrag(lcDragState::Piece);
|
2020-11-14 21:47:55 +01:00
|
|
|
return;
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
2020-11-14 21:47:55 +01:00
|
|
|
else if (MimeData->hasFormat("application/vnd.leocad-color"))
|
|
|
|
{
|
|
|
|
DragEnterEvent->acceptProposedAction();
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->BeginDrag(lcDragState::Color);
|
2013-08-09 06:57:18 +02:00
|
|
|
return;
|
2020-10-03 12:02:27 +02:00
|
|
|
}
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2020-11-14 21:47:55 +01:00
|
|
|
DragEnterEvent->ignore();
|
|
|
|
}
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::dragLeaveEvent(QDragLeaveEvent* DragLeaveEvent)
|
2020-11-14 21:47:55 +01:00
|
|
|
{
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->EndDrag(false);
|
2020-11-14 21:47:55 +01:00
|
|
|
DragLeaveEvent->accept();
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::dragMoveEvent(QDragMoveEvent* DragMoveEvent)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2020-11-14 21:47:55 +01:00
|
|
|
const QMimeData* MimeData = DragMoveEvent->mimeData();
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2020-11-14 21:47:55 +01:00
|
|
|
if (MimeData->hasFormat("application/vnd.leocad-part") || MimeData->hasFormat("application/vnd.leocad-color"))
|
|
|
|
{
|
2020-12-05 17:45:29 +01:00
|
|
|
float DeviceScale = GetDeviceScale();
|
2014-04-08 03:04:32 +02:00
|
|
|
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->SetMousePosition(DragMoveEvent->pos().x() * DeviceScale, mView->GetHeight() - DragMoveEvent->pos().y() * DeviceScale - 1);
|
|
|
|
mView->SetMouseModifiers(DragMoveEvent->keyboardModifiers());
|
2014-05-27 00:58:08 +02:00
|
|
|
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->OnMouseMove();
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2020-11-14 21:47:55 +01:00
|
|
|
DragMoveEvent->accept();
|
|
|
|
return;
|
2018-01-12 19:50:25 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 20:25:24 +01:00
|
|
|
QOpenGLWidget::dragMoveEvent(DragMoveEvent);
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 02:59:11 +01:00
|
|
|
void lcViewWidget::dropEvent(QDropEvent* DropEvent)
|
2013-08-09 06:57:18 +02:00
|
|
|
{
|
2020-11-14 21:47:55 +01:00
|
|
|
const QMimeData* MimeData = DropEvent->mimeData();
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2020-11-14 21:47:55 +01:00
|
|
|
if (MimeData->hasFormat("application/vnd.leocad-part") || MimeData->hasFormat("application/vnd.leocad-color"))
|
|
|
|
{
|
2020-12-13 21:05:54 +01:00
|
|
|
mView->EndDrag(true);
|
2020-11-14 21:47:55 +01:00
|
|
|
setFocus(Qt::MouseFocusReason);
|
2013-08-09 06:57:18 +02:00
|
|
|
|
2020-11-14 21:47:55 +01:00
|
|
|
DropEvent->accept();
|
|
|
|
return;
|
2018-01-12 19:50:25 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 20:25:24 +01:00
|
|
|
QOpenGLWidget::dropEvent(DropEvent);
|
2013-08-09 06:57:18 +02:00
|
|
|
}
|