diff --git a/src/represent/resultwidget.cpp b/src/represent/resultwidget.cpp index 289c749..26055df 100644 --- a/src/represent/resultwidget.cpp +++ b/src/represent/resultwidget.cpp @@ -11,13 +11,15 @@ #include #include #include +#include ResultWidget::ResultWidget(Manager &manager, Representer &representer, const Settings &settings, QWidget *parent) : QFrame(parent) , representer_(representer) , settings_(settings) - , image_(new QLabel(this)) + , imagePlaceholder_(new QWidget(this)) + , image_(new QLabel(imagePlaceholder_)) , recognized_(new QLabel(this)) , separator_(new QLabel(this)) , translated_(new QLabel(this)) @@ -30,8 +32,6 @@ ResultWidget::ResultWidget(Manager &manager, Representer &representer, setFrameShape(QFrame::StyledPanel); setFrameShadow(QFrame::Plain); - image_->setAlignment(Qt::AlignCenter); - recognized_->setAlignment(Qt::AlignCenter); recognized_->setWordWrap(true); @@ -65,7 +65,7 @@ ResultWidget::ResultWidget(Manager &manager, Representer &representer, installEventFilter(this); auto layout = new QVBoxLayout(this); - layout->addWidget(image_); + layout->addWidget(imagePlaceholder_); layout->addWidget(recognized_); layout->addWidget(separator_); layout->addWidget(translated_); @@ -86,6 +86,8 @@ void ResultWidget::show(const TaskPtr &task) task_ = task; image_->setPixmap(task->captured); + image_->resize(task->captured.size()); + imagePlaceholder_->setMinimumSize(image_->size()); recognized_->setText(task->corrected); const auto tooltip = task->recognized == task->corrected @@ -106,20 +108,37 @@ void ResultWidget::show(const TaskPtr &task) show(); adjustSize(); - if (!image_->isVisible()) + // window should not be smaller than selected image + if (!imagePlaceholder_->isVisible()) resize(std::max(width(), task->captured.width()), std::max(height(), task->captured.height())); - QDesktopWidget *desktop = QApplication::desktop(); - Q_CHECK_PTR(desktop); - const auto correction = - QPoint((width() - task->captured.width()) / 2, lineWidth()); - auto rect = QRect(task->capturePoint - correction, size()); + // if window is wider than image then image should be at horizontal center + const auto correctionToCenterImage = + QPoint((width() - task->captured.width()) / 2, 2 * lineWidth()); + auto rect = QRect(task->capturePoint - correctionToCenterImage, 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(); if (shouldTextOnTop) - rect.moveBottom(rect.top() + task->captured.height() + lineWidth()); + rect.moveBottom(rect.top() + task->captured.height() + 3 * lineWidth()); auto layout = static_cast(this->layout()); SOFT_ASSERT(layout, return ); @@ -156,7 +175,7 @@ void ResultWidget::updateSettings() palette.setColor(QPalette::Window, separatorColor); separator_->setPalette(palette); - image_->setVisible(settings_.showCaptured); + imagePlaceholder_->setVisible(settings_.showCaptured); } void ResultWidget::mousePressEvent(QMouseEvent *event) diff --git a/src/represent/resultwidget.h b/src/represent/resultwidget.h index 0e38e9d..e8f31ec 100644 --- a/src/represent/resultwidget.h +++ b/src/represent/resultwidget.h @@ -31,6 +31,7 @@ private: Representer& representer_; const Settings& settings_; TaskPtr task_; + QWidget* imagePlaceholder_; QLabel* image_; QLabel* recognized_; QLabel* separator_;