leocad/common/lc_viewwidget.cpp

396 lines
8.8 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"
#include "lc_partselectionwidget.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 "texfont.h"
2018-10-29 01:59:01 +01:00
#include "lc_viewsphere.h"
2018-09-24 04:31:33 +02:00
#include "lc_stringcache.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:37:59 +01:00
lcViewWidget::lcViewWidget(QWidget* Parent, View* View)
2020-11-14 21:47:55 +01:00
: QGLWidget(Parent, gWidgetList.isEmpty() ? nullptr : gWidgetList.first())
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
2020-11-14 19:53:02 +01:00
makeCurrent();
2013-08-09 06:57:18 +02:00
2016-12-28 22:30:31 +01:00
if (gWidgetList.isEmpty())
{
2020-07-11 18:17:43 +02:00
// TODO: Find a better place for the grid texture and font
2020-12-13 21:05:54 +01:00
gStringCache.Initialize(mView->mContext);
gTexFont.Initialize(mView->mContext);
2020-07-11 18:17:43 +02:00
2015-05-09 21:54:29 +02:00
lcInitializeGLExtensions(context());
2015-05-17 01:04:35 +02:00
lcContext::CreateResources();
2020-12-13 21:05:54 +01:00
View::CreateResources(mView->mContext);
lcViewSphere::CreateResources(mView->mContext);
2020-03-22 21:44:20 +01:00
if (!gSupportsShaderObjects && lcGetPreferences().mShadingMode == lcShadingMode::DefaultLights)
lcGetPreferences().mShadingMode = lcShadingMode::Flat;
2020-11-14 21:47:55 +01:00
if (!gSupportsFramebufferObjectARB && !gSupportsFramebufferObjectEXT)
gMainWindow->GetPartSelectionWidget()->DisableIconMode();
gPlaceholderMesh = new lcMesh;
gPlaceholderMesh->CreateBox();
}
2020-07-11 18:17:43 +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
2016-12-28 22:30:31 +01:00
if (gWidgetList.isEmpty())
{
2020-07-11 18:17:43 +02:00
gStringCache.Reset();
gTexFont.Reset();
2020-12-13 21:05:54 +01:00
lcGetPiecesLibrary()->ReleaseBuffers(mView->mContext);
View::DestroyResources(mView->mContext);
2015-05-17 01:04:35 +02:00
lcContext::DestroyResources();
2020-12-13 21:05:54 +01:00
lcViewSphere::DestroyResources(mView->mContext);
delete gPlaceholderMesh;
gPlaceholderMesh = nullptr;
}
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
{
2020-12-22 02:34:42 +01:00
return mPreferredSize.isEmpty() ? QGLWidget::sizeHint() : mPreferredSize;
2013-08-09 06:57:18 +02:00
}
2020-12-25 19:37:59 +01:00
void lcViewWidget::SetView(View* View)
2020-12-13 21:05:54 +01:00
{
mView = View;
if (View)
{
View->SetWidget(this);
const float Scale = GetDeviceScale();
View->SetSize(width() * Scale, height() * Scale);
if (hasFocus())
View->SetFocus(true);
}
}
2020-12-18 02:59:11 +01:00
void lcViewWidget::SetPreviewPosition(const QRect& ParentRect)
{
lcPreferences& Preferences = lcGetPreferences();
2020-12-24 20:32:56 +01:00
lcPreview* Preview = reinterpret_cast<lcPreview*>(mView);
setWindowTitle(tr("%1 Preview").arg(Preview->IsModel() ? "Submodel" : "Part"));
int Size[2] = { 300,200 };
if (Preferences.mPreviewSize == 400)
{
Size[0] = 400; Size[1] = 300;
}
2020-11-14 21:47:55 +01:00
mPreferredSize = QSize(Size[0], Size[1]);
const QRect desktop = QApplication::desktop()->geometry();
QPoint pos;
switch (Preferences.mPreviewLocation)
{
case lcPreviewLocation::TopRight:
pos = mapToGlobal(ParentRect.topRight());
break;
case lcPreviewLocation::TopLeft:
pos = mapToGlobal(ParentRect.topLeft());
break;
case lcPreviewLocation::BottomRight:
pos = mapToGlobal(ParentRect.bottomRight());
break;
default:
pos = mapToGlobal(ParentRect.bottomLeft());
break;
}
2020-12-18 21:15:35 +01:00
if (pos.x() < desktop.left())
pos.setX(desktop.left());
if (pos.y() < desktop.top())
pos.setY(desktop.top());
2020-12-18 21:15:35 +01:00
if ((pos.x() + width()) > desktop.right())
pos.setX(desktop.right() - width());
if ((pos.y() + height()) > desktop.bottom())
pos.setY(desktop.bottom() - height());
move(pos);
setMinimumSize(100,100);
show();
}
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
QGLWidget::focusInEvent(FocusEvent);
}
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
QGLWidget::focusOutEvent(FocusEvent);
}
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
}
2020-11-14 21:47:55 +01:00
QGLWidget::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
}
2020-11-14 21:47:55 +01:00
QGLWidget::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;
2013-08-09 06:57:18 +02:00
case Qt::MidButton:
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;
2015-09-18 07:53:43 +02:00
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
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;
2015-09-18 07:53:43 +02:00
#endif
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;
2013-08-09 06:57:18 +02:00
case Qt::MidButton:
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;
2015-09-18 07:53:43 +02:00
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
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;
2015-09-18 07:53:43 +02:00
#endif
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;
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 (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
if (WheelEvent->angleDelta().y() == 0)
#else
2020-11-14 21:47:55 +01:00
if ((WheelEvent->orientation() & Qt::Vertical) == 0)
2020-12-12 00:49:32 +01:00
#endif
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
2015-02-07 04:08:18 +01:00
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
2020-11-14 21:47:55 +01:00
mWheelAccumulator += WheelEvent->angleDelta().y() / 8;
2015-02-07 04:08:18 +01:00
#else
2020-11-14 21:53:58 +01:00
mWheelAccumulator += WheelEvent->delta() / 8;
2015-02-07 04:08:18 +01:00
#endif
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;
}
QGLWidget::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;
}
QGLWidget::dropEvent(DropEvent);
2013-08-09 06:57:18 +02:00
}