CuteLogger
Fast and simple logging solution for Qt based applications
screencapture.h
1/*
2 * Copyright (c) 2025 Meltytech, LLC
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef SCREENCAPTURE_H
19#define SCREENCAPTURE_H
20
21#include <memory>
22#include <QObject>
23#include <QRect>
24#include <QScreen>
25#include <QVariant>
26
27class ToolbarWidget;
28class RectangleSelector;
29class WindowPicker;
30
31class ScreenCapture : public QObject
32{
33 Q_OBJECT
34
35public:
36 enum CaptureMode { Fullscreen, Rectangle, Window, Interactive };
37
38 explicit ScreenCapture(const QString &outputFile, CaptureMode mode, QObject *parent = nullptr);
39 ~ScreenCapture();
40
41 void startRecording();
42 void startSnapshot();
43 static bool isWayland();
44
45signals:
46 void finished(bool success);
47 void beginRecording(const QRect &captureRect);
48 void onSelectionCanceled();
49
50private slots:
51 void onCaptureModeSelected(CaptureMode mode);
52 void onRectangleSelected(const QRect &rect);
53 void onWindowSelected(const QRect &rect);
54 void onImageRectangleSelected(const QRect &rect);
55 void onImageWindowSelected(const QRect &rect);
56 // xdg-desktop-portal response handler
57 void onPortalResponse(uint response, const QVariantMap &results);
58
59private:
60 void startFullscreenRecording();
61 void startRectangleRecording();
62 void startWindowRecording();
63 void startFullscreenSnapshot();
64 void startRectangleSnapshot();
65 void startWindowSnapshot();
66 void captureAndSaveImage(const QRect &rect);
67 void doCaptureAndSaveImage(const QRect &rect);
68 QPixmap captureScreen(const QRect &rect);
69 QRect adjustRectForVideo(const QRect &rect);
70 QRect applyDevicePixelRatio(const QRect &rect);
71 QRect invertDevicePixelRatio(const QRect &rect);
72 bool captureImagePortal(const QRect &rect, const QString &outputPath);
73
74 // Portal call state
75 bool m_portalSuccess = false;
76 QString m_portalUri;
77 QEventLoop *m_portalEventLoop = nullptr;
78
79 QString m_outputFile;
80 CaptureMode m_mode;
81 bool m_isImageMode;
82
83 std::unique_ptr<ToolbarWidget> m_toolbar;
84 std::unique_ptr<RectangleSelector> m_rectangleSelector;
85 std::unique_ptr<WindowPicker> m_windowPicker;
86};
87
88#endif // SCREENCAPTURE_H