ProcessingItem added

ResultDialog added
This commit is contained in:
msn 2013-11-26 20:59:47 +04:00
parent c7c9d72bd7
commit 476893a7cf
12 changed files with 261 additions and 118 deletions

View File

@ -23,7 +23,8 @@ Manager::Manager(QObject *parent) :
trayIcon_ (new QSystemTrayIcon (QIcon (":/images/icon.png"), this)), trayIcon_ (new QSystemTrayIcon (QIcon (":/images/icon.png"), this)),
selection_ (new SelectionDialog), selection_ (new SelectionDialog),
resultDialog_ (new ResultDialog), resultDialog_ (new ResultDialog),
captureAction_ (NULL) captureAction_ (NULL), repeatAction_ (NULL), clipboardAction_ (NULL),
useResultDialog_ (true)
{ {
GlobalActionHelper::init (); GlobalActionHelper::init ();
qRegisterMetaType<ProcessingItem>(); qRegisterMetaType<ProcessingItem>();
@ -77,6 +78,11 @@ QMenu*Manager::trayContextMenu()
{ {
QMenu* menu = new QMenu (); QMenu* menu = new QMenu ();
captureAction_ = menu->addAction (tr ("Захват"), this, SLOT (capture ())); captureAction_ = menu->addAction (tr ("Захват"), this, SLOT (capture ()));
QMenu* translateMenu = menu->addMenu (tr ("Перевод"));
repeatAction_ = translateMenu->addAction (tr ("Повторить"), this,
SLOT (showLast ()));
clipboardAction_ = translateMenu->addAction (tr ("Скопировать"), this,
SLOT (copyLastToClipboard ()));
menu->addAction (tr ("Настройки"), this, SLOT (settings ())); menu->addAction (tr ("Настройки"), this, SLOT (settings ()));
menu->addAction (tr ("О программе"), this, SLOT (about ())); menu->addAction (tr ("О программе"), this, SLOT (about ()));
menu->addAction (tr ("Выход"), this, SLOT (close ())); menu->addAction (tr ("Выход"), this, SLOT (close ()));
@ -88,35 +94,29 @@ void Manager::applySettings()
QSettings settings; QSettings settings;
settings.beginGroup (settings_names::guiGroup); settings.beginGroup (settings_names::guiGroup);
QString captureHotkey = settings.value (settings_names::captureHotkey, QString captureHotkey = settings.value (settings_names::captureHotkey,
settings_values::captureHotkey). settings_values::captureHotkey).toString ();
toString ();
Q_CHECK_PTR (captureAction_); Q_CHECK_PTR (captureAction_);
GlobalActionHelper::removeGlobal (captureAction_); GlobalActionHelper::removeGlobal (captureAction_);
captureAction_->setShortcut (captureHotkey); captureAction_->setShortcut (captureHotkey);
GlobalActionHelper::makeGlobal (captureAction_); GlobalActionHelper::makeGlobal (captureAction_);
}
void Manager::processTrayAction(QSystemTrayIcon::ActivationReason reason) QString repeatHotkey = settings.value (settings_names::repeatHotkey,
{ settings_values::repeatHotkey).toString ();
if (reason == QSystemTrayIcon::Trigger) Q_CHECK_PTR (repeatAction_);
{ GlobalActionHelper::removeGlobal (repeatAction_);
if (!lastMessage_.isEmpty ()) repeatAction_->setShortcut (repeatHotkey);
{ GlobalActionHelper::makeGlobal (repeatAction_);
trayIcon_->showMessage (tr ("Последний перевод"), lastMessage_,
QSystemTrayIcon::Information); QString clipboardHotkey = settings.value (settings_names::clipboardHotkey,
} settings_values::clipboardHotkey).toString ();
} Q_CHECK_PTR (clipboardAction_);
else if (reason == QSystemTrayIcon::MiddleClick) GlobalActionHelper::removeGlobal (clipboardAction_);
{ clipboardAction_->setShortcut (clipboardHotkey);
if (!lastMessage_.isEmpty ()) GlobalActionHelper::makeGlobal (clipboardAction_);
{
QClipboard* clipboard = QApplication::clipboard (); // Depends on SettingsEditor button indexes. 1==dialog
clipboard->setText (lastMessage_); useResultDialog_ = settings.value (settings_names::resultShowType,
trayIcon_->showMessage (tr ("Последний перевод"), settings_values::resultShowType).toBool ();
tr ("Последний перевод был скопирован в буфер обмена."),
QSystemTrayIcon::Information);
}
}
} }
Manager::~Manager() Manager::~Manager()
@ -160,12 +160,52 @@ void Manager::about()
message.exec (); message.exec ();
} }
void Manager::processTrayAction(QSystemTrayIcon::ActivationReason reason)
{
if (reason == QSystemTrayIcon::Trigger)
{
showLast ();
}
else if (reason == QSystemTrayIcon::MiddleClick)
{
copyLastToClipboard ();
}
}
void Manager::showLast()
{
if (lastItem_.isValid ())
{
showResult (lastItem_);
}
}
void Manager::copyLastToClipboard()
{
if (lastItem_.isValid ())
{
QClipboard* clipboard = QApplication::clipboard ();
QString message = lastItem_.recognized + " - " + lastItem_.translated;
clipboard->setText (message);
trayIcon_->showMessage (tr ("Перевод"),
tr ("Последний перевод был скопирован в буфер обмена."),
QSystemTrayIcon::Information);
}
}
void Manager::showResult(ProcessingItem item) void Manager::showResult(ProcessingItem item)
{ {
Q_ASSERT (item.isValid ());
lastItem_ = item;
if (useResultDialog_)
{
resultDialog_->showResult (item); resultDialog_->showResult (item);
// lastMessage_ = sourceText + " - " + translatedText; }
// qDebug () << sourceText << translatedText; else
// trayIcon_->showMessage (tr ("Перевод"), lastMessage_, QSystemTrayIcon::Information); {
QString message = item.recognized + " - " + item.translated;
trayIcon_->showMessage (tr ("Перевод"), message, QSystemTrayIcon::Information);
}
} }
void Manager::showError(QString text) void Manager::showError(QString text)

View File

@ -28,6 +28,8 @@ class Manager : public QObject
void settings (); void settings ();
void close (); void close ();
void about (); void about ();
void showLast ();
void copyLastToClipboard ();
void applySettings (); void applySettings ();
@ -44,7 +46,10 @@ class Manager : public QObject
SelectionDialog* selection_; SelectionDialog* selection_;
ResultDialog* resultDialog_; ResultDialog* resultDialog_;
QAction* captureAction_; QAction* captureAction_;
QString lastMessage_; QAction* repeatAction_;
QAction* clipboardAction_;
ProcessingItem lastItem_;
bool useResultDialog_;
}; };
#endif // MANAGER_H #endif // MANAGER_H

11
ProcessingItem.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "ProcessingItem.h"
bool ProcessingItem::isValid() const
{
bool valid = true;
valid &= (!screenPos.isNull ());
valid &= (!source.isNull ());
valid &= (!recognized.isEmpty ());
valid &= (!translated.isEmpty ());
return valid;
}

View File

@ -9,6 +9,8 @@ struct ProcessingItem
QPixmap source; QPixmap source;
QString recognized; QString recognized;
QString translated; QString translated;
bool isValid () const;
}; };
Q_DECLARE_METATYPE(ProcessingItem) Q_DECLARE_METATYPE(ProcessingItem)

View File

@ -1,6 +1,6 @@
#include "Recognizer.h" #include "Recognizer.h"
//#include <tesseract/baseapi.h> #include <tesseract/baseapi.h>
#include <QDebug> #include <QDebug>
#include <QSettings> #include <QSettings>
@ -35,67 +35,63 @@ void Recognizer::applySettings()
bool Recognizer::initEngine() bool Recognizer::initEngine()
{ {
// if (tessDataDir_.isEmpty () || ocrLanguage_.isEmpty ()) if (tessDataDir_.isEmpty () || ocrLanguage_.isEmpty ())
// { {
// emit error (tr ("Неверные параметры для OCR")); emit error (tr ("Неверные параметры для OCR"));
// return false; return false;
// } }
// if (engine_ != NULL) if (engine_ != NULL)
// { {
// delete engine_; delete engine_;
// } }
// engine_ = new tesseract::TessBaseAPI(); engine_ = new tesseract::TessBaseAPI();
// int result = engine_->Init(qPrintable (tessDataDir_), qPrintable (ocrLanguage_), int result = engine_->Init(qPrintable (tessDataDir_), qPrintable (ocrLanguage_),
// tesseract::OEM_DEFAULT); tesseract::OEM_DEFAULT);
// if (result != 0) if (result != 0)
// { {
// emit error (tr ("Ошибка инициализации OCR: %1").arg (result)); emit error (tr ("Ошибка инициализации OCR: %1").arg (result));
// delete engine_; delete engine_;
// engine_ = NULL; engine_ = NULL;
// return false; return false;
// } }
// return true; return true;
} }
void Recognizer::recognize(ProcessingItem item) void Recognizer::recognize(ProcessingItem item)
{ {
Q_ASSERT (!item.source.isNull ()); Q_ASSERT (!item.source.isNull ());
item.recognized = "test rec"; if (engine_ == NULL)
emit recognized (item); {
if (!initEngine ())
{
return; return;
// if (engine_ == NULL) }
// { }
// if (!initEngine ())
// {
// return;
// }
// }
// QPixmap scaled = pixmap; QPixmap scaled = item.source;
// if (imageScale_ > 0) if (imageScale_ > 0)
// { {
// scaled = pixmap.scaledToHeight (pixmap.height () * imageScale_, scaled = scaled.scaledToHeight (scaled.height () * imageScale_,
// Qt::SmoothTransformation); Qt::SmoothTransformation);
// } }
// QImage image = scaled.toImage (); QImage image = scaled.toImage ();
// const int bytesPerPixel = image.depth () / 8; const int bytesPerPixel = image.depth () / 8;
// engine_->SetImage (image.bits (), image.width (), image.height (), engine_->SetImage (image.bits (), image.width (), image.height (),
// bytesPerPixel, image.bytesPerLine ()); bytesPerPixel, image.bytesPerLine ());
// char* outText = engine_->GetUTF8Text(); char* outText = engine_->GetUTF8Text();
// engine_->Clear(); engine_->Clear();
// QString result (outText); QString result (outText);
// result = result.trimmed(); result = result.trimmed();
// if (!result.isEmpty ()) if (!result.isEmpty ())
// { {
// item.recognized = result; item.recognized = result;
// emit recognized (result); emit recognized (item);
// emit recognized (pixmap, result); }
// } else
// else {
// { emit error (tr ("Текст не распознан."));
// emit error (tr ("Текст не распознан.")); }
// } delete [] outText;
// delete [] outText;
} }

View File

@ -43,15 +43,22 @@ void ResultDialog::showResult(ProcessingItem item)
adjustSize (); adjustSize ();
QDesktopWidget* desktop = QApplication::desktop ();
Q_CHECK_PTR (desktop);
if (isShowAtCapturePos_) if (isShowAtCapturePos_)
{ {
QPoint correction = QPoint (ui->frame->lineWidth (), ui->frame->lineWidth ()); QPoint correction = QPoint (ui->frame->lineWidth (), ui->frame->lineWidth ());
move (item.screenPos - correction); move (item.screenPos - correction);
QRect screenRect = desktop->screenGeometry (this);
int minY = screenRect.bottom () - height ();
if (y () > minY)
{
move (x (), minY);
}
} }
else else
{ {
QDesktopWidget* desktop = QApplication::desktop ();
Q_CHECK_PTR (desktop);
QRect screenRect = desktop->availableGeometry (this); QRect screenRect = desktop->availableGeometry (this);
Q_ASSERT (screenRect.isValid ()); Q_ASSERT (screenRect.isValid ());
QPoint newPos (screenRect.width () - width (), screenRect.height () - height ()); QPoint newPos (screenRect.width () - width (), screenRect.height () - height ());

View File

@ -11,9 +11,9 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ScreenTranslator TARGET = ScreenTranslator
TEMPLATE = app TEMPLATE = app
#INCLUDEPATH += C:/build/include INCLUDEPATH += C:/build/include
#LIBS += -LC:/build/bin -ltesseract LIBS += -LC:/build/bin -ltesseract
SOURCES += main.cpp\ SOURCES += main.cpp\
Manager.cpp \ Manager.cpp \
@ -22,7 +22,8 @@ SOURCES += main.cpp\
GlobalActionHelper.cpp \ GlobalActionHelper.cpp \
Recognizer.cpp \ Recognizer.cpp \
Translator.cpp \ Translator.cpp \
ResultDialog.cpp ResultDialog.cpp \
ProcessingItem.cpp
HEADERS += \ HEADERS += \
Manager.h \ Manager.h \

View File

@ -9,6 +9,9 @@ namespace settings_names
const QString guiGroup = "GUI"; const QString guiGroup = "GUI";
const QString geometry = "geometry"; const QString geometry = "geometry";
const QString captureHotkey = "captureHotkey"; const QString captureHotkey = "captureHotkey";
const QString repeatHotkey = "repeatHotkey";
const QString clipboardHotkey = "clipboardHotkey";
const QString resultShowType = "resultShowType";
//! Recognition //! Recognition
const QString recogntionGroup = "Recognition"; const QString recogntionGroup = "Recognition";
@ -29,6 +32,9 @@ namespace settings_values
//! UI //! UI
const QString captureHotkey = "Ctrl+Alt+Z"; const QString captureHotkey = "Ctrl+Alt+Z";
const QString repeatHotkey = "Ctrl+Alt+X";
const QString clipboardHotkey = "Ctrl+Alt+C";
const QString resultShowType = "1";//dialog
//! Recognition //! Recognition
const QString tessDataPlace = "./"; const QString tessDataPlace = "./";

View File

@ -10,10 +10,14 @@
SettingsEditor::SettingsEditor(QWidget *parent) : SettingsEditor::SettingsEditor(QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::SettingsEditor) ui(new Ui::SettingsEditor),
buttonGroup_ (new QButtonGroup (this))
{ {
ui->setupUi(this); ui->setupUi(this);
buttonGroup_->addButton (ui->trayRadio, 0);
buttonGroup_->addButton (ui->dialogRadio, 1);
connect (ui->tessdataButton, SIGNAL (clicked ()), SLOT (openTessdataDialog ())); connect (ui->tessdataButton, SIGNAL (clicked ()), SLOT (openTessdataDialog ()));
connect (ui->tessdataEdit, SIGNAL (textChanged (const QString&)), connect (ui->tessdataEdit, SIGNAL (textChanged (const QString&)),
SLOT (initOcrLangCombo ())); SLOT (initOcrLangCombo ()));
@ -44,6 +48,9 @@ void SettingsEditor::saveSettings() const
QSettings settings; QSettings settings;
settings.beginGroup (settings_names::guiGroup); settings.beginGroup (settings_names::guiGroup);
settings.setValue (settings_names::captureHotkey, ui->captureEdit->text ()); settings.setValue (settings_names::captureHotkey, ui->captureEdit->text ());
settings.setValue (settings_names::repeatHotkey, ui->repeatEdit->text ());
settings.setValue (settings_names::clipboardHotkey, ui->clipboardEdit->text ());
settings.setValue (settings_names::resultShowType, buttonGroup_->checkedId ());
settings.endGroup (); settings.endGroup ();
@ -82,6 +89,17 @@ void SettingsEditor::loadSettings()
QString captureHotkey = settings.value (settings_names::captureHotkey, QString captureHotkey = settings.value (settings_names::captureHotkey,
settings_values::captureHotkey).toString (); settings_values::captureHotkey).toString ();
ui->captureEdit->setText (captureHotkey); ui->captureEdit->setText (captureHotkey);
QString repeatHotkey = settings.value (settings_names::repeatHotkey,
settings_values::repeatHotkey).toString ();
ui->repeatEdit->setText (repeatHotkey);
QString clipboardHotkey = settings.value (settings_names::clipboardHotkey,
settings_values::clipboardHotkey).toString ();
ui->clipboardEdit->setText (clipboardHotkey);
int resultShowType = settings.value (settings_names::resultShowType,
settings_values::resultShowType).toInt ();
QAbstractButton* button = buttonGroup_->button (resultShowType);
Q_CHECK_PTR (button);
button->setChecked (true);
settings.endGroup (); settings.endGroup ();

View File

@ -2,6 +2,7 @@
#define SETTINGSEDITOR_H #define SETTINGSEDITOR_H
#include <QDialog> #include <QDialog>
#include <QButtonGroup>
namespace Ui { namespace Ui {
class SettingsEditor; class SettingsEditor;
@ -34,6 +35,7 @@ class SettingsEditor : public QDialog
private: private:
Ui::SettingsEditor *ui; Ui::SettingsEditor *ui;
QButtonGroup* buttonGroup_;
}; };
#endif // SETTINGSEDITOR_H #endif // SETTINGSEDITOR_H

View File

@ -6,14 +6,14 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>456</width> <width>435</width>
<height>166</height> <height>221</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Настройки</string> <string>Настройки</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout_4"> <layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0"> <item row="0" column="0">
<widget class="QGroupBox" name="groupBox"> <widget class="QGroupBox" name="groupBox">
<property name="title"> <property name="title">
@ -26,20 +26,52 @@
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Сочетание клавиш для перехода в режим захвата.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Сочетание клавиш для перехода в режим захвата.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property> </property>
<property name="text"> <property name="text">
<string>Захват</string> <string>Захватить</string>
</property> </property>
<property name="buddy"> <property name="buddy">
<cstring>captureEdit</cstring> <cstring>captureEdit</cstring>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="0" column="1" colspan="2">
<widget class="QLineEdit" name="captureEdit"/> <widget class="QLineEdit" name="captureEdit"/>
</item> </item>
<item row="2" column="0">
<widget class="QLabel" name="label_7">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Сочетание клавиш для перехода в режим захвата.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Скопировать</string>
</property>
<property name="buddy">
<cstring>captureEdit</cstring>
</property>
</widget>
</item>
<item row="2" column="1" colspan="2">
<widget class="QLineEdit" name="clipboardEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Сочетание клавиш для перехода в режим захвата.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Повторить</string>
</property>
<property name="buddy">
<cstring>captureEdit</cstring>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QLineEdit" name="repeatEdit"/>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>
<item row="0" column="1" rowspan="2"> <item row="0" column="1">
<widget class="QGroupBox" name="groupBox_2"> <widget class="QGroupBox" name="groupBox_2">
<property name="title"> <property name="title">
<string>Распознавание</string> <string>Распознавание</string>
@ -103,7 +135,46 @@
</layout> </layout>
</widget> </widget>
</item> </item>
<item row="0" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0"> <item row="1" column="0">
<widget class="QGroupBox" name="resultGroup">
<property name="title">
<string>Вывод результата</string>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="1">
<widget class="QRadioButton" name="trayRadio">
<property name="text">
<string>Трей</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QRadioButton" name="dialogRadio">
<property name="text">
<string>Окно</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="1">
<widget class="QGroupBox" name="groupBox_3"> <widget class="QGroupBox" name="groupBox_3">
<property name="title"> <property name="title">
<string>Перевод</string> <string>Перевод</string>
@ -128,33 +199,20 @@
</layout> </layout>
</widget> </widget>
</item> </item>
<item row="1" column="2"> <item row="2" column="0">
<spacer name="horizontalSpacer"> <spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>24</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
</property> </property>
<property name="sizeHint" stdset="0"> <property name="sizeHint" stdset="0">
<size> <size>
<width>20</width> <width>20</width>
<height>37</height> <height>1</height>
</size> </size>
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="3" column="0" colspan="3"> <item row="3" column="1">
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>

View File

@ -38,9 +38,6 @@ void Translator::applySettings()
void Translator::translate(ProcessingItem item) void Translator::translate(ProcessingItem item)
{ {
Q_ASSERT (!item.recognized.isEmpty ()); Q_ASSERT (!item.recognized.isEmpty ());
item.translated = "проверка";
emit translated (item);
return;
if (translationLanguage_.isEmpty ()) if (translationLanguage_.isEmpty ())
{ {
emit error (tr ("Неверные парметры для перевода.")); emit error (tr ("Неверные парметры для перевода."));