Improve result window placement on screen borders

See #74
This commit is contained in:
Gres 2022-05-04 19:36:38 +03:00
parent 030c99d0fe
commit 1f6fff0050
2 changed files with 33 additions and 13 deletions

View File

@ -11,13 +11,15 @@
#include <QLabel> #include <QLabel>
#include <QMenu> #include <QMenu>
#include <QMouseEvent> #include <QMouseEvent>
#include <QScreen>
ResultWidget::ResultWidget(Manager &manager, Representer &representer, ResultWidget::ResultWidget(Manager &manager, Representer &representer,
const Settings &settings, QWidget *parent) const Settings &settings, QWidget *parent)
: QFrame(parent) : QFrame(parent)
, representer_(representer) , representer_(representer)
, settings_(settings) , settings_(settings)
, image_(new QLabel(this)) , imagePlaceholder_(new QWidget(this))
, image_(new QLabel(imagePlaceholder_))
, recognized_(new QLabel(this)) , recognized_(new QLabel(this))
, separator_(new QLabel(this)) , separator_(new QLabel(this))
, translated_(new QLabel(this)) , translated_(new QLabel(this))
@ -30,8 +32,6 @@ ResultWidget::ResultWidget(Manager &manager, Representer &representer,
setFrameShape(QFrame::StyledPanel); setFrameShape(QFrame::StyledPanel);
setFrameShadow(QFrame::Plain); setFrameShadow(QFrame::Plain);
image_->setAlignment(Qt::AlignCenter);
recognized_->setAlignment(Qt::AlignCenter); recognized_->setAlignment(Qt::AlignCenter);
recognized_->setWordWrap(true); recognized_->setWordWrap(true);
@ -65,7 +65,7 @@ ResultWidget::ResultWidget(Manager &manager, Representer &representer,
installEventFilter(this); installEventFilter(this);
auto layout = new QVBoxLayout(this); auto layout = new QVBoxLayout(this);
layout->addWidget(image_); layout->addWidget(imagePlaceholder_);
layout->addWidget(recognized_); layout->addWidget(recognized_);
layout->addWidget(separator_); layout->addWidget(separator_);
layout->addWidget(translated_); layout->addWidget(translated_);
@ -86,6 +86,8 @@ void ResultWidget::show(const TaskPtr &task)
task_ = task; task_ = task;
image_->setPixmap(task->captured); image_->setPixmap(task->captured);
image_->resize(task->captured.size());
imagePlaceholder_->setMinimumSize(image_->size());
recognized_->setText(task->corrected); recognized_->setText(task->corrected);
const auto tooltip = task->recognized == task->corrected const auto tooltip = task->recognized == task->corrected
@ -106,20 +108,37 @@ void ResultWidget::show(const TaskPtr &task)
show(); show();
adjustSize(); adjustSize();
if (!image_->isVisible()) // window should not be smaller than selected image
if (!imagePlaceholder_->isVisible())
resize(std::max(width(), task->captured.width()), resize(std::max(width(), task->captured.width()),
std::max(height(), task->captured.height())); std::max(height(), task->captured.height()));
QDesktopWidget *desktop = QApplication::desktop(); // if window is wider than image then image should be at horizontal center
Q_CHECK_PTR(desktop); const auto correctionToCenterImage =
const auto correction = QPoint((width() - task->captured.width()) / 2, 2 * lineWidth());
QPoint((width() - task->captured.width()) / 2, lineWidth()); auto rect = QRect(task->capturePoint - correctionToCenterImage, size());
auto rect = QRect(task->capturePoint - correction, size());
const auto screenRect = desktop->screenGeometry(this); auto screen = QApplication::screenAt(task->capturePoint);
SOFT_ASSERT(screen, return );
const auto screenRect = screen->geometry();
// window should not exceed horizontal borders
if (rect.right() > screenRect.right())
rect.moveRight(screenRect.right());
if (rect.left() < screenRect.left())
rect.moveLeft(screenRect.left());
// image should be where it was selected
if (imagePlaceholder_->isVisible()) {
const auto imageOffset =
task->capturePoint.x() - rect.left() - 2 * lineWidth();
image_->move(imageOffset, image_->y());
}
// window should not exceed vertical borders
const auto shouldTextOnTop = rect.bottom() > screenRect.bottom(); const auto shouldTextOnTop = rect.bottom() > screenRect.bottom();
if (shouldTextOnTop) if (shouldTextOnTop)
rect.moveBottom(rect.top() + task->captured.height() + lineWidth()); rect.moveBottom(rect.top() + task->captured.height() + 3 * lineWidth());
auto layout = static_cast<QBoxLayout *>(this->layout()); auto layout = static_cast<QBoxLayout *>(this->layout());
SOFT_ASSERT(layout, return ); SOFT_ASSERT(layout, return );
@ -156,7 +175,7 @@ void ResultWidget::updateSettings()
palette.setColor(QPalette::Window, separatorColor); palette.setColor(QPalette::Window, separatorColor);
separator_->setPalette(palette); separator_->setPalette(palette);
image_->setVisible(settings_.showCaptured); imagePlaceholder_->setVisible(settings_.showCaptured);
} }
void ResultWidget::mousePressEvent(QMouseEvent *event) void ResultWidget::mousePressEvent(QMouseEvent *event)

View File

@ -31,6 +31,7 @@ private:
Representer& representer_; Representer& representer_;
const Settings& settings_; const Settings& settings_;
TaskPtr task_; TaskPtr task_;
QWidget* imagePlaceholder_;
QLabel* image_; QLabel* image_;
QLabel* recognized_; QLabel* recognized_;
QLabel* separator_; QLabel* separator_;