dictwidget.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*****************************************************************************
  2. * dictwidget.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. #include "dictwidget.h"
  20. #include <QScrollBar>
  21. #include <QVBoxLayout>
  22. #include <QToolBar>
  23. #include <QAction>
  24. #include <QIcon>
  25. #include <QFileDialog>
  26. #include <QDir>
  27. #include <QFile>
  28. #include <QTextStream>
  29. #include <QMessageBox>
  30. #include <QMouseEvent>
  31. #include <QTextDocument>
  32. #include <QPrinter>
  33. #include <QPrintDialog>
  34. #include "application.h"
  35. #include "dictbrowser.h"
  36. #include "dictbrowsersearch.h"
  37. #include "speaker.h"
  38. namespace
  39. {
  40. class DictWidgetToolbar: public QToolBar
  41. {
  42. public:
  43. DictWidgetToolbar(QWidget *parent = 0)
  44. : QToolBar(parent)
  45. { }
  46. protected:
  47. virtual void mouseDoubleClickEvent(QMouseEvent *event)
  48. {
  49. if (! actionAt(event->pos()))
  50. QToolBar::mouseDoubleClickEvent(event);
  51. }
  52. };
  53. }
  54. namespace QStarDict
  55. {
  56. DictWidget::DictWidget(QWidget *parent, Qt::WindowFlags f)
  57. : QFrame(parent, f)
  58. {
  59. m_translationView = new DictBrowser(this);
  60. setFrameStyle(m_translationView->frameStyle());
  61. m_translationView->setFrameStyle(QFrame::NoFrame);
  62. m_translationView->verticalScrollBar()->setCursor(Qt::ArrowCursor);
  63. m_translationView->horizontalScrollBar()->setCursor(Qt::ArrowCursor);
  64. m_translationView->setOpenExternalLinks(true);
  65. connect(m_translationView, SIGNAL(sourceChanged(const QUrl&)), SLOT(on_translationView_sourceChanged(const QUrl&)));
  66. m_search = new DictBrowserSearch(this);
  67. connect(m_search, SIGNAL(search(const QString &,QTextDocument::FindFlags)),
  68. m_translationView, SLOT(search(const QString &,QTextDocument::FindFlags)));
  69. connect(m_translationView, SIGNAL(searchResult(bool)), m_search, SLOT(searchResult(bool)));
  70. m_search->hide();
  71. m_toolBar = new DictWidgetToolbar(this);
  72. m_toolBar->setMouseTracking(true);
  73. QAction *actionBackward = m_toolBar->addAction(QIcon(":/icons/go-previous.png"), tr("Go to &previous translation"),
  74. m_translationView, SLOT(backward()));
  75. actionBackward->setDisabled(true);
  76. connect(m_translationView, SIGNAL(backwardAvailable(bool)), actionBackward, SLOT(setEnabled(bool)));
  77. QAction *actionForward = m_toolBar->addAction(QIcon(":/icons/go-next.png"), tr("Go to &next translation"),
  78. m_translationView, SLOT(forward()));
  79. actionForward->setDisabled(true);
  80. connect(m_translationView, SIGNAL(forwardAvailable(bool)), actionForward, SLOT(setEnabled(bool)));
  81. m_toolBar->addAction(QIcon(":/icons/document-save-as.png"), tr("&Save to file"),
  82. this, SLOT(saveToFile()));
  83. m_toolBar->addAction(QIcon(":/icons/document-print.png"), tr("Prin&t translation"),
  84. this, SLOT(print()));
  85. m_toolBar->addAction(QIcon(":/icons/speaker.png"), tr("Speak &word"),
  86. this, SLOT(speak()));
  87. QAction *actionSearch = m_toolBar->addAction(QIcon(":/icons/system-search.png"), tr("Search"), this, SLOT(handleSearch()));
  88. actionSearch->setCheckable(true);
  89. actionSearch->setShortcut(QKeySequence::Find);
  90. QVBoxLayout *layout = new QVBoxLayout(this);
  91. layout->setMargin(0);
  92. layout->setSpacing(0);
  93. layout->addWidget(m_toolBar);
  94. layout->addWidget(m_translationView);
  95. layout->addWidget(m_search);
  96. setLayout(layout);
  97. }
  98. void DictWidget::translate(const QString &str)
  99. {
  100. m_translationView->setSource(QUrl("qstardict:" + str));
  101. }
  102. void DictWidget::on_translationView_sourceChanged(const QUrl &name)
  103. {
  104. emit wordTranslated(name.toString(QUrl::RemoveScheme));
  105. }
  106. void DictWidget::saveToFile()
  107. {
  108. static QDir dir( QDir::homePath() ); //added by Frank
  109. static QString filter(tr("Text files (*.txt)")); //added by Frank
  110. QFileDialog dialog(this, tr("Save translation"),
  111. dir.path(), filter); //updated by Frank
  112. dialog.selectFile(translatedWord());//added by Frank
  113. dialog.setNameFilters(QStringList() << tr("HTML files (*.html *.htm)") << tr("Text files (*.txt)"));//updated by Frank
  114. dialog.selectNameFilter(filter); //added by Frank
  115. if (dialog.exec() && dialog.selectedFiles().size())
  116. {
  117. QString fileName = dialog.selectedFiles().first();
  118. /*QString*/ filter = dialog.selectedNameFilter();//updated by Frank
  119. dir = dialog.directory(); //added by Frank
  120. if (filter == tr("HTML files (*.html, *.htm)") &&
  121. ! (fileName.endsWith(".html", Qt::CaseInsensitive) || fileName.endsWith(".htm", Qt::CaseInsensitive)))
  122. fileName += ".html";
  123. else if (filter == tr("Text files (*.txt)") && ! fileName.endsWith(".txt", Qt::CaseInsensitive))
  124. fileName += ".txt";
  125. QFile outputFile(fileName);
  126. if (! outputFile.open(QIODevice::WriteOnly | QIODevice::Text))
  127. {
  128. QMessageBox::warning(this, tr("Error"),
  129. tr("Cannot save translation as %1").arg(fileName));
  130. return;
  131. }
  132. QTextStream outputStream(&outputFile);
  133. if (filter == tr("HTML files (*.html, *.htm)"))
  134. outputStream << m_translationView->document()->toHtml("UTF-8");
  135. else
  136. outputStream << m_translationView->toPlainText();
  137. }
  138. }
  139. void DictWidget::speak()
  140. {
  141. Application::instance()->speaker()->speak(translatedWord());
  142. }
  143. void DictWidget::print()
  144. {
  145. QPrinter printer(QPrinter::HighResolution);
  146. QPrintDialog dialog(&printer, this);
  147. if (dialog.exec() == QDialog::Accepted)
  148. m_translationView->print(&printer);
  149. }
  150. void DictWidget::handleSearch()
  151. {
  152. m_search->setVisible(!m_search->isVisible());
  153. }
  154. void DictWidget::setDefaultStyleSheet(const QString &css)
  155. {
  156. m_translationView->document()->setDefaultStyleSheet(css);
  157. m_translationView->reload();
  158. }
  159. }
  160. // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc