1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
#include "DialogSelect.h"
#include <functional>
#include <QCheckBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QDebug>
#include <QImageReader>
#include <QPlainTextEdit>
#include <QRadioButton>
#include <QScrollArea>
#include <QSplitter>
#include <QStackedWidget>
#include <QVBoxLayout>
#include "MIT-licencedCodeToDoStuff/checkboxwordwrap.h"
// there is no hover signal, the only way to know is to override enterEvent()
//
template <class T, typename... Args>
class HoverableWidget : public T
{
public:
std::function<void ()> onHover;
HoverableWidget(Args... args, std::function<void ()> h)
: T(args...), onHover(std::move(h))
{
}
protected: void enterEvent(QEnterEvent*) override
{
onHover();
}
};
FixedAspectRatioImageLabel::FixedAspectRatioImageLabel(QWidget* parent) : QLabel(parent)
{
}
void FixedAspectRatioImageLabel::setUnscaledPixmap(const QPixmap& pixmap)
{
mUnscaledPixmap = pixmap;
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
rescalePixmap(size());
copySizeLabel.setPixmap(pixmap);
}
const QPixmap& FixedAspectRatioImageLabel::unscaledPixmap() const
{
return mUnscaledPixmap;
}
QSize FixedAspectRatioImageLabel::sizeHint() const
{
if (mUnscaledPixmap.isNull())
{
return QLabel::sizeHint();
}
else
{
// maybe should add frame border
return mUnscaledPixmap.size();
//return copySizeLabel.sizeHint();
}
}
bool FixedAspectRatioImageLabel::hasHeightForWidth() const
{
return true;
}
int FixedAspectRatioImageLabel::heightForWidth(int width) const
{
// this ignores the difference between size and contentsRect size
return mUnscaledPixmap.height() * width / (double)mUnscaledPixmap.width();
}
int FixedAspectRatioImageLabel::widthForHeight(int height) const
{
return mUnscaledPixmap.width() * height / (double)mUnscaledPixmap.height();
}
void FixedAspectRatioImageLabel::resizeEvent(QResizeEvent* resizeEvent)
{
QLabel::resizeEvent(resizeEvent);
rescalePixmap(contentsRect().size());
}
void FixedAspectRatioImageLabel::rescalePixmap(const QSize& size)
{
setPixmap(mUnscaledPixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
std::optional<QVector<int>> DialogSelect(
QWidget* parent, const QString& title, const QVector<QString>& items,
const QVector<QString>& descriptions, const QVector<QString>& pixmaps,
bool multiSelect)
{
QDialog d(parent);
d.setWindowTitle(title);
auto* mainLayout = new QVBoxLayout(&d);
d.setLayout(mainLayout);
auto* splitter = new QSplitter(&d);
mainLayout->addWidget(splitter);
// button box
auto* buttons = new QDialogButtonBox(
QDialogButtonBox::Ok|QDialogButtonBox::Cancel, &d);
/* QObject::connect: signal not found in QDialogButtonBox
even though this worked with my initial attempt.
Holt had the same issue: https://stackoverflow.com/questions/61879664/qobjectconnect-not-working-signal-not-found-with-function-syntax
The new syntax is much better, but as this code wants replacing, that's not a hill I'm going to die on.
QObject::connect(buttons, &QDialogButtonBox::accepted, [&] {
d.accept();
});
QObject::connect(buttons, &QDialogButtonBox::rejected, [&] {
d.reject();
});
*/
QObject::connect(buttons, SIGNAL(accepted()), &d, SLOT(accept()));
QObject::connect(buttons, SIGNAL(rejected()), &d, SLOT(reject()));
mainLayout->addWidget(buttons);
// left panel
auto* left = new QWidget(splitter);
auto* leftLayout = new QVBoxLayout(left);
leftLayout->setContentsMargins(0, 0, 0, 0);
auto* stack = new QStackedWidget(left);
leftLayout->addWidget(stack);
// don't put descriptions of pixmaps at all if there aren't any
const bool hasDescriptions = !descriptions.empty();
const bool hasPixmaps = !pixmaps.empty();
// for each description/item
for (int i=0; i < std::max(descriptions.size(), pixmaps.size()); ++i) {
auto* panel = new QWidget(left);
auto* panelLayout = new QVBoxLayout(panel);
//panelLayout->setContentsMargins(0, 0, 0, 0);
if (hasPixmaps) {
auto* pixmapLabel = new FixedAspectRatioImageLabel(panel);
pixmapLabel->setFrameStyle(QFrame::StyledPanel);
pixmapLabel->setLineWidth(1);
// make it resizable
//pixmapLabel->setMinimumSize(1, 150);
//pixmapLabel->setScaledContents(true);
//pixmapLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
if (i < pixmaps.size()) {
// this item has a pixmap
// Use QImageReader as (despite what the documentation says) QPixmap uses the extension, rather than the file header, to determine the format.
QImageReader reader(pixmaps[i]);
reader.setDecideFormatFromContent(true);
QImage image = reader.read();
if (image.isNull()) {
pixmapLabel->setText(QString("failed to load '%1': %2 (code %3)").arg(pixmaps[i], reader.errorString(), QString::number(reader.error())));
} else {
QPixmap pixmap = QPixmap::fromImage(image);
pixmapLabel->setUnscaledPixmap(std::move(pixmap));
}
}
panelLayout->addWidget(pixmapLabel);
}
if (hasDescriptions)
{
// description under pixmap (if any)
auto* description = new QPlainTextEdit(descriptions[i], panel);
// this puts the description on top if there's no pixmap
//description->setAlignment(Qt::AlignLeft|Qt::AlignTop);
//description->setWordWrap(true);
panelLayout->addWidget(description);
}
// if the pixmap is first, it'll take as much space as it can; if the
// description is first, the AlignTop makes it work anyways
panelLayout->setStretch(0, 1);
stack->addWidget(panel);
}
// right panel
auto* right = new QWidget(&d);
auto* rightOuterLayout = new QVBoxLayout(right);
rightOuterLayout->setContentsMargins(0, 0, 0, 0);
// title
auto* titleLabel = new QLabel(title, right);
titleLabel->setWordWrap(true);
rightOuterLayout->addWidget(titleLabel);
QWidget* rightInner;
QBoxLayout* rightInnerLayout;
// Doing this unconditionally breaks HGEC lower body choices.
// Somehow the fixed aspect ratio image label forces the scroll area to be resized below its preferred size.
// Because the word wrapped radio buttons aren't good at telling Qt their size hint is only minimal for the current width,
// the layout ends up with their preferred width as its minimum and won't let itself be shrunk smaller than that even though you can't see the right hand side.
// This means that as they're in a widget with enough space, the text isn't wrapped.
// The exact same behaviour happens with non-wrappable radio buttons, but it's more expected.
// Making the fixed aspect ratio image label play nicely with being shrunk will probably fix this.
// It only seems to be MEAT that actually needs a scrollbar, though, and that looks fine, so fixing this mess is left as an exercise for the reader.
if (items.size() >= 10)
{
QScrollArea* rightScrollArea = new QScrollArea(right);
rightOuterLayout->addWidget(rightScrollArea);
rightScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
rightScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
rightInner = new QWidget(right);
rightScrollArea->setWidget(rightInner);
rightScrollArea->setWidgetResizable(true);
rightInnerLayout = new QVBoxLayout(rightInner);
}
else
{
rightInner = right;
rightInnerLayout = rightOuterLayout;
}
// callback when hovering
auto onHover = [&](int i) {
stack->setCurrentIndex(i);
};
// remember buttons to see which were checked
QVector<QAbstractButton*> itemButtons;
// for each item
for (int i=0; i<items.size(); ++i) {
QAbstractButton* w = nullptr;
QString labelText = items[i];
bool checked = false;
// THIS IS IMPORTANT WHEN REPLACING THIS WITH BETTER CODE. OMODS HAVE A PIPE AT THE START OF ANY OPTIONS SELECTED BY DEFAULT.
if (labelText.startsWith('|'))
{
labelText = labelText.remove(0, 1);
checked = true;
}
if (multiSelect) {
w = new HoverableWidget<CheckBoxWordWrap, const QString&, QWidget*>(labelText, rightInner, [=]{ onHover(i); });
} else {
w = new HoverableWidget<RadioButtonWordWrap, const QString&, QWidget*>(labelText, rightInner, [=]{ onHover(i); });
checked |= i == 0;
}
rightInnerLayout->addWidget(w);
itemButtons.push_back(w);
w->setChecked(checked);
}
// push all the items to the top
rightInnerLayout->addStretch(1);
splitter->addWidget(left);
splitter->addWidget(right);
// decent initial size
d.resize(800, 500);
if (d.exec() != QDialog::Accepted) {
return std::nullopt;
}
// go through every button, it doesn't matter whether they're checkboxes or
// radio buttons
QVector<int> selection;
for (int i=0; i<itemButtons.size(); ++i) {
if (itemButtons[i]->isChecked()) {
selection.push_back(i);
}
}
return selection;
}
|