-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfManageAlbum_.pas
323 lines (263 loc) · 8.73 KB
/
fManageAlbum_.pas
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
unit fManageAlbum_;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.UITypes,
fMain_, DataStructs, DataModule, Vcl.ExtCtrls;
type
TfManageAlbum = class(TForm)
Label1: TLabel;
luBands: TComboBox;
Label2: TLabel;
luAlbums: TComboBox;
cbFavorite: TCheckBox;
Label3: TLabel;
textBox: TMemo;
btnSave: TButton;
btnDelete: TButton;
Label4: TLabel;
edYear: TEdit;
btnApplyBand: TButton;
btnApplySong: TButton;
Label5: TLabel;
luColor: TColorBox;
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure luBandsChange(Sender: TObject);
procedure luAlbumsChange(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
procedure btnDeleteClick(Sender: TObject);
procedure btnApplyBandClick(Sender: TObject);
procedure btnApplySongClick(Sender: TObject);
procedure DetermineNeedSave(Sender: TObject);
private
{ Private declarations }
oldBandSelection, oldAlbumSelection: String;
oldCbSelection, needSave: Boolean;
oldTbText: TStringList;
public
{ Public declarations }
end;
var
fManageAlbum: TfManageAlbum;
implementation
{$R *.dfm}
procedure TfManageAlbum.FormCreate(Sender: TObject);
var
listOfBands: TStringList;
bandAt: string;
begin
//put this dialog in the exact middle of the main window
Top := fMain.Top + Trunc(fMain.Height / 2) - Trunc(Height / 2);
Left := fMain.Left + Trunc(fMain.Width / 2) - Trunc(Width / 2);
//is there a better way to remove default text from a text box?
textBox.Text := '';
oldBandSelection := '';
oldAlbumSelection := '';
oldCbSelection := false;
oldTbText := TStringList.Create;
needSave := false;
//add list of bands to the dropdown after sorting, if there are any bands
if dm.bandCount > 0 then
begin
listOfBands := dm.GetSortedBands;
for bandAt in listOfBands do
begin
luBands.Items.Add(bandAt);
end;
end;
end;
procedure TfManageAlbum.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if needSave and (messageDlg('Your changes have not been saved. Continue?',
mtConfirmation, [mbYes, mbNo], 0, mbYes) <> mrYes) then
CanClose := false;
end;
procedure TfManageAlbum.FormClose(Sender: TObject; var Action: TCloseAction);
begin
fMain.RefreshGrid;
//don't know if this is needed, maybe add it to all other dialogs as wel
Action := caFree;
fManageAlbum := nil;
end;
procedure TfManageAlbum.luBandsChange(Sender: TObject);
var
albumList: TStringList;
albumAt: String;
begin
if (oldBandSelection <> '') and needSave then
begin
if messageDlg('Your changes have not been saved. Continue?',
mtConfirmation, [mbYes, mbNo], 0, mbYes) <> mrYes then
begin
luBands.ItemIndex := luBands.Items.IndexOf(oldBandSelection);
Exit;
end;
end;
//somewhat confusing, i guess oldBandSelection is now the CURRENT selection
oldBandSelection := luBands.Items[luBands.ItemIndex];
oldAlbumSelection := '';
//how handle the album lookup box.
//first, clear it out entirely and add back in the N/A
luAlbums.Items.Clear;
luAlbums.ItemIndex := -1;
luAlbums.ClearSelection;
luAlbums.Text := '';
luAlbums.Items.Add('N/A');
//put that band's albums in the album lookup box after sorting
albumList := dm.GetSortedAlbumsOfBand(oldBandSelection);
for albumAt in albumList do
begin
//don't add the N/A album, because we already added it above
if albumAt <> 'N/A' then
luAlbums.Items.Add(albumAt);
end;
//also clear out all the controls
cbFavorite.Checked := false;
edYear.Clear;
textBox.Clear;
oldAlbumSelection := '';
needSave := false;
end;
procedure TfManageAlbum.luAlbumsChange(Sender: TObject);
begin
if (oldAlbumSelection <> '') and needSave then
begin
if messageDlg('Your changes have not been saved. Continue?',
mtConfirmation, [mbYes, mbNo], 0, mbYes) <> mrYes then
begin
luAlbums.ItemIndex := luAlbums.Items.IndexOf(oldAlbumSelection);
Exit;
end;
end;
oldAlbumSelection := luAlbums.Items[luAlbums.ItemIndex];
cbFavorite.Checked := dm.bands[oldBandSelection].albums[oldAlbumSelection].isFavorite;
luColor.Selected := dm.bands[oldBandSelection].albums[oldAlbumSelection].color;
textBox.Clear;
textBox.Lines := dm.bands[oldBandSelection].albums[oldAlbumSelection].tags;
edYear.Clear;
edYear.Text := IntToStr(dm.bands[oldBandSelection].albums[oldAlbumSelection].year);
needSave := false;
end;
procedure TfManageAlbum.btnDeleteClick(Sender: TObject);
var
albumAt: String;
begin
if not dm.bands.ContainsKey(oldBandSelection) then
begin
messageDlg('Please select a band.', mtWarning, [mbok], 0, mbOk);
Exit;
end;
if not dm.bands[oldBandSelection].albums.ContainsKey(oldAlbumSelection) then
begin
messageDlg('Please select an album.', mtWarning, [mbOk], 0, mbOk);
Exit;
end;
if messageDlg('Are you sure you want to delete ' + oldAlbumSelection + ' from the ' +
'system?', mtConfirmation, [mbYes, mbNo], 0, mbNo) = mrYes then
begin
//we'll need to do some freeing at some point in the future
dm.bands[oldBandSelection].albums.Remove(oldAlbumSelection);
needSave := false;
oldAlbumSelection := '';
//need to clear out the removed album from the lookup's list of items.
//best way to do this seems to be constructing a new list without the old one
luAlbums.Items.Clear;
luAlbums.Items.Add('N/A');
for albumAt in dm.GetSortedAlbumsOfBand(oldBandSelection) do
luAlbums.Items.Add(albumAt);
luAlbums.ItemIndex := -1;
luAlbums.Text := '';
luAlbums.DroppedDown := true;
fMain.manageneedSave := true;
end;
end;
procedure TfManageAlbum.btnSaveClick(Sender: TObject);
var
album: TAlbum;
begin
if not dm.bands.ContainsKey(oldBandSelection) then
begin
messageDlg('Please select a band.', mtWarning, [mbok], 0, mbOk);
Exit;
end;
if not dm.bands[oldBandSelection].albums.ContainsKey(oldAlbumSelection) then
begin
messageDlg('Please select an album.', mtWarning, [mbOk], 0, mbOk);
Exit;
end;
album := dm.bands[oldBandSelection].albums[oldAlbumSelection];
album.isFavorite := cbFavorite.Checked;
album.color := luColor.Selected;
album.year := StrToInt(edYear.Text); //set to number only
album.tags.Clear;
album.tags.Assign(textBox.Lines);
if needSave then
fMain.manageNeedSave := true;
needSave := false;
messageDlg('Saved.', mtInformation, [mbOk], 0, mbOk);
luAlbums.DroppedDown := true;
end;
procedure TfManageAlbum.DetermineNeedSave(Sender: TObject);
var
album: TAlbum;
total: Integer;
begin
if (oldBandSelection = '') or (oldAlbumSelection = '') then
Exit;
album := dm.bands[oldBandSelection].albums[oldAlbumSelection];
total := 0;
if album.isFavorite <> cbFavorite.Checked then
Inc(total);
if textBox.Lines <> album.tags then
Inc(total);
if luColor.Selected <> album.color then
Inc(total);
if IntToStr(album.year) <> edYear.Text then
Inc(total);
//if any value has changed, need save. otherwise, don't need save
needSave := total > 0;
end;
procedure TfManageAlbum.btnApplyBandClick(Sender: TObject);
begin
if not dm.bands.ContainsKey(oldBandSelection) then
begin
messageDlg('Please select a band.', mtWarning, [mbok], 0, mbOk);
Exit;
end;
if not dm.bands[oldBandSelection].albums.ContainsKey(oldAlbumSelection) then
begin
messageDlg('Please select an album.', mtWarning, [mbOk], 0, mbOk);
Exit;
end;
if messageDlg('Apply these tags to ' + oldBandSelection + '?' + #13#10 +
'Note: This will not apply to any of that band''s songs.', mtConfirmation,
[mbYes, mbNo], 0, mbYes) = mrNo then
Exit;
dm.bands[oldBandSelection].AddTags(textBox.Lines);
messageDlg('Done.', mtInformation, [mbOk], 0, mbOk);
end;
procedure TfManageAlbum.btnApplySongClick(Sender: TObject);
var
songAt: TSong;
begin
if not dm.bands.ContainsKey(oldBandSelection) then
begin
messageDlg('Please select a band.', mtWarning, [mbok], 0, mbOk);
Exit;
end;
if not dm.bands[oldBandSelection].albums.ContainsKey(oldAlbumSelection) then
begin
messageDlg('Please select an album.', mtWarning, [mbOk], 0, mbOk);
Exit;
end;
if messageDlg('Apply these tags to ' + oldAlbumSelection + '''s ' +
IntToStr(dm.bands[oldBandSelection].albums[oldAlbumSelection].songs.Count)
+ ' songs?' + #13#10, mtConfirmation, [mbYes, mbNo], 0, mbYes) = mrNo then
Exit;
for songAt in dm.bands[oldBandSelection].albums[oldAlbumSelection].songs.Values do
songAt.AddTags(textBox.Lines);
messageDlg('Done.', mtInformation, [mbOk], 0, mbOk);
end;
end.