itom  4.1.0
codeCheckerItem.h
1 /* ********************************************************************
2 itom software
3 URL: http://www.uni-stuttgart.de/ito
4 Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5 Universitaet Stuttgart, Germany
6 
7 This file is part of itom.
8 
9 itom is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Library General Public Licence as published by
11 the Free Software Foundation; either version 2 of the Licence, or (at
12 your option) any later version.
13 
14 itom is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
17 General Public Licence for more details.
18 
19 You should have received a copy of the GNU Library General Public License
20 along with itom. If not, see <http://www.gnu.org/licenses/>.
21 
22 Further hints:
23 ------------------------
24 
25 This file belongs to the code editor of itom. The code editor is
26 in major parts a fork / rewritten version of the python-based source
27 code editor PyQode from Colin Duquesnoy and others
28 (see https://github.com/pyQode). PyQode itself is licensed under
29 the MIT License (MIT).
30 
31 Some parts of the code editor of itom are also inspired by the
32 source code editor of the Spyder IDE (https://github.com/spyder-ide),
33 also licensed under the MIT License and developed by the Spyder Project
34 Contributors.
35 
36 *********************************************************************** */
37 
38 #ifndef CODECHECKERITEM_H
39 #define CODECHECKERITEM_H
40 
41 #include <qstring.h>
42 #include <qcolor.h>
43 #include <qobject.h>
44 
45 namespace ito {
46 
47  class CodeEditor;
48 
49  //------------------------------------------------------
50  /*
51  Holds data for a message displayed by the
52  :class:`pyqode.core.modes.CheckerMode`.
53 
54  One entry as a result of the code checker (linter, e.g. pyflakes, flake8...).
55  */
57  {
58 
59  public:
60  enum CheckerType
61  {
62  Info = 0x001, //must be a bitmask
63  Warning = 0x002,
64  Error = 0x004,
65  };
66 
67  //---------------------------------------------------------
68  /*
69  :param description: The message description (used as a tooltip)
70  :param status: The status associated with the message.
71  :param line: The message line number
72  :param col: The message start column (at the moment the message ends at
73  the end of the line).
74  :param icon: Unused, we keep it for backward compatiblity.
75  :param color: Text decoration color
76  :param path: file path. Optional
77  */
78  explicit CodeCheckerItem(CheckerType messageType,
79  const QString &description,
80  const QString &messageCode = "",
81  int lineNumber = -1,
82  int col = -1,
83  const QString canonicalPath = QString()) :
84  m_description(description), //The description of the message, used as a tooltip.
85  m_code(messageCode),
86  m_type(messageType), //The status associated with the message
87  m_col(col), //: The start column (used for the text decoration). If the col is None,
88  //: the whole line is highlighted.
89  m_lineNumber(lineNumber),
90  m_filePath(canonicalPath)
91  {
92  if (m_color.isValid() == false)
93  {
94  m_color = statusToColor(m_type);
95  }
96  }
97 
98  //-----------------------------------------------------------
99  CodeCheckerItem(const CodeCheckerItem &other) :
100  m_description(other.m_description), //The description of the message, used as a tooltip.
101  m_code(other.m_code),
102  m_type(other.m_type), //The status associated with the message
103  m_col(other.m_col), //: The start column (used for the text decoration). If the col is None,
104  //: the whole line is highlighted.
105  m_lineNumber(other.m_lineNumber),
106  m_filePath(other.m_filePath),
107  m_color(other.m_color)
108  {
109 
110  }
111 
112  //------------------------------------------------------------
113  CheckerType type() const
114  {
115  return m_type;
116  }
117 
118  //------------------------------------------------------------
119  QString description() const
120  {
121  return m_description;
122  }
123 
124  //------------------------------------------------------------
125  int lineNumber() const
126  {
127  return m_lineNumber;
128  }
129 
130  //------------------------------------------------------------
131  /*
132  Converts a message status to a string.
133 
134  :param status: Status to convert (p yqode.core.modes.CheckerMessages)
135  :return: The status string.
136  :rtype: str
137  */
138  static QString statusToString(CheckerType status)
139  {
140  switch (status)
141  {
142  case Info:
143  return QObject::tr("Info");
144  case Warning:
145  return QObject::tr("Warning");
146  case Error:
147  return QObject::tr("Error");
148  }
149  }
150 
151  //------------------------------------------------------------
152  /*
153  Converts a message status to a color.
154 
155  :param status: Status to convert (p yqode.core.modes.CheckerMessages)
156  :return: The status color.
157  :rtype: QColor
158  */
159  static QColor statusToColor(CheckerType status)
160  {
161  switch (status)
162  {
163  case Info:
164  return QColor("#4040DD");
165  case Warning:
166  return QColor("#DDDD40");
167  case Error:
168  default:
169  return QColor("#DD4040");
170  }
171  }
172 
173  //----------------------------------------------------
174  /*
175  Returns the message status as a string.
176 
177  :return: The status string.
178  */
179  QString statusString() const
180  {
181  return statusToString(m_type);
182  }
183 
184  //----------------------------------------------------
185  /* return a string representation of this checker item. */
186  QString checkerItemText(bool addShortType = false) const
187  {
188  QString prefix;
189 
190  if (addShortType)
191  {
192  switch (m_type)
193  {
194  case Info:
195  prefix = QObject::tr("[I] ");
196  break;
197  case Warning:
198  prefix = QObject::tr("[W] ");
199  break;
200  case Error:
201  prefix = QObject::tr("[E] ");
202  break;
203  }
204 
205  }
206 
207  if (m_code.isEmpty())
208  {
209  if (m_col != -1)
210  {
211  return QObject::tr("%1 (Column %2)").arg(prefix + m_description).arg(m_col + 1);
212  }
213  else
214  {
215  return m_description;
216  }
217  }
218  else
219  {
220  if (m_col != -1)
221  {
222  return QObject::tr("%1: %2 (Column %3)").arg(prefix + m_code).arg(m_description).arg(m_col + 1);
223  }
224  else
225  {
226  return QString("%1: %2").arg(prefix + m_code).arg(m_description);
227  }
228  }
229  }
230 
231  private:
232  CheckerType m_type;
233  QString m_code; //code of the message
234  QString m_description;
235  QString m_filePath;
236  int m_lineNumber;
237  int m_col;
238  QColor m_color;
239  };
240 
241 
242 
243 
244 } //end namespace ito
245 
246 #endif
Definition: codeCheckerItem.h:56
Definition: apiFunctionsGraph.cpp:39