1 /***********************************************************************
2 
3                           C I N E   E N C O D E R
4                                 JULY, 2020
5                             COPYRIGHT (C) 2020
6 
7  FILE: mainwindow.h
8  MODIFIED: November, 2021
9  COMMENT:
10  LICENSE: GNU General Public License v3.0
11 
12 ***********************************************************************/
13 
14 #ifndef WIDGET_H
15 #define WIDGET_H
16 
17 #include <QWidget>
18 #include <QCheckBox>
19 #include <QMainWindow>
20 #include <QtGlobal>
21 #include <QSystemTrayIcon>
22 #include <QDesktopWidget>
23 #include <QPaintEvent>
24 #include <QMouseEvent>
25 #include <QHoverEvent>
26 #include <QCloseEvent>
27 #include <QShowEvent>
28 #include <QDragEnterEvent>
29 #include <QDragLeaveEvent>
30 #include <QDragMoveEvent>
31 #include <QDropEvent>
32 #include <QMimeDatabase>
33 #include <QMimeData>
34 #include <QTableWidgetItem>
35 #include <QListView>
36 #include <QUrl>
37 #include <QList>
38 #include <QMenu>
39 #include <QProcess>
40 #include <QTimer>
41 #include <QDate>
42 #include <QFileDialog>
43 #include <QPixmap>
44 #include <QHBoxLayout>
45 #include <QGridLayout>
46 #include <QLabel>
47 #include <iostream>
48 #include <iomanip>
49 #include <sstream>
50 #include <ctime>
51 #include <math.h>
52 #include <QMovie>
53 #include <QDockWidget>
54 #include <QFile>
55 #include <QTreeWidgetItem>
56 #include <QSizePolicy>
57 #include <QSettings>
58 #include <QMap>
59 #include <QTranslator>
60 #include "openingfiles.h"
61 
62 
63 enum columnIndex {
64     FILENAME,       FORMAT,         RESOLUTION,     DURATION,           FPS,
65     AR,             STATUS,         BITRATE,        SUBSAMPLING,        BITDEPTH,
66     COLORSPACE,     COLORRANGE,     COLORPRIM,      COLORMATRIX,        TRANSFER,
67     MAXLUM,         MINLUM,         MAXCLL,         MAXFALL,            MASTERDISPLAY,
68     PATH,           T_DUR,          T_CHROMACOORD,  T_WHITECOORD,       T_STREAMSIZE,
69     T_WIDTH,        T_HEIGHT,       T_VIDEOTITLE,   T_VIDEOMOVIENAME,   T_VIDEOYEAR,
70     T_VIDEOAUTHOR,  T_VIDEOPERF,    T_VIDEODESCR,   T_AUDIO_1,          T_AUDIO_2,
71     T_AUDIO_3,      T_AUDIO_4,      T_AUDIO_5,      T_AUDIO_6,          T_AUDIO_7,
72     T_AUDIO_8,      T_AUDIO_9,      T_AUDIOLANG_1,  T_AUDIOLANG_2,      T_AUDIOLANG_3,
73     T_AUDIOLANG_4,  T_AUDIOLANG_5,  T_AUDIOLANG_6,  T_AUDIOLANG_7,      T_AUDIOLANG_8,
74     T_AUDIOLANG_9,  T_AUDIOTITLE_1, T_AUDIOTITLE_2, T_AUDIOTITLE_3,     T_AUDIOTITLE_4,
75     T_AUDIOTITLE_5, T_AUDIOTITLE_6, T_AUDIOTITLE_7, T_AUDIOTITLE_8,     T_AUDIOTITLE_9,
76     T_AUDIOCHECK_1, T_AUDIOCHECK_2, T_AUDIOCHECK_3, T_AUDIOCHECK_4,     T_AUDIOCHECK_5,
77     T_AUDIOCHECK_6, T_AUDIOCHECK_7, T_AUDIOCHECK_8, T_AUDIOCHECK_9,     T_SUBTITLE_1,
78     T_SUBTITLE_2,   T_SUBTITLE_3,   T_SUBTITLE_4,   T_SUBTITLE_5,       T_SUBTITLE_6,
79     T_SUBTITLE_7,   T_SUBTITLE_8,   T_SUBTITLE_9,   T_SUBLANG_1,        T_SUBLANG_2,
80     T_SUBLANG_3,    T_SUBLANG_4,    T_SUBLANG_5,    T_SUBLANG_6,        T_SUBLANG_7,
81     T_SUBLANG_8,    T_SUBLANG_9,    T_TITLESUB_1,   T_TITLESUB_2,       T_TITLESUB_3,
82     T_TITLESUB_4,   T_TITLESUB_5,   T_TITLESUB_6,   T_TITLESUB_7,       T_TITLESUB_8,
83     T_TITLESUB_9,   T_SUBCHECK_1,   T_SUBCHECK_2,   T_SUBCHECK_3,       T_SUBCHECK_4,
84     T_SUBCHECK_5,   T_SUBCHECK_6,   T_SUBCHECK_7,   T_SUBCHECK_8,       T_SUBCHECK_9,
85     T_STARTTIME,    T_ENDTIME
86 };
87 enum curParamIndex {
88     OUTPUT_PARAM,   CODEC,          MODE,
89     CONTAINER,      BQR,            MAXRATE,
90     BUFSIZE,        FRAME_RATE,     BLENDING,
91     WIDTH,          HEIGHT,         PASS,
92     PRESET,         COLOR_RANGE,    MIN_LUM,
93     MAX_LUM,        MAX_CLL,        MAX_FALL,
94     MASTER_DISPLAY, CHROMA_COORD,   WHITE_COORD,
95     AUDIO_CODEC,    AUDIO_BITRATE,  MINRATE,
96     LEVEL,          ASAMPLE_RATE,   ACHANNELS,
97     MATRIX,         PRIMARY,        TRC,
98     PRESET_NAME,    REP_PRIM,       REP_MATRIX,
99     REP_TRC
100 };
101 enum curHDRIndex {
102     CUR_COLOR_RANGE,    CUR_COLOR_PRIMARY,  CUR_COLOR_MATRIX,
103     CUR_TRANSFER,       CUR_MAX_LUM,        CUR_MIN_LUM,
104     CUR_MAX_CLL,        CUR_MAX_FALL,       CUR_MASTER_DISPLAY,
105     CUR_CHROMA_COORD,   CUR_WHITE_COORD
106 };
107 enum videoMetadataIndex {
108     VIDEO_TITLE,        VIDEO_AUTHOR,       VIDEO_YEAR,
109     VIDEO_PERFORMER,    VIDEO_DESCRIPTION,  VIDEO_MOVIENAME, ENUM_END_LABEL
110 };
111 enum dockIndex {
112     PRESETS_DOCK, PREVIEW_DOCK, SOURCE_DOCK, OUTPUT_DOCK,
113     STREAMS_DOCK, LOG_DOCK, METADATA_DOCK, SPLIT_DOCK
114 };
115 
116 
117 QT_BEGIN_NAMESPACE
118 namespace Ui
119 {
120     class Widget;
121 }
122 QT_END_NAMESPACE
123 
124 
125 class Widget : public QWidget
126 {
127     Q_OBJECT
128 
129 public:
130 
131     Widget(QWidget *parent = nullptr);
132 
133     ~Widget();
134 
135     static const int PARAMETERS_COUNT = 34;
136 
137     // ******************** Settings *************************//
138 
139     //QString _new_param[PARAMETERS_COUNT];
140 
141     QVector<QString> _new_param;
142 
143     QByteArray _settingsWindowGeometry;
144 
145     QByteArray _presetWindowGeometry;
146 
147     bool _hideInTrayFlag;
148 
149     QString _desktopEnv;
150 
151     QString _language;
152 
153     QString _output_folder;
154 
155     QString _temp_folder;
156 
157     QString _prefixName;
158 
159     QString _suffixName;
160 
161     QString _font;
162 
163     int _fontSize;
164 
165     int _prefxType;
166 
167     int _suffixType;
168 
169     int _pos_top;
170 
171     int _pos_cld;
172 
173     int _timer_interval;
174 
175     bool _showHDR_mode;
176 
177     bool _protection;
178 
179 private slots:
180 
181     void trayIconActivated(QSystemTrayIcon::ActivationReason reason);
182 
183     void setTrayIconActions();
184 
185     void showTrayIcon();
186 
187     void on_closeWindow_clicked();
188 
189     void setExpandIcon();
190 
191     void on_hideWindow_clicked();
192 
193     void on_expandWindow_clicked();
194 
195     void on_actionSettings_clicked();
196 
197     void on_actionAdd_clicked();
198 
199     void on_actionRemove_clicked();
200 
201     void on_actionEncode_clicked();
202 
203     void on_actionStop_clicked();
204 
205     void on_buttonSortDown_clicked();
206 
207     void on_buttonSortUp_clicked();
208 
209     void setParameters();
210 
211     void setDocksParameters(QList<int> dockSizesX, QList<int> dockSizesY);
212 
213     void showEvent(QShowEvent *event);
214 
215     void closeEvent(QCloseEvent *event);
216 
217     bool eventFilter(QObject *watched, QEvent *event);
218 
219     void dragEnterEvent(QDragEnterEvent* event);
220 
221     void dragMoveEvent(QDragMoveEvent* event);
222 
223     void dragLeaveEvent(QDragLeaveEvent* event);
224 
225     void dropEvent(QDropEvent* event);
226 
227     void on_tableWidget_itemSelectionChanged();
228 
229     QString callFileDialog(const QString title);
230 
231     void make_preset();
232 
233     void pause();
234 
235     void resume();
236 
237     void add_metadata();
238 
239     void complete();
240 
241     void progress_1();
242 
243     void progress_2();
244 
245     void cancel();
246 
247     void error();
248 
249     void encode();
250 
251     void on_lineEditTitleVideo_editingFinished();
252 
253     void on_lineEditAuthorVideo_editingFinished();
254 
255     void on_lineEditYearVideo_editingFinished();
256 
257     void on_lineEditPerfVideo_editingFinished();
258 
259     void on_lineEditMovieNameVideo_editingFinished();
260 
261     void on_lineEditDescriptionVideo_editingFinished();
262 
263     void on_horizontalSlider_valueChanged(int value);
264 
265     void on_buttonFramePrevious_clicked();
266 
267     void on_buttonFrameNext_clicked();
268 
269     void on_buttonSetStartTime_clicked();
270 
271     void on_buttonSetEndTime_clicked();
272 
273     void showMetadataEditor();
274 
275     void showAudioStreamsSelection();
276 
277     void showSubtitlesSelection();
278 
279     void showVideoSplitter();
280 
281     void repeatHandler_Type_1();
282 
283     void repeatHandler_Type_2();
284 
285     void on_buttonApplyPreset_clicked();
286 
287     void on_actionRemove_preset_clicked();
288 
289     void on_actionEdit_preset_clicked();
290 
291     void add_section();
292 
293     void add_preset();
294 
295     void renameSectionPreset();
296 
297     void setPresetIcon(QTreeWidgetItem *item, bool collapsed);
298 
299     void on_treeWidget_itemCollapsed(QTreeWidgetItem *item);
300 
301     void on_treeWidget_itemExpanded(QTreeWidgetItem *item);
302 
303     void on_buttonHotInputFile_clicked();
304 
305     void on_buttonHotOutputFile_clicked();
306 
307     void on_buttonCloseTaskWindow_clicked();
308 
309     void paintEvent(QPaintEvent *event);
310 
311     void on_treeWidget_itemChanged(QTreeWidgetItem *item, int column);
312 
313     void on_comboBoxMode_currentIndexChanged(int index);
314 
315     void on_horizontalSlider_resize_valueChanged(int value);
316 
317     void on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column);
318 
319     void desktopEnvDetection();
320 
321     void on_actionClearMetadata_clicked();
322 
323     void on_actionUndoMetadata_clicked();
324 
325     void on_actionClearAudioTitles_clicked();
326 
327     void on_actionClearSubtitleTitles_clicked();
328 
329     void on_actionUndoTitles_clicked();
330 
331     void on_actionResetLabels_clicked();
332 
333 private:
334 
335     Ui::Widget *ui;
336 
337     //QString _cur_param[PARAMETERS_COUNT];
338 
339     QVector<QString> _cur_param;
340 
341     QVector<QVector<QString>> _preset_table;
342 
343     OpeningFiles openingFiles;
344 
345     // ******************* Constants *****************************//
346 
347     static const int PRESETS_VERSION = 352;
348 
349     static const int SETTINGS_VERSION = 352;
350 
351     static const int NUMBER_PRESETS = 29;
352 
353     static const int AMOUNT_AUDIO_STREAMS = 9;
354 
355     static const int AMOUNT_SUBTITLES = 9;
356 
357     static constexpr float MAXIMUM_ALLOWED_TIME = 359999.0f;
358 
359     // ******************** Dock area ****************************//
360 
361     QMainWindow *window;
362 
363     QWidget *centralWidget;
364 
365     static const int DOCKS_COUNT = 8;
366 
367     QDockWidget *docks[DOCKS_COUNT];
368 
369     // ******************** Top label ****************************//
370 
371     QLabel *raiseThumb;
372 
373     QLabel *audioThumb;
374 
375     QLabel *subtitleThumb;
376 
377     // **************** Progress animation ***********************//
378 
379     QMovie *animation;
380 
381     // ***************** Tray menu actions ************************//
382 
383     QSystemTrayIcon *trayIcon;
384 
385     QMenu *trayIconMenu;
386 
387     QAction *minimizeAction;
388 
389     QAction *restoreAction;
390 
391     QAction *quitAction;
392 
393     // ***************** Top menu actions ************************//
394 
395     QAction *add_files;
396 
397     QAction *remove_files;
398 
399     QAction *close_prog;
400 
401 
402     QAction *select_preset;
403 
404     QAction *encode_files;
405 
406     QAction *stop_encode;
407 
408 
409     QAction *edit_metadata;
410 
411     QAction *select_audio;
412 
413     QAction *select_subtitles;
414 
415     QAction *split_video;
416 
417 
418     QAction *settings;
419 
420     QAction *reset_view;
421 
422     QAction *about;
423 
424     QAction *donate;
425 
426 
427     QMenu* menuFiles;
428 
429     QMenu* menuEdit;
430 
431     QMenu* menuTools;
432 
433     QMenu* menuView;
434 
435     QMenu* menuPreferences;
436 
437     QMenu* menuAbout;
438 
439     // ***************** Table menu actions ************************//
440 
441     QMenu* itemMenu;
442 
443     // ****************** Tree menu actions ************************//
444 
445     QMenu* sectionMenu;
446 
447     QMenu* presetMenu;
448 
449     // ***************** Preset menu actions ***********************//
450 
451     QAction *addsection;
452 
453     QAction *addpreset;
454 
455     QMenu* menu;
456 
457     // ********************** Processes ****************************//
458 
459     QProcess *processEncoding;
460 
461     QProcess *processThumbCreation;
462 
463     // *********************** Timers ******************************//
464 
465     QTimer *timer;
466 
467     QTimer *timerCallSetThumbnail;
468 
469     // ******************** Initialization *************************//
470 
471     QFile _wind_file;
472 
473     QString _openDir;
474 
475     int _theme;
476 
477     // ************************** Metadata ************************//
478 
479     QString _hdr[11];
480 
481     QString _videoMetadata[6];
482 
483     int _audioStreamCheckState[AMOUNT_AUDIO_STREAMS];
484 
485     QString _audioLang[AMOUNT_AUDIO_STREAMS];
486 
487     QString _audioTitle[AMOUNT_AUDIO_STREAMS];
488 
489     int _subtitleCheckState[AMOUNT_SUBTITLES];
490 
491     QString _subtitleLang[AMOUNT_SUBTITLES];
492 
493     QString _subtitleTitle[AMOUNT_SUBTITLES];
494 
495     // ************************* Encoding ***************************//
496 
497     QString _message;
498 
499     QString _error_message;
500 
501 
502     QSettings *_settings;
503 
504     QString _curFilename;
505 
506     QString _curPath;
507 
508     QString _temp_file;
509 
510     QString _input_file;
511 
512     QString _output_file;
513 
514     QString _settings_path;
515 
516     QString _thumb_path;
517 
518     QString _preset_file;
519 
520     QString _preset_0;
521 
522     QString _preset_pass1;
523 
524     QString _preset;
525 
526     QString _preset_mkvmerge;
527 
528     QString _sub_mux_param;
529 
530 
531     QString _fmt;
532 
533     QString _width;
534 
535     QString _height;
536 
537     QString _fps;
538 
539     QString _stream_size;
540 
541     double _dur;
542 
543     int _fr_count;
544 
545 
546     time_t _loop_start;
547 
548     time_t _strt_t;
549 
550     double _curTime;
551 
552     double _startTime;
553 
554     double _endTime;
555 
556 
557     int _row;
558 
559     QString _status_encode_btn;
560 
561     bool _flag_two_pass;
562 
563     bool _flag_hdr;
564 
565     bool _calling_pr_1;
566 
567     bool _mux_mode;
568 
569     bool _batch_mode;
570 
571     // *********************** Geometry ***********************************//
572 
573     int _rowSize;
574 
575     bool _windowActivated;
576 
577     bool _expandWindowsState;
578 
579     bool clickPressedFlag;
580 
581     bool clickPressed_Left_ResizeFlag;
582 
583     bool clickPressed_Left_Top_ResizeFlag;
584 
585     bool clickPressed_Top_ResizeFlag;
586 
587     bool clickPressed_Right_Top_ResizeFlag;
588 
589     bool clickPressed_Right_ResizeFlag;
590 
591     bool clickPressed_Right_Bottom_ResizeFlag;
592 
593     bool clickPressed_Bottom_ResizeFlag;
594 
595     bool clickPressed_Left_Bottom_ResizeFlag;
596 
597     QPoint mouseClickCoordinate;
598 
599     QPoint mouseCoordinate;
600 
601     int oldWidth;
602 
603     int oldHeight;
604 
605     int curWidth;
606 
607     int curHeight;
608 
609     // ****************************************************************************//
610 
611     void createConnections();
612 
613     void on_actionAbout_clicked();
614 
615     void on_actionDonate_clicked();
616 
617     void showOpeningFiles(bool status);
618 
619     void showOpeningFiles(QString text);
620 
621     void showOpeningFiles(int percent);
622 
623     void openFiles(const QStringList &file_name_open);
624 
625     void get_current_data();
626 
627     void get_output_filename();
628 
629     void setStatus(QString status);
630 
631     void restore_initial_state();
632 
633     void call_task_complete(const QString &_message, const bool &_timer_mode);
634 
635     bool call_dialog(const QString &_message);
636 
637     void setTheme(int &ind_theme);
638 
639     QString styleCreator(const QString &list);
640 
641     QString timeConverter(double &time);
642 
643     QString timeConverter(float &time);
644 
645     QString setThumbnail(QString curFilename, double time, QString quality, int destination);
646 
647     void provideContextMenu(const QPoint &position);
648 
649     void providePresetContextMenu(const QPoint &position);
650 
651     void resizeTableRows(int rows_height);
652 
653     void resetView();
654 
655     /************************* Preset Window ******************************/
656 
657     void set_defaults();
658 
659     void setItemStyle(QTreeWidgetItem *item);
660 
661     void updateCurPresetPos(int &index_top, int &index_child);
662 
663     void updateInfoFields(QString &codec_qstr, QString &mode_qstr, QString &container_qstr,
664                           QString &bqr_qstr, QString &pass_qstr, QString &preset_qstr,
665                           QString &acodec_qstr, QTreeWidgetItem *item, bool defaultNameFlag);
666 
667     void updatePresetTable();
668 
669     QString updateFieldCodec(int &codec);
670 
671     QString updateFieldMode(int &codec, int &mode);
672 
673     QString updateFieldPreset(int &codec, int &preset);
674 
675     QString updateFieldPass(int &codec, int &pass);
676 
677     QString updateFieldAcodec(int &codec, int &acodec);
678 
679     QString updateFieldContainer(int &codec, int &container);
680 };
681 
682 #endif // WIDGET_H
683