ScreenTranslator/ResultDialog.cpp

100 lines
3.2 KiB
C++
Raw Normal View History

2013-11-26 13:44:00 +07:00
#include "ResultDialog.h"
#include "ui_ResultDialog.h"
2015-06-30 00:26:33 +07:00
#include "StAssert.h"
#include "LanguageHelper.h"
2013-11-26 13:44:00 +07:00
#include <QDesktopWidget>
#include <QMouseEvent>
#include <QMenu>
2013-11-26 13:44:00 +07:00
ResultDialog::ResultDialog (const LanguageHelper &dictionary, QWidget *parent) :
2015-09-23 01:41:08 +07:00
QDialog (parent),
ui (new Ui::ResultDialog),
2015-10-01 01:28:00 +07:00
dictionary_ (dictionary),
contextMenu_ (NULL), recognizeSubMenu_ (NULL), translateSubMenu_ (NULL),
clipboardAction_ (NULL) {
2015-09-23 01:41:08 +07:00
ui->setupUi (this);
2013-11-26 13:44:00 +07:00
setWindowFlags (Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint |
Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
2013-11-26 13:44:00 +07:00
installEventFilter (this);
createContextMenu ();
applySettings ();
2013-11-26 13:44:00 +07:00
}
2015-09-23 01:41:08 +07:00
ResultDialog::~ResultDialog () {
delete contextMenu_;
2013-11-26 13:44:00 +07:00
delete ui;
}
const ProcessingItem &ResultDialog::item () const {
return item_;
}
void ResultDialog::applySettings () {
dictionary_.updateMenu (recognizeSubMenu_, dictionary_.availableOcrLanguagesUi ());
dictionary_.updateMenu (translateSubMenu_, dictionary_.translateLanguagesUi ());
}
void ResultDialog::createContextMenu () {
contextMenu_ = new QMenu ();
recognizeSubMenu_ = contextMenu_->addMenu (tr ("Распознать другой язык"));
translateSubMenu_ = contextMenu_->addMenu (tr ("Перевести на другой язык"));
clipboardAction_ = contextMenu_->addAction (tr ("Скопировать в буфер"));
}
2015-09-23 01:41:08 +07:00
bool ResultDialog::eventFilter (QObject *object, QEvent *event) {
2013-11-26 13:44:00 +07:00
Q_UNUSED (object);
if (event->type () == QEvent::MouseButtonPress) {
Qt::MouseButton button = static_cast<QMouseEvent *>(event)->button ();
if (button == Qt::RightButton) {
QAction *action = contextMenu_->exec (QCursor::pos ());
2015-10-01 01:24:39 +07:00
if (recognizeSubMenu_->findChildren<QAction *> ().contains (action)) {
ProcessingItem item = item_;
item.translated = item.recognized = QString ();
item.ocrLanguage = dictionary_.ocrUiToCode (action->text ());
emit requestRecognize (item);
}
else if (translateSubMenu_->findChildren<QAction *> ().contains (action)) {
ProcessingItem item = item_;
item.translated.clear ();
item.translateLanguage = dictionary_.translateUiToCode (action->text ());
emit requestTranslate (item);
}
else if (action == clipboardAction_) {
emit requestClipboard ();
}
}
hide ();
}
else if (event->type () == QEvent::WindowDeactivate) {
2013-11-26 13:44:00 +07:00
hide ();
}
return QDialog::eventFilter (object, event);
}
2015-09-23 01:41:08 +07:00
void ResultDialog::showResult (ProcessingItem item) {
2015-09-29 03:24:08 +07:00
ST_ASSERT (item.isValid ());
item_ = item;
2013-11-26 13:44:00 +07:00
ui->sourceLabel->setPixmap (item.source);
ui->recognizeLabel->setText (item.recognized);
ui->translateLabel->setText (item.translated);
bool gotTranslation = !item.translated.isEmpty ();
ui->translateLabel->setVisible (gotTranslation);
ui->translateLine->setVisible (gotTranslation);
2013-11-26 13:44:00 +07:00
2013-11-28 02:15:21 +07:00
show ();
2013-11-26 13:44:00 +07:00
adjustSize ();
2015-09-23 01:41:08 +07:00
QDesktopWidget *desktop = QApplication::desktop ();
Q_CHECK_PTR (desktop);
2015-10-01 01:28:00 +07:00
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);
2013-11-26 13:44:00 +07:00
}
activateWindow ();
2013-11-26 13:44:00 +07:00
}