LibreCAD
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
addlayerdialog.cpp
Go to the documentation of this file.
1 #include <QtWidgets/QMessageBox>
2 #include "addlayerdialog.h"
3 #include "ui_addlayerdialog.h"
4 
5 AddLayerDialog::AddLayerDialog(lc::Document_SPtr document, QWidget* parent) :
6  AddLayerDialog(nullptr, document, parent) {
7 }
8 
9 AddLayerDialog::AddLayerDialog(lc::Layer_CSPtr oldLayer, lc::Document_SPtr document, QWidget *parent) :
10  QDialog(parent),
11  ui(new Ui::AddLayerDialog),
12  _oldLayer(oldLayer) {
13 
14  ui->setupUi(this);
15 
16  linePatternSelect = new lc::ui::LinePatternSelect(document, this, false, false);
17  lineWidthSelect = new lc::ui::LineWidthSelect(nullptr, this, false, false);
18  colorSelect = new lc::ui::ColorSelect(nullptr, this, false, false);
19 
20  auto layout = dynamic_cast<QFormLayout*>(this->layout());
21  if(layout) {
22  layout->setWidget(1, QFormLayout::FieldRole, colorSelect);
23  layout->setWidget(2, QFormLayout::FieldRole, lineWidthSelect);
24  layout->setWidget(3, QFormLayout::FieldRole, linePatternSelect);
25  }
26 
27  if(oldLayer != nullptr) {
28  ui->name->setText(oldLayer->name().c_str());
29  if(oldLayer->linePattern() != nullptr) {
30  linePatternSelect->setCurrentText(oldLayer->linePattern()->name().c_str());
31  }
32  lineWidthSelect->setWidth(std::make_shared<lc::MetaLineWidthByValue>(oldLayer->lineWidth()));
33  colorSelect->setColor(oldLayer->color());
34 
35  if(oldLayer->linePattern() != nullptr) {
36  int linePatternIndex = linePatternSelect->findText(oldLayer->linePattern()->name().c_str());
37 
38  if (linePatternIndex != -1) {
39  linePatternSelect->setCurrentIndex(linePatternIndex);
40  }
41  }
42  }
43 }
44 
46  delete ui;
47 }
48 
50  if(ui->name->text().isEmpty()) {
51  QToolTip::showText(ui->name->mapToGlobal(QPoint()), tr("Name cannot be empty."));
52  return;
53  }
54 
55  auto linePattern = std::dynamic_pointer_cast<const lc::DxfLinePatternByValue>(linePatternSelect->linePattern());
56  lc::Layer_CSPtr layer;
57 
58  auto lineWidth = std::dynamic_pointer_cast<const lc::MetaLineWidthByValue>(lineWidthSelect->lineWidth());
59  if(lineWidth == nullptr) {
60  return;
61  }
62 
63  if(linePattern == nullptr) {
64  layer = std::make_shared<const lc::Layer>(
65  ui->name->text().toStdString(),
66  *lineWidth,
68  );
69  }
70  else {
71  layer = std::make_shared<const lc::Layer>(
72  ui->name->text().toStdString(),
73  *lineWidth,
74  colorSelect->color(),
75  linePattern,
76  false
77  );
78  }
79 
80  if(_oldLayer == nullptr) {
81  emit newLayer(layer);
82  }
83  else {
84  emit editLayer(_oldLayer, layer);
85  }
86 
87  this->close();
88 }
lc::ui::ColorSelect * colorSelect
void editLayer(lc::Layer_CSPtr oldLayer, lc::Layer_CSPtr newLayer)
lc::Color color()
Returns selected color.
void accept()
Create layer This function is called when "Ok" button is pressed. It creates the layer according to t...
lc::MetaLineWidth_CSPtr lineWidth()
Return selected line width.
Dropdown select for line widths. Line widths are hardcoded in the constructor.
lc::Layer_CSPtr _oldLayer
void setWidth(lc::MetaLineWidth_CSPtr lineWidth)
Select a new width.
Ui::AddLayerDialog * ui
void setColor(lc::Color color)
Set selected color.
Definition: colorselect.cpp:78
AddLayerDialog(lc::Document_SPtr document, QWidget *parent=0)
Create empty dialog.
lc::DxfLinePattern_CSPtr linePattern()
Return selected line pattern.
Dropdown select for DxfLinePatterns.
lc::ui::LinePatternSelect * linePatternSelect
Create a dialog with all required fields to create a new layer.
lc::ui::LineWidthSelect * lineWidthSelect
void newLayer(lc::Layer_CSPtr)