Skip to content

Commit d9f0785

Browse files
committed
GUI/Options: Make CreateOptionUI even more flexible
1 parent 50feba0 commit d9f0785

File tree

2 files changed

+67
-24
lines changed

2 files changed

+67
-24
lines changed

src/qt/optionsdialog.cpp

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,39 +96,78 @@ void OptionsDialog::FixTabOrder(QWidget * const o)
9696
}
9797
}
9898

99-
void OptionsDialog::CreateOptionUI(QBoxLayout * const layout, QWidget * const o, const QString& text, QBoxLayout *horizontalLayout)
99+
struct CreateOptionUIOpts {
100+
QBoxLayout *horizontal_layout{nullptr};
101+
int stretch{1};
102+
int indent{0};
103+
};
104+
105+
void OptionsDialog::CreateOptionUI(QBoxLayout * const layout, const QString& text, const std::vector<QWidget *>& objs, const CreateOptionUIOpts& opts)
100106
{
101-
QWidget * const parent = o->parentWidget();
102-
const QStringList text_parts = text.split("%s");
103-
104-
if (!horizontalLayout) horizontalLayout = new QHBoxLayout();
105-
106-
if (!text_parts[0].isEmpty()) {
107-
QLabel * const labelBefore = new QLabel(parent);
108-
labelBefore->setText(text_parts[0]);
109-
labelBefore->setTextFormat(Qt::PlainText);
110-
labelBefore->setBuddy(o);
111-
labelBefore->setToolTip(o->toolTip());
112-
horizontalLayout->addWidget(labelBefore);
113-
}
107+
Assert(!objs.empty());
114108

115-
horizontalLayout->addWidget(o);
109+
auto& first_o = objs[0];
110+
QWidget * const parent = first_o->parentWidget();
116111

117-
QLabel * const labelAfter = new QLabel(parent);
118-
labelAfter->setText(text_parts[1]);
119-
labelAfter->setTextFormat(Qt::PlainText);
120-
labelAfter->setBuddy(o);
121-
labelAfter->setToolTip(o->toolTip());
112+
QBoxLayout * const horizontalLayout = opts.horizontal_layout ? opts.horizontal_layout : (new QHBoxLayout);
122113

123-
horizontalLayout->addWidget(labelAfter);
114+
if (opts.indent) horizontalLayout->addSpacing(opts.indent);
115+
116+
int processed{0}, index_start{0};
117+
QWidget *last_widget{nullptr};
118+
while (true) {
119+
int pos = text.indexOf('%', index_start);
120+
int idx;
121+
if (pos == -1) {
122+
pos = text.size();
123+
idx = -1;
124+
} else {
125+
const int pos_next{pos + 1};
126+
const auto char_next = text[pos_next];
127+
idx = (char_next == 's') ? 0 : (char_next.digitValue() - 1);
128+
if (pos_next == text.size() || idx < 0 || idx > 8 || (unsigned)idx >= objs.size()) {
129+
index_start = pos_next;
130+
continue;
131+
}
132+
}
133+
if (processed != pos) {
134+
auto label_text = text.mid(processed, pos - processed);
135+
if (auto last_widget_as_qcheckbox = qobject_cast<QCheckBox*>(last_widget)) {
136+
if (label_text[0].isSpace()) label_text = label_text.mid(1);
137+
last_widget_as_qcheckbox->setText(label_text);
138+
} else {
139+
const auto label = new QLabel(parent);
140+
label->setText(label_text);
141+
label->setTextFormat(Qt::PlainText);
142+
label->setBuddy(first_o);
143+
label->setToolTip(first_o->toolTip());
144+
horizontalLayout->addWidget(label);
145+
}
146+
}
147+
if (idx == -1) break;
148+
last_widget = objs[idx];
149+
horizontalLayout->addWidget(last_widget);
150+
index_start = processed = pos + 2;
151+
}
124152

125-
horizontalLayout->addStretch(1);
153+
if (opts.stretch) horizontalLayout->addStretch(opts.stretch);
126154

127155
layout->addLayout(horizontalLayout);
128156

129-
o->setProperty("L", QVariant::fromValue((QLayout*)horizontalLayout));
157+
for (auto& o : objs) {
158+
o->setProperty("L", QVariant::fromValue((QLayout*)horizontalLayout));
159+
FixTabOrder(o);
160+
}
161+
}
130162

131-
FixTabOrder(o);
163+
void OptionsDialog::CreateOptionUI(QBoxLayout * const layout, const QString& text, const std::vector<QWidget *>& objs)
164+
{
165+
CreateOptionUI(layout, text, objs, {});
166+
}
167+
168+
void OptionsDialog::CreateOptionUI(QBoxLayout * const layout, QWidget * const o, const QString& text, QBoxLayout *horizontalLayout)
169+
{
170+
CreateOptionUI(layout, text, {o}, { .horizontal_layout = horizontalLayout, });
132171
}
133172

134173
static void setSiblingsEnabled(QWidget * const o, const bool state)

src/qt/optionsdialog.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class QValueComboBox;
2727
class QWidget;
2828
QT_END_NAMESPACE
2929

30+
struct CreateOptionUIOpts;
31+
3032
/** QScrollArea, but returning reasonable size hints.
3133
*/
3234
class ModScrollArea : public QScrollArea {
@@ -110,6 +112,8 @@ private Q_SLOTS:
110112

111113
QWidget *prevwidget{nullptr};
112114
void FixTabOrder(QWidget *);
115+
void CreateOptionUI(QBoxLayout *, const QString& text, const std::vector<QWidget *>&, const CreateOptionUIOpts&);
116+
void CreateOptionUI(QBoxLayout *, const QString& text, const std::vector<QWidget *>&);
113117
void CreateOptionUI(QBoxLayout *, QWidget *, const QString& text, QBoxLayout *horizontalLayout = nullptr);
114118

115119
QCheckBox *walletrbf;

0 commit comments

Comments
 (0)