Use of ResultDialog and ProcessingItem
This commit is contained in:
		
							parent
							
								
									a4879c8f91
								
							
						
					
					
						commit
						c7c9d72bd7
					
				
							
								
								
									
										42
									
								
								Manager.cpp
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								Manager.cpp
									
									
									
									
									
								
							@ -16,44 +16,53 @@
 | 
			
		||||
#include "GlobalActionHelper.h"
 | 
			
		||||
#include "Recognizer.h"
 | 
			
		||||
#include "Translator.h"
 | 
			
		||||
#include "ResultDialog.h"
 | 
			
		||||
 | 
			
		||||
Manager::Manager(QObject *parent) :
 | 
			
		||||
  QObject(parent),
 | 
			
		||||
  trayIcon_ (new QSystemTrayIcon (QIcon (":/images/icon.png"), this)),
 | 
			
		||||
  selection_ (new SelectionDialog),
 | 
			
		||||
  resultDialog_ (new ResultDialog),
 | 
			
		||||
  captureAction_ (NULL)
 | 
			
		||||
{
 | 
			
		||||
  GlobalActionHelper::init ();
 | 
			
		||||
  qRegisterMetaType<ProcessingItem>();
 | 
			
		||||
 | 
			
		||||
  selection_->setWindowIcon (trayIcon_->icon ());
 | 
			
		||||
  connect (this, SIGNAL (showPixmap (QPixmap)),
 | 
			
		||||
           selection_, SLOT (setPixmap (QPixmap)));
 | 
			
		||||
 | 
			
		||||
  // Recognizer
 | 
			
		||||
  Recognizer* recognizer = new Recognizer;
 | 
			
		||||
  connect (selection_, SIGNAL (selected (QPixmap)),
 | 
			
		||||
           recognizer, SLOT (recognize (QPixmap)));
 | 
			
		||||
  connect (selection_, SIGNAL (selected (ProcessingItem)),
 | 
			
		||||
           recognizer, SLOT (recognize (ProcessingItem)));
 | 
			
		||||
  connect (recognizer, SIGNAL (error (QString)),
 | 
			
		||||
           SLOT (showError (QString)));
 | 
			
		||||
  connect (this, SIGNAL (settingsEdited ()),
 | 
			
		||||
           recognizer, SLOT (applySettings ()));
 | 
			
		||||
  QThread* recognizerThread = new QThread (this);
 | 
			
		||||
  recognizer->moveToThread (recognizerThread);
 | 
			
		||||
  recognizerThread->start ();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  // Translator
 | 
			
		||||
  Translator* translator = new Translator;
 | 
			
		||||
  connect (recognizer, SIGNAL (recognized (QString)),
 | 
			
		||||
           translator, SLOT (translate (QString)));
 | 
			
		||||
  connect (recognizer, SIGNAL (recognized (ProcessingItem)),
 | 
			
		||||
           translator, SLOT (translate (ProcessingItem)));
 | 
			
		||||
  connect (translator, SIGNAL (error (QString)),
 | 
			
		||||
           SLOT (showError (QString)));
 | 
			
		||||
  connect (this, SIGNAL (settingsEdited ()),
 | 
			
		||||
           translator, SLOT (applySettings ()));
 | 
			
		||||
  QThread* translatorThread = new QThread (this);
 | 
			
		||||
  translator->moveToThread (translatorThread);
 | 
			
		||||
  translatorThread->start ();
 | 
			
		||||
 | 
			
		||||
  connect (translator, SIGNAL (translated (QString, QString)),
 | 
			
		||||
           SLOT (showTranslation (QString, QString)));
 | 
			
		||||
  connect (translator, SIGNAL (translated (ProcessingItem)),
 | 
			
		||||
           SLOT (showResult (ProcessingItem)));
 | 
			
		||||
 | 
			
		||||
  connect (this, SIGNAL (showPixmap (QPixmap)),
 | 
			
		||||
           selection_, SLOT (setPixmap (QPixmap)));
 | 
			
		||||
 | 
			
		||||
  connect (this, SIGNAL (settingsEdited ()), this, SLOT (applySettings ()));
 | 
			
		||||
  connect (this, SIGNAL (settingsEdited ()), recognizer, SLOT (applySettings ()));
 | 
			
		||||
  connect (this, SIGNAL (settingsEdited ()), translator, SLOT (applySettings ()));
 | 
			
		||||
  selection_->setWindowIcon (trayIcon_->icon ());
 | 
			
		||||
  resultDialog_->setWindowIcon (trayIcon_->icon ());
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  connect (trayIcon_, SIGNAL (activated (QSystemTrayIcon::ActivationReason)),
 | 
			
		||||
           SLOT (processTrayAction (QSystemTrayIcon::ActivationReason)));
 | 
			
		||||
@ -151,11 +160,12 @@ void Manager::about()
 | 
			
		||||
  message.exec ();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Manager::showTranslation(QString sourceText, QString translatedText)
 | 
			
		||||
void Manager::showResult(ProcessingItem item)
 | 
			
		||||
{
 | 
			
		||||
  lastMessage_ = sourceText + " - " + translatedText;
 | 
			
		||||
  qDebug () << sourceText << translatedText;
 | 
			
		||||
  trayIcon_->showMessage (tr ("Перевод"), lastMessage_, QSystemTrayIcon::Information);
 | 
			
		||||
  resultDialog_->showResult (item);
 | 
			
		||||
//  lastMessage_ = sourceText + " - " + translatedText;
 | 
			
		||||
//  qDebug () << sourceText << translatedText;
 | 
			
		||||
//  trayIcon_->showMessage (tr ("Перевод"), lastMessage_, QSystemTrayIcon::Information);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Manager::showError(QString text)
 | 
			
		||||
 | 
			
		||||
@ -4,10 +4,13 @@
 | 
			
		||||
#include <QPixmap>
 | 
			
		||||
#include <QSystemTrayIcon>
 | 
			
		||||
 | 
			
		||||
#include "ProcessingItem.h"
 | 
			
		||||
 | 
			
		||||
class QAction;
 | 
			
		||||
class QMenu;
 | 
			
		||||
 | 
			
		||||
class SelectionDialog;
 | 
			
		||||
class ResultDialog;
 | 
			
		||||
 | 
			
		||||
class Manager : public QObject
 | 
			
		||||
{
 | 
			
		||||
@ -18,7 +21,6 @@ class Manager : public QObject
 | 
			
		||||
 | 
			
		||||
  signals:
 | 
			
		||||
    void showPixmap (QPixmap pixmap);
 | 
			
		||||
    void recognize (QPixmap pixmap);
 | 
			
		||||
    void settingsEdited ();
 | 
			
		||||
 | 
			
		||||
  private slots:
 | 
			
		||||
@ -31,7 +33,7 @@ class Manager : public QObject
 | 
			
		||||
 | 
			
		||||
    void processTrayAction (QSystemTrayIcon::ActivationReason reason);
 | 
			
		||||
 | 
			
		||||
    void showTranslation (QString sourceText, QString translatedText);
 | 
			
		||||
    void showResult (ProcessingItem item);
 | 
			
		||||
    void showError (QString text);
 | 
			
		||||
 | 
			
		||||
  private:
 | 
			
		||||
@ -40,6 +42,7 @@ class Manager : public QObject
 | 
			
		||||
  private:
 | 
			
		||||
    QSystemTrayIcon* trayIcon_;
 | 
			
		||||
    SelectionDialog* selection_;
 | 
			
		||||
    ResultDialog* resultDialog_;
 | 
			
		||||
    QAction* captureAction_;
 | 
			
		||||
    QString lastMessage_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										15
									
								
								ProcessingItem.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								ProcessingItem.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,15 @@
 | 
			
		||||
#ifndef PROCESSINGITEM_H
 | 
			
		||||
#define PROCESSINGITEM_H
 | 
			
		||||
 | 
			
		||||
#include <QPixmap>
 | 
			
		||||
 | 
			
		||||
struct ProcessingItem
 | 
			
		||||
{
 | 
			
		||||
    QPoint screenPos;
 | 
			
		||||
    QPixmap source;
 | 
			
		||||
    QString recognized;
 | 
			
		||||
    QString translated;
 | 
			
		||||
};
 | 
			
		||||
Q_DECLARE_METATYPE(ProcessingItem)
 | 
			
		||||
 | 
			
		||||
#endif // PROCESSINGITEM_H
 | 
			
		||||
							
								
								
									
										112
									
								
								Recognizer.cpp
									
									
									
									
									
								
							
							
						
						
									
										112
									
								
								Recognizer.cpp
									
									
									
									
									
								
							@ -1,6 +1,6 @@
 | 
			
		||||
#include "Recognizer.h"
 | 
			
		||||
 | 
			
		||||
#include <tesseract/baseapi.h>
 | 
			
		||||
//#include <tesseract/baseapi.h>
 | 
			
		||||
 | 
			
		||||
#include <QDebug>
 | 
			
		||||
#include <QSettings>
 | 
			
		||||
@ -35,63 +35,67 @@ void Recognizer::applySettings()
 | 
			
		||||
 | 
			
		||||
bool Recognizer::initEngine()
 | 
			
		||||
{
 | 
			
		||||
  if (tessDataDir_.isEmpty () || ocrLanguage_.isEmpty ())
 | 
			
		||||
  {
 | 
			
		||||
    emit error (tr ("Неверные параметры для OCR"));
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
  if (engine_ != NULL)
 | 
			
		||||
  {
 | 
			
		||||
    delete engine_;
 | 
			
		||||
  }
 | 
			
		||||
  engine_ = new tesseract::TessBaseAPI();
 | 
			
		||||
  int result = engine_->Init(qPrintable (tessDataDir_), qPrintable (ocrLanguage_),
 | 
			
		||||
                             tesseract::OEM_DEFAULT);
 | 
			
		||||
  if (result != 0)
 | 
			
		||||
  {
 | 
			
		||||
    emit error (tr ("Ошибка инициализации OCR: %1").arg (result));
 | 
			
		||||
    delete engine_;
 | 
			
		||||
    engine_ = NULL;
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
  return true;
 | 
			
		||||
//  if (tessDataDir_.isEmpty () || ocrLanguage_.isEmpty ())
 | 
			
		||||
//  {
 | 
			
		||||
//    emit error (tr ("Неверные параметры для OCR"));
 | 
			
		||||
//    return false;
 | 
			
		||||
//  }
 | 
			
		||||
//  if (engine_ != NULL)
 | 
			
		||||
//  {
 | 
			
		||||
//    delete engine_;
 | 
			
		||||
//  }
 | 
			
		||||
//  engine_ = new tesseract::TessBaseAPI();
 | 
			
		||||
//  int result = engine_->Init(qPrintable (tessDataDir_), qPrintable (ocrLanguage_),
 | 
			
		||||
//                             tesseract::OEM_DEFAULT);
 | 
			
		||||
//  if (result != 0)
 | 
			
		||||
//  {
 | 
			
		||||
//    emit error (tr ("Ошибка инициализации OCR: %1").arg (result));
 | 
			
		||||
//    delete engine_;
 | 
			
		||||
//    engine_ = NULL;
 | 
			
		||||
//    return false;
 | 
			
		||||
//  }
 | 
			
		||||
//  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Recognizer::recognize(QPixmap pixmap)
 | 
			
		||||
void Recognizer::recognize(ProcessingItem item)
 | 
			
		||||
{
 | 
			
		||||
  Q_ASSERT (!pixmap.isNull ());
 | 
			
		||||
  if (engine_ == NULL)
 | 
			
		||||
  {
 | 
			
		||||
    if (!initEngine ())
 | 
			
		||||
    {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  Q_ASSERT (!item.source.isNull ());
 | 
			
		||||
  item.recognized = "test rec";
 | 
			
		||||
  emit recognized (item);
 | 
			
		||||
  return;
 | 
			
		||||
//  if (engine_ == NULL)
 | 
			
		||||
//  {
 | 
			
		||||
//    if (!initEngine ())
 | 
			
		||||
//    {
 | 
			
		||||
//      return;
 | 
			
		||||
//    }
 | 
			
		||||
//  }
 | 
			
		||||
 | 
			
		||||
  QPixmap scaled = pixmap;
 | 
			
		||||
  if (imageScale_ > 0)
 | 
			
		||||
  {
 | 
			
		||||
    scaled = pixmap.scaledToHeight (pixmap.height () * imageScale_,
 | 
			
		||||
                                    Qt::SmoothTransformation);
 | 
			
		||||
  }
 | 
			
		||||
  QImage image = scaled.toImage ();
 | 
			
		||||
  const int bytesPerPixel = image.depth () / 8;
 | 
			
		||||
  engine_->SetImage (image.bits (), image.width (), image.height (),
 | 
			
		||||
                     bytesPerPixel, image.bytesPerLine ());
 | 
			
		||||
//  QPixmap scaled = pixmap;
 | 
			
		||||
//  if (imageScale_ > 0)
 | 
			
		||||
//  {
 | 
			
		||||
//    scaled = pixmap.scaledToHeight (pixmap.height () * imageScale_,
 | 
			
		||||
//                                    Qt::SmoothTransformation);
 | 
			
		||||
//  }
 | 
			
		||||
//  QImage image = scaled.toImage ();
 | 
			
		||||
//  const int bytesPerPixel = image.depth () / 8;
 | 
			
		||||
//  engine_->SetImage (image.bits (), image.width (), image.height (),
 | 
			
		||||
//                     bytesPerPixel, image.bytesPerLine ());
 | 
			
		||||
 | 
			
		||||
  char* outText = engine_->GetUTF8Text();
 | 
			
		||||
  engine_->Clear();
 | 
			
		||||
//  char* outText = engine_->GetUTF8Text();
 | 
			
		||||
//  engine_->Clear();
 | 
			
		||||
 | 
			
		||||
  QString result (outText);
 | 
			
		||||
  result = result.trimmed();
 | 
			
		||||
  if (!result.isEmpty ())
 | 
			
		||||
  {
 | 
			
		||||
    emit recognized (result);
 | 
			
		||||
    emit recognized (pixmap, result);
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    emit error (tr ("Текст не распознан."));
 | 
			
		||||
  }
 | 
			
		||||
  delete [] outText;
 | 
			
		||||
//  QString result (outText);
 | 
			
		||||
//  result = result.trimmed();
 | 
			
		||||
//  if (!result.isEmpty ())
 | 
			
		||||
//  {
 | 
			
		||||
//  item.recognized = result;
 | 
			
		||||
//    emit recognized (result);
 | 
			
		||||
//    emit recognized (pixmap, result);
 | 
			
		||||
//  }
 | 
			
		||||
//  else
 | 
			
		||||
//  {
 | 
			
		||||
//    emit error (tr ("Текст не распознан."));
 | 
			
		||||
//  }
 | 
			
		||||
//  delete [] outText;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -4,6 +4,7 @@
 | 
			
		||||
#include <QObject>
 | 
			
		||||
#include "QPixmap"
 | 
			
		||||
 | 
			
		||||
#include "ProcessingItem.h"
 | 
			
		||||
 | 
			
		||||
namespace tesseract
 | 
			
		||||
{
 | 
			
		||||
@ -17,12 +18,11 @@ class Recognizer : public QObject
 | 
			
		||||
    explicit Recognizer(QObject *parent = 0);
 | 
			
		||||
 | 
			
		||||
  signals:
 | 
			
		||||
    void recognized (QString text);
 | 
			
		||||
    void recognized (QPixmap pixmap, QString text);
 | 
			
		||||
    void recognized (ProcessingItem item);
 | 
			
		||||
    void error (QString text);
 | 
			
		||||
 | 
			
		||||
  public slots:
 | 
			
		||||
    void recognize (QPixmap pixmap);
 | 
			
		||||
    void recognize (ProcessingItem item);
 | 
			
		||||
    void applySettings ();
 | 
			
		||||
 | 
			
		||||
  private:
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										62
									
								
								ResultDialog.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								ResultDialog.cpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,62 @@
 | 
			
		||||
#include "ResultDialog.h"
 | 
			
		||||
#include "ui_ResultDialog.h"
 | 
			
		||||
 | 
			
		||||
#include <QDesktopWidget>
 | 
			
		||||
 | 
			
		||||
ResultDialog::ResultDialog(QWidget *parent) :
 | 
			
		||||
  QDialog(parent),
 | 
			
		||||
  ui(new Ui::ResultDialog),
 | 
			
		||||
  isShowAtCapturePos_ (true)
 | 
			
		||||
{
 | 
			
		||||
  ui->setupUi(this);
 | 
			
		||||
  setWindowFlags (Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint |
 | 
			
		||||
                  Qt::WindowStaysOnTopHint);
 | 
			
		||||
 | 
			
		||||
  installEventFilter (this);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ResultDialog::~ResultDialog()
 | 
			
		||||
{
 | 
			
		||||
  delete ui;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ResultDialog::eventFilter(QObject* object, QEvent* event)
 | 
			
		||||
{
 | 
			
		||||
  Q_UNUSED (object);
 | 
			
		||||
  if (event->type () == QEvent::MouseButtonRelease)
 | 
			
		||||
  {
 | 
			
		||||
    hide ();
 | 
			
		||||
  }
 | 
			
		||||
  return QDialog::eventFilter (object, event);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ResultDialog::showResult(ProcessingItem item)
 | 
			
		||||
{
 | 
			
		||||
  Q_ASSERT (!item.source.isNull ());
 | 
			
		||||
  Q_ASSERT (!item.recognized.isEmpty ());
 | 
			
		||||
  Q_ASSERT (!item.translated.isEmpty ());
 | 
			
		||||
  Q_ASSERT (!item.screenPos.isNull ());
 | 
			
		||||
 | 
			
		||||
  ui->sourceLabel->setPixmap (item.source);
 | 
			
		||||
  ui->recognizeLabel->setText (item.recognized);
 | 
			
		||||
  ui->translateLabel->setText (item.translated);
 | 
			
		||||
 | 
			
		||||
  adjustSize ();
 | 
			
		||||
 | 
			
		||||
  if (isShowAtCapturePos_)
 | 
			
		||||
  {
 | 
			
		||||
    QPoint correction = QPoint (ui->frame->lineWidth (), ui->frame->lineWidth ());
 | 
			
		||||
    move (item.screenPos - correction);
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
    QDesktopWidget* desktop = QApplication::desktop ();
 | 
			
		||||
    Q_CHECK_PTR (desktop);
 | 
			
		||||
    QRect screenRect = desktop->availableGeometry (this);
 | 
			
		||||
    Q_ASSERT (screenRect.isValid ());
 | 
			
		||||
    QPoint newPos (screenRect.width () - width (), screenRect.height () - height ());
 | 
			
		||||
    move (newPos);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  show ();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										31
									
								
								ResultDialog.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								ResultDialog.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,31 @@
 | 
			
		||||
#ifndef RESULTDIALOG_H
 | 
			
		||||
#define RESULTDIALOG_H
 | 
			
		||||
 | 
			
		||||
#include <QDialog>
 | 
			
		||||
 | 
			
		||||
#include "ProcessingItem.h"
 | 
			
		||||
 | 
			
		||||
namespace Ui {
 | 
			
		||||
  class ResultDialog;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class ResultDialog : public QDialog
 | 
			
		||||
{
 | 
			
		||||
    Q_OBJECT
 | 
			
		||||
 | 
			
		||||
  public:
 | 
			
		||||
    explicit ResultDialog(QWidget *parent = 0);
 | 
			
		||||
    ~ResultDialog();
 | 
			
		||||
 | 
			
		||||
  public:
 | 
			
		||||
    bool eventFilter (QObject *object, QEvent *event);
 | 
			
		||||
 | 
			
		||||
  public slots:
 | 
			
		||||
    void showResult (ProcessingItem item);
 | 
			
		||||
 | 
			
		||||
  private:
 | 
			
		||||
    Ui::ResultDialog *ui;
 | 
			
		||||
    bool isShowAtCapturePos_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // RESULTDIALOG_H
 | 
			
		||||
							
								
								
									
										98
									
								
								ResultDialog.ui
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								ResultDialog.ui
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,98 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<ui version="4.0">
 | 
			
		||||
 <class>ResultDialog</class>
 | 
			
		||||
 <widget class="QDialog" name="ResultDialog">
 | 
			
		||||
  <property name="geometry">
 | 
			
		||||
   <rect>
 | 
			
		||||
    <x>0</x>
 | 
			
		||||
    <y>0</y>
 | 
			
		||||
    <width>260</width>
 | 
			
		||||
    <height>222</height>
 | 
			
		||||
   </rect>
 | 
			
		||||
  </property>
 | 
			
		||||
  <property name="windowTitle">
 | 
			
		||||
   <string>Результат</string>
 | 
			
		||||
  </property>
 | 
			
		||||
  <layout class="QGridLayout" name="gridLayout">
 | 
			
		||||
   <property name="margin">
 | 
			
		||||
    <number>0</number>
 | 
			
		||||
   </property>
 | 
			
		||||
   <property name="spacing">
 | 
			
		||||
    <number>0</number>
 | 
			
		||||
   </property>
 | 
			
		||||
   <item row="0" column="0">
 | 
			
		||||
    <widget class="QFrame" name="frame">
 | 
			
		||||
     <property name="frameShape">
 | 
			
		||||
      <enum>QFrame::StyledPanel</enum>
 | 
			
		||||
     </property>
 | 
			
		||||
     <property name="frameShadow">
 | 
			
		||||
      <enum>QFrame::Plain</enum>
 | 
			
		||||
     </property>
 | 
			
		||||
     <layout class="QGridLayout" name="gridLayout_2">
 | 
			
		||||
      <property name="margin">
 | 
			
		||||
       <number>0</number>
 | 
			
		||||
      </property>
 | 
			
		||||
      <property name="spacing">
 | 
			
		||||
       <number>0</number>
 | 
			
		||||
      </property>
 | 
			
		||||
      <item row="0" column="0">
 | 
			
		||||
       <widget class="QLabel" name="sourceLabel">
 | 
			
		||||
        <property name="text">
 | 
			
		||||
         <string/>
 | 
			
		||||
        </property>
 | 
			
		||||
        <property name="alignment">
 | 
			
		||||
         <set>Qt::AlignCenter</set>
 | 
			
		||||
        </property>
 | 
			
		||||
        <property name="wordWrap">
 | 
			
		||||
         <bool>true</bool>
 | 
			
		||||
        </property>
 | 
			
		||||
       </widget>
 | 
			
		||||
      </item>
 | 
			
		||||
      <item row="1" column="0">
 | 
			
		||||
       <widget class="Line" name="line_2">
 | 
			
		||||
        <property name="orientation">
 | 
			
		||||
         <enum>Qt::Horizontal</enum>
 | 
			
		||||
        </property>
 | 
			
		||||
       </widget>
 | 
			
		||||
      </item>
 | 
			
		||||
      <item row="2" column="0">
 | 
			
		||||
       <widget class="QLabel" name="recognizeLabel">
 | 
			
		||||
        <property name="text">
 | 
			
		||||
         <string/>
 | 
			
		||||
        </property>
 | 
			
		||||
        <property name="alignment">
 | 
			
		||||
         <set>Qt::AlignCenter</set>
 | 
			
		||||
        </property>
 | 
			
		||||
        <property name="wordWrap">
 | 
			
		||||
         <bool>true</bool>
 | 
			
		||||
        </property>
 | 
			
		||||
       </widget>
 | 
			
		||||
      </item>
 | 
			
		||||
      <item row="3" column="0">
 | 
			
		||||
       <widget class="Line" name="line">
 | 
			
		||||
        <property name="orientation">
 | 
			
		||||
         <enum>Qt::Horizontal</enum>
 | 
			
		||||
        </property>
 | 
			
		||||
       </widget>
 | 
			
		||||
      </item>
 | 
			
		||||
      <item row="4" column="0">
 | 
			
		||||
       <widget class="QLabel" name="translateLabel">
 | 
			
		||||
        <property name="text">
 | 
			
		||||
         <string/>
 | 
			
		||||
        </property>
 | 
			
		||||
        <property name="alignment">
 | 
			
		||||
         <set>Qt::AlignCenter</set>
 | 
			
		||||
        </property>
 | 
			
		||||
        <property name="wordWrap">
 | 
			
		||||
         <bool>true</bool>
 | 
			
		||||
        </property>
 | 
			
		||||
       </widget>
 | 
			
		||||
      </item>
 | 
			
		||||
     </layout>
 | 
			
		||||
    </widget>
 | 
			
		||||
   </item>
 | 
			
		||||
  </layout>
 | 
			
		||||
 </widget>
 | 
			
		||||
 <resources/>
 | 
			
		||||
 <connections/>
 | 
			
		||||
</ui>
 | 
			
		||||
@ -11,9 +11,9 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 | 
			
		||||
TARGET = ScreenTranslator
 | 
			
		||||
TEMPLATE = app
 | 
			
		||||
 | 
			
		||||
INCLUDEPATH += C:/build/include
 | 
			
		||||
#INCLUDEPATH += C:/build/include
 | 
			
		||||
 | 
			
		||||
LIBS += -LC:/build/bin -ltesseract
 | 
			
		||||
#LIBS += -LC:/build/bin -ltesseract
 | 
			
		||||
 | 
			
		||||
SOURCES += main.cpp\
 | 
			
		||||
    Manager.cpp \
 | 
			
		||||
@ -21,7 +21,8 @@ SOURCES += main.cpp\
 | 
			
		||||
    SelectionDialog.cpp \
 | 
			
		||||
    GlobalActionHelper.cpp \
 | 
			
		||||
    Recognizer.cpp \
 | 
			
		||||
    Translator.cpp
 | 
			
		||||
    Translator.cpp \
 | 
			
		||||
    ResultDialog.cpp
 | 
			
		||||
 | 
			
		||||
HEADERS  += \
 | 
			
		||||
    Manager.h \
 | 
			
		||||
@ -30,11 +31,14 @@ HEADERS  += \
 | 
			
		||||
    GlobalActionHelper.h \
 | 
			
		||||
    Recognizer.h \
 | 
			
		||||
    Translator.h \
 | 
			
		||||
    Settings.h
 | 
			
		||||
    Settings.h \
 | 
			
		||||
    ProcessingItem.h \
 | 
			
		||||
    ResultDialog.h
 | 
			
		||||
 | 
			
		||||
FORMS    += \
 | 
			
		||||
    SettingsEditor.ui \
 | 
			
		||||
    SelectionDialog.ui
 | 
			
		||||
    SelectionDialog.ui \
 | 
			
		||||
    ResultDialog.ui
 | 
			
		||||
 | 
			
		||||
RESOURCES += \
 | 
			
		||||
    Recources.qrc
 | 
			
		||||
 | 
			
		||||
@ -52,7 +52,10 @@ bool SelectionDialog::eventFilter(QObject* object, QEvent* event)
 | 
			
		||||
      QPixmap selectedPixmap = currentPixmap_.copy (selection);
 | 
			
		||||
      if (!selectedPixmap.isNull ())
 | 
			
		||||
      {
 | 
			
		||||
        emit selected (selectedPixmap);
 | 
			
		||||
        ProcessingItem item;
 | 
			
		||||
        item.source = selectedPixmap;
 | 
			
		||||
        item.screenPos = selection.topLeft ();
 | 
			
		||||
        emit selected (item);
 | 
			
		||||
        accept ();
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -4,6 +4,8 @@
 | 
			
		||||
#include <QDialog>
 | 
			
		||||
#include <QPixmap>
 | 
			
		||||
 | 
			
		||||
#include "ProcessingItem.h"
 | 
			
		||||
 | 
			
		||||
namespace Ui {
 | 
			
		||||
  class SelectionDialog;
 | 
			
		||||
}
 | 
			
		||||
@ -19,7 +21,7 @@ class SelectionDialog : public QDialog
 | 
			
		||||
    bool eventFilter (QObject *object, QEvent *event);
 | 
			
		||||
 | 
			
		||||
  signals:
 | 
			
		||||
    void selected (QPixmap pixmap);
 | 
			
		||||
    void selected (ProcessingItem pixmap);
 | 
			
		||||
 | 
			
		||||
  public slots:
 | 
			
		||||
    void setPixmap (QPixmap pixmap);
 | 
			
		||||
 | 
			
		||||
@ -35,20 +35,26 @@ void Translator::applySettings()
 | 
			
		||||
                         toString ();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Translator::translate(QString text)
 | 
			
		||||
void Translator::translate(ProcessingItem item)
 | 
			
		||||
{
 | 
			
		||||
  Q_ASSERT (!text.isEmpty ());
 | 
			
		||||
  Q_ASSERT (!item.recognized.isEmpty ());
 | 
			
		||||
  item.translated = "проверка";
 | 
			
		||||
  emit translated (item);
 | 
			
		||||
  return;
 | 
			
		||||
  if (translationLanguage_.isEmpty ())
 | 
			
		||||
  {
 | 
			
		||||
    emit error (tr ("Неверные парметры для перевода."));
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  QUrl url (translateBaseUrl.arg (text, translationLanguage_));
 | 
			
		||||
  network_.get (QNetworkRequest (url));
 | 
			
		||||
  QUrl url (translateBaseUrl.arg (item.recognized, translationLanguage_));
 | 
			
		||||
  QNetworkReply* reply = network_.get (QNetworkRequest (url));
 | 
			
		||||
  items_.insert (reply, item);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Translator::replyFinished(QNetworkReply* reply)
 | 
			
		||||
{
 | 
			
		||||
  Q_ASSERT (items_.contains (reply));
 | 
			
		||||
  ProcessingItem item = items_.take (reply);
 | 
			
		||||
  Q_ASSERT (reply->isFinished ());
 | 
			
		||||
  if (reply->error () != QNetworkReply::NoError)
 | 
			
		||||
  {
 | 
			
		||||
@ -73,7 +79,6 @@ void Translator::replyFinished(QNetworkReply* reply)
 | 
			
		||||
  }
 | 
			
		||||
  QJsonArray answerArray = document.array ();
 | 
			
		||||
  QJsonArray fullTranslation = answerArray.first ().toArray ();
 | 
			
		||||
  QString source = "";
 | 
			
		||||
  QString translation = "";
 | 
			
		||||
  foreach (QJsonValue part, fullTranslation)
 | 
			
		||||
  {
 | 
			
		||||
@ -83,7 +88,7 @@ void Translator::replyFinished(QNetworkReply* reply)
 | 
			
		||||
      continue;
 | 
			
		||||
    }
 | 
			
		||||
    translation += partTranslation.at (0).toString ();
 | 
			
		||||
    source += partTranslation.at (1).toString ();
 | 
			
		||||
  }
 | 
			
		||||
  emit translated (source, translation);
 | 
			
		||||
  item.translated = translation;
 | 
			
		||||
  emit translated (item);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,8 @@
 | 
			
		||||
 | 
			
		||||
#include <QNetworkAccessManager>
 | 
			
		||||
 | 
			
		||||
#include "ProcessingItem.h"
 | 
			
		||||
 | 
			
		||||
class Translator : public QObject
 | 
			
		||||
{
 | 
			
		||||
    Q_OBJECT
 | 
			
		||||
@ -10,11 +12,11 @@ class Translator : public QObject
 | 
			
		||||
    explicit Translator(QObject *parent = 0);
 | 
			
		||||
 | 
			
		||||
  signals:
 | 
			
		||||
    void translated (QString sourceText, QString translatedText);
 | 
			
		||||
    void translated (ProcessingItem item);
 | 
			
		||||
    void error (QString text);
 | 
			
		||||
 | 
			
		||||
  public slots:
 | 
			
		||||
    void translate (QString text);
 | 
			
		||||
    void translate (ProcessingItem item);
 | 
			
		||||
    void applySettings ();
 | 
			
		||||
 | 
			
		||||
  private slots:
 | 
			
		||||
@ -23,6 +25,7 @@ class Translator : public QObject
 | 
			
		||||
  private:
 | 
			
		||||
    QNetworkAccessManager network_;
 | 
			
		||||
    QString translationLanguage_;
 | 
			
		||||
    QHash<QNetworkReply*, ProcessingItem> items_;
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user