1 #include "BadgePickerDialog.hpp"
2 #include <QSizePolicy>
3 #include "singletons/Resources.hpp"
4 
5 #include "providers/twitch/TwitchBadges.hpp"
6 
7 #include <QDialogButtonBox>
8 #include <QVBoxLayout>
9 
10 namespace chatterino {
11 
BadgePickerDialog(QList<DisplayBadge> badges,QWidget * parent)12 BadgePickerDialog::BadgePickerDialog(QList<DisplayBadge> badges,
13                                      QWidget *parent)
14     : QDialog(parent)
15 {
16     this->dropdown_ = new QComboBox;
17     auto vbox = new QVBoxLayout(this);
18     auto buttonBox =
19         new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
20 
21     vbox->addWidget(this->dropdown_);
22     vbox->addWidget(buttonBox);
23 
24     QObject::connect(buttonBox, &QDialogButtonBox::accepted, [this] {
25         this->accept();
26         this->close();
27     });
28     QObject::connect(buttonBox, &QDialogButtonBox::rejected, [this] {
29         this->reject();
30         this->close();
31     });
32 
33     this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
34 
35     this->setWindowFlags(
36         (this->windowFlags() & ~(Qt::WindowContextHelpButtonHint)) |
37         Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
38 
39     // Add items.
40     for (const auto &item : badges)
41     {
42         this->dropdown_->addItem(item.displayName(), item.badgeName());
43     }
44 
45     const auto updateBadge = [=](int index) {
46         BadgeOpt badge;
47         if (index >= 0 && index < badges.size())
48         {
49             badge = badges[index];
50         }
51         this->currentBadge_ = badge;
52     };
53 
54     QObject::connect(this->dropdown_,
55                      QOverload<int>::of(&QComboBox::currentIndexChanged),
56                      updateBadge);
57     updateBadge(0);
58 
59     // Set icons.
60     TwitchBadges::instance()->getBadgeIcons(
61         badges,
62         [&dropdown = this->dropdown_](QString identifier, const QIconPtr icon) {
63             if (!dropdown)
64                 return;
65 
66             int index = dropdown->findData(identifier);
67             if (index != -1)
68             {
69                 dropdown->setItemIcon(index, *icon);
70             }
71         });
72 }
73 
74 }  // namespace chatterino
75