init
[qstardict] / qstardict / popupwindow.cpp
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
20 #include "popupwindow.h"
21
22 #include <QGridLayout>
23 #include <QMouseEvent>
24 #include <QSettings>
25 #include <QRegExp>
26 #include "dictwidget.h"
27 #include "keyboard.h"
28 #include "selection.h"
29 #include "application.h"
30 #include "speaker.h"
31
32 namespace QStarDict
33 {
34
35 PopupWindow::PopupWindow(QWidget *parent)
36         : ResizablePopup(parent)
37 {
38     m_dict = 0;
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);
46
47     m_selection = new Selection(this);
48     connect(m_selection, SIGNAL(changed(const QString&)), this, SLOT(selectionChanged(const QString&)));
49
50     loadSettings();
51 }
52
53 PopupWindow::~PopupWindow()
54 {
55     saveSettings();
56 }
57
58 void PopupWindow::loadSettings()
59 {
60     QSettings config;
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());
69 }
70
71 void PopupWindow::saveSettings()
72 {
73     QSettings config;
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());
82 }
83
84 void PopupWindow::setScan(bool scan)
85 {
86     m_selection->setScan(scan);
87     emit scanChanged(scan);
88 }
89
90 bool PopupWindow::isScan() const
91 {
92     return m_selection->isScan();
93 }
94
95 void PopupWindow::setDict(DictCore *dict)
96 {
97     translationView->setDict(dict);
98     m_dict = dict;
99 }
100
101 void PopupWindow::selectionChanged(const QString &text)
102 {
103     if (m_modifierKey && ! Keyboard::activeModifiers().testFlag(static_cast<Qt::KeyboardModifier>(m_modifierKey)))
104         return;
105     showTranslation(text);
106 }
107
108 void PopupWindow::showTranslation(const QString &text)
109 {
110     QString simpl = text.simplified();
111     simpl.remove(QRegExp("[&%-/+?\\*#!:\\(\\)\\[\\]]+"));
112     if (simpl.isEmpty())
113         return;
114
115     bool isFound = m_dict->isTranslatable(simpl);
116
117     if (m_showIfNotFound || isFound)
118     {
119         translationView->translate(simpl);
120         translationView->clearHistory();
121         popup();
122         if (isFound && m_pronounceWord)
123             Application::instance()->speaker()->speak(simpl);
124     }
125 }
126
127 }
128
129 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
130