Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / editors / sc-ide / widgets / settings / editor_page.cpp
blob5eb2a90c2d156aa4532ee517a078a0230bc0f5ad
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program 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 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "editor_page.hpp"
22 #include "ui_settings_editor.h"
23 #include "../code_editor/highlighter.hpp"
24 #include "../../core/settings/manager.hpp"
25 #include "../../core/main.hpp"
27 #include <QMenu>
28 #include <QDebug>
29 #include <QListWidgetItem>
30 #include <QFontDatabase>
32 namespace ScIDE { namespace Settings {
34 EditorPage::EditorPage(QWidget *parent) :
35 QWidget(parent),
36 fontDatabase(new QFontDatabase),
37 ui( new Ui::EditorConfigPage )
39 ui->setupUi(this);
41 connect( ui->tabs, SIGNAL(currentChanged(int)), this, SLOT(onCurrentTabChanged(int)) );
43 connect( ui->onlyMonoFonts, SIGNAL(toggled(bool)), this, SLOT(onMonospaceToggle(bool)) );
44 connect( ui->fontCombo, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateFontPreview()) );
45 connect( ui->fontSize, SIGNAL(valueChanged(int)), this, SLOT(updateFontPreview()) );
46 connect( ui->fontAntialias, SIGNAL(stateChanged(int)), this, SLOT(updateFontPreview()) );
48 connect( ui->textFormats, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem*)),
49 this, SLOT(updateTextFormatEdit()) );
50 connect( ui->fgPicker, SIGNAL(colorPicked(QColor)),
51 this, SLOT(updateTextFormatDisplay()) );
52 connect( ui->bgPicker, SIGNAL(colorPicked(QColor)),
53 this, SLOT(updateTextFormatDisplay()) );
54 connect( ui->italicOption, SIGNAL(clicked()),
55 this, SLOT(updateTextFormatDisplay()) );
56 connect( ui->boldOption, SIGNAL(clicked()),
57 this, SLOT(updateTextFormatDisplay()) );
58 connect( ui->fgClearBtn, SIGNAL(clicked()), ui->fgPicker, SLOT(clear()) );
59 connect( ui->fgClearBtn, SIGNAL(clicked()),
60 this, SLOT(updateTextFormatDisplay()) );
61 connect( ui->bgClearBtn, SIGNAL(clicked()), ui->bgPicker, SLOT(clear()) );
62 connect( ui->bgClearBtn, SIGNAL(clicked()),
63 this, SLOT(updateTextFormatDisplay()) );
65 updateTextFormatEdit();
68 EditorPage::~EditorPage()
70 delete ui;
71 delete fontDatabase;
74 void EditorPage::load( Manager *s )
76 s->beginGroup("IDE/editor");
78 ui->spaceIndent->setChecked( s->value("spaceIndent").toBool() );
79 ui->indentWidth->setValue( s->value("indentWidth").toInt() );
80 ui->stepForwardEvaluation->setChecked( s->value("stepForwardEvaluation").toBool() );
82 ui->blinkDuration->setValue( s->value("blinkDuration").toInt() );
83 ui->postWindowScrollback->setValue( s->value("postWindowScrollback").toInt() );
85 s->beginGroup("font");
86 QString fontFamily = s->value("family").toString();
87 int fontSize = s->value("size").toInt();
88 ui->fontAntialias->setChecked( s->value("antialias").toBool() );
90 s->endGroup();
92 // Display info about the font that would actually be used:
93 QFont defaultFont = QApplication::font("QPlainTextEdit");
94 defaultFont.setFamily(fontFamily);
95 if (fontSize > 0)
96 defaultFont.setPointSize(fontSize);
97 defaultFont.setStyleHint( QFont::TypeWriter );
99 QFontInfo fontInfo( defaultFont );
100 fontFamily = fontInfo.family();
101 fontSize = fontInfo.pointSize();
103 populateFontList( ui->onlyMonoFonts->isChecked() );
104 int fontFamilyIndex = ui->fontCombo->findText( fontFamily, Qt::MatchFixedString );
105 ui->fontCombo->setCurrentIndex( fontFamilyIndex );
107 ui->fontSize->setValue( fontSize );
109 ui->textFormats->clear();
111 s->beginGroup("colors");
112 loadGeneralTextFormats( s );
113 s->endGroup(); // colors
115 s->beginGroup("highlighting");
116 loadSyntaxTextFormats( s );
117 s->endGroup(); // highlighting
119 s->endGroup(); // IDE/editor
121 updateFontPreview();
122 updateTextFormatDisplayCommons();
124 ui->textFormats->expandAll();
127 void EditorPage::loadGeneralTextFormats( Manager *settings )
129 mGeneralFormatsItem = new QTreeWidgetItem( ui->textFormats );
130 mGeneralFormatsItem->setText(0, "General" );
132 // commont text format item is special - don't set foreground and background on the item!
133 mCommonTextFormatItem = new QTreeWidgetItem();
134 mCommonTextFormatItem->setText( 0, "Text" );
135 mCommonTextFormatItem->setData( 0, TextFormatConfigKeyRole, "text" );
136 mCommonTextFormatItem->setData( 0, TextFormatRole, settings->value( "text" ) );
137 mGeneralFormatsItem->addChild( mCommonTextFormatItem );
139 static char const * const keys[] = {
140 "matchingBrackets", "evaluatedCode", "lineNumbers"
143 static char const * const strings[] = {
144 "Matching Brackets", "Evaluated Code", "Line Numbers"
147 static int count = sizeof(keys) / sizeof(keys[0]);
149 for (int idx = 0; idx < count; ++idx) {
150 QTextCharFormat format = settings->value( keys[idx] ).value<QTextCharFormat>();
151 addTextFormat( mGeneralFormatsItem, strings[idx], keys[idx], format );
155 void EditorPage::loadSyntaxTextFormats( Manager *settings )
157 mSyntaxFormatsItem = new QTreeWidgetItem( ui->textFormats );
158 mSyntaxFormatsItem->setText(0, "Syntax Highlighting" );
160 static char const * const keys[] = {
161 "keyword", "built-in", "env-var", "class", "number",
162 "symbol", "string", "char", "comment", "primitive"
165 static char const * const strings[] = {
166 "Keyword", "Built-in Value", "Environment Variable", "Class",
167 "Number", "Symbol", "String", "Char", "Comment", "Primitive"
170 static int count = sizeof(keys) / sizeof(keys[0]);
172 for (int idx = 0; idx < count; ++idx) {
173 QTextCharFormat format = settings->value( keys[idx] ).value<QTextCharFormat>();
174 addTextFormat( mSyntaxFormatsItem, strings[idx], keys[idx], format );
178 void EditorPage::populateFontList( bool onlyMonospaced )
180 ui->fontCombo->clear();
181 QStringList fontFamilies = fontDatabase->families();
182 foreach(QString family, fontFamilies)
184 if (onlyMonospaced && !fontDatabase->isFixedPitch(family))
185 continue;
187 ui->fontCombo->addItem(family);
191 void EditorPage::store( Manager *s )
193 s->beginGroup("IDE/editor");
195 s->setValue("spaceIndent", ui->spaceIndent->isChecked());
196 s->setValue("indentWidth", ui->indentWidth->value());
197 s->setValue("stepForwardEvaluation", ui->stepForwardEvaluation->isChecked());
199 s->setValue("blinkDuration", ui->blinkDuration->value());
200 s->setValue("postWindowScrollback", ui->postWindowScrollback->value());
202 s->beginGroup("font");
203 QString fontFamily = ui->fontCombo->currentText();
204 if (!fontFamily.isEmpty())
205 s->setValue("family", fontFamily);
206 s->setValue("size", ui->fontSize->value());
207 s->setValue("antialias", ui->fontAntialias->checkState());
208 s->endGroup();
210 s->beginGroup("colors");
212 int generalFormatCount = mGeneralFormatsItem->childCount();
213 for(int i = 0; i < generalFormatCount; ++i)
215 QTreeWidgetItem *item = mGeneralFormatsItem->child(i);
216 QTextCharFormat fm = item->data( 0, TextFormatRole).value<QTextCharFormat>();
217 QString key = item->data( 0, TextFormatConfigKeyRole ).toString();
218 s->setValue( key, QVariant::fromValue<QTextCharFormat>(fm) );
221 s->endGroup(); // colors
223 s->beginGroup("highlighting");
225 int syntaxFormatCount = mSyntaxFormatsItem->childCount();
226 for(int i = 0; i < syntaxFormatCount; ++i)
228 QTreeWidgetItem *item =mSyntaxFormatsItem->child(i);
229 QTextCharFormat fm = item->data( 0, TextFormatRole).value<QTextCharFormat>();
230 QString key = item->data( 0, TextFormatConfigKeyRole ).toString();
231 s->setValue( key, QVariant::fromValue<QTextCharFormat>(fm) );
234 s->endGroup(); // highlighting
236 s->endGroup();
239 void EditorPage::onCurrentTabChanged(int)
241 if (ui->tabs->currentWidget() == ui->colorsTab)
242 updateFontPreview();
245 void EditorPage::onMonospaceToggle(bool onlyMonospaced)
247 populateFontList( onlyMonospaced );
250 void EditorPage::updateFontPreview()
252 if (ui->tabs->currentWidget() == ui->colorsTab)
253 ui->textFormats->setFont( constructFont() );
256 QFont EditorPage::constructFont()
258 QString family = ui->fontCombo->currentText();
259 int size = ui->fontSize->value();
260 bool antialias = (ui->fontAntialias->checkState() == Qt::Checked);
262 QFont font(family, size);
263 font.setStyleHint( QFont::TypeWriter );
265 if (!antialias)
266 font.setStyleStrategy(QFont::StyleStrategy(font.styleStrategy() | QFont::NoAntialias));
268 return font;
271 void EditorPage::addTextFormat
272 ( QTreeWidgetItem * parent, const QString & name, const QString &key, const QTextCharFormat & format )
274 QTreeWidgetItem *item = new QTreeWidgetItem();
275 item->setText( 0, name );
276 item->setData( 0, TextFormatConfigKeyRole, key );
277 setTextFormat( item, format );
279 if (!parent)
280 parent = ui->textFormats->invisibleRootItem();
282 parent->addChild(item);
285 void EditorPage::setTextFormat( QTreeWidgetItem *item, const QTextCharFormat & format )
287 QBrush fg = format.foreground();
288 if ( fg != Qt::NoBrush)
289 item->setForeground( 0, fg );
290 else
291 item->setData( 0, Qt::ForegroundRole, QVariant() );
293 QBrush bg = format.background();
294 if ( bg != Qt::NoBrush)
295 item->setBackground( 0, bg );
296 else
297 item->setData( 0, Qt::BackgroundRole, QVariant() );
299 QFont f;
300 if (format.hasProperty(QTextFormat::FontItalic))
301 f.setItalic( format.fontItalic() );
302 if (format.hasProperty(QTextFormat::FontWeight))
303 f.setWeight( format.fontWeight() );
305 item->setFont( 0, f );
307 item->setData( 0, TextFormatRole, QVariant::fromValue(format) );
310 QTextCharFormat EditorPage::constructTextFormat()
312 QTextCharFormat format;
314 QBrush fg = ui->fgPicker->brush();
315 if ( fg.style() != Qt::NoBrush)
316 format.setForeground(fg);
318 QBrush bg = ui->bgPicker->brush();
319 if (bg.style() != Qt::NoBrush)
320 format.setBackground(bg);
322 if (ui->italicOption->isChecked())
323 format.setFontItalic(true);
325 if (ui->boldOption->isChecked())
326 format.setFontWeight(QFont::Bold);
328 return format;
331 void EditorPage::updateTextFormatEdit()
333 QTreeWidgetItem *item = ui->textFormats->currentItem();
334 bool canEdit = item && item->data(0, TextFormatConfigKeyRole).isValid();
335 ui->textFormatEdit->setEnabled(canEdit);
337 if (!canEdit) {
338 ui->fgPicker->setBrush( QBrush() );
339 ui->bgPicker->setBrush( QBrush() );
340 ui->italicOption->setChecked(false);
341 ui->italicOption->setChecked(false);
343 else {
344 QTextCharFormat format = item->data( 0, TextFormatRole).value<QTextCharFormat>();
345 ui->fgPicker->setBrush( format.foreground() );
346 ui->bgPicker->setBrush( format.background() );
347 ui->italicOption->setChecked( format.fontItalic() );
348 ui->boldOption->setChecked( format.fontWeight() == QFont::Bold );
352 void EditorPage::updateTextFormatDisplay()
354 QTreeWidgetItem *item = ui->textFormats->currentItem();
355 bool canEdit = item && item->data(0, TextFormatConfigKeyRole).isValid();
356 if (!canEdit)
357 return;
359 QTextCharFormat format = constructTextFormat();
361 if (item != mCommonTextFormatItem)
362 setTextFormat( item, format );
363 else {
364 // treat common text format specially
365 item->setData( 0, TextFormatRole, QVariant::fromValue(format) );
366 updateTextFormatDisplayCommons();
370 void EditorPage::updateTextFormatDisplayCommons()
372 QTextCharFormat commonFormat =
373 mCommonTextFormatItem->data(0, TextFormatRole).value<QTextCharFormat>();
375 QPalette palette;
377 QBrush fg = commonFormat.foreground();
378 if ( fg != Qt::NoBrush)
379 palette.setBrush( QPalette::Text, fg );
381 QBrush bg = commonFormat.background();
382 if (bg != Qt::NoBrush)
383 palette.setBrush( QPalette::Base, bg );
385 ui->textFormats->setPalette(palette);
388 }} // namespace ScIDE::Settings