1 #pragma once
2 
3 #include "common/common_pch.h"
4 
5 #include <QDateTime>
6 
7 #include "mkvtoolnix-gui/header_editor/page_model.h"
8 #include "mkvtoolnix-gui/util/kax_analyzer.h"
9 #include "mkvtoolnix-gui/util/modify_tracks_submenu.h"
10 
11 class QAction;
12 class QMenu;
13 
14 class property_element_c;
15 
16 namespace mtx::gui::HeaderEditor {
17 
18 namespace Ui {
19 class Tab;
20 }
21 
22 using KaxAttachedPtr  = std::shared_ptr<libmatroska::KaxAttached>;
23 
24 class AttachmentsPage;
25 class TopLevelPage;
26 class TrackTypePage;
27 class ValuePage;
28 
29 class Tab : public QWidget {
30 public:
31   enum class ModifiedConfirmationMode {
32     Closing,
33     Reloading,
34   };
35 
36 private:
37   Q_OBJECT
38 
39 protected:
40   // UI stuff:
41   std::unique_ptr<Ui::Tab> ui;
42 
43   QString m_fileName;
44   std::unique_ptr<mtx::gui::Util::KaxAnalyzer> m_analyzer;
45 
46   PageModel *m_model;
47   PageBase *m_segmentinfoPage{};
48   AttachmentsPage *m_attachmentsPage{};
49   bool m_ignoreSelectionChanges{}, m_tracksReordered{};
50 
51   QMenu *m_treeContextMenu, *m_modifySelectedTrackMenu, *m_languageShortcutsMenu;
52   QAction *m_expandAllAction, *m_collapseAllAction, *m_addAttachmentsAction, *m_removeAttachmentAction, *m_removeAllAttachmentsAction, *m_saveAttachmentContentAction;
53   QAction *m_replaceAttachmentContentAction, *m_replaceAttachmentContentSetValuesAction;
54 
55   mtx::gui::Util::ModifyTracksSubmenu m_modifyTracksSubmenu;
56 
57   std::shared_ptr<EbmlElement> m_eSegmentInfo, m_eTracks;
58 
59 public:
60   explicit Tab(QWidget *parent, QString const &fileName);
61   ~Tab();
62 
63   PageModel *model() const;
64 
65   virtual PageBase *hasBeenModified();
66   virtual void retranslateUi();
67   virtual void appendPage(PageBase *page, QModelIndex const &parentIdx = {});
68   virtual QString const &fileName() const;
69   virtual QString title() const;
70   virtual void validate();
71   virtual void addAttachment(KaxAttachedPtr const &attachment);
72   virtual bool isClosingOrReloadingOkIfModified(ModifiedConfirmationMode mode);
73   virtual void toggleSpecificTrackFlag(unsigned int wantedId);
74   virtual void toggleTrackFlag();
75   virtual bool isTrackSelected();
76 
77 Q_SIGNALS:
78   void removeThisTab();
79 
80 public Q_SLOTS:
81   virtual void showTreeContextMenu(QPoint const &pos);
82   virtual void selectionChanged(QModelIndex const &current, QModelIndex const &previous);
83   virtual void load();
84   virtual void save();
85   virtual void expandAll();
86   virtual void collapseAll();
87   virtual void selectAttachmentsAndAdd();
88   virtual void addAttachments(QStringList const &fileNames);
89   virtual void removeAllAttachments();
90   virtual void removeSelectedAttachment();
91   virtual void saveAttachmentContent();
92   virtual void replaceAttachmentContent(bool deriveNameAndMimeType);
93   virtual void handleDroppedFiles(QStringList const &fileNames, Qt::MouseButtons mouseButtons);
94   virtual void focusPage(PageBase *page);
95   virtual void handleReorderedTracks();
96   virtual void changeTrackLanguage(QString const &formattedLanguage);
97   virtual void moveElementUpOrDown(bool up);
98 
99 protected:
100   void setupUi();
101   void setupModifyTracksMenu();
102   void setupToolTips();
103   void handleSegmentInfo(kax_analyzer_data_c const &data);
104   void handleTracks(kax_analyzer_data_c const &data);
105   void handleAttachments();
106   void populateTree();
107   void resetData();
108   void doModifications();
109   void reportValidationFailure(bool isCritical, QModelIndex const &pageIdx);
110   std::unordered_map<uint64_t, uint64_t> determineTrackUIDChanges();
111 
112   ValuePage *createValuePage(TopLevelPage &parentPage, EbmlMaster &parentMaster, property_element_c const &element);
113   PageBase *currentlySelectedPage() const;
114 
115   KaxAttachedPtr createAttachmentFromFile(QString const &fileName);
116 
117   void pruneEmptyMastersForTrack(TrackTypePage &page);
118   void pruneEmptyMastersForAllTracks();
119 
120   void updateTracksElementToMatchTrackOrder();
121 
122   void walkPagesOfSelectedTopLevelNode(std::function<bool(PageBase *)> worker);
123 
124 public:
125   static memory_cptr readFileData(QWidget *parent, QString const &fileName);
126 };
127 
128 }
129