leocad/common/lc_viewwidget.cpp

308 lines
6.4 KiB
C++
Raw Normal View History

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"
#include "lc_texture.h"
#include "lc_mesh.h"
2016-12-28 22:30:31 +01:00
#include "lc_profile.h"
#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;
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);
2013-08-09 06:57:18 +02:00
setMouseTracking(true);
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();
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()
{
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
{
2020-12-13 21:05:54 +01:00
mView->SetSize(Width, Height);
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;
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;
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;
case Qt::BackButton:
2020-12-13 21:05:54 +01:00
mView->OnBackButtonDown();
break;
case Qt::ForwardButton:
2020-12-13 21:05:54 +01:00
mView->OnForwardButtonDown();
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;
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;
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;
case Qt::BackButton:
2020-12-13 21:05:54 +01:00
mView->OnBackButtonUp();
break;
case Qt::ForwardButton:
2020-12-13 21:05:54 +01:00
mView->OnForwardButtonUp();
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;
}
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;
}
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;
}
2021-01-08 20:25:24 +01:00
QOpenGLWidget::dropEvent(DropEvent);
2013-08-09 06:57:18 +02:00
}