init
[qstardict] / qstardict / dictbrowser.cpp
1 /*****************************************************************************
2  * dictbrowser.cpp - QStarDict, a StarDict clone written 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 "dictbrowser.h"
21
22 #include <QDesktopServices>
23 #include <QMouseEvent>
24 #include <QTextBlock>
25 #include <QTextCharFormat>
26 #include <QTextDocument>
27 #include <QTextDocumentFragment>
28 #include "../plugins/dictplugin.h"
29
30 namespace
31 {
32 const QString translationCSS = 
33     "body {\n"
34         "font-size: 10pt; }\n"
35     "font.dict_name {\n"
36         "color: blue;\n"
37         "font-style: italic; }\n"
38     "font.title {\n"
39         "font-size: 16pt;\n"
40         "font-weight: bold; }\n"
41     "font.explanation {\n"
42         "color: #7f7f7f;\n"
43         "font-style: italic; }\n"
44     "font.abbreviature {\n"
45         "font-style: italic; }\n"
46     "font.example {\n"
47         "font-style: italic; }\n"
48     "font.transcription {\n"
49         "font-weight: bold; }\n";
50 }
51
52 namespace QStarDict
53 {
54
55 DictBrowser::DictBrowser(QWidget *parent)
56     : QTextBrowser(parent),
57       m_dict(0),
58       m_highlighted(false)
59 {
60     document()->setDefaultStyleSheet(translationCSS);
61     setOpenLinks(false);
62     setOpenExternalLinks(false);
63     connect(this, SIGNAL(anchorClicked(const QUrl &)), SLOT(on_anchorClicked(const QUrl &)));
64 }
65
66 QVariant DictBrowser::loadResource(int type, const QUrl &name)
67 {
68     if (type == QTextDocument::HtmlResource && name.scheme() == "qstardict")
69     {
70         QString str = name.toString(QUrl::RemoveScheme);
71         QString result = m_dict->translate(str);
72         if (result.isEmpty())
73             result = "<table><tr><td><img src=\":/icons/dialog-warning.png\" width=64 height=64/></td><td valign=middle>" +
74                 tr("The word <b>%1</b> is not found.").arg(str) +
75                 "</td></tr></table>";
76         return "<title>Translation for \"" + str + "\"</title>\n"
77             + "<body>" + result + "</body>";
78     }
79     else if (name.scheme() == "plugin")
80     {
81         DictPlugin *plugin = m_dict->plugin(name.host());
82         if (! plugin)
83             return QVariant();
84         return plugin->resource(type, name);
85     }
86     return QTextBrowser::loadResource(type, name);
87 }
88
89 void DictBrowser::mouseMoveEvent(QMouseEvent *event)
90 {
91     if (m_highlighted)
92     {
93         m_oldCursor.setCharFormat(m_oldFormat);
94         m_highlighted = false;
95     }
96     if (event->modifiers().testFlag(Qt::ControlModifier))
97     {
98         QTextCursor cursor = cursorForPosition(event->pos());
99         cursor.select(QTextCursor::WordUnderCursor);
100         QString selection = cursor.selection().toPlainText().simplified();
101         if (m_dict->isTranslatable(selection))
102         {
103             m_oldCursor = cursor;
104             m_oldFormat = cursor.charFormat();
105
106             QTextCharFormat format = m_oldFormat;
107             format.setForeground(Qt::blue);
108             format.setFontUnderline(true);
109             cursor.setCharFormat(format);
110
111             m_highlighted = true;
112         }
113     }
114
115     QTextBrowser::mouseMoveEvent(event);
116 }
117
118 void DictBrowser::mousePressEvent(QMouseEvent *event)
119 {
120     if (event->modifiers().testFlag(Qt::ControlModifier))
121     {
122         QTextCursor cursor = cursorForPosition(event->pos());
123         cursor.select(QTextCursor::WordUnderCursor);
124         QString selection = cursor.selection().toPlainText().simplified();
125         if (m_dict->isTranslatable(selection))
126         {
127             setSource(selection);
128             if (m_highlighted)
129                 m_highlighted = false;
130         }
131     }
132     QTextBrowser::mousePressEvent(event);
133 }
134
135 void DictBrowser::on_anchorClicked(const QUrl &link)
136 {
137     QString scheme = link.scheme();
138     if (scheme == "plugin" || scheme == "qrc")
139         setSource(link);
140     else
141         QDesktopServices::openUrl(link);
142 }
143
144 }
145
146 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
147