Commit 71fb4fc1 authored by Laurent Montel's avatar Laurent Montel 😁

Remove duplicate code

parent 381d3268
......@@ -19,7 +19,7 @@
#include "xmlprintingsyntaxhighlighter.h"
XMLPrintingSyntaxHighLighter::XMLPrintingSyntaxHighLighter( QTextDocument *doc )
: QSyntaxHighlighter( doc )
: PimCommon::SyntaxHighlighterBase( doc )
{
init();
}
......@@ -28,20 +28,6 @@ XMLPrintingSyntaxHighLighter::~XMLPrintingSyntaxHighLighter()
{
}
void XMLPrintingSyntaxHighLighter::highlightBlock(const QString &text)
{
Q_FOREACH (const Rule &rule, m_rules) {
const QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
int length = 0;
while (index >= 0 && ( length = expression.matchedLength() ) > 0 ) {
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
}
void XMLPrintingSyntaxHighLighter::init()
{
QTextCharFormat testFormat;
......
......@@ -18,33 +18,21 @@
#ifndef XMLPRINTINGSYNTAXHIGHLIGHTER_H
#define XMLPRINTINGSYNTAXHIGHLIGHTER_H
#include "pimcommon/syntaxhighlighter/syntaxhighlighterbase.h"
#include <QList>
#include <QRegExp>
#include <QSyntaxHighlighter>
class QTextDocument;
class XMLPrintingSyntaxHighLighter : public QSyntaxHighlighter
class XMLPrintingSyntaxHighLighter : public PimCommon::SyntaxHighlighterBase
{
Q_OBJECT
public:
explicit XMLPrintingSyntaxHighLighter( QTextDocument *doc );
~XMLPrintingSyntaxHighLighter();
void highlightBlock(const QString &text);
private:
void init();
struct Rule {
QRegExp pattern;
QTextCharFormat format;
Rule( const QRegExp &r, const QTextCharFormat &f )
: pattern(r), format(f) {}
};
QList<Rule> m_rules;
};
......
......@@ -20,7 +20,7 @@
using namespace MessageViewer;
AdBlockSyntaxHighlighter::AdBlockSyntaxHighlighter(QTextDocument *doc)
: QSyntaxHighlighter(doc)
: PimCommon::SyntaxHighlighterBase(doc)
{
init();
}
......@@ -29,19 +29,6 @@ AdBlockSyntaxHighlighter::~AdBlockSyntaxHighlighter()
{
}
void AdBlockSyntaxHighlighter::highlightBlock(const QString &text)
{
Q_FOREACH (const Rule &rule, m_rules) {
const QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
int length = 0;
while (index >= 0 && ( length = expression.matchedLength() ) > 0 ) {
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
}
void AdBlockSyntaxHighlighter::init()
{
QTextCharFormat commentFormat;
......
......@@ -18,32 +18,20 @@
#ifndef ADBLOCKSYNTAXHIGHLIGHTER_H
#define ADBLOCKSYNTAXHIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include "pimcommon/syntaxhighlighter/syntaxhighlighterbase.h"
#include <QList>
#include <QRegExp>
namespace MessageViewer {
class AdBlockSyntaxHighlighter : public QSyntaxHighlighter
class AdBlockSyntaxHighlighter : public PimCommon::SyntaxHighlighterBase
{
Q_OBJECT
public:
explicit AdBlockSyntaxHighlighter(QTextDocument *doc);
~AdBlockSyntaxHighlighter();
void highlightBlock(const QString &text);
private:
void init();
struct Rule {
QRegExp pattern;
QTextCharFormat format;
Rule( const QRegExp &r, const QTextCharFormat &f )
: pattern(r), format(f) {}
};
QList<Rule> m_rules;
};
}
......
......@@ -176,6 +176,9 @@ set(libpimcommon_baloodebug_SRCS
baloodebug/baloosyntaxhighlighter.cpp
)
set(libpimcommon_syntaxhighlighter_SRCS
syntaxhighlighter/syntaxhighlighterbase.cpp
)
set(libpimcommon_SRCS
util/createresource.cpp
......@@ -193,6 +196,7 @@ set(libpimcommon_SRCS
${libpimcommon_sieve_SRCS}
${libpimcommon_widgets_SRCS}
${libpimcommon_baloodebug_SRCS}
${libpimcommon_syntaxhighlighter_SRCS}
)
......
......@@ -21,7 +21,7 @@
using namespace PimCommon;
BalooSyntaxHighlighter::BalooSyntaxHighlighter(QTextDocument *doc)
: QSyntaxHighlighter(doc)
: SyntaxHighlighterBase(doc)
{
init();
}
......@@ -31,19 +31,6 @@ BalooSyntaxHighlighter::~BalooSyntaxHighlighter()
}
void BalooSyntaxHighlighter::highlightBlock(const QString &text)
{
Q_FOREACH (const Rule &rule, m_rules) {
const QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
int length = 0;
while (index >= 0 && ( length = expression.matchedLength() ) > 0 ) {
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
}
void BalooSyntaxHighlighter::init()
{
QTextCharFormat testFormat;
......
......@@ -19,28 +19,17 @@
#define BALOOSYNTAXHIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include "pimcommon/syntaxhighlighter/syntaxhighlighterbase.h"
namespace PimCommon {
class BalooSyntaxHighlighter : public QSyntaxHighlighter
class BalooSyntaxHighlighter : public SyntaxHighlighterBase
{
Q_OBJECT
public:
explicit BalooSyntaxHighlighter(QTextDocument *doc);
~BalooSyntaxHighlighter();
void highlightBlock(const QString &text);
private:
void init();
struct Rule {
QRegExp pattern;
QTextCharFormat format;
Rule( const QRegExp &r, const QTextCharFormat &f )
: pattern(r), format(f) {}
};
QList<Rule> m_rules;
};
}
#endif // BALOOSYNTAXHIGHLIGHTER_H
......@@ -21,7 +21,7 @@
using namespace PimCommon;
SieveSyntaxHighlighter::SieveSyntaxHighlighter( QTextDocument *doc )
: QSyntaxHighlighter( doc )
: SyntaxHighlighterBase( doc )
{
init();
}
......@@ -30,19 +30,6 @@ SieveSyntaxHighlighter::~SieveSyntaxHighlighter()
{
}
void SieveSyntaxHighlighter::highlightBlock(const QString &text)
{
Q_FOREACH (const Rule &rule, m_rules) {
const QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
int length = 0;
while (index >= 0 && ( length = expression.matchedLength() ) > 0 ) {
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
}
void SieveSyntaxHighlighter::addCapabilities(const QStringList &capabilities)
{
// capabilities
......
......@@ -20,40 +20,27 @@
#define KSIEVE_KSIEVEUI_SIEVESYNTAXHIGHLIGHTER_H
#include "pimcommon_export.h"
#include "pimcommon/syntaxhighlighter/syntaxhighlighterbase.h"
#include <QList>
#include <QRegExp>
#include <QSyntaxHighlighter>
class QTextDocument;
namespace PimCommon {
class PIMCOMMON_EXPORT SieveSyntaxHighlighter : public QSyntaxHighlighter
class PIMCOMMON_EXPORT SieveSyntaxHighlighter : public SyntaxHighlighterBase
{
Q_OBJECT
public:
explicit SieveSyntaxHighlighter( QTextDocument *doc );
~SieveSyntaxHighlighter();
void highlightBlock(const QString &text);
void addCapabilities(const QStringList &capabilities);
private:
void init();
struct Rule {
QRegExp pattern;
QTextCharFormat format;
Rule( const QRegExp &r, const QTextCharFormat &f )
: pattern(r), format(f) {}
};
QList<Rule> m_rules;
};
}
......
/*
Copyright (c) 2014 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "syntaxhighlighterbase.h"
using namespace PimCommon;
SyntaxHighlighterBase::SyntaxHighlighterBase(QTextDocument *doc)
: QSyntaxHighlighter(doc)
{
}
SyntaxHighlighterBase::~SyntaxHighlighterBase()
{
}
void SyntaxHighlighterBase::highlightBlock( const QString &text )
{
Q_FOREACH ( const Rule &rule, m_rules ) {
const QRegExp expression( rule.pattern );
int index = expression.indexIn( text );
int length = 0;
while ( index >= 0 && ( length = expression.matchedLength() ) > 0 ) {
setFormat( index, length, rule.format );
index = expression.indexIn( text, index + length );
}
}
}
/*
Copyright (c) 2014 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SYNTAXHIGHLIGHTERBASE_H
#define SYNTAXHIGHLIGHTERBASE_H
#include <QSyntaxHighlighter>
#include "pimcommon_export.h"
namespace PimCommon {
class PIMCOMMON_EXPORT SyntaxHighlighterBase : public QSyntaxHighlighter
{
Q_OBJECT
public:
explicit SyntaxHighlighterBase(QTextDocument *doc);
~SyntaxHighlighterBase();
void highlightBlock( const QString &text );
protected:
virtual void init() = 0;
struct Rule {
QRegExp pattern;
QTextCharFormat format;
Rule( const QRegExp &r, const QTextCharFormat &f )
: pattern( r ), format( f ) {}
};
QList<Rule> m_rules;
};
}
#endif // SYNTAXHIGHLIGHTERBASE_H
......@@ -23,7 +23,7 @@
using namespace TemplateParser;
TemplatesSyntaxHighlighter::TemplatesSyntaxHighlighter( QTextDocument *doc )
: QSyntaxHighlighter( doc )
: PimCommon::SyntaxHighlighterBase( doc )
{
init();
}
......@@ -32,19 +32,6 @@ TemplatesSyntaxHighlighter::~TemplatesSyntaxHighlighter()
{
}
void TemplatesSyntaxHighlighter::highlightBlock( const QString &text )
{
Q_FOREACH ( const Rule &rule, m_rules ) {
const QRegExp expression( rule.pattern );
int index = expression.indexIn( text );
int length = 0;
while ( index >= 0 && ( length = expression.matchedLength() ) > 0 ) {
setFormat( index, length, rule.format );
index = expression.indexIn( text, index + length );
}
}
}
void TemplatesSyntaxHighlighter::init()
{
QTextCharFormat keywordFormat;
......@@ -68,7 +55,6 @@ void TemplatesSyntaxHighlighter::init()
const QRegExp regex( s, Qt::CaseInsensitive );
m_rules.append( Rule( regex, keywordWithArgsFormat ) );
}
}
......@@ -20,31 +20,19 @@
#define TEMPLATEPARSER_TEMPLATESSYNTAXHIGHLIGHTER_H
#include "templateparser_export.h"
#include <QSyntaxHighlighter>
#include "pimcommon/syntaxhighlighter/syntaxhighlighterbase.h"
namespace TemplateParser {
class TEMPLATEPARSER_EXPORT TemplatesSyntaxHighlighter : public QSyntaxHighlighter
class TEMPLATEPARSER_EXPORT TemplatesSyntaxHighlighter : public PimCommon::SyntaxHighlighterBase
{
Q_OBJECT
public:
explicit TemplatesSyntaxHighlighter( QTextDocument *doc );
~TemplatesSyntaxHighlighter();
void highlightBlock( const QString &text );
private:
void init();
struct Rule {
QRegExp pattern;
QTextCharFormat format;
Rule( const QRegExp &r, const QTextCharFormat &f )
: pattern( r ), format( f ) {}
};
QList<Rule> m_rules;
};
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment