Load tags seperately from saves in search

This commit is contained in:
Simon Robertshaw 2012-09-03 17:38:26 +01:00
parent 7d9f8a0b1d
commit 41b464953a
4 changed files with 182 additions and 106 deletions

View File

@ -22,6 +22,11 @@ void SearchModel::SetShowTags(bool show)
showTags = show;
}
bool SearchModel::GetShowTags()
{
return showTags;
}
void * SearchModel::updateSaveListTHelper(void * obj)
{
return ((SearchModel *)obj)->updateSaveListT();
@ -36,26 +41,30 @@ void * SearchModel::updateSaveListT()
category = "Favourites";
if(showOwn && Client::Ref().GetAuthUser().ID)
category = "by:"+Client::Ref().GetAuthUser().Username;
information[0] = Client::Ref().SearchSaves((currentPage-1)*20, 20, lastQuery, currentSort=="new"?"date":"votes", category, thResultCount);
if(showTags)
{
int tagResultCount;
information[1] = Client::Ref().GetTags(0, 24, "", tagResultCount);
}
else
{
information[1] = NULL;
}
vector<SaveInfo*> * saveList = Client::Ref().SearchSaves((currentPage-1)*20, 20, lastQuery, currentSort=="new"?"date":"votes", category, thResultCount);
updateSaveListFinished = true;
return information;
return saveList;
}
void * SearchModel::updateTagListTHelper(void * obj)
{
return ((SearchModel *)obj)->updateTagListT();
}
void * SearchModel::updateTagListT()
{
int tagResultCount;
std::vector<std::pair<std::string, int> > * tagList = Client::Ref().GetTags(0, 24, "", tagResultCount);
updateTagListFinished = true;
return tagList;
}
void SearchModel::UpdateSaveList(int pageNumber, std::string query)
{
//Threading
if(!updateSaveListWorking)
if(!updateSaveListWorking && !updateTagListWorking)
{
lastQuery = query;
lastError = "";
@ -63,15 +72,24 @@ void SearchModel::UpdateSaveList(int pageNumber, std::string query)
saveList.clear();
//resultCount = 0;
currentPage = pageNumber;
notifySaveListChanged();
notifyPageChanged();
selected.clear();
notifySelectedChanged();
if(pageNumber == 1 && !showOwn && !showFavourite && currentSort == "best" && query == "")
SetShowTags(true);
else
SetShowTags(false);
notifySaveListChanged();
notifyTagListChanged();
notifyPageChanged();
selected.clear();
notifySelectedChanged();
if(GetShowTags() && !tagList.size())
{
updateTagListFinished = false;
updateTagListWorking = true;
pthread_create(&updateTagListThread, 0, &SearchModel::updateTagListTHelper, this);
}
updateSaveListFinished = false;
updateSaveListWorking = true;
@ -116,26 +134,15 @@ void SearchModel::Update()
updateSaveListWorking = false;
lastError = "";
saveListLoaded = true;
void ** tempInformation;
//vector<SaveInfo*> * tempSaveList;
pthread_join(updateSaveListThread, (void**)(&tempInformation));
vector<SaveInfo*> * tempSaveList;
pthread_join(updateSaveListThread, (void**)&tempSaveList);
saveList = *(vector<SaveInfo*>*)tempInformation[0];
delete (vector<SaveInfo*>*)tempInformation[0];
if(tempInformation[1])
if(tempSaveList)
{
tagList = *(vector<pair<string, int> >*)tempInformation[1];
delete (vector<pair<string, int> >*)tempInformation[1];
saveList = *tempSaveList;
delete tempSaveList;
}
else
{
tagList = vector<pair<string, int> >();
}
delete[] tempInformation;
if(!saveList.size())
{
@ -147,6 +154,23 @@ void SearchModel::Update()
notifySaveListChanged();
}
}
if(updateTagListWorking)
{
if(updateTagListFinished)
{
updateTagListWorking = false;
vector<pair<string, int> > * tempTagList;
pthread_join(updateTagListThread, (void**)&tempTagList);
if(tempTagList)
{
tagList = *tempTagList;
delete tempTagList;
}
notifyTagListChanged();
}
}
}
void SearchModel::AddObserver(SearchView * observer)
@ -156,6 +180,7 @@ void SearchModel::AddObserver(SearchView * observer)
observer->NotifyPageChanged(this);
observer->NotifySortChanged(this);
observer->NotifyShowOwnChanged(this);
observer->NotifyTagListChanged(this);
}
void SearchModel::SelectSave(int saveID)
@ -197,6 +222,15 @@ void SearchModel::notifySaveListChanged()
}
}
void SearchModel::notifyTagListChanged()
{
for(int i = 0; i < observers.size(); i++)
{
SearchView* cObserver = observers[i];
cObserver->NotifyTagListChanged(this);
}
}
void SearchModel::notifyPageChanged()
{
for(int i = 0; i < observers.size(); i++)

View File

@ -30,6 +30,7 @@ private:
bool showFavourite;
bool showTags;
void notifySaveListChanged();
void notifyTagListChanged();
void notifySelectedChanged();
void notifyPageChanged();
void notifySortChanged();
@ -43,11 +44,18 @@ private:
pthread_t updateSaveListThread;
static void * updateSaveListTHelper(void * obj);
void * updateSaveListT();
bool updateTagListWorking;
volatile bool updateTagListFinished;
pthread_t updateTagListThread;
static void * updateTagListTHelper(void * obj);
void * updateTagListT();
public:
SearchModel();
virtual ~SearchModel();
void SetShowTags(bool show);
bool GetShowTags();
void AddObserver(SearchView * observer);
void UpdateSaveList(int pageNumber, std::string query);
vector<SaveInfo*> GetSaveList();

View File

@ -381,6 +381,111 @@ void SearchView::CheckAccess()
}
}
void SearchView::NotifyTagListChanged(SearchModel * sender)
{
int i = 0;
int buttonWidth, buttonHeight, saveX = 0, saveY = 0, savesX = 5, savesY = 4, buttonPadding = 1;
int buttonAreaWidth, buttonAreaHeight, buttonXOffset, buttonYOffset;
int tagWidth, tagHeight, tagX = 0, tagY = 0, tagsX = 6, tagsY = 4, tagPadding = 1;
int tagAreaWidth, tagAreaHeight, tagXOffset, tagYOffset;
vector<pair<string, int> > tags = sender->GetTagList();
RemoveComponent(motdLabel);
motdLabel->SetParentWindow(NULL);
RemoveComponent(tagsLabel);
tagsLabel->SetParentWindow(NULL);
for(i = 0; i < tagButtons.size(); i++)
{
RemoveComponent(tagButtons[i]);
delete tagButtons[i];
}
tagButtons.clear();
buttonYOffset = 28;
buttonXOffset = buttonPadding;
buttonAreaWidth = Size.X;
buttonAreaHeight = Size.Y - buttonYOffset - 18;
if(sender->GetShowTags())
{
buttonYOffset += (buttonAreaHeight/savesY) - buttonPadding*2;
buttonAreaHeight = Size.Y - buttonYOffset - 18;
savesY--;
tagXOffset = tagPadding;
tagYOffset = 60;
tagAreaWidth = Size.X;
tagAreaHeight = ((buttonAreaHeight/savesY) - buttonPadding*2)-(tagYOffset-28)-5;
tagWidth = (tagAreaWidth/tagsX) - tagPadding*2;
tagHeight = (tagAreaHeight/tagsY) - tagPadding*2;
AddComponent(tagsLabel);
tagsLabel->Position.Y = tagYOffset-16;
AddComponent(motdLabel);
motdLabel->Position.Y = tagYOffset-28;
}
class TagAction: public ui::ButtonAction
{
SearchView * v;
std::string tag;
public:
TagAction(SearchView * v, std::string tag) : v(v), tag(tag) {}
virtual void ActionCallback(ui::Button * sender)
{
v->Search(tag);
}
};
if(sender->GetShowTags())
{
for(i = 0; i < tags.size(); i++)
{
int maxTagVotes = tags[0].second;
pair<string, int> tag = tags[i];
if(tagX == tagsX)
{
if(tagY == tagsY-1)
break;
tagX = 0;
tagY++;
}
int tagAlpha = 192;
if (maxTagVotes)
tagAlpha = 127+(128*tag.second)/maxTagVotes;
ui::Button * tagButton;
tagButton = new ui::Button(
ui::Point(
tagXOffset + tagPadding + tagX*(tagWidth+tagPadding*2),
tagYOffset + tagPadding + tagY*(tagHeight+tagPadding*2)
),
ui::Point(tagWidth, tagHeight),
tag.first
);
tagButton->SetActionCallback(new TagAction(this, tag.first));
tagButton->Appearance.BorderInactive = ui::Colour(0, 0, 0);
tagButton->Appearance.BorderHover = ui::Colour(0, 0, 0);
tagButton->Appearance.BorderActive = ui::Colour(0, 0, 0);
tagButton->Appearance.BackgroundHover = ui::Colour(0, 0, 0);
tagButton->Appearance.TextInactive = ui::Colour(tagAlpha, tagAlpha, tagAlpha);
tagButton->Appearance.TextHover = ui::Colour((tagAlpha*5)/6, (tagAlpha*5)/6, tagAlpha);
AddComponent(tagButton);
tagButtons.push_back(tagButton);
tagX++;
}
}
}
void SearchView::NotifySaveListChanged(SearchModel * sender)
{
int i = 0;
@ -391,28 +496,15 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
int tagAreaWidth, tagAreaHeight, tagXOffset, tagYOffset;
vector<SaveInfo*> saves = sender->GetSaveList();
vector<pair<string, int> > tags = sender->GetTagList();
//string messageOfTheDay = sender->GetMessageOfTheDay();
RemoveComponent(motdLabel);
motdLabel->SetParentWindow(NULL);
RemoveComponent(tagsLabel);
tagsLabel->SetParentWindow(NULL);
Client::Ref().ClearThumbnailRequests();
for(i = 0; i < saveButtons.size(); i++)
{
RemoveComponent(saveButtons[i]);
delete saveButtons[i];
}
for(i = 0; i < tagButtons.size(); i++)
{
RemoveComponent(tagButtons[i]);
delete tagButtons[i];
}
saveButtons.clear();
tagButtons.clear();
if(!sender->GetSavesLoaded())
{
nextButton->Enabled = false;
@ -461,7 +553,7 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
buttonAreaWidth = Size.X;
buttonAreaHeight = Size.Y - buttonYOffset - 18;
if(tags.size())
if(sender->GetShowTags())
{
buttonYOffset += (buttonAreaHeight/savesY) - buttonPadding*2;
buttonAreaHeight = Size.Y - buttonYOffset - 18;
@ -473,12 +565,6 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
tagAreaHeight = ((buttonAreaHeight/savesY) - buttonPadding*2)-(tagYOffset-28)-5;
tagWidth = (tagAreaWidth/tagsX) - tagPadding*2;
tagHeight = (tagAreaHeight/tagsY) - tagPadding*2;
AddComponent(tagsLabel);
tagsLabel->Position.Y = tagYOffset-16;
AddComponent(motdLabel);
motdLabel->Position.Y = tagYOffset-28;
}
buttonWidth = (buttonAreaWidth/savesX) - buttonPadding*2;
@ -504,59 +590,6 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
v->Search("user:"+sender->GetSave()->GetUserName());
}
};
class TagAction: public ui::ButtonAction
{
SearchView * v;
std::string tag;
public:
TagAction(SearchView * v, std::string tag) : v(v), tag(tag) {}
virtual void ActionCallback(ui::Button * sender)
{
v->Search(tag);
}
};
for(i = 0; i < tags.size(); i++)
{
int maxTagVotes = tags[0].second;
pair<string, int> tag = tags[i];
if(tagX == tagsX)
{
if(tagY == tagsY-1)
break;
tagX = 0;
tagY++;
}
int tagAlpha = 192;
if (maxTagVotes)
tagAlpha = 127+(128*tag.second)/maxTagVotes;
ui::Button * tagButton;
tagButton = new ui::Button(
ui::Point(
tagXOffset + tagPadding + tagX*(tagWidth+tagPadding*2),
tagYOffset + tagPadding + tagY*(tagHeight+tagPadding*2)
),
ui::Point(tagWidth, tagHeight),
tag.first
);
tagButton->SetActionCallback(new TagAction(this, tag.first));
tagButton->Appearance.BorderInactive = ui::Colour(0, 0, 0);
tagButton->Appearance.BorderHover = ui::Colour(0, 0, 0);
tagButton->Appearance.BorderActive = ui::Colour(0, 0, 0);
tagButton->Appearance.BackgroundHover = ui::Colour(0, 0, 0);
tagButton->Appearance.TextInactive = ui::Colour(tagAlpha, tagAlpha, tagAlpha);
tagButton->Appearance.TextHover = ui::Colour((tagAlpha*5)/6, (tagAlpha*5)/6, tagAlpha);
AddComponent(tagButton);
tagButtons.push_back(tagButton);
tagX++;
}
for(i = 0; i < saves.size(); i++)
{
if(saveX == savesX)

View File

@ -50,6 +50,7 @@ private:
void clearSearch();
void doSearch();
public:
void NotifyTagListChanged(SearchModel * sender);
void NotifySaveListChanged(SearchModel * sender);
void NotifySelectedChanged(SearchModel * sender);
void NotifyPageChanged(SearchModel * sender);