From 41b464953a03751c5adccef3b3e9236f0eb48784 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Mon, 3 Sep 2012 17:38:26 +0100 Subject: Load tags seperately from saves in search diff --git a/src/search/SearchModel.cpp b/src/search/SearchModel.cpp index f6a9480..b436e60 100644 --- a/src/search/SearchModel.cpp +++ b/src/search/SearchModel.cpp @@ -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 * 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 > * 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 * tempSaveList; - pthread_join(updateSaveListThread, (void**)(&tempInformation)); - - - saveList = *(vector*)tempInformation[0]; - delete (vector*)tempInformation[0]; + vector * tempSaveList; + pthread_join(updateSaveListThread, (void**)&tempSaveList); - if(tempInformation[1]) + if(tempSaveList) { - tagList = *(vector >*)tempInformation[1]; - delete (vector >*)tempInformation[1]; + saveList = *tempSaveList; + delete tempSaveList; } - else - { - tagList = vector >(); - } - - delete[] tempInformation; if(!saveList.size()) { @@ -147,6 +154,23 @@ void SearchModel::Update() notifySaveListChanged(); } } + if(updateTagListWorking) + { + if(updateTagListFinished) + { + updateTagListWorking = false; + + vector > * 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++) diff --git a/src/search/SearchModel.h b/src/search/SearchModel.h index 08f04b7..4ac41c1 100644 --- a/src/search/SearchModel.h +++ b/src/search/SearchModel.h @@ -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 GetSaveList(); diff --git a/src/search/SearchView.cpp b/src/search/SearchView.cpp index 422a040..1211cfc 100644 --- a/src/search/SearchView.cpp +++ b/src/search/SearchView.cpp @@ -381,7 +381,7 @@ void SearchView::CheckAccess() } } -void SearchView::NotifySaveListChanged(SearchModel * sender) +void SearchView::NotifyTagListChanged(SearchModel * sender) { int i = 0; int buttonWidth, buttonHeight, saveX = 0, saveY = 0, savesX = 5, savesY = 4, buttonPadding = 1; @@ -390,9 +390,7 @@ void SearchView::NotifySaveListChanged(SearchModel * sender) int tagWidth, tagHeight, tagX = 0, tagY = 0, tagsX = 6, tagsY = 4, tagPadding = 1; int tagAreaWidth, tagAreaHeight, tagXOffset, tagYOffset; - vector saves = sender->GetSaveList(); vector > tags = sender->GetTagList(); - //string messageOfTheDay = sender->GetMessageOfTheDay(); RemoveComponent(motdLabel); motdLabel->SetParentWindow(NULL); @@ -400,19 +398,113 @@ void SearchView::NotifySaveListChanged(SearchModel * sender) 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 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; + 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 saves = sender->GetSaveList(); + //string messageOfTheDay = sender->GetMessageOfTheDay(); + 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 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) diff --git a/src/search/SearchView.h b/src/search/SearchView.h index 899a993..05ecde2 100644 --- a/src/search/SearchView.h +++ b/src/search/SearchView.h @@ -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); -- cgit v0.9.2-21-gd62e