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