Skip to content

Commit

Permalink
Fix error message position.
Browse files Browse the repository at this point in the history
  • Loading branch information
karliss committed Oct 11, 2021
1 parent 5543c3c commit 496ec72
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
48 changes: 36 additions & 12 deletions src/widgets/capture/capturewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ void CaptureWidget::drawContent(QPainter& painter)
drawErrorMessage(tr("Configuration error resolved. Launch `flameshot "
"gui` again to apply it."),
&painter);
} else {
m_hadErrorMessage = false;
}
}

Expand Down Expand Up @@ -563,7 +565,6 @@ void CaptureWidget::mousePressEvent(QMouseEvent* e)
}
auto scrollWidgetPos = scrollWidgetPoint(
m_mousePressedPos); // position relative to ScrollArea::widget
auto capturePoint = widgetToCapturePoint(m_mousePressedPos);

// reset object selection if capture area selection is active
if (m_selection->getMouseSide(scrollWidgetPos) != SelectionWidget::CENTER) {
Expand Down Expand Up @@ -631,6 +632,17 @@ void CaptureWidget::mouseDoubleClickEvent(QMouseEvent* event)
}
}

void CaptureWidget::leaveEvent(QEvent *event)
{
m_mouseOutside = true;
redrawErrorMessage();
}

void CaptureWidget::enterEvent(QEvent *event)
{
m_mouseOutside = false;
}

void CaptureWidget::mouseMoveEvent(QMouseEvent* e)
{
m_context.mousePos = widgetToCapturePoint(e->pos());
Expand All @@ -642,6 +654,11 @@ void CaptureWidget::mouseMoveEvent(QMouseEvent* e)
updateViewTransform();
repaint();
}

if (m_hadErrorMessage) {
redrawErrorMessage();
}

if (e->buttons() != Qt::LeftButton) {
updateTool(activeButtonTool());
updateCursor();
Expand Down Expand Up @@ -852,13 +869,17 @@ void CaptureWidget::moveEvent(QMoveEvent* e)
void CaptureWidget::changeEvent(QEvent* e)
{
if (e->type() == QEvent::ActivationChange) {
QPoint bottomRight = rect().bottomRight();
// Update the message in the bottom right corner. A rough estimate is
// used for the update rect
update(QRect(bottomRight - QPoint(1000, 200), bottomRight));
redrawErrorMessage();
}
}

void CaptureWidget::redrawErrorMessage() {
QPoint bottomRight = rect().bottomRight();
// Update the message in the bottom right corner. A rough estimate is
// used for the update rect
update(QRect(bottomRight - QPoint(1000, 200), bottomRight));
}

void CaptureWidget::scrollContentsBy(int dx, int dy)
{
Q_UNUSED(dx);
Expand Down Expand Up @@ -1548,12 +1569,12 @@ QPoint CaptureWidget::widgetToCapturePoint(QPoint point)

QPoint CaptureWidget::scrollWidgetPoint(QPoint p)
{
return p - (viewport()->pos() + widget()->pos());
return widget()->mapFrom(this, p);
}

QPoint CaptureWidget::scrollWidgetPointToThis(QPoint p)
{
return p + (viewport()->pos() + widget()->pos());
return widget()->mapTo(this, p);
}

QRect CaptureWidget::imageSize() const
Expand Down Expand Up @@ -1728,17 +1749,20 @@ QRect CaptureWidget::paddedUpdateRect(const QRect& r) const
void CaptureWidget::drawErrorMessage(const QString& msg, QPainter* painter)
{
auto textRect = painter->fontMetrics().boundingRect(msg);
int w = textRect.width(), h = textRect.height();
textRect = {
size().width() - w - 10, size().height() - h - 5, w + 100, h + 100
};
auto sizeOffset = viewport()->size() - textRect.size();
textRect.moveTo(QPoint{sizeOffset.width(), sizeOffset.height()} // bottom left corner
+ m_viewOffset); // compensate for scrollview movement
textRect.adjust(-10, -5, 0, 0); // padding

QScreen* currentScreen = QGuiAppCurrentScreen().currentScreen();
auto mouseRelativeToContent = widget()->mapFromGlobal(QCursor::pos(currentScreen));

if (!textRect.contains(QCursor::pos(currentScreen))) {
if (!textRect.contains(mouseRelativeToContent) || m_mouseOutside) {
QColor textColor(Qt::white);
painter->setPen(textColor);
painter->drawText(textRect, msg);
}
m_hadErrorMessage = true;
}

void CaptureWidget::drawInactiveRegion(QPainter* painter)
Expand Down
5 changes: 5 additions & 0 deletions src/widgets/capture/capturewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ private slots:
void mouseMoveEvent(QMouseEvent* mouseEvent) override;
void mouseReleaseEvent(QMouseEvent* mouseEvent) override;
void mouseDoubleClickEvent(QMouseEvent* event) override;
void leaveEvent(QEvent *event) override;
void enterEvent(QEvent *event) override;
void keyPressEvent(QKeyEvent* keyEvent) override;
void keyReleaseEvent(QKeyEvent* keyEvent) override;
void wheelEvent(QWheelEvent* wheelEvent) override;
Expand Down Expand Up @@ -145,6 +147,7 @@ private slots:
void drawInactiveRegion(QPainter* painter);
void drawToolsData();
void drawObjectSelection();
void redrawErrorMessage();

void processPixmapWithTool(QPixmap* pixmap, CaptureTool* tool);

Expand Down Expand Up @@ -177,6 +180,8 @@ private slots:
bool m_adjustmentButtonPressed;
bool m_configError;
bool m_configErrorResolved;
bool m_mouseOutside = false;
bool m_hadErrorMessage = false;

UpdateNotificationWidget* m_updateNotificationWidget;
quint64 m_lastMouseWheel;
Expand Down

0 comments on commit 496ec72

Please sign in to comment.