leocad/qt/lc_qglwidget.cpp

437 lines
10 KiB
C++
Raw Normal View History

2013-08-09 06:57:18 +02:00
#include "lc_global.h"
#include "lc_qglwidget.h"
#include "lc_glwidget.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"
2014-05-27 00:58:08 +02:00
#include "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"
2016-12-28 22:30:31 +01:00
static QList<QGLWidget*> gWidgetList;
2013-08-09 06:57:18 +02:00
void lcGLWidget::MakeCurrent()
{
QGLWidget* Widget = (QGLWidget*)mWidget;
Widget->makeCurrent();
}
void lcGLWidget::Redraw()
{
2014-09-15 00:41:22 +02:00
lcQGLWidget* Widget = (lcQGLWidget*)mWidget;
2013-08-09 06:57:18 +02:00
2020-11-14 19:53:02 +01:00
Widget->update();
2013-08-09 06:57:18 +02:00
}
2020-02-15 20:36:06 +01:00
void lcGLWidget::SetCursor(lcCursor CursorType)
2013-08-09 06:57:18 +02:00
{
2020-02-15 20:36:06 +01:00
if (mCursor == CursorType)
2013-08-09 06:57:18 +02:00
return;
struct lcCursorInfo
{
int x, y;
const char* Name;
};
2020-02-15 20:36:06 +01:00
const lcCursorInfo Cursors[] =
2013-08-09 06:57:18 +02:00
{
2020-02-15 20:36:06 +01:00
{ 0, 0, "" }, // lcCursor::Default
{ 8, 3, ":/resources/cursor_insert" }, // lcCursor::Brick
{ 15, 15, ":/resources/cursor_light" }, // lcCursor::Light
{ 7, 10, ":/resources/cursor_spotlight" }, // lcCursor::Spotlight
{ 15, 9, ":/resources/cursor_camera" }, // lcCursor::Camera
{ 0, 2, ":/resources/cursor_select" }, // lcCursor::Select
{ 0, 2, ":/resources/cursor_select_add" }, // lcCursor::SelectAdd
{ 0, 2, ":/resources/cursor_select_remove" }, // lcCursor::SelectRemove
{ 15, 15, ":/resources/cursor_move" }, // lcCursor::Move
{ 15, 15, ":/resources/cursor_rotate" }, // lcCursor::Rotate
{ 15, 15, ":/resources/cursor_rotatex" }, // lcCursor::RotateX
{ 15, 15, ":/resources/cursor_rotatey" }, // lcCursor::RotateY
{ 0, 10, ":/resources/cursor_delete" }, // lcCursor::Delete
{ 14, 14, ":/resources/cursor_paint" }, // lcCursor::Paint
{ 1, 13, ":/resources/cursor_color_picker" }, // lcCursor::ColorPicker
{ 15, 15, ":/resources/cursor_zoom" }, // lcCursor::Zoom
{ 9, 9, ":/resources/cursor_zoom_region" }, // lcCursor::ZoomRegion
{ 15, 15, ":/resources/cursor_pan" }, // lcCursor::Pan
{ 15, 15, ":/resources/cursor_roll" }, // lcCursor::Roll
{ 15, 15, ":/resources/cursor_rotate_view" }, // lcCursor::RotateView
2013-08-09 06:57:18 +02:00
};
2020-02-15 20:36:06 +01:00
static_assert(LC_ARRAY_COUNT(Cursors) == static_cast<int>(lcCursor::Count), "Array size mismatch");
2013-08-09 06:57:18 +02:00
QGLWidget* widget = (QGLWidget*)mWidget;
2020-02-15 20:36:06 +01:00
if (CursorType != lcCursor::Default && CursorType < lcCursor::Count)
2013-08-09 06:57:18 +02:00
{
2020-02-15 20:36:06 +01:00
const lcCursorInfo& Cursor = Cursors[static_cast<int>(CursorType)];
2013-08-09 06:57:18 +02:00
widget->setCursor(QCursor(QPixmap(Cursor.Name), Cursor.x, Cursor.y));
2020-02-15 20:36:06 +01:00
mCursor = CursorType;
2013-08-09 06:57:18 +02:00
}
else
{
widget->unsetCursor();
2020-02-15 20:36:06 +01:00
mCursor = lcCursor::Default;
2013-08-09 06:57:18 +02:00
}
}
2020-11-14 21:47:55 +01:00
lcQGLWidget::lcQGLWidget(QWidget* Parent, lcGLWidget* Owner)
: 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;
2020-11-14 21:47:55 +01:00
widget = Owner;
2013-08-09 06:57:18 +02:00
widget->mWidget = this;
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
gStringCache.Initialize(widget->mContext);
gTexFont.Initialize(widget->mContext);
2015-05-09 21:54:29 +02:00
lcInitializeGLExtensions(context());
2015-05-17 01:04:35 +02:00
lcContext::CreateResources();
View::CreateResources(widget->mContext);
lcViewSphere::CreateResources(widget->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);
2015-05-09 21:54:29 +02:00
widget->OnInitialUpdate();
2013-08-09 06:57:18 +02:00
setMouseTracking(true);
2020-11-14 21:47:55 +01:00
if (dynamic_cast<View*>(Owner))
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
}
}
lcQGLWidget::~lcQGLWidget()
{
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();
2016-12-28 22:30:31 +01:00
lcGetPiecesLibrary()->ReleaseBuffers(widget->mContext);
2020-11-14 21:47:55 +01:00
View::DestroyResources(widget->mContext);
2015-05-17 01:04:35 +02:00
lcContext::DestroyResources();
lcViewSphere::DestroyResources(widget->mContext);
delete gPlaceholderMesh;
gPlaceholderMesh = nullptr;
}
2016-03-06 02:47:00 +01:00
delete widget;
2013-08-09 06:57:18 +02:00
}
QSize lcQGLWidget::sizeHint() const
{
2020-11-14 21:47:55 +01:00
return mPreferredSize.isNull() ? QGLWidget::sizeHint() : mPreferredSize;
2013-08-09 06:57:18 +02:00
}
void lcQGLWidget::SetPreviewPosition(const QRect& ParentRect)
{
lcPreferences& Preferences = lcGetPreferences();
lcPreviewWidget* Preview = reinterpret_cast<lcPreviewWidget*>(widget);
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]);
float Scale = deviceScale();
Preview->mWidth = width() * Scale;
Preview->mHeight = height() * Scale;
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;
}
if (pos.x() < desktop.left())
pos.setX(desktop.left());
if (pos.y() < desktop.top())
pos.setY(desktop.top());
if ((pos.x() + width()) > desktop.width())
pos.setX(desktop.width() - width());
if ((pos.y() + height()) > desktop.bottom())
pos.setY(desktop.bottom() - height());
move(pos);
setMinimumSize(100,100);
show();
setFocus();
}
2020-11-14 21:47:55 +01:00
void lcQGLWidget::resizeGL(int Width, int Height)
2013-08-09 06:57:18 +02:00
{
2020-11-14 21:47:55 +01:00
widget->mWidth = Width;
widget->mHeight = Height;
2013-08-09 06:57:18 +02:00
}
void lcQGLWidget::paintGL()
{
widget->OnDraw();
}
2020-11-14 21:47:55 +01:00
void lcQGLWidget::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-11-14 21:47:55 +01:00
widget->mInputState.Modifiers = KeyEvent->modifiers();
2013-08-09 06:57:18 +02:00
widget->OnUpdateCursor();
}
2020-11-14 21:47:55 +01:00
QGLWidget::keyPressEvent(KeyEvent);
2013-08-09 06:57:18 +02:00
}
2020-11-14 21:47:55 +01:00
void lcQGLWidget::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-11-14 21:47:55 +01:00
widget->mInputState.Modifiers = KeyEvent->modifiers();
2013-08-09 06:57:18 +02:00
widget->OnUpdateCursor();
}
2020-11-14 21:47:55 +01:00
QGLWidget::keyReleaseEvent(KeyEvent);
2013-08-09 06:57:18 +02:00
}
2020-11-14 21:47:55 +01:00
void lcQGLWidget::mousePressEvent(QMouseEvent* MouseEvent)
2013-08-09 06:57:18 +02:00
{
2014-04-08 03:04:32 +02:00
float scale = deviceScale();
2014-04-06 23:44:58 +02:00
2020-11-14 21:47:55 +01:00
widget->mInputState.x = MouseEvent->x() * scale;
widget->mInputState.y = widget->mHeight - MouseEvent->y() * scale - 1;
widget->mInputState.Modifiers = 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:
widget->OnLeftButtonDown();
break;
2013-08-09 06:57:18 +02:00
case Qt::MidButton:
widget->OnMiddleButtonDown();
break;
2013-08-09 06:57:18 +02:00
case Qt::RightButton:
widget->OnRightButtonDown();
break;
2015-09-18 07:53:43 +02:00
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
case Qt::BackButton:
widget->OnBackButtonDown();
break;
case Qt::ForwardButton:
widget->OnForwardButtonDown();
break;
2015-09-18 07:53:43 +02:00
#endif
2013-08-09 06:57:18 +02:00
default:
break;
}
}
2020-11-14 21:47:55 +01:00
void lcQGLWidget::mouseReleaseEvent(QMouseEvent* MouseEvent)
2013-08-09 06:57:18 +02:00
{
2014-04-08 03:04:32 +02:00
float scale = deviceScale();
2014-04-06 23:44:58 +02:00
2020-11-14 21:47:55 +01:00
widget->mInputState.x = MouseEvent->x() * scale;
widget->mInputState.y = widget->mHeight - MouseEvent->y() * scale - 1;
widget->mInputState.Modifiers = 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:
widget->OnLeftButtonUp();
break;
2013-08-09 06:57:18 +02:00
case Qt::MidButton:
widget->OnMiddleButtonUp();
break;
2013-08-09 06:57:18 +02:00
case Qt::RightButton:
widget->OnRightButtonUp();
break;
2015-09-18 07:53:43 +02:00
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
case Qt::BackButton:
widget->OnBackButtonUp();
break;
case Qt::ForwardButton:
widget->OnForwardButtonUp();
break;
2015-09-18 07:53:43 +02:00
#endif
2013-08-09 06:57:18 +02:00
default:
break;
}
}
2020-11-14 21:47:55 +01:00
void lcQGLWidget::mouseDoubleClickEvent(QMouseEvent* MouseEvent)
2014-04-26 08:23:12 +02:00
{
float scale = deviceScale();
2020-11-14 21:47:55 +01:00
widget->mInputState.x = MouseEvent->x() * scale;
widget->mInputState.y = widget->mHeight - MouseEvent->y() * scale - 1;
widget->mInputState.Modifiers = 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:
widget->OnLeftButtonDoubleClick();
break;
default:
break;
}
}
2020-11-14 21:47:55 +01:00
void lcQGLWidget::mouseMoveEvent(QMouseEvent* MouseEvent)
2013-08-09 06:57:18 +02:00
{
2014-04-08 03:04:32 +02:00
float scale = deviceScale();
2014-04-06 23:44:58 +02:00
2020-11-14 21:47:55 +01:00
widget->mInputState.x = MouseEvent->x() * scale;
widget->mInputState.y = widget->mHeight - MouseEvent->y() * scale - 1;
widget->mInputState.Modifiers = MouseEvent->modifiers();
2013-08-09 06:57:18 +02:00
widget->OnMouseMove();
}
2020-11-14 21:47:55 +01:00
void lcQGLWidget::wheelEvent(QWheelEvent* WheelEvent)
2013-08-09 06:57:18 +02:00
{
2020-11-14 21:47:55 +01:00
if ((WheelEvent->orientation() & Qt::Vertical) == 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;
}
2014-04-08 03:04:32 +02:00
float scale = deviceScale();
2014-04-06 23:44:58 +02:00
2020-11-14 21:47:55 +01:00
widget->mInputState.x = WheelEvent->x() * scale;
widget->mInputState.y = widget->mHeight - WheelEvent->y() * scale - 1;
widget->mInputState.Modifiers = 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)
{
widget->OnMouseWheel(numSteps);
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
}
void lcQGLWidget::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();
widget->BeginDrag(lcDragState::Piece);
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();
widget->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-11-14 21:47:55 +01:00
void lcQGLWidget::dragLeaveEvent(QDragLeaveEvent* DragLeaveEvent)
{
widget->EndDrag(false);
DragLeaveEvent->accept();
2013-08-09 06:57:18 +02:00
}
void lcQGLWidget::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"))
{
float scale = deviceScale();
2014-04-08 03:04:32 +02:00
2020-11-14 21:47:55 +01:00
widget->mInputState.x = DragMoveEvent->pos().x() * scale;
widget->mInputState.y = widget->mHeight - DragMoveEvent->pos().y() * scale - 1;
widget->mInputState.Modifiers = DragMoveEvent->keyboardModifiers();
2014-05-27 00:58:08 +02:00
2020-11-14 21:47:55 +01:00
widget->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
}
void lcQGLWidget::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"))
{
widget->EndDrag(true);
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
}