srchiliteqt::Qt4SyntaxHighlighter Class Reference

An implementation of QSyntaxHighlighter using GNU Source-highlight library (by relying on GNUSyntaxHighlighter provided by the common part of this library). More...

#include <Qt4SyntaxHighlighter.h>

Inherits srchiliteqt::GNUSyntaxHighlighter.

Collaboration diagram for srchiliteqt::Qt4SyntaxHighlighter:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Qt4SyntaxHighlighter (QTextDocument *doc=0)
void init (const QString &langFile, const QString &styleFile="default.style")
 Initializes this highlighter with the specified language definition file.
bool initFromFileName (const QString &fileName)
 Initializes this highlighter by using the file name for detecting the syntax of the file (e.g., foo.cpp brings to cpp.lang, ChangeLog brings to changelog.lang).
void formatString (int start, int count, const QTextCharFormat &format)
 This function is applied to the syntax highlighter's current text block (i.e.
void setFormattingStyle (const QString &styleFile)
 Sets the styleFile for highlighting.
const QString getFormattingStyle () const
const srchilite::FormatterMap & getFormatterMap () const
 Returns the formatter map, i.e., for each language element we have a formatter.
Qt4TextFormatterMap getQt4TextFormatterMap ()
 Returns the formatter map of this highlighter.
const QString & getLangFile () const
srchilite::HighlightStatePtr getHighlightState (const QString &file)
 Given a language definition file name, returns the corresponding highlight state.
void initHighlighter (const QString &file)
 Given a language definition file name, initializes the Source-highlight's highlighter.
const QString getLangDefFileFromFileName (const QString &filename)
 Returns the the lang def file name by using the file name for detecting the syntax of the file (e.g., foo.cpp brings to cpp.lang, ChangeLog brings to changelog.lang).
srchilite::SourceHighlighter * getHighlighter () const
const TextFormatterMap & getTextFormatterMap (TextFormatterFactory &formatterFactory, const QString &styleFile="default.style")
 Given a TextFormatterFactory, it uses to build the TextFormatterMap, by reading a specified source-highlight style file.
srchilite::FormatterManager * getFormatterManager () const
void highlightLine (const QString &line, HighlightStateData *&stateData)
 Highlights the passed line.
srchilite::LangDefManager * getLangDefManager () const
srchilite::LangMap * getLangMap () const
srchilite::LangMap * getOutLangMap () const
const QString & getForegroundColor () const
const QString & getBackgroundColor () const
void setForegroundColor (const QString &f)
void setBackgroundColor (const QString &b)
bool isReadOnly () const
void setReadOnly (bool r)
 Specifies that we highlight readonly contents.
bool isDefaultToMonospace () const
void setDefaultToMonospace (bool d)
 Whether the highlighter should use monospace font by default if nothing is specified about the font family.

Protected Member Functions

void highlightBlock (const QString &text)
 The implementation of the virtual pure method of QSyntaxHighlighter.
void setFormatters (const TextFormatterMap &formatterMap)
 Creates formatters (and use them to initialize the formatter manager), by using the passed TextFormatterMap.

Protected Attributes

srchilite::FormatterManager * formatterManager
 This will be used by subclasses to generate the right table of formatters.

Private Attributes

QString currentStyleFile
 the current style file for highlighting

Detailed Description

An implementation of QSyntaxHighlighter using GNU Source-highlight library (by relying on GNUSyntaxHighlighter provided by the common part of this library).

You can use such highlighter with a QTextEdit, and initialize the highlighter with the language definition file, e.g.,

    QTextEdit *editor = new QTextEdit;
    srchiliteqt::Qt4SyntaxHighlighter *highlighter =
            new srchiliteqt::Qt4SyntaxHighlighter(editor->document());
    highlighter->init("java.lang");

Member Function Documentation

void srchiliteqt::Qt4SyntaxHighlighter::formatString ( int  start,
int  count,
const QTextCharFormat &  format 
) [inline]

This function is applied to the syntax highlighter's current text block (i.e.

the text that is passed to the highlightBlock() method).

The specified format is applied to the text from the start position for a length of count characters (if count is 0, nothing is done). The formatting properties set in format are merged at display time with the formatting information stored directly in the document, for example as previously set with QTextCursor's functions.

Note that this helper function will be called by the corresponding TextFormatter, from Source-highglight library code, and relies on the corresponding protected method of QSyntaxHighlighter: setFormat).

const QString& srchiliteqt::GNUSyntaxHighlighter::getBackgroundColor (  )  const [inline, inherited]
Returns:
the background color string (can be empty)
const QString& srchiliteqt::GNUSyntaxHighlighter::getForegroundColor (  )  const [inline, inherited]
Returns:
the foreground color string (can be empty)
srchilite::FormatterManager* srchiliteqt::GNUSyntaxHighlighter::getFormatterManager (  )  const [inline, inherited]
Returns:
the Source-highlight FormatterManager
const srchilite::FormatterMap& srchiliteqt::Qt4SyntaxHighlighter::getFormatterMap (  )  const [inline]

Returns the formatter map, i.e., for each language element we have a formatter.

This should be called after the highlighter has already been initialized. Note that the key of the map is a std::string representing the language element and the associated value is a base class FormatterPtr (a shared pointer), so you need to retrieve the associated actual pointer with .get() and to it cast it to a Qt4TextFormatter, e.g.,:

 Qt4TextFormatter *formatter =
     dynamic_cast<Qt4TextFormatter *>(map["normal"].get());

Note also that in case no formatter is associated to a specific string, then .get() will return 0, so you should always check for the resulting pointer. You might want to use, instead getQt4TextFormatterMap() where the values are already of Qt4TextFormatter type (and the formatters are exactly the same).

Returns:
the
const QString srchiliteqt::Qt4SyntaxHighlighter::getFormattingStyle (  )  const [inline]
Returns:
the current style file used for highlighting
srchilite::SourceHighlighter* srchiliteqt::GNUSyntaxHighlighter::getHighlighter (  )  const [inline, inherited]
Returns:
the Source-highlight SourceHighlighter (can be null)
srchilite::HighlightStatePtr srchiliteqt::GNUSyntaxHighlighter::getHighlightState ( const QString &  file  )  [inherited]

Given a language definition file name, returns the corresponding highlight state.

This method is mainly for testing purposes.

Parameters:
file 
Returns:
the corresponding highlight state
Exceptions:
srchilite::ParserException 
const QString srchiliteqt::GNUSyntaxHighlighter::getLangDefFileFromFileName ( const QString &  filename  )  [inherited]

Returns the the lang def file name by using the file name for detecting the syntax of the file (e.g., foo.cpp brings to cpp.lang, ChangeLog brings to changelog.lang).

Parameters:
filename 
Returns:
the lang def file name or the empty string if no mapping exists
srchilite::LangDefManager * srchiliteqt::GNUSyntaxHighlighter::getLangDefManager (  )  const [inherited]
Returns:
the LangDefManager object used by this highlighter
srchilite::LangMap * srchiliteqt::GNUSyntaxHighlighter::getLangMap (  )  const [inherited]
Returns:
the LangMap dealing with language definition files.
srchilite::LangMap * srchiliteqt::GNUSyntaxHighlighter::getOutLangMap (  )  const [inherited]
Returns:
the LangMap dealing with output language definition files.
Qt4TextFormatterMap srchiliteqt::Qt4SyntaxHighlighter::getQt4TextFormatterMap (  ) 

Returns the formatter map of this highlighter.

It can be used to change formatters; in this case, you should call rehighlight() after changing formatters.

Returns:
the map of Qt4TextFormatters used by this highlighter
const TextFormatterMap & srchiliteqt::GNUSyntaxHighlighter::getTextFormatterMap ( TextFormatterFactory formatterFactory,
const QString &  styleFile = "default.style" 
) [inherited]

Given a TextFormatterFactory, it uses to build the TextFormatterMap, by reading a specified source-highlight style file.

If the TextFormatterFactory already contains some formatters, then simpy returns its TextFormatterMap, without rebuilding it.

Parameters:
formatterFactory the formatter factory to create formatters
styleFile the style file name (can be a .style file or a .css file - in this case it must be a css file that source-highlight is able to handle)
Returns:
the TextFormatterMap
Exceptions:
ParserException in case of parsing error of the style file
void srchiliteqt::GNUSyntaxHighlighter::highlightLine ( const QString &  line,
HighlightStateData *&  stateData 
) [inherited]

Highlights the passed line.

This method assumes that all the fields are already initialized (e.g., the FormatterManager).

The passed HighlightStateData is used to configure the SourceHighlighter with info like the current highlighting state and the state stack. If it is null, we simply ignore it.

This method can modify the bassed pointer and even make it NULL (after deleting it).

Parameters:
line 
stateData the highlight state data to use
Returns:
in case after highlighting the stack changed we return either the original stateData (after updating) or a new HighlightStateData (again with the updated information)
void srchiliteqt::Qt4SyntaxHighlighter::init ( const QString &  langFile,
const QString &  styleFile = "default.style" 
)

Initializes this highlighter with the specified language definition file.

Parameters:
langFile 
styleFile the style file name (can be a .style file or a .css file - in this case it must be a css file that source-highlight is able to handle)
Exceptions:
ParserException in case of parsing error of the lang file or of the style file
bool srchiliteqt::Qt4SyntaxHighlighter::initFromFileName ( const QString &  fileName  ) 

Initializes this highlighter by using the file name for detecting the syntax of the file (e.g., foo.cpp brings to cpp.lang, ChangeLog brings to changelog.lang).

Parameters:
fileName 
Returns:
true if we could find a lang def file for the specified filename
void srchiliteqt::GNUSyntaxHighlighter::initHighlighter ( const QString &  file  )  [inherited]

Given a language definition file name, initializes the Source-highlight's highlighter.

Parameters:
file the lang file of Source-highlight
Exceptions:
srchilite::ParserException 
bool srchiliteqt::GNUSyntaxHighlighter::isReadOnly (  )  const [inline, inherited]
Returns:
whether we must highlight readonly contents
void srchiliteqt::GNUSyntaxHighlighter::setDefaultToMonospace ( bool  d  )  [inline, inherited]

Whether the highlighter should use monospace font by default if nothing is specified about the font family.

Parameters:
d 
void srchiliteqt::Qt4SyntaxHighlighter::setFormatters ( const TextFormatterMap &  formatterMap  )  [protected]

Creates formatters (and use them to initialize the formatter manager), by using the passed TextFormatterMap.

This can be called only after the highlighter was initialized through init().

Parameters:
formatterMap 
void srchiliteqt::Qt4SyntaxHighlighter::setFormattingStyle ( const QString &  styleFile  ) 

Sets the styleFile for highlighting.

It also rehighlights the contents of the document. This must be called after the highlighter has already been initialized, otherwise it does nothing.

Parameters:
styleFile the style file name (can be a .style file or a .css file - in this case it must be a css file that source-highlight is able to handle)
Exceptions:
ParserException in case of parsing error of the lang file or of the style file
void srchiliteqt::GNUSyntaxHighlighter::setReadOnly ( bool  r  )  [inline, inherited]

Specifies that we highlight readonly contents.

This optimizes the highlighting: being readonly, we must not keep track of the state of the previous line, and this saves time and memory.

Parameters:
r 

The documentation for this class was generated from the following files:

Generated on Sat Mar 6 11:22:59 2010 for Source-highlight Qt Library by  doxygen 1.6.1