]> Shamusworld >> Repos - architektonas/blob - src/forms/dlghatch.cpp
Refactoring: Moved RS_GraphicView to GraphicView.
[architektonas] / src / forms / dlghatch.cpp
1 // dlghatch.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  06/11/2010  Created this file. :-)
13 //
14
15 #include "dlghatch.h"
16
17 #include "rs_entitycontainer.h"
18 #include "rs_hatch.h"
19 #include "rs_pattern.h"
20 #include "rs_patternlist.h"
21 #include "settings.h"
22
23 DlgHatch::DlgHatch(QWidget * parent/*= NULL*/, Qt::WindowFlags flags/*= 0*/):
24         QDialog(parent, flags)
25 {
26         ui.setupUi(this);
27
28         pattern = NULL;
29         hatch = NULL;
30         isNew = false;
31
32         preview = new RS_EntityContainer();
33         ui.gvPreview->setContainer(preview);
34         ui.gvPreview->setBorders(15, 15, 15, 15);
35
36         ui.cbPattern->init();
37 }
38
39 DlgHatch::~DlgHatch()
40 {
41         if (isNew)
42         {
43                 settings.beginGroup("Draw");
44                 settings.setValue("HatchSolid", ui.cbSolid->isChecked());
45                 settings.setValue("HatchPattern", ui.cbPattern->currentText());
46                 settings.setValue("HatchScale", ui.leScale->text());
47                 settings.setValue("HatchAngle", ui.leAngle->text());
48                 settings.setValue("HatchPreview", ui.cbEnablePreview->isChecked());
49                 settings.endGroup();
50         }
51
52         delete preview;
53 }
54
55 void DlgHatch::polish()
56 {
57 //WTF is this supposed to be for???
58 //      QDialog::polish();
59         ui.gvPreview->zoomAuto();
60 }
61
62 void DlgHatch::showEvent(QShowEvent * e)
63 {
64         QDialog::showEvent(e);
65         ui.gvPreview->zoomAuto();
66 }
67
68 void DlgHatch::setHatch(RS_Hatch & h, bool isNew)
69 {
70         hatch = &h;
71         this->isNew = isNew;
72
73         settings.beginGroup("Draw");
74         bool enablePrev = settings.value("HatchPreview", false).toBool();
75         settings.endGroup();
76
77         ui.cbEnablePreview->setChecked(enablePrev);
78
79         // read defaults from config file:
80         if (isNew)
81         {
82                 settings.beginGroup("Draw");
83                 bool solid = settings.value("HatchSolid", false).toBool();
84                 QString pat = settings.value("HatchPattern", "ANSI31").toString();
85                 QString scale = settings.value("HatchScale", "1.0").toString();
86                 QString angle = settings.value("HatchAngle", "0.0").toString();
87                 settings.endGroup();
88
89                 ui.cbSolid->setChecked(solid);
90                 setPattern(pat);
91                 ui.leScale->setText(scale);
92                 ui.leAngle->setText(angle);
93         }
94         // initialize dialog based on given hatch:
95         else
96         {
97                 ui.cbSolid->setChecked(hatch->isSolid());
98                 setPattern(hatch->getPattern());
99                 QString s;
100                 s.setNum(hatch->getScale());
101                 ui.leScale->setText(s);
102                 s.setNum(RS_Math::rad2deg(hatch->getAngle()));
103                 ui.leAngle->setText(s);
104         }
105 }
106
107 void DlgHatch::updateHatch()
108 {
109         if (!hatch)
110                 return;
111
112         hatch->setSolid(ui.cbSolid->isChecked());
113         hatch->setPattern(ui.cbPattern->currentText());
114         hatch->setScale(RS_Math::eval(ui.leScale->text()));
115         hatch->setAngle(RS_Math::deg2rad(RS_Math::eval(ui.leAngle->text())));
116 }
117
118 void DlgHatch::setPattern(const QString & p)
119 {
120         if (!RS_PATTERNLIST->contains(p))
121 //              ui.cbPattern->insertItem(p);
122                 ui.cbPattern->addItem(p);
123
124 //      ui.cbPattern->setCurrentText(p);
125         ui.cbPattern->setCurrentIndex(ui.cbPattern->findText(p));
126         pattern = ui.cbPattern->getPattern();
127 }
128
129 void DlgHatch::resizeEvent(QResizeEvent *)
130 {
131         updatePreview(NULL);
132 }
133
134 void DlgHatch::updatePreview()
135 {
136         updatePreview(NULL);
137 }
138
139 void DlgHatch::updatePreview(RS_Pattern *)
140 {
141         if (!preview)
142                 return;
143
144         if (!hatch || !ui.cbEnablePreview->isChecked())
145         {
146                 preview->clear();
147                 ui.gvPreview->zoomAuto();
148                 return;
149         }
150
151         QString patName = ui.cbPattern->currentText();
152         bool isSolid = ui.cbSolid->isChecked();
153         double prevSize;
154         //double scale = RS_Math::eval(leScale->text(), 1.0);
155         double angle = RS_Math::deg2rad(RS_Math::eval(ui.leAngle->text(), 0.0));
156
157         if (pattern != NULL)
158                 prevSize = pattern->getSize().x * 10;
159         else
160                 prevSize = 10.0;
161
162         preview->clear();
163
164         RS_Hatch * prevHatch = new RS_Hatch(preview, RS_HatchData(isSolid, 0.2, angle, patName));
165         prevHatch->setPen(hatch->getPen());
166
167         RS_EntityContainer * loop = new RS_EntityContainer(prevHatch);
168         loop->setPen(RS_Pen(RS2::FlagInvalid));
169         loop->addEntity(new RS_Line(loop, RS_LineData(Vector(0.0, 0.0), Vector(10.0, 0.0))));
170         loop->addEntity(new RS_Line(loop, RS_LineData(Vector(10.0, 0.0), Vector(10.0, 10.0))));
171         loop->addEntity(new RS_Line(loop, RS_LineData(Vector(10.0, 10.0), Vector(0.0, 10.0))));
172         loop->addEntity(new RS_Line(loop, RS_LineData(Vector(0.0, 10.0), Vector(0.0, 0.0))));
173         prevHatch->addEntity(loop);
174         preview->addEntity(prevHatch);
175
176         if (!isSolid)
177                 prevHatch->update();
178
179         ui.gvPreview->zoomAuto();
180 }