15
15
#include < TextControl.h>
16
16
#include < TranslationUtils.h>
17
17
18
+ #include " Query.h"
19
+ #include " Utility.h"
20
+
18
21
19
22
static const uint32 kIconSize = 64 ;
23
+ static const uint32 kMaxColumns = 4 ;
20
24
21
25
static const uint32 kMsgAddImage = ' adIm' ;
22
26
static const uint32 kMsgQuery = ' quer' ;
27
+ static const uint32 kMsgSelect = ' sele' ;
28
+
29
+
30
+ class ImageLoaderResultListener : public ResultListener {
31
+ public:
32
+ ImageLoaderResultListener (
33
+ const BMessenger& target);
34
+ virtual ~ImageLoaderResultListener ();
35
+
36
+ virtual void AddResult (const BString& id,
37
+ const char * info, UrlMap urls);
38
+
39
+ private:
40
+ BMessenger fTarget ;
41
+ };
23
42
24
43
25
44
class IconView : public BView {
26
45
public:
27
- IconView (const char * id, BBitmap* bitmap);
46
+ IconView (const char * identifier,
47
+ const char * info, const char * url,
48
+ const char * imageUrl, BBitmap* bitmap);
28
49
virtual ~IconView ();
29
50
30
51
void SetSelected (bool selected);
@@ -35,26 +56,93 @@ class IconView : public BView {
35
56
36
57
private:
37
58
BString fIdentifier ;
59
+ BString fInfo ;
60
+ BString fUrl ;
61
+ BString fImageUrl ;
38
62
BBitmap* fBitmap ;
39
63
bool fSelected ;
40
64
float fDescent ;
41
65
};
42
66
43
67
68
+ // #pragma mark - ImageLoaderResultListener
69
+
70
+
71
+ ImageLoaderResultListener::ImageLoaderResultListener (const BMessenger& target)
72
+ :
73
+ fTarget(target)
74
+ {
75
+ }
76
+
77
+
78
+ ImageLoaderResultListener::~ImageLoaderResultListener ()
79
+ {
80
+ }
81
+
82
+
83
+ void
84
+ ImageLoaderResultListener::AddResult (const BString& id, const char * info,
85
+ UrlMap urls)
86
+ {
87
+ BBitmap* bitmap = NULL ;
88
+
89
+ UrlType thumbTypes[] = {kThumbImage , kLargeImage , kOriginalImage };
90
+ for (int index = 0 ; index < 3 ; index ++) {
91
+ UrlMap::const_iterator found = urls.find (thumbTypes[index ]);
92
+ if (found == urls.end ())
93
+ continue ;
94
+
95
+ const BUrl& url = found->second ;
96
+ printf (" LOAD: %s\n " , url.UrlString ().String ());
97
+
98
+ bitmap = FetchImage (url);
99
+ if (bitmap != NULL )
100
+ break ;
101
+
102
+ fprintf (stderr, " Could not load image: %s\n " ,
103
+ url.UrlString ().String ());
104
+ }
105
+ if (bitmap == NULL )
106
+ return ;
107
+
108
+ BMessage add (kMsgAddImage );
109
+ add.AddPointer (" image" , bitmap);
110
+ add.AddString (" id" , id);
111
+ add.AddString (" info" , info);
112
+ add.AddString (" url" , urls[kSource ].UrlString ().String ());
113
+
114
+ UrlType types[] = {kOriginalImage , kLargeImage };
115
+ for (int index = 0 ; index < 2 ; index ++) {
116
+ UrlMap::const_iterator found = urls.find (types[index ]);
117
+ if (found == urls.end ())
118
+ continue ;
119
+
120
+ add.AddString (" image_url" , found->second .UrlString ().String ());
121
+ break ;
122
+ }
123
+
124
+ fTarget .SendMessage (&add);
125
+ }
126
+
127
+
44
128
// #pragma mark - IconView
45
129
46
130
47
- IconView::IconView (const char * identifier, BBitmap* bitmap)
131
+ IconView::IconView (const char * identifier, const char * info, const char * url,
132
+ const char * imageUrl, BBitmap* bitmap)
48
133
:
49
134
BView(" image" , B_WILL_DRAW),
50
135
fIdentifier(identifier),
136
+ fInfo(info),
137
+ fUrl(url),
138
+ fImageUrl(imageUrl),
51
139
fBitmap(bitmap),
52
140
fSelected(false )
53
141
{
54
142
font_height fontHeight;
55
143
GetFontHeight (&fontHeight);
56
144
57
- float width = StringWidth (" WWWWWWWWWWWW " );
145
+ float width = StringWidth (" WWWWWWWWWWWWWWWWWWW " );
58
146
float height = kIconSize + be_plain_font->Size () / 12 .0f * 3 .0f
59
147
+ ceilf (fontHeight.ascent + fontHeight.descent );
60
148
@@ -91,7 +179,15 @@ IconView::IsSelected()
91
179
void
92
180
IconView::MouseDown (BPoint where)
93
181
{
94
- SetSelected (!IsSelected ());
182
+ int32 count = Window ()->CurrentMessage ()->GetInt32 (" clicks" , 1 );
183
+ if (count == 1 )
184
+ SetSelected (!IsSelected ());
185
+ else if (count == 2 ) {
186
+ BMessage select (kMsgSelect );
187
+ select .AddString (" id" , fIdentifier );
188
+ select .AddString (" image_url" , fImageUrl );
189
+ Window ()->PostMessage (&select );
190
+ }
95
191
}
96
192
97
193
@@ -107,9 +203,11 @@ IconView::Draw(BRect updateRect)
107
203
108
204
DrawBitmap (fBitmap , imageBounds);
109
205
110
- BString text = fIdentifier ;
111
- text << " " << fBitmap ->Bounds ().IntegerWidth () << " x "
112
- << fBitmap ->Bounds ().IntegerHeight ();
206
+ BString text = fInfo ;
207
+ if (text.IsEmpty ())
208
+ text = fIdentifier ;
209
+ else
210
+ text << " (" << fIdentifier << " )" ;
113
211
114
212
float width = StringWidth (text.String ());
115
213
if (width > bounds.Width ()) {
@@ -164,30 +262,12 @@ CoverWindow::CoverWindow()
164
262
.Add (fTitleControl )
165
263
.End ()
166
264
.Add (scrollView);
167
-
168
- // TODO: for testing purposes only!
169
- const char * files[] = {" /Media/Kram/Pics/band.jpg" ,
170
- " /boot/home/Desktop/CD Cover/Antemasque - dto.jpg" ,
171
- " /boot/home/Desktop/CD Cover/Danzig - dto.jpg" ,
172
- " /boot/home/Desktop/CD Cover/Kate Bush - Never For Ever.jpg" ,
173
- " /boot/home/Desktop/animation.gif" ,
174
- " /boot/home/develop/haiku/haiku/data/artwork/HAIKU logo - white on black - normal.png" ,
175
- " /boot/home/develop/haiku/haiku/data/artwork/HAIKU square - white on blue.png" , NULL };
176
- for (int i = 0 ; files[i] != NULL ; i++) {
177
- BBitmap* bitmap = BTranslationUtils::GetBitmapFile (files[i]);
178
- if (bitmap == NULL )
179
- continue ;
180
-
181
- BMessage add (kMsgAddImage );
182
- add.AddString (" amazon_id" , " BX348938" );
183
- add.AddPointer (" image" , bitmap);
184
- PostMessage (&add);
185
- }
186
265
}
187
266
188
267
189
268
CoverWindow::~CoverWindow ()
190
269
{
270
+ _AbortQuery ();
191
271
}
192
272
193
273
@@ -199,32 +279,57 @@ CoverWindow::MessageReceived(BMessage* message)
199
279
{
200
280
BBitmap* bitmap = (BBitmap*)message->GetPointer (" image" );
201
281
if (bitmap != NULL ) {
202
- const char * identifier = message->GetString (" amazon_id" );
203
- fMainView ->AddChild (new IconView (identifier, bitmap));
204
- // fMainView->GridLayout()->AddView(new IconView(bitmap), 0, 0);
282
+ const char * identifier = message->GetString (" id" );
283
+ const char * info = message->GetString (" info" );
284
+ const char * url = message->GetString (" url" );
285
+ const char * imageUrl = message->GetString (" image_url" );
286
+
287
+ IconView* view = new IconView (identifier, info, url, imageUrl,
288
+ bitmap);
289
+ fMainView ->GridLayout ()->AddView (view,
290
+ fCount % kMaxColumns , fCount / kMaxColumns );
291
+ fCount ++;
205
292
}
206
293
break ;
207
294
}
295
+ case kMsgSelect :
296
+ {
297
+ message->PrintToStream ();
298
+ break ;
299
+ }
208
300
case kMsgQuery :
209
301
{
210
- if (fQuery != NULL ) {
211
- // Abort
212
-
213
- // Remove previous results
214
- while (BView* view = fMainView ->ChildAt (0 )) {
215
- fMainView ->RemoveChild (view);
216
- delete view;
217
- }
218
- }
302
+ _AbortQuery ();
219
303
220
304
// Start new query
221
305
BString artist = fArtistControl ->Text ();
222
306
BString title = fTitleControl ->Text ();
223
- printf (" A: %s, T: %s\n " , artist.String (), title.String ());
307
+
308
+ fQuery = new Query (artist, title);
309
+ fQuery ->AddListener (new ImageLoaderResultListener (this ));
310
+ fQuery ->Run ();
224
311
break ;
225
312
}
226
313
default :
227
314
BWindow::MessageReceived (message);
228
315
break ;
229
316
}
230
317
}
318
+
319
+
320
+ void
321
+ CoverWindow::_AbortQuery ()
322
+ {
323
+ if (fQuery != NULL ) {
324
+ // TODO: abort asynchronously in message loop!
325
+ fQuery ->Abort ();
326
+ delete fQuery ;
327
+
328
+ // Remove previous results
329
+ while (BView* view = fMainView ->ChildAt (0 )) {
330
+ fMainView ->RemoveChild (view);
331
+ delete view;
332
+ }
333
+ fCount ++;
334
+ }
335
+ }
0 commit comments