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