Source-highlight Qt Library
Qt4TextFormatter.h
00001 /*
00002  *  Copyright (C) 2008-2010  Lorenzo Bettini, http://www.lorenzobettini.it
00003  *  License: See COPYING file that comes with this distribution
00004  */
00005 
00006 #ifndef QT4TEXTFORMATTER_H_
00007 #define QT4TEXTFORMATTER_H_
00008 
00009 #include <QTextCharFormat>
00010 #include <QSyntaxHighlighter>
00011 
00012 #include "TextFormatter.h"
00013 
00014 namespace srchiliteqt {
00015 
00016 class Qt4SyntaxHighlighter;
00017 
00021 class Qt4TextFormatter: public TextFormatter {
00022 protected:
00026     QTextCharFormat textFormat;
00027 
00031     QColor foregroundColor;
00032 
00034     QColor backgroundColor;
00035 
00043     Qt4SyntaxHighlighter *qSyntaxHighlighter;
00044 
00045 public:
00046     Qt4TextFormatter(const std::string &elem_ = "normal");
00047     virtual ~Qt4TextFormatter();
00048 
00049     const QTextCharFormat &getQTextCharFormat() const {
00050         return textFormat;
00051     }
00052 
00053     virtual void setQSyntaxHighlighter(QSyntaxHighlighter *qSyntaxHighlighter_);
00054 
00061     void format(const std::string &s, const srchilite::FormatterParams *params);
00062 
00066     const QString toString() const;
00067 
00075     const QString toSourceHighlightStyleString() const;
00076 
00077     bool isBold() const {
00078         return textFormat.fontWeight() == QFont::Bold;
00079     }
00080 
00081     void setBold(bool b) {
00082         textFormat.setFontWeight(b ? QFont::Bold : QFont::Normal);
00083     }
00084 
00085     bool isItalic() const {
00086         return textFormat.fontItalic();
00087     }
00088 
00089     void setItalic(bool i) {
00090         textFormat.setFontItalic(i);
00091     }
00092 
00093     bool isUnderline() const {
00094         return textFormat.fontUnderline();
00095     }
00096 
00097     void setUnderline(bool u) {
00098         textFormat.setFontUnderline(u);
00099     }
00100 
00101     bool isMonospace() const {
00102         return textFormat.fontFamily() == "Monospace";
00103     }
00104 
00105     void setMonospace(bool m) {
00106         if (m)
00107             textFormat.setFontFamily("Monospace");
00108     }
00109 
00111     const QColor &getForegroundColor() const {
00112         return foregroundColor;
00113     }
00114 
00115     void setForegroundColor(const QColor &color);
00116 
00118     const QColor &getBackgroundColor() const {
00119         return backgroundColor;
00120     }
00121 
00122     void setBackgroundColor(const QColor &color);
00123 };
00124 
00125 }
00126 
00127 #endif /* QT4TEXTFORMATTER_H_ */