1 #include "common/common_pch.h"
2 
3 #include <QComboBox>
4 
5 #include "common/qt.h"
6 #include "mkvtoolnix-gui/forms/info/job_settings_widget.h"
7 #include "mkvtoolnix-gui/info/job_settings.h"
8 #include "mkvtoolnix-gui/info/job_settings_widget.h"
9 #include "mkvtoolnix-gui/util/file_dialog.h"
10 #include "mkvtoolnix-gui/util/settings.h"
11 
12 namespace mtx::gui::Info {
13 
14 using namespace mtx::gui;
15 
16 class JobSettingsWidgetPrivate {
17 public:
18   std::unique_ptr<Ui::JobSettingsWidget> m_ui{new Ui::JobSettingsWidget};
19 };
20 
JobSettingsWidget(QWidget * parent)21 JobSettingsWidget::JobSettingsWidget(QWidget *parent)
22   : QWidget{parent}
23   , p_ptr{new JobSettingsWidgetPrivate}
24 {
25   auto p = p_func();
26 
27   // Setup UI controls.
28   p->m_ui->setupUi(this);
29 
30   connect(p->m_ui->mode,           static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &JobSettingsWidget::enableControlsAccordingToMode);
31   connect(p->m_ui->browseFileName, &QPushButton::clicked,                                                 this, &JobSettingsWidget::browseFileName);
32   connect(p->m_ui->fileName,       &QLineEdit::textChanged,                                               this, &JobSettingsWidget::emitFileNameChangeSignal);
33 }
34 
~JobSettingsWidget()35 JobSettingsWidget::~JobSettingsWidget() {
36 }
37 
38 void
setFileNameVisible(bool visible)39 JobSettingsWidget::setFileNameVisible(bool visible) {
40   auto &ui = *p_func()->m_ui;
41 
42   for (auto &widget : QList<QWidget *>{ui.fileNameLabel, ui.fileName, ui.browseFileName})
43     widget->setVisible(visible);
44 }
45 
46 void
setSettings(JobSettings const & jobSettings)47 JobSettingsWidget::setSettings(JobSettings const &jobSettings) {
48   auto &ui = *p_func()->m_ui;
49 
50   ui.fileName->setText(jobSettings.m_fileName);
51   ui.mode->setCurrentIndex(jobSettings.m_mode == JobSettings::Mode::Tree ? 0 : 1);
52   ui.verbosity->setCurrentIndex(jobSettings.m_verbosity == JobSettings::Verbosity::StopAtFirstCluster ? 0 : 1);
53   ui.hexDumps->setCurrentIndex(jobSettings.m_hexDumps == JobSettings::HexDumps::None ? 0 : jobSettings.m_hexDumps == JobSettings::HexDumps::First16Bytes ? 1 : 2);
54   ui.checksums->setChecked(jobSettings.m_checksums);
55   ui.trackInfo->setChecked(jobSettings.m_trackInfo);
56   ui.hexPositions->setChecked(jobSettings.m_hexPositions);
57 
58   enableControlsAccordingToMode();
59 }
60 
61 JobSettings
settings()62 JobSettingsWidget::settings() {
63   auto &ui = *p_func()->m_ui;
64   JobSettings jobSettings;
65 
66   jobSettings.m_fileName     = ui.fileName->text();
67   jobSettings.m_mode         = ui.mode->currentIndex()      == 0 ? JobSettings::Mode::Tree                    : JobSettings::Mode::Summary;
68   jobSettings.m_verbosity    = ui.verbosity->currentIndex() == 0 ? JobSettings::Verbosity::StopAtFirstCluster : JobSettings::Verbosity::AllElements;
69   jobSettings.m_hexDumps     = ui.hexDumps->currentIndex()  == 0 ? JobSettings::HexDumps::None
70                              : ui.hexDumps->currentIndex()  == 1 ? JobSettings::HexDumps::First16Bytes
71                              :                                     JobSettings::HexDumps::AllBytes;
72   jobSettings.m_checksums    = ui.checksums->isChecked();
73   jobSettings.m_trackInfo    = ui.trackInfo->isChecked();
74   jobSettings.m_hexPositions = ui.hexPositions->isChecked();
75 
76   return jobSettings;
77 }
78 
79 void
enableControlsAccordingToMode()80 JobSettingsWidget::enableControlsAccordingToMode() {
81   auto &ui    = *p_func()->m_ui;
82   bool isTree = ui.mode->currentIndex() == 0;
83 
84   for (auto &widget : QList<QWidget *>{ui.verbosityLabel, ui.verbosity, ui.hexDumpsLabel, ui.hexDumps, ui.checksums})
85     widget->setEnabled(isTree);
86 }
87 
88 void
browseFileName()89 JobSettingsWidget::browseFileName() {
90   auto &ui       = *p_func()->m_ui;
91   auto &settings = Util::Settings::get();
92   auto path      = !ui.fileName->text().isEmpty() ? QFileInfo{ ui.fileName->text() }.absoluteDir() : Util::dirPath(settings.m_lastOpenDir.path());
93   auto mkvName   = !ui.fileName->text().isEmpty() ? QFileInfo{ ui.fileName->text() }.completeBaseName() + Q(".txt") : QString{};
94   auto fileName  = Util::getSaveFileName(this, QY("Select destination file name"), Util::dirPath(settings.m_lastOpenDir.path()), mkvName, QY("Text files") + Q(" (*.txt)"), Q("txt"));
95 
96   if (fileName.isEmpty())
97     return;
98 
99   auto fileInfo          = QFileInfo{ fileName };
100   settings.m_lastOpenDir = fileInfo.absoluteDir();
101   settings.save();
102 
103   ui.fileName->setText(fileName);
104 }
105 
106 void
emitFileNameChangeSignal(QString const & fileName)107 JobSettingsWidget::emitFileNameChangeSignal(QString const &fileName) {
108   Q_EMIT fileNameChanged(fileName);
109 }
110 
111 }
112