• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

lokalize

  • sources
  • kde-4.14
  • kdesdk
  • lokalize
  • src
syntaxhighlighter.cpp
Go to the documentation of this file.
1 /* ****************************************************************************
2  This file is part of Lokalize
3 
4  Copyright (C) 2007-2009 by Nick Shaforostoff <shafff@ukr.net>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of
9  the License or (at your option) version 3 or any later version
10  accepted by the membership of KDE e.V. (or its successor approved
11  by the membership of KDE e.V.), which shall act as a proxy
12  defined in Section 14 of version 3 of the license.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 **************************************************************************** */
23 
24 #include "syntaxhighlighter.h"
25 
26 #include "project.h"
27 #include "prefs_lokalize.h"
28 #include "prefs.h"
29 
30 #include <KDebug>
31 #include <KColorScheme>
32 
33 #include <QTextEdit>
34 #include <QApplication>
35 
36 #define STATE_NORMAL 0
37 #define STATE_TAG 1
38 
39 
40 
41 #define NUM_OF_RULES 5
42 
43 SyntaxHighlighter::SyntaxHighlighter(QTextEdit *parent)
44  : Sonnet::Highlighter(parent)
45  , tagBrush(KColorScheme::View,KColorScheme::VisitedText)
46  , m_approved(true)
47 // , fuzzyState(false)
48 // , fromDocbook(docbook)
49 {
50  setAutomatic(false);
51 
52  highlightingRules.reserve(NUM_OF_RULES);
53  HighlightingRule rule;
54  //rule.format.setFontItalic(true);
55 // tagFormat.setForeground(tagBrush.brush(QApplication::palette()));
56  tagFormat.setForeground(tagBrush.brush(QApplication::palette()));
57  //QTextCharFormat format;
58  //tagFormat.setForeground(Qt::darkBlue);
59 // if (!docbook) //support multiline tags
60 // {
61 // rule.format = tagFormat;
62 // rule.pattern = QRegExp("<.+>");
63 // rule.pattern.setMinimal(true);
64 // highlightingRules.append(rule);
65 // }
66 
67  //entity
68  rule.format.setForeground(Qt::darkMagenta);
69  rule.pattern = QRegExp("(&[A-Za-z_:][A-Za-z0-9_\\.:-]*;)");
70  highlightingRules.append(rule);
71 
72  QString accel=Project::instance()->accel();
73  if (!accel.isEmpty())
74  {
75  rule.format.setForeground(Qt::darkMagenta);
76  rule.pattern = QRegExp(accel);
77  highlightingRules.append(rule);
78  }
79 
80  //\n \t \"
81  rule.format.setForeground(Qt::darkGreen);
82  rule.pattern = QRegExp("(\\\\[abfnrtv'\?\\\\])|(\\\\\\d+)|(\\\\x[\\dabcdef]+)");
83  highlightingRules.append(rule);
84 
85 
86 
87 
88 
89  //spaces
90  settingsChanged();
91  connect(SettingsController::instance(),SIGNAL(generalSettingsChanged()),this, SLOT(settingsChanged()));
92 
93 }
94 
95 void SyntaxHighlighter::settingsChanged()
96 {
97  QRegExp re(QString(" +$|^ +|.?")+QChar(0x0000AD)+".?"); //soft hyphen
98  if (Settings::highlightSpaces() && highlightingRules.last().pattern!=re)
99  {
100  KColorScheme colorScheme(QPalette::Normal);
101  HighlightingRule rule;
102  rule.format.clearForeground();
103 
104  //nbsp
105  rule.format.setBackground(colorScheme.background(KColorScheme::AlternateBackground));
106  rule.pattern = QRegExp(QChar(0x00a0U));
107  highlightingRules.append(rule);
108 
109  //usual spaces at the end
110  rule.format.setBackground(colorScheme.background(KColorScheme::ActiveBackground));
111  rule.pattern = re;
112  highlightingRules.append(rule);
113  rehighlight();
114  }
115  else if (!Settings::highlightSpaces() && highlightingRules.last().pattern==re)
116  {
117  highlightingRules.resize(highlightingRules.size()-2);
118  rehighlight();
119  }
120 }
121 
122 /*
123 void SyntaxHighlighter::setFuzzyState(bool fuzzy)
124 {
125  return;
126  int i=NUM_OF_RULES;
127  while(--i>=0)
128  highlightingRules[i].format.setFontItalic(fuzzy);
129 
130  tagFormat.setFontItalic(fuzzy);
131 }*/
132 
133 void SyntaxHighlighter::highlightBlock(const QString &text)
134 {
135  int currentBlockState = STATE_NORMAL;
136  QTextCharFormat f;
137  f.setFontItalic(!m_approved);
138  setFormat(0, text.length(), f);
139 
140  tagFormat.setFontItalic(!m_approved);
141  //if (fromDocbook)
142  {
143  int startIndex = STATE_NORMAL;
144  if (previousBlockState() != STATE_TAG)
145  startIndex = text.indexOf('<');
146 
147  while (startIndex >= 0)
148  {
149  int endIndex = text.indexOf('>', startIndex);
150  int commentLength;
151  if (endIndex == -1)
152  {
153  currentBlockState = STATE_TAG;
154  commentLength = text.length() - startIndex;
155  }
156  else
157  {
158  commentLength = endIndex - startIndex
159  +1/*+ commentEndExpression.matchedLength()*/;
160  }
161  setFormat(startIndex, commentLength, tagFormat);
162  startIndex = text.indexOf('<', startIndex + commentLength);
163  }
164  }
165 
166  foreach (const HighlightingRule &rule, highlightingRules)
167  {
168  QRegExp expression(rule.pattern);
169  int index = expression.indexIn(text);
170  while (index >= 0)
171  {
172  int length = expression.matchedLength();
173  QTextCharFormat f=rule.format;
174  f.setFontItalic(!m_approved);
175  setFormat(index, length, f);
176  index = expression.indexIn(text, index + length);
177  }
178  }
179 
180  if (spellCheckerFound())
181  Sonnet::Highlighter::highlightBlock(text); // Resets current block state
182 
183  setCurrentBlockState(currentBlockState);
184 }
185 
186 #if 0
187 void SyntaxHighlighter::setFormatRetainingUnderlines(int start, int count, QTextCharFormat f)
188 {
189  QVector<bool> underLines(count);
190  for (int i=0;i<count;++i)
191  underLines[i]=format(start+i).fontUnderline();
192 
193  setFormat(start, count, f);
194 
195  f.setFontUnderline(true);
196  int prevStart=-1;
197  bool isPrevUnderLined=false;
198  for (int i=0;i<count;++i)
199  {
200  if (!underLines.at(i) && prevStart!=-1)
201  setFormat(start+isPrevUnderLined, i-prevStart, f);
202  else if (underLines.at(i)&&!isPrevUnderLined)
203  prevStart=i;
204 
205  isPrevUnderLined=underLines.at(i);
206  }
207 }
208 #endif
209 
210 void SyntaxHighlighter::setMisspelled(int start, int count)
211 {
212  QString text=currentBlock().text();
213  QString word=text.mid(start,count);
214  if (m_sourceString.contains(word))
215  return;
216 
217  QString accel=Project::instance()->accel();
218 
219  if (!isWordMisspelled(word.remove(accel)))
220  return;
221  count=word.length();//safety
222 
223  bool smthPreceeding=(start>0) &&
224  (accel.endsWith(text.at(start-1))
225  || text.at(start-1)==QChar(0x0000AD) //soft hyphen
226  );
227 
228  //HACK. Needs Sonnet API redesign (KDE 5)
229  if (smthPreceeding)
230  {
231  kWarning()<<"ampersand is in the way. word len:"<<count;
232  int realStart=text.lastIndexOf(QRegExp("\\b"),start-2);
233  if (realStart==-1)
234  realStart=0;
235  QString t=text.mid(realStart, count+start-realStart);
236  t.remove(accel);
237  t.remove(QChar(0x0000AD));
238  if (!isWordMisspelled(t))
239  return;
240  }
241 
242  bool smthAfter=(start+count+1<text.size()) &&
243  (accel.startsWith(text.at(start+count))
244  || text.at(start+count)==QChar(0x0000AD) //soft hyphen
245  );
246  if (smthAfter)
247  {
248  kWarning()<<"smthAfter. ampersand is in the way. word len:"<<count;
249  int realEnd=text.indexOf(QRegExp("\\b"),start+count+2);
250  if (realEnd==-1)
251  realEnd=text.size();
252  QString t=text.mid(start, realEnd-start);
253  t.remove(accel);
254  t.remove(QChar(0x0000AD));
255  if (!isWordMisspelled(t))
256  return;
257  }
258 
259  if (count && format(start)==tagFormat)
260  return;
261 
262  for (int i=0;i<count;++i)
263  {
264  QTextCharFormat f(format(start+i));
265  f.setFontUnderline(true);
266  f.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
267  f.setUnderlineColor(Qt::red);
268  setFormat(start+i, 1, f);
269  }
270 }
271 
272 void SyntaxHighlighter::unsetMisspelled(int start, int count)
273 {
274  for (int i=0;i<count;++i)
275  {
276  QTextCharFormat f(format(start+i));
277  f.setFontUnderline(false);
278  setFormat(start+i, 1, f);
279  }
280 }
281 
282 
283 
284 #include "syntaxhighlighter.moc"
285 
NUM_OF_RULES
#define NUM_OF_RULES
Definition: syntaxhighlighter.cpp:41
QTextCharFormat::setFontItalic
void setFontItalic(bool italic)
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
project.h
QTextCharFormat::setUnderlineStyle
void setUnderlineStyle(UnderlineStyle style)
QVector::append
void append(const T &value)
QChar
Project::instance
static Project * instance()
Definition: project.cpp:67
QVector::last
T & last()
QString::size
int size() const
prefs.h
SyntaxHighlighter::SyntaxHighlighter
SyntaxHighlighter(QTextEdit *parent)
Definition: syntaxhighlighter.cpp:43
QString::remove
QString & remove(int position, int n)
QString::lastIndexOf
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QRegExp::matchedLength
int matchedLength() const
QTextFormat::setForeground
void setForeground(const QBrush &brush)
QRegExp::indexIn
int indexIn(const QString &str, int offset, CaretMode caretMode) const
QRegExp
ProjectBase::accel
QString accel() const
Get Accel.
Definition: projectbase.h:235
QVector::resize
void resize(int size)
SyntaxHighlighter::highlightBlock
void highlightBlock(const QString &text)
Definition: syntaxhighlighter.cpp:133
QString::isEmpty
bool isEmpty() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
QString
QApplication::palette
QPalette palette()
STATE_NORMAL
#define STATE_NORMAL
Definition: syntaxhighlighter.cpp:36
QTextCharFormat
QVector::reserve
void reserve(int size)
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
SettingsController::instance
static SettingsController * instance()
Definition: prefs.cpp:70
Settings::highlightSpaces
static bool highlightSpaces()
Get HighlightSpaces.
Definition: prefs_lokalize.h:151
syntaxhighlighter.h
QString::mid
QString mid(int position, int n) const
QVector< bool >
prefs_lokalize.h
SyntaxHighlighter::setMisspelled
void setMisspelled(int start, int count)
Definition: syntaxhighlighter.cpp:210
QString::at
const QChar at(int position) const
QString::length
int length() const
QTextCharFormat::setFontUnderline
void setFontUnderline(bool underline)
QTextCharFormat::setUnderlineColor
void setUnderlineColor(const QColor &color)
SyntaxHighlighter::unsetMisspelled
void unsetMisspelled(int start, int count)
Definition: syntaxhighlighter.cpp:272
STATE_TAG
#define STATE_TAG
Definition: syntaxhighlighter.cpp:37
QVector::size
int size() const
QTextEdit
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:40:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

lokalize

Skip menu "lokalize"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal