LUAEditorFindResults.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "LUAEditorFindResults.hxx"
  9. #include "LUAEditorStyleMessages.h"
  10. #include "LUAEditorBlockState.h"
  11. #include <Source/LUA/moc_LUAEditorFindResults.cpp>
  12. #include <Source/LUA/ui_LUAEditorFindResults.h>
  13. namespace LUAEditor
  14. {
  15. void FindResultsHighlighter::SetSearchString(const QString& searchString, bool regEx, bool wholeWord, bool caseSensitive)
  16. {
  17. m_searchString = searchString;
  18. m_regEx = regEx;
  19. m_wholeWord = wholeWord;
  20. m_caseSensitive = caseSensitive;
  21. }
  22. void FindResultsHighlighter::highlightBlock(const QString& text)
  23. {
  24. auto colors = AZ::UserSettings::CreateFind<SyntaxStyleSettings>(AZ_CRC_CE("LUA Editor Text Settings"), AZ::UserSettings::CT_GLOBAL);
  25. auto block = currentBlock();
  26. QTBlockState blockState;
  27. blockState.m_qtBlockState = block.userState();
  28. QTextCharFormat textFormat;
  29. textFormat.setFont(m_font);
  30. if (!blockState.m_blockState.m_uninitialized)
  31. {
  32. if (blockState.m_blockState.m_syntaxHighlighterState == 0)
  33. {
  34. textFormat.setForeground(colors->GetFindResultsHeaderColor());
  35. setFormat(0, block.length(), textFormat);
  36. }
  37. else if (blockState.m_blockState.m_syntaxHighlighterState == 1)
  38. {
  39. textFormat.setForeground(colors->GetFindResultsFileColor());
  40. setFormat(0, block.length(), textFormat);
  41. }
  42. else
  43. {
  44. textFormat.setForeground(colors->GetTextColor());
  45. setFormat(0, block.length(), textFormat);
  46. textFormat.setForeground(colors->GetFindResultsMatchColor());
  47. QRegExp regex(m_searchString, m_caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
  48. int index = 0;
  49. if (m_regEx || m_wholeWord)
  50. {
  51. index = text.indexOf(regex, index);
  52. }
  53. else
  54. {
  55. index = text.indexOf(m_searchString, index, m_caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
  56. }
  57. while (index > 1)
  58. {
  59. if (m_regEx || m_wholeWord)
  60. {
  61. setFormat(index, regex.matchedLength(), textFormat);
  62. }
  63. else
  64. {
  65. setFormat(index, m_searchString.length(), textFormat);
  66. }
  67. ++index;
  68. if (m_regEx || m_wholeWord)
  69. {
  70. index = text.indexOf(regex, index);
  71. }
  72. else
  73. {
  74. index = text.indexOf(m_searchString, index, m_caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. FindResults::FindResults(QWidget* parent)
  81. : QWidget(parent)
  82. {
  83. m_gui = azcreate(Ui::LUAEditorFindResults, ());
  84. m_gui->setupUi(this);
  85. m_gui->m_foldingWidget->setEnabled(true);
  86. m_gui->m_foldingWidget->SetTextEdit(m_gui->m_resultsList);
  87. connect(m_gui->m_resultsList, &LUAEditor::LUAEditorPlainTextEdit::BlockDoubleClicked, this, &FindResults::OnBlockDoubleClicked);
  88. connect(m_gui->m_resultsList, &LUAEditor::LUAEditorPlainTextEdit::cursorPositionChanged, this, [&]() { m_gui->m_foldingWidget->update(); });
  89. connect(m_gui->m_resultsList, &LUAEditor::LUAEditorPlainTextEdit::Scrolled, this, [&]() { m_gui->m_foldingWidget->update(); });
  90. connect(m_gui->m_foldingWidget, &FoldingWidget::TextBlockFoldingChanged, this, [&]() {m_gui->m_resultsList->update(); });
  91. connect(m_gui->m_resultsList->document(), &QTextDocument::contentsChange, m_gui->m_foldingWidget, &FoldingWidget::OnContentChanged);
  92. auto settings = AZ::UserSettings::CreateFind<SyntaxStyleSettings>(AZ_CRC_CE("LUA Editor Text Settings"), AZ::UserSettings::CT_GLOBAL);
  93. m_gui->m_resultsList->setFont(settings->GetFont());
  94. m_gui->m_foldingWidget->SetFont(settings->GetFont());
  95. QString styleSheet = QString(R"(
  96. QPlainTextEdit[readOnly="true"]:focus
  97. {
  98. background-color: %1;
  99. selection-color: %3;
  100. selection-background-color: %4;
  101. }
  102. QPlainTextEdit[readOnly="true"]:!focus
  103. {
  104. background-color: %2;
  105. selection-color: %3;
  106. selection-background-color: %4;
  107. }
  108. )");
  109. styleSheet = styleSheet.arg(settings->GetTextReadOnlyFocusedBackgroundColor().name())
  110. .arg(settings->GetTextReadOnlyUnfocusedBackgroundColor().name())
  111. .arg(settings->GetTextSelectedColor().name())
  112. .arg(settings->GetTextSelectedBackgroundColor().name());
  113. m_gui->m_resultsList->setStyleSheet(styleSheet);
  114. m_resultLineHighlightColor = settings->GetFindResultsLineHighlightColor();
  115. m_highlighter = aznew FindResultsHighlighter(m_gui->m_resultsList->document());
  116. }
  117. FindResults::~FindResults()
  118. {
  119. delete m_highlighter;
  120. azdestroy(m_gui);
  121. }
  122. void FindResults::OnBlockDoubleClicked(QMouseEvent* event, const QTextBlock& block)
  123. {
  124. if (block.isValid())
  125. {
  126. auto userData = block.userData();
  127. if (userData)
  128. {
  129. auto blockInfo = static_cast<FindResultsBlockInfo*>(userData);
  130. // highlight the line
  131. QTextEdit::ExtraSelection selection;
  132. selection.format.setBackground(m_resultLineHighlightColor);
  133. selection.format.setProperty(QTextFormat::FullWidthSelection, true);
  134. selection.cursor = m_gui->m_resultsList->textCursor();
  135. selection.cursor.clearSelection();
  136. QList<QTextEdit::ExtraSelection> extraSelections;
  137. extraSelections.append(selection);
  138. m_gui->m_resultsList->setExtraSelections(extraSelections);
  139. emit ResultSelected(*blockInfo);
  140. event->accept();
  141. }
  142. }
  143. }
  144. void FindResults::AssignAssetId(const AZStd::string& assetName, const AZStd::string& assetId)
  145. {
  146. for (auto block = m_gui->m_resultsList->document()->begin(); block != m_gui->m_resultsList->document()->end(); block = block.next())
  147. {
  148. auto userData = block.userData();
  149. if (userData)
  150. {
  151. auto blockInfo = static_cast<FindResultsBlockInfo*>(userData);
  152. if (blockInfo->m_assetName == assetName)
  153. {
  154. blockInfo->m_assetId = assetId;
  155. }
  156. }
  157. }
  158. }
  159. void FindResults::FinishedAddingText(const QString& searchString, bool regEx, bool wholeWord, bool caseSensitive)
  160. {
  161. m_highlighter->SetSearchString(searchString, regEx, wholeWord, caseSensitive);
  162. m_highlighter->rehighlight();
  163. }
  164. void FindResults::Clear()
  165. {
  166. m_gui->m_resultsList->clear();
  167. }
  168. QTextDocument* FindResults::Document()
  169. {
  170. return m_gui->m_resultsList->document();
  171. }
  172. void FindResults::AppendPlainText(const QString& text)
  173. {
  174. m_gui->m_resultsList->appendPlainText(text);
  175. }
  176. }