00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "truthTableCanvas.hpp"
00022 #include <math.h>
00023
00024
00025
00026 DEFINE_EVENT_TYPE( TRUTH_TABLE_CANVAS_ACTION )
00027
00028
00029
00033 TruthTableCanvasEvent::TruthTableCanvasEvent(wxEventType commandType, int id)
00034 : wxNotifyEvent( commandType, id )
00035 {
00036 }
00037
00038
00040
00043 vector<bool> TruthTableCanvasEvent::getInput()
00044 {
00045 return input;
00046 }
00047
00048
00050
00053 int TruthTableCanvasEvent::getOutput()
00054 {
00055 return output;
00056 }
00057
00058
00060
00063 void TruthTableCanvasEvent::setInput(vector<bool> newInput)
00064 {
00065 input = newInput;
00066 }
00067
00068
00070
00073 void TruthTableCanvasEvent::setOutput(int newOutput)
00074 {
00075 output = newOutput;
00076 }
00077
00078
00079 BEGIN_EVENT_TABLE(TruthTableCanvas, wxGrid)
00080 EVT_GRID_CELL_LEFT_CLICK (TruthTableCanvas::OnCellChanged)
00081 END_EVENT_TABLE()
00082
00083
00085
00090 TruthTableCanvas::TruthTableCanvas(wxWindow *parent, wxWindowID id, TruthTable initialTable)
00091 :wxGrid(parent, id), table(initialTable)
00092 {
00093 initColours();
00094 if ((initialTable.getInputs() > 0) && (initialTable.getOutputs() > 0))
00095 {
00096 CreateGrid((int)pow(2, table.getInputs()), table.getInputs() + table.getOutputs());
00097 setTable();
00098 }
00099 else
00100 {
00101 Show(false);
00102 }
00103 this->EnableDragGridSize(false);
00104 wxColour defaultColor = wxSystemSettings::GetColour (wxSYS_COLOUR_3DFACE);
00105 editable = true;
00106 this->SetDefaultCellBackgroundColour (defaultColor);
00107 }
00108
00109
00111
00114 void TruthTableCanvas::setNewTable(TruthTable newTable)
00115 {
00116 table.copyTable(newTable);
00117 DeleteCols(0, GetNumberCols());
00118 DeleteRows(0, GetNumberRows());
00119 if ((newTable.getInputs() > 0) && (newTable.getOutputs() > 0))
00120 {
00121 InsertRows(0, (int)pow(2, table.getInputs()));
00122 InsertCols(0, table.getInputs() + table.getOutputs());
00123 CreateGrid((int)pow(2, table.getInputs()), table.getInputs() + table.getOutputs());
00124 setTable();
00125 Show(true);
00126 }
00127 else
00128 {
00129 Show(false);
00130 }
00131 }
00132
00133
00135 TruthTableCanvas::~TruthTableCanvas()
00136 {
00137 }
00138
00139
00141
00145 void TruthTableCanvas::setTable()
00146 {
00147 for (int i = 0; i < pow(2, table.getInputs()); i++)
00148 {
00149 vector<wxString> labels = getInputsString(i);
00150 vector<bool> outputs = table.getOutput(getInputsVector(i));
00151 for (int j = 0; j < table.getInputs(); j++)
00152 {
00153 SetColLabelValue(j, _("Input ") + wxString::Format(_("%d"), j+1));
00154 SetCellValue(i, j, labels[j]);
00155 SetCellAlignment(i, j, wxALIGN_CENTRE, wxALIGN_CENTRE);
00156 SetReadOnly(i, j);
00157 if (labels[j] == _("True"))
00158 {
00159 SetCellBackgroundColour(i, j, *colourInputTrue);
00160 }
00161 else
00162 {
00163 SetCellBackgroundColour(i, j, *colourInputFalse);
00164 }
00165 }
00166 for(int j = table.getInputs(); j < table.getInputs() + table.getOutputs(); j++)
00167 {
00168 SetColLabelValue(j, _("Output ") + wxString::Format(_("%d"), j - table.getInputs()+1));
00169 if (outputs[j - table.getInputs()])
00170 {
00171 SetCellValue(i, j, _("True"));
00172 SetCellBackgroundColour(i, j, *colourOutputTrue);
00173 }
00174 else
00175 {
00176 SetCellValue(i, j, _("False"));
00177 SetCellBackgroundColour(i, j, *colourOutputFalse);
00178 }
00179 SetCellAlignment(i, j, wxALIGN_CENTRE, wxALIGN_CENTRE);
00180 SetReadOnly(i, j);
00181 }
00182 }
00183 }
00184
00185
00187
00193 vector<wxString> TruthTableCanvas::getInputsString(int i)
00194 {
00195 vector<wxString> result(table.getInputs(), _("False"));
00196
00197
00198 int j = result.size() -1;
00199 while (i > 0)
00200 {
00201 if ((i % 2) == 1)
00202 {
00203 result[j] = _("True");
00204 }
00205 i /= 2;
00206 j--;
00207 }
00208
00209 return result;
00210 }
00211
00212
00214
00220 vector<bool> TruthTableCanvas::getInputsVector(int i)
00221 {
00222 vector<bool> result(table.getInputs(), false);
00223
00224
00225 int j = result.size() -1;
00226 while (i > 0)
00227 {
00228 if ((i % 2) == 1)
00229 {
00230 result[j] = true;
00231 }
00232 i /= 2;
00233 j--;
00234 }
00235
00236 return result;
00237 }
00238
00239
00241
00245 vector<bool> TruthTableCanvas::getOutputs(int i)
00246 {
00247 vector<bool> result(table.getOutputs());
00248 for(int j = table.getInputs(); j < table.getInputs() + table.getOutputs(); j++)
00249 {
00250
00251 if (GetCellValue(i, j) == _("True"))
00252 {
00253 result[j - table.getInputs()] = true;
00254 }
00255 else
00256 {
00257 result[j - table.getInputs()] = false;
00258 }
00259 }
00260
00261 return result;
00262 }
00263
00264
00266
00269 void TruthTableCanvas::OnCellChanged(wxGridEvent& ev)
00270 {
00271 if (ev.GetCol() >= table.getInputs())
00272 {
00273 vector<bool> newInput(table.getInputs());
00274 newInput = getInputsVector(ev.GetRow());
00275 TruthTableCanvasEvent event( TRUTH_TABLE_CANVAS_ACTION, GetId() );
00276 event.SetEventObject( this );
00277 event.setInput(newInput);
00278 event.setOutput(ev.GetCol() - table.getInputs());
00279
00280
00281
00282 if (editable)
00283 {
00284 if (GetCellValue(ev.GetRow(), ev.GetCol()) == _("True"))
00285 {
00286 SetCellValue(ev.GetRow(), ev.GetCol(), _("False"));
00287 SetCellBackgroundColour(ev.GetRow(), ev.GetCol(), *colourOutputFalse);
00288 }
00289 else
00290 {
00291 SetCellValue(ev.GetRow(), ev.GetCol(), _("True"));
00292 SetCellBackgroundColour(ev.GetRow(), ev.GetCol(), *colourOutputTrue);
00293 }
00294 }
00295 GetEventHandler()->ProcessEvent( event );
00296 }
00297 else
00298 {
00299 ev.Skip();
00300 }
00301 }
00302
00303
00305 void TruthTableCanvas::initColours()
00306 {
00307 colourInputTrue = new wxColour(167, 232, 146);
00308 colourInputFalse = new wxColour(237, 167, 167);
00309 colourOutputTrue = new wxColour(66, 174, 13);
00310 colourOutputFalse = new wxColour(215, 72, 72);
00311 }
00312
00313
00315
00318 TruthTable TruthTableCanvas::getTable()
00319 {
00320 return table;
00321 }
00322
00323
00325
00329 void TruthTableCanvas::setEditable(bool editable)
00330 {
00331 this->editable = editable;
00332 }