summaryrefslogtreecommitdiff
path: root/src/search
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-01-21 18:51:28 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-01-21 18:51:28 (GMT)
commit1cee908c165ead0fcecba4519d4584f3935988b5 (patch)
treec03981effdc5543fd9e50f70aab678c9631dbbcd /src/search
parent8ec6aae617525d13697d1c2a612ac37be0f341d5 (diff)
downloadpowder-1cee908c165ead0fcecba4519d4584f3935988b5.zip
powder-1cee908c165ead0fcecba4519d4584f3935988b5.tar.gz
Fix thumbnail crash, turns out SaveButton was storing a Thumbnail pointer, not a Thumbnail as I thought
Diffstat (limited to 'src/search')
-rw-r--r--src/search/SearchController.cpp33
-rw-r--r--src/search/SearchController.h4
-rw-r--r--src/search/SearchModel.cpp48
-rw-r--r--src/search/SearchModel.h18
-rw-r--r--src/search/SearchView.cpp110
-rw-r--r--src/search/SearchView.h6
-rw-r--r--src/search/Thumbnail.h1
7 files changed, 201 insertions, 19 deletions
diff --git a/src/search/SearchController.cpp b/src/search/SearchController.cpp
index a9b346f..7103591 100644
--- a/src/search/SearchController.cpp
+++ b/src/search/SearchController.cpp
@@ -11,7 +11,7 @@ SearchController::SearchController()
searchModel->AddObserver(searchView);
searchView->AttachController(this);
- searchModel->UpdateSaveList("");
+ searchModel->UpdateSaveList(1, "");
//Set up interface
//windowPanel.AddChild();
@@ -25,5 +25,34 @@ SearchController::~SearchController()
void SearchController::DoSearch(std::string query)
{
- searchModel->UpdateSaveList(query);
+ searchModel->UpdateSaveList(1, query);
+}
+
+void SearchController::PrevPage()
+{
+ if(searchModel->GetPageNum()>1)
+ searchModel->UpdateSaveList(searchModel->GetPageNum()-1, searchModel->GetLastQuery());
+}
+
+void SearchController::NextPage()
+{
+ if(searchModel->GetPageNum() <= searchModel->GetPageCount())
+ searchModel->UpdateSaveList(searchModel->GetPageNum()+1, searchModel->GetLastQuery());
+}
+
+void SearchController::ChangeSort()
+{
+ if(searchModel->GetSort() == "date")
+ {
+ searchModel->SetSort("votes");
+ }
+ else
+ {
+ searchModel->SetSort("date");
+ }
+}
+
+void SearchController::ShowOwn(bool show)
+{
+
}
diff --git a/src/search/SearchController.h b/src/search/SearchController.h
index cbef93a..c9fdb21 100644
--- a/src/search/SearchController.h
+++ b/src/search/SearchController.h
@@ -16,6 +16,10 @@ public:
~SearchController();
SearchView * GetView() { return searchView; }
void DoSearch(std::string query);
+ void NextPage();
+ void PrevPage();
+ void ChangeSort();
+ void ShowOwn(bool show);
};
#endif // SEARCHCONTROLLER_H
diff --git a/src/search/SearchModel.cpp b/src/search/SearchModel.cpp
index 3b2d0e0..adb8cad 100644
--- a/src/search/SearchModel.cpp
+++ b/src/search/SearchModel.cpp
@@ -3,26 +3,30 @@
#include "client/Client.h"
-SearchModel::SearchModel()
+SearchModel::SearchModel():
+ currentSort("votes"),
+ showOwn(false)
{
}
-void SearchModel::UpdateSaveList(std::string query)
+void SearchModel::UpdateSaveList(int pageNumber, std::string query)
{
+ lastQuery = query;
lastError = "";
saveList.clear();
+ currentPage = 1;
+ resultCount = 0;
notifySaveListChanged();
- vector<Save*> * tempSaveList = Client::Ref().SearchSaves(0, 12, query, "");
+ notifyPageChanged();
+ vector<Save*> * tempSaveList = Client::Ref().SearchSaves((pageNumber-1)*12, 12, query, currentSort, resultCount);
saveList = *tempSaveList;
delete tempSaveList;
if(!saveList.size())
{
lastError = Client::Ref().GetLastError();
}
- /*for(int i = 0; i < 16; i++)
- {
- saveList.push_back(Save(2198, 2333, 315, "dima-gord", "Destroyable city 5 (wth metro)"));
- }*/
+ currentPage = pageNumber;
+ notifyPageChanged();
notifySaveListChanged();
}
@@ -35,6 +39,9 @@ void SearchModel::AddObserver(SearchView * observer)
{
observers.push_back(observer);
observer->NotifySaveListChanged(this);
+ observer->NotifyPageChanged(this);
+ observer->NotifySortChanged(this);
+ observer->NotifyShowOwnChanged(this);
}
void SearchModel::notifySaveListChanged()
@@ -45,3 +52,30 @@ void SearchModel::notifySaveListChanged()
cObserver->NotifySaveListChanged(this);
}
}
+
+void SearchModel::notifyPageChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ SearchView* cObserver = observers[i];
+ cObserver->NotifyPageChanged(this);
+ }
+}
+
+void SearchModel::notifySortChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ SearchView* cObserver = observers[i];
+ cObserver->NotifySortChanged(this);
+ }
+}
+
+void SearchModel::notifyShowOwnChanged()
+{
+ for(int i = 0; i < observers.size(); i++)
+ {
+ SearchView* cObserver = observers[i];
+ cObserver->NotifyShowOwnChanged(this);
+ }
+}
diff --git a/src/search/SearchModel.h b/src/search/SearchModel.h
index a2d73cb..dbd6757 100644
--- a/src/search/SearchModel.h
+++ b/src/search/SearchModel.h
@@ -3,6 +3,7 @@
#include <vector>
#include <string>
+#include <math.h>
#include "Save.h"
#include "SearchView.h"
@@ -12,16 +13,31 @@ class SearchView;
class SearchModel
{
private:
+ string currentSort;
+ string lastQuery;
string lastError;
vector<SearchView*> observers;
vector<Save*> saveList;
+ int currentPage;
+ int resultCount;
+ bool showOwn;
void notifySaveListChanged();
+ void notifyPageChanged();
+ void notifySortChanged();
+ void notifyShowOwnChanged();
public:
SearchModel();
void AddObserver(SearchView * observer);
- void UpdateSaveList(std::string query);
+ void UpdateSaveList(int pageNumber, std::string query);
vector<Save*> GetSaveList();
string GetLastError() { return lastError; }
+ int GetPageCount() { return max(1, (int)(ceil(resultCount/16))); }
+ int GetPageNum() { return currentPage; }
+ std::string GetLastQuery() { return lastQuery; }
+ void SetSort(string sort) { currentSort = sort; UpdateSaveList(currentPage, lastQuery); notifySortChanged(); }
+ string GetSort() { return currentSort; }
+ void SetShowOwn(bool show) { showOwn = show; UpdateSaveList(currentPage, lastQuery); notifyShowOwnChanged(); }
+ bool GetShowOwn() { return showOwn; }
};
#endif // SEARCHMODEL_H
diff --git a/src/search/SearchView.cpp b/src/search/SearchView.cpp
index 53ab443..ca848b5 100644
--- a/src/search/SearchView.cpp
+++ b/src/search/SearchView.cpp
@@ -1,4 +1,7 @@
+#include <sstream>
+
#include "SearchView.h"
+#include "client/Client.h"
#include "interface/SaveButton.h"
#include "interface/Label.h"
#include "interface/Textbox.h"
@@ -9,8 +12,10 @@ SearchView::SearchView():
saveButtons(vector<ui::SaveButton*>()),
errorLabel(NULL)
{
+
nextButton = new ui::Button(ui::Point(XRES+BARSIZE-52, YRES+MENUSIZE-18), ui::Point(50, 16), "Next \x95");
previousButton = new ui::Button(ui::Point(1, YRES+MENUSIZE-18), ui::Point(50, 16), "\x96 Prev");
+ infoLabel = new ui::Label(ui::Point(51, YRES+MENUSIZE-18), ui::Point(XRES+BARSIZE-102, 16), "Loading...");
class SearchAction : public ui::TextboxAction
{
@@ -26,11 +31,65 @@ SearchView::SearchView():
searchField->SetAlignment(AlignLeft, AlignBottom);
searchField->SetActionCallback(new SearchAction(this));
+ class SortAction : public ui::ButtonAction
+ {
+ SearchView * v;
+ public:
+ SortAction(SearchView * _v) { v = _v; }
+ void ActionCallback(ui::Button * sender)
+ {
+ v->c->ChangeSort();
+ }
+ };
+ sortButton = new ui::Button(ui::Point(XRES+BARSIZE-50-50-16-10, 10), ui::Point(50, 16), "Sort");
+ sortButton->SetActionCallback(new SortAction(this));
+ sortButton->SetAlignment(AlignLeft, AlignBottom);
+ AddComponent(sortButton);
+
+ class MyOwnAction : public ui::ButtonAction
+ {
+ SearchView * v;
+ public:
+ MyOwnAction(SearchView * _v) { v = _v; }
+ void ActionCallback(ui::Button * sender)
+ {
+ v->c->ShowOwn(sender->GetToggleState());
+ }
+ };
+ ownButton = new ui::Button(ui::Point(XRES+BARSIZE-50-16-10, 10), ui::Point(50, 16), "My Own");
+ ownButton->SetTogglable(true);
+ ownButton->SetActionCallback(new MyOwnAction(this));
+ ownButton->SetAlignment(AlignLeft, AlignBottom);
+ AddComponent(ownButton);
+
+ class NextPageAction : public ui::ButtonAction
+ {
+ SearchView * v;
+ public:
+ NextPageAction(SearchView * _v) { v = _v; }
+ void ActionCallback(ui::Button * sender)
+ {
+ v->c->NextPage();
+ }
+ };
+ nextButton->SetActionCallback(new NextPageAction(this));
nextButton->SetAlignment(AlignRight, AlignBottom);
+ class PrevPageAction : public ui::ButtonAction
+ {
+ SearchView * v;
+ public:
+ PrevPageAction(SearchView * _v) { v = _v; }
+ void ActionCallback(ui::Button * sender)
+ {
+ v->c->PrevPage();
+ }
+ };
+ previousButton->SetActionCallback(new PrevPageAction(this));
previousButton->SetAlignment(AlignLeft, AlignBottom);
AddComponent(nextButton);
AddComponent(previousButton);
AddComponent(searchField);
+ AddComponent(infoLabel);
ui::Label * searchPrompt = new ui::Label(ui::Point(10, 10), ui::Point(50, 16), "Search:");
searchPrompt->SetAlignment(AlignLeft, AlignBottom);
@@ -46,6 +105,39 @@ SearchView::~SearchView()
{
}
+void SearchView::NotifySortChanged(SearchModel * sender)
+{
+ sortButton->SetText("Sort: "+sender->GetSort());
+}
+
+void SearchView::NotifyShowOwnChanged(SearchModel * sender)
+{
+ sortButton->SetToggleState(sender->GetShowOwn());
+}
+
+void SearchView::NotifyPageChanged(SearchModel * sender)
+{
+ std::stringstream pageInfo;
+ pageInfo << "Page " << sender->GetPageNum() << " of " << sender->GetPageCount();
+ infoLabel->SetText(pageInfo.str());
+ if(sender->GetPageNum() == 1)
+ {
+ previousButton->Visible = false;
+ }
+ else
+ {
+ previousButton->Visible = true;
+ }
+ if(sender->GetPageNum() == sender->GetPageCount())
+ {
+ nextButton->Visible = false;
+ }
+ else
+ {
+ nextButton->Visible = true;
+ }
+}
+
void SearchView::NotifySaveListChanged(SearchModel * sender)
{
int i = 0;
@@ -53,6 +145,13 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
int buttonAreaWidth, buttonAreaHeight, buttonXOffset, buttonYOffset;
vector<Save*> saves = sender->GetSaveList();
+ Client::Ref().ClearThumbnailRequests();
+ for(i = 0; i < saveButtons.size(); i++)
+ {
+ RemoveComponent(saveButtons[i]);
+ delete saveButtons[i];
+ }
+ saveButtons.clear();
if(!saves.size())
{
if(!errorLabel)
@@ -61,9 +160,9 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
AddComponent(errorLabel);
}
if(sender->GetLastError().length())
- errorLabel->SetText(sender->GetLastError());
+ errorLabel->SetText("\bo" + sender->GetLastError());
else
- errorLabel->SetText("No saves found");
+ errorLabel->SetText("\boNo saves found");
}
else
{
@@ -79,13 +178,6 @@ void SearchView::NotifySaveListChanged(SearchModel * sender)
buttonAreaHeight = Size.Y - buttonYOffset - 18;
buttonWidth = (buttonAreaWidth/savesX) - buttonPadding*2;
buttonHeight = (buttonAreaHeight/savesY) - buttonPadding*2;
- for(i = 0; i < saveButtons.size(); i++)
- {
- RemoveComponent(saveButtons[i]);
-
- delete saveButtons[i];
- }
- saveButtons.clear();
for(i = 0; i < saves.size(); i++)
{
if(saveX == savesX)
diff --git a/src/search/SearchView.h b/src/search/SearchView.h
index 8777c30..370877e 100644
--- a/src/search/SearchView.h
+++ b/src/search/SearchView.h
@@ -22,9 +22,15 @@ private:
ui::Button * previousButton;
ui::Label * errorLabel;
ui::Textbox * searchField;
+ ui::Label * infoLabel;
+ ui::Button * sortButton;
+ ui::Button * ownButton;
void doSearch();
public:
void NotifySaveListChanged(SearchModel * sender);
+ void NotifyPageChanged(SearchModel * sender);
+ void NotifySortChanged(SearchModel * sender);
+ void NotifyShowOwnChanged(SearchModel * sender);
SearchView();
virtual ~SearchView();
void AttachController(SearchController * _c) { c = _c; }
diff --git a/src/search/Thumbnail.h b/src/search/Thumbnail.h
index 3b865ca..6311a23 100644
--- a/src/search/Thumbnail.h
+++ b/src/search/Thumbnail.h
@@ -1,6 +1,7 @@
#ifndef THUMBNAIL_H
#define THUMBNAIL_H
+#include <iostream>
#include "Graphics.h"
#include "interface/Point.h"