Skip to content

Commit

Permalink
refactor: remove fullscreen arg to CaptureWidget and minor cleanup
Browse files Browse the repository at this point in the history
There is a `bool fullScreen` arg to CaptureWidget that wasn't being used
anywhere. There are a few places in the code which are conditional on that
variable but in fact the widget wouldn't be functional without those pieces of
code. This commit removes the argument to the initializer, the corresponding
attribute from the context attribute and any conditionals around them (the
code in the conditionals stayed).

I also shuffled stuff around a little bit in `initWindowFlags()` so that the
move and resize calls could be common between both paths that use them.
  • Loading branch information
toofar committed Dec 28, 2023
1 parent 0ad59cc commit 9c1155b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 61 deletions.
104 changes: 45 additions & 59 deletions src/widgets/capture/capturewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
// enableSaveWindow

CaptureWidget::CaptureWidget(const CaptureRequest& req,
bool fullScreen,
QWidget* parent)
: QWidget(parent)
, m_toolSizeByKeyboard(0)
Expand Down Expand Up @@ -103,19 +102,15 @@ CaptureWidget::CaptureWidget(const CaptureRequest& req,
m_uiColor = m_config.uiColor();
m_contrastUiColor = m_config.contrastUiColor();
setMouseTracking(true);
initContext(fullScreen, req);
if (fullScreen) {
// Grab Screenshot
if (!initInitialScreenshot(req)) {
AbstractLogger::error() << tr("Unable to capture screen");
this->close();
}

initWindowFlags();
initContext(req);

positionWindow(req);
if (!initInitialScreenshot(req)) {
AbstractLogger::error() << tr("Unable to capture screen");
this->close();
}

initWindowFlags();
positionWindow(req);
initButtons();
initSelection(); // button handler must be initialized before
initShortcuts(); // must be called after initSelection
Expand Down Expand Up @@ -257,6 +252,12 @@ void CaptureWidget::initWindowFlags()
*/
void CaptureWidget::positionWindow(const CaptureRequest& req)
{
#if defined(Q_OS_MACOS)
QScreen* currentScreen = QGuiAppCurrentScreen().currentScreen();
move(currentScreen->geometry().x(), currentScreen->geometry().y());
resize(currentScreen->size());
#else // Q_OS_WIN || Q_OS_LINUX || Q_OS_UNIX

// Top left of the whole set of screens
QPoint topLeft(0, 0);
QScreen* selectedScreen = req.initialCaptureScreen();
Expand All @@ -277,13 +278,7 @@ void CaptureWidget::positionWindow(const CaptureRequest& req)
topLeft.setY(topLeftScreen.y());
}
}
move(topLeft);
resize(pixmap().size());
#elif defined(Q_OS_MACOS)
QScreen* currentScreen = QGuiAppCurrentScreen().currentScreen();
move(currentScreen->geometry().x(), currentScreen->geometry().y());
resize(currentScreen->size());
#else
#endif // defined(Q_OS_WIN)
move(topLeft);
resize(pixmap().size());
#endif
Expand All @@ -292,35 +287,31 @@ void CaptureWidget::positionWindow(const CaptureRequest& req)
QVector<QRect> CaptureWidget::initButtonAreas()
{
QVector<QRect> areas;
if (m_context.fullscreen) {
QPoint topLeftOffset = QPoint(0, 0);
QPoint topLeftOffset = QPoint(0, 0);
#if defined(Q_OS_WIN)
topLeftOffset = geometry().topLeft();
topLeftOffset = geometry().topLeft();
#endif

#if defined(Q_OS_MACOS)
// MacOS works just with one active display, so we need to append
// just one current display and keep multiple displays logic for
// other OS
QRect r;
QScreen* screen = QGuiAppCurrentScreen().currentScreen();
r = screen->geometry();
// all calculations are processed according to (0, 0) start
// point so we need to move current object to (0, 0)
r.moveTo(0, 0);
areas.append(r);
// MacOS works just with one active display, so we need to append
// just one current display and keep multiple displays logic for
// other OS
QRect r;
QScreen* screen = QGuiAppCurrentScreen().currentScreen();
r = screen->geometry();
// all calculations are processed according to (0, 0) start
// point so we need to move current object to (0, 0)
r.moveTo(0, 0);
areas.append(r);
#else
for (QScreen* const screen : QGuiApplication::screens()) {
QRect r = screen->geometry();
r.moveTo(r.x() / screen->devicePixelRatio(),
r.y() / screen->devicePixelRatio());
r.moveTo(r.topLeft() - topLeftOffset);
areas.append(r);
}
#endif
} else {
areas.append(rect());
for (QScreen* const screen : QGuiApplication::screens()) {
QRect r = screen->geometry();
r.moveTo(r.x() / screen->devicePixelRatio(),
r.y() / screen->devicePixelRatio());
r.moveTo(r.topLeft() - topLeftOffset);
areas.append(r);
}
#endif
return areas;
}

Expand Down Expand Up @@ -1069,10 +1060,8 @@ void CaptureWidget::resizeEvent(QResizeEvent* e)
{
QWidget::resizeEvent(e);
m_context.widgetOffset = mapToGlobal(QPoint(0, 0));
if (!m_context.fullscreen) {
m_panel->setFixedHeight(height());
m_buttonHandler->updateScreenRegions(rect());
}
m_panel->setFixedHeight(height());
m_buttonHandler->updateScreenRegions(rect());
}

void CaptureWidget::moveEvent(QMoveEvent* e)
Expand All @@ -1091,13 +1080,12 @@ void CaptureWidget::changeEvent(QEvent* e)
}
}

void CaptureWidget::initContext(bool fullscreen, const CaptureRequest& req)
void CaptureWidget::initContext(const CaptureRequest& req)
{
m_context.color = m_config.drawColor();
m_context.widgetOffset = mapToGlobal(QPoint(0, 0));
m_context.mousePos = mapFromGlobal(QCursor::pos());
m_context.toolSize = m_config.drawThickness();
m_context.fullscreen = fullscreen;

// initialize m_context.request
m_context.request = req;
Expand All @@ -1106,21 +1094,19 @@ void CaptureWidget::initContext(bool fullscreen, const CaptureRequest& req)
void CaptureWidget::initPanel()
{
QRect panelRect = rect();
if (m_context.fullscreen) {
#if (defined(Q_OS_MACOS) || defined(Q_OS_LINUX))
QScreen* currentScreen = QGuiAppCurrentScreen().currentScreen();
panelRect = currentScreen->geometry();
auto devicePixelRatio = currentScreen->devicePixelRatio();
panelRect.moveTo(static_cast<int>(panelRect.x() / devicePixelRatio),
static_cast<int>(panelRect.y() / devicePixelRatio));
QScreen* currentScreen = QGuiAppCurrentScreen().currentScreen();
panelRect = currentScreen->geometry();
auto devicePixelRatio = currentScreen->devicePixelRatio();
panelRect.moveTo(static_cast<int>(panelRect.x() / devicePixelRatio),
static_cast<int>(panelRect.y() / devicePixelRatio));
#else
panelRect = QGuiApplication::primaryScreen()->geometry();
auto devicePixelRatio =
QGuiApplication::primaryScreen()->devicePixelRatio();
panelRect.moveTo(panelRect.x() / devicePixelRatio,
panelRect.y() / devicePixelRatio);
panelRect = QGuiApplication::primaryScreen()->geometry();
auto devicePixelRatio =
QGuiApplication::primaryScreen()->devicePixelRatio();
panelRect.moveTo(panelRect.x() / devicePixelRatio,
panelRect.y() / devicePixelRatio);
#endif
}

if (ConfigHandler().showSidePanelButton()) {
auto* panelToggleButton =
Expand Down
3 changes: 1 addition & 2 deletions src/widgets/capture/capturewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class CaptureWidget : public QWidget

public:
explicit CaptureWidget(const CaptureRequest& req,
bool fullScreen = true,
QWidget* parent = nullptr);
~CaptureWidget();

Expand Down Expand Up @@ -115,7 +114,7 @@ private slots:
void showColorPicker(const QPoint& pos);
bool startDrawObjectTool(const QPoint& pos);
QPointer<CaptureTool> activeToolObject();
void initContext(bool fullscreen, const CaptureRequest& req);
void initContext(const CaptureRequest& req);
void initPanel();
void initSelection();
void initShortcuts();
Expand Down

0 comments on commit 9c1155b

Please sign in to comment.