findcmpwin.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "findcmpwin.h"
  2. #include "qscidisplaywindow.h"
  3. #include <QRadioButton>
  4. #include <QMessageBox>
  5. FindCmpWin::FindCmpWin(RC_DIRECTION dir, QWidget *parent):QMainWindow(parent), m_editWidget(nullptr), m_isFindFirst(true), m_findHistory(nullptr)
  6. {
  7. ui.setupUi(this);
  8. m_BackwardDir = false;
  9. m_matchWhole = false;
  10. m_matchCase = false;
  11. m_matchWrap = true;
  12. m_searchMode = 1;
  13. if (dir == RC_LEFT)
  14. {
  15. ui.leftSearch->setChecked(true);
  16. }
  17. else
  18. {
  19. ui.rightSearch->setChecked(true);
  20. }
  21. connect(ui.leftSearch, &QRadioButton::toggled, this, &FindCmpWin::slot_isSearchLeft);
  22. connect(ui.findModeRegularBt, &QRadioButton::toggled, this, &FindCmpWin::slot_findModeRegularBtChange);
  23. }
  24. FindCmpWin::~FindCmpWin()
  25. {
  26. }
  27. void FindCmpWin::slot_isSearchLeft(bool checked)
  28. {
  29. emit sgin_searchDirectionChange(checked ? RC_LEFT : RC_RIGHT);
  30. }
  31. void FindCmpWin::setCurrentTab(FindTabIndex index)
  32. {
  33. ui.findinfilesTab->setCurrentIndex(index);
  34. if (FIND_TAB == index)
  35. {
  36. ui.findComboBox->setFocus();
  37. }
  38. }
  39. void FindCmpWin::setWorkEdit(QsciDisplayWindow *editWidget)
  40. {
  41. m_editWidget = editWidget;
  42. m_isFindFirst = true;
  43. }
  44. void FindCmpWin::setFindText(QString &text)
  45. {
  46. ui.findComboBox->setEditText(text);
  47. }
  48. void FindCmpWin::setFindHistory(QList<QString>* findHistory)
  49. {
  50. m_findHistory = findHistory;
  51. if ((m_findHistory != nullptr) && !m_findHistory->isEmpty())
  52. {
  53. ui.findComboBox->addItems(*m_findHistory);
  54. ui.findComboBox->clearEditText();
  55. }
  56. }
  57. //从ui读取参数配置到成员变量
  58. void FindCmpWin::updateParameterFromUI()
  59. {
  60. if (ui.findinfilesTab->currentIndex() == 0)
  61. {
  62. int searchMode = 0;
  63. if (ui.findModeNormalBt->isChecked())
  64. {
  65. searchMode = 1;
  66. }
  67. else if (ui.findModeRegularBt->isChecked())
  68. {
  69. searchMode = 2;
  70. }
  71. if (m_searchMode != searchMode)
  72. {
  73. m_searchMode = searchMode;
  74. m_isFindFirst = true;
  75. }
  76. if (m_expr != ui.findComboBox->currentText())
  77. {
  78. m_expr = ui.findComboBox->currentText();
  79. m_isFindFirst = true;
  80. }
  81. if (m_BackwardDir != ui.findBackwardBox->isChecked())
  82. {
  83. m_BackwardDir = ui.findBackwardBox->isChecked();
  84. m_isFindFirst = true;
  85. }
  86. if (m_matchWhole != ui.findMatchWholeBox->isChecked())
  87. {
  88. m_matchWhole = ui.findMatchWholeBox->isChecked();
  89. m_isFindFirst = true;
  90. }
  91. if (m_matchCase != ui.findMatchCaseBox->isChecked())
  92. {
  93. m_matchCase = ui.findMatchCaseBox->isChecked();
  94. m_isFindFirst = true;
  95. }
  96. if (m_matchWrap != ui.findWrapBox->isChecked())
  97. {
  98. m_matchWrap = ui.findWrapBox->isChecked();
  99. m_isFindFirst = true;
  100. }
  101. }
  102. m_re = ((m_searchMode == 2) ? true : false);
  103. if (m_cs != m_matchCase)
  104. {
  105. m_cs = m_matchCase;
  106. }
  107. if (m_wo != m_matchWhole)
  108. {
  109. m_wo = m_matchWhole;
  110. }
  111. if (m_wrap != m_matchWrap)
  112. {
  113. m_wrap = m_matchWrap;
  114. }
  115. m_forward = !m_BackwardDir;
  116. }
  117. void FindCmpWin::addFindHistory(QString &text)
  118. {
  119. if ((m_findHistory != nullptr) && (-1 == m_findHistory->indexOf(text)))
  120. {
  121. m_findHistory->push_front(text);
  122. ui.findComboBox->insertItem(0, text);
  123. }
  124. }
  125. //检查是否是第一次查找,凡是参数变化了,则认定为是第一次查找。
  126. //因为查找分firstFirst和firstNext,则是qscint特性决定的。所以正确识别第一次查找是必要的
  127. bool FindCmpWin::isFirstFind()
  128. {
  129. return m_isFindFirst;
  130. }
  131. //一旦修改条件发生变化,则认定为第一次查找
  132. void FindCmpWin::slot_findNext()
  133. {
  134. if (ui.findComboBox->currentText().isEmpty())
  135. {
  136. ui.statusbar->showMessage(tr("what find is null !"), 3000);
  137. return;
  138. }
  139. updateParameterFromUI();
  140. QsciDisplayWindow* pEdit = m_editWidget;
  141. //第一次查找
  142. if (isFirstFind())
  143. {
  144. if (pEdit != nullptr)
  145. {
  146. QString whatFind = ui.findComboBox->currentText();
  147. if (!pEdit->findFirst(whatFind, m_re, m_cs, m_wo, m_wrap, m_forward))
  148. {
  149. ui.statusbar->showMessage(tr("cant't find text \'%1\'").arg(m_expr),3000);
  150. }
  151. m_isFindFirst = false;
  152. //加入历史列表
  153. addFindHistory(whatFind);
  154. }
  155. }
  156. else
  157. {
  158. //查找下一个
  159. if (pEdit != nullptr)
  160. {
  161. if (!pEdit->findNext())
  162. {
  163. ui.statusbar->showMessage(tr("no more find text \'%1\'").arg(m_expr),3000);
  164. }
  165. }
  166. }
  167. }
  168. //
  169. //void FindCmpWin::addCurFindRecord(QsciDisplayWindow* pEdit, FindCmpRecords& recordRet)
  170. //{
  171. // int pos = pEdit->execute(SCI_GETCURRENTPOS);
  172. // int lineNum = pEdit->execute(SCI_LINEFROMPOSITION, pos);
  173. // int lineLens = pEdit->execute(SCI_LINELENGTH, lineNum);
  174. //
  175. // char* lineText = new char[lineLens + 1];
  176. // memset(lineText, 0, lineLens + 1);
  177. // pEdit->execute(SCI_GETCURLINE, lineLens, reinterpret_cast<sptr_t>(lineText));
  178. //
  179. // FindCmpRecord aRecord;
  180. // aRecord.lineNum = lineNum;
  181. // aRecord.pos = pos;
  182. // aRecord.lineContents = QString(lineText);
  183. //
  184. // delete[]lineText;
  185. //
  186. // recordRet.records.append(aRecord);
  187. //}
  188. void FindCmpWin::slot_findModeRegularBtChange(bool checked)
  189. {
  190. if (checked)
  191. {
  192. ui.findBackwardBox->setEnabled(false);
  193. ui.findBackwardBox->setChecked(false);
  194. ui.findMatchWholeBox->setEnabled(false);
  195. ui.findMatchWholeBox->setChecked(false);
  196. }
  197. else
  198. {
  199. ui.findBackwardBox->setEnabled(true);
  200. ui.findMatchWholeBox->setEnabled(true);
  201. }
  202. m_isFindFirst = true;
  203. }