File indexing completed on 2025-01-12 06:47:30
0001 // 0002 // C++ Interface: ctextchunk 0003 // 0004 // Description: 0005 // 0006 /* 0007 Copyright 2004-2011 Tomas Mecir <kmuddy@kmuddy.com> 0008 0009 This program is free software; you can redistribute it and/or 0010 modify it under the terms of the GNU General Public License as 0011 published by the Free Software Foundation; either version 2 of 0012 the License, or (at your option) any later version. 0013 0014 This program is distributed in the hope that it will be useful, 0015 but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0017 GNU General Public License for more details. 0018 0019 You should have received a copy of the GNU General Public License 0020 along with this program. If not, see <http://www.gnu.org/licenses/>. 0021 */ 0022 0023 #ifndef CTEXTCHUNK_H 0024 #define CTEXTCHUNK_H 0025 0026 #include <QColor> 0027 #include <QDateTime> 0028 #include <QFont> 0029 #include <QString> 0030 #include <QTextCharFormat> 0031 #include <QTextCursor> 0032 0033 #include <list> 0034 #include <kmuddy_export.h> 0035 0036 using namespace std; 0037 0038 class cANSIParser; 0039 class cConsole; 0040 class cTextChunk; 0041 0042 class QPainter; 0043 0044 // struct paintStatus; 0045 0046 /** one item in a cTextChunk chunk - abstract base class */ 0047 0048 class KMUDDY_EXPORT chunkItem { 0049 public: 0050 chunkItem () : startpos(0), _chunk(nullptr) {} 0051 virtual ~chunkItem() {}; 0052 virtual int type() = 0; 0053 int startPos () { return startpos; } 0054 void setStartPos (int sp) { startpos = sp; } 0055 virtual int length() = 0; 0056 virtual chunkItem *split (int) { return nullptr; } 0057 virtual void trimLeft () {}; 0058 virtual chunkItem *duplicate() = 0; 0059 0060 virtual void replace (int, int, const QString &) {}; 0061 0062 //painting the text... 0063 // virtual void paint (QPainter *painter, paintStatus *ps) = 0; 0064 0065 //output to transcript... 0066 /** plain-text output */ 0067 virtual QString toText () { return QString(); }; 0068 /** output to plain-text with ANSI sequences */ 0069 virtual QString toAnsi (cANSIParser *) { return QString(); }; 0070 /** output to HTML, suffix can be used to provide closing tags if needed */ 0071 virtual QString toHTML (QString &) { return QString(); }; 0072 0073 /** Insert self into a text document at the QTextCursor's position */ 0074 virtual void insertToDocument (QTextCursor &, QTextCharFormat &) = 0; 0075 protected: 0076 // void paintText (const QString &text, QPainter *painter, QFont font, QColor fg, QColor bg, paintStatus *ps); 0077 int startpos; 0078 cTextChunk *_chunk; 0079 friend class cTextChunk; 0080 }; 0081 0082 /** one color change made by color triggers */ 0083 0084 struct colorChange { 0085 int start, len; 0086 QColor fg, bg; 0087 bool keepfg, keepbg; 0088 }; 0089 0090 /** starting chunk attributes */ 0091 0092 struct chunkStart { 0093 int startpos; 0094 QColor fg, bg; 0095 int attrib; 0096 }; 0097 0098 0099 /** 0100 One chunk of text, including attributes, links and all that stuff. 0101 Contains methods to break lines and so on. 0102 0103 @author Tomas Mecir 0104 */ 0105 0106 class KMUDDY_EXPORT cTextChunk { 0107 public: 0108 /** constructor */ 0109 cTextChunk (cConsole *_console); 0110 /** constructor that also adds some text */ 0111 cTextChunk (cConsole *_console, const QString &text); 0112 /** destructor */ 0113 ~cTextChunk (); 0114 /** get entries... */ 0115 list<chunkItem *> entries() { return _entries; }; 0116 0117 /** get starting attributes */ 0118 chunkStart startAttr () { return startattr; }; 0119 /** set starting attributes */ 0120 void setStartAttr (chunkStart attr) { startattr = attr; }; 0121 0122 /** append entry to the chunk */ 0123 void appendEntry (chunkItem *entry); 0124 /** append chunk2 to this chunk; chunk2 will then be DELETED */ 0125 void append (cTextChunk *chunk2); 0126 0127 /** returns chunkItem at a given position */ 0128 chunkItem *itemAt (int pos); 0129 0130 /** split the line in two, this object will become line 1, method returns line 2 0131 Method returns 0 if current text is not long enough. */ 0132 cTextChunk *splitLine (int idx, bool wordwrap = true, int indent = 0, bool trimSpaces = true); 0133 0134 /** apply color changes made by color triggers, keep everything else intact */ 0135 void applyColorChanges (list<colorChange> &changes); 0136 0137 /** replace text at given position with a new text */ 0138 void replace (int pos, int len, const QString newtext); 0139 0140 /** expire all links with given name, or all named links if no name given */ 0141 bool expireNamedLinks (const QString &name = QString()); 0142 0143 /** return plain-text version of this chunk */ 0144 QString plainText (); 0145 0146 QStringList words (int minLength = 3); 0147 0148 /** length of the chunk, EXCLUDING start position */ 0149 int length (); 0150 0151 /** simplify the chunk, removing unneeded items and merging where possible */ 0152 void simplify (); 0153 0154 /** create an exact copy of this chunk */ 0155 cTextChunk *duplicate (); 0156 0157 //painting... 0158 // void paint (int length, int selstart, int sellen, int charWidth, int charHeight, QPainter *painter, QPainter *blinkpainter = 0); 0159 0160 //output to transcript... 0161 /** plain-text output */ 0162 QString toText (); 0163 /** output to plain-text with ANSI sequences */ 0164 QString toAnsi (cANSIParser *ap); 0165 /** output to HTML */ 0166 QString toHTML (); 0167 0168 void insertToDocument (QTextCursor &cursor); 0169 //get timestamp in a textual form 0170 QDateTime getTimeStamp (); 0171 0172 /** create one line that's all in one color */ 0173 static cTextChunk *makeLine (const QString &text, QColor fg, QColor bg, cConsole *console); 0174 protected: 0175 void init (cConsole *_console); 0176 void fixupStartPositions (); 0177 0178 list<chunkItem *> _entries; 0179 0180 /** starting attributes... */ 0181 chunkStart startattr; 0182 0183 /** cConsole object, used when needed */ 0184 cConsole *console; 0185 0186 /** timestamp, used in console's tooltip */ 0187 QDateTime timestamp; 0188 0189 /** paint status used by paint() */ 0190 // paintStatus *pstatus; 0191 }; 0192 0193 0194 //chunkItem-derived classes... 0195 0196 #define CHUNK_TEXT 1 0197 class KMUDDY_EXPORT chunkText : public chunkItem { 0198 public: 0199 int type() override { return CHUNK_TEXT; }; 0200 0201 const QString &text() { return _text; } 0202 void setText (const QString &t) { _text = t; } 0203 int length() override { return _text.length(); } 0204 //pos is index of last index that will remain in this item 0205 chunkItem *split (int pos) override; 0206 chunkItem *duplicate() override; 0207 void trimLeft () override; 0208 void replace (int pos, int len, const QString &newtext) override; 0209 0210 void insertToDocument (QTextCursor &cursor, QTextCharFormat &format) override { cursor.insertText (_text, format); }; 0211 0212 //painting 0213 // virtual void paint (QPainter *painter, paintStatus *ps) override; 0214 0215 //output to transcript... 0216 /** plain-text output */ 0217 QString toText () override { return _text; }; 0218 /** output to plain-text with ANSI sequences */ 0219 QString toAnsi (cANSIParser *) override {return _text; }; 0220 /** output to HTML, suffix can be used to provide closing tags if needed */ 0221 QString toHTML (QString &) override; 0222 protected: 0223 QString _text; 0224 }; 0225 0226 #define CHUNK_FG 2 0227 class KMUDDY_EXPORT chunkFg : public chunkItem { 0228 public: 0229 int type() override { return CHUNK_FG; }; 0230 0231 QColor fg() { return _fg; } 0232 void setFg (QColor fgc) { _fg = fgc; } 0233 int length() override { return 0; } 0234 chunkItem *duplicate() override; 0235 0236 //painting 0237 // virtual void paint (QPainter *painter, paintStatus *ps) override; 0238 0239 void insertToDocument (QTextCursor &, QTextCharFormat &format) override { setFormat (format, _fg); }; 0240 0241 //output to transcript... 0242 /** output to plain-text with ANSI sequences */ 0243 QString toAnsi (cANSIParser *ap) override; 0244 /** output to HTML, suffix can be used to provide closing tags if needed */ 0245 QString toHTML (QString &suffix) override; 0246 0247 static void setFormat (QTextCharFormat &format, QColor color); 0248 static QString constructAnsi (QColor color, cANSIParser *ap); 0249 static QString constructHTML (QColor color, QString &suffix); 0250 protected: 0251 QColor _fg; 0252 }; 0253 0254 #define CHUNK_BG 3 0255 class KMUDDY_EXPORT chunkBg : public chunkItem { 0256 public: 0257 int type() override { return CHUNK_BG; }; 0258 0259 QColor bg() { return _bg; } 0260 void setBg (QColor bgc) { _bg = bgc; } 0261 int length() override { return 0; } 0262 chunkItem *duplicate() override; 0263 0264 void insertToDocument (QTextCursor &, QTextCharFormat &format) override { setFormat (format, _bg); }; 0265 0266 //painting 0267 // virtual void paint (QPainter *painter, paintStatus *ps) override; 0268 0269 //output to transcript... 0270 /** output to plain-text with ANSI sequences */ 0271 QString toAnsi (cANSIParser *ap) override; 0272 /** output to HTML, suffix can be used to provide closing tags if needed */ 0273 QString toHTML (QString &suffix) override; 0274 0275 static void setFormat (QTextCharFormat &format, QColor color); 0276 static QString constructAnsi (QColor color, cANSIParser *ap); 0277 static QString constructHTML (QColor color, QString &suffix); 0278 protected: 0279 QColor _bg; 0280 }; 0281 0282 //attributes... 0283 0284 #define ATTRIB_BOLD 1 0285 #define ATTRIB_ITALIC 2 0286 #define ATTRIB_UNDERLINE 4 0287 #define ATTRIB_STRIKEOUT 8 0288 #define ATTRIB_BLINK 16 0289 #define ATTRIB_NEGATIVE 32 0290 #define ATTRIB_INVISIBLE 64 0291 0292 #define CHUNK_ATTRIB 4 0293 class KMUDDY_EXPORT chunkAttrib : public chunkItem { 0294 public: 0295 int type() override { return CHUNK_ATTRIB; }; 0296 int attrib() { return _attrib; } 0297 void setAttrib (int a) { _attrib = a; } 0298 int length() override { return 0; } 0299 chunkItem *duplicate() override; 0300 0301 //painting 0302 // virtual void paint (QPainter *painter, paintStatus *ps) override; 0303 0304 void insertToDocument (QTextCursor &, QTextCharFormat &format) override { setFormat (format, _attrib); }; 0305 0306 //output to transcript... 0307 /** output to plain-text with ANSI sequences */ 0308 QString toAnsi (cANSIParser *) override; 0309 0310 //no HTML output here - we cannot handle closing tags properly without too much hassle 0311 0312 static void setFormat (QTextCharFormat &format, int attrib); 0313 static QString constructAnsi (unsigned char attrib); 0314 protected: 0315 int _attrib; 0316 }; 0317 0318 struct menuItem { 0319 QString caption; 0320 QString command; 0321 }; 0322 0323 #define CHUNK_LINK 5 0324 class KMUDDY_EXPORT chunkLink : public chunkItem { 0325 public: 0326 int type() override { return CHUNK_LINK; }; 0327 0328 QString name() { return _name; } 0329 void setName (const QString &n) { _name = n; } 0330 QString target() { return _target; } 0331 void setTarget (const QString &t) { _target = t; } 0332 QString text() { return _text; } 0333 void setText (const QString &t) { _text = t; } 0334 QString hint() { return _hint; } 0335 void setHint (const QString &h) { _hint = h; } 0336 0337 bool isCommand () { return _iscommand; }; 0338 void setIsCommand (bool val) { _iscommand = val; }; 0339 bool toPrompt () { return _toprompt; }; 0340 void setToPrompt (bool val) { _toprompt = val; }; 0341 bool isMenu () { return _ismenu; }; 0342 void setIsMenu (bool val) { _ismenu = val; }; 0343 0344 /** parse menu information */ 0345 void parseMenu (); 0346 0347 int length() override { return _text.length(); } 0348 chunkItem *split (int pos) override; 0349 chunkItem *duplicate () override; 0350 void trimLeft () override; 0351 void replace (int pos, int len, const QString &newtext) override; 0352 0353 const list<menuItem> &menu() { return _menu; }; 0354 //painting 0355 // virtual void paint (QPainter *painter, paintStatus *ps) override; 0356 0357 void insertToDocument (QTextCursor &cursor, QTextCharFormat &format) override; 0358 0359 //output to transcript... 0360 /** output to plain-text with ANSI sequences */ 0361 QString toAnsi (cANSIParser *ap) override; 0362 /** output to HTML, suffix can be used to provide closing tags if needed */ 0363 QString toHTML (QString &suffix) override; 0364 protected: 0365 QString _name, _target, _text, _hint; 0366 bool _iscommand, _toprompt, _ismenu; 0367 0368 list<menuItem> _menu; 0369 0370 static QColor linkColor; 0371 }; 0372 0373 0374 #endif