Chatterino
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GeneralPageView.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <QDebug>
4 #include <boost/variant.hpp>
5 #include <pajlada/signals/signalholder.hpp>
6 #include "Application.hpp"
10 
11 #include <QCheckBox>
12 #include <QComboBox>
13 #include <QPushButton>
14 #include <QSpinBox>
15 
16 class QScrollArea;
17 
18 namespace chatterino {
19 
20 class ColorButton;
21 
22 class Space : public QLabel
23 {
24  Q_OBJECT
25 };
26 
27 class TitleLabel : public QLabel
28 {
29  Q_OBJECT
30 
31 public:
32  TitleLabel(const QString &text)
33  : QLabel(text)
34  {
35  }
36 };
37 
38 class SubtitleLabel : public QLabel
39 {
40  Q_OBJECT
41 
42 public:
43  SubtitleLabel(const QString &text)
44  : QLabel(text)
45  {
46  }
47 };
48 
50 {
51  Q_OBJECT
52 
53 public:
54  NavigationLabel(const QString &text)
55  : SignalLabel()
56  {
57  this->setText(text);
58  }
59 };
60 
61 class DescriptionLabel : public QLabel
62 {
63  Q_OBJECT
64 
65 public:
66  DescriptionLabel(const QString &text)
67  : QLabel(text)
68  {
69  }
70 };
71 
72 class ComboBox : public QComboBox
73 {
74  Q_OBJECT
75 
76  void wheelEvent(QWheelEvent *event) override
77  {
78  }
79 };
80 
81 struct DropdownArgs {
82  QString value;
83  int index;
84  QComboBox *combobox;
85 };
86 
87 class GeneralPageView : public QWidget
88 {
89  Q_OBJECT
90 
91 public:
92  GeneralPageView(QWidget *parent = nullptr);
93 
94  void addWidget(QWidget *widget);
95  void addLayout(QLayout *layout);
96  void addStretch();
97 
98  TitleLabel *addTitle(const QString &text);
99  SubtitleLabel *addSubtitle(const QString &text);
101  QCheckBox *addCheckbox(const QString &text, BoolSetting &setting,
102  bool inverse = false, QString toolTipText = {});
103  ComboBox *addDropdown(const QString &text, const QStringList &items,
104  QString toolTipText = {});
105  ComboBox *addDropdown(const QString &text, const QStringList &items,
106  pajlada::Settings::Setting<QString> &setting,
107  bool editable = false, QString toolTipText = {});
108  ColorButton *addColorButton(const QString &text, const QColor &color,
109  pajlada::Settings::Setting<QString> &setting,
110  QString toolTipText = {});
111  QSpinBox *addIntInput(const QString &text, IntSetting &setting, int min,
112  int max, int step, QString toolTipText = {});
113  void addNavigationSpacing();
114 
115  template <typename OnClick>
116  QPushButton *makeButton(const QString &text, OnClick onClick)
117  {
118  auto button = new QPushButton(text);
119  this->groups_.back().widgets.push_back({button, {text}});
120  QObject::connect(button, &QPushButton::clicked, onClick);
121  return button;
122  }
123 
124  template <typename OnClick>
125  QPushButton *addButton(const QString &text, OnClick onClick)
126  {
127  auto button = makeButton(text, onClick);
128  auto layout = new QHBoxLayout();
129  layout->addWidget(button);
130  layout->addStretch(1);
131  this->addLayout(layout);
132  return button;
133  }
134 
135  template <typename T>
137  const QString &text, const QStringList &items,
138  pajlada::Settings::Setting<T> &setting,
139  std::function<boost::variant<int, QString>(T)> getValue,
140  std::function<T(DropdownArgs)> setValue, bool editable = true,
141  QString toolTipText = {})
142  {
143  auto items2 = items;
144  auto selected = getValue(setting.getValue());
145 
146  if (selected.which() == 1)
147  {
148  // QString
149  if (!editable && !items2.contains(boost::get<QString>(selected)))
150  items2.insert(0, boost::get<QString>(selected));
151  }
152 
153  auto combo = this->addDropdown(text, items2, toolTipText);
154  if (editable)
155  combo->setEditable(true);
156 
157  if (selected.which() == 0)
158  {
159  // int
160  auto value = boost::get<int>(selected);
161  if (value >= 0 && value < items2.size())
162  combo->setCurrentIndex(value);
163  }
164  else if (selected.which() == 1)
165  {
166  // QString
167  combo->setEditText(boost::get<QString>(selected));
168  }
169 
170  setting.connect(
171  [getValue = std::move(getValue), combo](const T &value, auto) {
172  auto var = getValue(value);
173  if (var.which() == 0)
174  combo->setCurrentIndex(boost::get<int>(var));
175  else
176  {
177  combo->setCurrentText(boost::get<QString>(var));
178  combo->setEditText(boost::get<QString>(var));
179  }
180  },
181  this->managedConnections_);
182 
183  QObject::connect(
184  combo, QOverload<const int>::of(&QComboBox::currentIndexChanged),
185  [combo, &setting,
186  setValue = std::move(setValue)](const int newIndex) {
187  setting = setValue(DropdownArgs{combo->itemText(newIndex),
188  combo->currentIndex(), combo});
190  });
191 
192  return combo;
193  }
194  DescriptionLabel *addDescription(const QString &text);
195 
196  void addSeperator();
197  bool filterElements(const QString &query);
198 
199 protected:
200  void resizeEvent(QResizeEvent *ev) override
201  {
202  }
203 
204 private:
205  void updateNavigationHighlighting();
206  void addToolTip(QWidget &widget, QString text) const;
207 
208  struct Widget {
209  QWidget *element;
210  QStringList keywords;
211  };
212 
213  struct Group {
214  QString name;
215  QWidget *title{};
216  QWidget *navigationLink{};
217  Space *space{};
218  std::vector<Widget> widgets;
219  };
220 
221  QScrollArea *contentScrollArea_;
222  QVBoxLayout *contentLayout_;
223  QVBoxLayout *navigationLayout_;
224 
225  std::vector<Group> groups_;
226  pajlada::Signals::SignalHolder managedConnections_;
227 };
228 
229 } // namespace chatterino
Application * getApp()
Definition: Application.cpp:623
SubtitleLabel(const QString &text)
Definition: GeneralPageView.hpp:43
Definition: GeneralPageView.hpp:72
Definition: SignalLabel.hpp:10
QString value
Definition: GeneralPageView.hpp:82
int index
Definition: GeneralPageView.hpp:83
Definition: Application.cpp:48
Definition: GeneralPageView.hpp:49
QPushButton * addButton(const QString &text, OnClick onClick)
Definition: GeneralPageView.hpp:125
void resizeEvent(QResizeEvent *ev) override
Definition: GeneralPageView.hpp:200
Definition: GeneralPageView.hpp:22
ComboBox * addDropdown(const QString &text, const QStringList &items, pajlada::Settings::Setting< T > &setting, std::function< boost::variant< int, QString >(T)> getValue, std::function< T(DropdownArgs)> setValue, bool editable=true, QString toolTipText={})
Definition: GeneralPageView.hpp:136
QComboBox * combobox
Definition: GeneralPageView.hpp:84
TitleLabel(const QString &text)
Definition: GeneralPageView.hpp:32
Definition: ColorButton.hpp:7
#define addSubtitle
Definition: GeneralPage.cpp:38
QPushButton * makeButton(const QString &text, OnClick onClick)
Definition: GeneralPageView.hpp:116
Definition: GeneralPageView.hpp:87
#define addTitle
Definition: GeneralPage.cpp:37
Definition: GeneralPageView.hpp:38
DescriptionLabel(const QString &text)
Definition: GeneralPageView.hpp:66
Definition: GeneralPageView.hpp:27
void forceLayoutChannelViews()
Definition: WindowManager.cpp:217
NavigationLabel(const QString &text)
Definition: GeneralPageView.hpp:54
QString name
Definition: Credentials.cpp:94
Definition: GeneralPageView.hpp:81
Definition: GeneralPageView.hpp:61
WindowManager *const windows
Definition: Application.hpp:85