diff --git a/CrossBench/CrossBench.mk b/CrossBench/CrossBench.mk index 27dd2ca..12fa393 100644 --- a/CrossBench/CrossBench.mk +++ b/CrossBench/CrossBench.mk @@ -13,7 +13,7 @@ CurrentFileName := CurrentFilePath := CurrentFileFullPath := User :=Aleksey Lobanov -Date :=16/06/15 +Date :=17/06/15 CodeLitePath :="/home/alex/.codelite" LinkerName :=/usr/bin/g++-4.8 SharedObjectLinkerName :=/usr/bin/g++-4.8 -shared -fPIC diff --git a/src/crossexport.hpp b/src/crossexport.hpp index 222ca49..5cb3095 100644 --- a/src/crossexport.hpp +++ b/src/crossexport.hpp @@ -5,10 +5,38 @@ #include "crossbasetypes.hpp" -//wxTextFile::GetEOL() +void fillCross(FilledCrossword &cross){ + for ( size_t i = 0; i < cross.words.size(); ++i ) { + if ( cross.words.at(i).direct == true ) { + for (size_t j = 0; j < cross.words.at(i).len; ++j){ + cross.grid.at(cross.words.at(i).x+j).at(cross.words.at(i).y) = cross.ans.at(i).at(j); + } + } else { + for (size_t j = 0; j < cross.words.at(i).len; ++j) + cross.grid.at(cross.words.at(i).x).at(cross.words.at(i).y+j) = cross.ans.at(i).at(j); + } + } +} -void exportToString(const FilledCrossword &cross, const bool prn_ans, wxString &str_out){ - +void exportToString(const FilledCrossword &cross, const bool prn_ans, + wxString &str_out, wxChar space = wxT('-')){ + FilledCrossword t_cross(cross); + if ( prn_ans && (t_cross.ans.size() != 0) ) { + fillCross(t_cross); + } + wxLogDebug(wxT("3")); + for (size_t i = 0; i < t_cross.grid.at(0).size(); ++i){ + wxString cur_line; + for (size_t j = 0; j < t_cross.grid.size(); ++j){ + if ( t_cross.grid.at(j).at(i) == CELL_BORDER ) + cur_line += space; + else + cur_line += t_cross.grid.at(j).at(i); + } + cur_line += wxTextFile::GetEOL(); + + str_out += cur_line; + } } bool exportToFile(const FilledCrossword &cross, const bool prn_ans, const wxString path){ @@ -16,6 +44,7 @@ bool exportToFile(const FilledCrossword &cross, const bool prn_ans, const wxStri if ( f.Exists() ) return false; f.Create(); + f.Open(path); wxString cont; exportToString(cross, prn_ans, cont); f.AddLine(cont); diff --git a/wxCrossGen/FormBuilderGUI.fbp b/wxCrossGen/FormBuilderGUI.fbp index 174bf4f..5018954 100644 --- a/wxCrossGen/FormBuilderGUI.fbp +++ b/wxCrossGen/FormBuilderGUI.fbp @@ -147,7 +147,7 @@ 0 1 - wxID_ANY + wxID_EXECUTE wxITEM_NORMAL &Generate miGenerate @@ -157,6 +157,21 @@ onGenerateClick + + + 0 + 1 + + wxID_EXPORT + wxITEM_NORMAL + &Export + miExport + none + + + onExportClick + + 0 @@ -269,7 +284,7 @@ 5 - wxALL + wxALIGN_BOTTOM|wxALIGN_LEFT|wxALIGN_RIGHT 0 diff --git a/wxCrossGen/fbgui/fbgui.cpp b/wxCrossGen/fbgui/fbgui.cpp index f074469..f5f449f 100644 --- a/wxCrossGen/fbgui/fbgui.cpp +++ b/wxCrossGen/fbgui/fbgui.cpp @@ -20,9 +20,13 @@ VMainFrame::VMainFrame( wxWindow* parent, wxWindowID id, const wxString& title, miFile->Append( miOpenGrid ); wxMenuItem* miGenerate; - miGenerate = new wxMenuItem( miFile, wxID_ANY, wxString( _("&Generate") ) , wxEmptyString, wxITEM_NORMAL ); + miGenerate = new wxMenuItem( miFile, wxID_EXECUTE, wxString( _("&Generate") ) , wxEmptyString, wxITEM_NORMAL ); miFile->Append( miGenerate ); + wxMenuItem* miExport; + miExport = new wxMenuItem( miFile, wxID_EXPORT, wxString( _("&Export") ) , wxEmptyString, wxITEM_NORMAL ); + miFile->Append( miExport ); + wxMenuItem* miExit; miExit = new wxMenuItem( miFile, wxID_EXIT, wxString( _("E&xit") ) + wxT('\t') + wxT("Ctrl+Q"), wxEmptyString, wxITEM_NORMAL ); miFile->Append( miExit ); @@ -48,7 +52,7 @@ VMainFrame::VMainFrame( wxWindow* parent, wxWindowID id, const wxString& title, bSizer4->Add( tPath, 1, wxEXPAND|wxLEFT|wxTOP, 5 ); bPath = new wxButton( this, wxID_ANY, _("..."), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT ); - bSizer4->Add( bPath, 0, wxALL, 5 ); + bSizer4->Add( bPath, 0, wxALIGN_BOTTOM|wxALIGN_LEFT|wxALIGN_RIGHT, 5 ); bSizer2->Add( bSizer4, 0, wxEXPAND, 5 ); @@ -81,6 +85,7 @@ VMainFrame::VMainFrame( wxWindow* parent, wxWindowID id, const wxString& title, this->Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( VMainFrame::onExitClick ) ); this->Connect( miOpenGrid->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( VMainFrame::onOpenGridClick ) ); this->Connect( miGenerate->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( VMainFrame::onGenerateClick ) ); + this->Connect( miExport->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( VMainFrame::onExportClick ) ); this->Connect( miExit->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( VMainFrame::onExitClick ) ); this->Connect( miAbout->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( VMainFrame::onAboutClick ) ); bPath->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VMainFrame::onOpenGridClick ), NULL, this ); @@ -93,6 +98,7 @@ VMainFrame::~VMainFrame() this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( VMainFrame::onExitClick ) ); this->Disconnect( wxID_ANY, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( VMainFrame::onOpenGridClick ) ); this->Disconnect( wxID_ANY, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( VMainFrame::onGenerateClick ) ); + this->Disconnect( wxID_ANY, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( VMainFrame::onExportClick ) ); this->Disconnect( wxID_ANY, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( VMainFrame::onExitClick ) ); this->Disconnect( wxID_ANY, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( VMainFrame::onAboutClick ) ); bPath->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( VMainFrame::onOpenGridClick ), NULL, this ); diff --git a/wxCrossGen/fbgui/fbgui.h b/wxCrossGen/fbgui/fbgui.h index 6bcb6ee..c73374d 100644 --- a/wxCrossGen/fbgui/fbgui.h +++ b/wxCrossGen/fbgui/fbgui.h @@ -27,6 +27,8 @@ /////////////////////////////////////////////////////////////////////////// +#define wxID_EXECUTE 1000 +#define wxID_EXPORT 1001 /////////////////////////////////////////////////////////////////////////////// /// Class VMainFrame @@ -50,6 +52,7 @@ class VMainFrame : public wxFrame virtual void onExitClick( wxCloseEvent& event ) { event.Skip(); } virtual void onOpenGridClick( wxCommandEvent& event ) { event.Skip(); } virtual void onGenerateClick( wxCommandEvent& event ) { event.Skip(); } + virtual void onExportClick( wxCommandEvent& event ) { event.Skip(); } virtual void onExitClick( wxCommandEvent& event ) { event.Skip(); } virtual void onAboutClick( wxCommandEvent& event ) { event.Skip(); } diff --git a/wxCrossGen/main.cpp b/wxCrossGen/main.cpp index 6812617..335dd37 100644 --- a/wxCrossGen/main.cpp +++ b/wxCrossGen/main.cpp @@ -27,9 +27,8 @@ void MainFrame::onOpenGridClick(wxCommandEvent &event) { return; } tPath->SetValue(dlgOpen.GetPath()); - GridType grid; - readGrid(tPath->GetValue(), grid); - SetGridImage(grid); + readGrid(tPath->GetValue(), _grid); + SetGridImage(_grid); } void MainFrame::SetGridImage(GridType &grid, size_t w) { @@ -62,7 +61,7 @@ void MainFrame::SetGridImage(GridType &grid, size_t w) { dc.DrawText(wxString::Format(wxT("%d"),winfos.at(i).ind), sq_w*winfos.at(i).x, sq_h*winfos.at(i).y); } - if ( _words.size() > 0 ) { + if ( _ans.size() > 0 ) { using std::vector; vector< vector< bool > > usedCells( grid.size(), @@ -76,7 +75,7 @@ void MainFrame::SetGridImage(GridType &grid, size_t w) { for (size_t j = 0; j < winfos.at(i).len; ++j) if ( !usedCells.at(winfos.at(i).x+j).at(winfos.at(i).y) ) { dc.DrawText( - _words.at(i).at(j), + _ans.at(i).at(j), sq_w*(winfos.at(i).x+j) + sq_w*0.24, sq_h*winfos.at(i).y ); @@ -86,7 +85,7 @@ void MainFrame::SetGridImage(GridType &grid, size_t w) { for (size_t j = 0; j < winfos.at(i).len; ++j) if ( !usedCells.at(winfos.at(i).x).at(winfos.at(i).y+j) ) { dc.DrawText( - _words.at(i).at(j), + _ans.at(i).at(j), sq_w*winfos.at(i).x + sq_w*0.24, sq_h*(winfos.at(i).y+j) ); @@ -112,20 +111,17 @@ void MainFrame::onGenerateClick(wxCommandEvent &event) { } std::vector words_out; - GridType grid; - if (tPath->GetValue() == wxEmptyString){ - wxMessageBox( _("Path to grid is empty"), _("Info"), wxICON_INFORMATION); + if ( _dict.size() == 0 ){ + wxMessageBox( _("Crossword grid isn't loaded!"), _("Info"), wxICON_INFORMATION); return; } - readGrid(tPath->GetValue(), grid); try{ - generateCross(grid, _allWords, _transType, words_out); + generateCross(_grid, _allWords, _transType, words_out); - _words.clear(); - _words = words_out; + _ans = words_out; std::vector winfos; - generateWordInfo(grid, winfos); + generateWordInfo(_grid, winfos); tOutput->Clear(); @@ -146,8 +142,7 @@ void MainFrame::onGenerateClick(wxCommandEvent &event) { } if (winfos.size() == 0) throw 42; - SetGridImage(grid); - _words.clear(); + SetGridImage(_grid); } catch ( ... ){ tOutput->Clear(); @@ -156,6 +151,25 @@ void MainFrame::onGenerateClick(wxCommandEvent &event) { this->Refresh(); } +void MainFrame::onExportClick(wxCommandEvent& event) { + if ( _grid.size() == 0 ) { + wxMessageBox( _("Grid isn't loaded now"), _("Info"), wxICON_INFORMATION ); + return; + } + wxFileDialog dlgSave(this, _("Exporting crossword"), wxEmptyString, wxEmptyString, + _("txt files (*.txt)|*.txt"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT); + if (dlgSave.ShowModal() == wxID_CANCEL) + return; + FilledCrossword t_cross; + t_cross.grid = _grid; + t_cross.ans = _ans; + generateWordInfo(_grid, t_cross.words); + if ( !exportToFile(t_cross, true, dlgSave.GetPath()) ){ + wxLogError(wxT("Cannot save current contents in file '%s'."), dlgSave.GetPath().GetData()); + return; + } + wxLogDebug(wxT("Exporting to ") + dlgSave.GetPath() + wxT("is complete")); +} class MyApp: public wxApp { public: diff --git a/wxCrossGen/wxCrossGen.mk b/wxCrossGen/wxCrossGen.mk index ce190f5..9b84faa 100644 --- a/wxCrossGen/wxCrossGen.mk +++ b/wxCrossGen/wxCrossGen.mk @@ -13,7 +13,7 @@ CurrentFileName := CurrentFilePath := CurrentFileFullPath := User :=Aleksey Lobanov -Date :=16/06/15 +Date :=17/06/15 CodeLitePath :="/home/alex/.codelite" LinkerName :=/usr/bin/g++-4.8 SharedObjectLinkerName :=/usr/bin/g++-4.8 -shared -fPIC diff --git a/wxCrossGen/wxgui.hpp b/wxCrossGen/wxgui.hpp index 2b03203..51eb274 100644 --- a/wxCrossGen/wxgui.hpp +++ b/wxCrossGen/wxgui.hpp @@ -20,11 +20,12 @@ class MainFrame: public VMainFrame { protected: - std::vector _words; + std::vector _ans; DictType _dict; bool _isDictLoaded; AllWordsType _allWords; CharsTransType _transType; + GridType _grid; public: void SetGridImage(GridType &grid, size_t w=400); @@ -41,11 +42,12 @@ public: _isDictLoaded = false; srand(time(NULL)); } - virtual void onExitClick( wxCloseEvent& event ) { event.Skip(); } - virtual void onOpenGridClick( wxCommandEvent& event ); - virtual void onGenerateClick( wxCommandEvent& event ); - virtual void onExitClick( wxCommandEvent& event ) { event.Skip(); } - virtual void onAboutClick( wxCommandEvent& event ) { event.Skip(); } + void onExitClick( wxCloseEvent& event ) { event.Skip(); } + void onOpenGridClick( wxCommandEvent& event ); + void onGenerateClick( wxCommandEvent& event ); + void onExportClick( wxCommandEvent& event ); + void onExitClick( wxCommandEvent& event ) { event.Skip(); } + void onAboutClick( wxCommandEvent& event ) { event.Skip(); } }; #endif // WXGUI_HPP