Ensure errors are caught and displayed during tag operations. Fix some issues with the Tag ui. Fixes #157

This commit is contained in:
Simon Robertshaw 2012-09-05 19:47:03 +01:00
parent 09c266f252
commit 5da70ef8a6
7 changed files with 102 additions and 32 deletions

View File

@ -1821,8 +1821,19 @@ std::vector<std::string> * Client::RemoveTag(int saveID, std::string tag)
try try
{ {
std::istringstream dataStream(data); std::istringstream dataStream(data);
json::Array tagsArray; json::Object responseObject;
json::Reader::Read(tagsArray, dataStream); json::Reader::Read(responseObject, dataStream);
json::Number status = responseObject["Status"];
if(status.Value()==0)
{
json::String error = responseObject["Error"];
lastError = error.Value();
}
else
{
json::Array tagsArray = responseObject["Tags"];
tags = new std::vector<std::string>(); tags = new std::vector<std::string>();
@ -1832,6 +1843,7 @@ std::vector<std::string> * Client::RemoveTag(int saveID, std::string tag)
tags->push_back(tempTag.Value()); tags->push_back(tempTag.Value());
} }
} }
}
catch (json::Exception &e) catch (json::Exception &e)
{ {
lastError = "Could not read response"; lastError = "Could not read response";
@ -1870,8 +1882,19 @@ std::vector<std::string> * Client::AddTag(int saveID, std::string tag)
try try
{ {
std::istringstream dataStream(data); std::istringstream dataStream(data);
json::Array tagsArray; json::Object responseObject;
json::Reader::Read(tagsArray, dataStream); json::Reader::Read(responseObject, dataStream);
json::Number status = responseObject["Status"];
if(status.Value()==0)
{
json::String error = responseObject["Error"];
lastError = error.Value();
}
else
{
json::Array tagsArray = responseObject["Tags"];
tags = new std::vector<std::string>(); tags = new std::vector<std::string>();
@ -1881,6 +1904,7 @@ std::vector<std::string> * Client::AddTag(int saveID, std::string tag)
tags->push_back(tempTag.Value()); tags->push_back(tempTag.Value());
} }
} }
}
catch (json::Exception &e) catch (json::Exception &e)
{ {
lastError = "Could not read response"; lastError = "Could not read response";

View File

@ -113,7 +113,7 @@ public:
TagsCallback(GameController * cc_) { cc = cc_; } TagsCallback(GameController * cc_) { cc = cc_; }
virtual void ControllerExit() virtual void ControllerExit()
{ {
cc->gameModel->SetSave(new SaveInfo(*(cc->tagsWindow->GetSave()))); cc->gameView->NotifySaveChanged(cc->gameModel);
} }
}; };

View File

@ -874,15 +874,27 @@ void Graphics::draw_icon(int x, int y, Icon icon, unsigned char alpha, bool inve
case IconDelete: case IconDelete:
if(invert) if(invert)
{ {
drawchar(x, y, 0x86, 255, 55, 55, alpha); drawchar(x, y, 0x86, 159, 47, 31, alpha);
drawchar(x, y, 0x85, 0, 0, 0, alpha); drawchar(x, y, 0x85, 0, 0, 0, alpha);
} }
else else
{ {
drawchar(x, y, 0x86, 255, 55, 55, alpha); drawchar(x, y, 0x86, 159, 47, 31, alpha);
drawchar(x, y, 0x85, 255, 255, 255, alpha); drawchar(x, y, 0x85, 255, 255, 255, alpha);
} }
break; break;
case IconAdd:
if(invert)
{
drawchar(x, y, 0x86, 32, 144, 32, alpha);
drawchar(x, y, 0x89, 0, 0, 0, alpha);
}
else
{
drawchar(x, y, 0x86, 32, 144, 32, alpha);
drawchar(x, y, 0x89, 255, 255, 255, alpha);
}
break;
default: default:
if(invert) if(invert)
drawchar(x, y, 't', 0, 0 ,0 ,alpha); drawchar(x, y, 't', 0, 0 ,0 ,alpha);

View File

@ -78,6 +78,7 @@ enum Icon
IconFolder, IconFolder,
IconSearch, IconSearch,
IconDelete, IconDelete,
IconAdd,
IconReport, IconReport,
IconUsername, IconUsername,
IconPassword, IconPassword,

View File

@ -68,7 +68,7 @@ public:
string GetSort() { return currentSort; } string GetSort() { return currentSort; }
void SetShowOwn(bool show) { if(!updateSaveListWorking) { if(show!=showOwn) { showOwn = show; } } notifyShowOwnChanged(); } void SetShowOwn(bool show) { if(!updateSaveListWorking) { if(show!=showOwn) { showOwn = show; } } notifyShowOwnChanged(); }
bool GetShowOwn() { return showOwn; } bool GetShowOwn() { return showOwn; }
void SetShowFavourite(bool show) { if(show!=showFavourite) { showFavourite = show; } notifyShowFavouriteChanged(); } void SetShowFavourite(bool show) { if(show!=showFavourite && !updateSaveListWorking) { showFavourite = show; } notifyShowFavouriteChanged(); }
bool GetShowFavourite() { return showFavourite; } bool GetShowFavourite() { return showFavourite; }
void SetLoadedSave(SaveInfo * save); void SetLoadedSave(SaveInfo * save);
SaveInfo * GetLoadedSave(); SaveInfo * GetLoadedSave();

View File

@ -33,15 +33,38 @@ TagsView::TagsView():
} }
}; };
closeButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(195, 16), "Close"); closeButton = new ui::Button(ui::Point(0, Size.Y-16), ui::Point(195, 16), "Close");
closeButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; closeButton->Appearance.VerticalAlign = ui::Appearance::AlignTop; closeButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
closeButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
closeButton->SetActionCallback(new CloseAction(this)); closeButton->SetActionCallback(new CloseAction(this));
AddComponent(closeButton); AddComponent(closeButton);
tagInput = new ui::Textbox(ui::Point(8, Size.Y-40), ui::Point(Size.X-16, 16), "");
tagInput = new ui::Textbox(ui::Point(8, Size.Y-40), ui::Point(Size.X-60, 16), "", "[new tag]");
tagInput->Appearance.icon = IconTag;
tagInput->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
tagInput->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
AddComponent(tagInput); AddComponent(tagInput);
class AddTagAction : public ui::ButtonAction
{
TagsView * v;
public:
AddTagAction(TagsView * _v) { v = _v; }
void ActionCallback(ui::Button * sender)
{
v->addTag();
}
};
addButton = new ui::Button(ui::Point(tagInput->Position.X+tagInput->Size.X+4, tagInput->Position.Y), ui::Point(40, 16), "Add");
addButton->Appearance.icon = IconAdd;
addButton->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
addButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
addButton->SetActionCallback(new AddTagAction(this));
AddComponent(addButton);
title = new ui::Label(ui::Point(5, 5), ui::Point(185, 16), "Manage tags:"); title = new ui::Label(ui::Point(5, 5), ui::Point(185, 16), "Manage tags:");
title->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; title->Appearance.VerticalAlign = ui::Appearance::AlignTop; title->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
title->Appearance.VerticalAlign = ui::Appearance::AlignTop;
AddComponent(title); AddComponent(title);
} }
@ -90,10 +113,14 @@ void TagsView::NotifyTagsChanged(TagsModel * sender)
tags.push_back(tempLabel); tags.push_back(tempLabel);
AddComponent(tempLabel); AddComponent(tempLabel);
if(sender->GetSave()->GetUserName()==Client::Ref().GetAuthUser().Username) if(sender->GetSave()->GetUserName() == Client::Ref().GetAuthUser().Username || Client::Ref().GetAuthUser().UserElevation == User::ElevationAdmin || Client::Ref().GetAuthUser().UserElevation == User::ElevationModerator)
{ {
ui::Button * tempButton = new ui::Button(ui::Point(15, 35+(16*i)), ui::Point(14, 14), "x"); ui::Button * tempButton = new ui::Button(ui::Point(15, 37+(16*i)), ui::Point(11, 12));
tempButton->Appearance.HorizontalAlign = ui::Appearance::AlignCentre; tempButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; tempButton->Appearance.icon = IconDelete;
tempButton->Appearance.Border = ui::Border(0);
tempButton->Appearance.Margin.Top += 2;
tempButton->Appearance.HorizontalAlign = ui::Appearance::AlignCentre;
tempButton->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
tempButton->SetActionCallback(new DeleteTagAction(this, sender->GetSave()->GetTags()[i])); tempButton->SetActionCallback(new DeleteTagAction(this, sender->GetSave()->GetTags()[i]));
tags.push_back(tempButton); tags.push_back(tempButton);
AddComponent(tempButton); AddComponent(tempButton);
@ -116,7 +143,14 @@ void TagsView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
case KEY_RETURN: case KEY_RETURN:
if(IsFocused(tagInput)) if(IsFocused(tagInput))
{ {
addTag();
}
break;
}
}
void TagsView::addTag()
{
try try
{ {
c->AddTag(tagInput->GetText()); c->AddTag(tagInput->GetText());
@ -126,9 +160,6 @@ void TagsView::OnKeyPress(int key, Uint16 character, bool shift, bool ctrl, bool
new ErrorMessage("Could not add tag", ex.what()); new ErrorMessage("Could not add tag", ex.what());
} }
tagInput->SetText(""); tagInput->SetText("");
}
break;
}
} }
TagsView::~TagsView() { TagsView::~TagsView() {

View File

@ -22,10 +22,12 @@ class TagsController;
class TagsModel; class TagsModel;
class TagsView: public ui::Window { class TagsView: public ui::Window {
TagsController * c; TagsController * c;
ui::Button * addButton;
ui::Button * closeButton; ui::Button * closeButton;
ui::Label * title; ui::Label * title;
ui::Textbox * tagInput; ui::Textbox * tagInput;
std::vector<ui::Component*> tags; std::vector<ui::Component*> tags;
void addTag();
public: public:
TagsView(); TagsView();
virtual void OnDraw(); virtual void OnDraw();