dictbrowser.cpp 5.6 KB

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