findwin.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185
  1. #include "findwin.h"
  2. #include "scintillaeditview.h"
  3. #include "ccnotepad.h"
  4. #include "progresswin.h"
  5. #include "doctypelistview.h"
  6. #include "filemanager.h"
  7. #include "ccnotepad.h"
  8. #include "nddsetting.h"
  9. #include <QMimeDatabase>
  10. #include <QRadioButton>
  11. #include <QMessageBox>
  12. #include <QFileDialog>
  13. #include <functional>
  14. #include <BoostRegexSearch.h>
  15. #include <QClipboard>
  16. #include <QDebug>
  17. enum TAB_TYPES {
  18. FIND_TYPE=0,
  19. RELPACE_TYPE,
  20. DIR_FIND_TYPE,
  21. MARK_TYPE,
  22. };
  23. const int MAX_RECORD_KEY_LENGTH = 120;
  24. FindWin::FindWin(QWidget *parent):QMainWindow(parent), m_editTabWidget(nullptr), m_isFindFirst(true), m_findHistory(nullptr), \
  25. pEditTemp(nullptr), m_curEditWin(nullptr), m_isStatic(false), m_isReverseFind(false), m_pMainPad(parent)
  26. {
  27. ui.setupUi(this);
  28. //默认值要与界面初始值一样。
  29. m_BackwardDir = false;
  30. m_matchWhole = false;
  31. m_matchCase = false;
  32. m_matchWrap = true;
  33. m_extend = false;
  34. m_searchMode = 1;
  35. m_re = false;
  36. m_cs = false;
  37. m_wo = false;
  38. m_wrap = true;
  39. m_forward = true;;//是否向前查找。注意如果向后,要为false
  40. m_extend = false;
  41. m_isFound = false;
  42. connect(ui.findModeRegularBt, &QRadioButton::toggled, this, &FindWin::slot_findModeRegularBtChange);
  43. connect(ui.replaceModeRegularBt, &QRadioButton::toggled, this, &FindWin::slot_replaceModeRegularBtChange);
  44. connect(ui.dealFileType, &QCheckBox::stateChanged, this,&FindWin::slot_dealFileTypeChange);
  45. connect(ui.skipDir, &QCheckBox::stateChanged, this, &FindWin::slot_skipDirChange);
  46. connect(ui.clearBt, &QAbstractButton::clicked, this, &FindWin::sign_clearResult);
  47. connect(ui.findClearBt, &QAbstractButton::clicked, this, &FindWin::sign_clearResult);
  48. connect(ui.findinfilesTab, &QTabWidget::currentChanged, this, &FindWin::slot_tabIndexChange);
  49. #if 0 //这样是无效的,记住一下,不删除,避免后面再做无用功
  50. Qt::WindowFlags m_flags = windowFlags();
  51. //这样使得窗口在上面,不至于总是到后面被挡住。
  52. #if defined (Q_MAC_OS)
  53. setWindowFlags(m_flags | Qt::WindowStaysOnTopHint);
  54. #endif
  55. #endif
  56. raise();
  57. setFocusPolicy(Qt::StrongFocus);
  58. setFocus();
  59. ui.findinfilesTab->setAttribute(Qt::WA_StyledBackground);
  60. ui.findComboBox->installEventFilter(this);
  61. }
  62. FindWin::~FindWin()
  63. {
  64. m_findHistory = nullptr;
  65. if (pEditTemp != nullptr)
  66. {
  67. delete pEditTemp;
  68. pEditTemp = nullptr;
  69. }
  70. }
  71. void FindWin::slot_tabIndexChange(int index)
  72. {
  73. TAB_TYPES type = (TAB_TYPES)index;
  74. if (RELPACE_TYPE == type)
  75. {
  76. ui.replaceTextBox->setFocus();
  77. ui.replaceTextBox->lineEdit()->selectAll();
  78. if (ui.replaceTextBox->currentText().isEmpty() && !ui.findComboBox->currentText().isEmpty())
  79. {
  80. if (ui.findComboBox->currentText().size() < MAX_RECORD_KEY_LENGTH)
  81. {
  82. ui.replaceTextBox->setCurrentText(ui.findComboBox->currentText());
  83. }
  84. }
  85. }
  86. else if(FIND_TYPE == type)
  87. {
  88. ui.findComboBox->setFocus();
  89. ui.findComboBox->lineEdit()->selectAll();
  90. if (ui.findComboBox->currentText().isEmpty() && !ui.replaceTextBox->currentText().isEmpty())
  91. {
  92. if (ui.replaceTextBox->currentText().size() < MAX_RECORD_KEY_LENGTH)
  93. {
  94. ui.findComboBox->setCurrentText(ui.replaceTextBox->currentText());
  95. }
  96. }
  97. }
  98. else if (DIR_FIND_TYPE == type)
  99. {
  100. ui.dirFindWhat->setFocus();
  101. ui.dirFindWhat->lineEdit()->selectAll();
  102. }
  103. else if (MARK_TYPE == type)
  104. {
  105. ui.markTextBox->setFocus();
  106. ui.markTextBox->lineEdit()->selectAll();
  107. }
  108. m_isFindFirst = true;
  109. if (m_findHistory->isEmpty())
  110. {
  111. return;
  112. }
  113. }
  114. void FindWin::slot_dealFileTypeChange(int state)
  115. {
  116. if (state == Qt::Checked)
  117. {
  118. ui.fileType->setEnabled(true);
  119. if (ui.fileType->text().isEmpty())
  120. {
  121. ui.fileType->setText(ui.fileType->placeholderText());
  122. }
  123. ui.fileType->setFocus();
  124. ui.fileType->selectAll();
  125. }
  126. else
  127. {
  128. ui.fileType->setEnabled(false);
  129. }
  130. }
  131. void FindWin::slot_skipDirChange(int state)
  132. {
  133. if (state == Qt::Checked)
  134. {
  135. ui.skipDirNames->setEnabled(true);
  136. if (ui.skipDirNames->text().isEmpty())
  137. {
  138. ui.skipDirNames->setText(ui.skipDirNames->placeholderText());
  139. }
  140. ui.skipDirNames->setFocus();
  141. ui.skipDirNames->selectAll();
  142. }
  143. else
  144. {
  145. ui.skipDirNames->setEnabled(false);
  146. }
  147. }
  148. //自动调整当前窗口的状态,如果发生了变化,则需要认定为是首次查找
  149. QWidget* FindWin::autoAdjustCurrentEditWin()
  150. {
  151. QWidget* pw = m_editTabWidget->currentWidget();
  152. if (m_curEditWin != pw)
  153. {
  154. m_curEditWin = pw;
  155. m_isFindFirst = true;
  156. }
  157. return pw;
  158. }
  159. void FindWin::setCurrentTab(FindTabIndex index)
  160. {
  161. ui.findinfilesTab->setCurrentIndex(index);
  162. if (FIND_TAB == index)
  163. {
  164. ui.findComboBox->setFocus();
  165. ui.findComboBox->lineEdit()->selectAll();
  166. }
  167. else if(REPLACE_TAB == index)
  168. {
  169. ui.replaceTextBox->setFocus();
  170. ui.replaceTextBox->lineEdit()->selectAll();
  171. }
  172. raise();
  173. }
  174. void FindWin::setTabWidget(QTabWidget *editTabWidget)
  175. {
  176. m_editTabWidget = editTabWidget;
  177. }
  178. void FindWin::setFindText(QString &text)
  179. {
  180. ui.findComboBox->setEditText(text);
  181. addFindHistory(text);
  182. }
  183. void FindWin::keywordWinGetFouse(FindTabIndex tabIndex)
  184. {
  185. switch (tabIndex)
  186. {
  187. case FIND_TAB:
  188. this->setFocus();
  189. ui.findComboBox->setFocus();
  190. ui.findComboBox->lineEdit()->selectAll();
  191. break;
  192. case REPLACE_TAB:
  193. ui.replaceTextBox->setFocus();
  194. ui.replaceTextBox->lineEdit()->selectAll();
  195. break;
  196. case DIR_FIND_TAB:
  197. ui.dirFindWhat->setFocus();
  198. ui.dirFindWhat->lineEdit()->selectAll();
  199. break;
  200. case MARK_TAB:
  201. ui.markTextBox->setFocus();
  202. ui.markTextBox->lineEdit()->selectAll();
  203. break;
  204. default:
  205. break;
  206. }
  207. }
  208. void FindWin::setReplaceFindText(QString& text)
  209. {
  210. ui.replaceTextBox->setEditText(text);
  211. addFindHistory(text);
  212. }
  213. void FindWin::setDirFindText(QString& text)
  214. {
  215. ui.dirFindWhat->setEditText(text);
  216. addFindHistory(text);
  217. }
  218. void FindWin::setMarkFindText(QString& text)
  219. {
  220. ui.markTextBox->setEditText(text);
  221. addFindHistory(text);
  222. }
  223. void FindWin::disableReplace()
  224. {
  225. ui.tab_replace->setEnabled(false);
  226. }
  227. void FindWin::setFindHistory(QList<QString>* findHistory)
  228. {
  229. m_findHistory = findHistory;
  230. if ((m_findHistory != nullptr) && !m_findHistory->isEmpty())
  231. {
  232. ui.findComboBox->addItems(*m_findHistory);
  233. ui.replaceTextBox->addItems(*m_findHistory);
  234. ui.dirFindWhat->addItems(*m_findHistory);
  235. ui.markTextBox->addItems(*m_findHistory);
  236. ui.findComboBox->clearEditText();
  237. ui.replaceTextBox->clearEditText();
  238. ui.dirFindWhat->clearEditText();
  239. ui.markTextBox->clearEditText();
  240. }
  241. }
  242. void FindWin::setReplaceHistory(QList<QString>* replaceHistory)
  243. {
  244. m_replaceHistory = replaceHistory;
  245. if ((m_replaceHistory != nullptr) && !m_replaceHistory->isEmpty())
  246. {
  247. ui.replaceWithBox->addItems(*m_replaceHistory);
  248. ui.replaceWithBox->clearEditText();
  249. ui.dirReplaceWhat->addItems(*m_replaceHistory);
  250. ui.dirReplaceWhat->clearEditText();
  251. }
  252. }
  253. //标记高亮所有word单词
  254. int FindWin::markAllWord(QString & word)
  255. {
  256. ui.markTextBox->setCurrentText(word);
  257. ui.findinfilesTab->setCurrentIndex(3);
  258. //发现中文在全词匹配下面不能匹配到单词。最好是英文全词匹配,中文不需要
  259. //但是好像没有一个现成的方法来判断word中的字符。暂时不做全词匹配
  260. ui.markMatchWholeBox->setChecked(false);
  261. ui.markMatchCaseBox->setChecked(true);
  262. return markAll();
  263. }
  264. //删除行首尾的空白字符
  265. void FindWin::removeLineHeadEndBlank(int mode)
  266. {
  267. QWidget* pw = autoAdjustCurrentEditWin();
  268. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  269. if (pEdit != nullptr)
  270. {
  271. if (pEdit->isReadOnly())
  272. {
  273. ui.statusbar->showMessage(tr("The ReadOnly document does not allow this operation."), 8000);
  274. QApplication::beep();
  275. return;
  276. }
  277. ui.findinfilesTab->setCurrentIndex(1);
  278. if (mode == 1)
  279. {
  280. ui.replaceTextBox->setCurrentText("^[ ]+");
  281. }
  282. else if (mode == 2)
  283. {
  284. ui.replaceTextBox->setCurrentText("[ ]+$");
  285. }
  286. ui.replaceWithBox->setEditText("");
  287. ui.replaceModeRegularBt->setChecked(true);
  288. m_isStatic = true;
  289. slot_replaceAll();
  290. m_isStatic = false;
  291. }
  292. }
  293. void FindWin::focusInEvent(QFocusEvent * ev)
  294. {
  295. QWidget::focusInEvent(ev);
  296. setWindowOpacity(1.0);
  297. }
  298. void FindWin::focusOutEvent(QFocusEvent * ev)
  299. {
  300. QWidget::focusOutEvent(ev);
  301. if (this->isActiveWindow())
  302. {
  303. //当前还是活动窗口,不设置透明
  304. return;
  305. }
  306. else
  307. {
  308. setWindowOpacity(0.6);
  309. //static int i = 0;
  310. //qDebug() << ++i;
  311. }
  312. }
  313. bool FindWin::eventFilter(QObject* watched, QEvent *event)
  314. {
  315. if (watched == ui.findComboBox)
  316. {
  317. if (event->type() == QEvent::KeyPress)
  318. {
  319. QKeyEvent *ke = static_cast<QKeyEvent*>(event);
  320. if (ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return)
  321. {
  322. emit ui.findTextNext->click();
  323. return true; //该事件已经被处理
  324. }
  325. return false;
  326. }
  327. else
  328. {
  329. return false;
  330. }
  331. }
  332. return QWidget::eventFilter(watched, event); // 最后将事件交给上层对话框
  333. }
  334. //从ui读取参数配置到成员变量
  335. void FindWin::updateParameterFromUI()
  336. {
  337. bool extend = false;
  338. if (ui.findinfilesTab->currentIndex() == 0)
  339. {
  340. int searchMode = 0;
  341. if (ui.findModeNormalBt->isChecked())
  342. {
  343. searchMode = 1;
  344. extend = false;
  345. }
  346. else if (ui.findModeRegularBt->isChecked())
  347. {
  348. searchMode = 2;
  349. extend = false;
  350. }
  351. else if (ui.findModeExtendBt->isChecked())
  352. {
  353. searchMode = 1;
  354. extend = true;
  355. }
  356. if (m_searchMode != searchMode)
  357. {
  358. m_searchMode = searchMode;
  359. m_isFindFirst = true;
  360. }
  361. if (m_expr != ui.findComboBox->currentText())
  362. {
  363. m_expr = ui.findComboBox->currentText();
  364. m_isFindFirst = true;
  365. }
  366. if (m_BackwardDir != ui.findBackwardBox->isChecked())
  367. {
  368. m_BackwardDir = ui.findBackwardBox->isChecked();
  369. m_isFindFirst = true;
  370. }
  371. if (m_matchWhole != ui.findMatchWholeBox->isChecked())
  372. {
  373. m_matchWhole = ui.findMatchWholeBox->isChecked();
  374. m_isFindFirst = true;
  375. }
  376. if (m_matchCase != ui.findMatchCaseBox->isChecked())
  377. {
  378. m_matchCase = ui.findMatchCaseBox->isChecked();
  379. m_isFindFirst = true;
  380. }
  381. if (m_matchWrap != ui.findWrapBox->isChecked())
  382. {
  383. m_matchWrap = ui.findWrapBox->isChecked();
  384. m_isFindFirst = true;
  385. }
  386. }
  387. else if (ui.findinfilesTab->currentIndex() == 1)
  388. {
  389. int searchMode = 0;
  390. if (ui.replaceModeNormalBox->isChecked())
  391. {
  392. searchMode = 1;
  393. extend = false;
  394. }
  395. else if (ui.replaceModeRegularBt->isChecked())
  396. {
  397. searchMode = 2;
  398. extend = false;
  399. }
  400. else if (ui.replaceModeExtendBox->isChecked())
  401. {
  402. searchMode = 1;
  403. extend = true;
  404. }
  405. if (m_searchMode != searchMode)
  406. {
  407. m_searchMode = searchMode;
  408. m_isFindFirst = true;
  409. }
  410. if (m_expr != ui.replaceTextBox->currentText())
  411. {
  412. m_expr = ui.replaceTextBox->currentText();
  413. m_isFindFirst = true;
  414. }
  415. if (m_replaceWithText != ui.replaceWithBox->currentText())
  416. {
  417. m_replaceWithText = ui.replaceWithBox->currentText();
  418. m_isFindFirst = true;
  419. }
  420. if (m_BackwardDir != ui.replaceBackwardBox->isChecked())
  421. {
  422. m_BackwardDir = ui.replaceBackwardBox->isChecked();
  423. m_isFindFirst = true;
  424. }
  425. if (m_matchWhole != ui.replaceMatchWholeBox->isChecked())
  426. {
  427. m_matchWhole = ui.replaceMatchWholeBox->isChecked();
  428. m_isFindFirst = true;
  429. }
  430. if (m_matchCase != ui.replaceMatchCaseBox->isChecked())
  431. {
  432. m_matchCase = ui.replaceMatchCaseBox->isChecked();
  433. m_isFindFirst = true;
  434. }
  435. if (m_matchWrap != ui.replaceWrapBox->isChecked())
  436. {
  437. m_matchWrap = ui.replaceWrapBox->isChecked();
  438. m_isFindFirst = true;
  439. }
  440. }
  441. else if (ui.findinfilesTab->currentIndex() == 2)
  442. {
  443. int searchMode = 0;
  444. if (ui.dirFindModeNormalBox->isChecked())
  445. {
  446. searchMode = 1;
  447. extend = false;
  448. }
  449. else if (ui.dirFindModeRegularBt->isChecked())
  450. {
  451. searchMode = 2;
  452. extend = false;
  453. }
  454. else if (ui.dirFindModeExtendBox->isChecked())
  455. {
  456. searchMode = 1;
  457. extend = true;
  458. }
  459. if (m_searchMode != searchMode)
  460. {
  461. m_searchMode = searchMode;
  462. m_isFindFirst = true;
  463. }
  464. if (m_expr != ui.dirFindWhat->currentText())
  465. {
  466. m_expr = ui.dirFindWhat->currentText();
  467. m_isFindFirst = true;
  468. }
  469. if (m_replaceWithText != ui.dirReplaceWhat->currentText())
  470. {
  471. m_replaceWithText = ui.dirReplaceWhat->currentText();
  472. m_isFindFirst = true;
  473. }
  474. m_BackwardDir = false;
  475. if (m_matchWhole != ui.dirFindMatchWholeBox->isChecked())
  476. {
  477. m_matchWhole = ui.dirFindMatchWholeBox->isChecked();
  478. m_isFindFirst = true;
  479. }
  480. if (m_matchCase != ui.dirFindMatchCaseBox->isChecked())
  481. {
  482. m_matchCase = ui.dirFindMatchCaseBox->isChecked();
  483. m_isFindFirst = true;
  484. }
  485. m_matchWrap = true;
  486. }
  487. else if (ui.findinfilesTab->currentIndex() == 3)
  488. {
  489. int searchMode = 0;
  490. if (ui.markModeNormalBox->isChecked())
  491. {
  492. searchMode = 1;
  493. extend = false;
  494. }
  495. else if (ui.markModeRegularBt->isChecked())
  496. {
  497. searchMode = 2;
  498. extend = false;
  499. }
  500. else if (ui.markModeExtendBox->isChecked())
  501. {
  502. searchMode = 1;
  503. extend = true;
  504. }
  505. if (m_searchMode != searchMode)
  506. {
  507. m_searchMode = searchMode;
  508. m_isFindFirst = true;
  509. }
  510. if (m_expr != ui.markTextBox->currentText())
  511. {
  512. m_expr = ui.markTextBox->currentText();
  513. m_isFindFirst = true;
  514. }
  515. m_BackwardDir = false;
  516. if (m_matchWhole != ui.markMatchWholeBox->isChecked())
  517. {
  518. m_matchWhole = ui.markMatchWholeBox->isChecked();
  519. m_isFindFirst = true;
  520. }
  521. if (m_matchCase != ui.markMatchCaseBox->isChecked())
  522. {
  523. m_matchCase = ui.markMatchCaseBox->isChecked();
  524. m_isFindFirst = true;
  525. }
  526. m_matchWrap = true;
  527. }
  528. m_re = ((m_searchMode == 2) ? true : false);
  529. if (m_cs != m_matchCase)
  530. {
  531. m_cs = m_matchCase;
  532. }
  533. if (m_wo != m_matchWhole)
  534. {
  535. m_wo = m_matchWhole;
  536. }
  537. if (m_wrap != m_matchWrap)
  538. {
  539. m_wrap = m_matchWrap;
  540. }
  541. if (m_extend != extend)
  542. {
  543. m_extend = extend;
  544. m_isFindFirst = true;
  545. }
  546. //本来的m_BackwardDir只控制是否勾选反向
  547. m_forward = !m_BackwardDir;
  548. //m_isReverseFind 控制是否还需要反向一直,只在查找前一个生效,只影响查找界面
  549. if (ui.findinfilesTab->currentIndex() == 0)
  550. {
  551. m_forward = (m_isReverseFind ? !m_forward : m_forward);
  552. }
  553. }
  554. #if 0
  555. //开始做了历史记录重复删除的,后面发现时序有问题,暂时不做
  556. void findItemAndRemove(QComboBox* pCombox, QString& text)
  557. {
  558. int index = pCombox->findText(text);
  559. if (index != -1)
  560. {
  561. pCombox->removeItem(index);
  562. }
  563. }
  564. #endif
  565. void FindWin::addFindHistory(QString &text)
  566. {
  567. //太长会导致看起来很杂乱,也不记录
  568. if (text.isEmpty() || text.size() >= MAX_RECORD_KEY_LENGTH)
  569. {
  570. return;
  571. }
  572. if (m_findHistory != nullptr)
  573. {
  574. int index = m_findHistory->indexOf(text,0);
  575. //已经是最上面一个了,直接返回
  576. if (0 == index)
  577. {
  578. return;
  579. }
  580. //没有直接添加到最前面。不做查找删除重复,一是慢,而是删除会引起信号逻辑时许有误
  581. if (-1 == index)
  582. {
  583. m_findHistory->push_front(text);
  584. ui.findComboBox->insertItem(0, text);
  585. ui.replaceTextBox->insertItem(0, text);
  586. ui.dirFindWhat->insertItem(0, text);
  587. ui.markTextBox->insertItem(0, text);
  588. }
  589. else
  590. {
  591. //有了怎么办,删除旧的,加新的
  592. m_findHistory->removeAt(index);
  593. m_findHistory->push_front(text);
  594. //发现不能只删除旧的,有bug,一旦删除后,查找框乱了,被切换到下一个。
  595. ui.findComboBox->removeItem(index);
  596. ui.replaceTextBox->removeItem(index);
  597. ui.dirFindWhat->removeItem(index);
  598. ui.markTextBox->removeItem(index);
  599. ui.findComboBox->insertItem(0, text);
  600. ui.replaceTextBox->insertItem(0, text);
  601. ui.dirFindWhat->insertItem(0, text);
  602. ui.markTextBox->insertItem(0, text);
  603. //发现不能只删除旧的,有bug,一旦删除后,查找框乱了,被切换到下一个。
  604. //必须重新设置一下,否则查找框里面字段乱跳到下一个去了
  605. ui.findComboBox->setCurrentIndex(0);
  606. ui.replaceTextBox->setCurrentIndex(0);
  607. ui.dirFindWhat->setCurrentIndex(0);
  608. ui.markTextBox->setCurrentIndex(0);
  609. }
  610. if (m_findHistory->size() >= 15)
  611. {
  612. m_findHistory->takeLast();
  613. }
  614. }
  615. }
  616. void FindWin::addReplaceHistory(QString& text)
  617. {
  618. //太长会导致看起来很杂乱,也不记录
  619. if (text.isEmpty() || text.size() >= MAX_RECORD_KEY_LENGTH)
  620. {
  621. return;
  622. }
  623. if (m_replaceHistory != nullptr)
  624. {
  625. int index = m_replaceHistory->indexOf(text, 0);
  626. //已经是最上面一个了,直接返回
  627. if (0 == index)
  628. {
  629. return;
  630. }
  631. //没有直接添加到最前面。不做查找删除重复,一是慢,而是删除会引起信号逻辑时序有误
  632. if (-1 == index)
  633. {
  634. m_replaceHistory->push_front(text);
  635. ui.replaceWithBox->insertItem(0, text);
  636. ui.dirReplaceWhat->insertItem(0, text);
  637. }
  638. else
  639. {
  640. //有了怎么办,删除旧的,加新的
  641. m_replaceHistory->removeAt(index);
  642. m_replaceHistory->push_front(text);
  643. //发现不能只删除旧的,有bug,一旦删除后,查找框乱了,被切换到下一个。
  644. ui.replaceWithBox->removeItem(index);
  645. ui.dirReplaceWhat->removeItem(index);
  646. ui.replaceWithBox->insertItem(0, text);
  647. ui.dirReplaceWhat->insertItem(0, text);
  648. //发现不能只删除旧的,有bug,一旦删除后,查找框乱了,被切换到下一个。
  649. //必须重新设置一下,否则查找框里面字段乱跳到下一个去了
  650. ui.replaceWithBox->setCurrentIndex(0);
  651. ui.dirReplaceWhat->setCurrentIndex(0);
  652. }
  653. if (m_replaceHistory->size() >= 15)
  654. {
  655. m_replaceHistory->takeLast();
  656. }
  657. }
  658. }
  659. //检查是否是第一次查找,凡是参数变化了,则认定为是第一次查找。
  660. //因为查找分firstFirst和firstNext,则是qscint特性决定的。所以正确识别第一次查找是必要的
  661. bool FindWin::isFirstFind()
  662. {
  663. return m_isFindFirst;
  664. }
  665. //const QChar *constData() const
  666. bool readBase(const QChar * str, int * value, int base, int size)
  667. {
  668. int i = 0, temp = 0;
  669. *value = 0;
  670. QChar max = QChar(QChar('0').digitValue() + base - 1);
  671. QChar current;
  672. while (i < size)
  673. {
  674. current = str[i];
  675. if (current >= 'A')
  676. {
  677. #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
  678. current = current.digitValue() & 0xdf;
  679. current = current.digitValue() - ('A' - '0' - 10);
  680. #else
  681. current = QChar(current.digitValue() & 0xdf);
  682. current = QChar(current.digitValue() - ('A' - '0' - 10));
  683. #endif
  684. }
  685. else if (current > '9')
  686. return false;
  687. if (current >= '0' && current <= max)
  688. {
  689. temp *= base;
  690. temp += (current.digitValue() - '0');
  691. }
  692. else
  693. {
  694. return false;
  695. }
  696. ++i;
  697. }
  698. *value = temp;
  699. return true;
  700. }
  701. int convertExtendedToString(QString& query, QString &result)
  702. { //query may equal to result, since it always gets smaller
  703. int i = 0, j = 0;
  704. int length = query.length();
  705. int charLeft = length;
  706. QChar current;
  707. while (i < length)
  708. { //because the backslash escape quences always reduce the size of the generic_string, no overflow checks have to be made for target, assuming parameters are correct
  709. current = query.at(i);
  710. --charLeft;
  711. if (current == '\\' && charLeft)
  712. { //possible escape sequence
  713. ++i;
  714. --charLeft;
  715. current = query.at(i);
  716. switch (current.toLatin1())
  717. {
  718. case 'r':
  719. result[j] = '\r';
  720. break;
  721. case 'n':
  722. result[j] = '\n';
  723. break;
  724. case '0':
  725. result[j] = '\0';
  726. break;
  727. case 't':
  728. result[j] = '\t';
  729. break;
  730. case '\\':
  731. result[j] = '\\';
  732. break;
  733. case 'b':
  734. case 'd':
  735. case 'o':
  736. case 'x':
  737. case 'u':
  738. {
  739. int size = 0, base = 0;
  740. if (current == 'b')
  741. { //11111111
  742. size = 8, base = 2;
  743. }
  744. else if (current == 'o')
  745. { //377
  746. size = 3, base = 8;
  747. }
  748. else if (current == 'd')
  749. { //255
  750. size = 3, base = 10;
  751. }
  752. else if (current == 'x')
  753. { //0xFF
  754. size = 2, base = 16;
  755. }
  756. else if (current == 'u')
  757. { //0xCDCD
  758. size = 4, base = 16;
  759. }
  760. if (charLeft >= size)
  761. {
  762. int res = 0;
  763. if (readBase(query.constData() + (i + 1), &res, base, size))
  764. {
  765. result[j] = QChar(res);
  766. i += size;
  767. break;
  768. }
  769. }
  770. //not enough chars to make parameter, use default method as fallback
  771. }
  772. default:
  773. { //unknown sequence, treat as regular text
  774. result[j] = '\\';
  775. ++j;
  776. result[j] = current;
  777. break;
  778. }
  779. }
  780. }
  781. else
  782. {
  783. result[j] = query[i];
  784. }
  785. ++i;
  786. ++j;
  787. }
  788. return j;
  789. }
  790. void FindWin::showCallTip(QsciScintilla* pEdit, int pos)
  791. {
  792. QString zeroString(tr("find-regex-zero-length-match"));
  793. QByteArray bytes = zeroString.toUtf8();
  794. bytes.append('\0');
  795. /*int size = bytes.size();
  796. char* newStr = new char[size + 1];
  797. memcpy(newStr, bytes.data(), size);
  798. newStr[bytes.size()] = '\0';*/
  799. pEdit->SendScintilla(SCI_CALLTIPSHOW, pos, (void*)(bytes.data()));
  800. /*delete[]newStr;*/
  801. }
  802. //删除空白行
  803. void FindWin::removeEmptyLine(bool isBlankContained)
  804. {
  805. QWidget* pw = autoAdjustCurrentEditWin();
  806. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  807. if (pEdit != nullptr)
  808. {
  809. if (pEdit->isReadOnly())
  810. {
  811. ui.statusbar->showMessage(tr("The ReadOnly document does not allow this operation."), 8000);
  812. QApplication::beep();
  813. return;
  814. }
  815. ui.findinfilesTab->setCurrentIndex(1);
  816. if (isBlankContained)
  817. {
  818. ui.replaceTextBox->setCurrentText("^[\\t ]*$(\\r\\n|\\r|\\n)");
  819. }
  820. else
  821. {
  822. ui.replaceTextBox->setCurrentText("^$(\\r\\n|\\r|\\n)");
  823. }
  824. ui.replaceWithBox->setEditText("");
  825. ui.replaceModeRegularBt->setChecked(true);
  826. m_isStatic = true;
  827. slot_replaceAll();
  828. m_isStatic = false;
  829. }
  830. }
  831. void FindWin::findNext()
  832. {
  833. slot_findNext();
  834. }
  835. void FindWin::findPrev()
  836. {
  837. slot_findPrev();
  838. }
  839. /*处理查找时零长的问题。一定要处理,否则会死循环,因为每次都在原地查找。
  840. * 就是把下次查找的startpos往前一个,否则每次都从这个startpos找到自己
  841. */
  842. void FindWin::dealWithZeroFound(QsciScintilla* pEdit)
  843. {
  844. FindState& state = pEdit->getLastFindState();
  845. if (state.targstart == state.targend)
  846. {
  847. //强行加1,否则每次查找总是在同一个地方。
  848. state.startpos++;
  849. }
  850. }
  851. //调整光标变化后,查找位置需要调整的情况
  852. void FindWin::adjustSearchStartPosChange(QsciScintilla* pEdit)
  853. {
  854. int caretPos = pEdit->SendScintilla(SCI_GETCURRENTPOS);
  855. FindState& state = pEdit->getLastFindState();
  856. if (state.targend != caretPos)
  857. {
  858. state.startpos = caretPos;
  859. }
  860. }
  861. /*处理查找时零长的问题。一定要处理,否则会死循环,因为每次都在原地查找。
  862. * 就是把下次查找的startpos往前一个,否则每次都从这个startpos找到自己
  863. * 和dealWithZeroFound是一样的,就是要显示消息而已
  864. */
  865. void FindWin::dealWithZeroFoundShowTip(QsciScintilla* pEdit, bool isShowTip)
  866. {
  867. if (!isShowTip)
  868. {
  869. return dealWithZeroFound(pEdit);
  870. }
  871. FindState& state = pEdit->getLastFindState();
  872. //int linpos = pEdit->SendScintilla(SCI_POSITIONFROMLINE, state.linenum);
  873. int line = 0;
  874. int indexStart = 0;
  875. int indexEnd = 0;
  876. pEdit->lineIndexFromPosition(state.targstart, &line, &indexStart);
  877. pEdit->lineIndexFromPosition(state.targend, &line, &indexEnd);
  878. ui.statusbar->showMessage(tr("target info linenum %1 pos is %2 - %3").arg(state.linenum + 1).arg(indexStart).arg(indexEnd), 8000);
  879. if (state.targstart == state.targend)
  880. {
  881. //强行加1,否则每次查找总是在同一个地方。
  882. //这里有个问题,如果是反向查找,则应该是pos减去1才对。
  883. //不过只有在正则查找才会出现零长,而正则查找时我们不允许反向查找。反向是回调的。
  884. state.startpos++;
  885. showCallTip(pEdit, state.targstart);
  886. }
  887. }
  888. void FindWin::dofindNext()
  889. {
  890. if (ui.findComboBox->currentText().isEmpty())
  891. {
  892. ui.statusbar->showMessage(tr("what find is null !"), 8000);
  893. QApplication::beep();
  894. return;
  895. }
  896. updateParameterFromUI();
  897. QWidget* pw = autoAdjustCurrentEditWin();
  898. QsciScintilla* pEdit = dynamic_cast<QsciScintilla*>(pw);
  899. //第一次查找
  900. if (isFirstFind())
  901. {
  902. if (pEdit != nullptr)
  903. {
  904. QString whatFind = ui.findComboBox->currentText();
  905. addFindHistory(whatFind);
  906. if (m_extend)
  907. {
  908. QString extendFind;
  909. convertExtendedToString(whatFind, extendFind);
  910. whatFind = extendFind;
  911. }
  912. if (!pEdit->findFirst(whatFind, m_re, m_cs, m_wo, m_wrap, m_forward, FINDNEXTTYPE_FINDNEXT, -1, -1, true, false, false))
  913. {
  914. ui.statusbar->showMessage(tr("cant't find text \'%1\'").arg(m_expr), 8000);
  915. QApplication::beep();
  916. m_isFindFirst = true;
  917. }
  918. else
  919. {
  920. m_isFindFirst = false;
  921. dealWithZeroFoundShowTip(pEdit);
  922. }
  923. }
  924. }
  925. else
  926. {
  927. //查找下一个
  928. if (pEdit != nullptr)
  929. {
  930. adjustSearchStartPosChange(pEdit);
  931. if (!pEdit->findNext())
  932. {
  933. ui.statusbar->showMessage(tr("no more find text \'%1\'").arg(m_expr), 8000);
  934. m_isFindFirst = true;
  935. QApplication::beep();
  936. }
  937. else
  938. {
  939. dealWithZeroFoundShowTip(pEdit);
  940. }
  941. }
  942. }
  943. }
  944. //一旦修改条件发生变化,则认定为第一次查找
  945. void FindWin::slot_findNext()
  946. {
  947. if (m_isReverseFind)
  948. {
  949. m_isReverseFind = false;
  950. m_isFindFirst = true;
  951. }
  952. dofindNext();
  953. }
  954. void FindWin::setFindBackward(bool isBackward)
  955. {
  956. if (ui.findBackwardBox->isChecked() != isBackward)
  957. {
  958. ui.findBackwardBox->setChecked(isBackward);
  959. }
  960. }
  961. void FindWin::slot_findPrev()
  962. {
  963. if (!m_isReverseFind)
  964. {
  965. m_isReverseFind = true;
  966. m_isFindFirst = true;
  967. }
  968. dofindNext();
  969. }
  970. //查找计数
  971. void FindWin::slot_findCount()
  972. {
  973. if (ui.findComboBox->currentText().isEmpty())
  974. {
  975. ui.statusbar->showMessage(tr("what find is null !"), 8000);
  976. QApplication::beep();
  977. return;
  978. }
  979. QWidget* pw = autoAdjustCurrentEditWin();
  980. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  981. if (pEdit != nullptr)
  982. {
  983. if (pEdit->isReadOnly())
  984. {
  985. ui.statusbar->showMessage(tr("The ReadOnly document does not allow this operation."), 8000);
  986. QApplication::beep();
  987. return;
  988. }
  989. updateParameterFromUI();
  990. int srcPostion = pEdit->execute(SCI_GETCURRENTPOS);
  991. int firstDisLineNum = pEdit->execute(SCI_GETFIRSTVISIBLELINE);
  992. int countNums = 0;
  993. //无条件进行第一次查找,从0行0列开始查找,而且不回环。如果没有找到,则替换完毕
  994. QString whatFind = ui.findComboBox->currentText();
  995. //这里不能直接修改results.findText的值,该值在外部显示还需要。如果修改则会显示紊乱
  996. if (m_extend)
  997. {
  998. QString extendFind;
  999. convertExtendedToString(whatFind, extendFind);
  1000. whatFind = extendFind;
  1001. }
  1002. //这里的forward一定要是true。回环一定是false
  1003. if (!pEdit->findFirst(whatFind, m_re, m_cs, m_wo, false, true, FINDNEXTTYPE_FINDNEXT, 0, 0,false))
  1004. {
  1005. ui.statusbar->showMessage(tr("count %1 times with \'%2\'").arg(countNums).arg(m_expr));
  1006. QApplication::beep();
  1007. m_isFindFirst = true;
  1008. return;
  1009. }
  1010. else
  1011. {
  1012. dealWithZeroFound(pEdit);
  1013. }
  1014. ++countNums;
  1015. //找到了,增加计数
  1016. while (pEdit->findNext())
  1017. {
  1018. ++countNums;
  1019. dealWithZeroFound(pEdit);
  1020. }
  1021. pEdit->execute(SCI_GOTOPOS, srcPostion);
  1022. pEdit->execute(SCI_SETFIRSTVISIBLELINE, firstDisLineNum);
  1023. //pEdit->execute(SCI_SETXOFFSET, 0);
  1024. //全部替换后,下次查找,必须算第一次查找
  1025. m_isFindFirst = true;
  1026. ui.statusbar->showMessage(tr("count %1 times with \'%2\'").arg(countNums).arg(m_expr));
  1027. }
  1028. else
  1029. {
  1030. ui.statusbar->showMessage(tr("The mode of the current document does not allow this operation."), 8000);
  1031. QApplication::beep();
  1032. }
  1033. }
  1034. //去掉行尾的\n\r符号
  1035. static QString trimmedEnd(QString lineText)
  1036. {
  1037. if (lineText.endsWith("\r\n"))
  1038. {
  1039. return lineText.mid(0, lineText.length()-2);
  1040. }
  1041. if (lineText.endsWith("\r"))
  1042. {
  1043. return lineText.mid(0, lineText.length()-1);
  1044. }
  1045. if (lineText.endsWith("\n"))
  1046. {
  1047. return lineText.mid(0, lineText.length()-1);
  1048. }
  1049. return lineText;
  1050. }
  1051. //getResult:是否提取结果目标字符串。在正则查找时,还是有用的
  1052. QString FindWin::addCurFindRecord(ScintillaEditView* pEdit, FindRecords& recordRet,bool isMark, bool getResult)
  1053. {
  1054. FindRecord aRecord;
  1055. QString ret;
  1056. //看了源码,当前查找到的结果,是会被选中的。所以可通过选中范围,来记录当前被查找中的结果
  1057. //光标在选择词的尾部下一个位置
  1058. #if 0
  1059. aRecord.pos = pEdit->execute(SCI_GETSELECTIONSTART);
  1060. aRecord.end = pEdit->execute(SCI_GETSELECTIONEND);
  1061. #endif
  1062. //加速
  1063. FindState& state = pEdit->getLastFindState();
  1064. aRecord.pos = state.targstart;
  1065. aRecord.end = state.targend;
  1066. aRecord.lineNum = state.linenum;
  1067. //mark模式不需要这么多信息,可直接返回
  1068. if (!isMark)
  1069. {
  1070. //静默查找,在后台批量查找时,不会返回这个state.linenum。需要手动获取一下
  1071. if (aRecord.lineNum == -1)
  1072. {
  1073. aRecord.lineNum = pEdit->execute(SCI_LINEFROMPOSITION, aRecord.pos);
  1074. }
  1075. aRecord.lineStartPos = pEdit->execute(SCI_POSITIONFROMLINE, aRecord.lineNum);
  1076. int lineLens = pEdit->execute(SCI_LINELENGTH, aRecord.lineNum);
  1077. if (lineLens <= 0)
  1078. {
  1079. return ret;
  1080. }
  1081. char* lineText = new char[lineLens + 1];
  1082. memset(lineText, 0, lineLens + 1);
  1083. //这里有个bug,是qscint的,查找最后一行,会漏掉最后一个字符
  1084. pEdit->execute(SCI_GETLINE, aRecord.lineNum, reinterpret_cast<sptr_t>(lineText));
  1085. //务必要去掉行位的换行,否则显示结果列表会显示换行
  1086. aRecord.lineContents = trimmedEnd(QString(lineText));
  1087. delete[]lineText;
  1088. //如果需要结果,再把结果提取一下
  1089. if (getResult && (state.targend - state.targstart > 0))
  1090. {
  1091. Sci_TextRange lineText;
  1092. lineText.chrg.cpMin = static_cast<Sci_Position>(state.targstart);
  1093. lineText.chrg.cpMax = static_cast<Sci_Position>(state.targend);
  1094. QByteArray result;
  1095. result.resize(state.targend - state.targstart);
  1096. lineText.lpstrText = result.data();
  1097. //获取原始行的内容
  1098. pEdit->SendScintilla(SCI_GETTEXTRANGE, 0, &lineText);
  1099. ret = QString(result);
  1100. }
  1101. }
  1102. recordRet.records.append(aRecord);
  1103. return ret;
  1104. }
  1105. //在后台批量查找
  1106. int FindWin::findAtBack(QStringList& keyword)
  1107. {
  1108. if (keyword.isEmpty())
  1109. {
  1110. return 0;
  1111. }
  1112. QWidget* pw = autoAdjustCurrentEditWin();
  1113. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  1114. if (pEdit != nullptr)
  1115. {
  1116. if (pEdit->isReadOnly())
  1117. {
  1118. ui.statusbar->showMessage(tr("The ReadOnly document does not allow replacement."), 8000);
  1119. QApplication::beep();
  1120. return 0;
  1121. }
  1122. }
  1123. m_isStatic = true;
  1124. int times = 0;
  1125. ProgressWin* loadFileProcessWin = nullptr;
  1126. if (keyword.size() > 1000)
  1127. {
  1128. loadFileProcessWin = new ProgressWin(this);
  1129. loadFileProcessWin->setWindowModality(Qt::WindowModal);
  1130. loadFileProcessWin->info(tr("total %1 keyword, please wait ...").arg(keyword.size()));
  1131. loadFileProcessWin->setTotalSteps(keyword.size() / 100);
  1132. loadFileProcessWin->show();
  1133. }
  1134. QString text = pEdit->text();
  1135. QByteArray bytes = text.toUtf8();
  1136. QByteArray findBytes;
  1137. int keyLens = 0;
  1138. int index = 0;
  1139. for (int i = 0; i < keyword.size(); ++i)
  1140. {
  1141. if ((loadFileProcessWin != nullptr) && loadFileProcessWin->isCancel())
  1142. {
  1143. break;
  1144. }
  1145. index = 0;
  1146. //20230223 不走老的逻辑了,批量替换太慢。直接把文件读取处理,在内存中一次性处理完毕。
  1147. //但是这样就不知道到底有多少字符串被替换了
  1148. findBytes = keyword.at(i).toUtf8();
  1149. keyLens = findBytes.size();
  1150. while (true)
  1151. {
  1152. index = bytes.indexOf(findBytes,index);
  1153. if (index == -1)
  1154. {
  1155. break;
  1156. }
  1157. ++times;
  1158. index += keyLens;
  1159. }
  1160. if ((loadFileProcessWin != nullptr)&& ((i % 100) == 0))
  1161. {
  1162. loadFileProcessWin->moveStep();
  1163. QCoreApplication::processEvents();
  1164. }
  1165. }
  1166. if (loadFileProcessWin != nullptr)
  1167. {
  1168. delete loadFileProcessWin;
  1169. }
  1170. return times;
  1171. }
  1172. //在后台批量替换
  1173. int FindWin::replaceAtBack(QStringList& keyword, QStringList& replace)
  1174. {
  1175. assert(keyword.size() == replace.size());
  1176. //this->setCurrentTab(REPLACE_TAB);
  1177. if (keyword.isEmpty())
  1178. {
  1179. return 0;
  1180. }
  1181. QWidget* pw = autoAdjustCurrentEditWin();
  1182. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  1183. if (pEdit != nullptr)
  1184. {
  1185. if (pEdit->isReadOnly())
  1186. {
  1187. ui.statusbar->showMessage(tr("The ReadOnly document does not allow replacement."), 8000);
  1188. QApplication::beep();
  1189. return 0;
  1190. }
  1191. }
  1192. m_isStatic = true;
  1193. int times = 0;
  1194. pEdit->execute(SCI_BEGINUNDOACTION);
  1195. ProgressWin* loadFileProcessWin = nullptr;
  1196. if (keyword.size() > 1000)
  1197. {
  1198. loadFileProcessWin = new ProgressWin(this);
  1199. loadFileProcessWin->setWindowModality(Qt::WindowModal);
  1200. loadFileProcessWin->info(tr("total %1 keyword, please wait ...").arg(keyword.size()));
  1201. loadFileProcessWin->setTotalSteps(keyword.size() / 10);
  1202. loadFileProcessWin->show();
  1203. }
  1204. QString text = pEdit->text();
  1205. for (int i = 0; i < keyword.size(); ++i)
  1206. {
  1207. if ((loadFileProcessWin!=nullptr) && loadFileProcessWin->isCancel())
  1208. {
  1209. break;
  1210. }
  1211. //20230223 不走老的逻辑了,批量替换太慢。直接把文件读取处理,在内存中一次性处理完毕。
  1212. //但是这样就不知道到底有多少字符串被替换了
  1213. text.replace(keyword.at(i), replace.at(i));
  1214. if ((loadFileProcessWin != nullptr) && ((i % 10) == 0))
  1215. {
  1216. loadFileProcessWin->moveStep();
  1217. QCoreApplication::processEvents();
  1218. }
  1219. }
  1220. //替换外部后,一次性整体替换
  1221. int selectionEnd = pEdit->length();
  1222. pEdit->execute(SCI_SETTARGETRANGE, 0, selectionEnd);
  1223. QByteArray bytes = text.toUtf8();
  1224. pEdit->execute(SCI_REPLACETARGET, bytes.size(), reinterpret_cast<sptr_t>(bytes.data()));
  1225. if (loadFileProcessWin != nullptr)
  1226. {
  1227. delete loadFileProcessWin;
  1228. }
  1229. pEdit->execute(SCI_ENDUNDOACTION);
  1230. m_isStatic = false;
  1231. return times;
  1232. }
  1233. //在后台批量高亮
  1234. int FindWin::markAtBack(QStringList& keyword)
  1235. {
  1236. if (keyword.isEmpty())
  1237. {
  1238. return 0;
  1239. }
  1240. QWidget* pw = autoAdjustCurrentEditWin();
  1241. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  1242. if (pEdit != nullptr)
  1243. {
  1244. if (pEdit->isReadOnly())
  1245. {
  1246. ui.statusbar->showMessage(tr("The ReadOnly document does not allow replacement."), 8000);
  1247. QApplication::beep();
  1248. return 0;
  1249. }
  1250. }
  1251. m_isStatic = true;
  1252. int times = 0;
  1253. ProgressWin* loadFileProcessWin = nullptr;
  1254. if (keyword.size() > 1000)
  1255. {
  1256. loadFileProcessWin = new ProgressWin(this);
  1257. loadFileProcessWin->setWindowModality(Qt::WindowModal);
  1258. loadFileProcessWin->info(tr("total %1 keyword, please wait ...").arg(keyword.size()));
  1259. loadFileProcessWin->setTotalSteps(keyword.size() / 100);
  1260. loadFileProcessWin->show();
  1261. }
  1262. QString text = pEdit->text();
  1263. QByteArray bytes = text.toUtf8();
  1264. QByteArray findBytes;
  1265. int keyLens = 0;
  1266. int index = 0;
  1267. QMap<QByteArray, QVector<int>* > keyPos;
  1268. for (int i = 0; i < keyword.size(); ++i)
  1269. {
  1270. if ((loadFileProcessWin != nullptr) && loadFileProcessWin->isCancel())
  1271. {
  1272. break;
  1273. }
  1274. index = 0;
  1275. //20230223 不走老的逻辑了,批量替换太慢。直接把文件读取处理,在内存中一次性处理完毕。
  1276. //但是这样就不知道到底有多少字符串被替换了
  1277. findBytes = keyword.at(i).toUtf8();
  1278. keyLens = findBytes.size();
  1279. if (keyLens == 0)
  1280. {
  1281. continue;
  1282. }
  1283. QVector<int>* vec = new QVector<int>();
  1284. vec->reserve(128);
  1285. keyPos.insert(findBytes, vec);
  1286. while (true)
  1287. {
  1288. index = bytes.indexOf(findBytes, index);
  1289. if (index == -1)
  1290. {
  1291. break;
  1292. }
  1293. ++times;
  1294. vec->append(index);
  1295. index += keyLens;
  1296. }
  1297. if ((loadFileProcessWin != nullptr) && ((i % 100) == 0))
  1298. {
  1299. loadFileProcessWin->moveStep();
  1300. QCoreApplication::processEvents();
  1301. }
  1302. }
  1303. //把结果高亮起来。
  1304. for (QMap<QByteArray, QVector<int>* >::iterator it = keyPos.begin(); it != keyPos.end(); ++it)
  1305. {
  1306. int foundTextLen = it.key().size();
  1307. QVector<int>* pVect = (*it);
  1308. for (int i = 0, s = pVect->size(); i < s; ++i)
  1309. {
  1310. pEdit->execute(SCI_SETINDICATORCURRENT, CCNotePad::s_curMarkColorId);
  1311. pEdit->execute(SCI_INDICATORFILLRANGE, pVect->at(i), foundTextLen);
  1312. }
  1313. delete pVect;
  1314. }
  1315. if (loadFileProcessWin != nullptr)
  1316. {
  1317. delete loadFileProcessWin;
  1318. }
  1319. return times;
  1320. }
  1321. int FindWin::findAllInCurDoc(QStringList* reResult)
  1322. {
  1323. if (ui.findComboBox->currentText().isEmpty())
  1324. {
  1325. ui.statusbar->showMessage(tr("what find is null !"), 8000);
  1326. QApplication::beep();
  1327. return 0;
  1328. }
  1329. QWidget* pw = autoAdjustCurrentEditWin();
  1330. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  1331. if (pEdit != nullptr)
  1332. {
  1333. if (pEdit->isReadOnly())
  1334. {
  1335. if (!m_isStatic)
  1336. {
  1337. ui.statusbar->showMessage(tr("The ReadOnly document does not allow this operation."), 8000);
  1338. }
  1339. QApplication::beep();
  1340. return 0;
  1341. }
  1342. QString resultDestStr;
  1343. int askAbortTimes = 0;
  1344. FindRecords results;
  1345. results.pEdit = pEdit;
  1346. results.findFilePath = pw->property("filePath").toString();
  1347. updateParameterFromUI();
  1348. //正则模式下面,拷贝所有结果到剪切板
  1349. bool isNeedResult(m_re && (reResult != nullptr));
  1350. int srcPostion = pEdit->execute(SCI_GETCURRENTPOS);
  1351. int firstDisLineNum = pEdit->execute(SCI_GETFIRSTVISIBLELINE);
  1352. int findNums = 0;
  1353. //无条件进行第一次查找,从0行0列开始查找,而且不回环。如果没有找到,则替换完毕
  1354. QString whatFind = ui.findComboBox->currentText();
  1355. results.findText = whatFind;
  1356. //这里不能直接修改results.findText的值,该值在外部显示还需要。如果修改则会显示紊乱
  1357. if (m_extend)
  1358. {
  1359. QString extendFind;
  1360. convertExtendedToString(whatFind, extendFind);
  1361. whatFind = extendFind;
  1362. }
  1363. //这里的forward一定要是true。回环一定是false
  1364. if (!pEdit->findFirst(whatFind, m_re, m_cs, m_wo, false, true, FINDNEXTTYPE_FINDNEXT, 0, 0))
  1365. {
  1366. ui.statusbar->showMessage(tr("cant't find text \'%1\'").arg(m_expr), 8000);
  1367. if (!m_isStatic)
  1368. {
  1369. QApplication::beep();
  1370. emit sign_findAllInCurDoc(&results);
  1371. }
  1372. m_isFindFirst = true;
  1373. return 0;
  1374. }
  1375. else
  1376. {
  1377. dealWithZeroFound(pEdit);
  1378. }
  1379. resultDestStr = addCurFindRecord(pEdit, results, false, isNeedResult);
  1380. //正则模式下面,拷贝所有结果到剪切板
  1381. if (isNeedResult)
  1382. {
  1383. reResult->append(resultDestStr);
  1384. }
  1385. ++findNums;
  1386. //找到了,把结果收集起来
  1387. while (pEdit->findNext())
  1388. {
  1389. resultDestStr = addCurFindRecord(pEdit, results, false, isNeedResult);
  1390. ++findNums;
  1391. //正则模式下面,拷贝所有结果到剪切板
  1392. if (isNeedResult)
  1393. {
  1394. reResult->append(resultDestStr);
  1395. }
  1396. dealWithZeroFound(pEdit);
  1397. //2000 和 4000时各询问一次,避免查询结果过大
  1398. if (((askAbortTimes == 0) && findNums > 2000) || ((askAbortTimes == 1) && findNums > 4000))
  1399. {
  1400. int ret = QMessageBox::question(this, tr("Continue Find ?"), tr("The search results have been greater than %1 times in %2 files, and more may be slow. Continue to search?").arg(findNums).arg(1), tr("Yes"), tr("Abort"));
  1401. if (ret == 1)
  1402. {
  1403. break;
  1404. }
  1405. ++askAbortTimes;
  1406. }
  1407. }
  1408. pEdit->execute(SCI_GOTOPOS, srcPostion);
  1409. pEdit->execute(SCI_SETFIRSTVISIBLELINE, firstDisLineNum);
  1410. //pEdit->execute(SCI_SETXOFFSET, 0);
  1411. //全部替换后,下次查找,必须算第一次查找
  1412. m_isFindFirst = true;
  1413. if (!isNeedResult)
  1414. {
  1415. ui.statusbar->showMessage(tr("find finished, total %1 found!").arg(findNums), 10000);
  1416. }
  1417. else
  1418. {
  1419. ui.statusbar->showMessage(tr("find finished, total %1 found! Result in clipboard.").arg(findNums), 10000);
  1420. }
  1421. emit sign_findAllInCurDoc(&results);
  1422. return findNums;
  1423. }
  1424. else
  1425. {
  1426. if (!m_isStatic)
  1427. {
  1428. ui.statusbar->showMessage(tr("The mode of the current document does not allow this operation."), 8000);
  1429. QApplication::beep();
  1430. }
  1431. }
  1432. return 0;
  1433. }
  1434. void FindWin::slot_findAllInCurDoc()
  1435. {
  1436. //findAllInCurDoc();
  1437. int index = m_editTabWidget->currentIndex();
  1438. if (index >= 0)
  1439. {
  1440. findAllInOpenDoc(index);
  1441. }
  1442. }
  1443. void FindWin::findAllInOpenDoc(int index)
  1444. {
  1445. if (ui.findComboBox->currentText().isEmpty())
  1446. {
  1447. ui.statusbar->showMessage(tr("what find is null !"), 8000);
  1448. QApplication::beep();
  1449. return;
  1450. }
  1451. QString whatFind = ui.findComboBox->currentText();
  1452. QString originWhatFine = whatFind;
  1453. if (m_extend)
  1454. {
  1455. QString extendFind;
  1456. convertExtendedToString(whatFind, extendFind);
  1457. whatFind = extendFind;
  1458. }
  1459. int replaceNums = 0;
  1460. QVector<FindRecords*>* allOpenFileRecord = new QVector<FindRecords*>();
  1461. for (int i = 0; i < m_editTabWidget->count(); ++i)
  1462. {
  1463. //是否只查找一个文档
  1464. if ((index != -1) && (i != index))
  1465. {
  1466. continue;
  1467. }
  1468. QWidget* pw = m_editTabWidget->widget(i);
  1469. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  1470. if (pEdit != nullptr)
  1471. {
  1472. if (pEdit->isReadOnly())
  1473. {
  1474. continue;
  1475. }
  1476. FindRecords* results = new FindRecords();
  1477. results->pEdit = pEdit;
  1478. results->findFilePath = pw->property("filePath").toString();
  1479. updateParameterFromUI();
  1480. //无条件进行第一次查找,从0行0列开始查找,而且不回环。如果没有找到,则替换完毕
  1481. //results->findText要是有原来的值,因为扩展模式下\r\n不会转义,直接输出会换行显示
  1482. results->findText = originWhatFine;
  1483. if (!pEdit->findFirst(whatFind, m_re, m_cs, m_wo, false, true, FINDNEXTTYPE_FINDNEXT, 0, 0))
  1484. {
  1485. delete results;
  1486. continue;
  1487. }
  1488. else
  1489. {
  1490. dealWithZeroFound(pEdit);
  1491. }
  1492. addCurFindRecord(pEdit, *results);
  1493. ++replaceNums;
  1494. //找到了,把结果收集起来
  1495. while (pEdit->findNext())
  1496. {
  1497. addCurFindRecord(pEdit, *results);
  1498. ++replaceNums;
  1499. dealWithZeroFound(pEdit);
  1500. }
  1501. allOpenFileRecord->append(results);
  1502. }
  1503. }
  1504. //全部替换后,下次查找,必须算第一次查找
  1505. m_isFindFirst = true;
  1506. ui.statusbar->showMessage(tr("find finished, total %1 found!").arg(replaceNums), 10000);
  1507. emit sign_findAllInOpenDoc(allOpenFileRecord, replaceNums, whatFind);
  1508. //释放元素
  1509. for (int i = 0; i < allOpenFileRecord->size(); ++i)
  1510. {
  1511. delete allOpenFileRecord->at(i);
  1512. }
  1513. delete allOpenFileRecord;
  1514. }
  1515. void FindWin::slot_findAllInOpenDoc()
  1516. {
  1517. findAllInOpenDoc(-1);
  1518. #if 0
  1519. if (ui.findComboBox->currentText().isEmpty())
  1520. {
  1521. ui.statusbar->showMessage(tr("what find is null !"), 8000);
  1522. QApplication::beep();
  1523. return;
  1524. }
  1525. QString whatFind = ui.findComboBox->currentText();
  1526. QString originWhatFine = whatFind;
  1527. if (m_extend)
  1528. {
  1529. QString extendFind;
  1530. convertExtendedToString(whatFind, extendFind);
  1531. whatFind = extendFind;
  1532. }
  1533. int replaceNums = 0;
  1534. QVector<FindRecords*>* allOpenFileRecord = new QVector<FindRecords*>();
  1535. for (int i = 0; i < m_editTabWidget->count(); ++i)
  1536. {
  1537. QWidget* pw = m_editTabWidget->widget(i);
  1538. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  1539. if (pEdit != nullptr)
  1540. {
  1541. if (pEdit->isReadOnly())
  1542. {
  1543. continue;
  1544. }
  1545. FindRecords* results = new FindRecords();
  1546. results->pEdit = pEdit;
  1547. results->findFilePath = pw->property("filePath").toString();
  1548. updateParameterFromUI();
  1549. //无条件进行第一次查找,从0行0列开始查找,而且不回环。如果没有找到,则替换完毕
  1550. //results->findText要是有原来的值,因为扩展模式下\r\n不会转义,直接输出会换行显示
  1551. results->findText = originWhatFine;
  1552. if (!pEdit->findFirst(whatFind, m_re, m_cs, m_wo, false, true, FINDNEXTTYPE_FINDNEXT, 0, 0))
  1553. {
  1554. delete results;
  1555. continue;
  1556. }
  1557. else
  1558. {
  1559. dealWithZeroFound(pEdit);
  1560. }
  1561. addCurFindRecord(pEdit, *results);
  1562. ++replaceNums;
  1563. //找到了,把结果收集起来
  1564. while (pEdit->findNext())
  1565. {
  1566. addCurFindRecord(pEdit, *results);
  1567. ++replaceNums;
  1568. dealWithZeroFound(pEdit);
  1569. }
  1570. allOpenFileRecord->append(results);
  1571. }
  1572. }
  1573. //全部替换后,下次查找,必须算第一次查找
  1574. m_isFindFirst = true;
  1575. ui.statusbar->showMessage(tr("find finished, total %1 found!").arg(replaceNums), 10000);
  1576. emit sign_findAllInOpenDoc(allOpenFileRecord, replaceNums, whatFind);
  1577. //释放元素
  1578. for (int i = 0; i < allOpenFileRecord->size(); ++i)
  1579. {
  1580. delete allOpenFileRecord->at(i);
  1581. }
  1582. delete allOpenFileRecord;
  1583. #endif
  1584. }
  1585. //返回是否查找得到内容
  1586. bool FindWin::replaceFindNext(QsciScintilla* pEdit, bool showZeroFindTip)
  1587. {
  1588. m_isFound = false;
  1589. //第一次查找
  1590. if (m_isFindFirst)
  1591. {
  1592. if (pEdit != nullptr)
  1593. {
  1594. QString whatFind = ui.replaceTextBox->currentText();
  1595. addFindHistory(whatFind);
  1596. QString replaceWith = ui.replaceWithBox->currentText();
  1597. addReplaceHistory(replaceWith);
  1598. if (m_extend)
  1599. {
  1600. QString extendFind;
  1601. convertExtendedToString(whatFind, extendFind);
  1602. whatFind = extendFind;
  1603. }
  1604. if (!pEdit->findFirst(whatFind, m_re, m_cs, m_wo, m_wrap, m_forward, FINDNEXTTYPE_REPLACENEXT))
  1605. {
  1606. ui.statusbar->showMessage(tr("cant't find text \'%1\'").arg(m_expr), 8000);
  1607. QApplication::beep();
  1608. m_isFindFirst = true;
  1609. }
  1610. else
  1611. {
  1612. m_isFound = true;
  1613. m_isFindFirst = false;
  1614. dealWithZeroFoundShowTip(pEdit, showZeroFindTip);
  1615. }
  1616. }
  1617. }
  1618. else
  1619. {
  1620. //查找下一个
  1621. if (pEdit != nullptr)
  1622. {
  1623. adjustSearchStartPosChange(pEdit);
  1624. if (!pEdit->findNext())
  1625. {
  1626. ui.statusbar->showMessage(tr("no more find text \'%1\'").arg(m_expr), 8000);
  1627. m_isFindFirst = true;
  1628. QApplication::beep();
  1629. }
  1630. else
  1631. {
  1632. m_isFound = true;
  1633. dealWithZeroFoundShowTip(pEdit, showZeroFindTip);
  1634. }
  1635. }
  1636. }
  1637. return m_isFound;
  1638. }
  1639. void FindWin::slot_replaceFindNext()
  1640. {
  1641. if (ui.replaceTextBox->currentText().isEmpty())
  1642. {
  1643. ui.statusbar->showMessage(tr("what find is null !"), 8000);
  1644. QApplication::beep();
  1645. return;
  1646. }
  1647. updateParameterFromUI();
  1648. QWidget* pw = autoAdjustCurrentEditWin();
  1649. QsciScintilla* pEdit = dynamic_cast<QsciScintilla*>(pw);
  1650. replaceFindNext(pEdit, true);
  1651. }
  1652. //返回值:是否还可以继续替换
  1653. bool FindWin::replace(ScintillaEditView* pEdit)
  1654. {
  1655. if (isFirstFind())
  1656. {
  1657. replaceFindNext(pEdit, false);
  1658. //如果没有找到,则不替换
  1659. if (!m_isFound)
  1660. {
  1661. return false;
  1662. }
  1663. }
  1664. QString findText = ui.replaceTextBox->currentText();
  1665. QString replaceText = ui.replaceWithBox->currentText();
  1666. if (m_extend)
  1667. {
  1668. QString extendFind;
  1669. convertExtendedToString(findText, extendFind);
  1670. findText = extendFind;
  1671. QString extendReplace;
  1672. convertExtendedToString(replaceText, extendReplace);
  1673. replaceText = extendReplace;
  1674. }
  1675. //当前有选中内容,而且与待替换内容一样,则直接替换
  1676. if (m_isFound && pEdit->hasSelectedText())
  1677. {
  1678. //如果选中内容,与待查找替换的内容一致,进行替换。前提是在normal模式下
  1679. if ((m_searchMode == 1) && (pEdit->selectedText().compare(findText, (m_cs ? Qt::CaseSensitive : Qt::CaseInsensitive)) == 0))
  1680. {
  1681. pEdit->replace(replaceText);
  1682. return replaceFindNext(pEdit,false);
  1683. }
  1684. else if (m_searchMode == 2)
  1685. {
  1686. //如果是正则表达式模式,则不能使用全部匹配才替换,要使用正则匹配,那样会比较麻烦
  1687. //只有上次查找成功,才替换?
  1688. if (m_isFound)
  1689. {
  1690. pEdit->replace(replaceText);
  1691. return replaceFindNext(pEdit,false);
  1692. }
  1693. else
  1694. {
  1695. ui.statusbar->showMessage(tr("no more replace text \'%1\'").arg(m_expr), 8000);
  1696. QApplication::beep();
  1697. return false;
  1698. }
  1699. }
  1700. else
  1701. {
  1702. ui.statusbar->showMessage(tr("no more replace text \'%1\'").arg(m_expr), 8000);
  1703. QApplication::beep();
  1704. return false;
  1705. }
  1706. }
  1707. else if (m_isFound && m_searchMode == 2)
  1708. {
  1709. //找到了内容,但是因为是0长,而无法选中。这种情况就是0长的情况。只在正则表达式情况出现
  1710. pEdit->replace(replaceText);
  1711. //每次替换后,因为是0长替换,再把下次查找位置加1,否则会一直在原地查找
  1712. dealWithZeroFound(pEdit);
  1713. return replaceFindNext(pEdit,false);
  1714. }
  1715. //当前没有查找到
  1716. return replaceFindNext(pEdit,false);
  1717. }
  1718. //把当前选中的内容,使用文本替换掉
  1719. void FindWin::slot_replace()
  1720. {
  1721. if (ui.replaceTextBox->currentText().isEmpty())
  1722. {
  1723. ui.statusbar->showMessage(tr("what find is null !"), 8000);
  1724. QApplication::beep();
  1725. return;
  1726. }
  1727. QWidget* pw = autoAdjustCurrentEditWin();
  1728. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  1729. if (pEdit != nullptr)
  1730. {
  1731. if (pEdit->isReadOnly())
  1732. {
  1733. ui.statusbar->showMessage(tr("The ReadOnly document does not allow replacement."), 8000);
  1734. QApplication::beep();
  1735. return;
  1736. }
  1737. //切换查询条件后,则是第一次查找。防止前后查找条件发生了变化,
  1738. //导致选中的内容不是需要替换的,所以检查是第一次查找,则查找一下
  1739. updateParameterFromUI();
  1740. replace(pEdit);
  1741. }
  1742. else
  1743. {
  1744. ui.statusbar->showMessage(tr("The mode of the current document does not allow replacement."), 8000);
  1745. QApplication::beep();
  1746. }
  1747. }
  1748. void FindWin::slot_findModeRegularBtChange(bool checked)
  1749. {
  1750. if (checked)
  1751. {
  1752. ui.findBackwardBox->setEnabled(false);
  1753. ui.findBackwardBox->setChecked(false);
  1754. ui.findMatchWholeBox->setEnabled(false);
  1755. ui.findMatchWholeBox->setChecked(false);
  1756. }
  1757. else
  1758. {
  1759. ui.findBackwardBox->setEnabled(true);
  1760. ui.findMatchWholeBox->setEnabled(true);
  1761. }
  1762. m_isFindFirst = true;
  1763. }
  1764. void FindWin::slot_replaceModeRegularBtChange(bool checked)
  1765. {
  1766. if (checked)
  1767. {
  1768. ui.replaceBackwardBox->setEnabled(false);
  1769. ui.replaceBackwardBox->setChecked(false);
  1770. ui.replaceMatchWholeBox->setEnabled(false);
  1771. ui.replaceMatchWholeBox->setChecked(false);
  1772. }
  1773. else
  1774. {
  1775. ui.replaceBackwardBox->setEnabled(true);
  1776. ui.replaceMatchWholeBox->setEnabled(true);
  1777. }
  1778. m_isFindFirst = true;
  1779. }
  1780. #if 0
  1781. //替换当前文档里面的所有
  1782. void FindWin::slot_replaceAll()
  1783. {
  1784. if (ui.replaceTextBox->currentText().isEmpty())
  1785. {
  1786. ui.statusbar->showMessage(tr("what find is null !"), 8000);
  1787. return;
  1788. }
  1789. if (!m_isStatic && QMessageBox::Yes != QMessageBox::question(this, tr("Replace All current Doc"), tr("Are you sure replace all occurrences in current documents?")))
  1790. {
  1791. return;
  1792. }
  1793. QWidget* pw = autoAdjustCurrentEditWin();
  1794. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  1795. if (pEdit != nullptr)
  1796. {
  1797. if (pEdit->isReadOnly())
  1798. {
  1799. ui.statusbar->showMessage(tr("The ReadOnly document does not allow replacement."), 8000);
  1800. QApplication::beep();
  1801. return;
  1802. }
  1803. updateParameterFromUI();
  1804. int srcPostion = pEdit->execute(SCI_GETCURRENTPOS);
  1805. int firstDisLineNum = pEdit->execute(SCI_GETFIRSTVISIBLELINE);
  1806. int replaceNums = 0;
  1807. //无条件进行第一次查找,从0行0列开始查找,而且不回环。如果没有找到,则替换完毕
  1808. QString whatFind = ui.replaceTextBox->currentText();
  1809. QString replaceText = ui.replaceWithBox->currentText();
  1810. if (m_extend)
  1811. {
  1812. QString extendFind;
  1813. convertExtendedToString(whatFind, extendFind);
  1814. whatFind = extendFind;
  1815. QString extendReplace;
  1816. convertExtendedToString(replaceText, extendReplace);
  1817. replaceText = extendReplace;
  1818. }
  1819. if (!pEdit->findFirst(whatFind, m_re, m_cs, m_wo, false, true, FINDNEXTTYPE_REPLACENEXT, 0,0))
  1820. {
  1821. ui.statusbar->showMessage(tr("cant't find text \'%1\'").arg(m_expr), 8000);
  1822. QApplication::beep();
  1823. m_isFindFirst = true;
  1824. return;
  1825. }
  1826. pEdit->execute(SCI_BEGINUNDOACTION);
  1827. pEdit->replace(replaceText);
  1828. dealWithZeroFound(pEdit);
  1829. ++replaceNums;
  1830. //找到了,则自动进行全部替换
  1831. while(pEdit->findNext())
  1832. {
  1833. pEdit->replace(replaceText);
  1834. ++replaceNums;
  1835. dealWithZeroFound(pEdit);
  1836. }
  1837. pEdit->execute(SCI_ENDUNDOACTION);
  1838. pEdit->execute(SCI_GOTOPOS, srcPostion);
  1839. pEdit->execute(SCI_SETFIRSTVISIBLELINE, firstDisLineNum);
  1840. pEdit->execute(SCI_SETXOFFSET, 0);
  1841. //全部替换后,下次查找,必须算第一次查找
  1842. m_isFindFirst = true;
  1843. ui.statusbar->showMessage(tr("replace finished, total %1 replaced!").arg(replaceNums), 10000);
  1844. }
  1845. else
  1846. {
  1847. ui.statusbar->showMessage(tr("The mode of the current document does not allow replacement."), 8000);
  1848. QApplication::beep();
  1849. }
  1850. }
  1851. #endif
  1852. // Find the first occurrence of a string.
  1853. int buildSearchFlags(bool re, bool cs, bool wo, bool wrap, bool forward, FindNextType findNextType, bool posix, bool cxx11)
  1854. {
  1855. int flags = 0;
  1856. flags = (cs ? SCFIND_MATCHCASE : 0) |
  1857. (wo ? SCFIND_WHOLEWORD : 0) |
  1858. (re ? SCFIND_REGEXP : 0) |
  1859. (posix ? SCFIND_POSIX : 0) |
  1860. (cxx11 ? SCFIND_CXX11REGEX : 0);
  1861. switch (findNextType)
  1862. {
  1863. case FINDNEXTTYPE_FINDNEXT:
  1864. flags |= SCFIND_REGEXP_EMPTYMATCH_ALL | SCFIND_REGEXP_SKIPCRLFASONE;
  1865. break;
  1866. case FINDNEXTTYPE_REPLACENEXT:
  1867. flags |= SCFIND_REGEXP_EMPTYMATCH_NOTAFTERMATCH | SCFIND_REGEXP_SKIPCRLFASONE;
  1868. break;
  1869. case FINDNEXTTYPE_FINDNEXTFORREPLACE:
  1870. flags |= SCFIND_REGEXP_EMPTYMATCH_ALL | SCFIND_REGEXP_EMPTYMATCH_ALLOWATSTART | SCFIND_REGEXP_SKIPCRLFASONE;
  1871. break;
  1872. }
  1873. return flags;
  1874. }
  1875. struct FindReplaceInfo
  1876. {
  1877. intptr_t _startRange = -1;
  1878. intptr_t _endRange = -1;
  1879. };
  1880. //返回值替换数量
  1881. int FindWin::doReplaceAll(ScintillaEditView* pEdit, QString &whatFind, QString& replaceText, bool isCombineUndo)
  1882. {
  1883. int replaceNums = 0;
  1884. int srcPostion = pEdit->execute(SCI_GETCURRENTPOS);
  1885. int firstDisLineNum = pEdit->execute(SCI_GETFIRSTVISIBLELINE);
  1886. if (isCombineUndo)
  1887. {
  1888. pEdit->execute(SCI_BEGINUNDOACTION);
  1889. }
  1890. int flags = buildSearchFlags(m_re, m_cs, m_wo, false, true, FINDNEXTTYPE_REPLACENEXT, 0, 0);
  1891. intptr_t targetStart = 0;
  1892. intptr_t targetEnd = 0;
  1893. //Initial range for searching
  1894. pEdit->execute(SCI_SETSEARCHFLAGS, flags);
  1895. FindReplaceInfo findReplaceInfo;
  1896. findReplaceInfo._startRange = 0;
  1897. findReplaceInfo._endRange = pEdit->execute(SCI_GETLENGTH);
  1898. QByteArray pTextFind = whatFind.toUtf8();
  1899. QByteArray pTextReplace = replaceText.toUtf8();
  1900. while (targetStart >= 0)
  1901. {
  1902. targetStart = pEdit->searchInTarget(pTextFind, findReplaceInfo._startRange, findReplaceInfo._endRange);
  1903. // If we've not found anything, just break out of the loop
  1904. if (targetStart == -1 || targetStart == -2)
  1905. break;
  1906. targetEnd = pEdit->execute(SCI_GETTARGETEND);
  1907. if (targetEnd > findReplaceInfo._endRange)
  1908. {
  1909. //we found a result but outside our range, therefore do not process it
  1910. break;
  1911. }
  1912. intptr_t foundTextLen = targetEnd - targetStart;
  1913. intptr_t replaceDelta = 0;
  1914. intptr_t replacedLength;
  1915. if (m_re)
  1916. {
  1917. replacedLength = pEdit->replaceTargetRegExMode(pTextReplace);
  1918. }
  1919. else
  1920. {
  1921. replacedLength = pEdit->replaceTarget(pTextReplace);
  1922. }
  1923. replaceDelta = replacedLength - foundTextLen;
  1924. ++replaceNums;
  1925. // After the processing of the last string occurrence the search loop should be stopped
  1926. // This helps to avoid the endless replacement during the EOL ("$") searching
  1927. if (targetStart + foundTextLen == findReplaceInfo._endRange)
  1928. break;
  1929. findReplaceInfo._startRange = targetStart + foundTextLen + replaceDelta; //search from result onwards
  1930. findReplaceInfo._endRange += replaceDelta; //adjust end of range in case of replace
  1931. }
  1932. if (isCombineUndo)
  1933. {
  1934. pEdit->execute(SCI_ENDUNDOACTION);
  1935. }
  1936. pEdit->execute(SCI_GOTOPOS, srcPostion);
  1937. pEdit->execute(SCI_SETFIRSTVISIBLELINE, firstDisLineNum);
  1938. //pEdit->execute(SCI_SETXOFFSET, 0);
  1939. return replaceNums;
  1940. }
  1941. int FindWin::replaceAll()
  1942. {
  1943. if (ui.replaceTextBox->currentText().isEmpty())
  1944. {
  1945. ui.statusbar->showMessage(tr("what find is null !"), 8000);
  1946. return 0;
  1947. }
  1948. if (!m_isStatic && QMessageBox::Yes != QMessageBox::question(this, tr("Replace All current Doc"), tr("Are you sure replace all occurrences in current documents?")))
  1949. {
  1950. return 0;
  1951. }
  1952. QWidget* pw = autoAdjustCurrentEditWin();
  1953. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  1954. if (pEdit != nullptr)
  1955. {
  1956. if (pEdit->isReadOnly())
  1957. {
  1958. ui.statusbar->showMessage(tr("The ReadOnly document does not allow replacement."), 8000);
  1959. QApplication::beep();
  1960. return 0;
  1961. }
  1962. }
  1963. updateParameterFromUI();
  1964. QString whatFind = ui.replaceTextBox->currentText();
  1965. QString replaceText = ui.replaceWithBox->currentText();
  1966. if (m_extend)
  1967. {
  1968. QString extendFind;
  1969. convertExtendedToString(whatFind, extendFind);
  1970. whatFind = extendFind;
  1971. QString extendReplace;
  1972. convertExtendedToString(replaceText, extendReplace);
  1973. replaceText = extendReplace;
  1974. }
  1975. int replaceNums = doReplaceAll(pEdit, whatFind, replaceText);
  1976. //全部替换后,下次查找,必须算第一次查找
  1977. m_isFindFirst = true;
  1978. ui.statusbar->showMessage(tr("replace finished, total %1 replaced!").arg(replaceNums), 10000);
  1979. return replaceNums;
  1980. }
  1981. //替换当前文档里面的所有。之前的要慢,是因为qscintilla中实时计算了行在屏幕需要的长度。
  1982. //大量的这种计算一行实时长度的操作,非常耗时。查找、标记均不耗时,只有替换修改了文本才耗时。
  1983. void FindWin::slot_replaceAll()
  1984. {
  1985. replaceAll();
  1986. }
  1987. void FindWin::slot_replaceAllInOpenDoc()
  1988. {
  1989. if (ui.replaceTextBox->currentText().isEmpty())
  1990. {
  1991. ui.statusbar->showMessage(tr("what find is null !"), 8000);
  1992. QApplication::beep();
  1993. return;
  1994. }
  1995. if (QMessageBox::Yes != QMessageBox::question(this, tr("Replace All Open Doc"), tr("Are you sure replace all occurrences in all open documents?")))
  1996. {
  1997. return;
  1998. }
  1999. updateParameterFromUI();
  2000. int replaceNums = 0;
  2001. QString whatFind = ui.replaceTextBox->currentText();
  2002. QString whatReplace = m_replaceWithText;
  2003. if (m_extend)
  2004. {
  2005. QString extendFind;
  2006. convertExtendedToString(whatFind, extendFind);
  2007. whatFind = extendFind;
  2008. QString extendReplace;
  2009. convertExtendedToString(whatReplace, extendReplace);
  2010. whatReplace = extendReplace;
  2011. }
  2012. for (int i = 0; i < m_editTabWidget->count(); ++i)
  2013. {
  2014. QWidget* pw = m_editTabWidget->widget(i);
  2015. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  2016. if (pEdit != nullptr)
  2017. {
  2018. //只读的文档不能替换
  2019. if (pEdit->isReadOnly())
  2020. {
  2021. continue;
  2022. }
  2023. replaceNums += doReplaceAll(pEdit, whatFind, whatReplace);
  2024. #if 0
  2025. //无条件进行第一次查找,从0行0列开始查找,而且不回环。如果没有找到,则替换完毕
  2026. if (!pEdit->findFirst(whatFind, m_re, m_cs, m_wo, false, true, FINDNEXTTYPE_REPLACENEXT,0, 0))
  2027. {
  2028. continue;
  2029. }
  2030. pEdit->replace(whatReplace);
  2031. dealWithZeroFound(pEdit);
  2032. ++replaceNums;
  2033. //找到了,则自动进行全部替换
  2034. while (pEdit->findNext())
  2035. {
  2036. pEdit->replace(whatReplace);
  2037. ++replaceNums;
  2038. dealWithZeroFound(pEdit);
  2039. }
  2040. #endif
  2041. }
  2042. }
  2043. //全部替换后,下次查找,必须算第一次查找
  2044. m_isFindFirst = true;
  2045. ui.statusbar->showMessage(tr("Replace in Opened Files: %1 occurrences were replaced.").arg(replaceNums), 10000);
  2046. }
  2047. int FindWin::markAll(QSet<int>* outLineNum)
  2048. {
  2049. if (ui.markTextBox->currentText().isEmpty())
  2050. {
  2051. ui.statusbar->showMessage(tr("what mark is null !"), 8000);
  2052. QApplication::beep();
  2053. return 0;
  2054. }
  2055. QWidget* pw = autoAdjustCurrentEditWin();
  2056. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  2057. if (pEdit != nullptr)
  2058. {
  2059. FindRecords* results = new FindRecords;
  2060. results->pEdit = pEdit;
  2061. results->hightLightColor = CCNotePad::s_curMarkColorId;
  2062. results->findFilePath = pw->property("filePath").toString();
  2063. updateParameterFromUI();
  2064. int replaceNums = 0;
  2065. //无条件进行第一次查找,从0行0列开始查找,而且不回环。如果没有找到,则替换完毕
  2066. QString whatMark = ui.markTextBox->currentText();
  2067. results->findText = whatMark;
  2068. if (m_extend)
  2069. {
  2070. QString extendFind;
  2071. convertExtendedToString(whatMark, extendFind);
  2072. whatMark = extendFind;
  2073. }
  2074. int srcPostion = pEdit->execute(SCI_GETCURRENTPOS);
  2075. int firstDisLineNum = pEdit->execute(SCI_GETFIRSTVISIBLELINE);
  2076. if (!pEdit->findFirst(whatMark, m_re, m_cs, m_wo, false, true, FINDNEXTTYPE_FINDNEXT, 0, 0))
  2077. {
  2078. ui.statusbar->showMessage(tr("cant't find text \'%1\'").arg(m_expr), 8000);
  2079. //QApplication::beep();
  2080. return 0;
  2081. }
  2082. else
  2083. {
  2084. //不支持零长的高亮。0长不高亮
  2085. FindState& state = pEdit->getLastFindState();
  2086. if (state.targstart == state.targend)
  2087. {
  2088. ui.statusbar->showMessage(tr("cant't mark text \'%1\'").arg(m_expr), 8000);
  2089. QApplication::beep();
  2090. return 0;
  2091. }
  2092. }
  2093. addCurFindRecord(pEdit, *results, true);
  2094. ++replaceNums;
  2095. //找到了,把结果收集起来
  2096. while (pEdit->findNext())
  2097. {
  2098. addCurFindRecord(pEdit, *results, true);
  2099. ++replaceNums;
  2100. }
  2101. //把结果高亮起来。
  2102. int foundTextLen = 0;
  2103. for (int i = 0, s = results->records.size(); i < s; ++i)
  2104. {
  2105. const FindRecord& rs = results->records.at(i);
  2106. foundTextLen = rs.end - rs.pos;
  2107. if (foundTextLen > 0)
  2108. {
  2109. pEdit->execute(SCI_SETINDICATORCURRENT, CCNotePad::s_curMarkColorId);
  2110. pEdit->execute(SCI_INDICATORFILLRANGE, rs.pos, foundTextLen);
  2111. }
  2112. if (outLineNum != nullptr)
  2113. {
  2114. outLineNum->insert(rs.lineNum);
  2115. }
  2116. }
  2117. if (!results->records.isEmpty())
  2118. {
  2119. pEdit->appendMarkRecord(results);
  2120. }
  2121. pEdit->execute(SCI_GOTOPOS, srcPostion);
  2122. pEdit->execute(SCI_SETFIRSTVISIBLELINE, firstDisLineNum);
  2123. //pEdit->execute(SCI_SETXOFFSET, 0);
  2124. //全部替换后,下次查找,必须算第一次查找
  2125. m_isFindFirst = true;
  2126. ui.statusbar->showMessage(tr("mark finished, total %1 found!").arg(replaceNums), 10000);
  2127. return replaceNums;
  2128. }
  2129. else
  2130. {
  2131. ui.statusbar->showMessage(tr("The mode of the current document does not allow mark."), 8000);
  2132. QApplication::beep();
  2133. }
  2134. return 0;
  2135. }
  2136. //标记高亮单词
  2137. void FindWin::slot_markAll()
  2138. {
  2139. markAll();
  2140. }
  2141. //取消高亮当前关键字
  2142. void FindWin::slot_clearMark()
  2143. {
  2144. if (ui.markTextBox->currentText().isEmpty())
  2145. {
  2146. ui.statusbar->showMessage(tr("what mark is null !"), 8000);
  2147. QApplication::beep();
  2148. return;
  2149. }
  2150. CCNotePad* pMainPad = dynamic_cast<CCNotePad*>(m_pMainPad);
  2151. if (pMainPad != nullptr)
  2152. {
  2153. pMainPad->clearHighlightWord(ui.markTextBox->currentText());
  2154. }
  2155. }
  2156. //取消所有高亮
  2157. void FindWin::slot_clearAllMark()
  2158. {
  2159. CCNotePad* pMainPad = dynamic_cast<CCNotePad*>(m_pMainPad);
  2160. if (pMainPad != nullptr)
  2161. {
  2162. pMainPad->slot_clearMark();
  2163. }
  2164. }
  2165. //选择查找目录
  2166. void FindWin::slot_dirSelectDest()
  2167. {
  2168. QString curDirPath = ui.destFindDir->text();
  2169. if (curDirPath.isEmpty())
  2170. {
  2171. if (CCNotePad::s_lastOpenDirPath.isEmpty())
  2172. {
  2173. CCNotePad::s_lastOpenDirPath = NddSetting::getKeyValueFromDelaySets(LAST_OPEN_DIR);
  2174. }
  2175. curDirPath = CCNotePad::s_lastOpenDirPath;
  2176. }
  2177. QString destDir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), curDirPath, QFileDialog::DontResolveSymlinks);
  2178. if (!destDir.isEmpty())
  2179. {
  2180. ui.destFindDir->setText(destDir);
  2181. }
  2182. }
  2183. //在walkDirfile中用作回调函数处理。命中则返回true
  2184. bool FindWin::findTextInFile(QString &filePath, int &findNums, QVector<FindRecords*>* allfileInDirRecord)
  2185. {
  2186. pEditTemp->clear();
  2187. if (0 != FileManager::getInstance().loadFileForSearch(pEditTemp, filePath))
  2188. {
  2189. return false;
  2190. }
  2191. FindRecords* results = new FindRecords();
  2192. //返回结果的edit无条件写无。注意不要忘记
  2193. results->pEdit = nullptr;
  2194. results->findFilePath = filePath;
  2195. //无条件进行第一次查找,从0行0列开始查找,而且不回环。如果没有找到,则替换完毕
  2196. results->findText = m_expr;
  2197. QString whatFind = m_expr;
  2198. if (m_extend)
  2199. {
  2200. QString extendFind;
  2201. convertExtendedToString(whatFind, extendFind);
  2202. whatFind = extendFind;
  2203. }
  2204. if (!pEditTemp->findFirst(whatFind, m_re, m_cs, m_wo, false, m_forward, FINDNEXTTYPE_FINDNEXT, 0, 0,false))
  2205. {
  2206. delete results;
  2207. return false;
  2208. }
  2209. else
  2210. {
  2211. dealWithZeroFound(pEditTemp);
  2212. }
  2213. addCurFindRecord(pEditTemp, *results);
  2214. ++findNums;
  2215. //找到了,把结果收集起来
  2216. while (pEditTemp->findNext())
  2217. {
  2218. addCurFindRecord(pEditTemp, *results);
  2219. ++findNums;
  2220. dealWithZeroFound(pEditTemp);
  2221. }
  2222. allfileInDirRecord->append(results);
  2223. return true;
  2224. }
  2225. //在walkDirfile中用作回调函数处理。命中则返回true 第三个参数不需要,为了复用walkdir,暂时保留和findTextInFile一致
  2226. bool FindWin::replaceTextInFile(QString &filePath, int &replaceNums, QVector<FindRecords*>*)
  2227. {
  2228. pEditTemp->clear();
  2229. if (0 != FileManager::getInstance().loadFileForSearch(pEditTemp, filePath))
  2230. {
  2231. return false;
  2232. }
  2233. QString find = m_expr;
  2234. QString replace = m_replaceWithText;
  2235. if (m_extend)
  2236. {
  2237. QString extendFind;
  2238. convertExtendedToString(find, extendFind);
  2239. find = extendFind;
  2240. QString extendReplace;
  2241. convertExtendedToString(replace, extendReplace);
  2242. replace = extendReplace;
  2243. }
  2244. int modifyTimes = doReplaceAll(pEditTemp, find, replace);
  2245. if (modifyTimes > 0)
  2246. {
  2247. replaceNums += modifyTimes;
  2248. //如果进行过替换,则必须要保存一下;否则不能保存,不然文件被修改。
  2249. emit sign_replaceSaveFile(filePath, pEditTemp);
  2250. }
  2251. return true;
  2252. }
  2253. //非递归版本的递归文件,从CompareDirs中修改而来
  2254. //isSkipBinary:是否跳过二进制
  2255. //isSkipHide:是否处理隐藏文件
  2256. //skipMaxSize::处理文件的最大大小,超过则不处理。如果是0,则表示不跳过任何文件
  2257. //isfilterFileType:过滤类型,只处理这类类型文件 为true时 fileExtType 不能为空
  2258. //fileExtType的格式为:.cpp .h 类似,但是不需要前面的.,传递的时候不传递下来
  2259. int FindWin::walkDirfile(QString path, int &foundTimes, bool isSkipBinary, bool isSkipHide, int skipMaxSize, bool isfilterFileType, QStringList& fileExtType, bool isSkipDir, QStringList & skipDirNames, bool isSkipChildDirs, std::function<bool(QString &, int &, QVector<FindRecords*>* allfileInDirRecord)> foundCallBack, bool isAskAbort)
  2260. {
  2261. QList<QString> dirsList;
  2262. QString oneDir(path);
  2263. dirsList.append(oneDir);
  2264. int fileNums = 0;
  2265. int hitFileNums = 0;
  2266. //再获取文件夹到列表
  2267. QDir::Filters dirfilter;
  2268. if (!isSkipHide)
  2269. {
  2270. dirfilter = QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::NoSymLinks;
  2271. }
  2272. else
  2273. {
  2274. dirfilter = QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks;
  2275. }
  2276. //过滤文件类型。true 合格,需要处理, false 不合格,跳过
  2277. auto fileTypeFilter = [&fileExtType](QFileInfo& fileInfo)->bool {
  2278. QString suffix = fileInfo.suffix();
  2279. if (!suffix.isEmpty())
  2280. {
  2281. return (-1 != fileExtType.indexOf(suffix));
  2282. }
  2283. //对于没有后缀的文件,一律跳过
  2284. return false;
  2285. };
  2286. //是否二进制文件
  2287. auto binaryFiltre = [](QFileInfo& fi)->bool {
  2288. return DocTypeListView::isHexExt(fi.suffix());
  2289. };
  2290. ProgressWin* loadFileProcessWin = new ProgressWin(this);
  2291. loadFileProcessWin->setWindowModality(Qt::WindowModal);
  2292. loadFileProcessWin->info(tr("load dir file in progress\n, please wait ..."));
  2293. loadFileProcessWin->show();
  2294. int dirNums = 0;
  2295. bool firstChildDirs = true;
  2296. int totalStep = 0;
  2297. bool canAbort = true;
  2298. bool canAbortSecond = true;
  2299. if (!isAskAbort)
  2300. {
  2301. canAbort = false;
  2302. canAbortSecond = false;
  2303. }
  2304. while (!dirsList.isEmpty())
  2305. {
  2306. QString curDir = dirsList.takeFirst();
  2307. if (!isSkipChildDirs)
  2308. {
  2309. /*添加path路径文件*/
  2310. QDir dir(curDir); //遍历各级子目录
  2311. QFileInfoList folder_list = dir.entryInfoList(dirfilter); //获取当前所有目录
  2312. for (int i = 0; i != folder_list.size(); ++i) //自动递归添加各目录到上一级目录
  2313. {
  2314. QString namepath = folder_list.at(i).absoluteFilePath(); //获取路径
  2315. QFileInfo folderinfo = folder_list.at(i);
  2316. if (folderinfo.baseName().isEmpty())
  2317. {
  2318. loadFileProcessWin->info(tr("skip dir %1").arg(namepath));
  2319. continue;
  2320. }
  2321. QString name = folderinfo.fileName(); //获取目录名
  2322. if (isSkipDir && (-1 != skipDirNames.indexOf(name)))
  2323. {
  2324. loadFileProcessWin->info(tr("skip dir %1").arg(namepath));
  2325. continue;
  2326. }
  2327. dirsList.push_front(namepath);
  2328. dirNums++;
  2329. loadFileProcessWin->info(tr("found %1 dir %2").arg(dirNums).arg(namepath));
  2330. QCoreApplication::processEvents(/*QEventLoop::ExcludeUserInputEvents*/);
  2331. }
  2332. if (firstChildDirs)
  2333. {
  2334. totalStep = dirNums;
  2335. loadFileProcessWin->setTotalSteps(dirNums);
  2336. firstChildDirs = false;
  2337. }
  2338. if (dirsList.size() < totalStep)
  2339. {
  2340. totalStep = dirsList.size();
  2341. loadFileProcessWin->moveStep();
  2342. }
  2343. }
  2344. QDir dir_file(curDir);
  2345. if (!isSkipHide)
  2346. {
  2347. dir_file.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::Hidden | QDir::NoSymLinks);//获取当前所有文件
  2348. }
  2349. else
  2350. {
  2351. dir_file.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);//获取当前所有文件
  2352. }
  2353. QFileInfoList list_file = dir_file.entryInfoList();
  2354. for (int i = 0; i < list_file.size(); ++i)
  2355. { //将当前目录中所有文件添加到treewidget中
  2356. QFileInfo fileInfo = list_file.at(i);
  2357. QString namepath = list_file.at(i).absoluteFilePath(); //获取路径
  2358. //不支持所有文件,仅仅支持指定类型文件。没有通过
  2359. if (isfilterFileType && !fileTypeFilter(fileInfo))
  2360. {
  2361. loadFileProcessWin->info(tr("ext type skip file %1").arg(namepath));
  2362. continue;
  2363. }
  2364. //大小过滤
  2365. if ((skipMaxSize != 0) && fileInfo.size() > skipMaxSize)
  2366. {
  2367. continue;
  2368. }
  2369. //二进制过滤//对于二进制文件该如何处理,我觉得必须要过滤,暂时不处理
  2370. if (isSkipBinary &&binaryFiltre(fileInfo))
  2371. {
  2372. continue;
  2373. }
  2374. //回调处理该函数
  2375. if (foundCallBack(namepath, foundTimes, nullptr))
  2376. {
  2377. ++hitFileNums;
  2378. }
  2379. if (i % 2 == 0)
  2380. {
  2381. if (loadFileProcessWin->isCancel())
  2382. {
  2383. loadFileProcessWin->info(tr("found in dir canceled ..."));
  2384. break;
  2385. }
  2386. QCoreApplication::processEvents();
  2387. }
  2388. if ((canAbort && (hitFileNums > 100 || foundTimes > 1000)) || (canAbortSecond && foundTimes > 8000))
  2389. {
  2390. int ret = QMessageBox::question(this, tr("Continue Find ?"), tr("The search results have been greater than %1 times in %2 files, and more may be slow. Continue to search?").arg(foundTimes).arg(hitFileNums), tr("Yes"), tr("Abort"));
  2391. if(ret == 1)
  2392. {
  2393. loadFileProcessWin->setCancel();
  2394. break;
  2395. }
  2396. else
  2397. {
  2398. if (canAbort && (hitFileNums > 100 || foundTimes > 1000))
  2399. {
  2400. canAbort = false;
  2401. }
  2402. if ((canAbortSecond && foundTimes > 8000))
  2403. {
  2404. canAbortSecond = false;
  2405. }
  2406. }
  2407. }
  2408. }
  2409. fileNums += list_file.size();
  2410. if (loadFileProcessWin->isCancel())
  2411. {
  2412. break;
  2413. }
  2414. }
  2415. if (loadFileProcessWin != nullptr)
  2416. {
  2417. delete loadFileProcessWin;
  2418. loadFileProcessWin = nullptr;
  2419. }
  2420. return fileNums;
  2421. }
  2422. //在目标文件夹中查找
  2423. void FindWin::slot_dirFindAll()
  2424. {
  2425. QString dirPath = ui.destFindDir->text().trimmed();
  2426. QString whatFind = ui.dirFindWhat->currentText();
  2427. if (dirPath.isEmpty())
  2428. {
  2429. ui.statusbar->showMessage(tr("please select find dest dir !"), 8000);
  2430. QApplication::beep();
  2431. return;
  2432. }
  2433. if(whatFind.isEmpty())
  2434. {
  2435. ui.statusbar->showMessage(tr("what find is null !"), 8000);
  2436. QApplication::beep();
  2437. return;
  2438. }
  2439. QDir dir(dirPath);
  2440. if (!dir.exists())
  2441. {
  2442. ui.statusbar->showMessage(tr("dest dir %1 not exist !").arg(dirPath), 8000);
  2443. QApplication::beep();
  2444. return;
  2445. }
  2446. bool isfilterFileType = ui.dealFileType->isChecked();
  2447. bool isSkipDirs = ui.skipDir->isChecked();
  2448. QStringList fileExtTypeList;
  2449. QStringList skipDirNameList;
  2450. if (isfilterFileType)
  2451. {
  2452. QString fileExtType = ui.fileType->text().trimmed();
  2453. if (fileExtType.isEmpty() || (fileExtType == "*.*"))
  2454. {
  2455. isfilterFileType = false;
  2456. }
  2457. else
  2458. {
  2459. //格式是*.h:*.c:*.cpp类似的
  2460. QStringList typeList = fileExtType.split(":");
  2461. foreach (QString var, typeList)
  2462. {
  2463. if (var.size() >= 3)
  2464. {
  2465. //只取后面的h或或cpp后缀
  2466. fileExtTypeList.append(var.mid(2));
  2467. }
  2468. }
  2469. if (fileExtTypeList.isEmpty())
  2470. {
  2471. isfilterFileType = false;
  2472. }
  2473. }
  2474. }
  2475. if (isSkipDirs)
  2476. {
  2477. QString dirNames = ui.skipDirNames->text().trimmed();
  2478. if (dirNames.isEmpty())
  2479. {
  2480. isSkipDirs = false;
  2481. }
  2482. else
  2483. {
  2484. QStringList nameList = dirNames.split(":");
  2485. foreach(QString var, nameList)
  2486. {
  2487. if (var.size() > 0)
  2488. {
  2489. //只取后面的h或或cpp后缀
  2490. skipDirNameList.append(var);
  2491. }
  2492. }
  2493. if (skipDirNameList.isEmpty())
  2494. {
  2495. isSkipDirs = false;
  2496. }
  2497. }
  2498. }
  2499. bool isSkipBinary = ui.skipBinary->isChecked();
  2500. bool isSkipHide = ui.skipHideFile->isChecked();
  2501. int skipMaxSize = (ui.skipFileMaxSize->isChecked()) ? ui.maxFileSizeSpinBox->value()*1024*1024:0;
  2502. bool isSkipChildDir = ui.skipChildDirs->isChecked();
  2503. updateParameterFromUI();
  2504. if (pEditTemp == nullptr)
  2505. {
  2506. pEditTemp = ScintillaEditView::createEditForSearch();
  2507. }
  2508. int foundNums = 0;
  2509. QVector<FindRecords*>* allfileInDirRecord = new QVector<FindRecords*>();
  2510. std::function<bool(QString &, int &, QVector<FindRecords*>* allfileInDirRecord)> foundCallBack = std::bind(&FindWin::findTextInFile, this, std::placeholders::_1, std::placeholders::_2, allfileInDirRecord);
  2511. int filesNum = walkDirfile(dirPath, foundNums, isSkipBinary, isSkipHide, skipMaxSize, isfilterFileType, fileExtTypeList, isSkipDirs, skipDirNameList, isSkipChildDir, foundCallBack);
  2512. //全部替换后,下次查找,必须算第一次查找
  2513. m_isFindFirst = true;
  2514. ui.statusbar->showMessage(tr("find finished, walk %1 files, total %2 found in %3 file!").arg(filesNum).arg(foundNums).arg(allfileInDirRecord->size()));
  2515. //复用了这个信号函数,没有新做消息,要注意
  2516. emit sign_findAllInOpenDoc(allfileInDirRecord, foundNums, whatFind);
  2517. addFindHistory(whatFind);
  2518. //释放元素
  2519. for (int i = 0; i < allfileInDirRecord->size(); ++i)
  2520. {
  2521. delete allfileInDirRecord->at(i);
  2522. }
  2523. delete allfileInDirRecord;
  2524. }
  2525. //目录中直接替换
  2526. void FindWin::slot_dirReplaceAll()
  2527. {
  2528. QString dirPath = ui.destFindDir->text();
  2529. QString whatFind = ui.dirFindWhat->currentText();
  2530. QString dirReplaceWhat = ui.dirReplaceWhat->currentText();
  2531. if (dirPath.isEmpty())
  2532. {
  2533. ui.statusbar->showMessage(tr("please select find dest dir !"), 8000);
  2534. QApplication::beep();
  2535. return;
  2536. }
  2537. if (whatFind.isEmpty())
  2538. {
  2539. ui.statusbar->showMessage(tr("what find is null !"), 8000);
  2540. QApplication::beep();
  2541. return;
  2542. }
  2543. if (QMessageBox::Yes != QMessageBox::question(this, tr("Replace All Dirs"), tr("Are you sure replace all \"%1\" to \"%2\" occurrences in selected dirs ?").arg(whatFind).arg(dirReplaceWhat)))
  2544. {
  2545. return;
  2546. }
  2547. bool isfilterFileType = ui.dealFileType->isChecked();
  2548. QStringList fileExtTypeList;
  2549. bool isSkipDirs = ui.skipDir->isChecked();
  2550. QStringList skipDirNameList;
  2551. if (isfilterFileType)
  2552. {
  2553. QString fileExtType = ui.fileType->text().trimmed();
  2554. if (fileExtType.isEmpty() || (fileExtType == "*.*"))
  2555. {
  2556. isfilterFileType = false;
  2557. }
  2558. else
  2559. {
  2560. //格式是*.h:*.c:*.cpp类似的
  2561. QStringList typeList = fileExtType.split(":");
  2562. foreach(QString var, typeList)
  2563. {
  2564. if (var.size() >= 3)
  2565. {
  2566. //只取后面的h或或cpp后缀
  2567. fileExtTypeList.append(var.mid(2));
  2568. }
  2569. }
  2570. if (fileExtTypeList.isEmpty())
  2571. {
  2572. isfilterFileType = false;
  2573. }
  2574. }
  2575. }
  2576. if (isSkipDirs)
  2577. {
  2578. QString dirNames = ui.skipDirNames->text().trimmed();
  2579. if (dirNames.isEmpty())
  2580. {
  2581. isSkipDirs = false;
  2582. }
  2583. else
  2584. {
  2585. QStringList nameList = dirNames.split(":");
  2586. foreach(QString var, nameList)
  2587. {
  2588. if (var.size() > 0)
  2589. {
  2590. //只取后面的h或或cpp后缀
  2591. skipDirNameList.append(var);
  2592. }
  2593. }
  2594. if (skipDirNameList.isEmpty())
  2595. {
  2596. isSkipDirs = false;
  2597. }
  2598. }
  2599. }
  2600. bool isSkipBinary = ui.skipBinary->isChecked();
  2601. bool isSkipHide = ui.skipHideFile->isChecked();
  2602. int skipMaxSize = (ui.skipFileMaxSize->isChecked()) ? ui.maxFileSizeSpinBox->value() * 1024 * 1024 : 0;
  2603. bool isSkipChildDir = ui.skipChildDirs->isChecked();
  2604. addReplaceHistory(dirReplaceWhat);
  2605. updateParameterFromUI();
  2606. if (pEditTemp == nullptr)
  2607. {
  2608. pEditTemp = ScintillaEditView::createEditForSearch();
  2609. }
  2610. int replaceNums = 0;
  2611. std::function<bool(QString &, int &, QVector<FindRecords*>* allfileInDirRecord)> foundCallBack = std::bind(&FindWin::replaceTextInFile, this, std::placeholders::_1, std::placeholders::_2, nullptr);
  2612. int filesNum = walkDirfile(dirPath, replaceNums, isSkipBinary, isSkipHide, skipMaxSize, isfilterFileType, fileExtTypeList, isSkipDirs, skipDirNameList,isSkipChildDir, foundCallBack,false);
  2613. //全部替换后,下次查找,必须算第一次查找
  2614. m_isFindFirst = true;
  2615. ui.statusbar->showMessage(tr("replace finished, walk %1 files, total %2 replace !").arg(filesNum).arg(replaceNums));
  2616. }
  2617. //把正则查找的结果,拷贝到剪切板
  2618. void FindWin::on_copyReFindResult()
  2619. {
  2620. if (!ui.findModeRegularBt->isChecked())
  2621. {
  2622. ui.statusbar->showMessage(tr("Only regular lookup mode can be used!"),10000);
  2623. return;
  2624. }
  2625. QStringList reResult;
  2626. findAllInCurDoc(&reResult);
  2627. if (!reResult.isEmpty())
  2628. {
  2629. QClipboard* clipboard = QApplication::clipboard();
  2630. clipboard->setText(reResult.join("\n"));
  2631. }
  2632. }
  2633. void FindWin::on_markAndBook()
  2634. {
  2635. QSet<int> outLineNum;
  2636. markAll(&outLineNum);
  2637. QWidget* pw = autoAdjustCurrentEditWin();
  2638. ScintillaEditView* pEdit = dynamic_cast<ScintillaEditView*>(pw);
  2639. if (pEdit != nullptr)
  2640. {
  2641. pEdit->bookmarkAdd(outLineNum);
  2642. }
  2643. }