LibreCAD
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
addlinepatterndialog.cpp
Go to the documentation of this file.
1 #include "addlinepatterndialog.h"
2 #include "ui_addlinepatterndialog.h"
3 
4 AddLinePatternDialog::AddLinePatternDialog(lc::Document_SPtr document, QWidget *parent) :
5  AddLinePatternDialog(document, nullptr, parent) {
6 }
7 
8 AddLinePatternDialog::AddLinePatternDialog(lc::Document_SPtr document, lc::DxfLinePatternByValue_CSPtr linePattern, QWidget *parent) :
9  QDialog(parent),
10  ui(new Ui::AddLinePatternDialog),
11  _document(document),
12  _oldLinePattern(linePattern) {
13 
14  ui->setupUi(this);
15 
16  _layout = static_cast<QVBoxLayout*>(ui->pathList->layout());
17  if(!_layout) {
18  throw "Unable to cast AddLinePatternDialog pathList layout to QVBoxLayout";
19  }
20 
21  if(linePattern != nullptr) {
22  ui->name->setText(linePattern->name().c_str());
23  ui->description->setText(linePattern->description().c_str());
24 
25  for (auto value : linePattern->path()) {
26  auto pathPart = new LinePatternPathPart(value, this);
28 
29  _layout->insertWidget(_layout->count() - 1, pathPart);
30  }
31 
32  ui->saveButton->setText(SAVE_AS_NEW_TEXT);
33 
34  editButton = new QPushButton(EDIT_TEXT);
35  connect(editButton, &QPushButton::pressed, this, &AddLinePatternDialog::onEditButtonPressed);
36  ui->buttonLayout->addWidget(editButton);
37  }
38 }
39 
41  std::string name = ui->name->text().toStdString();
42  if(name.empty()) {
43  name = "tmp";
44  }
45 
46  std::vector<double> path;
47  double length = 0.0;
48 
49  auto parts = findChildren<LinePatternPathPart*>();
50 
51  for(auto part : parts) {
52  auto type = part->type();
53  auto value = part->value();
54 
55  switch(type) {
57  path.push_back(0);
58  break;
59 
61  path.push_back(value);
62  length += value;
63  break;
64 
66  path.push_back(-value);
67  length += value;
68  break;
69  }
70  }
71 
72  _linePattern = std::make_shared<lc::DxfLinePatternByValue>(
73  name,
74  ui->description->text().toStdString(),
75  path,
76  length);
77 
78  QPixmap previewPixmap(ui->preview->frameSize());
79 
80  LinePatternPainter painter(&previewPixmap, _linePattern);
81  painter.drawLinePattern();
82 
83  ui->preview->setPixmap(previewPixmap);
84 }
85 
86 
88  auto pathPart = new LinePatternPathPart(this);
90 
91  _layout->insertWidget(_layout->count() - 1, pathPart);
92 }
93 
95  this->close();
96 }
97 
99  auto operation = std::make_shared<lc::operation::AddLinePattern>(_document, _linePattern);
100  operation->execute();
101 
102  this->close();
103 }
104 
106  auto operation = std::make_shared<lc::operation::ReplaceLinePattern>(_document, _oldLinePattern, _linePattern);
107  operation->execute();
108 
109  this->close();
110 }
Model used to construct line pattern path.
void on_saveButton_pressed()
Save Save the line pattern in the document and close the dialog.
void onEditButtonPressed()
Edit Replace the original line pattern with the new one and close the dialog.
lc::DxfLinePatternByValue_CSPtr _linePattern
AddLinePatternDialog(lc::Document_SPtr document, QWidget *parent=0)
Create empty dialog.
void on_cancelButton_pressed()
Cancel Close the dialog.
#define EDIT_TEXT
void on_newValueButton_pressed()
Create a new entry for the pattern path.
void generatePreview()
Generate line pattern preview Create a new QPixmap with a preview of the line pattern and add it in t...
#define SAVE_AS_NEW_TEXT
lc::DxfLinePatternByValue_CSPtr _oldLinePattern
lc::Document_SPtr _document
Create a dialog with all required fields to create a new line pattern.
Painting line patterns on a QPaintDevice.
Ui::AddLinePatternDialog * ui