1
0

fb2code.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. #include "fb2code.hpp"
  2. #include "fb2dlgs.hpp"
  3. #include <QXmlInputSource>
  4. #include <QtGui>
  5. static const QColor DEFAULT_SYNTAX_CHAR = Qt::blue;
  6. static const QColor DEFAULT_ELEMENT_NAME = Qt::darkRed;
  7. static const QColor DEFAULT_COMMENT = Qt::darkGray;
  8. static const QColor DEFAULT_ATTRIBUTE_NAME = Qt::red;
  9. static const QColor DEFAULT_ATTRIBUTE_VALUE = Qt::darkGreen;
  10. static const QColor DEFAULT_ERROR = Qt::darkMagenta;
  11. static const QColor DEFAULT_OTHER = Qt::black;
  12. // Regular expressions for parsing XML borrowed from:
  13. // http://www.cs.sfu.ca/~cameron/REX.html
  14. static const QString EXPR_COMMENT = "<!--[^-]*-([^-][^-]*-)*->";
  15. static const QString EXPR_COMMENT_BEGIN = "<!--";
  16. static const QString EXPR_COMMENT_END = "[^-]*-([^-][^-]*-)*->";
  17. static const QString EXPR_ATTRIBUTE_VALUE = "\"[^<\"]*\"|'[^<']*'";
  18. static const QString EXPR_NAME = "([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*";
  19. FbHighlighter::FbHighlighter(QObject* parent)
  20. : QSyntaxHighlighter(parent)
  21. {
  22. init();
  23. }
  24. FbHighlighter::FbHighlighter(QTextDocument* parent)
  25. : QSyntaxHighlighter(parent)
  26. {
  27. init();
  28. }
  29. FbHighlighter::FbHighlighter(QTextEdit* parent)
  30. : QSyntaxHighlighter(parent)
  31. {
  32. init();
  33. }
  34. FbHighlighter::~FbHighlighter()
  35. {
  36. }
  37. void FbHighlighter::init()
  38. {
  39. fmtSyntaxChar.setForeground(DEFAULT_SYNTAX_CHAR);
  40. fmtElementName.setForeground(DEFAULT_ELEMENT_NAME);
  41. fmtComment.setForeground(DEFAULT_COMMENT);
  42. fmtAttributeName.setForeground(DEFAULT_ATTRIBUTE_NAME);
  43. fmtAttributeValue.setForeground(DEFAULT_ATTRIBUTE_VALUE);
  44. fmtError.setForeground(DEFAULT_ERROR);
  45. fmtOther.setForeground(DEFAULT_OTHER);
  46. }
  47. void FbHighlighter::setHighlightColor(HighlightType type, QColor color, bool foreground)
  48. {
  49. QTextCharFormat format;
  50. if (foreground)
  51. format.setForeground(color);
  52. else
  53. format.setBackground(color);
  54. setHighlightFormat(type, format);
  55. }
  56. void FbHighlighter::setHighlightFormat(HighlightType type, QTextCharFormat format)
  57. {
  58. switch (type)
  59. {
  60. case SyntaxChar:
  61. fmtSyntaxChar = format;
  62. break;
  63. case ElementName:
  64. fmtElementName = format;
  65. break;
  66. case Comment:
  67. fmtComment = format;
  68. break;
  69. case AttributeName:
  70. fmtAttributeName = format;
  71. break;
  72. case AttributeValue:
  73. fmtAttributeValue = format;
  74. break;
  75. case Error:
  76. fmtError = format;
  77. break;
  78. case Other:
  79. fmtOther = format;
  80. break;
  81. }
  82. rehighlight();
  83. }
  84. void FbHighlighter::highlightBlock(const QString& text)
  85. {
  86. int i = 0;
  87. int pos = 0;
  88. int brackets = 0;
  89. state = (previousBlockState() == InElement ? ExpectAttributeOrEndOfElement : NoState);
  90. if (previousBlockState() == InComment)
  91. {
  92. // search for the end of the comment
  93. QRegExp expression(EXPR_COMMENT_END);
  94. pos = expression.indexIn(text, i);
  95. if (pos >= 0)
  96. {
  97. // end comment found
  98. const int iLength = expression.matchedLength();
  99. setFormat(0, iLength - 3, fmtComment);
  100. setFormat(iLength - 3, 3, fmtSyntaxChar);
  101. i += iLength; // skip comment
  102. }
  103. else
  104. {
  105. // in comment
  106. setFormat(0, text.length(), fmtComment);
  107. setCurrentBlockState(InComment);
  108. return;
  109. }
  110. }
  111. for (; i < text.length(); i++)
  112. {
  113. switch (text.at(i).toAscii())
  114. {
  115. case '<':
  116. brackets++;
  117. if (brackets == 1)
  118. {
  119. setFormat(i, 1, fmtSyntaxChar);
  120. state = ExpectElementNameOrSlash;
  121. }
  122. else
  123. {
  124. // wrong bracket nesting
  125. setFormat(i, 1, fmtError);
  126. }
  127. break;
  128. case '>':
  129. brackets--;
  130. if (brackets == 0)
  131. {
  132. setFormat(i, 1, fmtSyntaxChar);
  133. }
  134. else
  135. {
  136. // wrong bracket nesting
  137. setFormat( i, 1, fmtError);
  138. }
  139. state = NoState;
  140. break;
  141. case '/':
  142. if (state == ExpectElementNameOrSlash)
  143. {
  144. state = ExpectElementName;
  145. setFormat(i, 1, fmtSyntaxChar);
  146. }
  147. else
  148. {
  149. if (state == ExpectAttributeOrEndOfElement)
  150. {
  151. setFormat(i, 1, fmtSyntaxChar);
  152. }
  153. else
  154. {
  155. processDefaultText(i, text);
  156. }
  157. }
  158. break;
  159. case '=':
  160. if (state == ExpectEqual)
  161. {
  162. state = ExpectAttributeValue;
  163. setFormat(i, 1, fmtOther);
  164. }
  165. else
  166. {
  167. processDefaultText(i, text);
  168. }
  169. break;
  170. case '\'':
  171. case '\"':
  172. if (state == ExpectAttributeValue)
  173. {
  174. // search attribute value
  175. QRegExp expression(EXPR_ATTRIBUTE_VALUE);
  176. pos = expression.indexIn(text, i);
  177. if (pos == i) // attribute value found ?
  178. {
  179. const int iLength = expression.matchedLength();
  180. setFormat(i, 1, fmtOther);
  181. setFormat(i + 1, iLength - 2, fmtAttributeValue);
  182. setFormat(i + iLength - 1, 1, fmtOther);
  183. i += iLength - 1; // skip attribute value
  184. state = ExpectAttributeOrEndOfElement;
  185. }
  186. else
  187. {
  188. processDefaultText(i, text);
  189. }
  190. }
  191. else
  192. {
  193. processDefaultText(i, text);
  194. }
  195. break;
  196. case '!':
  197. if (state == ExpectElementNameOrSlash)
  198. {
  199. // search comment
  200. QRegExp expression(EXPR_COMMENT);
  201. pos = expression.indexIn(text, i - 1);
  202. if (pos == i - 1) // comment found ?
  203. {
  204. const int iLength = expression.matchedLength();
  205. setFormat(pos, 4, fmtSyntaxChar);
  206. setFormat(pos + 4, iLength - 7, fmtComment);
  207. setFormat(iLength - 3, 3, fmtSyntaxChar);
  208. i += iLength - 2; // skip comment
  209. state = NoState;
  210. brackets--;
  211. }
  212. else
  213. {
  214. // Try find multiline comment
  215. QRegExp expression(EXPR_COMMENT_BEGIN); // search comment start
  216. pos = expression.indexIn(text, i - 1);
  217. //if (pos == i - 1) // comment found ?
  218. if (pos >= i - 1)
  219. {
  220. setFormat(i, 3, fmtSyntaxChar);
  221. setFormat(i + 3, text.length() - i - 3, fmtComment);
  222. setCurrentBlockState(InComment);
  223. return;
  224. }
  225. else
  226. {
  227. processDefaultText(i, text);
  228. }
  229. }
  230. }
  231. else
  232. {
  233. processDefaultText(i, text);
  234. }
  235. break;
  236. default:
  237. const int iLength = processDefaultText(i, text);
  238. if (iLength > 0)
  239. i += iLength - 1;
  240. break;
  241. }
  242. }
  243. if (state == ExpectAttributeOrEndOfElement)
  244. {
  245. setCurrentBlockState(InElement);
  246. }
  247. }
  248. int FbHighlighter::processDefaultText(int i, const QString& text)
  249. {
  250. // length of matched text
  251. int iLength = 0;
  252. switch(state)
  253. {
  254. case ExpectElementNameOrSlash:
  255. case ExpectElementName:
  256. {
  257. // search element name
  258. QRegExp expression(EXPR_NAME);
  259. const int pos = expression.indexIn(text, i);
  260. if (pos == i) // found ?
  261. {
  262. iLength = expression.matchedLength();
  263. setFormat(pos, iLength, fmtElementName);
  264. state = ExpectAttributeOrEndOfElement;
  265. }
  266. else
  267. {
  268. setFormat(i, 1, fmtOther);
  269. }
  270. }
  271. break;
  272. case ExpectAttributeOrEndOfElement:
  273. {
  274. // search attribute name
  275. QRegExp expression(EXPR_NAME);
  276. const int pos = expression.indexIn(text, i);
  277. if (pos == i) // found ?
  278. {
  279. iLength = expression.matchedLength();
  280. setFormat(pos, iLength, fmtAttributeName);
  281. state = ExpectEqual;
  282. }
  283. else
  284. {
  285. setFormat(i, 1, fmtOther);
  286. }
  287. }
  288. break;
  289. default:
  290. setFormat(i, 1, fmtOther);
  291. break;
  292. }
  293. return iLength;
  294. }
  295. qreal FbCodeEdit::baseFontSize = 10;
  296. qreal FbCodeEdit::zoomRatioMin = 0.2;
  297. qreal FbCodeEdit::zoomRatioMax = 5.0;
  298. FbCodeEdit::FbCodeEdit(QWidget *parent) : QPlainTextEdit(parent)
  299. {
  300. lineNumberArea = new LineNumberArea(this);
  301. highlighter = new FbHighlighter(this);
  302. highlighter->setDocument( document() );
  303. connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
  304. connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
  305. connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
  306. zoomRatio = 1;
  307. #ifdef Q_WS_WIN
  308. setFont(QFont("Courier New", baseFontSize));
  309. #else
  310. setFont(QFont("Monospace", baseFontSize));
  311. #endif
  312. updateLineNumberAreaWidth(0);
  313. highlightCurrentLine();
  314. }
  315. bool FbCodeEdit::read(QIODevice *device)
  316. {
  317. QByteArray data = device->readAll();
  318. QXmlInputSource source;
  319. source.setData(data);
  320. setPlainText(source.data());
  321. return true;
  322. }
  323. int FbCodeEdit::lineNumberAreaWidth()
  324. {
  325. int digits = 1;
  326. int max = qMax(1, blockCount());
  327. while (max >= 10) {
  328. max /= 10;
  329. ++digits;
  330. }
  331. int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
  332. return space;
  333. }
  334. void FbCodeEdit::updateLineNumberAreaWidth(int /* newBlockCount */)
  335. {
  336. setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
  337. }
  338. void FbCodeEdit::updateLineNumberArea(const QRect &rect, int dy)
  339. {
  340. if (dy)
  341. lineNumberArea->scroll(0, dy);
  342. else
  343. lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
  344. if (rect.contains(viewport()->rect()))
  345. updateLineNumberAreaWidth(0);
  346. }
  347. void FbCodeEdit::resizeEvent(QResizeEvent *e)
  348. {
  349. QPlainTextEdit::resizeEvent(e);
  350. QRect cr = contentsRect();
  351. lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
  352. }
  353. void FbCodeEdit::highlightCurrentLine()
  354. {
  355. QList<QTextEdit::ExtraSelection> extraSelections;
  356. if (!isReadOnly()) {
  357. QTextEdit::ExtraSelection selection;
  358. QColor lineColor = QColor(Qt::yellow).lighter(160);
  359. selection.format.setBackground(lineColor);
  360. selection.format.setProperty(QTextFormat::FullWidthSelection, true);
  361. selection.cursor = textCursor();
  362. selection.cursor.clearSelection();
  363. extraSelections.append(selection);
  364. }
  365. setExtraSelections(extraSelections);
  366. }
  367. void FbCodeEdit::lineNumberAreaPaintEvent(QPaintEvent *event)
  368. {
  369. QPainter painter(lineNumberArea);
  370. painter.fillRect(event->rect(), Qt::lightGray);
  371. QTextBlock block = firstVisibleBlock();
  372. int blockNumber = block.blockNumber();
  373. int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
  374. int bottom = top + (int) blockBoundingRect(block).height();
  375. while (block.isValid() && top <= event->rect().bottom()) {
  376. if (block.isVisible() && bottom >= event->rect().top()) {
  377. QString number = QString::number(blockNumber + 1);
  378. painter.setPen(Qt::black);
  379. painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
  380. Qt::AlignRight, number);
  381. }
  382. block = block.next();
  383. top = bottom;
  384. bottom = top + (int) blockBoundingRect(block).height();
  385. ++blockNumber;
  386. }
  387. }
  388. bool FbCodeEdit::findText(const QString &exp, QTextDocument::FindFlags options)
  389. {
  390. return QPlainTextEdit::find(exp, options);
  391. }
  392. void FbCodeEdit::find()
  393. {
  394. FbCodeFindDlg dlg(*this);
  395. dlg.exec();
  396. }
  397. void FbCodeEdit::zoomIn()
  398. {
  399. qreal ratio = zoomRatio * 1.1;
  400. ratio = qMin(ratio, zoomRatioMax);
  401. setZoomRatio(ratio);
  402. }
  403. void FbCodeEdit::zoomOut()
  404. {
  405. qreal ratio = zoomRatio / 1.1;
  406. ratio = qMax(ratio, zoomRatioMin);
  407. setZoomRatio(ratio);
  408. }
  409. void FbCodeEdit::zoomReset()
  410. {
  411. setZoomRatio(1.0);
  412. }
  413. void FbCodeEdit::setZoomRatio(qreal ratio)
  414. {
  415. if (!qFuzzyCompare(1 + zoomRatio, 1 + ratio)) {
  416. zoomRatio = ratio;
  417. QFont f = font();
  418. f.setPointSizeF(baseFontSize * zoomRatio);
  419. setFont(f);
  420. }
  421. }