| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (C) 2006-2009 by Guy Rutenberg * |
|---|
| 3 | * guyrutenberg@gmail.com * |
|---|
| 4 | * * |
|---|
| 5 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 6 | * it under the terms of the GNU General Public License as published by * |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 8 | * (at your option) any later version. * |
|---|
| 9 | * * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, * |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 13 | * GNU General Public License for more details. * |
|---|
| 14 | * * |
|---|
| 15 | * You should have received a copy of the GNU General Public License * |
|---|
| 16 | * along with this program; if not, write to the * |
|---|
| 17 | * Free Software Foundation, Inc., * |
|---|
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 19 | ***************************************************************************/ |
|---|
| 20 | |
|---|
| 21 | #include <wx/button.h> |
|---|
| 22 | #include <wx/sizer.h> |
|---|
| 23 | |
|---|
| 24 | #include "statistics_dialog.h" |
|---|
| 25 | |
|---|
| 26 | #include "icon32.xpm" |
|---|
| 27 | |
|---|
| 28 | using namespace std; |
|---|
| 29 | using namespace statistics_dialog; |
|---|
| 30 | |
|---|
| 31 | StatisticsDialog::StatisticsDialog(wxWindow* parent,statistics::Statistics* stats) : |
|---|
| 32 | wxDialog(parent, wxID_ANY, wxT("Statistics"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) |
|---|
| 33 | { |
|---|
| 34 | m_stats = stats; |
|---|
| 35 | |
|---|
| 36 | SetIcon(wxIcon(icon32_xpm)); |
|---|
| 37 | |
|---|
| 38 | CreateControls(); |
|---|
| 39 | LoadData(); |
|---|
| 40 | DoLayout(); |
|---|
| 41 | ConnectEventTable(); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | void StatisticsDialog::CreateControls() |
|---|
| 45 | { |
|---|
| 46 | games_started = new wxStaticText(this, wxID_ANY, wxT("")); |
|---|
| 47 | games_finished = new wxStaticText(this, wxID_ANY, wxT("")); |
|---|
| 48 | pie_plot = new simple_pie_plot::SimplePiePlot(this, wxID_ANY); |
|---|
| 49 | statistics_reset_date = new wxStaticText(this, wxID_ANY, wxT("")); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | void StatisticsDialog::LoadData() |
|---|
| 53 | { |
|---|
| 54 | wxString tmp; |
|---|
| 55 | tmp = wxString::Format(wxT("Games started: %i"), m_stats->games_started()); |
|---|
| 56 | games_started->SetLabel(tmp); |
|---|
| 57 | |
|---|
| 58 | tmp = wxString::Format(wxT("Games finished: %i"), m_stats->games_finished()); |
|---|
| 59 | games_finished->SetLabel(tmp); |
|---|
| 60 | |
|---|
| 61 | wxDateTime reset_time(m_stats->last_reset()); |
|---|
| 62 | tmp = wxT("Last reset: ") + reset_time.Format(); |
|---|
| 63 | statistics_reset_date->SetLabel(tmp); |
|---|
| 64 | |
|---|
| 65 | vector<int> score_dist = m_stats->score_distribution(); |
|---|
| 66 | vector<double> dist; |
|---|
| 67 | vector<wxString> labels; |
|---|
| 68 | for (int i; i<score_dist.size(); i++) { |
|---|
| 69 | if (score_dist[i]) { |
|---|
| 70 | assert(score_dist[i]>0); |
|---|
| 71 | dist.push_back(score_dist[i]); |
|---|
| 72 | tmp = wxString::Format(wxT("%i - %i"), |
|---|
| 73 | i*statistics::score_distribution_granuality, |
|---|
| 74 | (i+1)*statistics::score_distribution_granuality); |
|---|
| 75 | labels.push_back(tmp); |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | pie_plot->SetData(dist, labels); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | void StatisticsDialog::DoLayout() |
|---|
| 82 | { |
|---|
| 83 | wxBoxSizer* top_sizer = new wxBoxSizer(wxVERTICAL); |
|---|
| 84 | |
|---|
| 85 | wxStaticBoxSizer* game_counts = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Game Counts") ), wxHORIZONTAL); |
|---|
| 86 | game_counts->Add(games_started); |
|---|
| 87 | game_counts->AddStretchSpacer(10); |
|---|
| 88 | game_counts->Add(games_finished); |
|---|
| 89 | top_sizer->Add(game_counts,0,wxEXPAND,10); |
|---|
| 90 | |
|---|
| 91 | wxStaticBoxSizer* score_distribution = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Score Distribution") ), wxHORIZONTAL); |
|---|
| 92 | score_distribution->Add(pie_plot, 1, wxEXPAND, 10); |
|---|
| 93 | top_sizer->Add(score_distribution,10,wxEXPAND, 10); |
|---|
| 94 | |
|---|
| 95 | top_sizer->Add(statistics_reset_date,0,wxEXPAND,10); |
|---|
| 96 | |
|---|
| 97 | wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL ); |
|---|
| 98 | wxSizerFlags flags = wxSizerFlags().Border(wxALL & ~wxLEFT, 10); |
|---|
| 99 | button_sizer->AddStretchSpacer(); |
|---|
| 100 | button_sizer->Add(new wxButton(this,wxID_CLEAR, wxT("Reset")),flags); |
|---|
| 101 | button_sizer->Add( new wxButton(this,wxID_CLOSE), flags); |
|---|
| 102 | top_sizer->Add(button_sizer,0,wxEXPAND); |
|---|
| 103 | |
|---|
| 104 | SetSizer(top_sizer); |
|---|
| 105 | top_sizer->SetSizeHints(this); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | void StatisticsDialog::ConnectEventTable() |
|---|
| 110 | { |
|---|
| 111 | Connect(wxID_CLOSE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(StatisticsDialog::OnClose)); |
|---|
| 112 | Connect(wxID_CLEAR, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(StatisticsDialog::OnReset)); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void StatisticsDialog::OnClose(wxCommandEvent& event) |
|---|
| 116 | { |
|---|
| 117 | Close(); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | void StatisticsDialog::OnReset(wxCommandEvent& event) |
|---|
| 122 | { |
|---|
| 123 | int answer = wxMessageBox( |
|---|
| 124 | wxT("Are you sure you want to clear the statistics data?\nThis action cannot be reversed."), |
|---|
| 125 | wxT("Are You Sure?"), |
|---|
| 126 | wxYES | wxNO, |
|---|
| 127 | this); |
|---|
| 128 | |
|---|
| 129 | if (answer == wxYES) { |
|---|
| 130 | m_stats->reset(); |
|---|
| 131 | LoadData(); |
|---|
| 132 | } |
|---|
| 133 | } |
|---|