dictbrowsersearch.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*****************************************************************************
  2. * dictbrowsersearch.cpp - QStarDict, a dictionary for learning foreign *
  3. languages *
  4. * Copyright (C) 2007 Petr Vanek *
  5. * Copyright (C) 2012 Alexander Rodin *
  6. * *
  7. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation; either version 2 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General Public License along *
  18. * with this program; if not, write to the Free Software Foundation, Inc., *
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  20. *****************************************************************************/
  21. #include "dictbrowsersearch.h"
  22. namespace QStarDict
  23. {
  24. DictBrowserSearch::DictBrowserSearch(QWidget *parent) :
  25. QWidget(parent)
  26. {
  27. setupUi(this);
  28. connect(searchEdit, SIGNAL(textEdited(QString)), this, SLOT(searchAll()));
  29. connect(caseSensitiveCheckBox, SIGNAL(clicked()), this, SLOT(searchAll()));
  30. connect(wholeWordsCheckBox, SIGNAL(clicked()), this, SLOT(searchAll()));
  31. }
  32. void DictBrowserSearch::searchAll()
  33. {
  34. QTextDocument::FindFlags f = 0;
  35. if (caseSensitiveCheckBox->isChecked())
  36. f |= QTextDocument::FindCaseSensitively;
  37. if (wholeWordsCheckBox->isChecked())
  38. f |= QTextDocument::FindWholeWords;
  39. emit search(searchEdit->text(), f);
  40. }
  41. void DictBrowserSearch::searchResult(bool success)
  42. {
  43. QPalette p;
  44. if (!success)
  45. {
  46. p.setColor(QPalette::Base, QColor(229, 152, 169));
  47. }
  48. searchEdit->setPalette(p);
  49. }
  50. void DictBrowserSearch::showEvent(QShowEvent * event)
  51. {
  52. emit searchActive(true);
  53. QWidget::showEvent(event);
  54. searchEdit->setFocus(Qt::OtherFocusReason);
  55. if (searchEdit->text().length())
  56. searchAll();
  57. }
  58. void DictBrowserSearch::hideEvent(QHideEvent * event)
  59. {
  60. emit searchActive(false);
  61. QWidget::hideEvent(event);
  62. }
  63. void DictBrowserSearch::keyPressEvent(QKeyEvent *event) {
  64. if (event->key() == Qt::Key_Escape) {
  65. hide();
  66. }
  67. }
  68. } // namespace