diff --git a/common/lc_mainwindow.cpp b/common/lc_mainwindow.cpp index a1fc6b5c..1e7ddf45 100644 --- a/common/lc_mainwindow.cpp +++ b/common/lc_mainwindow.cpp @@ -966,14 +966,11 @@ void lcMainWindow::ProjectFileChanged(const QString& Path) void lcMainWindow::Print(QPrinter* Printer) { #ifndef QT_NO_PRINTER - lcModel* Model = lcGetActiveModel(); int DocCopies; int PageCopies; - int Rows = lcGetProfileInt(LC_PROFILE_PRINT_ROWS); - int Columns = lcGetProfileInt(LC_PROFILE_PRINT_COLUMNS); - int StepsPerPage = Rows * Columns; - int PageCount = (Model->GetLastStep() + StepsPerPage - 1) / StepsPerPage; + std::vector> PageLayouts = lcGetActiveProject()->GetPageLayouts(); + const int PageCount = PageLayouts.size(); if (Printer->collateCopies()) { @@ -1014,22 +1011,10 @@ void lcMainWindow::Print(QPrinter* Printer) Ascending = false; } - GetActiveView()->MakeCurrent(); - lcContext* Context = GetActiveView()->mContext; - QRect PageRect = Printer->pageRect(); - int StepWidth = PageRect.width() / Columns; - int StepHeight = PageRect.height() / Rows; - - View View(Model); - View.SetCamera(GetActiveView()->mCamera, false); - View.SetContext(Context); - View.BeginRenderToImage(StepWidth, StepHeight); - - lcStep PreviousTime = Model->GetCurrentStep(); - QPainter Painter(Printer); + bool FirstPage = true; // TODO: option to print background for (int DocCopy = 0; DocCopy < DocCopies; DocCopy++) @@ -1041,75 +1026,52 @@ void lcMainWindow::Print(QPrinter* Printer) for (int PageCopy = 0; PageCopy < PageCopies; PageCopy++) { if (Printer->printerState() == QPrinter::Aborted || Printer->printerState() == QPrinter::Error) - { - Model->SetTemporaryStep(PreviousTime); - View.EndRenderToImage(); - Context->ClearResources(); return; + + if (!FirstPage) + Printer->newPage(); + else + FirstPage = false; + + int StepWidth = PageRect.width(); + int StepHeight = PageRect.height(); + + lcModel* Model = PageLayouts[Page - 1].first; + lcStep Step = PageLayouts[Page - 1].second; + QImage Image = Model->GetStepImage(false, false, StepWidth, StepHeight, Step); + + QRect Rect = Painter.viewport(); + + Painter.drawImage(Rect.left(), Rect.top(), Image); + + // TODO: add print options somewhere but Qt doesn't allow changes to the page setup dialog +// DWORD dwPrint = theApp.GetProfileInt("Settings","Print", PRINT_NUMBERS|PRINT_BORDER); + +// if (print text) + { + QFont Font("Helvetica", Printer->resolution()); + Painter.setFont(Font); + + QFontMetrics FontMetrics(Font); + + int TextTop = Rect.top() + Printer->resolution() / 2 + FontMetrics.ascent(); + int TextLeft = Rect.left() + Printer->resolution() / 2; + + Painter.drawText(TextLeft, TextTop, QString::number(Step)); } - lcStep CurrentStep = 1 + ((Page - 1) * Rows * Columns); - - for (int Row = 0; Row < Rows; Row++) +// if (print border) { - for (int Column = 0; Column < Columns; Column++) - { - if (CurrentStep > Model->GetLastStep()) - break; + QPen BlackPen(Qt::black, 2); + Painter.setPen(BlackPen); - Model->SetTemporaryStep(CurrentStep); - - View.OnDraw(); - - QRect rect = Painter.viewport(); - int left = rect.x() + (StepWidth * Column); - int bottom = rect.y() + (StepHeight * Row); - - Painter.drawImage(left, bottom, View.GetRenderImage()); - - // TODO: add print options somewhere but Qt doesn't allow changes to the page setup dialog -// DWORD dwPrint = theApp.GetProfileInt("Settings","Print", PRINT_NUMBERS|PRINT_BORDER); - - QRect Rect = Painter.viewport(); - int Left = Rect.x() + (StepWidth * Column); - int Right = Rect.x() + (StepWidth * (Column + 1)); - int Top = Rect.y() + (StepHeight * Row); - int Bottom = Rect.y() + (StepHeight * (Row + 1)); - -// if (print text) - { - QFont Font("Helvetica", Printer->resolution()); - Painter.setFont(Font); - - QFontMetrics FontMetrics(Font); - - int TextTop = Top + Printer->resolution() / 2 + FontMetrics.ascent(); - int TextLeft = Left + Printer->resolution() / 2; - - Painter.drawText(TextLeft, TextTop, QString::number(CurrentStep)); - } - -// if (print border) - { - QPen BlackPen(Qt::black, 2); - Painter.setPen(BlackPen); - - if (Row == 0) - Painter.drawLine(Left, Top, Right, Top); - if (Column == 0) - Painter.drawLine(Left, Top, Left, Bottom); - Painter.drawLine(Left, Bottom, Right, Bottom); - Painter.drawLine(Right, Top, Right, Bottom); - } - - CurrentStep++; - } + Painter.drawLine(Rect.left(), Rect.top(), Rect.right(), Rect.top()); + Painter.drawLine(Rect.left(), Rect.bottom(), Rect.right(), Rect.bottom()); + Painter.drawLine(Rect.left(), Rect.top(), Rect.left(), Rect.bottom()); + Painter.drawLine(Rect.right(), Rect.top(), Rect.right(), Rect.bottom()); } // TODO: print header and footer - - if (PageCopy < PageCopies - 1) - Printer->newPage(); } if (Page == ToPage) @@ -1119,18 +1081,8 @@ void lcMainWindow::Print(QPrinter* Printer) Page++; else Page--; - - Printer->newPage(); } - - if (DocCopy < DocCopies - 1) - Printer->newPage(); } - - Model->SetTemporaryStep(PreviousTime); - - View.EndRenderToImage(); - Context->ClearResources(); #endif } @@ -1183,11 +1135,7 @@ void lcMainWindow::ShowRenderDialog() void lcMainWindow::ShowPrintDialog() { #ifndef QT_NO_PRINTER - lcModel* Model = lcGetActiveModel(); - int Rows = lcGetProfileInt(LC_PROFILE_PRINT_ROWS); - int Columns = lcGetProfileInt(LC_PROFILE_PRINT_COLUMNS); - int StepsPerPage = Rows * Columns; - int PageCount = (Model->GetLastStep() + StepsPerPage - 1) / StepsPerPage; + int PageCount = lcGetActiveProject()->GetPageLayouts().size(); QPrinter Printer(QPrinter::HighResolution); Printer.setFromTo(1, PageCount + 1); @@ -1784,11 +1732,7 @@ void lcMainWindow::TogglePrintPreview() #ifndef QT_NO_PRINTER // todo: print preview inside main window - lcModel* Model = lcGetActiveModel(); - int Rows = lcGetProfileInt(LC_PROFILE_PRINT_ROWS); - int Columns = lcGetProfileInt(LC_PROFILE_PRINT_COLUMNS); - int StepsPerPage = Rows * Columns; - int PageCount = (Model->GetLastStep() + StepsPerPage - 1) / StepsPerPage; + int PageCount = lcGetActiveProject()->GetPageLayouts().size(); QPrinter Printer(QPrinter::ScreenResolution); Printer.setFromTo(1, PageCount + 1); diff --git a/common/lc_model.cpp b/common/lc_model.cpp index 5602de5f..9c312d9c 100644 --- a/common/lc_model.cpp +++ b/common/lc_model.cpp @@ -1353,7 +1353,7 @@ void lcModel::DrawBackground(lcGLWidget* Widget) Context->SetDepthWrite(true); } -void lcModel::SaveStepImages(const QString& BaseName, bool AddStepSuffix, bool Zoom, bool Highlight, int Width, int Height, lcStep Start, lcStep End) +QImage lcModel::GetStepImage(bool Zoom, bool Highlight, int Width, int Height, lcStep Step) { View* ActiveView = gMainWindow->GetActiveView(); ActiveView->MakeCurrent(); @@ -1361,7 +1361,7 @@ void lcModel::SaveStepImages(const QString& BaseName, bool AddStepSuffix, bool Z lcStep CurrentStep = mCurrentStep; - lcCamera* Camera = gMainWindow->GetActiveView()->mCamera; + lcCamera* Camera = ActiveView->mCamera; if (Zoom) ZoomExtents(Camera, (float)Width / (float)Height); @@ -1373,14 +1373,29 @@ void lcModel::SaveStepImages(const QString& BaseName, bool AddStepSuffix, bool Z if (!View.BeginRenderToImage(Width, Height)) { QMessageBox::warning(gMainWindow, tr("LeoCAD"), tr("Error creating images.")); - return; + return QImage(); } + SetTemporaryStep(Step); + View.OnDraw(); + + QImage Image = View.GetRenderImage(); + + View.EndRenderToImage(); + Context->ClearResources(); + + SetTemporaryStep(CurrentStep); + + if (!mActive) + CalculateStep(LC_STEP_MAX); + + return Image; +} + +void lcModel::SaveStepImages(const QString& BaseName, bool AddStepSuffix, bool Zoom, bool Highlight, int Width, int Height, lcStep Start, lcStep End) +{ for (lcStep Step = Start; Step <= End; Step++) { - SetTemporaryStep(Step); - View.OnDraw(); - QString FileName; if (AddStepSuffix) @@ -1393,20 +1408,55 @@ void lcModel::SaveStepImages(const QString& BaseName, bool AddStepSuffix, bool Z if (Writer.format().isEmpty()) Writer.setFormat("png"); - if (!Writer.write(View.GetRenderImage())) + QImage Image = GetStepImage(Zoom, Highlight, Width, Height, Step); + if (!Writer.write(Image)) { QMessageBox::information(gMainWindow, tr("Error"), tr("Error writing to file '%1':\n%2").arg(FileName, Writer.errorString())); break; } } +} - View.EndRenderToImage(); - Context->ClearResources(); +std::vector> lcModel::GetPageLayouts(std::vector& AddedModels) +{ + std::vector> PageLayouts; - SetTemporaryStep(CurrentStep); + if (std::find(AddedModels.begin(), AddedModels.end(), this) != AddedModels.end()) + return PageLayouts; - if (!mActive) - CalculateStep(LC_STEP_MAX); + AddedModels.push_back(this); + + std::map> StepPieces; + + for (lcPiece* Piece : mPieces) + if (!Piece->IsHidden()) + StepPieces[Piece->GetStepShow()].push_back(Piece); + + lcStep CurrentStep = 1; + + for (const std::pair>& StepIt : StepPieces) + { + while (StepIt.first > CurrentStep) + { + PageLayouts.emplace_back(std::make_pair(this, CurrentStep)); + CurrentStep++; + } + + for (lcPiece* Piece : StepIt.second) + { + if (Piece->mPieceInfo->IsModel()) + { + lcModel* SubModel = Piece->mPieceInfo->GetModel(); + std::vector> SubModelLayouts = SubModel->GetPageLayouts(AddedModels); + PageLayouts.insert(PageLayouts.end(), std::make_move_iterator(SubModelLayouts.begin()), std::make_move_iterator(SubModelLayouts.end())); + } + } + + PageLayouts.emplace_back(std::make_pair(this, CurrentStep)); + CurrentStep++; + } + + return PageLayouts; } void lcModel::UpdateBackgroundTexture() diff --git a/common/lc_model.h b/common/lc_model.h index 958039d4..bd1047d8 100644 --- a/common/lc_model.h +++ b/common/lc_model.h @@ -228,7 +228,9 @@ public: void GetScene(lcScene& Scene, lcCamera* ViewCamera, bool Highlight) const; void AddSubModelRenderMeshes(lcScene& Scene, const lcMatrix44& WorldMatrix, int DefaultColorIndex, lcRenderMeshState RenderMeshState, bool ParentActive) const; void DrawBackground(lcGLWidget* Widget); + QImage GetStepImage(bool Zoom, bool Highlight, int Width, int Height, lcStep Step); void SaveStepImages(const QString& BaseName, bool AddStepSuffix, bool Zoom, bool Highlight, int Width, int Height, lcStep Start, lcStep End); + std::vector> GetPageLayouts(std::vector& AddedModels); void RayTest(lcObjectRayTest& ObjectRayTest) const; void BoxTest(lcObjectBoxTest& ObjectBoxTest) const; diff --git a/common/lc_profile.cpp b/common/lc_profile.cpp index d502acc3..f38746e9 100644 --- a/common/lc_profile.cpp +++ b/common/lc_profile.cpp @@ -93,8 +93,6 @@ static lcProfileEntry gProfileEntries[LC_NUM_PROFILE_KEYS] = lcProfileEntry("Settings", "ImageWidth", 1280), // LC_PROFILE_IMAGE_WIDTH lcProfileEntry("Settings", "ImageHeight", 720), // LC_PROFILE_IMAGE_HEIGHT lcProfileEntry("Settings", "ImageExtension", ".png"), // LC_PROFILE_IMAGE_EXTENSION - lcProfileEntry("Settings", "PrintRows", 1), // LC_PROFILE_PRINT_ROWS - lcProfileEntry("Settings", "PrintColumns", 1), // LC_PROFILE_PRINT_COLUMNS lcProfileEntry("Settings", "PartsListIcons", 64), // LC_PROFILE_PARTS_LIST_ICONS lcProfileEntry("Settings", "PartsListNames", 0), // LC_PROFILE_PARTS_LIST_NAMES lcProfileEntry("Settings", "PartsListFixedColor", -1), // LC_PROFILE_PARTS_LIST_FIXED_COLOR diff --git a/common/lc_profile.h b/common/lc_profile.h index ee5e64b2..b5bd3e4c 100644 --- a/common/lc_profile.h +++ b/common/lc_profile.h @@ -41,8 +41,6 @@ enum LC_PROFILE_KEY LC_PROFILE_IMAGE_WIDTH, LC_PROFILE_IMAGE_HEIGHT, LC_PROFILE_IMAGE_EXTENSION, - LC_PROFILE_PRINT_ROWS, - LC_PROFILE_PRINT_COLUMNS, LC_PROFILE_PARTS_LIST_ICONS, LC_PROFILE_PARTS_LIST_NAMES, LC_PROFILE_PARTS_LIST_COLOR, diff --git a/common/project.cpp b/common/project.cpp index a3ac6005..8b8d53e4 100644 --- a/common/project.cpp +++ b/common/project.cpp @@ -1491,6 +1491,16 @@ void Project::ExportCSV() } } +std::vector> Project::GetPageLayouts() const +{ + std::vector AddedModels; + + if (mActiveModel) + return mActiveModel->GetPageLayouts(AddedModels); + + return std::vector>(); +} + QImage Project::CreatePartsListImage(lcModel* Model, lcStep Step) { lcPartsList PartsList; diff --git a/common/project.h b/common/project.h index c6aec5f2..4fb01af2 100644 --- a/common/project.h +++ b/common/project.h @@ -86,6 +86,8 @@ public: QString GetImageFileName(bool AllowCurrentFolder) const; + std::vector> GetPageLayouts() const; + void SetActiveModel(int ModelIndex); void SetActiveModel(const QString& ModelName);