1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "inputtab.h" |
5 | |
6 | #include "inputlistview.h" |
7 | #include "settingstab.h" |
8 | |
9 | #include <QFileDialog> |
10 | #include <QGridLayout> |
11 | #include <QLabel> |
12 | #include <QLineEdit> |
13 | #include <QPlainTextEdit> |
14 | #include <QPushButton> |
15 | |
16 | InputTab::InputTab(SettingsTab *settings, QWidget *parent) : QWidget(parent), settingsTab(settings) |
17 | { |
18 | QLabel *fileNameLabel = new QLabel(tr(s: "Input files:" )); |
19 | lastInputPath = QDir::currentPath(); |
20 | inputFilesListBox = new InputListView; |
21 | |
22 | convertButton = new QPushButton("Convert" , this); |
23 | connect(sender: convertButton, signal: &QAbstractButton::clicked, context: this, slot: [&]() { convert(); }); |
24 | |
25 | statusText = new QPlainTextEdit; |
26 | statusText->setReadOnly(true); |
27 | |
28 | QObject::connect(sender: &converterThread, signal: &ConverterThread::convertDone, context: this, slot: &InputTab::convertDone); |
29 | QObject::connect(sender: &converterThread, signal: &ConverterThread::convertStart, context: this, slot: &InputTab::convertStart); |
30 | QObject::connect(sender: &converterThread, signal: &ConverterThread::convertUpdate, context: this, slot: &InputTab::convertUpdate); |
31 | |
32 | QPushButton *browseInputButton = new QPushButton(tr(s: "Browse..." ), this); |
33 | connect(sender: browseInputButton, signal: &QAbstractButton::clicked, context: this, slot: &InputTab::browseInput); |
34 | |
35 | QPushButton *removeSelectedButton = new QPushButton(tr(s: "Remove selected" ), this); |
36 | connect(sender: removeSelectedButton, signal: &QAbstractButton::clicked, context: this, slot: &InputTab::removeSelected); |
37 | QPushButton *selectAllButton = new QPushButton(tr(s: "Select all" ), this); |
38 | connect(sender: selectAllButton, signal: &QAbstractButton::clicked, context: this, slot: &InputTab::selectAll); |
39 | |
40 | QLabel *pathLabel = new QLabel(tr(s: "Output folder:" )); |
41 | outputPathEdit = new QLineEdit(QDir::currentPath()); |
42 | QPushButton *browseOutputButton = new QPushButton(tr(s: "Browse..." ), this); |
43 | connect(sender: browseOutputButton, signal: &QAbstractButton::clicked, context: this, slot: &InputTab::browseOutput); |
44 | |
45 | QLabel *emptyLabel = new QLabel(); |
46 | |
47 | QGridLayout *mainLayout = new QGridLayout; |
48 | |
49 | constexpr int rowFileNameLabel = 0; |
50 | constexpr int rowinputFiles = 1; |
51 | constexpr int rowOutputPathLabel = 5; |
52 | constexpr int rowOutputPathEdit = 6; |
53 | constexpr int rowStatusLabel = 7; |
54 | |
55 | mainLayout->addWidget(fileNameLabel, row: rowFileNameLabel, column: 0, rowSpan: 1, columnSpan: 2); |
56 | mainLayout->addWidget(inputFilesListBox, row: rowinputFiles, column: 0, rowSpan: 4, columnSpan: 1); |
57 | mainLayout->addWidget(browseInputButton, row: rowinputFiles, column: 1); |
58 | mainLayout->addWidget(removeSelectedButton, row: rowinputFiles + 1, column: 1); |
59 | mainLayout->addWidget(selectAllButton, row: rowinputFiles + 2, column: 1); |
60 | mainLayout->addWidget(emptyLabel, row: rowinputFiles + 3, column: 1); |
61 | mainLayout->setRowStretch(row: rowinputFiles + 3, stretch: 1); |
62 | |
63 | mainLayout->addWidget(pathLabel, row: rowOutputPathLabel, column: 0, rowSpan: 1, columnSpan: 2); |
64 | |
65 | mainLayout->addWidget(outputPathEdit, row: rowOutputPathEdit, column: 0); |
66 | mainLayout->addWidget(browseOutputButton, row: rowOutputPathEdit, column: 1); |
67 | |
68 | mainLayout->addWidget(new QLabel("Status:" ), row: rowStatusLabel, column: 0, rowSpan: 1, columnSpan: 2); |
69 | mainLayout->addWidget(statusText, row: rowStatusLabel + 1, column: 0, rowSpan: 1, columnSpan: 2); |
70 | mainLayout->addWidget(convertButton, row: rowStatusLabel + 2, column: 0, rowSpan: 1, columnSpan: 2); |
71 | |
72 | setLayout(mainLayout); |
73 | } |
74 | |
75 | void InputTab::convert() |
76 | { |
77 | QStringList filenames = getInputFiles(); |
78 | QDir outputPath = getOutputPath(); |
79 | |
80 | const auto options = settingsTab->getOptions(); |
81 | |
82 | converterThread.convert(filenames, outputPath, options); |
83 | } |
84 | |
85 | void InputTab::convertStart(const QString &text) |
86 | { |
87 | statusText->setPlainText(text); |
88 | convertButton->setText(QString("Converting..." )); |
89 | convertButton->setDisabled(true); |
90 | } |
91 | |
92 | void InputTab::convertUpdate(const QString &text) |
93 | { |
94 | statusText->appendPlainText(text); |
95 | } |
96 | |
97 | void InputTab::convertDone(const QString &text) |
98 | { |
99 | statusText->appendPlainText(text); |
100 | convertButton->setText(QString("Convert" )); |
101 | convertButton->setDisabled(false); |
102 | } |
103 | |
104 | QStringList InputTab::getInputFiles() const |
105 | { |
106 | QStringList files; |
107 | for (int i = 0; i < inputFilesListBox->count(); ++i) |
108 | files.push_back(t: inputFilesListBox->item(row: i)->text()); |
109 | return files; |
110 | } |
111 | |
112 | QString InputTab::getOutputPath() const |
113 | { |
114 | return outputPathEdit->text(); |
115 | } |
116 | |
117 | void InputTab::browseInput() |
118 | { |
119 | QFileDialog dialog(this); |
120 | dialog.setFileMode(QFileDialog::ExistingFiles); |
121 | dialog.setDirectory(lastInputPath); |
122 | |
123 | if (!dialog.exec()) |
124 | return; |
125 | |
126 | const auto selectedFiles = dialog.selectedFiles(); |
127 | for (const auto &file : selectedFiles) |
128 | inputFilesListBox->tryAddItem(label: QDir::toNativeSeparators(pathName: file)); |
129 | |
130 | if (!selectedFiles.empty()) |
131 | lastInputPath = QFileInfo(selectedFiles.first()).absoluteDir().absolutePath(); |
132 | } |
133 | |
134 | void InputTab::browseOutput() |
135 | { |
136 | QString dir = QFileDialog::getExistingDirectory(parent: this, |
137 | caption: tr(s: "Open Directory" ), |
138 | dir: outputPathEdit->text(), |
139 | options: QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); |
140 | if (!dir.isEmpty()) { |
141 | outputPathEdit->setText(dir); |
142 | } |
143 | } |
144 | |
145 | void InputTab::removeSelected() |
146 | { |
147 | QList<QListWidgetItem *> selectedItems = inputFilesListBox->selectedItems(); |
148 | for (int i = 0; i < inputFilesListBox->count(); ++i) { |
149 | auto item = inputFilesListBox->item(row: i); |
150 | if (selectedItems.contains(t: item)) { |
151 | inputFilesListBox->takeItem(row: i); |
152 | selectedItems.removeAll(t: item); |
153 | i--; |
154 | } |
155 | } |
156 | } |
157 | |
158 | void InputTab::selectAll() |
159 | { |
160 | for (int i = 0; i < inputFilesListBox->count(); ++i) { |
161 | auto item = inputFilesListBox->item(row: i); |
162 | item->setSelected(true); |
163 | } |
164 | } |
165 | |