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 QEventLoop;
28class ScreenCaptureToolbar;
29class RectangleSelector;
30class WindowPicker;
31
32class ScreenCapture : public QObject
33{
34 Q_OBJECT
35
36public:
37 enum CaptureMode { Fullscreen, Rectangle, Window, Interactive };
38
39 explicit ScreenCapture(const QString &outputFile, CaptureMode mode, QObject *parent = nullptr);
40 ~ScreenCapture();
41
42 void startRecording();
43 void startSnapshot();
44 static bool isWayland();
45
46signals:
47 void finished(bool success);
48 void beginRecording(const QRect &captureRect, bool recordAudio);
49 void onSelectionCanceled();
50 void minimizeShotcut();
51
52private slots:
53 void onCaptureModeSelected(CaptureMode mode, bool minimizeShotcut, bool recordAudio);
54 void onRectangleSelected(const QRect &rect);
55 void onWindowSelected(const QRect &rect);
56 void onImageRectangleSelected(const QRect &rect);
57 void onImageWindowSelected(const QRect &rect);
58 // xdg-desktop-portal response handler
59 void onPortalResponse(uint response, const QVariantMap &results);
60
61private:
62 void startFullscreenRecording();
63 void startRectangleRecording();
64 void startWindowRecording();
65 void startFullscreenSnapshot();
66 void startRectangleSnapshot();
67 void startWindowSnapshot();
68 void captureAndSaveImage(const QRect &rect);
69 void doCaptureAndSaveImage(const QRect &rect);
70 QPixmap captureScreen(const QRect &rect);
71 QRect adjustRectForVideo(const QRect &rect);
72 QRect applyDevicePixelRatio(const QRect &rect);
73 QRect invertDevicePixelRatio(const QRect &rect);
74 bool captureImagePortal(const QRect &rect, const QString &outputPath);
75
76 // Portal call state
77 bool m_portalSuccess = false;
78 QString m_portalUri;
79 QEventLoop *m_portalEventLoop = nullptr;
80
81 QString m_outputFile;
82 CaptureMode m_mode;
83 bool m_isImageMode;
84 bool m_minimizeShotcut;
85 bool m_recordAudio;
86
87 std::unique_ptr<ScreenCaptureToolbar> m_toolbar;
88 std::unique_ptr<RectangleSelector> m_rectangleSelector;
89 std::unique_ptr<WindowPicker> m_windowPicker;
90};
91
92#endif // SCREENCAPTURE_H