leocad/common/lc_instructions.cpp

206 lines
4.9 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;
CreatePages();
}
2021-01-15 03:19:58 +01:00
lcInstructionsStepProperties lcInstructions::GetStepProperties(lcModel* Model, lcStep Step) const
{
lcInstructionsStepProperties StepProperties = mStepProperties;
std::map<lcModel*, lcInstructionsModel>::const_iterator ModelIt = mModels.find(Model);
if (ModelIt == mModels.end())
return StepProperties;
const lcInstructionsModel& InstructionModel = ModelIt->second;
for (lcStep StepIndex = 0; StepIndex <= Step; StepIndex++)
{
const lcInstructionsStepProperties& Properties = InstructionModel.StepProperties[StepIndex];
2021-01-15 23:24:44 +01:00
auto ShouldSetProperty = [StepIndex, Step](lcInstructionsPropertyMode Mode)
{
switch (Mode)
{
case lcInstructionsPropertyMode::NotSet:
return false;
case lcInstructionsPropertyMode::Default:
return true;
case lcInstructionsPropertyMode::Model:
return true;
case lcInstructionsPropertyMode::StepForward:
return true;
case lcInstructionsPropertyMode::StepOnly:
return StepIndex == Step;
}
return false;
};
if (ShouldSetProperty(Properties.BackgroundColorMode))
{
StepProperties.BackgroundColorMode = Properties.BackgroundColorMode;
2021-01-15 03:19:58 +01:00
StepProperties.BackgroundColor = Properties.BackgroundColor;
2021-01-15 23:24:44 +01:00
}
if (ShouldSetProperty(Properties.StepNumberFontMode))
{
StepProperties.StepNumberFontMode = Properties.StepNumberFontMode;
StepProperties.StepNumberFont = Properties.StepNumberFont;
}
if (ShouldSetProperty(Properties.StepNumberColorMode))
{
StepProperties.StepNumberColorMode = Properties.StepNumberColorMode;
StepProperties.StepNumberColor = Properties.StepNumberColor;
}
2021-01-15 03:19:58 +01:00
}
return StepProperties;
}
2021-01-06 03:48:12 +01:00
void lcInstructions::SetDefaultPageSettings(const lcInstructionsPageSettings& PageSettings)
{
mPageSettings = PageSettings;
CreatePages();
}
2021-01-15 03:19:58 +01:00
void lcInstructions::SetDefaultStepBackgroundColor(quint32 Color)
{
if (mStepProperties.BackgroundColor == Color)
return;
mStepProperties.BackgroundColor = Color;
for (size_t PageIndex = 0; PageIndex < mPages.size(); PageIndex++)
emit PageInvalid(static_cast<int>(PageIndex)); // todo: invalidate steps, not pages
}
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;
for (lcPiece* Piece : Model->GetPieces())
{
if (!Piece->IsHidden() && Piece->GetStepShow() == Step && Piece->mPieceInfo->IsModel())
{
lcModel* SubModel = Piece->mPieceInfo->GetModel();
if (std::find(AddedModels.begin(), AddedModels.end(), SubModel) != AddedModels.end())
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;
}
}
}
}