popupwindow.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*****************************************************************************
  2. * popupwindow.cpp - QStarDict, a StarDict clone written with using Qt *
  3. * Copyright (C) 2007 Alexander Rodin *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License along *
  16. * with this program; if not, write to the Free Software Foundation, Inc., *
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  18. *****************************************************************************/
  19. #include "popupwindow.h"
  20. #include <QGridLayout>
  21. #include <QMouseEvent>
  22. #include <QSettings>
  23. #include <QRegExp>
  24. #include "dictwidget.h"
  25. #include "keyboard.h"
  26. #include "selection.h"
  27. #include "application.h"
  28. #include "speaker.h"
  29. namespace QStarDict
  30. {
  31. PopupWindow::PopupWindow(QWidget *parent)
  32. : ResizablePopup(parent)
  33. {
  34. m_dict = 0;
  35. translationView = new DictWidget(this);
  36. translationView->setFrameStyle(QFrame::NoFrame);
  37. translationView->setDict(m_dict);
  38. translationView->setMouseTracking(true);
  39. QGridLayout *mainLayout = new QGridLayout(this);
  40. mainLayout->setMargin(0);
  41. mainLayout->addWidget(translationView);
  42. m_selection = new Selection(this);
  43. connect(m_selection, SIGNAL(changed(const QString&)), this, SLOT(selectionChanged(const QString&)));
  44. loadSettings();
  45. }
  46. PopupWindow::~PopupWindow()
  47. {
  48. saveSettings();
  49. }
  50. void PopupWindow::loadSettings()
  51. {
  52. QSettings config;
  53. setScan(config.value("PopupWindow/scan", true).toBool());
  54. setModifierKey(config.value("PopupWindow/modifierKey", 0).toInt());
  55. setShowIfNotFound(config.value("PopupWindow/showIfNotFound", false).toBool());
  56. setWindowOpacity(config.value("PopupWindow/opacity", 1.0).toDouble());
  57. setTimeoutBeforeHide(config.value("PopupWindow/timeoutBeforeHide", 500).toInt());
  58. setDefaultSize(config.value("PopupWindow/defaultSize", QSize(320, 240)).toSize());
  59. setPronounceWord(config.value("PopupWindow/pronounceWord", true).toBool());
  60. setDefaultStyleSheet(config.value("PopupWindow/defaultStyleSheet", defaultStyleSheet()).toString());
  61. }
  62. void PopupWindow::saveSettings()
  63. {
  64. QSettings config;
  65. config.setValue("PopupWindow/scan", isScan());
  66. config.setValue("PopupWindow/modifierKey", m_modifierKey);
  67. config.setValue("PopupWindow/showIfNotFound", m_showIfNotFound);
  68. config.setValue("PopupWindow/opacity", windowOpacity());
  69. config.setValue("PopupWindow/timeoutBeforeHide", timeoutBeforeHide());
  70. config.setValue("PopupWindow/defaultSize", defaultSize());
  71. config.setValue("PopupWindow/pronounceWord", pronounceWord());
  72. config.setValue("PopupWindow/defaultStyleSheet", defaultStyleSheet());
  73. }
  74. void PopupWindow::setScan(bool scan)
  75. {
  76. m_selection->setScan(scan);
  77. emit scanChanged(scan);
  78. }
  79. bool PopupWindow::isScan() const
  80. {
  81. return m_selection->isScan();
  82. }
  83. void PopupWindow::setDict(DictCore *dict)
  84. {
  85. translationView->setDict(dict);
  86. m_dict = dict;
  87. }
  88. void PopupWindow::selectionChanged(const QString &text)
  89. {
  90. if (m_modifierKey && ! Keyboard::activeModifiers().testFlag(static_cast<Qt::KeyboardModifier>(m_modifierKey)))
  91. return;
  92. showTranslation(text);
  93. }
  94. void PopupWindow::showTranslation(const QString &text)
  95. {
  96. QString simpl = text.simplified();
  97. simpl.remove(QRegExp("[&%-/+?\\*#!:\\(\\)\\[\\]]+"));
  98. if (simpl.isEmpty())
  99. return;
  100. bool isFound = m_dict->isTranslatable(simpl);
  101. if (m_showIfNotFound || isFound)
  102. {
  103. translationView->translate(simpl);
  104. translationView->clearHistory();
  105. popup();
  106. if (isFound && m_pronounceWord)
  107. Application::instance()->speaker()->speak(simpl);
  108. }
  109. }
  110. }
  111. // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc