1 #include "common/common_pch.h"
2
3 #include <QScrollBar>
4 #include <QVariant>
5
6 #include "common/locale.h"
7 #include "common/qt.h"
8 #include "mkvtoolnix-gui/forms/main_window/select_character_set_dialog.h"
9 #include "mkvtoolnix-gui/main_window/main_window.h"
10 #include "mkvtoolnix-gui/main_window/select_character_set_dialog.h"
11 #include "mkvtoolnix-gui/util/widget.h"
12
13 namespace mtx::gui {
14
15 class SelectCharacterSetDialogPrivate {
16 friend class SelectCharacterSetDialog;
17
18 std::unique_ptr<Ui::SelectCharacterSetDialog> m_ui{new Ui::SelectCharacterSetDialog};
19 QByteArray m_content;
20 QVariant m_userData;
21 };
22
SelectCharacterSetDialog(QWidget * parent,QString const & fileName,QString const & initialCharacterSet,QStringList const & additionalCharacterSets)23 SelectCharacterSetDialog::SelectCharacterSetDialog(QWidget *parent,
24 QString const &fileName,
25 QString const &initialCharacterSet,
26 QStringList const &additionalCharacterSets)
27 : QDialog{parent, Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint}
28 , p_ptr{new SelectCharacterSetDialogPrivate}
29 {
30 auto p = p_func();
31 auto characterSetToSelect = initialCharacterSet.isEmpty() ? Q(g_cc_local_utf8->get_charset()) : initialCharacterSet;
32
33 QFile file{fileName};
34 if (file.open(QIODevice::ReadOnly))
35 p->m_content = file.readAll();
36
37 p->m_ui->setupUi(this);
38
39 p->m_ui->fileName->setText(fileName);
40
41 auto characterSets = additionalCharacterSets;
42 characterSets << characterSetToSelect;
43
44 p->m_ui->characterSet->setAdditionalItems(characterSets).setup().setCurrentByData(characterSetToSelect);
45
46 p->m_ui->content->setPlaceholderText(QY("File not found"));
47
48 connect(MainWindow::get(), &MainWindow::preferencesChanged, this, &SelectCharacterSetDialog::retranslateUi);
49 connect(p->m_ui->characterSet, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &SelectCharacterSetDialog::updatePreview);
50 connect(p->m_ui->buttonBox, &QDialogButtonBox::accepted, this, &SelectCharacterSetDialog::accept);
51 connect(p->m_ui->buttonBox, &QDialogButtonBox::rejected, this, &SelectCharacterSetDialog::reject);
52 connect(this, &SelectCharacterSetDialog::accepted, this, &SelectCharacterSetDialog::emitResult);
53 connect(this, &SelectCharacterSetDialog::finished, this, &SelectCharacterSetDialog::deleteLater);
54
55 Util::restoreWidgetGeometry(this);
56
57 updatePreview();
58 }
59
~SelectCharacterSetDialog()60 SelectCharacterSetDialog::~SelectCharacterSetDialog() {
61 Util::saveWidgetGeometry(this);
62 }
63
64 void
retranslateUi()65 SelectCharacterSetDialog::retranslateUi() {
66 auto p = p_func();
67
68 p->m_ui->retranslateUi(this);
69 }
70
71 void
updatePreview()72 SelectCharacterSetDialog::updatePreview() {
73 auto p = p_func();
74
75 if (p->m_content.isEmpty())
76 return;
77
78 auto converter = charset_converter_c::init(to_utf8(selectedCharacterSet()));
79 if (!converter)
80 return;
81
82 auto horizontalScrollBar = p->m_ui->content->horizontalScrollBar();
83 auto verticalScrollBar = p->m_ui->content->verticalScrollBar();
84 auto horizontalPosition = horizontalScrollBar->value();
85 auto verticalPosition = verticalScrollBar->value();
86
87 p->m_ui->content->setPlainText(Q(converter->utf8(p->m_content.data())));
88
89 horizontalScrollBar->setValue(std::min(horizontalPosition, horizontalScrollBar->maximum()));
90 verticalScrollBar ->setValue(std::min(verticalPosition, verticalScrollBar->maximum()));
91 }
92
93 void
emitResult()94 SelectCharacterSetDialog::emitResult() {
95 Q_EMIT characterSetSelected(selectedCharacterSet());
96 }
97
98 QString
selectedCharacterSet() const99 SelectCharacterSetDialog::selectedCharacterSet()
100 const {
101 return p_func()->m_ui->characterSet->currentData().toString();
102 }
103
104 void
setUserData(QVariant const & data)105 SelectCharacterSetDialog::setUserData(QVariant const &data) {
106 p_func()->m_userData = data;
107 }
108
109 QVariant const &
userData() const110 SelectCharacterSetDialog::userData()
111 const {
112 return p_func()->m_userData;
113 }
114
115 }
116