popupwindow.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*****************************************************************************
  2. * popupwindow.cpp - QStarDict, a dictionary for learning foreign languages *
  3. * Copyright (C) 2007-2023 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 <QClipboard>
  25. #include "dictwidget.h"
  26. #include "keyboard.h"
  27. #include "selection.h"
  28. #include "application.h"
  29. #include "speaker.h"
  30. #include "mainwindow.h"
  31. namespace QStarDict
  32. {
  33. PopupWindow::PopupWindow(QWidget *parent)
  34. : ResizablePopup(parent)
  35. {
  36. m_dict = 0;
  37. translationView = new DictWidget(this, 0, true);
  38. translationView->setFrameStyle(QFrame::NoFrame);
  39. translationView->setDict(m_dict);
  40. translationView->setMouseTracking(true);
  41. QGridLayout *mainLayout = new QGridLayout(this);
  42. mainLayout->setMargin(0);
  43. mainLayout->addWidget(translationView);
  44. m_selection = new Selection(this);
  45. connect(m_selection, SIGNAL(changed(const QString&)), this, SLOT(selectionChanged(const QString&)));
  46. loadSettings();
  47. }
  48. PopupWindow::~PopupWindow()
  49. {
  50. saveSettings();
  51. }
  52. void PopupWindow::loadSettings()
  53. {
  54. QSettings config;
  55. setScan(config.value("PopupWindow/scan", true).toBool());
  56. setModifierKey(config.value("PopupWindow/modifierKey", 0).toInt());
  57. setShowIfNotFound(config.value("PopupWindow/showIfNotFound", false).toBool());
  58. setWindowOpacity(config.value("PopupWindow/opacity", 1.0).toDouble());
  59. setTimeoutBeforeHide(config.value("PopupWindow/timeoutBeforeHide", 500).toInt());
  60. setDefaultSize(config.value("PopupWindow/defaultSize", QSize(320, 240)).toSize());
  61. setPronounceWord(config.value("PopupWindow/pronounceWord", false).toBool());
  62. setDefaultStyleSheet(config.value("PopupWindow/defaultStyleSheet", defaultStyleSheet()).toString());
  63. setShowLinks(config.value("PopupWindow/showLinks", showLinks()).toBool());
  64. setShowLinksModifierKey(config.value("PopupWindow/showLinksModifierKey", showLinksModifierKey()).toInt());
  65. setShowIpaPronouncers(config.value("PopupWindow/showIpaPronouncers", showIpaPronouncers()).toBool());
  66. }
  67. void PopupWindow::saveSettings()
  68. {
  69. QSettings config;
  70. config.setValue("PopupWindow/scan", isScan());
  71. config.setValue("PopupWindow/modifierKey", m_modifierKey);
  72. config.setValue("PopupWindow/showIfNotFound", m_showIfNotFound);
  73. config.setValue("PopupWindow/opacity", windowOpacity());
  74. config.setValue("PopupWindow/timeoutBeforeHide", timeoutBeforeHide());
  75. config.setValue("PopupWindow/defaultSize", defaultSize());
  76. config.setValue("PopupWindow/pronounceWord", pronounceWord());
  77. config.setValue("PopupWindow/defaultStyleSheet", defaultStyleSheet());
  78. config.setValue("PopupWindow/showLinks", showLinks());
  79. config.setValue("PopupWindow/showLinksModifierKey", showLinksModifierKey());
  80. config.setValue("PopupWindow/showIpaPronouncers", showIpaPronouncers());
  81. }
  82. void PopupWindow::setScan(bool scan)
  83. {
  84. m_selection->setScan(scan);
  85. emit scanChanged(scan);
  86. }
  87. bool PopupWindow::isScan() const
  88. {
  89. return m_selection->isScan();
  90. }
  91. void PopupWindow::setDict(DictCore *dict)
  92. {
  93. translationView->setDict(dict);
  94. m_dict = dict;
  95. }
  96. void PopupWindow::selectionChanged(const QString &text)
  97. {
  98. if (m_modifierKey && ! Keyboard::activeModifiers().testFlag(static_cast<Qt::KeyboardModifier>(m_modifierKey)))
  99. return;
  100. if (Application::instance()->mainWindow()->underMouse() || Application::instance()->popupWindow()->underMouse())
  101. return;
  102. showTranslation(text);
  103. }
  104. void PopupWindow::showTranslation(const QString &text)
  105. {
  106. QString simpl = text.simplified();
  107. simpl.remove(QRegExp("[&%-/+?\\*#!:\\(\\)\\[\\]]+"));
  108. if (simpl.isEmpty())
  109. return;
  110. bool isFound = m_dict->isTranslatable(simpl);
  111. if (m_showIfNotFound || isFound)
  112. {
  113. translationView->translate(simpl);
  114. translationView->clearHistory();
  115. translationView->setFocus();
  116. popup();
  117. if (isFound && m_pronounceWord)
  118. Application::instance()->speaker()->speak(simpl);
  119. }
  120. }
  121. void PopupWindow::showClipboardTranslation()
  122. {
  123. showTranslation(Application::clipboard()->text(QClipboard::Selection));
  124. }
  125. }
  126. // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc