diff --git a/GoogleWebTranslator.cpp b/GoogleWebTranslator.cpp new file mode 100644 index 0000000..750ce71 --- /dev/null +++ b/GoogleWebTranslator.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include + +#include "GoogleWebTranslator.h" +#include "Settings.h" + +GoogleWebTranslator::GoogleWebTranslator() + : QObject (), view_ (new QWebView), + isLoadFinished_ (true), isTranslationFinished_ (false) { + + view_->settings()->setAttribute(QWebSettings::AutoLoadImages, false); + connect (view_, SIGNAL (loadStarted()), this, SLOT (loadStarted())); + connect (view_, SIGNAL (loadFinished(bool)), this, SLOT (loadFinished(bool))); + + connect (view_->page()->networkAccessManager(), SIGNAL (finished(QNetworkReply*)), + this, SLOT(replyFinished(QNetworkReply*))); + applySettings (); +} + +GoogleWebTranslator::~GoogleWebTranslator() { + delete view_; +} + +void GoogleWebTranslator::translate(ProcessingItem item) { + queue_.push_back (item); + if (isLoadFinished_) { + load (item); + } +} + +void GoogleWebTranslator::applySettings(){ + QSettings settings; + settings.beginGroup (settings_names::translationGroup); + translationLanguage_ = settings.value (settings_names::translationLanguage, + settings_values::translationLanguage).toString (); +} + +void GoogleWebTranslator::loadStarted() { + isLoadFinished_ = false; + isTranslationFinished_ = false; +} + +void GoogleWebTranslator::loadFinished(bool ok) { + isLoadFinished_ = true; + if (ok && !isTranslationFinished_) { + return; + } + + Q_ASSERT (!queue_.isEmpty()); + ProcessingItem item = queue_.front(); + queue_.pop_front(); + if (ok) { + QWebElementCollection result = view_->page()->mainFrame()->findAllElements("#result_box > span"); + item.translated = ""; + foreach (const QWebElement& element, result) { + item.translated += element.toInnerXml() + " "; + } + emit translated(item, !item.translated.isEmpty()); + } + else { + emit translated (item, false); + } + + if (!queue_.isEmpty()) { + load (queue_.front()); + } +} + +void GoogleWebTranslator::replyFinished(QNetworkReply *reply) +{ + if (reply->url().toString().contains ("/translate_a/single")) { + isTranslationFinished_ = true; + if (isLoadFinished_) { + QTimer::singleShot(2000, this, SLOT(loadFinished())); + } + } +} + +void GoogleWebTranslator::load(const ProcessingItem &item) { + Q_ASSERT (!item.recognized.isEmpty ()); + if (translationLanguage_.isEmpty ()) { + emit error (tr ("Неверные парметры для перевода.")); + return; + } + QUrl url (QString ("https://translate.google.com/#auto/%1/%2").arg(translationLanguage_, item.recognized)); + view_->setUrl(url); +} diff --git a/GoogleWebTranslator.h b/GoogleWebTranslator.h new file mode 100644 index 0000000..b48cc82 --- /dev/null +++ b/GoogleWebTranslator.h @@ -0,0 +1,43 @@ +#ifndef GOOGLEWEBTRANSLATOR_H +#define GOOGLEWEBTRANSLATOR_H + +#include + +#include "ProcessingItem.h" + +class QWebView; +class QUrl; +class QNetworkReply; + +class GoogleWebTranslator : public QObject +{ + Q_OBJECT + public: + GoogleWebTranslator(); + ~GoogleWebTranslator(); + + signals: + void translated (ProcessingItem item, bool success); + void error (QString text); + + public slots: + void translate (ProcessingItem item); + void applySettings (); + + private slots: + void loadStarted (); + void loadFinished(bool ok=true); + void replyFinished(QNetworkReply * reply); + + private: + void load (const ProcessingItem& item); + + private: + QVector queue_; + QString translationLanguage_; + QWebView *view_; + bool isLoadFinished_; + bool isTranslationFinished_; +}; + +#endif // GOOGLEWEBTRANSLATOR_H diff --git a/ScreenTranslator.pro b/ScreenTranslator.pro index 61bf608..ca5b294 100644 --- a/ScreenTranslator.pro +++ b/ScreenTranslator.pro @@ -26,7 +26,8 @@ SOURCES += main.cpp\ ResultDialog.cpp \ ProcessingItem.cpp \ ImageProcessing.cpp \ - LanguageHelper.cpp + LanguageHelper.cpp \ + GoogleWebTranslator.cpp HEADERS += \ Manager.h \ @@ -39,7 +40,8 @@ HEADERS += \ ProcessingItem.h \ ResultDialog.h \ ImageProcessing.h \ - LanguageHelper.h + LanguageHelper.h \ + GoogleWebTranslator.h FORMS += \ SettingsEditor.ui \