aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/screenrecorder/screenrecorderplugin.cpp
blob: a816a276bcfcd93234da24e2e0a60dad253060f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "screenrecorderconstants.h"

#include "cropandtrim.h"
#include "export.h"
#include "record.h"
#include "screenrecorderconstants.h"
#include "screenrecordersettings.h"
#include "screenrecordertr.h"

#ifdef WITH_TESTS
#include "screenrecorder_test.h"
#endif // WITH_TESTS

#include <extensionsystem/iplugin.h>

#include <utils/layoutbuilder.h>
#include <utils/styledbar.h>
#include <utils/stylehelper.h>
#include <utils/temporaryfile.h>
#include <utils/utilsicons.h>

#include <solutions/spinner/spinner.h>

#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/icore.h>

#include <QDialog>

using namespace Utils;
using namespace Core;

namespace ScreenRecorder::Internal {

class ScreenRecorderDialog : public QDialog
{
public:
    ScreenRecorderDialog(QWidget *parent = nullptr)
        : QDialog(parent)
        , m_recordFile("XXXXXX" + RecordWidget::recordFileExtension())
    {
        setWindowTitle(Tr::tr("Record Screen"));
        StyleHelper::setPanelWidget(this);

        m_recordFile.open();
        m_recordWidget = new RecordWidget(FilePath::fromString(m_recordFile.fileName()));

        m_cropAndTrimStatusWidget = new CropAndTrimWidget;

        m_exportWidget = new ExportWidget;

        using namespace Layouting;
        Column {
            m_recordWidget,
            Row { m_cropAndTrimStatusWidget, m_exportWidget },
            noMargin(), spacing(0),
        }.attachTo(this);

        auto setLowerRowEndabled = [this] (bool enabled) {
            m_cropAndTrimStatusWidget->setEnabled(enabled);
            m_exportWidget->setEnabled(enabled);
        };
        setLowerRowEndabled(false);
        connect(m_recordWidget, &RecordWidget::started,
                this, [setLowerRowEndabled] { setLowerRowEndabled(false); });
        connect(m_recordWidget, &RecordWidget::finished,
                this, [this, setLowerRowEndabled] (const ClipInfo &clip) {
            m_cropAndTrimStatusWidget->setClip(clip);
            m_exportWidget->setClip(clip);
            setLowerRowEndabled(true);
        });
        connect(m_cropAndTrimStatusWidget, &CropAndTrimWidget::cropRectChanged,
                m_exportWidget, &ExportWidget::setCropRect);
        connect(m_cropAndTrimStatusWidget, &CropAndTrimWidget::trimRangeChanged,
                m_exportWidget, &ExportWidget::setTrimRange);
        connect(m_exportWidget, &ExportWidget::started, this, [this] {
            setEnabled(false);
            m_spinner->show();
        });
        connect(m_exportWidget, &ExportWidget::finished, this, [this] {
            setEnabled(true);
            m_spinner->hide();
        });

        m_spinner = new SpinnerSolution::Spinner(SpinnerSolution::SpinnerSize::Medium, this);
        m_spinner->hide();

        layout()->setSizeConstraint(QLayout::SetFixedSize);
    }

    static void showDialog()
    {
        static QPointer<QDialog> staticInstance;

        if (staticInstance.isNull()) {
            staticInstance = new ScreenRecorderDialog(Core::ICore::dialogParent());
            staticInstance->setAttribute(Qt::WA_DeleteOnClose);
        }
        staticInstance->show();
        staticInstance->raise();
        staticInstance->activateWindow();
    }

private:
    RecordWidget *m_recordWidget;
    TemporaryFile m_recordFile;
    CropAndTrimWidget *m_cropAndTrimStatusWidget;
    ExportWidget *m_exportWidget;
    SpinnerSolution::Spinner *m_spinner;
};

class ScreenRecorderPlugin final : public ExtensionSystem::IPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "ScreenRecorder.json")

public:
    void initialize() final
    {
        ActionBuilder(this, Constants::ACTION_ID)
            .setText(Tr::tr("Record Screen..."))
            .setIcon(Icon({{":/utils/images/filledcircle.png", Theme::IconsStopColor}},
                          Icon::MenuTintedStyle).icon())
            .addToContainer(Core::Constants::M_TOOLS)
            .addOnTriggered(this, &ScreenRecorderPlugin::showDialogOrSettings);

#ifdef WITH_TESTS
        addTest<FFmpegOutputParserTest>();
#endif
    }

private:
    void showDialogOrSettings()
    {
        if (!Internal::settings().toolsRegistered()) {
            // Show options if ffmpeg/ffprobe are neither autodetected nor manually set
            Core::ICore::showOptionsDialog(Constants::TOOLSSETTINGSPAGE_ID);
            if (!Internal::settings().toolsRegistered())
                return; // User did not set ffmpeg/ffprobe
        }

        ScreenRecorderDialog::showDialog();
    }
};

} // namespace ScreenRecorder::Internal

#include "screenrecorderplugin.moc"