summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorArtem Dyomin <artem.dyomin@qt.io>2023-06-23 19:02:27 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-06-26 12:31:17 +0000
commit73482d0add6772d3bcc9b3e68c9d2e68da2131d0 (patch)
tree85dd67f69cc45aee41167b69cb40bc71c8a90cea /examples
parent22bf04c4522ccb050fb31cae843ce7461f413804 (diff)
Apply QWindowCapture to the screen capturing example
The example is pretty useful for testing. Change-Id: I6206e445738e0cdd694c58146d4da5303e2eb6cf Reviewed-by: Jøger Hansegård <joger.hansegard@qt.io> Reviewed-by: Lars Knoll <lars@knoll.priv.no> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> (cherry picked from commit 3edff8e367b9060dd138a2b67cb87d2246a4a3e6) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'examples')
-rw-r--r--examples/multimedia/screencapture/screencapturepreview.cpp125
-rw-r--r--examples/multimedia/screencapture/screencapturepreview.h21
-rw-r--r--examples/multimedia/screencapture/windowlistmodel.cpp14
-rw-r--r--examples/multimedia/screencapture/windowlistmodel.h8
4 files changed, 116 insertions, 52 deletions
diff --git a/examples/multimedia/screencapture/screencapturepreview.cpp b/examples/multimedia/screencapture/screencapturepreview.cpp
index 6d217261a..41ff02212 100644
--- a/examples/multimedia/screencapture/screencapturepreview.cpp
+++ b/examples/multimedia/screencapture/screencapturepreview.cpp
@@ -16,31 +16,28 @@
#include <QMessageBox>
#include <QPushButton>
-#include <QGuiApplication>
-
ScreenCapturePreview::ScreenCapturePreview(QWidget *parent)
: QWidget(parent),
screenListView(new QListView(this)),
windowListView(new QListView(this)),
screenCapture(new QScreenCapture(this)),
- windows(QWindowCapture::capturableWindows()),
+ windowCapture(new QWindowCapture(this)),
mediaCaptureSession(new QMediaCaptureSession(this)),
videoWidget(new QVideoWidget(this)),
gridLayout(new QGridLayout(this)),
- startStopButton(new QPushButton(tr("Stop screencapture"), this)),
- screenLabel(new QLabel(tr("Double-click screen to capture:"), this)),
- windowLabel(new QLabel(tr("Double-click window to capture:"), this)),
- videoWidgetLabel(new QLabel(tr("QScreenCapture output:"), this))
+ startStopButton(new QPushButton(this)),
+ screenLabel(new QLabel(tr("Select screen to capture:"), this)),
+ windowLabel(new QLabel(tr("Select window to capture:"), this)),
+ videoWidgetLabel(new QLabel(tr("Capture output:"), this))
{
// Get lists of screens and windows:
screenListModel = new ScreenListModel(this);
- windowListModel = new WindowListModel(windows, this);
+ windowListModel = new WindowListModel(this);
// Setup QScreenCapture with initial source:
- screenCapture->setScreen(QGuiApplication::primaryScreen());
- screenCapture->start();
mediaCaptureSession->setScreenCapture(screenCapture);
+ mediaCaptureSession->setWindowCapture(windowCapture);
mediaCaptureSession->setVideoOutput(videoWidget);
// Setup UI:
@@ -62,51 +59,111 @@ ScreenCapturePreview::ScreenCapturePreview(QWidget *parent)
gridLayout->setColumnMinimumWidth(1, 400);
gridLayout->setRowMinimumHeight(3, 1);
- connect(screenListView, &QAbstractItemView::activated, this, &ScreenCapturePreview::onScreenSelectionChanged);
- connect(windowListView, &QAbstractItemView::activated, this,
- &ScreenCapturePreview::onWindowSelectionChanged);
+ connect(screenListView->selectionModel(), &QItemSelectionModel::selectionChanged, this,
+ &ScreenCapturePreview::onCurrentScreenSelectionChanged);
+ connect(windowListView->selectionModel(), &QItemSelectionModel::selectionChanged, this,
+ &ScreenCapturePreview::onCurrentWindowSelectionChanged);
connect(startStopButton, &QPushButton::clicked, this,
&ScreenCapturePreview::onStartStopButtonClicked);
- connect(screenCapture, &QScreenCapture::errorOccurred, this, &ScreenCapturePreview::onScreenCaptureErrorOccured);
+ connect(screenCapture, &QScreenCapture::errorOccurred, this,
+ &ScreenCapturePreview::onScreenCaptureErrorOccured, Qt::QueuedConnection);
+ connect(windowCapture, &QWindowCapture::errorOccurred, this,
+ &ScreenCapturePreview::onWindowCaptureErrorOccured, Qt::QueuedConnection);
+
+ updateActive(SourceType::Screen, true);
}
ScreenCapturePreview::~ScreenCapturePreview() = default;
-void ScreenCapturePreview::onScreenSelectionChanged(QModelIndex index)
+void ScreenCapturePreview::onCurrentScreenSelectionChanged(QItemSelection selection)
{
- screenCapture->setScreen(screenListModel->screen(index));
- screenSelected = true;
- updateCapture();
+ if (auto indexes = selection.indexes(); !indexes.empty()) {
+ screenCapture->setScreen(screenListModel->screen(indexes.front()));
+ updateActive(SourceType::Screen, isActive());
+
+ windowListView->clearSelection();
+ } else {
+ screenCapture->setScreen(nullptr);
+ }
}
-void ScreenCapturePreview::onWindowSelectionChanged(QModelIndex index)
+void ScreenCapturePreview::onCurrentWindowSelectionChanged(QItemSelection selection)
+{
+ if (auto indexes = selection.indexes(); !indexes.empty()) {
+ auto window = windowListModel->window(indexes.front());
+ qDebug() << indexes.front() << window.description();
+ if (!window.isValid()) {
+ const auto questionResust = QMessageBox::question(
+ this, tr("Invalid window"),
+ tr("The window is no longer valid. Update the list of windows?"));
+ if (questionResust == QMessageBox::Yes) {
+ updateActive(SourceType::Window, false);
+
+ windowListView->clearSelection();
+ windowListModel->populate();
+ return;
+ }
+ }
+
+ windowCapture->setWindow(window);
+ updateActive(SourceType::Window, isActive());
+
+ screenListView->clearSelection();
+ } else {
+ windowCapture->setWindow({});
+ }
+}
+
+void ScreenCapturePreview::onWindowCaptureErrorOccured(QWindowCapture::Error,
+ const QString &errorString)
{
- windowCapture->setWindow(windowListModel->window(index));
- screenSelected = false;
- updateCapture();
+ QMessageBox::warning(this, tr("QWindowCapture: Error occurred"), errorString);
}
-void ScreenCapturePreview::onScreenCaptureErrorOccured([[maybe_unused]] QScreenCapture::Error error,
+void ScreenCapturePreview::onScreenCaptureErrorOccured(QScreenCapture::Error,
const QString &errorString)
{
- QMessageBox::warning(this, "QScreenCapture: Error occurred", errorString);
+ QMessageBox::warning(this, tr("QScreenCapture: Error occurred"), errorString);
}
void ScreenCapturePreview::onStartStopButtonClicked()
{
- started = !started;
- startStopButton->setText(started ? tr("Stop screencapture") : tr("Start screencapture"));
- updateCapture();
+ updateActive(sourceType, !isActive());
}
-void ScreenCapturePreview::updateCapture()
+void ScreenCapturePreview::updateStartStopButtonText()
{
- if (screenSelected) {
- if (started != screenCapture->isActive())
- screenCapture->setActive(started);
- } else {
- if (started != windowCapture->isActive())
- windowCapture->setActive(started);
+ switch (sourceType) {
+ case SourceType::Window:
+ startStopButton->setText(isActive() ? tr("Stop window capture")
+ : tr("Start window capture"));
+ break;
+ case SourceType::Screen:
+ startStopButton->setText(isActive() ? tr("Stop screen capture")
+ : tr("Start screen capture"));
+ break;
+ }
+}
+
+void ScreenCapturePreview::updateActive(SourceType sourceType, bool active)
+{
+ this->sourceType = sourceType;
+
+ screenCapture->setActive(active && sourceType == SourceType::Screen);
+ windowCapture->setActive(active && sourceType == SourceType::Window);
+
+ updateStartStopButtonText();
+}
+
+bool ScreenCapturePreview::isActive() const
+{
+ switch (sourceType) {
+ case SourceType::Window:
+ return windowCapture->isActive();
+ case SourceType::Screen:
+ return screenCapture->isActive();
+ default:
+ return false;
}
}
diff --git a/examples/multimedia/screencapture/screencapturepreview.h b/examples/multimedia/screencapture/screencapturepreview.h
index ec8bf260a..c03097c02 100644
--- a/examples/multimedia/screencapture/screencapturepreview.h
+++ b/examples/multimedia/screencapture/screencapturepreview.h
@@ -7,7 +7,7 @@
#include <QScreenCapture>
#include <QWindowCapture>
#include <QWidget>
-#include <QModelIndex>
+#include <QItemSelection>
class ScreenListModel;
class WindowListModel;
@@ -31,16 +31,21 @@ class ScreenCapturePreview : public QWidget
public:
explicit ScreenCapturePreview(QWidget *parent = nullptr);
- ~ScreenCapturePreview();
+ ~ScreenCapturePreview() override;
-public slots:
- void onScreenSelectionChanged(QModelIndex index);
- void onWindowSelectionChanged(QModelIndex index);
+private slots:
+ void onCurrentScreenSelectionChanged(QItemSelection index);
+ void onCurrentWindowSelectionChanged(QItemSelection index);
+ void onWindowCaptureErrorOccured(QWindowCapture::Error error, const QString &errorString);
void onScreenCaptureErrorOccured(QScreenCapture::Error error, const QString &errorString);
void onStartStopButtonClicked();
private:
- void updateCapture();
+ enum class SourceType { Screen, Window };
+
+ void updateActive(SourceType sourceType, bool active);
+ void updateStartStopButtonText();
+ bool isActive() const;
private:
ScreenListModel *screenListModel = nullptr;
@@ -49,7 +54,6 @@ private:
QListView *windowListView = nullptr;
QScreenCapture *screenCapture = nullptr;
QWindowCapture *windowCapture = nullptr;
- QList<QCapturableWindow> windows;
QMediaCaptureSession *mediaCaptureSession = nullptr;
QVideoWidget *videoWidget = nullptr;
QGridLayout *gridLayout = nullptr;
@@ -57,8 +61,7 @@ private:
QLabel *screenLabel = nullptr;
QLabel *windowLabel = nullptr;
QLabel *videoWidgetLabel = nullptr;
- bool screenSelected = true;
- bool started = true;
+ SourceType sourceType = SourceType::Screen;
};
#endif // SCREENCAPTUREPREVIEW_H
diff --git a/examples/multimedia/screencapture/windowlistmodel.cpp b/examples/multimedia/screencapture/windowlistmodel.cpp
index 961f495e3..45b8ceabd 100644
--- a/examples/multimedia/screencapture/windowlistmodel.cpp
+++ b/examples/multimedia/screencapture/windowlistmodel.cpp
@@ -2,11 +2,10 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "windowlistmodel.h"
+#include <QWindowCapture>
-#include <QWindow>
-
-WindowListModel::WindowListModel(QList<QCapturableWindow> data, QObject *parent)
- : QAbstractListModel(parent), windowList(data)
+WindowListModel::WindowListModel(QObject *parent)
+ : QAbstractListModel(parent), windowList(QWindowCapture::capturableWindows())
{
}
@@ -33,4 +32,11 @@ QCapturableWindow WindowListModel::window(const QModelIndex &index) const
return windowList.at(index.row());
}
+void WindowListModel::populate()
+{
+ beginResetModel();
+ windowList = QWindowCapture::capturableWindows();
+ endResetModel();
+}
+
#include "moc_windowlistmodel.cpp"
diff --git a/examples/multimedia/screencapture/windowlistmodel.h b/examples/multimedia/screencapture/windowlistmodel.h
index b1608eba4..1a24b01f8 100644
--- a/examples/multimedia/screencapture/windowlistmodel.h
+++ b/examples/multimedia/screencapture/windowlistmodel.h
@@ -7,10 +7,6 @@
#include <QAbstractListModel>
#include <QCapturableWindow>
-QT_BEGIN_NAMESPACE
-class QWindow;
-QT_END_NAMESPACE
-
QT_USE_NAMESPACE
class WindowListModel : public QAbstractListModel
@@ -18,11 +14,13 @@ class WindowListModel : public QAbstractListModel
Q_OBJECT
public:
- explicit WindowListModel(QList<QCapturableWindow> data = {}, QObject *parent = nullptr);
+ explicit WindowListModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
+
QCapturableWindow window(const QModelIndex &index) const;
+ void populate();
private:
QList<QCapturableWindow> windowList;