textfinder.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2017 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the examples of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:BSD$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** BSD License Usage
  18. ** Alternatively, you may use this file under the terms of the BSD license
  19. ** as follows:
  20. **
  21. ** "Redistribution and use in source and binary forms, with or without
  22. ** modification, are permitted provided that the following conditions are
  23. ** met:
  24. ** * Redistributions of source code must retain the above copyright
  25. ** notice, this list of conditions and the following disclaimer.
  26. ** * Redistributions in binary form must reproduce the above copyright
  27. ** notice, this list of conditions and the following disclaimer in
  28. ** the documentation and/or other materials provided with the
  29. ** distribution.
  30. ** * Neither the name of The Qt Company Ltd nor the names of its
  31. ** contributors may be used to endorse or promote products derived
  32. ** from this software without specific prior written permission.
  33. **
  34. **
  35. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  39. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  46. **
  47. ** $QT_END_LICENSE$
  48. **
  49. ****************************************************************************/
  50. #include "textfinder.h"
  51. #include <QFile>
  52. #include <QLineEdit>
  53. #include <QMessageBox>
  54. #include <QPushButton>
  55. #include <QTextEdit>
  56. #include <QTextStream>
  57. #include <QUiLoader>
  58. #include <QVBoxLayout>
  59. //! [4]
  60. static QWidget *loadUiFile(QWidget *parent)
  61. {
  62. QFile file(":/res/forms/textfinder.ui");
  63. file.open(QIODevice::ReadOnly);
  64. QUiLoader loader;
  65. return loader.load(&file, parent);
  66. }
  67. static QString loadTextFile(QString contentPath)
  68. {
  69. QFile inputFile(contentPath);
  70. inputFile.open(QIODevice::ReadOnly);
  71. QTextStream in(&inputFile);
  72. in.setCodec("UTF-8");
  73. return in.readAll();
  74. }
  75. TextFinder::TextFinder(QString contentPath, QString titleText, QWidget *parent)
  76. : QDialog(parent)
  77. {
  78. QWidget *formWidget = loadUiFile(this);
  79. ui_findButton = findChild<QPushButton*>("findButton");
  80. ui_textEdit = findChild<QTextEdit*>("textEdit");
  81. ui_lineEdit = findChild<QLineEdit*>("lineEdit");
  82. QMetaObject::connectSlotsByName(this);
  83. ui_textEdit->setText(loadTextFile(contentPath));
  84. QFont font = QFont("Microsoft YaHei",10);
  85. ui_textEdit->setFont(font);
  86. QVBoxLayout *layout = new QVBoxLayout;
  87. layout->setMargin(0);
  88. layout->setContentsMargins(0,0,0,0);
  89. layout->addWidget(formWidget);
  90. setLayout(layout);
  91. setWindowTitle(titleText);
  92. Qt::WindowFlags flags=Qt::Dialog;
  93. flags |=Qt::WindowMinMaxButtonsHint;
  94. flags |=Qt::WindowCloseButtonHint;
  95. setWindowFlags(flags);
  96. resize(1520, 890);
  97. connect(ui_findButton, SIGNAL(clicked()), this, SLOT(OnFindButtonClicked()));
  98. }
  99. bool TextFinder::HighlightKeyWord(QString keyWorld,bool bDefault)
  100. {
  101. QTextDocument *document = ui_textEdit->document();
  102. bool found = false;
  103. // undo previous change (if any)
  104. document->undo();
  105. QTextCursor highlightCursor(document);
  106. QTextCursor cursor(document);
  107. cursor.beginEditBlock();
  108. QTextCharFormat plainFormat(highlightCursor.charFormat());
  109. QTextCharFormat colorFormat = plainFormat;
  110. colorFormat.setForeground(Qt::red);
  111. if(bDefault)
  112. colorFormat.setForeground(Qt::red);
  113. else
  114. colorFormat.setForeground(Qt::blue);
  115. colorFormat.setFontWeight(75);
  116. while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {
  117. highlightCursor = document->find(keyWorld, highlightCursor, QTextDocument::FindWholeWords);
  118. if (!highlightCursor.isNull()) {
  119. found = true;
  120. highlightCursor.movePosition(QTextCursor::WordRight,QTextCursor::KeepAnchor);
  121. highlightCursor.mergeCharFormat(colorFormat);
  122. }
  123. }
  124. cursor.endEditBlock();
  125. return found;
  126. }
  127. void TextFinder::OnFindButtonClicked()
  128. {
  129. QString searchString = ui_lineEdit->text();
  130. if (searchString.isEmpty()) {
  131. QMessageBox::information(this, QStringLiteral("查找"), QStringLiteral("请输入要查找的关键词 ! "));
  132. } else {
  133. if (!HighlightKeyWord(searchString)) {
  134. QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("抱歉!未找到相关关键词! "));
  135. }
  136. }
  137. }