| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <QFontDatabase> |
| 25 |
|
| 26 |
|
| 27 |
#include <Base/Color.h> |
| 28 |
#include <Gui/PythonEditor.h> |
| 29 |
#include <Gui/Tools.h> |
| 30 |
|
| 31 |
#include "DlgSettingsEditor.h" |
| 32 |
#include "ui_DlgSettingsEditor.h" |
| 33 |
|
| 34 |
|
| 35 |
using namespace Gui; |
| 36 |
using namespace Gui::Dialog; |
| 37 |
|
| 38 |
namespace Gui |
| 39 |
{ |
| 40 |
namespace Dialog |
| 41 |
{ |
| 42 |
struct DlgSettingsEditorP |
| 43 |
{ |
| 44 |
QVector<QPair<QString, unsigned int>> colormap; // Color map |
| 45 |
}; |
| 46 |
} // namespace Dialog |
| 47 |
} // namespace Gui |
| 48 |
|
| 49 |
namespace |
| 50 |
{ |
| 51 |
|
| 52 |
/** Get the system-preferred fixed-width font. */ |
| 53 |
QFont getMonospaceFont() |
| 54 |
{ |
| 55 |
return QFontDatabase::systemFont(QFontDatabase::FixedFont); |
| 56 |
} |
| 57 |
} // namespace |
| 58 |
|
| 59 |
/* TRANSLATOR Gui::Dialog::DlgSettingsEditor */ |
| 60 |
|
| 61 |
/** |
| 62 |
* Constructs a DlgSettingsEditor which is a child of 'parent', with the |
| 63 |
* name 'name' and widget flags set to 'f' |
| 64 |
* |
| 65 |
* The dialog will by default be modeless, unless you set 'modal' to |
| 66 |
* true to construct a modal dialog. |
| 67 |
*/ |
| 68 |
DlgSettingsEditor::DlgSettingsEditor(QWidget* parent) |
| 69 |
: PreferencePage(parent) |
| 70 |
, ui(new Ui_DlgSettingsEditor) |
| 71 |
{ |
| 72 |
ui->setupUi(this); |
| 73 |
ui->EnableFolding->hide(); // Switch off until we have an editor with folding |
| 74 |
|
| 75 |
setupConnections(); |
| 76 |
|
| 77 |
ui->textEdit1->setTabStopDistance(40.0); |
| 78 |
|
| 79 |
d = new DlgSettingsEditorP(); |
| 80 |
QColor col; |
| 81 |
col = qApp->palette().windowText().color(); |
| 82 |
unsigned int lText = Base::Color::asPackedRGB<QColor>(col); |
| 83 |
d->colormap.push_back(QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Text")), lText)); |
| 84 |
|
| 85 |
unsigned int lBookmarks = Base::Color::asPackedRGB<QColor>(QColor(Qt::cyan)); |
| 86 |
d->colormap.push_back( |
| 87 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Bookmark")), lBookmarks) |
| 88 |
); |
| 89 |
|
| 90 |
unsigned int lBreakpnts = Base::Color::asPackedRGB<QColor>(QColor(Qt::red)); |
| 91 |
d->colormap.push_back( |
| 92 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Breakpoint")), lBreakpnts) |
| 93 |
); |
| 94 |
|
| 95 |
unsigned int lKeywords = Base::Color::asPackedRGB<QColor>(QColor(Qt::blue)); |
| 96 |
d->colormap.push_back( |
| 97 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Keyword")), lKeywords) |
| 98 |
); |
| 99 |
|
| 100 |
unsigned int lComments = Base::Color::asPackedRGB<QColor>(QColor(0, 170, 0)); |
| 101 |
d->colormap.push_back( |
| 102 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Comment")), lComments) |
| 103 |
); |
| 104 |
|
| 105 |
unsigned int lBlockCom = Base::Color::asPackedRGB<QColor>(QColor(160, 160, 164)); |
| 106 |
d->colormap.push_back( |
| 107 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Block comment")), lBlockCom) |
| 108 |
); |
| 109 |
|
| 110 |
unsigned int lNumbers = Base::Color::asPackedRGB<QColor>(QColor(Qt::blue)); |
| 111 |
d->colormap.push_back( |
| 112 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Number")), lNumbers) |
| 113 |
); |
| 114 |
|
| 115 |
unsigned int lStrings = Base::Color::asPackedRGB<QColor>(QColor(Qt::red)); |
| 116 |
d->colormap.push_back( |
| 117 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("String")), lStrings) |
| 118 |
); |
| 119 |
|
| 120 |
unsigned int lCharacter = Base::Color::asPackedRGB<QColor>(QColor(Qt::red)); |
| 121 |
d->colormap.push_back( |
| 122 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Character")), lCharacter) |
| 123 |
); |
| 124 |
|
| 125 |
unsigned int lClass = Base::Color::asPackedRGB<QColor>(QColor(255, 170, 0)); |
| 126 |
d->colormap.push_back( |
| 127 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Class name")), lClass) |
| 128 |
); |
| 129 |
|
| 130 |
unsigned int lDefine = Base::Color::asPackedRGB<QColor>(QColor(255, 170, 0)); |
| 131 |
d->colormap.push_back( |
| 132 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Define name")), lDefine) |
| 133 |
); |
| 134 |
|
| 135 |
unsigned int lOperat = Base::Color::asPackedRGB<QColor>(QColor(160, 160, 164)); |
| 136 |
d->colormap.push_back( |
| 137 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Operator")), lOperat) |
| 138 |
); |
| 139 |
|
| 140 |
unsigned int lPyOutput = Base::Color::asPackedRGB<QColor>(QColor(170, 170, 127)); |
| 141 |
d->colormap.push_back( |
| 142 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Python output")), lPyOutput) |
| 143 |
); |
| 144 |
|
| 145 |
unsigned int lPyError = Base::Color::asPackedRGB<QColor>(QColor(Qt::red)); |
| 146 |
d->colormap.push_back( |
| 147 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Python error")), lPyError) |
| 148 |
); |
| 149 |
|
| 150 |
unsigned int lCLine = Base::Color::asPackedRGB<QColor>(QColor(224, 224, 224)); |
| 151 |
d->colormap.push_back( |
| 152 |
QPair<QString, unsigned int>(QString::fromLatin1(QT_TR_NOOP("Current line highlight")), lCLine) |
| 153 |
); |
| 154 |
|
| 155 |
QStringList labels; |
| 156 |
labels << tr("Items"); |
| 157 |
ui->displayItems->setHeaderLabels(labels); |
| 158 |
ui->displayItems->header()->hide(); |
| 159 |
for (const auto& [textType, textColor] : d->colormap) { |
| 160 |
auto item = new QTreeWidgetItem(ui->displayItems); |
| 161 |
item->setText(0, tr(textType.toLatin1())); |
| 162 |
} |
| 163 |
pythonSyntax = new PythonSyntaxHighlighter(ui->textEdit1); |
| 164 |
pythonSyntax->setDocument(ui->textEdit1->document()); |
| 165 |
} |
| 166 |
|
| 167 |
/** Destroys the object and frees any allocated resources */ |
| 168 |
DlgSettingsEditor::~DlgSettingsEditor() |
| 169 |
{ |
| 170 |
// no need to delete child widgets, Qt does it all for us |
| 171 |
delete pythonSyntax; |
| 172 |
delete d; |
| 173 |
} |
| 174 |
|
| 175 |
void DlgSettingsEditor::setupConnections() |
| 176 |
{ |
| 177 |
// clang-format off |
| 178 |
connect(ui->displayItems, &QTreeWidget::currentItemChanged, |
| 179 |
this, &DlgSettingsEditor::onDisplayItemsCurrentItemChanged); |
| 180 |
connect(ui->colorButton, &ColorButton::changed, |
| 181 |
this, &DlgSettingsEditor::onColorButtonChanged); |
| 182 |
connect(ui->fontFamily, &QComboBox::textActivated, |
| 183 |
this, &DlgSettingsEditor::onFontFamilyActivated); |
| 184 |
connect(ui->fontSize, &PrefSpinBox::textChanged, |
| 185 |
this, &DlgSettingsEditor::onFontSizeValueChanged); |
| 186 |
// clang-format on |
| 187 |
} |
| 188 |
|
| 189 |
/** Searches for the color value corresponding to \e name in current editor |
| 190 |
* settings ColorMap and assigns it to the color button |
| 191 |
* @see Gui::ColorButton |
| 192 |
*/ |
| 193 |
void DlgSettingsEditor::onDisplayItemsCurrentItemChanged(QTreeWidgetItem* item) |
| 194 |
{ |
| 195 |
int index = ui->displayItems->indexOfTopLevelItem(item); |
| 196 |
unsigned int col = d->colormap[index].second; |
| 197 |
ui->colorButton->setColor(Base::Color::fromPackedRGB<QColor>(col)); |
| 198 |
} |
| 199 |
|
| 200 |
/** Updates the color map if a color was changed */ |
| 201 |
void DlgSettingsEditor::onColorButtonChanged() |
| 202 |
{ |
| 203 |
QColor col = ui->colorButton->color(); |
| 204 |
unsigned int lcol = Base::Color::asPackedRGB<QColor>(col); |
| 205 |
|
| 206 |
int index = ui->displayItems->indexOfTopLevelItem(ui->displayItems->currentItem()); |
| 207 |
d->colormap[index].second = lcol; |
| 208 |
pythonSyntax->setColor(d->colormap[index].first, col); |
| 209 |
} |
| 210 |
|
| 211 |
void DlgSettingsEditor::setEditorTabWidth(int tabWidth) |
| 212 |
{ |
| 213 |
QFontMetrics metric(font()); |
| 214 |
int fontSize = QtTools::horizontalAdvance(metric, QLatin1Char('0')); |
| 215 |
ui->textEdit1->setTabStopDistance(tabWidth * fontSize); |
| 216 |
} |
| 217 |
|
| 218 |
void DlgSettingsEditor::saveSettings() |
| 219 |
{ |
| 220 |
ui->EnableLineNumber->onSave(); |
| 221 |
ui->EnableBlockCursor->onSave(); |
| 222 |
ui->EnableFolding->onSave(); |
| 223 |
ui->tabSize->onSave(); |
| 224 |
ui->indentSize->onSave(); |
| 225 |
ui->radioTabs->onSave(); |
| 226 |
ui->radioSpaces->onSave(); |
| 227 |
|
| 228 |
// Saves the color map |
| 229 |
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("Editor"); |
| 230 |
for (const auto& [textType, textColor] : d->colormap) { |
| 231 |
auto col = static_cast<unsigned long>(textColor); |
| 232 |
hGrp->SetUnsigned(textType.toLatin1(), col); |
| 233 |
} |
| 234 |
hGrp->SetInt("FontSize", ui->fontSize->value()); |
| 235 |
hGrp->SetASCII("Font", ui->fontFamily->currentText().toLatin1()); |
| 236 |
|
| 237 |
setEditorTabWidth(ui->tabSize->value()); |
| 238 |
} |
| 239 |
|
| 240 |
void DlgSettingsEditor::loadSettings() |
| 241 |
{ |
| 242 |
ui->EnableLineNumber->onRestore(); |
| 243 |
ui->EnableBlockCursor->onRestore(); |
| 244 |
ui->EnableFolding->onRestore(); |
| 245 |
ui->tabSize->onRestore(); |
| 246 |
ui->indentSize->onRestore(); |
| 247 |
ui->radioTabs->onRestore(); |
| 248 |
ui->radioSpaces->onRestore(); |
| 249 |
|
| 250 |
setEditorTabWidth(ui->tabSize->value()); |
| 251 |
ui->textEdit1->setPlainText(QStringLiteral( |
| 252 |
"# Short Python sample\n" |
| 253 |
"import sys\n" |
| 254 |
"\n" |
| 255 |
"def foo(begin, end):\n" |
| 256 |
" i = begin\n" |
| 257 |
" while i < end:\n" |
| 258 |
" print(i)\n" |
| 259 |
" i = i + 1\n" |
| 260 |
" print(\"Text\")\n" |
| 261 |
" return None\n" |
| 262 |
"\n" |
| 263 |
"foo(0, 20)\n" |
| 264 |
)); |
| 265 |
|
| 266 |
// Restores the color map |
| 267 |
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("Editor"); |
| 268 |
for (auto& [textType, textColor] : d->colormap) { |
| 269 |
auto col = static_cast<unsigned long>(textColor); |
| 270 |
col = hGrp->GetUnsigned(textType.toLatin1(), col); |
| 271 |
textColor = static_cast<unsigned int>(col); |
| 272 |
QColor color = Base::Color::fromPackedRGB<QColor>(col); |
| 273 |
pythonSyntax->setColor(textType, color); |
| 274 |
} |
| 275 |
|
| 276 |
// fill up font styles |
| 277 |
// |
| 278 |
ui->fontSize->setValue(10); |
| 279 |
ui->fontSize->setValue(hGrp->GetInt("FontSize", ui->fontSize->value())); |
| 280 |
|
| 281 |
QByteArray defaultMonospaceFont = getMonospaceFont().family().toLatin1(); |
| 282 |
|
| 283 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 284 |
QStringList familyNames = QFontDatabase().families(QFontDatabase::Any); |
| 285 |
QStringList fixedFamilyNames; |
| 286 |
for (const auto& name : familyNames) { |
| 287 |
if (QFontDatabase().isFixedPitch(name)) { |
| 288 |
// cursor.pcf was removed to cope with a problem with the Qt Font Manager |
| 289 |
// See https://github.com/FreeCAD/FreeCAD/issues/10514 for details |
| 290 |
if (name.compare(QLatin1String("8514oem"), Qt::CaseInsensitive) != 0 |
| 291 |
&& name.compare(QLatin1String("cursor.pcf"), Qt::CaseInsensitive) != 0) { |
| 292 |
fixedFamilyNames.append(name); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
#else |
| 297 |
QStringList familyNames = QFontDatabase::families(QFontDatabase::Any); |
| 298 |
QStringList fixedFamilyNames; |
| 299 |
for (const auto& name : familyNames) { |
| 300 |
if (QFontDatabase::isFixedPitch(name)) { |
| 301 |
// cursor.pcf was removed to cope with a problem with the Qt Font Manager |
| 302 |
// See https://github.com/FreeCAD/FreeCAD/issues/10514 for details |
| 303 |
if (name.compare(QLatin1String("8514oem"), Qt::CaseInsensitive) != 0 |
| 304 |
&& name.compare(QLatin1String("cursor.pcf"), Qt::CaseInsensitive) != 0) { |
| 305 |
fixedFamilyNames.append(name); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
#endif |
| 310 |
ui->fontFamily->addItems(fixedFamilyNames); |
| 311 |
int index = fixedFamilyNames.indexOf( |
| 312 |
QString::fromLatin1(hGrp->GetASCII("Font", defaultMonospaceFont).c_str()) |
| 313 |
); |
| 314 |
if (index < 0) { |
| 315 |
index = 0; |
| 316 |
} |
| 317 |
ui->fontFamily->setCurrentIndex(index); |
| 318 |
onFontFamilyActivated(ui->fontFamily->currentText()); |
| 319 |
ui->displayItems->setCurrentItem(ui->displayItems->topLevelItem(0)); |
| 320 |
} |
| 321 |
|
| 322 |
void DlgSettingsEditor::resetSettingsToDefaults() |
| 323 |
{ |
| 324 |
ParameterGrp::handle hGrp; |
| 325 |
hGrp = WindowParameter::getDefaultParameter()->GetGroup("Editor"); |
| 326 |
// reset the parameters in the "Editor" group |
| 327 |
for (const auto& [textType, textColor] : d->colormap) { |
| 328 |
hGrp->RemoveUnsigned(textType.toLatin1()); |
| 329 |
} |
| 330 |
// reset "FontSize" parameter |
| 331 |
hGrp->RemoveInt("FontSize"); |
| 332 |
// reset "Font" parameter |
| 333 |
hGrp->RemoveASCII("Font"); |
| 334 |
|
| 335 |
// finally reset all the parameters associated to Gui::Pref* widgets |
| 336 |
PreferencePage::resetSettingsToDefaults(); |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Sets the strings of the subwidgets using the current language. |
| 341 |
*/ |
| 342 |
void DlgSettingsEditor::changeEvent(QEvent* e) |
| 343 |
{ |
| 344 |
if (e->type() == QEvent::LanguageChange) { |
| 345 |
int index = 0; |
| 346 |
for (const auto& [textType, textColor] : d->colormap) { |
| 347 |
ui->displayItems->topLevelItem(index++)->setText(0, tr(textType.toLatin1())); |
| 348 |
} |
| 349 |
ui->retranslateUi(this); |
| 350 |
} |
| 351 |
else { |
| 352 |
QWidget::changeEvent(e); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
void DlgSettingsEditor::onFontFamilyActivated(const QString& fontFamily) |
| 357 |
{ |
| 358 |
int fontSize = ui->fontSize->value(); |
| 359 |
QFont ft(fontFamily, fontSize); |
| 360 |
ui->textEdit1->setFont(ft); |
| 361 |
} |
| 362 |
|
| 363 |
void DlgSettingsEditor::onFontSizeValueChanged(const QString&) |
| 364 |
{ |
| 365 |
onFontFamilyActivated(ui->fontFamily->currentText()); |
| 366 |
} |
| 367 |
|
| 368 |
#include "moc_DlgSettingsEditor.cpp" |