leocad/common/lc_previewwidget.cpp

174 lines
4.1 KiB
C++
Raw Normal View History

#include "lc_global.h"
#include "lc_previewwidget.h"
#include "pieceinf.h"
#include "piece.h"
#include "project.h"
#include "lc_model.h"
#include "lc_library.h"
2020-12-18 02:59:11 +01:00
#include "lc_viewwidget.h"
2020-12-25 19:43:22 +01:00
#include "lc_view.h"
lcPreviewDockWidget::lcPreviewDockWidget(QMainWindow* Parent)
: QMainWindow(Parent)
{
2020-12-24 20:32:56 +01:00
mPreview = new lcPreview();
2020-12-18 02:59:11 +01:00
mViewWidget = new lcViewWidget(nullptr, mPreview);
setCentralWidget(mViewWidget);
setMinimumSize(200, 200);
2020-10-07 13:42:43 +02:00
mLockAction = new QAction(QIcon(":/resources/action_preview_unlocked.png"),tr("Lock Preview"), this);
mLockAction->setCheckable(true);
mLockAction->setChecked(false);
mLockAction->setShortcut(tr("Ctrl+L"));
connect(mLockAction, SIGNAL(triggered()), this, SLOT(SetPreviewLock()));
SetPreviewLock();
2020-12-24 20:32:56 +01:00
mLabel = new QLabel();
2020-10-07 13:42:43 +02:00
mToolBar = addToolBar(tr("Toolbar"));
mToolBar->setObjectName("Toolbar");
mToolBar->setStatusTip(tr("Preview Toolbar"));
mToolBar->setMovable(false);
2020-10-07 13:42:43 +02:00
mToolBar->addAction(mLockAction);
mToolBar->addSeparator();
mToolBar->addWidget(mLabel);
if (mToolBar->isHidden())
mToolBar->show();
}
bool lcPreviewDockWidget::SetCurrentPiece(const QString& PartType, int ColorCode)
{
2020-10-07 13:42:43 +02:00
if (mLockAction->isChecked())
return true;
mLabel->setText(tr("Loading..."));
if (mPreview->SetCurrentPiece(PartType, ColorCode))
{
mLabel->setText(mPreview->GetDescription());
return true;
}
return false;
}
void lcPreviewDockWidget::UpdatePreview()
{
mPreview->UpdatePreview();
}
void lcPreviewDockWidget::ClearPreview()
{
2024-05-26 22:01:34 +02:00
if (mPreview->GetModel()->GetPieces().size())
mPreview->ClearPreview();
2020-12-24 20:32:56 +01:00
mLabel->setText(QString());
}
2020-10-07 13:42:43 +02:00
void lcPreviewDockWidget::SetPreviewLock()
{
bool Locked = mLockAction->isChecked();
2020-12-24 20:32:56 +01:00
2024-05-26 22:01:34 +02:00
if (Locked && mPreview->GetModel()->GetPieces().empty())
2020-10-07 13:42:43 +02:00
{
mLockAction->setChecked(false);
return;
}
2020-12-24 20:32:56 +01:00
2020-10-07 13:42:43 +02:00
QIcon LockIcon(Locked ? ":/resources/action_preview_locked.png" : ":/resources/action_preview_unlocked.png");
2022-05-16 21:36:52 +02:00
QString StatusTip(Locked
? tr("Unlock the preview display to enable updates")
: tr("Lock the preview display to disable updates"));
2020-12-24 20:32:56 +01:00
2022-05-16 21:36:52 +02:00
mLockAction->setToolTip(Locked ? tr("Unlock Preview") : tr("Lock Preview"));
2020-10-07 13:42:43 +02:00
mLockAction->setIcon(LockIcon);
mLockAction->setStatusTip(StatusTip);
}
2020-12-24 20:32:56 +01:00
lcPreview::lcPreview()
2020-12-25 19:54:33 +01:00
: lcView(lcViewType::Preview, nullptr), mLoader(new Project(true))
{
mLoader->SetActiveModel(0);
mModel = mLoader->GetActiveModel();
}
2020-12-24 20:32:56 +01:00
bool lcPreview::SetCurrentPiece(const QString& PartType, int ColorCode)
{
lcPiecesLibrary* Library = lcGetPiecesLibrary();
PieceInfo* Info = Library->FindPiece(PartType.toLatin1().constData(), nullptr, false, false);
if (Info)
{
2024-06-20 04:22:18 +02:00
for (const std::unique_ptr<lcPiece>& ModelPiece : mModel->GetPieces())
{
if (Info == ModelPiece->mPieceInfo)
{
2021-01-17 03:27:39 +01:00
int ModelColorCode = ModelPiece->GetColorCode();
if (ModelColorCode == ColorCode)
return true;
}
}
mIsModel = Info->IsModel();
mDescription = Info->m_strDescription;
2020-12-24 20:32:56 +01:00
mModel->SelectAllPieces();
mModel->DeleteSelectedObjects();
Library->LoadPieceInfo(Info, false, true);
Library->WaitForLoadQueue();
2021-01-09 00:11:02 +01:00
mModel->SetPreviewPieceInfo(Info, lcGetColorIndex(ColorCode));
2021-01-09 00:11:02 +01:00
Library->ReleasePieceInfo(Info);
}
else
{
QString ModelPath = QString("%1/%2").arg(QDir::currentPath()).arg(PartType);
if (!mLoader->Load(ModelPath, false))
return false;
mLoader->SetActiveModel(0);
lcGetPiecesLibrary()->RemoveTemporaryPieces();
mModel = mLoader->GetActiveModel();
if (!mModel->GetProperties().mDescription.isEmpty())
mDescription = mModel->GetProperties().mDescription;
else
mDescription = PartType;
mIsModel = true;
}
ZoomExtents();
return true;
}
2020-12-24 20:32:56 +01:00
void lcPreview::ClearPreview()
{
2020-12-24 20:32:56 +01:00
mLoader = std::unique_ptr<Project>(new Project(true/*IsPreview*/));
mLoader->SetActiveModel(0);
mModel = mLoader->GetActiveModel();
lcGetPiecesLibrary()->UnloadUnusedParts();
Redraw();
}
2020-12-24 20:32:56 +01:00
void lcPreview::UpdatePreview()
{
QString PartType;
int ColorCode = -1;
2020-12-24 20:32:56 +01:00
2024-06-20 04:22:18 +02:00
for (const std::unique_ptr<lcPiece>& ModelPiece : mModel->GetPieces())
{
if (ModelPiece->mPieceInfo)
{
PartType = ModelPiece->mPieceInfo->mFileName;
2021-01-17 03:27:39 +01:00
ColorCode = ModelPiece->GetColorCode();
break;
}
}
ClearPreview();
if (!PartType.isEmpty() && ColorCode > -1)
SetCurrentPiece(PartType, ColorCode);
}