Place editor respecting available space

This commit is contained in:
Gres 2020-03-25 20:32:02 +03:00
parent e1c2defd71
commit 4b5fb132f7
4 changed files with 70 additions and 1 deletions

View File

@ -44,6 +44,7 @@ HEADERS += \
src/represent/resultwidget.h \
src/service/apptranslator.h \
src/service/debug.h \
src/service/geometryutils.h \
src/service/globalaction.h \
src/service/runatsystemstart.h \
src/service/singleapplication.h \
@ -75,6 +76,7 @@ SOURCES += \
src/represent/resultwidget.cpp \
src/service/apptranslator.cpp \
src/service/debug.cpp \
src/service/geometryutils.cpp \
src/service/globalaction.cpp \
src/service/runatsystemstart.cpp \
src/service/singleapplication.cpp \

View File

@ -3,6 +3,7 @@
#include "captureareaeditor.h"
#include "capturer.h"
#include "debug.h"
#include "geometryutils.h"
#include "languagecodes.h"
#include "settings.h"
@ -219,8 +220,10 @@ void CaptureAreaSelector::customize(const CaptureArea &area)
{
SOFT_ASSERT(editor_, return );
editor_->set(area);
editor_->move(QCursor::pos());
editor_->show();
const auto topLeft = service::geometry::cornerAtPoint(
area.rect().center(), editor_->size(), geometry());
editor_->move(topLeft);
update();
}

View File

@ -0,0 +1,50 @@
#include "geometryutils.h"
#include <QRect>
namespace service
{
namespace geometry
{
QPoint cornerAtPoint(const QPoint &corner, const QSize &size,
const QRect &boundRect)
{
auto clamped = corner;
if (!boundRect.contains(clamped)) {
const auto x = std::clamp(clamped.x(), boundRect.left(), boundRect.right());
const auto y = std::clamp(clamped.y(), boundRect.top(), boundRect.bottom());
clamped = QPoint(x, y);
}
const auto zeroCorner = clamped - boundRect.topLeft();
const auto boundWidth = boundRect.width();
const auto boundHeight = boundRect.height();
QPoint result;
if (boundWidth <= size.width()) { // not fits
result.rx() = 0;
} else if (boundWidth - zeroCorner.x() >= size.width()) { // enough on right
result.rx() = zeroCorner.x();
} else if (zeroCorner.x() >= size.width()) { // enough on left
result.rx() = zeroCorner.x() - size.width() + 1;
} else { // not enough on both sides
result.rx() =
zeroCorner.x() >= boundWidth / 2 ? 0 : boundWidth - size.width() + 1;
}
if (boundHeight <= size.height()) {
result.ry() = 0;
} else if (boundHeight - zeroCorner.y() >= size.height()) {
result.ry() = zeroCorner.y();
} else if (zeroCorner.y() >= size.height()) {
result.ry() = zeroCorner.y() - size.height() + 1;
} else {
result.ry() =
zeroCorner.y() >= boundHeight / 2 ? 0 : boundHeight - size.height() + 1;
}
return result + boundRect.topLeft();
}
} // namespace geometry
} // namespace service

View File

@ -0,0 +1,14 @@
#pragma once
class QRect;
class QPoint;
class QSize;
namespace service
{
namespace geometry
{
QPoint cornerAtPoint(const QPoint& corner, const QSize& size,
const QRect& boundRect);
}
} // namespace service