Salome HOME
updated copyright message
[modules/gui.git] / tools / PyEditor / src / PyEditor_Keywords.cxx
1 // Copyright (C) 2015-2023  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_Keywords.cxx
20 // Author : Sergey TELKOV, Open CASCADE S.A.S. (sergey.telkov@opencascade.com)
21 //
22
23 #include "PyEditor_Keywords.h"
24
25 #include <QSet>
26
27 /*!
28   \brief PyEditor_Keywords
29 */
30
31 /*!
32   \brief Constructor.
33 */
34 PyEditor_Keywords::PyEditor_Keywords( QObject* parent )
35   : QObject( parent )
36 {
37 }
38
39 /*!
40   \brief Destructor.
41 */
42 PyEditor_Keywords::~PyEditor_Keywords()
43 {
44 }
45
46
47 QList<int> PyEditor_Keywords::types() const
48 {
49   QMap<int, bool> map;
50   for ( KeywordMap::const_iterator it = myKeywords.begin(); it != myKeywords.end(); ++it ) {
51     map.insert( it.value().type, false );
52   }
53   return map.keys();
54 }
55
56 /*!
57   \brief Gets the colors list.
58   \return color list
59 */
60 QList<QColor> PyEditor_Keywords::colors() const
61 {
62   QList<QColor> list;
63   QSet<QRgb> set;
64   for ( KeywordMap::const_iterator it = myKeywords.begin(); it != myKeywords.end(); ++it ) {
65     const QColor& color = it.value().color;
66     if ( !set.contains( color.rgba() ) ) {
67       list.append( color );
68       set.insert( color.rgba() );
69     }
70   }
71   return list;
72 }
73
74 /*!
75   \brief Gets the keyword type.
76   \return type number
77 */
78 int PyEditor_Keywords::type( const QString& keyword ) const
79 {
80   return myKeywords.contains(keyword) ? myKeywords[keyword].type : -1;
81 }
82
83 /*!
84   \brief Gets the keyword color.
85   \return color
86 */
87 QColor PyEditor_Keywords::color( const QString& keyword ) const
88 {
89   return myKeywords.contains(keyword) ? myKeywords[keyword].color : QColor();
90 }
91
92 /*!
93   \brief Gets all keywords.
94   \return keywords string list
95 */
96 QStringList PyEditor_Keywords::keywords() const
97 {
98   return myKeywords.keys();
99 }
100
101 /*!
102   \brief Gets all keywords of specified type.
103   \return keywords string list
104 */
105 QStringList PyEditor_Keywords::keywords( int type ) const
106 {
107   QStringList keywords;
108   for ( KeywordMap::const_iterator it = myKeywords.begin(); it != myKeywords.end(); ++it ) {
109     if ( it.value().type == type )
110       keywords.append( it.key() );
111   }
112   return keywords;
113 }
114
115 /*!
116   \brief Gets all keywords with specified color.
117   \return keywords string list
118 */
119 QStringList PyEditor_Keywords::keywords( const QColor& color ) const
120 {
121   QStringList keywords;
122   for ( KeywordMap::const_iterator it = myKeywords.begin(); it != myKeywords.end(); ++it ) {
123     if ( it.value().color == color )
124       keywords.append( it.key() );
125   }
126   return keywords;
127 }
128
129 /*!
130   \brief Append keyword with type and color.
131 */
132 void PyEditor_Keywords::append( const QString& keyword,
133                                 int type, const QColor& color )
134 {
135   append( QStringList() << keyword, type, color );
136 }
137
138 /*!
139   \brief Append keyword list with type and color.
140 */
141 void PyEditor_Keywords::append( const QStringList& keywords,
142                                 int type, const QColor& color )
143 {
144   bool modif = false;
145   for ( QStringList::const_iterator it = keywords.begin(); it != keywords.end(); ++it ) {
146     const QString& kw = *it;
147     bool changed = false;
148     if ( !myKeywords.contains( kw ) ) {
149       myKeywords.insert( kw, KeywordInfo() );
150       changed = true;
151     }
152     KeywordInfo& info = myKeywords[kw];
153     changed = changed || info.type != type || info.color != color;
154     info.type = type;
155     info.color = color;
156
157     modif = modif || changed;
158   }
159
160   if ( modif )
161     Q_EMIT keywordsChanged();
162 }
163
164 /*!
165   \brief Remove all keywords with specified type.
166 */
167 void PyEditor_Keywords::remove( int type )
168 {
169   remove( keywords( type ) );
170 }
171
172 /*!
173   \brief Remove keyword.
174 */
175 void PyEditor_Keywords::remove( const QString& keyword )
176 {
177   remove( QStringList() << keyword );
178 }
179
180 /*!
181   \brief Remove keywords.
182 */
183 void PyEditor_Keywords::remove( const QStringList& keywords )
184 {
185   bool changed = false;
186   for ( QStringList::const_iterator it = keywords.begin(); it != keywords.end(); ++it ) {
187     if ( myKeywords.contains( *it ) ) {
188       myKeywords.remove( *it );
189       changed = true;
190     }
191   }
192   if ( changed )
193     Q_EMIT keywordsChanged();
194 }
195
196 /*!
197   \brief Remove all keywords.
198 */
199 void PyEditor_Keywords::clear()
200 {
201   if ( !myKeywords.isEmpty() ) {
202     myKeywords.clear();
203     Q_EMIT keywordsChanged();
204   }
205 }
206
207 /*!
208   \brief PyEditor_StandardKeywords
209 */
210
211 PyEditor_StandardKeywords::PyEditor_StandardKeywords( QObject* parent )
212   : PyEditor_Keywords( parent )
213 {
214   QStringList aBase;
215   aBase << "and" << "as" << "assert" << "break" << "class" << "continue"
216         << "def" << "elif" << "else" << "except" << "exec" << "finally"
217         << "False" << "for" << "from" << "global" << "if" << "import"
218         << "in" << "is" << "lambda" << "None" << "not" << "or" << "pass"
219         << "print" << "raise" << "return" << "True" << "try" << "while"
220         << "with" << "yield";
221   append( aBase, Base, Qt::blue );
222
223   QStringList anExcept;
224   anExcept << "ArithmeticError" << "AssertionError" << "AttributeError"
225            << "EnvironmentError" << "EOFError" << "Exception"
226            << "FloatingPointError" << "ImportError" << "IndentationError"
227            << "IndexError" << "IOError" << "KeyboardInterrupt" << "KeyError"
228            << "LookupError" << "MemoryError" << "NameError" << "OSError"
229            << "NotImplementedError" << "OverflowError" << "ReferenceError"
230            << "RuntimeError" << "StandardError" << "StopIteration"
231            << "SyntaxError" << "SystemError" << "SystemExit" << "TabError"
232            << "TypeError" << "UnboundLocalError" << "UnicodeDecodeError"
233            << "UnicodeEncodeError" << "UnicodeError" << "UnicodeTranslateError"
234            << "ValueError" << "WindowsError" << "ZeroDivisionError"
235            << "Warning" << "UserWarning" << "DeprecationWarning"
236            << "PendingDeprecationWarning" << "SyntaxWarning"
237            << "OverflowWarning" << "RuntimeWarning" << "FutureWarning";
238   append( anExcept, Exceptions, Qt::magenta );
239 }
240
241 PyEditor_StandardKeywords::~PyEditor_StandardKeywords()
242 {
243 }