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 ** Copyright (C) 2015, 2016 ravas (github.com/r-a-v-a-s)
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_dlgoptionsgeneral.h"
27
28 #include <QMessageBox>
29 #include <QColorDialog>
30 #include "rs_system.h"
31 #include "rs_settings.h"
32 #include "rs_units.h"
33 #include "qg_filedialog.h"
34 #include "rs_debug.h"
35
36 /*
37 * Constructs a QG_DlgOptionsGeneral 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 */
43
44 int QG_DlgOptionsGeneral::current_tab = 0;
45
QG_DlgOptionsGeneral(QWidget * parent,bool modal,Qt::WindowFlags fl)46 QG_DlgOptionsGeneral::QG_DlgOptionsGeneral(QWidget* parent, bool modal, Qt::WindowFlags fl)
47 : QDialog(parent, fl)
48 {
49 setModal(modal);
50 setupUi(this);
51 tabWidget->setCurrentIndex(current_tab);
52 init();
53 connect(variablefile_button, &QToolButton::clicked,
54 this, &QG_DlgOptionsGeneral::setVariableFile);
55 connect(fonts_button, &QToolButton::clicked,
56 this, &QG_DlgOptionsGeneral::setFontsFolder);
57 }
58
59 /*
60 * Destroys the object and frees any allocated resources
61 */
~QG_DlgOptionsGeneral()62 QG_DlgOptionsGeneral::~QG_DlgOptionsGeneral()
63 {
64 destroy();
65 // no need to delete child widgets, Qt does it all for us
66 }
67
68 /*
69 * Sets the strings of the subwidgets using the current
70 * language.
71 */
languageChange()72 void QG_DlgOptionsGeneral::languageChange()
73 {
74 retranslateUi(this);
75 }
76
init()77 void QG_DlgOptionsGeneral::init()
78 {
79 // Fill combobox with languages:
80 QStringList languageList = RS_SYSTEM->getLanguageList();
81 languageList.sort();
82 languageList.prepend("en");
83 for(auto const& lang: languageList){
84
85 RS_DEBUG->print("QG_DlgOptionsGeneral::init: adding %s to combobox",
86 lang.toLatin1().data());
87
88 QString l = RS_SYSTEM->symbolToLanguage(lang);
89 if (!l.isEmpty() && cbLanguage->findData(lang)==-1) {
90 RS_DEBUG->print("QG_DlgOptionsGeneral::init: %s", l.toLatin1().data());
91 cbLanguage->addItem(l,lang);
92 cbLanguageCmd->addItem(l, lang);
93 }
94 }
95
96 RS_SETTINGS->beginGroup("/Appearance");
97
98 // set current language:
99 QString def_lang = "en";
100
101 QString lang = RS_SETTINGS->readEntry("/Language", def_lang);
102 cbLanguage->setCurrentIndex( cbLanguage->findText(RS_SYSTEM->symbolToLanguage(lang)) );
103
104 QString langCmd = RS_SETTINGS->readEntry("/LanguageCmd", def_lang);
105 cbLanguageCmd->setCurrentIndex( cbLanguageCmd->findText(RS_SYSTEM->symbolToLanguage(langCmd)) );
106
107 // graphic view:
108
109 // Snap Indicators
110 bool indicator_lines_state = RS_SETTINGS->readNumEntry("/indicator_lines_state", 1);
111 indicator_lines_checkbox->setChecked(indicator_lines_state);
112
113 QString indicator_lines_type = RS_SETTINGS->readEntry("/indicator_lines_type", "Crosshair");
114 int index = indicator_lines_combobox->findText(indicator_lines_type);
115 indicator_lines_combobox->setCurrentIndex(index);
116
117 bool indicator_shape_state = RS_SETTINGS->readNumEntry("/indicator_shape_state", 1);
118 indicator_shape_checkbox->setChecked(indicator_shape_state);
119
120 QString indicator_shape_type = RS_SETTINGS->readEntry("/indicator_shape_type", "Circle");
121 index = indicator_shape_combobox->findText(indicator_shape_type);
122 indicator_shape_combobox->setCurrentIndex(index);
123
124 bool cursor_hiding = RS_SETTINGS->readNumEntry("/cursor_hiding", 0);
125 cursor_hiding_checkbox->setChecked(cursor_hiding);
126
127 // scale grid:
128 QString scaleGrid = RS_SETTINGS->readEntry("/ScaleGrid", "1");
129 cbScaleGrid->setChecked(scaleGrid=="1");
130 QString minGridSpacing = RS_SETTINGS->readEntry("/MinGridSpacing", "10");
131 cbMinGridSpacing->setCurrentIndex( cbMinGridSpacing->findText(minGridSpacing) );
132
133 int checked = RS_SETTINGS->readNumEntry("/Antialiasing");
134 cb_antialiasing->setChecked(checked?true:false);
135
136 checked = RS_SETTINGS->readNumEntry("/ScrollBars");
137 scrollbars_check_box->setChecked(checked?true:false);
138
139 // preview:
140 initComboBox(cbMaxPreview, RS_SETTINGS->readEntry("/MaxPreview", "100"));
141
142 RS_SETTINGS->endGroup();
143
144 RS_SETTINGS->beginGroup("Colors");
145 initComboBox(cbBackgroundColor, RS_SETTINGS->readEntry("/background", Colors::background));
146 initComboBox(cbGridColor, RS_SETTINGS->readEntry("/grid", Colors::grid));
147 initComboBox(cbMetaGridColor, RS_SETTINGS->readEntry("/meta_grid", Colors::meta_grid));
148 initComboBox(cbSelectedColor, RS_SETTINGS->readEntry("/select", Colors::select));
149 initComboBox(cbHighlightedColor, RS_SETTINGS->readEntry("/highlight", Colors::highlight));
150 initComboBox(cbStartHandleColor, RS_SETTINGS->readEntry("/start_handle", Colors::start_handle));
151 initComboBox(cbHandleColor, RS_SETTINGS->readEntry("/handle", Colors::handle));
152 initComboBox(cbEndHandleColor, RS_SETTINGS->readEntry("/end_handle", Colors::end_handle));
153 initComboBox(cb_snap_color, RS_SETTINGS->readEntry("/snap_indicator", Colors::snap_indicator));
154 RS_SETTINGS->endGroup();
155
156 RS_SETTINGS->beginGroup("/Paths");
157
158 lePathTranslations->setText(RS_SETTINGS->readEntry("/Translations", ""));
159 lePathHatch->setText(RS_SETTINGS->readEntry("/Patterns", ""));
160 lePathFonts->setText(RS_SETTINGS->readEntry("/Fonts", ""));
161 lePathLibrary->setText(RS_SETTINGS->readEntry("/Library", "").trimmed());
162 leTemplate->setText(RS_SETTINGS->readEntry("/Template", "").trimmed());
163 variablefile_field->setText(RS_SETTINGS->readEntry("/VariableFile", "").trimmed());
164 RS_SETTINGS->endGroup();
165
166 // units:
167 for (int i=RS2::None; i<RS2::LastUnit; i++) {
168 if (i!=(int)RS2::None)
169 cbUnit->addItem(RS_Units::unitToString((RS2::Unit)i));
170 }
171 // RVT_PORT cbUnit->listBox()->sort();
172 cbUnit->insertItem( 0, RS_Units::unitToString(RS2::None) );
173
174 QString def_unit = "Millimeter";
175
176 RS_SETTINGS->beginGroup("/Defaults");
177 // cbUnit->setCurrentIndex( cbUnit->findText(QObject::tr( RS_SETTINGS->readEntry("/Unit", def_unit) )) );
178 cbUnit->setCurrentIndex( cbUnit->findText(QObject::tr( RS_SETTINGS->readEntry("/Unit", def_unit).toUtf8().data() )) );
179 // Auto save timer
180 cbAutoSaveTime->setValue(RS_SETTINGS->readNumEntry("/AutoSaveTime", 5));
181 cbAutoBackup->setChecked(RS_SETTINGS->readNumEntry("/AutoBackupDocument", 1));
182 cbUseQtFileOpenDialog->setChecked(RS_SETTINGS->readNumEntry("/UseQtFileOpenDialog", 1));
183 cbWheelScrollInvertH->setChecked(RS_SETTINGS->readNumEntry("/WheelScrollInvertH", 0));
184 cbWheelScrollInvertV->setChecked(RS_SETTINGS->readNumEntry("/WheelScrollInvertV", 0));
185 cbInvertZoomDirection->setChecked(RS_SETTINGS->readNumEntry("/InvertZoomDirection", 0));
186 RS_SETTINGS->endGroup();
187
188 //update entities to selected entities to the current active layer
189 RS_SETTINGS->beginGroup("/Modify");
190 auto toActive=RS_SETTINGS->readNumEntry("/ModifyEntitiesToActiveLayer", 0);
191 cbToActiveLayer->setChecked(toActive==1);
192 RS_SETTINGS->writeEntry("/ModifyEntitiesToActiveLayer", cbToActiveLayer->isChecked()?1:0);
193 RS_SETTINGS->endGroup();
194
195 RS_SETTINGS->beginGroup("/CADPreferences");
196 cbAutoZoomDrawing->setChecked(RS_SETTINGS->readNumEntry("/AutoZoomDrawing"));
197 RS_SETTINGS->endGroup();
198
199 RS_SETTINGS->beginGroup("Startup");
200 cbSplash->setChecked(RS_SETTINGS->readNumEntry("/ShowSplash",1)==1);
201 tab_mode_check_box->setChecked(RS_SETTINGS->readNumEntry("/TabMode", 0));
202 maximize_checkbox->setChecked(RS_SETTINGS->readNumEntry("/Maximize", 0));
203 left_sidebar_checkbox->setChecked(RS_SETTINGS->readNumEntry("/EnableLeftSidebar", 1));
204 cad_toolbars_checkbox->setChecked(RS_SETTINGS->readNumEntry("/EnableCADToolbars", 1));
205 RS_SETTINGS->endGroup();
206
207 cbEvaluateOnSpace->setChecked(RS_SETTINGS->readNumEntry("/Keyboard/EvaluateCommandOnSpace", false));
208 cbToggleFreeSnapOnSpace->setChecked(RS_SETTINGS->readNumEntry("/Keyboard/ToggleFreeSnapOnSpace", false));
209
210 restartNeeded = false;
211 }
212
initComboBox(QComboBox * cb,QString text)213 void QG_DlgOptionsGeneral::initComboBox(QComboBox* cb, QString text) {
214 int idx = cb->findText(text);
215 if( idx < 0) {
216 idx =0;
217 cb->insertItem(idx, text);
218 }
219 cb->setCurrentIndex( idx );
220 }
221
destroy()222 void QG_DlgOptionsGeneral::destroy() {
223 }
224
setRestartNeeded()225 void QG_DlgOptionsGeneral::setRestartNeeded() {
226 restartNeeded = true;
227 }
228
setTemplateFile()229 void QG_DlgOptionsGeneral::setTemplateFile() {
230 RS2::FormatType type = RS2::FormatDXFRW;
231 QG_FileDialog dlg(this);
232 QString fileName = dlg.getOpenFile(&type);
233 leTemplate->setText(fileName);
234 }
235
ok()236 void QG_DlgOptionsGeneral::ok()
237 {
238 if (RS_Settings::save_is_allowed)
239 {
240 //RS_SYSTEM->loadTranslation(cbLanguage->currentText());
241 RS_SETTINGS->beginGroup("/Appearance");
242 RS_SETTINGS->writeEntry("/ScaleGrid", QString("%1").arg((int)cbScaleGrid->isChecked()));
243 RS_SETTINGS->writeEntry("/MinGridSpacing", cbMinGridSpacing->currentText());
244 RS_SETTINGS->writeEntry("/MaxPreview", cbMaxPreview->currentText());
245 RS_SETTINGS->writeEntry("/Language",cbLanguage->itemData(cbLanguage->currentIndex()));
246 RS_SETTINGS->writeEntry("/LanguageCmd",cbLanguageCmd->itemData(cbLanguageCmd->currentIndex()));
247 RS_SETTINGS->writeEntry("/indicator_lines_state", indicator_lines_checkbox->isChecked());
248 RS_SETTINGS->writeEntry("/indicator_lines_type", indicator_lines_combobox->currentText());
249 RS_SETTINGS->writeEntry("/indicator_shape_state", indicator_shape_checkbox->isChecked());
250 RS_SETTINGS->writeEntry("/indicator_shape_type", indicator_shape_combobox->currentText());
251 RS_SETTINGS->writeEntry("/cursor_hiding", cursor_hiding_checkbox->isChecked());
252 RS_SETTINGS->writeEntry("/Antialiasing", cb_antialiasing->isChecked()?1:0);
253 RS_SETTINGS->writeEntry("/ScrollBars", scrollbars_check_box->isChecked()?1:0);
254 RS_SETTINGS->endGroup();
255
256 RS_SETTINGS->beginGroup("Colors");
257 RS_SETTINGS->writeEntry("/background", cbBackgroundColor->currentText());
258 RS_SETTINGS->writeEntry("/grid", cbGridColor->currentText());
259 RS_SETTINGS->writeEntry("/meta_grid", cbMetaGridColor->currentText());
260 RS_SETTINGS->writeEntry("/select", cbSelectedColor->currentText());
261 RS_SETTINGS->writeEntry("/highlight", cbHighlightedColor->currentText());
262 RS_SETTINGS->writeEntry("/start_handle", cbStartHandleColor->currentText());
263 RS_SETTINGS->writeEntry("/handle", cbHandleColor->currentText());
264 RS_SETTINGS->writeEntry("/end_handle", cbEndHandleColor->currentText());
265 RS_SETTINGS->writeEntry("/snap_indicator", cb_snap_color->currentText());
266 RS_SETTINGS->endGroup();
267
268 RS_SETTINGS->beginGroup("/Paths");
269 RS_SETTINGS->writeEntry("/Translations", lePathTranslations->text());
270 RS_SETTINGS->writeEntry("/Patterns", lePathHatch->text());
271 RS_SETTINGS->writeEntry("/Fonts", lePathFonts->text());
272 RS_SETTINGS->writeEntry("/Library", lePathLibrary->text());
273 RS_SETTINGS->writeEntry("/Template", leTemplate->text());
274 RS_SETTINGS->writeEntry("/VariableFile", variablefile_field->text());
275 RS_SETTINGS->endGroup();
276
277 RS_SETTINGS->beginGroup("/Defaults");
278 RS_SETTINGS->writeEntry("/Unit",
279 RS_Units::unitToString( RS_Units::stringToUnit( cbUnit->currentText() ), false/*untr.*/) );
280 RS_SETTINGS->writeEntry("/AutoSaveTime", cbAutoSaveTime->value() );
281 RS_SETTINGS->writeEntry("/AutoBackupDocument", cbAutoBackup->isChecked() ? 1 : 0);
282 RS_SETTINGS->writeEntry("/UseQtFileOpenDialog", cbUseQtFileOpenDialog->isChecked() ? 1 : 0);
283 RS_SETTINGS->writeEntry("/WheelScrollInvertH", cbWheelScrollInvertH->isChecked() ? 1 : 0);
284 RS_SETTINGS->writeEntry("/WheelScrollInvertV", cbWheelScrollInvertV->isChecked() ? 1 : 0);
285 RS_SETTINGS->writeEntry("/InvertZoomDirection", cbInvertZoomDirection->isChecked() ? 1 : 0);
286 RS_SETTINGS->endGroup();
287
288 //update entities to selected entities to the current active layer
289 RS_SETTINGS->beginGroup("/Modify");
290 RS_SETTINGS->writeEntry("/ModifyEntitiesToActiveLayer", cbToActiveLayer->isChecked()?1:0);
291 RS_SETTINGS->endGroup();
292
293 RS_SETTINGS->beginGroup("/CADPreferences");
294 RS_SETTINGS->writeEntry("/AutoZoomDrawing", cbAutoZoomDrawing->isChecked() ? 1 : 0);
295 RS_SETTINGS->endGroup();
296
297 RS_SETTINGS->beginGroup("Startup");
298 RS_SETTINGS->writeEntry("/ShowSplash", cbSplash->isChecked()?1:0);
299 RS_SETTINGS->writeEntry("/TabMode", tab_mode_check_box->isChecked()?1:0);
300 RS_SETTINGS->writeEntry("/Maximize", maximize_checkbox->isChecked()?1:0);
301 RS_SETTINGS->writeEntry("/EnableLeftSidebar", left_sidebar_checkbox->isChecked()?1:0);
302 RS_SETTINGS->writeEntry("/EnableCADToolbars", cad_toolbars_checkbox->isChecked()?1:0);
303 RS_SETTINGS->endGroup();
304
305 RS_SETTINGS->writeEntry("/Keyboard/EvaluateCommandOnSpace", cbEvaluateOnSpace->isChecked() ? 1 : 0);
306 RS_SETTINGS->writeEntry("/Keyboard/ToggleFreeSnapOnSpace", cbToggleFreeSnapOnSpace->isChecked() ? 1 : 0);
307 }
308
309 if (restartNeeded==true) {
310 QMessageBox::warning( this, tr("Preferences"),
311 tr("Please restart the application to apply all changes."),
312 QMessageBox::Ok,
313 Qt::NoButton);
314 }
315 accept();
316 }
317
318
on_tabWidget_currentChanged(int index)319 void QG_DlgOptionsGeneral::on_tabWidget_currentChanged(int index)
320 {
321 current_tab = index;
322 }
323
set_color(QComboBox * combo,QColor custom)324 void QG_DlgOptionsGeneral::set_color(QComboBox* combo, QColor custom)
325 {
326 QColor current;
327 current.setNamedColor(combo->lineEdit()->text());
328
329 QColorDialog dlg;
330 dlg.setCustomColor(0, custom.rgb());
331
332 QColor color = dlg.getColor(current, this, "Select Color", QColorDialog::DontUseNativeDialog);
333 if (color.isValid())
334 {
335 combo->lineEdit()->setText(color.name());
336 }
337 }
338
on_pb_background_clicked()339 void QG_DlgOptionsGeneral::on_pb_background_clicked()
340 {
341 set_color(cbBackgroundColor, QColor(Colors::background));
342 }
343
on_pb_grid_clicked()344 void QG_DlgOptionsGeneral::on_pb_grid_clicked()
345 {
346 set_color(cbGridColor, QColor(Colors::grid));
347 }
348
on_pb_meta_clicked()349 void QG_DlgOptionsGeneral::on_pb_meta_clicked()
350 {
351 set_color(cbMetaGridColor, QColor(Colors::meta_grid));
352 }
353
on_pb_selected_clicked()354 void QG_DlgOptionsGeneral::on_pb_selected_clicked()
355 {
356 set_color(cbSelectedColor, QColor(Colors::select));
357 }
358
on_pb_highlighted_clicked()359 void QG_DlgOptionsGeneral::on_pb_highlighted_clicked()
360 {
361 set_color(cbHighlightedColor, QColor(Colors::highlight));
362 }
363
on_pb_start_clicked()364 void QG_DlgOptionsGeneral::on_pb_start_clicked()
365 {
366 set_color(cbStartHandleColor, QColor(Colors::start_handle));
367 }
368
on_pb_handle_clicked()369 void QG_DlgOptionsGeneral::on_pb_handle_clicked()
370 {
371 set_color(cbHandleColor, QColor(Colors::handle));
372 }
373
on_pb_end_clicked()374 void QG_DlgOptionsGeneral::on_pb_end_clicked()
375 {
376 set_color(cbEndHandleColor, QColor(Colors::end_handle));
377 }
378
on_pb_snap_color_clicked()379 void QG_DlgOptionsGeneral::on_pb_snap_color_clicked()
380 {
381 set_color(cb_snap_color, QColor(Colors::snap_indicator));
382 }
383
on_pb_clear_all_clicked()384 void QG_DlgOptionsGeneral::on_pb_clear_all_clicked()
385 {
386 QMessageBox::StandardButton reply;
387 reply = QMessageBox::question(this, tr("Clear settings"),
388 tr("This will also include custom menus and toolbars. Continue?"),
389 QMessageBox::Yes|QMessageBox::No);
390 if (reply == QMessageBox::Yes)
391 {
392 RS_SETTINGS->clear_all();
393 QMessageBox::information(this, "info", "You must restart LibreCAD to see the changes.");
394 }
395 }
396
on_pb_clear_geometry_clicked()397 void QG_DlgOptionsGeneral::on_pb_clear_geometry_clicked()
398 {
399 RS_SETTINGS->clear_geometry();
400 QMessageBox::information(this, "info", "You must restart LibreCAD to see the changes.");
401 }
402
setVariableFile()403 void QG_DlgOptionsGeneral::setVariableFile()
404 {
405 QString path = QFileDialog::getOpenFileName(this);
406 if (!path.isEmpty())
407 {
408 variablefile_field->setText(QDir::toNativeSeparators(path));
409 }
410 }
411
412 /*!
413 * \brief slot for the font folder selection icon
414 * \author ravas
415 * \date 2016-286
416 */
setFontsFolder()417 void QG_DlgOptionsGeneral::setFontsFolder()
418 {
419 QFileDialog dlg(this);
420 dlg.setFileMode(QFileDialog::Directory);
421 dlg.setOption(QFileDialog::ShowDirsOnly);
422
423 if (dlg.exec())
424 {
425 auto dir = dlg.selectedFiles()[0];
426 lePathFonts->setText(QDir::toNativeSeparators(dir));
427 }
428 }
429