ScreenTranslator/SelectionDialog.cpp

114 lines
3.8 KiB
C++
Raw Normal View History

2013-11-23 13:48:34 +07:00
#include "SelectionDialog.h"
#include "ui_SelectionDialog.h"
#include "LanguageHelper.h"
2015-06-30 00:26:33 +07:00
#include "StAssert.h"
2013-11-23 13:48:34 +07:00
#include <QMouseEvent>
#include <QPainter>
#include <QDebug>
#include <QMenu>
2013-11-23 13:48:34 +07:00
2015-09-23 01:41:08 +07:00
SelectionDialog::SelectionDialog (const LanguageHelper &dictionary, QWidget *parent) :
QDialog (parent),
ui (new Ui::SelectionDialog), dictionary_ (dictionary),
languageMenu_ (new QMenu) {
ui->setupUi (this);
2013-11-23 13:48:34 +07:00
setWindowFlags (Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint |
Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
2013-11-23 13:48:34 +07:00
2015-09-23 01:41:08 +07:00
ui->label->setAutoFillBackground (false);
2013-11-23 13:48:34 +07:00
ui->label->installEventFilter (this);
applySettings ();
2013-11-23 13:48:34 +07:00
}
2015-09-23 01:41:08 +07:00
SelectionDialog::~SelectionDialog () {
2013-11-23 13:48:34 +07:00
delete ui;
}
void SelectionDialog::applySettings () {
dictionary_.updateMenu (languageMenu_, dictionary_.availableOcrLanguagesUi ());
}
2015-09-23 01:41:08 +07:00
bool SelectionDialog::eventFilter (QObject *object, QEvent *event) {
if (object != ui->label) {
return QDialog::eventFilter (object, event);
}
2015-09-23 01:41:08 +07:00
if (event->type () == QEvent::Show) {
startSelectPos_ = currentSelectPos_ = QPoint ();
}
2015-09-23 01:41:08 +07:00
else if (event->type () == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = static_cast <QMouseEvent *> (event);
if ((mouseEvent->button () == Qt::LeftButton ||
2015-09-23 01:41:08 +07:00
mouseEvent->button () == Qt::RightButton) && startSelectPos_.isNull ()) {
startSelectPos_ = mouseEvent->pos ();
2013-11-23 13:48:34 +07:00
}
}
2015-09-23 01:41:08 +07:00
else if (event->type () == QEvent::MouseMove) {
QMouseEvent *mouseEvent = static_cast <QMouseEvent *> (event);
if ((mouseEvent->buttons () & Qt::LeftButton ||
2015-09-23 01:41:08 +07:00
mouseEvent->buttons () & Qt::RightButton) && !startSelectPos_.isNull ()) {
2013-11-23 13:48:34 +07:00
currentSelectPos_ = mouseEvent->pos ();
ui->label->repaint ();
}
}
2015-09-23 01:41:08 +07:00
else if (event->type () == QEvent::Paint) {
2013-11-23 13:48:34 +07:00
QRect selection = QRect (startSelectPos_, currentSelectPos_).normalized ();
2015-09-23 01:41:08 +07:00
if (selection.isValid ()) {
2013-11-24 19:43:37 +07:00
QPainter painter (ui->label);
painter.setPen (Qt::red);
painter.drawRect (selection);
}
2013-11-23 13:48:34 +07:00
}
2015-09-23 01:41:08 +07:00
else if (event->type () == QEvent::MouseButtonRelease) {
QMouseEvent *mouseEvent = static_cast <QMouseEvent *> (event);
if (mouseEvent->button () == Qt::LeftButton ||
2015-09-23 01:41:08 +07:00
mouseEvent->button () == Qt::RightButton) {
if (startSelectPos_.isNull () || currentPixmap_.isNull ()) {
return QDialog::eventFilter (object, event);
}
QPoint endPos = mouseEvent->pos ();
QRect selection = QRect (startSelectPos_, endPos).normalized ();
startSelectPos_ = currentSelectPos_ = QPoint ();
QPixmap selectedPixmap = currentPixmap_.copy (selection);
2015-09-30 23:48:21 +07:00
if (selectedPixmap.width () < 3 || selectedPixmap.height () < 3) {
reject ();
return QDialog::eventFilter (object, event);
}
ProcessingItem item;
item.source = selectedPixmap;
item.screenPos = pos () + selection.topLeft ();
item.modifiers = mouseEvent->modifiers ();
2013-11-23 13:48:34 +07:00
2015-09-30 23:48:21 +07:00
if (mouseEvent->button () == Qt::RightButton &&
!languageMenu_->children ().isEmpty ()) {
QAction *action = languageMenu_->exec (QCursor::pos ());
if (action == NULL) {
reject ();
return QDialog::eventFilter (object, event);
}
2015-09-30 23:48:21 +07:00
item.ocrLanguage = dictionary_.ocrUiToCode (action->text ());
ST_ASSERT (!item.ocrLanguage.isEmpty ());
item.sourceLanguage = dictionary_.translateForOcrCode (item.ocrLanguage);
ST_ASSERT (!item.sourceLanguage.isEmpty ());
}
2015-09-30 23:48:21 +07:00
emit selected (item);
}
}
2013-11-23 13:48:34 +07:00
return QDialog::eventFilter (object, event);
}
void SelectionDialog::setPixmap (QPixmap pixmap, const QRect &showGeometry) {
2015-06-30 00:26:33 +07:00
ST_ASSERT (!pixmap.isNull ());
ST_ASSERT (!showGeometry.isEmpty ());
2013-11-23 13:48:34 +07:00
currentPixmap_ = pixmap;
QPalette palette = this->palette ();
palette.setBrush (this->backgroundRole (), pixmap);
this->setPalette (palette);
this->setGeometry (showGeometry);
2013-11-23 13:48:34 +07:00
show ();
activateWindow ();
2013-11-23 13:48:34 +07:00
}