veditutils.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #ifndef VEDITUTILS_H
  2. #define VEDITUTILS_H
  3. #include <QTextBlock>
  4. #include <QTextCursor>
  5. class QTextDocument;
  6. class QTextEdit;
  7. class QPlainTextEdit;
  8. // Utils for text edit.
  9. class VEditUtils
  10. {
  11. public:
  12. // Remove the whole block @p_block.
  13. // If @p_text is not NULL, return the deleted text here.
  14. static void removeBlock(QTextBlock &p_block, QString *p_text = NULL);
  15. // Remove the whole block under @p_cursor.
  16. // If @p_text is not NULL, return the deleted text here.
  17. // Need to call setTextCursor() to make it take effect.
  18. static void removeBlock(QTextCursor &p_cursor, QString *p_text = NULL);
  19. // Move @p_cursor to the first non-space character of current block.
  20. // Need to call setTextCursor() to make it take effect.
  21. static void moveCursorFirstNonSpaceCharacter(QTextCursor &p_cursor,
  22. QTextCursor::MoveMode p_mode);
  23. // Indent current block as next/previous block.
  24. // Return true if some changes have been made.
  25. // @p_cursor will be placed at the position after inserting leading spaces.
  26. // @p_next: indent as next block or previous block.
  27. static bool indentBlockAsBlock(QTextCursor &p_cursor, bool p_next);
  28. // Returns true if two blocks has the same indent.
  29. static bool hasSameIndent(const QTextBlock &p_blocka, const QTextBlock &p_blockb);
  30. // Insert a new block at current position with the same indentation as
  31. // current block. Should clear the selection before calling this.
  32. // Returns true if non-empty indentation has been inserted.
  33. // Need to call setTextCursor() to make it take effect.
  34. static bool insertBlockWithIndent(QTextCursor &p_cursor);
  35. // Fetch the list mark of previous block, and insert it at current position.
  36. // Returns true if list mark has been inserted.
  37. // Need to call setTextCursor() to make it take effect.
  38. static bool insertListMarkAsPreviousBlock(QTextCursor &p_cursor);
  39. // Remove ObjectReplaceCharacter in p_text.
  40. // If the ObjectReplaceCharacter is in a block with only other spaces, remove the
  41. // whole block.
  42. static void removeObjectReplacementCharacter(QString &p_text);
  43. // p_cursor.selectedText() will use U+2029 (QChar::ParagraphSeparator)
  44. // instead of \n for a new line.
  45. // This function will translate it to \n.
  46. static QString selectedText(const QTextCursor &p_cursor);
  47. // Indent selected blocks. If no selection, indent current block.
  48. // @p_isIndent: whether it is indentation or unindentation.
  49. static void indentSelectedBlocks(const QTextDocument *p_doc,
  50. const QTextCursor &p_cursor,
  51. const QString &p_indentationText,
  52. bool p_isIndent);
  53. // Indent current block.
  54. // @p_skipEmpty: skip empty block.
  55. static void indentBlock(QTextCursor &p_cursor,
  56. const QString &p_indentationText,
  57. bool p_skipEmpty = true);
  58. static void unindentBlock(QTextCursor &p_cursor,
  59. const QString &p_indentationText);
  60. // Find @p_repeat th occurence of a char within a block.
  61. // Returns true if target is found.
  62. // Please pay attention to the one-step-forward/backward in KeepAnchor mode
  63. // and exclusive case.
  64. static bool findTargetWithinBlock(QTextCursor &p_cursor,
  65. QTextCursor::MoveMode p_mode,
  66. QChar p_target,
  67. bool p_forward,
  68. bool p_inclusive,
  69. int p_repeat);
  70. // Find th first occurence of a char in @p_targets within a block.
  71. // Returns the index of the found char in @p_targets if found.
  72. // Returns -1 if none of the @p_targets is found.
  73. // Please pay attention to the one-step-forward/backward in KeepAnchor mode
  74. // and exclusive case.
  75. static int findTargetsWithinBlock(QTextCursor &p_cursor,
  76. QTextCursor::MoveMode p_mode,
  77. const QList<QChar> &p_targets,
  78. bool p_forward,
  79. bool p_inclusive);
  80. // Find a pair target (@p_opening, @p_closing) containing current cursor and
  81. // select the range between them.
  82. // Need to call setTextCursor() to make it take effect.
  83. // Returns true if target is found.
  84. static bool selectPairTargetAround(QTextCursor &p_cursor,
  85. QChar p_opening,
  86. QChar p_closing,
  87. bool p_inclusive,
  88. bool p_crossBlock,
  89. int p_repeat);
  90. // Get the count of blocks selected.
  91. static int selectedBlockCount(const QTextCursor &p_cursor);
  92. // Scroll block @p_blockNum into the visual window.
  93. // @p_dest is the position of the window: 0 for top, 1 for center, 2 for bottom.
  94. // @p_blockNum is based on 0.
  95. // Will set the cursor to the block.
  96. static void scrollBlockInPage(QTextEdit *p_edit,
  97. int p_blockNum,
  98. int p_dest);
  99. // Scroll block @p_blockNum into the visual window.
  100. // @p_dest is the position of the window: 0 for top, 1 for center, 2 for bottom.
  101. // @p_blockNum is based on 0.
  102. // Will set the cursor to the block.
  103. static void scrollBlockInPage(QPlainTextEdit *p_edit,
  104. int p_blockNum,
  105. int p_dest);
  106. // Check if @p_block is a auto list block.
  107. // @p_seq will be the seq number of the ordered list, or -1.
  108. // Returns true if it is an auto list block.
  109. static bool isListBlock(const QTextBlock &p_block, int *p_seq = NULL);
  110. // If the start of @p_block to postition @p_posInBlock are spaces.
  111. static bool isSpaceToBlockStart(const QTextBlock &p_block, int p_posInBlock);
  112. // If block @p_block only contains spaces.
  113. static bool isSpaceBlock(const QTextBlock &p_block);
  114. // @p_cursor is positioned right after auto indetn and auto list.
  115. // Need to call setTextCursor() to make it take effect.
  116. static void deleteIndentAndListMark(QTextCursor &p_cursor);
  117. // Find next @p_repeat empty block.
  118. // Returns the position of that block if found. Otherwise, returns -1.
  119. static int findNextEmptyBlock(const QTextCursor &p_cursor,
  120. bool p_forward,
  121. int p_repeat);
  122. // Check if we need to cancel auto indent.
  123. // @p_autoIndentPos: the position of the cursor after auto indent.
  124. static bool needToCancelAutoIndent(int p_autoIndentPos,
  125. const QTextCursor &p_cursor);
  126. // Insert title Mark at level @p_level in front of block @p_block
  127. // If there already exists title marks, remove it first.
  128. // Move cursor at the end of the block after insertion.
  129. // If @p_level is 0, remove the title mark.
  130. static void insertTitleMark(QTextCursor &p_cursor,
  131. const QTextBlock &p_block,
  132. int p_level);
  133. // Find the start and end of the word @p_cursor locates in (within a single block).
  134. // @p_start and @p_end will be the global position of the start and end of the word.
  135. // @p_start will equals to @p_end if @p_cursor is a space.
  136. static void findCurrentWord(QTextCursor p_cursor,
  137. int &p_start,
  138. int &p_end);
  139. // Find the start and end of the WORD @p_cursor locates in (within a single block).
  140. // @p_start and @p_end will be the global position of the start and end of the WORD.
  141. // @p_start will equals to @p_end if @p_cursor is a space.
  142. // Attention: www|sss will select www, which is different from findCurrentWord().
  143. static void findCurrentWORD(const QTextCursor &p_cursor,
  144. int &p_start,
  145. int &p_end);
  146. // Return the leading spaces of @p_block.
  147. static QString fetchIndentSpaces(const QTextBlock &p_block);
  148. // Insert a block above/below current block. Move the cursor to the start of
  149. // the new block after insertion.
  150. static void insertBlock(QTextCursor &p_cursor,
  151. bool p_above);
  152. private:
  153. VEditUtils() {}
  154. };
  155. #endif // VEDITUTILS_H