leocad/common/lc_instructions.cpp

257 lines
7.2 KiB
C++
Raw Normal View History

2021-01-06 03:48:12 +01:00
#include "lc_global.h"
#include "lc_instructions.h"
#include "project.h"
#include "lc_model.h"
#include "piece.h"
#include "pieceinf.h"
2021-01-07 19:46:57 +01:00
const float lcInstructions::mDisplayDPI = 72.0f;
2021-01-06 03:48:12 +01:00
lcInstructions::lcInstructions(Project* Project)
: mProject(Project)
{
2021-01-07 19:46:57 +01:00
mPageSetup.Width = 8.5f * mDisplayDPI;
mPageSetup.Height = 11.0f * mDisplayDPI;
mPageSetup.MarginLeft = 0.5 * mDisplayDPI;
mPageSetup.MarginRight = 0.5 * mDisplayDPI;
mPageSetup.MarginTop = 0.5 * mDisplayDPI;
mPageSetup.MarginBottom = 0.5 * mDisplayDPI;
2021-01-06 03:48:12 +01:00
mPageSettings.Rows = 1;
mPageSettings.Columns = 1;
mPageSettings.Direction = lcInstructionsDirection::Horizontal;
2021-01-17 20:05:50 +01:00
mStepProperties[static_cast<int>(lcInstructionsPropertyType::ShowStepNumber)].Value = true;
mStepProperties[static_cast<int>(lcInstructionsPropertyType::ShowStepPLI)].Value = true;
2021-01-16 02:50:15 +01:00
mStepProperties[static_cast<int>(lcInstructionsPropertyType::StepNumberFont)].Value = QFont("Arial", 72).toString();
mStepProperties[static_cast<int>(lcInstructionsPropertyType::StepNumberColor)].Value = LC_RGBA(0, 0, 0, 255);
mStepProperties[static_cast<int>(lcInstructionsPropertyType::StepBackgroundColor)].Value = LC_RGBA(255, 255, 255, 0);
2021-01-16 23:43:24 +01:00
mStepProperties[static_cast<int>(lcInstructionsPropertyType::PLIBackgroundColor)].Value = LC_RGBA(255, 255, 255, 255);
2021-01-17 00:14:05 +01:00
mStepProperties[static_cast<int>(lcInstructionsPropertyType::PLIFont)].Value = QFont("Arial", 16, QFont::Bold).toString();
2021-01-16 23:43:24 +01:00
mStepProperties[static_cast<int>(lcInstructionsPropertyType::PLITextColor)].Value = LC_RGBA(0, 0, 0, 255);
mStepProperties[static_cast<int>(lcInstructionsPropertyType::PLIBorderColor)].Value = LC_RGBA(0, 0, 0, 255);
// mStepProperties[static_cast<int>(lcInstructionsPropertyType::PLIBorderWidth)].Value = 2.0f;
// mStepProperties[static_cast<int>(lcInstructionsPropertyType::PLIBorderRound)].Value = true;
2021-01-16 03:28:41 +01:00
2021-01-17 20:05:50 +01:00
static_assert(static_cast<int>(lcInstructionsPropertyType::Count) == 9, "Missing default property");
2021-01-16 02:50:15 +01:00
CreatePages();
}
2021-01-17 20:05:50 +01:00
QString lcInstructions::GetPropertyLabel(lcInstructionsPropertyType Type)
{
switch (Type)
{
case lcInstructionsPropertyType::ShowStepNumber:
return tr("Show Step Number");
case lcInstructionsPropertyType::ShowStepPLI:
return tr("Show Parts List");
case lcInstructionsPropertyType::StepNumberFont:
return tr("Font:");
case lcInstructionsPropertyType::StepNumberColor:
return tr("Text Color:");
case lcInstructionsPropertyType::StepBackgroundColor:
return tr("Background Color:");
case lcInstructionsPropertyType::PLIBackgroundColor:
return tr("Background Color:");
case lcInstructionsPropertyType::PLIFont:
return tr("Font:");
case lcInstructionsPropertyType::PLITextColor:
return tr("Text Color:");
case lcInstructionsPropertyType::PLIBorderColor:
return tr("Border Color:");
case lcInstructionsPropertyType::Count:
break;
}
return QString();
}
2021-01-16 02:50:15 +01:00
void lcInstructions::SetDefaultPageSettings(const lcInstructionsPageSettings& PageSettings)
{
mPageSettings = PageSettings;
2021-01-06 03:48:12 +01:00
CreatePages();
}
2021-01-17 20:05:50 +01:00
bool lcInstructions::GetBoolProperty(lcInstructionsPropertyType Type, lcModel* Model, lcStep Step) const
{
QVariant Value = GetProperty(Type, Model, Step);
return Value.toBool();
}
2021-01-16 02:50:15 +01:00
QColor lcInstructions::GetColorProperty(lcInstructionsPropertyType Type, lcModel* Model, lcStep Step) const
2021-01-15 03:19:58 +01:00
{
2021-01-16 02:50:15 +01:00
QVariant Value = GetProperty(Type, Model, Step);
return lcQColorFromRGBA(Value.toUInt());
2021-01-16 02:50:15 +01:00
}
QFont lcInstructions::GetFontProperty(lcInstructionsPropertyType Type, lcModel* Model, lcStep Step) const
{
QVariant Value = GetProperty(Type, Model, Step);
QFont Font;
Font.fromString(Value.toString());
return Font;
}
QVariant lcInstructions::GetProperty(lcInstructionsPropertyType Type, lcModel* Model, lcStep Step) const
{
QVariant Value = mStepProperties[static_cast<int>(Type)].Value;
2021-01-15 03:19:58 +01:00
std::map<lcModel*, lcInstructionsModel>::const_iterator ModelIt = mModels.find(Model);
if (ModelIt == mModels.end())
2021-01-16 02:50:15 +01:00
return Value;
2021-01-15 03:19:58 +01:00
const lcInstructionsModel& InstructionModel = ModelIt->second;
for (lcStep StepIndex = 0; StepIndex <= Step; StepIndex++)
{
2021-01-16 02:50:15 +01:00
const lcInstructionsProperties& Properties = InstructionModel.StepProperties[StepIndex];
const lcInstructionsProperty& Property = Properties[static_cast<int>(Type)];
2021-01-15 03:19:58 +01:00
2021-01-16 02:50:15 +01:00
if (Property.Mode == lcInstructionsPropertyMode::NotSet || (Property.Mode == lcInstructionsPropertyMode::StepOnly && StepIndex != Step))
continue;
2021-01-15 03:19:58 +01:00
2021-01-16 02:50:15 +01:00
Value = Property.Value;
}
2021-01-06 03:48:12 +01:00
2021-01-16 02:50:15 +01:00
return Value;
2021-01-06 03:48:12 +01:00
}
2021-01-17 20:05:50 +01:00
void lcInstructions::SetDefaultBool(lcInstructionsPropertyType Type, bool Enabled)
{
SetDefaultProperty(Type, Enabled);
}
2021-01-16 02:50:15 +01:00
void lcInstructions::SetDefaultColor(lcInstructionsPropertyType Type, const QColor& Color)
2021-01-15 03:19:58 +01:00
{
2021-01-16 02:50:15 +01:00
SetDefaultProperty(Type, lcRGBAFromQColor(Color));
2021-01-15 03:19:58 +01:00
}
2021-01-16 02:50:15 +01:00
void lcInstructions::SetDefaultFont(lcInstructionsPropertyType Type, const QFont& Font)
2021-01-16 00:37:28 +01:00
{
2021-01-16 02:50:15 +01:00
SetDefaultProperty(Type, Font.toString());
2021-01-16 00:37:28 +01:00
}
2021-01-16 02:50:15 +01:00
void lcInstructions::SetDefaultProperty(lcInstructionsPropertyType Type, const QVariant& Value)
2021-01-16 00:37:28 +01:00
{
2021-01-16 02:50:15 +01:00
lcInstructionsProperty& Property = mStepProperties[static_cast<int>(Type)];
if (Property.Value == Value)
2021-01-16 00:37:28 +01:00
return;
2021-01-16 02:50:15 +01:00
Property.Value = Value;
2021-01-16 00:37:28 +01:00
emit StepSettingsChanged(nullptr, 0);
2021-01-16 00:37:28 +01:00
}
2021-01-06 03:48:12 +01:00
void lcInstructions::CreatePages()
{
mPages.clear();
if (mProject)
{
std::vector<const lcModel*> AddedModels;
lcModel* Model = mProject->GetMainModel();
if (Model)
AddDefaultPages(Model, AddedModels);
}
}
void lcInstructions::AddDefaultPages(lcModel* Model, std::vector<const lcModel*>& AddedModels)
{
if (std::find(AddedModels.begin(), AddedModels.end(), Model) != AddedModels.end())
return;
AddedModels.push_back(Model);
const lcStep LastStep = Model->GetLastStep();
lcInstructionsPage Page;
int Row = 0, Column = 0;
2021-01-15 03:19:58 +01:00
lcInstructionsModel InstructionModel;
InstructionModel.StepProperties.resize(LastStep + 1);
mModels.emplace(Model, std::move(InstructionModel));
2021-01-06 03:48:12 +01:00
for (lcStep Step = 1; Step <= LastStep; Step++)
{
std::set<lcModel*> StepSubModels;
2024-06-20 04:22:18 +02:00
for (const std::unique_ptr<lcPiece>& Piece : Model->GetPieces())
2021-01-06 03:48:12 +01:00
{
if (!Piece->IsHidden() && Piece->GetStepShow() == Step && Piece->mPieceInfo->IsModel())
{
lcModel* SubModel = Piece->mPieceInfo->GetModel();
if (std::find(AddedModels.begin(), AddedModels.end(), SubModel) == AddedModels.end())
2021-01-06 03:48:12 +01:00
StepSubModels.insert(SubModel);
}
}
if (!StepSubModels.empty())
{
if (!Page.Steps.empty())
{
mPages.emplace_back(std::move(Page));
Row = 0;
Column = 0;
}
for (lcModel* SubModel : StepSubModels)
AddDefaultPages(SubModel, AddedModels);
}
lcInstructionsStep InstructionsStep;
InstructionsStep.Model = Model;
InstructionsStep.Step = Step;
const double Width = 1.0 / (double)mPageSettings.Columns;
const double Height = 1.0 / (double)mPageSettings.Rows;
InstructionsStep.Rect = QRectF(Column * Width, Row * Height, Width, Height);
Page.Steps.push_back(std::move(InstructionsStep));
if (mPageSettings.Direction == lcInstructionsDirection::Horizontal)
{
Column++;
if (Column == mPageSettings.Columns)
{
Row++;
Column = 0;
}
if (Row == mPageSettings.Rows)
{
mPages.emplace_back(std::move(Page));
Row = 0;
Column = 0;
}
}
else
{
Row++;
if (Row == mPageSettings.Rows)
{
Row = 0;
Column++;
}
if (Column == mPageSettings.Columns)
{
mPages.emplace_back(std::move(Page));
Row = 0;
Column = 0;
}
}
}
if (!Page.Steps.empty())
mPages.emplace_back(std::move(Page));
2021-01-06 03:48:12 +01:00
}