From f84cd8644d85792d032782147af71877c48c69ae Mon Sep 17 00:00:00 2001 From: itsmattkc <34096995+itsmattkc@users.noreply.github.com> Date: Thu, 22 Sep 2022 10:56:52 -0700 Subject: [PATCH] exportformatcombobox: categorize formats where possible --- app/dialog/export/exportformatcombobox.cpp | 145 ++++++++++++++------- app/dialog/export/exportformatcombobox.h | 18 ++- 2 files changed, 111 insertions(+), 52 deletions(-) diff --git a/app/dialog/export/exportformatcombobox.cpp b/app/dialog/export/exportformatcombobox.cpp index dcbcdd01d..2c60cb1a2 100644 --- a/app/dialog/export/exportformatcombobox.cpp +++ b/app/dialog/export/exportformatcombobox.cpp @@ -20,75 +20,120 @@ #include "exportformatcombobox.h" +#include +#include + +#include "ui/icons/icons.h" + namespace olive { ExportFormatComboBox::ExportFormatComboBox(Mode mode, QWidget *parent) : QComboBox(parent) { + custom_menu_ = new Menu(this); + // Populate combobox formats - for (int i=0; i(i); + switch (mode) { + case kShowAllFormats: + custom_menu_->addAction(CreateHeader(icon::Video, tr("Video"))); + PopulateType(Track::kVideo); + custom_menu_->addSeparator(); - switch (mode) { - case kShowAllFormats: - break; - case kShowAudioOnly: - if (!ExportFormat::GetVideoCodecs(f).isEmpty() - || !ExportFormat::GetSubtitleCodecs(f).isEmpty() - || ExportFormat::GetAudioCodecs(f).isEmpty()) { - continue; - } - break; - case kShowVideoOnly: - if (ExportFormat::GetVideoCodecs(f).isEmpty() - || !ExportFormat::GetSubtitleCodecs(f).isEmpty() - || !ExportFormat::GetAudioCodecs(f).isEmpty()) { - continue; - } - break; - case kShowSubtitlesOnly: - if (!ExportFormat::GetVideoCodecs(f).isEmpty() - || ExportFormat::GetSubtitleCodecs(f).isEmpty() - || !ExportFormat::GetAudioCodecs(f).isEmpty()) { - continue; - } - break; - } + custom_menu_->addAction(CreateHeader(icon::Audio, tr("Audio"))); + PopulateType(Track::kAudio); + custom_menu_->addSeparator(); - QString format_name = ExportFormat::GetName(f); - - bool inserted = false; - - // Sort formats alphabetically - for (int j=0; j format_name) { - insertItem(j, format_name, i); - inserted = true; - break; - } - } - - if (!inserted) { - addItem(format_name, i); - } + custom_menu_->addAction(CreateHeader(icon::Subtitles, tr("Subtitle"))); + PopulateType(Track::kSubtitle); + break; + case kShowAudioOnly: + PopulateType(Track::kAudio); + break; + case kShowVideoOnly: + PopulateType(Track::kVideo); + break; + case kShowSubtitlesOnly: + PopulateType(Track::kSubtitle); + break; } - connect(this, static_cast(&QComboBox::currentIndexChanged), this, &ExportFormatComboBox::HandleIndexChange); + connect(custom_menu_, &Menu::triggered, this, &ExportFormatComboBox::HandleIndexChange); +} + +void ExportFormatComboBox::showPopup() +{ + custom_menu_->setMinimumWidth(this->width()); + custom_menu_->exec(mapToGlobal(QPoint(0, 0))); } void ExportFormatComboBox::SetFormat(ExportFormat::Format fmt) { - for (int i=0; i(a->data().toInt()); + SetFormat(f); + emit FormatChanged(f); +} + +void ExportFormatComboBox::PopulateType(Track::Type type) +{ + for (int i=0; i(i); + + if (type == Track::kVideo + && !ExportFormat::GetVideoCodecs(f).isEmpty()) { + // Do nothing + } else if (type == Track::kAudio + && ExportFormat::GetVideoCodecs(f).isEmpty() + && !ExportFormat::GetAudioCodecs(f).isEmpty()) { + // Do nothing + } else if (type == Track::kSubtitle + && ExportFormat::GetVideoCodecs(f).isEmpty() + && ExportFormat::GetAudioCodecs(f).isEmpty() + && !ExportFormat::GetSubtitleCodecs(f).isEmpty()) { + // Do nothing + } else { + continue; } + + QString format_name = ExportFormat::GetName(f); + + QAction *a = custom_menu_->addAction(format_name); + a->setData(i); + a->setIconVisibleInMenu(false); } } -void ExportFormatComboBox::HandleIndexChange(int index) +QWidgetAction *ExportFormatComboBox::CreateHeader(const QIcon &icon, const QString &title) { - emit FormatChanged(static_cast(itemData(index).toInt())); + QWidgetAction *a = new QWidgetAction(this); + + QWidget *w = new QWidget(); + QHBoxLayout *layout = new QHBoxLayout(w); + + QLabel *icon_lbl = new QLabel(); + + QLabel *text_lbl = new QLabel(title); + text_lbl->setAlignment(Qt::AlignCenter); + QFont f = text_lbl->font(); + f.setWeight(QFont::Bold); + text_lbl->setFont(f); + + icon_lbl->setPixmap(icon.pixmap(text_lbl->sizeHint())); + + layout->addStretch(); + layout->addWidget(icon_lbl); + layout->addWidget(text_lbl); + layout->addStretch(); + + a->setDefaultWidget(w); + a->setEnabled(false); + return a; } } diff --git a/app/dialog/export/exportformatcombobox.h b/app/dialog/export/exportformatcombobox.h index c90479e72..8cff5beec 100644 --- a/app/dialog/export/exportformatcombobox.h +++ b/app/dialog/export/exportformatcombobox.h @@ -22,8 +22,11 @@ #define EXPORTFORMATCOMBOBOX_H #include +#include #include "codec/exportformat.h" +#include "node/output/track/track.h" +#include "widget/menu/menu.h" namespace olive { @@ -45,9 +48,11 @@ public: ExportFormat::Format GetFormat() const { - return static_cast(currentData().toInt()); + return current_; } + void showPopup(); + signals: void FormatChanged(ExportFormat::Format fmt); @@ -55,7 +60,16 @@ public slots: void SetFormat(ExportFormat::Format fmt); private slots: - void HandleIndexChange(int index); + void HandleIndexChange(QAction *a); + +private: + void PopulateType(Track::Type type); + + QWidgetAction *CreateHeader(const QIcon &icon, const QString &title); + + Menu *custom_menu_; + + ExportFormat::Format current_; };