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

View File

@ -28,6 +28,8 @@ class Manager : public QObject
void settings ();
void close ();
void about ();
void showLast ();
void copyLastToClipboard ();
void applySettings ();
@ -44,7 +46,10 @@ class Manager : public QObject
SelectionDialog* selection_;
ResultDialog* resultDialog_;
QAction* captureAction_;
QString lastMessage_;
QAction* repeatAction_;
QAction* clipboardAction_;
ProcessingItem lastItem_;
bool useResultDialog_;
};
#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;
QString recognized;
QString translated;
bool isValid () const;
};
Q_DECLARE_METATYPE(ProcessingItem)

View File

@ -1,6 +1,6 @@
#include "Recognizer.h"
//#include <tesseract/baseapi.h>
#include <tesseract/baseapi.h>
#include <QDebug>
#include <QSettings>
@ -35,67 +35,63 @@ 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(ProcessingItem item)
{
Q_ASSERT (!item.source.isNull ());
item.recognized = "test rec";
emit recognized (item);
return;
// if (engine_ == NULL)
// {
// if (!initEngine ())
// {
// 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 = item.source;
if (imageScale_ > 0)
{
scaled = scaled.scaledToHeight (scaled.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 ())
// {
// item.recognized = result;
// 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 (item);
}
else
{
emit error (tr ("Текст не распознан."));
}
delete [] outText;
}

View File

@ -43,15 +43,22 @@ void ResultDialog::showResult(ProcessingItem item)
adjustSize ();
QDesktopWidget* desktop = QApplication::desktop ();
Q_CHECK_PTR (desktop);
if (isShowAtCapturePos_)
{
QPoint correction = QPoint (ui->frame->lineWidth (), ui->frame->lineWidth ());
move (item.screenPos - correction);
QRect screenRect = desktop->screenGeometry (this);
int minY = screenRect.bottom () - height ();
if (y () > minY)
{
move (x (), minY);
}
}
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 ());

View File

@ -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 \
@ -22,7 +22,8 @@ SOURCES += main.cpp\
GlobalActionHelper.cpp \
Recognizer.cpp \
Translator.cpp \
ResultDialog.cpp
ResultDialog.cpp \
ProcessingItem.cpp
HEADERS += \
Manager.h \

View File

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

View File

@ -10,10 +10,14 @@
SettingsEditor::SettingsEditor(QWidget *parent) :
QDialog(parent),
ui(new Ui::SettingsEditor)
ui(new Ui::SettingsEditor),
buttonGroup_ (new QButtonGroup (this))
{
ui->setupUi(this);
buttonGroup_->addButton (ui->trayRadio, 0);
buttonGroup_->addButton (ui->dialogRadio, 1);
connect (ui->tessdataButton, SIGNAL (clicked ()), SLOT (openTessdataDialog ()));
connect (ui->tessdataEdit, SIGNAL (textChanged (const QString&)),
SLOT (initOcrLangCombo ()));
@ -44,6 +48,9 @@ void SettingsEditor::saveSettings() const
QSettings settings;
settings.beginGroup (settings_names::guiGroup);
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 ();
@ -82,6 +89,17 @@ void SettingsEditor::loadSettings()
QString captureHotkey = settings.value (settings_names::captureHotkey,
settings_values::captureHotkey).toString ();
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 ();

View File

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

View File

@ -6,14 +6,14 @@
<rect>
<x>0</x>
<y>0</y>
<width>456</width>
<height>166</height>
<width>435</width>
<height>221</height>
</rect>
</property>
<property name="windowTitle">
<string>Настройки</string>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox">
<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>
</property>
<property name="text">
<string>Захват</string>
<string>Захватить</string>
</property>
<property name="buddy">
<cstring>captureEdit</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<item row="0" column="1" colspan="2">
<widget class="QLineEdit" name="captureEdit"/>
</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>
</widget>
</item>
<item row="0" column="1" rowspan="2">
<item row="0" column="1">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Распознавание</string>
@ -103,7 +135,46 @@
</layout>
</widget>
</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">
<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">
<property name="title">
<string>Перевод</string>
@ -128,33 +199,20 @@
</layout>
</widget>
</item>
<item row="1" column="2">
<spacer name="horizontalSpacer">
<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">
<item row="2" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>37</height>
<height>1</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="0" colspan="3">
<item row="3" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>

View File

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