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_dlgimageoptions.h"
27 
28 #include "rs_math.h"
29 #include "rs_settings.h"
30 
31 /*
32  *  Constructs a QG_ImageOptionsDialog as a child of 'parent', with the
33  *  name 'name' and widget flags set to 'f'.
34  *
35  *  The dialog will by default be modeless, unless you set 'modal' to
36  *  true to construct a modal dialog.
37  */
QG_ImageOptionsDialog(QWidget * parent,bool modal,Qt::WindowFlags fl)38 QG_ImageOptionsDialog::QG_ImageOptionsDialog(QWidget* parent, bool modal, Qt::WindowFlags fl)
39     : QDialog(parent, fl)
40 {
41     setModal(modal);
42     setupUi(this);
43 
44     init();
45 }
46 
47 /*
48  *  Destroys the object and frees any allocated resources
49  */
~QG_ImageOptionsDialog()50 QG_ImageOptionsDialog::~QG_ImageOptionsDialog()
51 {
52     // no need to delete child widgets, Qt does it all for us
53 }
54 
55 /*
56  *  Sets the strings of the subwidgets using the current
57  *  language.
58  */
languageChange()59 void QG_ImageOptionsDialog::languageChange()
60 {
61     retranslateUi(this);
62 }
63 
init()64 void QG_ImageOptionsDialog::init() {
65     graphicSize = RS_Vector(0.0,0.0);
66     updateEnabled = false;
67     useResolution = true;
68 
69     RS_SETTINGS->beginGroup("/Export");
70     if (RS_SETTINGS->readEntry("/UseResolution", "1")=="1") {
71         cbResolution->setCurrentIndex(cbResolution->findText(QString("%1").arg(RS_SETTINGS->readEntry("/Resolution","1"))));
72 	}
73     else {
74 		leWidth->setText(RS_SETTINGS->readEntry("/Width", "640"));
75         leHeight->setText(RS_SETTINGS->readEntry("/Height", "480"));
76     }
77     if (RS_SETTINGS->readEntry("/BlackBackground", "0")=="1") {
78         rbBlack->setChecked(true);
79         rbWhite->setChecked(false);
80     }
81     else {
82     	rbBlack->setChecked(false);
83 		rbWhite->setChecked(true);
84     }
85     if (RS_SETTINGS->readEntry("/BlackWhite", "1")=="1") {
86         rbBlackWhite->setChecked(true);
87         rbColoured->setChecked(false);
88     }
89     else {
90     	rbBlackWhite->setChecked(false);
91     	rbColoured->setChecked(true);
92     }
93     leLeftRight->setText(RS_SETTINGS->readEntry("/BorderLeftRight", "5"));
94     leTopBottom->setText(RS_SETTINGS->readEntry("/BorderTopBottom", "5"));
95     if (RS_SETTINGS->readEntry("/BorderSameSize", "1")=="1") {
96         cbSameBorders->setChecked(true);
97         sameBordersChanged();
98     }
99     RS_SETTINGS->endGroup();
100 
101     updateEnabled = true;
102 }
103 
setGraphicSize(const RS_Vector & s)104 void QG_ImageOptionsDialog::setGraphicSize(const RS_Vector& s) {
105     graphicSize = s;
106     if(!useResolution){
107         sizeChanged();
108     }
109     else {
110         resolutionChanged();
111     }
112 }
113 
ok()114 void QG_ImageOptionsDialog::ok() {
115     RS_SETTINGS->beginGroup("/Export");
116     RS_SETTINGS->writeEntry("/UseResolution", (int)useResolution);
117     RS_SETTINGS->writeEntry("/Resolution", cbResolution->currentText());
118     RS_SETTINGS->writeEntry("/Width", leWidth->text());
119     RS_SETTINGS->writeEntry("/Height", leHeight->text());
120     RS_SETTINGS->writeEntry("/BorderLeftRight", leLeftRight->text());
121     RS_SETTINGS->writeEntry("/BorderTopBottom", leTopBottom->text());
122     RS_SETTINGS->writeEntry("/BorderSameSize", (int)cbSameBorders->isChecked());
123     RS_SETTINGS->writeEntry("/BlackBackground", (int)rbBlack->isChecked());
124     RS_SETTINGS->writeEntry("/BlackWhite", (int)rbBlackWhite->isChecked());
125     RS_SETTINGS->endGroup();
126 
127     accept();
128 }
129 
sameBordersChanged()130 void QG_ImageOptionsDialog::sameBordersChanged() {
131     if(cbSameBorders->isChecked()) {
132         leTopBottom->setText(leLeftRight->text());
133         leTopBottom->setDisabled(true);
134     }
135     else {
136         leTopBottom->setEnabled(true);
137     }
138 }
139 
borderChanged()140 void QG_ImageOptionsDialog::borderChanged() {
141     if(cbSameBorders->isChecked()) {
142         leTopBottom->setText(leLeftRight->text());
143     }
144 }
145 
sizeChanged()146 void QG_ImageOptionsDialog::sizeChanged() {
147     if (updateEnabled) {
148 		updateEnabled = false;
149         useResolution = false;
150 		cbResolution->setCurrentIndex(cbResolution->findText("auto"));
151 		updateEnabled = true;
152     }
153 }
154 
resolutionChanged()155 void  QG_ImageOptionsDialog::resolutionChanged() {
156     if (updateEnabled) {
157 		updateEnabled = false;
158 		bool ok = false;
159 		double res = RS_Math::eval(cbResolution->currentText(), &ok);
160 		if (!ok) {
161 			res = 1.0;
162 		}
163 		int w = RS_Math::round(res * graphicSize.x);
164 		int h = RS_Math::round(res * graphicSize.y);
165         useResolution = true;
166 		leWidth->setText(QString("%1").arg(w));
167 		leHeight->setText(QString("%1").arg(h));
168 		updateEnabled = true;
169     }
170 }
171 
getSize()172 QSize QG_ImageOptionsDialog::getSize() {
173     return QSize(RS_Math::round(RS_Math::eval(leWidth->text())),
174                     RS_Math::round(RS_Math::eval(leHeight->text())));
175 }
176 
getBorders()177 QSize QG_ImageOptionsDialog::getBorders() {
178     return QSize(RS_Math::round(RS_Math::eval(leLeftRight->text())),
179                    RS_Math::round(RS_Math::eval(leTopBottom->text())));
180 }
181 
isBackgroundBlack()182 bool QG_ImageOptionsDialog::isBackgroundBlack() {
183     return rbBlack->isChecked();
184 }
185 
isBlackWhite()186 bool QG_ImageOptionsDialog::isBlackWhite() {
187     return rbBlackWhite->isChecked();
188 }
189