columnedit.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "columnedit.h"
  2. #include "scintillaeditview.h"
  3. ColumnEdit::ColumnEdit(QWidget *parent)
  4. : QWidget(parent), m_curEditWin(nullptr), m_editTabWidget(nullptr)
  5. {
  6. ui.setupUi(this);
  7. connect(ui.addPrefix, &QCheckBox::stateChanged, this, &ColumnEdit::slot_addPrefix);
  8. connect(ui.is16, &QRadioButton::clicked, this, &ColumnEdit::slot_bigChar);
  9. }
  10. ColumnEdit::~ColumnEdit()
  11. {
  12. }
  13. void ColumnEdit::slot_insertTextEnable(bool check)
  14. {
  15. if (check)
  16. {
  17. if (ui.numGroupBox->isChecked())
  18. {
  19. ui.numGroupBox->setChecked(!check);
  20. }
  21. }
  22. }
  23. void ColumnEdit::slot_insertNumEnable(bool check)
  24. {
  25. if (check)
  26. {
  27. if (ui.textGroupBox->isChecked())
  28. {
  29. ui.textGroupBox->setChecked(!check);
  30. }
  31. }
  32. }
  33. void ColumnEdit::slot_addPrefix(int s)
  34. {
  35. if (s == Qt::Checked)
  36. {
  37. ui.prefix->setEnabled(true);
  38. }
  39. else
  40. {
  41. ui.prefix->setEnabled(false);
  42. }
  43. }
  44. void ColumnEdit::slot_bigChar(bool isCheck)
  45. {
  46. ui.capital->setEnabled(isCheck);
  47. }
  48. //自动调整当前窗口的状态
  49. QWidget* ColumnEdit::autoAdjustCurrentEditWin()
  50. {
  51. QWidget* pw = m_editTabWidget->currentWidget();
  52. if (m_curEditWin != pw)
  53. {
  54. m_curEditWin = pw;
  55. }
  56. return pw;
  57. }
  58. void ColumnEdit::setTabWidget(QTabWidget *editTabWidget)
  59. {
  60. m_editTabWidget = editTabWidget;
  61. }
  62. void ColumnEdit::slot_ok()
  63. {
  64. autoAdjustCurrentEditWin();
  65. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(m_curEditWin);
  66. if (pEdit == nullptr || pEdit->isReadOnly())
  67. {
  68. QApplication::beep();
  69. return;
  70. }
  71. QString text;
  72. bool isNum = false;
  73. int numType = 10;
  74. int initNum = 0;
  75. int inc = ui.incNum->value();
  76. int repeNum = ui.repeNum->value();
  77. bool isAddPrefix = ui.addPrefix->isChecked();
  78. QString prefix = ui.prefix->text();
  79. bool isCapital = ui.capital->isChecked();
  80. //是插入文本模式
  81. if (ui.textGroupBox->isChecked())
  82. {
  83. text = ui.inputText->text();
  84. }
  85. else
  86. {
  87. isNum = true;
  88. text = ui.initNum->text();
  89. bool ok = false;
  90. if (ui.is10->isChecked())
  91. {
  92. numType = 10;
  93. }
  94. else if (ui.is16->isChecked())
  95. {
  96. numType = 16;
  97. }
  98. else if (ui.is8->isChecked())
  99. {
  100. numType = 8;
  101. }
  102. else if (ui.is2->isChecked())
  103. {
  104. numType = 2;
  105. }
  106. int num = text.toInt(&ok, 10);
  107. initNum = num;
  108. if (ok)
  109. {
  110. text = QString::number(num, numType);
  111. if (isAddPrefix)
  112. {
  113. text = prefix + text;
  114. }
  115. }
  116. else
  117. {
  118. //这里要加个提示
  119. QApplication::beep();
  120. return;
  121. }
  122. }
  123. pEdit->execute(SCI_BEGINUNDOACTION);
  124. if (ui.textGroupBox->isChecked())
  125. {
  126. if (pEdit->execute(SCI_SELECTIONISRECTANGLE) || pEdit->execute(SCI_GETSELECTIONS) > 1)
  127. {
  128. ColumnModeInfos colInfos = pEdit->getColumnModeSelectInfo();
  129. std::sort(colInfos.begin(), colInfos.end(), SortInPositionOrder());
  130. QByteArray bytes = text.toUtf8();
  131. pEdit->columnReplace(colInfos, bytes);
  132. std::sort(colInfos.begin(), colInfos.end(), SortInSelectOrder());
  133. pEdit->setMultiSelections(colInfos);
  134. return;
  135. }
  136. }
  137. else
  138. {
  139. if (pEdit->execute(SCI_SELECTIONISRECTANGLE) || pEdit->execute(SCI_GETSELECTIONS) > 1)
  140. {
  141. ColumnModeInfos colInfos = pEdit->getColumnModeSelectInfo();
  142. // If there is no column mode info available, no need to do anything
  143. // If required a message can be shown to user, that select column properly or something similar
  144. if (colInfos.size() > 0)
  145. {
  146. std::sort(colInfos.begin(), colInfos.end(), SortInPositionOrder());
  147. QByteArray bytes;
  148. if (isAddPrefix)
  149. {
  150. bytes = prefix.toUtf8();
  151. }
  152. pEdit->columnReplace(colInfos, initNum, inc, repeNum, numType, isCapital, bytes);
  153. std::sort(colInfos.begin(), colInfos.end(), SortInSelectOrder());
  154. pEdit->setMultiSelections(colInfos);
  155. }
  156. return;
  157. }
  158. }
  159. auto cursorPos = pEdit->execute(SCI_GETCURRENTPOS);
  160. auto cursorCol = pEdit->execute(SCI_GETCOLUMN, cursorPos);
  161. auto cursorLine = pEdit->execute(SCI_LINEFROMPOSITION, cursorPos);
  162. auto endPos = pEdit->execute(SCI_GETLENGTH);
  163. auto endLine = pEdit->execute(SCI_LINEFROMPOSITION, endPos);
  164. QByteArray lineData;
  165. int rn = repeNum;
  166. for (size_t i = cursorLine; i <= static_cast<size_t>(endLine); ++i)
  167. {
  168. auto lineBegin = pEdit->execute(SCI_POSITIONFROMLINE, i);
  169. auto lineEnd = pEdit->execute(SCI_GETLINEENDPOSITION, i);
  170. auto lineEndCol = pEdit->execute(SCI_GETCOLUMN, lineEnd);
  171. auto lineLen = lineEnd - lineBegin;
  172. lineData.resize(lineLen);
  173. Sci_TextRange lineText;
  174. lineText.chrg.cpMin = static_cast<Sci_Position>(lineBegin);
  175. lineText.chrg.cpMax = static_cast<Sci_Position>(lineEnd);
  176. lineText.lpstrText = lineData.data();
  177. //获取原始行的内容
  178. pEdit->SendScintilla(SCI_GETTEXTRANGE, 0, &lineText);
  179. if (lineEndCol < cursorCol)
  180. {
  181. QByteArray s_space(cursorCol - lineEndCol, ' ');
  182. lineData.append(s_space);
  183. lineData.append(text.toUtf8());
  184. }
  185. else
  186. {
  187. int posAbs2Start = pEdit->execute(SCI_FINDCOLUMN, i, cursorCol);
  188. int posRelative2Start = posAbs2Start - lineBegin;
  189. lineData.insert(posRelative2Start, text.toUtf8());
  190. }
  191. pEdit->SendScintilla(SCI_SETTARGETRANGE, lineBegin, lineEnd);
  192. pEdit->SendScintilla(SCI_REPLACETARGET, lineData.size(), lineData.data());
  193. if (isNum)
  194. {
  195. --rn;
  196. if (rn > 0)
  197. {
  198. }
  199. else
  200. {
  201. rn = repeNum;
  202. initNum += inc;
  203. }
  204. if (numType != 16)
  205. {
  206. text = QString::number(initNum, numType);
  207. }
  208. else
  209. {
  210. if (isCapital)
  211. {
  212. text = QString::number(initNum, numType).toUpper();
  213. }
  214. else
  215. {
  216. text = QString::number(initNum, numType);
  217. }
  218. }
  219. if (isAddPrefix)
  220. {
  221. text = prefix + text;
  222. }
  223. }
  224. }
  225. pEdit->execute(SCI_ENDUNDOACTION);
  226. }