1 #include "common/common_pch.h"
2
3 #include <QDragEnterEvent>
4 #include <QDropEvent>
5
6 #include <matroska/KaxAttached.h>
7
8 #include "common/qt.h"
9 #include "mkvtoolnix-gui/forms/header_editor/attachments_page.h"
10 #include "mkvtoolnix-gui/forms/header_editor/tab.h"
11 #include "mkvtoolnix-gui/header_editor/attached_file_page.h"
12 #include "mkvtoolnix-gui/header_editor/attachments_page.h"
13
14 namespace mtx::gui::HeaderEditor {
15
16 using namespace mtx::gui;
17
AttachmentsPage(Tab & parent,KaxAttachedList const & attachments)18 AttachmentsPage::AttachmentsPage(Tab &parent,
19 KaxAttachedList const &attachments)
20 : TopLevelPage{parent, YT("Attachments"), true}
21 , ui{new Ui::AttachmentsPage}
22 , m_filesDDHandler{Util::FilesDragDropHandler::Mode::Remember}
23 , m_initialAttachments{attachments}
24 {
25 ui->setupUi(this);
26
27 connect(ui->addAttachments, &QPushButton::clicked, &parent, &Tab::selectAttachmentsAndAdd);
28 connect(this, &AttachmentsPage::filesDropped, &parent, &Tab::addAttachments);
29 }
30
~AttachmentsPage()31 AttachmentsPage::~AttachmentsPage() {
32 }
33
34 void
init()35 AttachmentsPage::init() {
36 TopLevelPage::init();
37
38 for (auto const &attachment : m_initialAttachments)
39 m_parent.addAttachment(attachment);
40 }
41
42 bool
hasThisBeenModified() const43 AttachmentsPage::hasThisBeenModified()
44 const {
45 auto numChildren = m_children.count();
46
47 if (m_initialAttachments.count() != numChildren)
48 return true;
49
50 for (auto idx = 0; idx < numChildren; ++idx)
51 if (m_initialAttachments[idx] != dynamic_cast<AttachedFilePage &>(*m_children[idx]).m_attachment)
52 return true;
53
54 return false;
55 }
56
57 void
retranslateUi()58 AttachmentsPage::retranslateUi() {
59 ui->retranslateUi(this);
60 }
61
62 void
dragEnterEvent(QDragEnterEvent * event)63 AttachmentsPage::dragEnterEvent(QDragEnterEvent *event) {
64 m_filesDDHandler.handle(event, false);
65 }
66
67 void
dropEvent(QDropEvent * event)68 AttachmentsPage::dropEvent(QDropEvent *event) {
69 if (m_filesDDHandler.handle(event, true))
70 Q_EMIT filesDropped(m_filesDDHandler.fileNames());
71 }
72
73 QString
internalIdentifier() const74 AttachmentsPage::internalIdentifier()
75 const {
76 return Q("attachments");
77 }
78
79 void
rereadChildren(PageModel & model)80 AttachmentsPage::rereadChildren(PageModel &model) {
81 auto numRows = model.rowCount(m_pageIdx);
82
83 m_children.clear();
84 m_children.reserve(numRows);
85
86 for (int row = 0; row < numRows; ++row)
87 m_children.push_back(model.selectedPage(model.index(row, 0, m_pageIdx)));
88 }
89
90 }
91