/
DlgGeneralImp.cpp
316 lines (275 loc) · 12.9 KB
/
DlgGeneralImp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/***************************************************************************
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QApplication>
# include <QLocale>
# include <QStyleFactory>
# include <QTextStream>
#endif
#include "DlgGeneralImp.h"
#include "ui_DlgGeneral.h"
#include "Action.h"
#include "Application.h"
#include "DockWindowManager.h"
#include "MainWindow.h"
#include "PrefWidgets.h"
#include "PythonConsole.h"
#include "Language/Translator.h"
using namespace Gui::Dialog;
/* TRANSLATOR Gui::Dialog::DlgGeneralImp */
/**
* Constructs a DlgGeneralImp which is a child of 'parent', with the
* name 'name' and widget flags set to 'f'
*
* The dialog will by default be modeless, unless you set 'modal' to
* true to construct a modal dialog.
*/
DlgGeneralImp::DlgGeneralImp( QWidget* parent )
: PreferencePage(parent)
, ui(new Ui_DlgGeneral)
{
ui->setupUi(this);
// fills the combo box with all available workbenches
// sorted by their menu text
QStringList work = Application::Instance->workbenches();
QMap<QString, QString> menuText;
for (QStringList::Iterator it = work.begin(); it != work.end(); ++it) {
QString text = Application::Instance->workbenchMenuText(*it);
menuText[text] = *it;
}
{ // add special workbench to selection
QPixmap px = Application::Instance->workbenchIcon(QString::fromLatin1("NoneWorkbench"));
QString key = QString::fromLatin1("<last>");
QString value = QString::fromLatin1("$LastModule");
if (px.isNull())
ui->AutoloadModuleCombo->addItem(key, QVariant(value));
else
ui->AutoloadModuleCombo->addItem(px, key, QVariant(value));
}
for (QMap<QString, QString>::Iterator it = menuText.begin(); it != menuText.end(); ++it) {
QPixmap px = Application::Instance->workbenchIcon(it.value());
if (px.isNull())
ui->AutoloadModuleCombo->addItem(it.key(), QVariant(it.value()));
else
ui->AutoloadModuleCombo->addItem(px, it.key(), QVariant(it.value()));
}
}
/**
* Destroys the object and frees any allocated resources
*/
DlgGeneralImp::~DlgGeneralImp()
{
}
/** Sets the size of the recent file list from the user parameters.
* @see RecentFilesAction
* @see StdCmdRecentFiles
*/
void DlgGeneralImp::setRecentFileSize()
{
RecentFilesAction *recent = getMainWindow()->findChild<RecentFilesAction *>(QLatin1String("recentFiles"));
if (recent) {
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("RecentFiles");
recent->resizeList(hGrp->GetInt("RecentFiles", 4));
}
}
void DlgGeneralImp::saveSettings()
{
int index = ui->AutoloadModuleCombo->currentIndex();
QVariant data = ui->AutoloadModuleCombo->itemData(index);
QString startWbName = data.toString();
App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")->
SetASCII("AutoloadModule", startWbName.toLatin1());
ui->RecentFiles->onSave();
ui->SplashScreen->onSave();
ui->PythonWordWrap->onSave();
QWidget* pc = DockWindowManager::instance()->getDockWindow("Python console");
PythonConsole *pcPython = qobject_cast<PythonConsole*>(pc);
if (pcPython) {
bool pythonWordWrap = App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("General")->GetBool("PythonWordWrap", true);
if (pythonWordWrap) {
pcPython->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
} else {
pcPython->setWordWrapMode(QTextOption::NoWrap);
}
}
setRecentFileSize();
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("General");
QString lang = QLocale::languageToString(QLocale::system().language());
QByteArray language = hGrp->GetASCII("Language", (const char*)lang.toLatin1()).c_str();
QByteArray current = ui->Languages->itemData(ui->Languages->currentIndex()).toByteArray();
if (current != language) {
hGrp->SetASCII("Language", current.constData());
Translator::instance()->activateLanguage(current.constData());
}
QVariant size = ui->toolbarIconSize->itemData(ui->toolbarIconSize->currentIndex());
int pixel = size.toInt();
hGrp->SetInt("ToolbarIconSize", pixel);
getMainWindow()->setIconSize(QSize(pixel,pixel));
hGrp->SetInt("TreeViewMode",ui->treeMode->currentIndex());
hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/MainWindow");
hGrp->SetBool("TiledBackground", ui->tiledBackground->isChecked());
QMdiArea* mdi = getMainWindow()->findChild<QMdiArea*>();
mdi->setProperty("showImage", ui->tiledBackground->isChecked());
QVariant sheet = ui->StyleSheets->itemData(ui->StyleSheets->currentIndex());
if (this->selectedStyleSheet != sheet.toString()) {
this->selectedStyleSheet = sheet.toString();
hGrp->SetASCII("StyleSheet", (const char*)sheet.toByteArray());
if (!sheet.toString().isEmpty()) {
QFile f(sheet.toString());
if (f.open(QFile::ReadOnly)) {
mdi->setBackground(QBrush(Qt::NoBrush));
QTextStream str(&f);
qApp->setStyleSheet(str.readAll());
ActionStyleEvent e(ActionStyleEvent::Clear);
qApp->sendEvent(getMainWindow(), &e);
}
}
}
if (sheet.toString().isEmpty()) {
if (ui->tiledBackground->isChecked()) {
qApp->setStyleSheet(QString());
ActionStyleEvent e(ActionStyleEvent::Restore);
qApp->sendEvent(getMainWindow(), &e);
mdi->setBackground(QPixmap(QLatin1String("images:background.png")));
}
else {
qApp->setStyleSheet(QString());
ActionStyleEvent e(ActionStyleEvent::Restore);
qApp->sendEvent(getMainWindow(), &e);
mdi->setBackground(QBrush(QColor(160,160,160)));
}
#if QT_VERSION == 0x050600 && defined(Q_OS_WIN32)
// Under Windows the tree indicator branch gets corrupted after a while.
// For more details see also https://bugreports.qt.io/browse/QTBUG-52230
// and https://codereview.qt-project.org/#/c/154357/2//ALL,unified
// A workaround for Qt 5.6.0 is to set a minimal style sheet.
QString qss = QString::fromLatin1(
"QTreeView::branch:closed:has-children {\n"
" image: url(:/icons/style/windows_branch_closed.png);\n"
"}\n"
"\n"
"QTreeView::branch:open:has-children {\n"
" image: url(:/icons/style/windows_branch_open.png);\n"
"}\n");
qApp->setStyleSheet(qss);
#endif
}
if (mdi->style())
mdi->style()->unpolish(qApp);
}
void DlgGeneralImp::loadSettings()
{
std::string start = App::Application::Config()["StartWorkbench"];
start = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")->
GetASCII("AutoloadModule", start.c_str());
QString startWbName = QLatin1String(start.c_str());
ui->AutoloadModuleCombo->setCurrentIndex(ui->AutoloadModuleCombo->findData(startWbName));
ui->RecentFiles->onRestore();
ui->SplashScreen->onRestore();
ui->PythonWordWrap->onRestore();
// search for the language files
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("General");
QString langToStr = QLocale::languageToString(QLocale::system().language());
QByteArray language = hGrp->GetASCII("Language", langToStr.toLatin1()).c_str();
int index = 1;
TStringMap list = Translator::instance()->supportedLocales();
ui->Languages->clear();
ui->Languages->addItem(QString::fromLatin1("English"), QByteArray("English"));
for (TStringMap::iterator it = list.begin(); it != list.end(); ++it, index++) {
QByteArray lang = it->first.c_str();
QString langname = QString::fromLatin1(lang.constData());
#if QT_VERSION >= 0x040800
QLocale locale(QString::fromLatin1(it->second.c_str()));
QString native = locale.nativeLanguageName();
if (!native.isEmpty()) {
if (native[0].isLetter())
native[0] = native[0].toUpper();
langname = native;
}
#endif
ui->Languages->addItem(langname, lang);
if (language == lang) {
ui->Languages->setCurrentIndex(index);
}
}
QAbstractItemModel* model = ui->Languages->model();
if (model)
model->sort(0);
int current = getMainWindow()->iconSize().width();
ui->toolbarIconSize->addItem(tr("Small (%1px)").arg(16), QVariant((int)16));
ui->toolbarIconSize->addItem(tr("Medium (%1px)").arg(24), QVariant((int)24));
ui->toolbarIconSize->addItem(tr("Large (%1px)").arg(32), QVariant((int)32));
ui->toolbarIconSize->addItem(tr("Extra large (%1px)").arg(48), QVariant((int)48));
index = ui->toolbarIconSize->findData(QVariant(current));
if (index < 0) {
ui->toolbarIconSize->addItem(tr("Custom (%1px)").arg(current), QVariant((int)current));
index = ui->toolbarIconSize->findData(QVariant(current));
}
ui->toolbarIconSize->setCurrentIndex(index);
ui->treeMode->addItem(tr("CombiView"));
ui->treeMode->addItem(tr("TreeView + PropertyView"));
ui->treeMode->addItem(tr("Both"));
index = hGrp->GetInt("TreeViewMode");
if (index<0 || index>2)
index=0;
ui->treeMode->setCurrentIndex(index);
hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/MainWindow");
ui->tiledBackground->setChecked(hGrp->GetBool("TiledBackground", false));
// List all .qss/.css files
QMap<QString, QString> cssFiles;
QDir dir;
QStringList filter;
filter << QString::fromLatin1("*.qss");
filter << QString::fromLatin1("*.css");
QFileInfoList fileNames;
// read from user, resource and built-in directory
QStringList qssPaths = QDir::searchPaths(QString::fromLatin1("qss"));
for (QStringList::iterator it = qssPaths.begin(); it != qssPaths.end(); ++it) {
dir.setPath(*it);
fileNames = dir.entryInfoList(filter, QDir::Files, QDir::Name);
for (QFileInfoList::iterator jt = fileNames.begin(); jt != fileNames.end(); ++jt) {
if (cssFiles.find(jt->baseName()) == cssFiles.end()) {
cssFiles[jt->baseName()] = jt->absoluteFilePath();
}
}
}
// now add all unique items
ui->StyleSheets->addItem(tr("No style sheet"), QString::fromLatin1(""));
for (QMap<QString, QString>::iterator it = cssFiles.begin(); it != cssFiles.end(); ++it) {
ui->StyleSheets->addItem(it.key(), it.value());
}
this->selectedStyleSheet = QString::fromLatin1(hGrp->GetASCII("StyleSheet").c_str());
index = ui->StyleSheets->findData(this->selectedStyleSheet);
if (index > -1) ui->StyleSheets->setCurrentIndex(index);
}
void DlgGeneralImp::changeEvent(QEvent *e)
{
if (e->type() == QEvent::LanguageChange) {
ui->retranslateUi(this);
}
else {
QWidget::changeEvent(e);
}
}
#include "moc_DlgGeneralImp.cpp"