1 /*****************************************************************************
2 * dictbrowser.cpp - QStarDict, a StarDict clone written 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 "dictbrowser.h"
22 #include <QDesktopServices>
23 #include <QMouseEvent>
25 #include <QTextCharFormat>
26 #include <QTextDocument>
27 #include <QTextDocumentFragment>
28 #include "../plugins/dictplugin.h"
32 const QString translationCSS =
34 "font-size: 10pt; }\n"
37 "font-style: italic; }\n"
40 "font-weight: bold; }\n"
41 "font.explanation {\n"
43 "font-style: italic; }\n"
44 "font.abbreviature {\n"
45 "font-style: italic; }\n"
47 "font-style: italic; }\n"
48 "font.transcription {\n"
49 "font-weight: bold; }\n";
55 DictBrowser::DictBrowser(QWidget *parent)
56 : QTextBrowser(parent),
60 document()->setDefaultStyleSheet(translationCSS);
62 setOpenExternalLinks(false);
63 connect(this, SIGNAL(anchorClicked(const QUrl &)), SLOT(on_anchorClicked(const QUrl &)));
66 QVariant DictBrowser::loadResource(int type, const QUrl &name)
68 if (type == QTextDocument::HtmlResource && name.scheme() == "qstardict")
70 QString str = name.toString(QUrl::RemoveScheme);
71 QString result = m_dict->translate(str);
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) +
76 return "<title>Translation for \"" + str + "\"</title>\n"
77 + "<body>" + result + "</body>";
79 else if (name.scheme() == "plugin")
81 DictPlugin *plugin = m_dict->plugin(name.host());
84 return plugin->resource(type, name);
86 return QTextBrowser::loadResource(type, name);
89 void DictBrowser::mouseMoveEvent(QMouseEvent *event)
93 m_oldCursor.setCharFormat(m_oldFormat);
94 m_highlighted = false;
96 if (event->modifiers().testFlag(Qt::ControlModifier))
98 QTextCursor cursor = cursorForPosition(event->pos());
99 cursor.select(QTextCursor::WordUnderCursor);
100 QString selection = cursor.selection().toPlainText().simplified();
101 if (m_dict->isTranslatable(selection))
103 m_oldCursor = cursor;
104 m_oldFormat = cursor.charFormat();
106 QTextCharFormat format = m_oldFormat;
107 format.setForeground(Qt::blue);
108 format.setFontUnderline(true);
109 cursor.setCharFormat(format);
111 m_highlighted = true;
115 QTextBrowser::mouseMoveEvent(event);
118 void DictBrowser::mousePressEvent(QMouseEvent *event)
120 if (event->modifiers().testFlag(Qt::ControlModifier))
122 QTextCursor cursor = cursorForPosition(event->pos());
123 cursor.select(QTextCursor::WordUnderCursor);
124 QString selection = cursor.selection().toPlainText().simplified();
125 if (m_dict->isTranslatable(selection))
127 setSource(selection);
129 m_highlighted = false;
132 QTextBrowser::mousePressEvent(event);
135 void DictBrowser::on_anchorClicked(const QUrl &link)
137 QString scheme = link.scheme();
138 if (scheme == "plugin" || scheme == "qrc")
141 QDesktopServices::openUrl(link);
146 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc