2013-11-23 13:48:34 +07:00
|
|
|
#include "SelectionDialog.h"
|
|
|
|
#include "ui_SelectionDialog.h"
|
2014-04-04 21:39:49 +07:00
|
|
|
#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>
|
2014-04-04 21:39:49 +07:00
|
|
|
#include <QMenu>
|
2013-11-23 13:48:34 +07:00
|
|
|
|
2014-04-04 21:39:49 +07:00
|
|
|
SelectionDialog::SelectionDialog(const LanguageHelper &dictionary, QWidget *parent) :
|
2013-11-23 13:48:34 +07:00
|
|
|
QDialog(parent),
|
2014-04-04 21:39:49 +07:00
|
|
|
ui(new Ui::SelectionDialog), dictionary_ (dictionary),
|
|
|
|
languageMenu_ (new QMenu)
|
2013-11-23 13:48:34 +07:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
setWindowFlags (Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint |
|
|
|
|
Qt::WindowStaysOnTopHint);
|
|
|
|
|
|
|
|
ui->label->setAutoFillBackground(false);
|
|
|
|
ui->label->installEventFilter (this);
|
2014-04-04 21:39:49 +07:00
|
|
|
|
|
|
|
updateMenu ();
|
2013-11-23 13:48:34 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
SelectionDialog::~SelectionDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:39:49 +07:00
|
|
|
void SelectionDialog::updateMenu()
|
2013-11-23 13:48:34 +07:00
|
|
|
{
|
2014-04-04 21:39:49 +07:00
|
|
|
Q_CHECK_PTR (languageMenu_);
|
|
|
|
languageMenu_->clear ();
|
|
|
|
QStringList languages = dictionary_.availableOcrLanguagesUi ();
|
|
|
|
if (languages.isEmpty ())
|
2013-11-23 13:48:34 +07:00
|
|
|
{
|
2014-04-04 21:39:49 +07:00
|
|
|
return;
|
2013-11-23 13:48:34 +07:00
|
|
|
}
|
|
|
|
|
2014-04-04 21:39:49 +07:00
|
|
|
const int max = 10;
|
|
|
|
|
|
|
|
if (languages.size () <= max)
|
2013-11-23 13:48:34 +07:00
|
|
|
{
|
2014-04-04 21:39:49 +07:00
|
|
|
foreach (const QString& language, languages)
|
2013-11-23 13:48:34 +07:00
|
|
|
{
|
2014-04-04 21:39:49 +07:00
|
|
|
languageMenu_->addAction (language);
|
2013-11-23 13:48:34 +07:00
|
|
|
}
|
|
|
|
}
|
2014-04-04 21:39:49 +07:00
|
|
|
else
|
2013-11-23 13:48:34 +07:00
|
|
|
{
|
2014-04-04 21:39:49 +07:00
|
|
|
int subIndex = max;
|
|
|
|
QMenu* subMenu = NULL;
|
|
|
|
QString prevLetter;
|
|
|
|
foreach (const QString& language, languages)
|
2013-11-23 13:48:34 +07:00
|
|
|
{
|
2014-04-04 21:39:49 +07:00
|
|
|
QString curLetter = language.left (1);
|
|
|
|
if (++subIndex >= max && prevLetter != curLetter)
|
2013-11-23 13:48:34 +07:00
|
|
|
{
|
2014-04-04 21:39:49 +07:00
|
|
|
if (subMenu != NULL)
|
|
|
|
{
|
|
|
|
subMenu->setTitle (subMenu->title () + " - " + prevLetter);
|
|
|
|
}
|
|
|
|
subMenu = languageMenu_->addMenu (curLetter);
|
|
|
|
subIndex = 0;
|
2013-11-24 19:43:37 +07:00
|
|
|
}
|
2014-04-04 21:39:49 +07:00
|
|
|
prevLetter = curLetter;
|
|
|
|
subMenu->addAction (language);
|
|
|
|
}
|
|
|
|
subMenu->setTitle (subMenu->title () + " - " + prevLetter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SelectionDialog::eventFilter(QObject* object, QEvent* event)
|
|
|
|
{
|
|
|
|
if (object != ui->label)
|
|
|
|
{
|
|
|
|
return QDialog::eventFilter (object, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event->type () == QEvent::Show)
|
|
|
|
{
|
|
|
|
startSelectPos_ = currentSelectPos_ = QPoint ();
|
|
|
|
}
|
|
|
|
else if (event->type () == QEvent::MouseButtonPress)
|
|
|
|
{
|
|
|
|
QMouseEvent* mouseEvent = static_cast <QMouseEvent*> (event);
|
|
|
|
if ((mouseEvent->button () == Qt::LeftButton ||
|
|
|
|
mouseEvent->button () == Qt::RightButton) && startSelectPos_.isNull ())
|
|
|
|
{
|
|
|
|
startSelectPos_ = mouseEvent->pos ();
|
2013-11-23 13:48:34 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (event->type () == QEvent::MouseMove)
|
|
|
|
{
|
|
|
|
QMouseEvent* mouseEvent = static_cast <QMouseEvent*> (event);
|
2014-04-04 21:39:49 +07:00
|
|
|
if ((mouseEvent->buttons () & Qt::LeftButton ||
|
|
|
|
mouseEvent->buttons () & Qt::RightButton) && !startSelectPos_.isNull ())
|
2013-11-23 13:48:34 +07:00
|
|
|
{
|
|
|
|
currentSelectPos_ = mouseEvent->pos ();
|
|
|
|
ui->label->repaint ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (event->type () == QEvent::Paint)
|
|
|
|
{
|
|
|
|
QRect selection = QRect (startSelectPos_, currentSelectPos_).normalized ();
|
2013-11-24 19:43:37 +07:00
|
|
|
if (selection.isValid ())
|
|
|
|
{
|
|
|
|
QPainter painter (ui->label);
|
|
|
|
painter.setPen (Qt::red);
|
|
|
|
painter.drawRect (selection);
|
|
|
|
}
|
2013-11-23 13:48:34 +07:00
|
|
|
}
|
2014-04-04 21:39:49 +07:00
|
|
|
else if (event->type () == QEvent::MouseButtonRelease)
|
|
|
|
{
|
|
|
|
QMouseEvent* mouseEvent = static_cast <QMouseEvent*> (event);
|
|
|
|
if (mouseEvent->button () == Qt::LeftButton ||
|
|
|
|
mouseEvent->button () == Qt::RightButton)
|
|
|
|
{
|
|
|
|
if (startSelectPos_.isNull () || currentPixmap_.isNull ())
|
|
|
|
{
|
|
|
|
return QDialog::eventFilter (object, event);
|
|
|
|
}
|
|
|
|
QPoint endPos = mouseEvent->pos ();
|
|
|
|
QRect selection = QRect (startSelectPos_, endPos).normalized ();
|
|
|
|
QPixmap selectedPixmap = currentPixmap_.copy (selection);
|
|
|
|
if (!selectedPixmap.isNull ())
|
|
|
|
{
|
|
|
|
ProcessingItem item;
|
|
|
|
item.source = selectedPixmap;
|
|
|
|
item.screenPos = selection.topLeft ();
|
2013-11-23 13:48:34 +07:00
|
|
|
|
2014-04-04 21:39:49 +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);
|
|
|
|
}
|
|
|
|
item.ocrLanguage = dictionary_.ocrUiToCode (action->text ());
|
2015-06-30 00:26:33 +07:00
|
|
|
ST_ASSERT (!item.ocrLanguage.isEmpty ());
|
2014-04-04 21:39:49 +07:00
|
|
|
item.sourceLanguage = dictionary_.translateForOcrCode (item.ocrLanguage);
|
2015-06-30 00:26:33 +07:00
|
|
|
ST_ASSERT (!item.sourceLanguage.isEmpty ());
|
2014-04-04 21:39:49 +07:00
|
|
|
}
|
|
|
|
emit selected (item);
|
|
|
|
accept ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-23 13:48:34 +07:00
|
|
|
return QDialog::eventFilter (object, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SelectionDialog::setPixmap(QPixmap pixmap)
|
|
|
|
{
|
2015-06-30 00:26:33 +07:00
|
|
|
ST_ASSERT (!pixmap.isNull ());
|
2013-11-23 13:48:34 +07:00
|
|
|
currentPixmap_ = pixmap;
|
|
|
|
QPalette palette = this->palette ();
|
|
|
|
palette.setBrush (this->backgroundRole (), pixmap);
|
|
|
|
this->setPalette (palette);
|
|
|
|
this->resize (pixmap.size ());
|
|
|
|
|
|
|
|
show ();
|
|
|
|
setFocus ();
|
|
|
|
}
|