-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlaylistGeneratorWindow.cpp
More file actions
364 lines (299 loc) · 10.5 KB
/
PlaylistGeneratorWindow.cpp
File metadata and controls
364 lines (299 loc) · 10.5 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "PlaylistGeneratorWindow.h"
#include "Messages.h"
#include <Button.h>
#include <CardLayout.h>
#include <Catalog.h>
#include <CheckBox.h>
#include <GroupLayoutBuilder.h>
#include <LayoutBuilder.h>
#include <ListView.h>
#include <MenuField.h>
#include <MenuItem.h>
#include <PopUpMenu.h>
#include <ScrollView.h>
#include <SeparatorView.h>
#include <StringItem.h>
#include <StringView.h>
#include <TextControl.h>
#include <cstdio>
#include <stdlib.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "PlaylistGeneratorWindow"
/** @name Internal Commands */
///@{
static const uint32 MSG_TYPE_CHANGED = 'tpch';
static const uint32 MSG_ADD_RULE = 'addR';
static const uint32 MSG_REMOVE_RULE = 'remR';
static const uint32 MSG_GEN_GENERATE = 'geng';
static const uint32 MSG_GEN_CANCEL = 'genc';
///@}
/**
* @brief Returns a string representation of the rule for display in the
* ListView.
*/
BString Rule::ToString() const {
BString s;
if (exclude)
s << B_TRANSLATE("NOT ");
if (type == 0)
s << B_TRANSLATE("Genre: ") << value;
else if (type == 1)
s << B_TRANSLATE("Artist: ") << value;
else if (type == 2)
s << B_TRANSLATE("Year: ") << value << " - " << value2;
return s;
}
/**
* @class RuleItem
* @brief A BStringItem wrapper for a Rule object to display in the BListView.
*/
class RuleItem : public BStringItem {
public:
explicit RuleItem(const Rule &r) : BStringItem(r.ToString()), rule(r) {}
Rule rule;
};
/**
* @brief Constructs the PlaylistGenerator window.
*
* Sets up the complex layout including card layout for dynamic rule inputs,
* rule list view, and limit options.
*
* @param target Messenger to send the final generation request to.
* @param genres List of genres to populate the genre dropdown.
*/
PlaylistGeneratorWindow::PlaylistGeneratorWindow(
BMessenger target, const std::vector<BString> &genres)
: BWindow(BRect(100, 100, 600, 500), B_TRANSLATE("Generate Playlist"),
B_TITLED_WINDOW, B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
fTarget(target), fGenres(genres), fInputCardLayout(nullptr),
fRuleList(nullptr) {
_BuildUI(genres);
CenterOnScreen();
_UpdateInputFields();
}
PlaylistGeneratorWindow::~PlaylistGeneratorWindow() {
if (fRuleList) {
while (BListItem *it = fRuleList->RemoveItem((int32)0))
delete it;
}
}
/**
* @brief Builds the window UI layout.
*/
void PlaylistGeneratorWindow::_BuildUI(const std::vector<BString> &genres) {
// Calculate font-relative sizes for DPI scaling
font_height fh;
be_plain_font->GetHeight(&fh);
float fontHeight = fh.ascent + fh.descent + fh.leading;
fNameInput = new BTextControl("Name", B_TRANSLATE("Name:"),
B_TRANSLATE("New Playlist"), nullptr);
BPopUpMenu *typeMenu = new BPopUpMenu("Type");
typeMenu->AddItem(
new BMenuItem(B_TRANSLATE("Genre"), new BMessage(MSG_TYPE_CHANGED)));
typeMenu->AddItem(
new BMenuItem(B_TRANSLATE("Artist"), new BMessage(MSG_TYPE_CHANGED)));
typeMenu->AddItem(
new BMenuItem(B_TRANSLATE("Year"), new BMessage(MSG_TYPE_CHANGED)));
typeMenu->ItemAt(0)->SetMarked(true);
typeMenu->SetTargetForItems(this);
fTypeField = new BMenuField("Type", B_TRANSLATE("Criterion:"), typeMenu);
fExcludeCheck = new BCheckBox("Exclude", B_TRANSLATE("not"), nullptr);
fAddRuleBtn =
new BButton("Add", B_TRANSLATE("Add"), new BMessage(MSG_ADD_RULE));
fAddRuleBtn->SetTarget(this);
BView *dynamicContainer = new BView("DynamicContainer", B_WILL_DRAW);
dynamicContainer->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
fInputCardLayout = new BCardLayout();
dynamicContainer->SetLayout(fInputCardLayout);
{
BGroupView *genreGroup =
new BGroupView(B_HORIZONTAL, B_USE_DEFAULT_SPACING);
BPopUpMenu *genreMenu = new BPopUpMenu("SelectGenre");
for (const auto &g : genres)
genreMenu->AddItem(new BMenuItem(g, nullptr));
if (!genres.empty())
genreMenu->ItemAt(0)->SetMarked(true);
fGenreSelect = new BMenuField("GenreSel", B_TRANSLATE("Genre:"), genreMenu);
genreGroup->GroupLayout()->AddView(fGenreSelect);
genreGroup->GroupLayout()->AddItem(BSpaceLayoutItem::CreateGlue());
fInputCardLayout->AddView(genreGroup);
}
{
BGroupView *artistGroup =
new BGroupView(B_HORIZONTAL, B_USE_DEFAULT_SPACING);
fArtistInput =
new BTextControl("ArtistVal", B_TRANSLATE("Name:"), "", nullptr);
artistGroup->AddChild(fArtistInput);
fInputCardLayout->AddView(artistGroup);
}
{
BGroupView *yearGroup = new BGroupView(B_HORIZONTAL, B_USE_DEFAULT_SPACING);
fYearFromInput =
new BTextControl("YearFrom", B_TRANSLATE("From:"), "", nullptr);
fYearToInput = new BTextControl("YearTo", B_TRANSLATE("To:"), "", nullptr);
yearGroup->AddChild(fYearFromInput);
yearGroup->AddChild(fYearToInput);
fInputCardLayout->AddView(yearGroup);
}
fInputCardLayout->SetVisibleItem((int32)0);
fRuleList = new BListView("Rules", B_SINGLE_SELECTION_LIST);
BScrollView *listScroll =
new BScrollView("ScrollRule", fRuleList, B_FRAME_EVENTS, false, true);
listScroll->SetExplicitMinSize(BSize(fontHeight * 24, fontHeight * 12));
fRemoveRuleBtn = new BButton("Remove", B_TRANSLATE("Remove"),
new BMessage(MSG_REMOVE_RULE));
fRemoveRuleBtn->SetTarget(this);
BPopUpMenu *limitMenu = new BPopUpMenu("Modus");
limitMenu->AddItem(new BMenuItem(B_TRANSLATE("No Limit"), nullptr));
limitMenu->AddItem(new BMenuItem(B_TRANSLATE("Max. Tracks"), nullptr));
limitMenu->AddItem(
new BMenuItem(B_TRANSLATE("Max. Duration (Min)"), nullptr));
limitMenu->ItemAt(0)->SetMarked(true);
fLimitModeField =
new BMenuField("LimitMode", B_TRANSLATE("Limit:"), limitMenu);
fLimitValue =
new BTextControl("LimitVal", B_TRANSLATE("Value:"), "50", nullptr);
fShuffleCheck =
new BCheckBox("Shuffle", B_TRANSLATE("Shuffle Playback"), nullptr);
fGenerateBtn = new BButton("Generate", B_TRANSLATE("Generate"),
new BMessage(MSG_GEN_GENERATE));
fCancelBtn = new BButton("Cancel", B_TRANSLATE("Cancel"),
new BMessage(MSG_GEN_CANCEL));
fGenerateBtn->SetTarget(this);
fCancelBtn->SetTarget(this);
BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_DEFAULT_SPACING)
.SetInsets(B_USE_WINDOW_SPACING)
.Add(fNameInput)
.Add(new BSeparatorView(B_HORIZONTAL))
.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING)
.Add(new BStringView("h1", B_TRANSLATE("Define Rule")))
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.Add(fTypeField)
.Add(fExcludeCheck)
.End()
.Add(dynamicContainer)
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.AddGlue()
.Add(fAddRuleBtn)
.End()
.End()
.Add(new BSeparatorView(B_HORIZONTAL))
.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING, 1.0f)
.Add(new BStringView("h2", B_TRANSLATE("Rule List")))
.Add(listScroll, 1.0f)
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.AddGlue()
.Add(fRemoveRuleBtn)
.End()
.End()
.Add(new BSeparatorView(B_HORIZONTAL))
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.Add(fLimitModeField)
.Add(fLimitValue)
.End()
.Add(fShuffleCheck)
.Add(new BSeparatorView(B_HORIZONTAL))
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.AddGlue()
.Add(fCancelBtn)
.Add(fGenerateBtn)
.End();
fGenerateBtn->MakeDefault(true);
}
/**
* @brief Updates the visible input fields based on the selected rule criteria
* type.
*/
void PlaylistGeneratorWindow::_UpdateInputFields() {
if (!fInputCardLayout || !fTypeField || !fTypeField->Menu())
return;
BMenuItem *marked = fTypeField->Menu()->FindMarked();
int32 type = marked ? fTypeField->Menu()->IndexOf(marked) : 0;
if (type < 0)
type = 0;
if (type > 2)
type = 2;
fInputCardLayout->SetVisibleItem(type);
}
/**
* @brief Creates a Rule object from current inputs and adds it to the list.
*/
void PlaylistGeneratorWindow::_AddRule() {
Rule r;
r.type = fTypeField->Menu()->IndexOf(fTypeField->Menu()->FindMarked());
r.exclude = (fExcludeCheck->Value() == B_CONTROL_ON);
if (r.type == 0) {
BMenuItem *item = fGenreSelect->Menu()->FindMarked();
if (!item)
return;
r.value = item->Label();
} else if (r.type == 1) {
r.value = fArtistInput->Text();
if (r.value.IsEmpty())
return;
} else {
r.value = fYearFromInput->Text();
r.value2 = fYearToInput->Text();
if (r.value.IsEmpty())
return;
}
fRuleList->AddItem(new RuleItem(r));
int32 last = fRuleList->CountItems() - 1;
if (last >= 0) {
fRuleList->Select(last);
fRuleList->ScrollToSelection();
}
fRuleList->Invalidate();
}
/**
* @brief Removes the currently selected rule from the list.
*/
void PlaylistGeneratorWindow::_RemoveRule() {
int32 sel = fRuleList->CurrentSelection();
if (sel >= 0)
delete fRuleList->RemoveItem(sel);
}
void PlaylistGeneratorWindow::MessageReceived(BMessage *msg) {
switch (msg->what) {
case MSG_TYPE_CHANGED:
_UpdateInputFields();
break;
case MSG_ADD_RULE:
_AddRule();
break;
case MSG_REMOVE_RULE:
_RemoveRule();
break;
case MSG_GEN_CANCEL:
Quit();
break;
case MSG_GEN_GENERATE: {
BMessage genMsg(MSG_GENERATE_PLAYLIST);
genMsg.AddString("name", fNameInput->Text());
for (int32 i = 0; i < fRuleList->CountItems(); i++) {
RuleItem *item = dynamic_cast<RuleItem *>(fRuleList->ItemAt(i));
if (!item)
continue;
BMessage ruleMsg;
ruleMsg.AddInt32("type", item->rule.type);
ruleMsg.AddString("val1", item->rule.value);
ruleMsg.AddString("val2", item->rule.value2);
ruleMsg.AddBool("exclude", item->rule.exclude);
genMsg.AddMessage("rule", &ruleMsg);
}
BMenuItem *limitItem = fLimitModeField->Menu()->FindMarked();
if (limitItem) {
int32 index = fLimitModeField->Menu()->IndexOf(limitItem);
genMsg.AddInt32("limit_mode", index);
genMsg.AddInt32("limit_value", atoi(fLimitValue->Text()));
}
genMsg.AddBool("shuffle", fShuffleCheck->Value() == B_CONTROL_ON);
fTarget.SendMessage(&genMsg);
Quit();
break;
}
default:
BWindow::MessageReceived(msg);
break;
}
}