ScreenTranslator/ResultDialog.cpp

93 lines
2.8 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),
dictionary_ (dictionary), isShowAtCapturePos_ (true),
contextMenu_ (NULL), recognizeSubMenu_ (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;
}
void ResultDialog::applySettings () {
dictionary_.updateMenu (recognizeSubMenu_, dictionary_.availableOcrLanguagesUi ());
}
void ResultDialog::createContextMenu () {
contextMenu_ = new QMenu ();
recognizeSubMenu_ = contextMenu_->addMenu (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 ());
QWidget *subMenu = action->parentWidget ();
if (recognizeSubMenu_->isAncestorOf (subMenu)) {
ProcessingItem item = lastItem_;
item.translated = item.recognized = QString ();
item.ocrLanguage = dictionary_.ocrUiToCode (action->text ());
emit requestRecognize (item);
}
}
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 ());
lastItem_ = 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-09-23 01:41:08 +07:00
if (isShowAtCapturePos_) {
2013-11-26 13:44: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 ();
2015-09-23 01:41:08 +07:00
if (y () > minY) {
move (x (), minY);
}
2013-11-26 13:44:00 +07:00
}
2015-09-23 01:41:08 +07:00
else {
2013-11-26 13:44:00 +07:00
QRect screenRect = desktop->availableGeometry (this);
2015-06-30 00:26:33 +07:00
ST_ASSERT (screenRect.isValid ());
2013-11-26 13:44:00 +07:00
QPoint newPos (screenRect.width () - width (), screenRect.height () - height ());
move (newPos);
}
activateWindow ();
2013-11-26 13:44:00 +07:00
}