]> SALOME platform Git repositories - modules/gui.git/blob - src/PyEditor/PyEditor_Settings.cxx
Salome HOME
Integrates the python editor as new SALOME viewer. Writes comments. Designs icons...
[modules/gui.git] / src / PyEditor / PyEditor_Settings.cxx
1 // Copyright (C) 2015 OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // File   : PyEditor_Settings.cxx
20 // Author : Maxim GLIBIN, Open CASCADE S.A.S. (maxim.glibin@opencascade.com)
21 //
22
23 #include "PyEditor_Settings.h"
24
25 #include <QtxResourceMgr.h>
26
27 #include <QDir>
28 #include <QFile>
29 #include <QSettings>
30
31 /*!
32   \class PyEditor_Settings
33   \brief Manager of setting values.
34 */
35
36 /*!
37   \brief Constructor.
38   \param isSingle flag determined single application or reccesed.
39 */
40 PyEditor_Settings::PyEditor_Settings( QtxResourceMgr* theMgr, bool isSingle )
41     : p_HighlightCurrentLine( true ),
42       p_LineNumberArea( true ),
43       p_TextWrapping( false ),
44       p_CenterCursorOnScroll( true ),
45       p_TabSpaceVisible( true ),
46       p_TabSize( 4 ),
47       p_VerticalEdge( true ),
48       p_NumberColumns( 80 ),
49       p_Font( "Courier", 10 ),
50       m_ResourceMgr(theMgr),
51       m_Single(isSingle)
52
53 {
54   if ( m_Single )
55   {
56     m_Settings = new QSettings( "config.ini", QSettings::IniFormat );
57     if ( !QFile::exists( m_Settings->fileName() ) )
58       toSettings( PY_EDITOR );
59   }
60
61   readSettings();
62 }
63
64 /*!
65   \brief Reads the setting values.
66  */
67 void PyEditor_Settings::readSettings()
68 {
69   if ( isSingle() )
70     fromSettings( PY_EDITOR );
71   else
72     readPreferences();
73 }
74
75 /*!
76   \brief Writes the setting values.
77  */
78 void PyEditor_Settings::writeSettings()
79 {
80   if ( isSingle() )
81     toSettings( PY_EDITOR );
82   else
83     writePreferences();
84 }
85
86 /*!
87   \return \c true if the application is single
88  */
89 bool PyEditor_Settings::isSingle()
90 {
91   return m_Single;
92 }
93
94 /*!
95   \brief Loads the setting values from file settings.
96  */
97 void PyEditor_Settings::fromSettings( const QString &theCategory )
98 {
99   QString aGroup = theCategory;
100   aGroup += "/";
101
102   p_HighlightCurrentLine = m_Settings->value(aGroup + QLatin1String( HIGHLIGHT_CURRLINE ), p_HighlightCurrentLine).toBool();
103   p_LineNumberArea       = m_Settings->value(aGroup + QLatin1String( LINE_NUMBER_AREA ), p_LineNumberArea).toBool();
104   p_TextWrapping         = m_Settings->value(aGroup + QLatin1String( TEXT_WRAP ), p_TextWrapping).toBool();
105   p_CenterCursorOnScroll = m_Settings->value(aGroup + QLatin1String( CURSOR_SCROLL ), p_CenterCursorOnScroll).toBool();
106   p_TabSpaceVisible      = m_Settings->value(aGroup + QLatin1String( TAB_WHITESPACES ), p_TabSpaceVisible).toBool();
107   p_TabSize              = m_Settings->value(aGroup + QLatin1String( TAB_SIZE ), p_TabSize).toInt();
108   p_VerticalEdge         = m_Settings->value(aGroup + QLatin1String( VERTICAL_EDGE ), p_VerticalEdge).toBool();
109   p_NumberColumns        = m_Settings->value(aGroup + QLatin1String( NUM_COLUMNS ), p_NumberColumns).toInt();
110   p_Font = QFont( m_Settings->value(aGroup + QLatin1String( FONT_FAMILY ), p_Font.family()).toString(),
111     m_Settings->value(aGroup + QLatin1String( FONT_SIZE ), p_Font.pointSize()).toInt() );
112 }
113
114 /*!
115   \brief Saves the setting values into file settings.
116  */
117 void PyEditor_Settings::toSettings( const QString &theCategory ) const
118 {
119   m_Settings->beginGroup( theCategory );
120   m_Settings->setValue( QLatin1String( HIGHLIGHT_CURRLINE ), p_HighlightCurrentLine );
121   m_Settings->setValue( QLatin1String( LINE_NUMBER_AREA ),   p_LineNumberArea );
122   m_Settings->setValue( QLatin1String( TEXT_WRAP ),          p_TextWrapping );
123   m_Settings->setValue( QLatin1String( CURSOR_SCROLL ),      p_CenterCursorOnScroll );
124   m_Settings->setValue( QLatin1String( TAB_WHITESPACES ),    p_TabSpaceVisible );
125   m_Settings->setValue( QLatin1String( TAB_SIZE ),           p_TabSize );
126   m_Settings->setValue( QLatin1String( VERTICAL_EDGE ),      p_VerticalEdge );
127   m_Settings->setValue( QLatin1String( NUM_COLUMNS ),        p_NumberColumns );
128   m_Settings->setValue( QLatin1String( FONT_FAMILY ),        p_Font.family() );
129   m_Settings->setValue( QLatin1String( FONT_SIZE ),          p_Font.pointSize() );
130   m_Settings->endGroup();
131 }
132
133 /*!
134   \brief Loads the setting values from setting resources.
135  */
136 void PyEditor_Settings::readPreferences()
137 {
138   if(m_ResourceMgr) 
139     {
140       p_HighlightCurrentLine = m_ResourceMgr->booleanValue( "PyEditor", "HighlightCurrentLine", p_HighlightCurrentLine );
141       p_LineNumberArea       = m_ResourceMgr->booleanValue( "PyEditor", "LineNumberArea", p_LineNumberArea );
142       p_TextWrapping         = m_ResourceMgr->booleanValue( "PyEditor", "TextWrapping", p_TextWrapping );
143       p_CenterCursorOnScroll = m_ResourceMgr->booleanValue( "PyEditor", "CenterCursorOnScroll", p_CenterCursorOnScroll );
144       p_TabSpaceVisible      = m_ResourceMgr->booleanValue( "PyEditor", "TabSpaceVisible", p_TabSpaceVisible );
145       p_TabSize              = m_ResourceMgr->integerValue( "PyEditor", "TabSize", p_TabSize );
146       p_VerticalEdge         = m_ResourceMgr->booleanValue( "PyEditor", "VerticalEdge", p_VerticalEdge );
147       p_NumberColumns        = m_ResourceMgr->integerValue( "PyEditor", "NumberColumns", p_NumberColumns );
148       p_Font                 = m_ResourceMgr->fontValue( "PyEditor", "Font", p_Font );
149     }
150 }
151
152 /*!
153   \brief Saves the setting values into setting resources.
154  */
155 void PyEditor_Settings::writePreferences()
156 {
157   if(m_ResourceMgr) 
158     {
159       m_ResourceMgr->setValue( "PyEditor", "HighlightCurrentLine", p_HighlightCurrentLine );
160       m_ResourceMgr->setValue( "PyEditor", "LineNumberArea", p_LineNumberArea );
161       m_ResourceMgr->setValue( "PyEditor", "TextWrapping", p_TextWrapping );
162       m_ResourceMgr->setValue( "PyEditor", "CenterCursorOnScroll", p_CenterCursorOnScroll );
163       m_ResourceMgr->setValue( "PyEditor", "TabSpaceVisible", p_TabSpaceVisible );
164       m_ResourceMgr->setValue( "PyEditor", "TabSize", p_TabSize );
165       m_ResourceMgr->setValue( "PyEditor", "VerticalEdge", p_VerticalEdge );
166       m_ResourceMgr->setValue( "PyEditor", "NumberColumns", p_NumberColumns );
167       m_ResourceMgr->setValue( "PyEditor", "Font", p_Font );
168     }
169 }