1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
7 **
8 **
9 ** This file may be distributed and/or modified under the terms of the
10 ** GNU General Public License version 2 as published by the Free Software
11 ** Foundation and appearing in the file gpl-2.0.txt included in the
12 ** packaging of this file.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program; if not, write to the Free Software
21 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26 #include "qg_dlghatch.h"
27
28 #include "rs_settings.h"
29 #include "rs_line.h"
30 #include "rs_hatch.h"
31 #include "rs_patternlist.h"
32 #include "rs_pattern.h"
33 #include "rs_math.h"
34 #include "rs_math.h"
35
36 /*
37 * Constructs a QG_DlgHatch as a child of 'parent', with the
38 * name 'name' and widget flags set to 'f'.
39 *
40 * The dialog will by default be modeless, unless you set 'modal' to
41 * true to construct a modal dialog.
42 */
QG_DlgHatch(QWidget * parent,bool modal,Qt::WindowFlags fl)43 QG_DlgHatch::QG_DlgHatch(QWidget* parent, bool modal, Qt::WindowFlags fl)
44 : QDialog(parent, fl)
45 {
46 setModal(modal);
47 setupUi(this);
48
49 init();
50 }
51
52
53 /*
54 * Sets the strings of the subwidgets using the current
55 * language.
56 */
languageChange()57 void QG_DlgHatch::languageChange()
58 {
59 retranslateUi(this);
60 }
61
init()62 void QG_DlgHatch::init() {
63 pattern=nullptr;
64 hatch = nullptr;
65 isNew = false;
66
67 preview = new RS_EntityContainer();
68 gvPreview->setContainer(preview);
69 gvPreview->setBorders(15,15,15,15);
70 gvPreview->addScrollbars();
71
72 cbPattern->init();
73
74 }
75
polish()76 void QG_DlgHatch::polish() {
77 QDialog::ensurePolished();
78 gvPreview->zoomAuto();
79 }
80
showEvent(QShowEvent * e)81 void QG_DlgHatch::showEvent ( QShowEvent * e) {
82 QDialog::showEvent(e);
83 gvPreview->zoomAuto();
84 }
85
~QG_DlgHatch()86 QG_DlgHatch::~QG_DlgHatch()
87 {
88 delete preview;
89 }
90
setHatch(RS_Hatch & h,bool isNew)91 void QG_DlgHatch::setHatch(RS_Hatch& h, bool isNew) {
92 hatch = &h;
93 this->isNew = isNew;
94
95 RS_SETTINGS->beginGroup("/Draw");
96 QString enablePrev = RS_SETTINGS->readEntry("/HatchPreview", "0");
97 RS_SETTINGS->endGroup();
98
99 cbEnablePreview->setChecked(enablePrev=="1");
100
101 // read defaults from config file:
102 if (isNew) {
103 RS_SETTINGS->beginGroup("/Draw");
104 QString solid = RS_SETTINGS->readEntry("/HatchSolid", "0");
105 QString pat = RS_SETTINGS->readEntry("/HatchPattern", "ANSI31");
106 QString scale = RS_SETTINGS->readEntry("/HatchScale", "1.0");
107 QString angle = RS_SETTINGS->readEntry("/HatchAngle", "0.0");
108 RS_SETTINGS->endGroup();
109
110 cbSolid->setChecked(solid=="1");
111 setPattern(pat);
112 leScale->setText(scale);
113 leAngle->setText(angle);
114 }
115 // initialize dialog based on given hatch:
116 else {
117 cbSolid->setChecked(hatch->isSolid());
118 setPattern(hatch->getPattern());
119 QString s;
120 s.setNum(hatch->getScale());
121 leScale->setText(s);
122 s.setNum(RS_Math::rad2deg(hatch->getAngle()));
123 leAngle->setText(s);
124 }
125 }
126
updateHatch()127 void QG_DlgHatch::updateHatch() {
128 if (hatch) {
129 hatch->setSolid(cbSolid->isChecked());
130 hatch->setPattern(cbPattern->currentText());
131 hatch->setScale(RS_Math::eval(leScale->text()));
132 hatch->setAngle(RS_Math::deg2rad(RS_Math::eval(leAngle->text())));
133 }
134 }
135
setPattern(const QString & p)136 void QG_DlgHatch::setPattern(const QString& p) {
137 if (!RS_PATTERNLIST->contains(p)) {
138 cbPattern->addItem(p);
139 }
140 cbPattern->setCurrentIndex( cbPattern->findText(p) );
141 pattern = cbPattern->getPattern();
142 }
143
resizeEvent(QResizeEvent *)144 void QG_DlgHatch::resizeEvent ( QResizeEvent * ) {
145 updatePreview();
146 }
147
updatePreview()148 void QG_DlgHatch::updatePreview() {
149 if (preview==nullptr) {
150 return;
151 }
152 if (hatch==nullptr || !cbEnablePreview->isChecked()) {
153 preview->clear();
154 gvPreview->zoomAuto();
155 return;
156 }
157 pattern = cbPattern->getPattern();
158 if (pattern->countDeep()==0)
159 return;
160
161 QString patName = cbPattern->currentText();
162 bool isSolid = cbSolid->isChecked();
163 double scale = RS_Math::eval(leScale->text(), 1.0);
164 double angle = RS_Math::deg2rad(RS_Math::eval(leAngle->text(), 0.0));
165 double prevSize = 100.0;
166 if (pattern) {
167 pattern->calculateBorders();
168 prevSize = std::max(prevSize, pattern->getSize().magnitude());
169 }
170
171 preview->clear();
172
173 RS_Hatch* prevHatch = new RS_Hatch(preview,
174 RS_HatchData(isSolid, scale, angle, patName));
175 prevHatch->setPen(hatch->getPen());
176
177 RS_EntityContainer* loop = new RS_EntityContainer(prevHatch);
178 loop->setPen(RS_Pen(RS2::FlagInvalid));
179 loop->addRectangle({0., 0.}, {prevSize,prevSize});
180 prevHatch->addEntity(loop);
181 preview->addEntity(prevHatch);
182 if (!isSolid) {
183 prevHatch->update();
184 }
185
186 gvPreview->zoomAuto();
187 }
188
saveSettings()189 void QG_DlgHatch::saveSettings()
190 {
191 if (isNew)
192 {
193 RS_SETTINGS->beginGroup("/Draw");
194 RS_SETTINGS->writeEntry("/HatchSolid", cbSolid->isChecked());
195 RS_SETTINGS->writeEntry("/HatchPattern", cbPattern->currentText());
196 RS_SETTINGS->writeEntry("/HatchScale", leScale->text());
197 RS_SETTINGS->writeEntry("/HatchAngle", leAngle->text());
198 RS_SETTINGS->writeEntry("/HatchPreview", cbEnablePreview->isChecked());
199 RS_SETTINGS->endGroup();
200 }
201 }
202