/home/enzo/treballs/fib/pfc/nanocomp/src/ruleView.cpp

Go to the documentation of this file.
00001 /*
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  *   This program is distributed in the hope that it will be useful,       *
00009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00011  *   GNU General Public License for more details.                          *
00012  *                                                                         *
00013  *   You should have received a copy of the GNU General Public License     *
00014  *   along with this program; if not, write to the                         *
00015  *   Free Software Foundation, Inc.,                                       *
00016  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00017                                                                           */
00018 // $Revision: 1.25 $
00019 
00020 #include "ruleView.hpp"
00021 #include <wx/numdlg.h>
00022 #include <wx/statline.h>
00023 #include "resources/forward.xpm"
00024 #include "resources/cross.xpm"
00025 #include "resources/fileopen.xpm"
00026 #include "resources/filesave.xpm"
00027 #include "resources/filesaveas.xpm"
00028 #include "resources/new.xpm"
00029 
00030 enum
00031 {
00032     ID_BUTTON_NEW = 0,
00033     ID_BUTTON_OPEN,
00034     ID_BUTTON_SAVE,
00035     ID_BUTTON_REMOVE,
00036     ID_BUTTON_SAVEALL,
00037     ID_BUTTON_SAVEAS,
00038     ID_BUTTON_COPY,
00039     ID_BUTTON_CHECK,
00040     ID_LISTRULES,
00041     ID_CANVAS_INITIAL,
00042     ID_CANVAS_FINAL
00043 };
00044 
00045 
00046 BEGIN_EVENT_TABLE(RuleView, wxPanel)
00047     EVT_LAYOUT_CANVAS  (ID_CANVAS_INITIAL,  RuleView::OnClick)
00048     EVT_LAYOUT_CANVAS  (ID_CANVAS_FINAL,  RuleView::OnClick)
00049     EVT_BUTTON  (ID_BUTTON_NEW, RuleView::OnNewRule)
00050     EVT_LISTBOX (ID_LISTRULES, RuleView::OnSelection)
00051     EVT_BUTTON  (ID_BUTTON_REMOVE, RuleView::OnRemove)
00052     EVT_BUTTON  (ID_BUTTON_COPY, RuleView::OnCopy)
00053     EVT_BUTTON  (ID_BUTTON_SAVE, RuleView::OnSave)
00054     EVT_BUTTON  (ID_BUTTON_SAVEAS, RuleView::OnSaveAs)
00055     EVT_BUTTON  (ID_BUTTON_OPEN, RuleView::OnOpen)
00056     EVT_BUTTON  (ID_BUTTON_CHECK, RuleView::OnCheck)
00057 END_EVENT_TABLE()
00058 
00059 
00061 
00066 RuleView::RuleView(wxWindow* parent, wxWindowID id, RuleManager *controller)
00067     :wxPanel(parent, id)
00068 {
00069     this->controller = controller;
00070     initControls();
00071     initSizers();
00072     controller->setView(this);
00073 }
00074 
00075 
00077 RuleView::~RuleView()
00078 {
00079 }
00080 
00081 
00083 void RuleView::initControls()
00084 {
00085     //Labels
00086     labelInitialLayout = new wxStaticText(this, wxID_ANY, _("Initial layout"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE);
00087     labelFinalLayout = new wxStaticText(this, wxID_ANY, _("Final layout"), wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE);
00088     
00089     //Buttons
00090     wxBitmap newBitmap(wxBITMAP(new));
00091     wxBitmap saveBitmap(wxBITMAP(filesave));
00092     wxBitmap saveAsBitmap(wxBITMAP(filesaveas));
00093     wxBitmap removeBitmap(wxBITMAP(cross));
00094     wxBitmap openBitmap(wxBITMAP(fileopen));
00095     buttonNew = new wxBitmapButton(this, ID_BUTTON_NEW, newBitmap);
00096     buttonNew->SetToolTip(_("Add a new rule"));
00097     buttonOpen = new wxBitmapButton(this, ID_BUTTON_OPEN, openBitmap);
00098     buttonOpen->SetToolTip(_("Open a rule file"));
00099     buttonSave = new wxBitmapButton(this, ID_BUTTON_SAVE, saveBitmap);
00100     buttonSave->SetToolTip(_("Save rules into a file"));
00101     buttonSaveAs = new wxBitmapButton(this, ID_BUTTON_SAVEAS, saveAsBitmap);
00102     buttonSave->SetToolTip(_("Save rules into a new file"));
00103     buttonRemove = new wxBitmapButton(this, ID_BUTTON_REMOVE, removeBitmap);
00104     buttonRemove->SetToolTip(_("Delete selected rule"));
00105     buttonSave->Enable(false);
00106     buttonSaveAs->Enable(false);
00107     buttonRemove->Enable(false);
00108     wxBitmap copyBitmap(wxBITMAP(forward));
00109     buttonCopy = new wxBitmapButton(this, ID_BUTTON_COPY, copyBitmap);
00110     buttonCopy->SetToolTip(_("Copy initial layout to final layout"));
00111     buttonCheck = new wxButton(this, ID_BUTTON_CHECK, _("Check Rules"));
00112     
00113     //Other controls
00114     listRules = new wxListBox(this, ID_LISTRULES, wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_SINGLE);
00115     canvasInitial = new LayoutCanvas(this, ID_CANVAS_INITIAL, Grid());
00116     canvasFinal = new LayoutCanvas(this, ID_CANVAS_FINAL, Grid());
00117  }
00118 
00119 
00121 void RuleView::initSizers()
00122 {
00123     //Main Sizer
00124     wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
00125     
00126         //List and layout sizer
00127         wxBoxSizer *listLayoutSizer = new wxBoxSizer(wxHORIZONTAL);
00128             
00129             //List sizer
00130             wxStaticBoxSizer *listVerticalSizer = new wxStaticBoxSizer(wxVERTICAL, this, _("Rule list"));
00131             wxBoxSizer *listSizer = new wxBoxSizer(wxHORIZONTAL);
00132             listSizer->Add(listRules, 1, wxEXPAND | wxALL, 1);
00133             
00134                 //Buttons sizer
00135                 wxBoxSizer *buttonSizer = new wxBoxSizer(wxVERTICAL);
00136                 buttonSizer->Add(buttonNew, 0, wxEXPAND | wxALL);
00137                 buttonSizer->Add(buttonOpen, 0, wxEXPAND | wxALL);
00138                 buttonSizer->Add(buttonRemove, 0, wxEXPAND | wxALL);
00139                 buttonSizer->Add(buttonSave, 0, wxEXPAND | wxALL);
00140                 buttonSizer->Add(buttonSaveAs, 0, wxEXPAND | wxALL);
00141                 
00142             listSizer->Add(buttonSizer, 0, wxEXPAND | wxALL, 1);
00143         
00144         listVerticalSizer->Add(listSizer, 1, wxEXPAND | wxALL, 1);
00145 
00146         //Check button sizer
00147         wxBoxSizer *checkButtonSizer = new wxBoxSizer(wxVERTICAL);
00148         checkButtonSizer->Add(buttonCheck, 0, wxSHAPED | wxALIGN_CENTER, 1);
00149             
00150         listVerticalSizer->Add(checkButtonSizer, 0, wxSHAPED | wxALIGN_CENTER, 1);
00151         
00152         listLayoutSizer->Add(listVerticalSizer, 2, wxEXPAND | wxALL, 1);
00153         
00154             //Layouts sizer
00155             wxStaticBoxSizer *layoutSizer = new wxStaticBoxSizer(wxHORIZONTAL, this, _("Layouts"));
00156             wxBoxSizer *initialLayoutSizer = new wxBoxSizer(wxVERTICAL);
00157             wxBoxSizer *finalLayoutSizer = new wxBoxSizer(wxVERTICAL);
00158             initialLayoutSizer->Add(labelInitialLayout, 0, wxEXPAND | wxALL | wxALIGN_CENTER, 0);
00159             initialLayoutSizer->Add(canvasInitial, 1, wxEXPAND | wxALL | wxALIGN_CENTER, 1);
00160             layoutSizer->Add(initialLayoutSizer, 1, wxSHAPED | wxALL | wxALIGN_CENTER, 1);
00161             layoutSizer->Add(buttonCopy, 0, wxSHAPED | wxALL | wxALIGN_CENTER, 0);
00162             finalLayoutSizer->Add(labelFinalLayout, 0, wxEXPAND | wxALL | wxALIGN_CENTER, 0);
00163             finalLayoutSizer->Add(canvasFinal, 1, wxEXPAND | wxALL | wxALIGN_CENTER, 1);
00164             layoutSizer->Add(finalLayoutSizer, 1, wxSHAPED | wxALL | wxALIGN_CENTER, 1);
00165             
00166         listLayoutSizer->Add(layoutSizer, 3, wxEXPAND | wxALL, 1);
00167         
00168     mainSizer->Add(listLayoutSizer, 3, wxEXPAND | wxALL, 1);
00169         
00170     SetSizer(mainSizer);
00171 }
00172 
00173 
00175 
00180 void RuleView::OnClick(LayoutCanvasEvent &event)
00181 {
00182     switch (event.GetId())
00183     {
00184         case ID_CANVAS_INITIAL:
00185         {
00186             controller->cellChanged(nINITIAL, event.getX(), event.getY());
00187             break;
00188         }
00189         case ID_CANVAS_FINAL:
00190         {
00191             controller->cellChanged(nFINAL, event.getX(), event.getY());
00192             break;
00193         }
00194     }
00195 }
00196 
00197 
00199 
00202 void RuleView::OnNewRule(wxCommandEvent& event)
00203 {
00204     int width;
00205     int height;
00206     wxNumberEntryDialog widthDialog(this,
00207                         wxEmptyString,
00208                         _("Insert the layout width"),
00209                         _("New Rule"),
00210                         1, 1, 100);
00211     if (widthDialog.ShowModal() == wxID_OK)
00212     {
00213         width = (int)(widthDialog.GetValue());
00214     
00215         wxNumberEntryDialog heightDialog(this,
00216                         wxEmptyString,
00217                         _("Insert the layout height"),
00218                         _("New Rule"),
00219                         1, 1, 100);
00220         if (heightDialog.ShowModal() == wxID_OK)
00221         {
00222             height = (int)(heightDialog.GetValue());
00223             controller->newRule(width, height);
00224         }
00225     }
00226 }
00227 
00228 
00230 
00235 void RuleView::updateGrids(Grid initial, Grid final, bool changed)
00236 {
00237     canvasInitial->setGrid(initial, changed);
00238     canvasFinal->setGrid(final, changed);
00239 }
00240 
00241 
00243 
00246 void RuleView::updateList(wxArrayString strings)
00247 {
00248     listRules->Set(strings);
00249     buttonRemove->Enable(strings.size() > 0);
00250     buttonSave->Enable(strings.size() > 0);
00251     buttonSaveAs->Enable(strings.size() > 0);
00252 }
00253 
00254 
00256 
00259 void RuleView::OnSelection(wxCommandEvent &event)
00260 {
00261     //New rule selected, we must let the controller know
00262     //to update us with new info
00263     if (event.IsSelection())
00264     {
00265         wxString selection = event.GetString();
00266         unsigned long int temp;
00267         selection.Mid(4).ToULong(&temp);
00268         unsigned int ruleId = temp;
00269         controller->ruleSelected(ruleId);
00270         buttonRemove->Enable(true);
00271         buttonSave->Enable(true);
00272     }
00273 }
00274 
00275 
00277 
00280 void RuleView::OnRemove(wxCommandEvent &event)
00281 {
00282     controller->removeRule();
00283 }
00284 
00285 
00287 
00290 void RuleView::selectRule(unsigned int rule)
00291 {
00292     int item = listRules->FindString(wxString::Format(_("Rule %d"), rule));
00293     if (item != wxNOT_FOUND)
00294     {
00295         listRules->SetSelection(item);
00296         buttonRemove->Enable(true);
00297         buttonSave->Enable(true);
00298     }
00299 }
00300 
00301 
00303 
00306 void RuleView::OnCopy(wxCommandEvent& event)
00307 {
00308     controller->copyGrids();  
00309 }
00310 
00311 
00313 
00316 void RuleView::OnSave(wxCommandEvent& event)
00317 {
00318     controller->saveFile();
00319 }
00320 
00321 
00323 
00326 void RuleView::OnSaveAs(wxCommandEvent& event)
00327 {
00328     controller->saveFileAs();
00329 }
00330 
00331 
00333 
00336 void RuleView::errorMsg(wxString message)
00337 {
00338     wxMessageDialog dialog(this,
00339             message,
00340             _("Error"), 
00341             wxOK | wxICON_ERROR); 
00342     dialog.ShowModal();
00343 }
00344 
00345 
00347 
00350 void RuleView::OnOpen(wxCommandEvent& event)
00351 {
00352     controller->openFile();
00353 }
00354 
00355 
00357 
00361 bool RuleView::msgYesNo(wxString message)
00362 {
00363     wxMessageDialog dialog(this,
00364             message,
00365             _("Question"), 
00366             wxYES | wxNO); 
00367     return (dialog.ShowModal() == wxID_YES);
00368 }
00369 
00370 
00372 
00375 void RuleView::OnCheck(wxCommandEvent& event)
00376 {
00377     controller->checkRules();
00378 }
00379 
00380 
00382 
00386 void RuleView::setListMessage(int id, wxString message)
00387 {
00388     //TODO: explota si les regles no són seguides, p.e. 1-3
00389     //perquè la 2 s'ha esborrat. S'ha de buscar l'id.
00390     listRules->SetString(id - 1, listRules
00391     ->GetString(id - 1) + message);
00392 }
00393 
00394 
00396 
00399 void RuleView::resetListItem(int id)
00400 {
00401     //TODO: el mateix que adalt.
00402     listRules->SetString(id - 1, wxString::Format(_("Rule %d"), id));
00403 }

Generated on Fri Sep 1 23:55:14 2006 for NanoComp by  doxygen 1.4.6