Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / editors / sc-ide / core / settings / manager.cpp
blob78d0734aedf60f9c9230840dd2f9a0331a703444
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 "manager.hpp"
22 #include "serialization.hpp"
24 #include <QApplication>
25 #include <QPalette>
26 #include <QTextCharFormat>
28 namespace ScIDE { namespace Settings {
30 Manager::Manager( const QString & filename, QObject * parent ):
31 QObject(parent),
32 mSettings( new QSettings(filename, serializationFormat(), this) )
34 initDefaults();
37 void Manager::initDefaults()
39 QPalette appPlt( QApplication::palette() );
41 beginGroup("IDE");
43 setDefault("startWithSession", "last");
45 beginGroup("interpreter");
46 setDefault("autoStart", true);
47 endGroup();
49 beginGroup("editor");
51 setDefault("spaceIndent", false);
52 setDefault("indentWidth", 4);
53 setDefault("stepForwardEvaluation", false);
55 setDefault("blinkDuration", 600);
56 setDefault("postWindowScrollback", 1000);
58 setDefault("font/family", "monospace");
59 setDefault("font/antialias", true);
61 beginGroup("colors");
63 QTextCharFormat matchingBracketsFormat;
64 matchingBracketsFormat.setForeground(Qt::red);
65 matchingBracketsFormat.setFontWeight(QFont::Bold);
66 setDefault("matchingBrackets", QVariant::fromValue(matchingBracketsFormat));
68 QTextCharFormat evaluatedCodeFormat;
69 evaluatedCodeFormat.setBackground(QColor("#F8A200"));
70 evaluatedCodeFormat.setForeground(Qt::black);
71 setDefault("evaluatedCode", QVariant::fromValue(evaluatedCodeFormat));
73 endGroup(); // colors
75 beginGroup("highlighting");
76 initHighlightingDefaults();
77 endGroup(); // highlighting
79 endGroup(); // editor
81 endGroup(); // IDE
84 inline static QVariant makeHlFormat
85 ( const QBrush &fg, QFont::Weight w = QFont::Normal )
87 QTextCharFormat f;
88 f.setForeground(fg);
89 if(w != QFont::Normal)
90 f.setFontWeight(w);
91 return QVariant::fromValue<QTextCharFormat>(f);
94 void Manager::initHighlightingDefaults()
96 QPalette plt( QApplication::palette() );
97 QColor base = plt.color(QPalette::Base);
98 int shade = (base.red() + base.green() + base.blue() < 380) ? 160 : 100;
100 setDefault( "keyword", makeHlFormat( QColor(0,0,230).lighter(shade), QFont::Bold ) );
101 setDefault( "built-in", makeHlFormat( QColor(51,51,191).lighter(shade) ) );
102 setDefault( "env-var", makeHlFormat( QColor(255,102,0).lighter(shade) ) );
103 setDefault( "class", makeHlFormat( QColor(0,0,210).lighter(shade) ) );
104 setDefault( "number", makeHlFormat( QColor(152,0,153).lighter(shade) ) );
105 setDefault( "symbol", makeHlFormat( QColor(0,115,0).lighter(shade) ) );
106 setDefault( "string", makeHlFormat( QColor(95,95,95).lighter(shade) ) );
107 setDefault( "char", makeHlFormat( QColor(0,115,0).lighter(shade) ) );
108 setDefault( "comment", makeHlFormat( QColor(191,0,0).lighter(shade) ) );
109 setDefault( "primitive", makeHlFormat( QColor(51,51,191).lighter(shade) ) );
112 bool Manager::contains ( const QString & key ) const
114 if ( mSettings->contains(key) )
115 return true;
116 else
117 return mDefaults.contains( resolvedKey(key) );
120 QVariant Manager::value ( const QString & key ) const
122 if ( mSettings->contains(key) )
123 return mSettings->value(key);
124 else
125 return mDefaults.value(resolvedKey(key));
128 void Manager::setValue ( const QString & key, const QVariant & value )
130 mSettings->setValue(key, value);
133 QKeySequence Manager::shortcut( const QString & key )
135 return QKeySequence( value(key).toString() );
138 void Manager::addAction ( QAction *action )
140 mActions.append(action);
141 QString key = keyForAction(action);
143 beginGroup("IDE/shortcuts");
145 setDefault( key, QVariant::fromValue<QKeySequence>(action->shortcut()) );
146 action->setShortcut( value(key).value<QKeySequence>() );
148 endGroup();
151 QString Manager::keyForAction ( QAction *action )
153 return action->text().toLower().remove('&').replace(' ', '_');
156 QFont Manager::codeFont()
158 QString fontFamily = value("IDE/editor/font/family").toString();
159 int fontSize = value("IDE/editor/font/size").toInt();
160 bool fontAntialas = value("IDE/editor/font/antialias").toBool();
162 QFont font = QApplication::font("QPlainTextEdit");
163 font.setStyleHint(QFont::TypeWriter);
164 font.setFamily(fontFamily);
165 if (fontSize > 0)
166 font.setPointSize(fontSize);
168 if (!fontAntialas)
169 font.setStyleStrategy(QFont::StyleStrategy(font.styleStrategy() | QFont::NoAntialias));
171 return font;
174 }} // namespace ScIDE::Settings