@@ -96,39 +96,78 @@ void OptionsDialog::FixTabOrder(QWidget * const o)
96
96
}
97
97
}
98
98
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)
100
106
{
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 ());
114
108
115
- horizontalLayout->addWidget (o);
109
+ auto & first_o = objs[0 ];
110
+ QWidget * const parent = first_o->parentWidget ();
116
111
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);
122
113
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
+ }
124
152
125
- horizontalLayout->addStretch (1 );
153
+ if (opts. stretch ) horizontalLayout->addStretch (opts. stretch );
126
154
127
155
layout->addLayout (horizontalLayout);
128
156
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
+ }
130
162
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, });
132
171
}
133
172
134
173
static void setSiblingsEnabled (QWidget * const o, const bool state)
0 commit comments