1 /*****************************************************************************
2 * popupwindow.cpp - QStarDict, a StarDict clone written with using Qt *
3 * Copyright (C) 2007 Alexander Rodin *
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. *
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. *
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 *****************************************************************************/
20 #include "popupwindow.h"
22 #include <QGridLayout>
23 #include <QMouseEvent>
26 #include "dictwidget.h"
28 #include "selection.h"
29 #include "application.h"
35 PopupWindow::PopupWindow(QWidget *parent)
36 : ResizablePopup(parent)
39 translationView = new DictWidget(this);
40 translationView->setFrameStyle(QFrame::NoFrame);
41 translationView->setDict(m_dict);
42 translationView->setMouseTracking(true);
43 QGridLayout *mainLayout = new QGridLayout(this);
44 mainLayout->setMargin(0);
45 mainLayout->addWidget(translationView);
47 m_selection = new Selection(this);
48 connect(m_selection, SIGNAL(changed(const QString&)), this, SLOT(selectionChanged(const QString&)));
53 PopupWindow::~PopupWindow()
58 void PopupWindow::loadSettings()
61 setScan(config.value("PopupWindow/scan", true).toBool());
62 setModifierKey(config.value("PopupWindow/modifierKey", 0).toInt());
63 setShowIfNotFound(config.value("PopupWindow/showIfNotFound", false).toBool());
64 setWindowOpacity(config.value("PopupWindow/opacity", 1.0).toDouble());
65 setTimeoutBeforeHide(config.value("PopupWindow/timeoutBeforeHide", 500).toInt());
66 setDefaultSize(config.value("PopupWindow/defaultSize", QSize(320, 240)).toSize());
67 setPronounceWord(config.value("PopupWindow/pronounceWord", true).toBool());
68 setDefaultStyleSheet(config.value("PopupWindow/defaultStyleSheet", defaultStyleSheet()).toString());
71 void PopupWindow::saveSettings()
74 config.setValue("PopupWindow/scan", isScan());
75 config.setValue("PopupWindow/modifierKey", m_modifierKey);
76 config.setValue("PopupWindow/showIfNotFound", m_showIfNotFound);
77 config.setValue("PopupWindow/opacity", windowOpacity());
78 config.setValue("PopupWindow/timeoutBeforeHide", timeoutBeforeHide());
79 config.setValue("PopupWindow/defaultSize", defaultSize());
80 config.setValue("PopupWindow/pronounceWord", pronounceWord());
81 config.setValue("PopupWindow/defaultStyleSheet", defaultStyleSheet());
84 void PopupWindow::setScan(bool scan)
86 m_selection->setScan(scan);
87 emit scanChanged(scan);
90 bool PopupWindow::isScan() const
92 return m_selection->isScan();
95 void PopupWindow::setDict(DictCore *dict)
97 translationView->setDict(dict);
101 void PopupWindow::selectionChanged(const QString &text)
103 if (m_modifierKey && ! Keyboard::activeModifiers().testFlag(static_cast<Qt::KeyboardModifier>(m_modifierKey)))
105 showTranslation(text);
108 void PopupWindow::showTranslation(const QString &text)
110 QString simpl = text.simplified();
111 simpl.remove(QRegExp("[&%-/+?\\*#!:\\(\\)\\[\\]]+"));
115 bool isFound = m_dict->isTranslatable(simpl);
117 if (m_showIfNotFound || isFound)
119 translationView->translate(simpl);
120 translationView->clearHistory();
122 if (isFound && m_pronounceWord)
123 Application::instance()->speaker()->speak(simpl);
129 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc