Skip to content

Commit e57e1f3

Browse files
committed
builder: combine the converter set to converter_runner
Signed-off-by: Jack Lau <[email protected]>
1 parent bdc7f9a commit e57e1f3

File tree

11 files changed

+454
-378
lines changed

11 files changed

+454
-378
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ if(ENABLE_GUI)
240240
list(APPEND GUI_SOURCES
241241
${CMAKE_SOURCE_DIR}/builder/src/base_page.cpp
242242
${CMAKE_SOURCE_DIR}/builder/src/file_selector_widget.cpp
243+
${CMAKE_SOURCE_DIR}/builder/src/converter_runner.cpp
243244
${CMAKE_SOURCE_DIR}/builder/src/placeholder_page.cpp
244245
${CMAKE_SOURCE_DIR}/builder/src/info_view_page.cpp
245246
${CMAKE_SOURCE_DIR}/builder/src/compress_picture_page.cpp
@@ -257,6 +258,7 @@ if(ENABLE_GUI)
257258
list(APPEND GUI_HEADERS
258259
${CMAKE_SOURCE_DIR}/builder/include/base_page.h
259260
${CMAKE_SOURCE_DIR}/builder/include/file_selector_widget.h
261+
${CMAKE_SOURCE_DIR}/builder/include/converter_runner.h
260262
${CMAKE_SOURCE_DIR}/builder/include/placeholder_page.h
261263
${CMAKE_SOURCE_DIR}/builder/include/info_view_page.h
262264
${CMAKE_SOURCE_DIR}/builder/include/compress_picture_page.h
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright 2025 Jack Lau
3+
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef CONVERTER_RUNNER_H
19+
#define CONVERTER_RUNNER_H
20+
21+
#include "../../common/include/process_observer.h"
22+
#include <QLabel>
23+
#include <QObject>
24+
#include <QProgressBar>
25+
#include <QPushButton>
26+
#include <QString>
27+
#include <functional>
28+
29+
class EncodeParameter;
30+
class ProcessParameter;
31+
32+
/**
33+
* @brief Helper class to manage conversion operations with progress tracking
34+
*
35+
* This class encapsulates the common pattern of:
36+
* - Input validation
37+
* - Progress bar management
38+
* - Button state management
39+
* - Thread-based conversion execution
40+
* - Completion handling with success/error messages
41+
*
42+
* Usage:
43+
* @code
44+
* ConverterRunner *runner = new ConverterRunner(
45+
* progressBar, progressLabel, convertButton,
46+
* "Converting...", "Convert",
47+
* "Success", "File converted successfully!",
48+
* "Error", "Failed to convert file.",
49+
* this
50+
* );
51+
*
52+
* connect(runner, &ConverterRunner::ConversionFinished, this, &MyPage::OnConversionFinished);
53+
*
54+
* runner->RunConversion(inputPath, outputPath, encodeParam, processParam);
55+
* @endcode
56+
*/
57+
class ConverterRunner : public QObject, public ProcessObserver {
58+
Q_OBJECT
59+
60+
public:
61+
/**
62+
* @brief Construct a ConverterRunner
63+
* @param progressBar Progress bar widget to show conversion progress
64+
* @param progressLabel Label to show time remaining
65+
* @param actionButton Button to trigger conversion (will be disabled during conversion)
66+
* @param runningButtonText Text to show on button during conversion (e.g., "Converting...")
67+
* @param idleButtonText Text to show on button when idle (e.g., "Convert")
68+
* @param successTitle Title for success message box
69+
* @param successMessage Message for success message box
70+
* @param errorTitle Title for error message box
71+
* @param errorMessage Message for error message box
72+
* @param parent Parent QObject
73+
*/
74+
explicit ConverterRunner(QProgressBar *progressBar,
75+
QLabel *progressLabel,
76+
QPushButton *actionButton,
77+
const QString &runningButtonText,
78+
const QString &idleButtonText,
79+
const QString &successTitle,
80+
const QString &successMessage,
81+
const QString &errorTitle,
82+
const QString &errorMessage,
83+
QObject *parent = nullptr);
84+
85+
~ConverterRunner() override;
86+
87+
/**
88+
* @brief Run conversion in a separate thread
89+
* @param inputPath Input file path
90+
* @param outputPath Output file path
91+
* @param encodeParam Encoding parameters (ownership transferred to runner)
92+
* @param processParam Process parameters (ownership transferred to runner)
93+
* @param transcoderName Transcoder name (default: "FFMPEG")
94+
* @return true if conversion started successfully, false if validation failed
95+
*/
96+
bool RunConversion(const QString &inputPath,
97+
const QString &outputPath,
98+
EncodeParameter *encodeParam,
99+
ProcessParameter *processParam,
100+
const QString &transcoderName = "FFMPEG");
101+
102+
/**
103+
* @brief Set custom validation function
104+
* @param validator Function that returns true if validation passes, false otherwise
105+
*/
106+
void SetValidator(std::function<bool()> validator);
107+
108+
/**
109+
* @brief Set custom completion handler (called before showing message box)
110+
* @param handler Function called with success status
111+
*/
112+
void SetCompletionHandler(std::function<void(bool)> handler);
113+
114+
// ProcessObserver interface
115+
void on_process_update(double progress) override;
116+
void on_time_update(double timeRequired) override;
117+
118+
signals:
119+
/**
120+
* @brief Emitted when conversion finishes
121+
* @param success true if conversion succeeded, false otherwise
122+
*/
123+
void ConversionFinished(bool success);
124+
125+
private:
126+
void ShowProgressUI();
127+
void HideProgressUI();
128+
void SetButtonRunning();
129+
void SetButtonIdle();
130+
void ShowCompletionMessage(bool success);
131+
132+
QProgressBar *progressBar;
133+
QLabel *progressLabel;
134+
QPushButton *actionButton;
135+
QString runningButtonText;
136+
QString idleButtonText;
137+
QString successTitle;
138+
QString successMessage;
139+
QString errorTitle;
140+
QString errorMessage;
141+
142+
std::function<bool()> customValidator;
143+
std::function<void(bool)> customCompletionHandler;
144+
};
145+
146+
#endif // CONVERTER_RUNNER_H

src/builder/include/cut_video_page.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@
1919
#define CUT_VIDEO_PAGE_H
2020

2121
#include "base_page.h"
22+
#include "converter_runner.h"
2223
#include "file_selector_widget.h"
2324
#include "simple_video_player.h"
24-
#include "../../common/include/process_observer.h"
2525
#include <QGroupBox>
2626
#include <QLabel>
2727
#include <QProgressBar>
2828
#include <QPushButton>
2929
#include <QSlider>
30-
#include <QThread>
3130
#include <QTimeEdit>
3231

3332
class EncodeParameter;
3433
class ProcessParameter;
3534

36-
class CutVideoPage : public BasePage, public ProcessObserver {
35+
class CutVideoPage : public BasePage {
3736
Q_OBJECT
3837

3938
public:
@@ -45,10 +44,6 @@ class CutVideoPage : public BasePage, public ProcessObserver {
4544
QString GetPageTitle() const override { return "Cut Video"; }
4645
void RetranslateUi() override;
4746

48-
// ProcessObserver interface
49-
void on_process_update(double progress) override;
50-
void on_time_update(double timeRequired) override;
51-
5247
protected:
5348
void OnInputFileChanged(const QString &newPath) override;
5449
void OnOutputPathUpdate() override;
@@ -79,8 +74,6 @@ private slots:
7974
void UpdateTimeLabels();
8075
void UpdateDurationLabel();
8176
QString FormatTime(qint64 milliseconds);
82-
void RunCutInThread(const QString &inputPath, const QString &outputPath,
83-
EncodeParameter *encodeParam, ProcessParameter *processParam);
8477

8578
// Input/Output section
8679
FileSelectorWidget *inputFileSelector;
@@ -118,6 +111,9 @@ private slots:
118111
// Action section
119112
QPushButton *cutButton;
120113

114+
// Conversion runner
115+
ConverterRunner *converterRunner;
116+
121117
// State
122118
qint64 videoDuration; // in milliseconds
123119
qint64 startTime; // in milliseconds

src/builder/include/extract_audio_page.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,16 @@
1919
#define EXTRACT_AUDIO_PAGE_H
2020

2121
#include "base_page.h"
22+
#include "converter_runner.h"
2223
#include "file_selector_widget.h"
23-
#include "../../common/include/process_observer.h"
2424
#include <QComboBox>
2525
#include <QGroupBox>
2626
#include <QLabel>
2727
#include <QProgressBar>
2828
#include <QPushButton>
2929
#include <QSpinBox>
30-
#include <QThread>
3130

32-
class EncodeParameter;
33-
class ProcessParameter;
34-
35-
class ExtractAudioPage : public BasePage, public ProcessObserver {
31+
class ExtractAudioPage : public BasePage {
3632
Q_OBJECT
3733

3834
public:
@@ -44,10 +40,6 @@ class ExtractAudioPage : public BasePage, public ProcessObserver {
4440
QString GetPageTitle() const override { return "Extract Audio"; }
4541
void RetranslateUi() override;
4642

47-
// ProcessObserver interface
48-
void on_process_update(double progress) override;
49-
void on_time_update(double timeRequired) override;
50-
5143
protected:
5244
void OnOutputPathUpdate() override;
5345

@@ -64,8 +56,6 @@ private slots:
6456
private:
6557
void SetupUI();
6658
void UpdateOutputPath();
67-
void RunExtractInThread(const QString &inputPath, const QString &outputPath,
68-
EncodeParameter *encodeParam, ProcessParameter *processParam);
6959
QString DetectAudioCodecFromFile(const QString &filePath);
7060
QString MapCodecToFormat(const QString &codec);
7161

@@ -86,6 +76,9 @@ private slots:
8676

8777
// Action section
8878
QPushButton *extractButton;
79+
80+
// Conversion runner
81+
ConverterRunner *converterRunner;
8982
};
9083

9184
#endif // EXTRACT_AUDIO_PAGE_H

src/builder/include/remux_page.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@
1919
#define REMUX_PAGE_H
2020

2121
#include "base_page.h"
22+
#include "converter_runner.h"
2223
#include "file_selector_widget.h"
23-
#include "../../common/include/process_observer.h"
2424
#include <QCheckBox>
2525
#include <QComboBox>
2626
#include <QGroupBox>
2727
#include <QLabel>
2828
#include <QProgressBar>
2929
#include <QPushButton>
3030
#include <QScrollArea>
31-
#include <QThread>
3231
#include <QVBoxLayout>
3332
#include <QVector>
3433

@@ -47,7 +46,7 @@ struct StreamInfo {
4746
QCheckBox *checkbox;
4847
};
4948

50-
class RemuxPage : public BasePage, public ProcessObserver {
49+
class RemuxPage : public BasePage {
5150
Q_OBJECT
5251

5352
public:
@@ -59,10 +58,6 @@ class RemuxPage : public BasePage, public ProcessObserver {
5958
QString GetPageTitle() const override { return "Remux"; }
6059
void RetranslateUi() override;
6160

62-
// ProcessObserver interface
63-
void on_process_update(double progress) override;
64-
void on_time_update(double timeRequired) override;
65-
6661
protected:
6762
void OnInputFileChanged(const QString &newPath) override;
6863
void OnOutputPathUpdate() override;
@@ -84,8 +79,6 @@ private slots:
8479
void ClearStreams();
8580
QString GetStreamTypeName(int codecType);
8681
QString FormatBitrate(int64_t bitsPerSec);
87-
void RunRemuxInThread(const QString &inputPath, const QString &outputPath,
88-
EncodeParameter *encodeParam, ProcessParameter *processParam);
8982

9083
// Input/Output section
9184
FileSelectorWidget *inputFileSelector;
@@ -109,6 +102,9 @@ private slots:
109102

110103
// Action section
111104
QPushButton *remuxButton;
105+
106+
// Conversion runner
107+
ConverterRunner *converterRunner;
112108
};
113109

114110
#endif // REMUX_PAGE_H

src/builder/include/transcode_page.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,19 @@
1919
#define TRANSCODE_PAGE_H
2020

2121
#include "base_page.h"
22+
#include "converter_runner.h"
2223
#include "file_selector_widget.h"
23-
#include "../../common/include/process_observer.h"
2424
#include <QComboBox>
2525
#include <QGroupBox>
2626
#include <QLabel>
2727
#include <QProgressBar>
2828
#include <QPushButton>
2929
#include <QSpinBox>
30-
#include <QThread>
3130

3231
class EncodeParameter;
3332
class ProcessParameter;
3433

35-
class TranscodePage : public BasePage, public ProcessObserver {
34+
class TranscodePage : public BasePage {
3635
Q_OBJECT
3736

3837
public:
@@ -44,10 +43,6 @@ class TranscodePage : public BasePage, public ProcessObserver {
4443
QString GetPageTitle() const override { return "Transcode"; }
4544
void RetranslateUi() override;
4645

47-
// ProcessObserver interface
48-
void on_process_update(double progress) override;
49-
void on_time_update(double timeRequired) override;
50-
5146
protected:
5247
void OnInputFileChanged(const QString &newPath) override;
5348
void OnOutputPathUpdate() override;
@@ -67,8 +62,6 @@ private slots:
6762
void SetupUI();
6863
void UpdateOutputPath();
6964
QString GetFileExtension(const QString &filePath);
70-
void RunTranscodeInThread(const QString &inputPath, const QString &outputPath,
71-
EncodeParameter *encodeParam, ProcessParameter *processParam);
7265

7366
// Input/Output section
7467
FileSelectorWidget *inputFileSelector;
@@ -112,6 +105,9 @@ private slots:
112105

113106
// Action section
114107
QPushButton *transcodeButton;
108+
109+
// Conversion runner
110+
ConverterRunner *converterRunner;
115111
};
116112

117113
#endif // TRANSCODE_PAGE_H

0 commit comments

Comments
 (0)