Improve database game import

This commit is contained in:
Loic Guegan 2022-02-26 12:30:07 +01:00
parent 3b6e7d39cf
commit e0a1894928
15 changed files with 547 additions and 155 deletions

View file

@ -56,6 +56,10 @@ MainWindow::MainWindow()
Bind(REFRESH_TAB_TITLE, &MainWindow::OnRefreshTabTitle, this, wxID_ANY);
Bind(NEW_GAME_EVENT, &MainWindow::OnNewGame, this, wxID_ANY);
Bind(wxEVT_CLOSE_WINDOW, &MainWindow::OnClose, this);
/*BaseTab *bt = new BaseTab((wxFrame *)notebook, "/home/loic/hartwig.pgn");
notebook->AddPage(bt, bt->GetLabel());
notebook->SetSelection(notebook->GetPageIndex(bt));*/
}
void MainWindow::OnSettings(wxCommandEvent &event) {
@ -77,6 +81,14 @@ void MainWindow::ApplyPreferences() {
void MainWindow::OnExit(wxCommandEvent &event) { Close(true); }
std::vector<TabInfos *> MainWindow::ListTabInfos() {
std::vector<TabInfos *> tinfos;
for (int i = 0; i < notebook->GetPageCount(); i++) {
tinfos.push_back(dynamic_cast<TabInfos *>(notebook->GetPage(i)));
}
return (tinfos);
}
void MainWindow::OnClose(wxCloseEvent &e) {
if (prefsEditor != NULL) {
prefsEditor->Dismiss();

View file

@ -1,3 +1,5 @@
#pragma once
#include "base_tab/BaseTab.hpp"
#include "game_tab/GameTab.hpp"
#include "ochess.hpp"
@ -28,4 +30,5 @@ class MainWindow : public wxFrame {
public:
MainWindow();
void ApplyPreferences();
std::vector<TabInfos*> ListTabInfos();
};

View file

@ -0,0 +1,28 @@
#include "AppendGameDialog.hpp"
#include "MainWindow.hpp"
#include "ochess.hpp"
AppendGameDialog::AppendGameDialog(wxWindow *parent, GameBase *base)
: AppendGameDialogBF(parent), base(base) {
tinfos = MAINWIN->ListTabInfos();
for (TabInfos *i : tinfos) {
wxWindow *win = dynamic_cast<wxWindow *>(i);
game_list->Append(win->GetLabel());
}
Bind(wxEVT_BUTTON, &AppendGameDialog::OnCancel, this,
ID_DIALOG_CANCEL_BUTTON);
Bind(wxEVT_BUTTON, &AppendGameDialog::OnImport, this,
ID_DIALOG_IMPORT_BUTTON);
}
void AppendGameDialog::OnCancel(wxCommandEvent &event) { this->Close(); }
void AppendGameDialog::OnImport(wxCommandEvent &event) {
std::vector<std::uint32_t> to_ignore;
std::vector<GameBase *> new_games_bases;
std::vector<Game *> new_games;
this->Close();
}

View file

@ -0,0 +1,12 @@
#include "AppendGameDialogBF.h"
#include "gamebase/GameBase.hpp"
class AppendGameDialog : public AppendGameDialogBF {
GameBase *base;
std::vector<TabInfos *> tinfos;
public:
AppendGameDialog(wxWindow *parent, GameBase *base);
void OnCancel(wxCommandEvent &event);
void OnImport(wxCommandEvent &event);
};

View file

@ -0,0 +1,50 @@
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version 3.10.1-40-g8042f487)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////
#include "AppendGameDialogBF.h"
///////////////////////////////////////////////////////////////////////////
AppendGameDialogBF::AppendGameDialogBF( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxDialog( parent, id, title, pos, size, style )
{
this->SetSizeHints( wxDefaultSize, wxDefaultSize );
wxBoxSizer* main_sizer;
main_sizer = new wxBoxSizer( wxVERTICAL );
main_label = new wxStaticText( this, wxID_ANY, wxT("Please select games to import:"), wxDefaultPosition, wxDefaultSize, 0 );
main_label->Wrap( -1 );
main_sizer->Add( main_label, 0, wxALL|wxEXPAND, 5 );
game_list = new wxListBox( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_MULTIPLE );
main_sizer->Add( game_list, 1, wxALL|wxEXPAND, 5 );
wxBoxSizer* bSizer5;
bSizer5 = new wxBoxSizer( wxHORIZONTAL );
bSizer5->Add( 0, 0, 1, wxEXPAND, 5 );
cancel_button = new wxButton( this, ID_DIALOG_CANCEL_BUTTON, wxT("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
bSizer5->Add( cancel_button, 0, wxALL, 5 );
import_button = new wxButton( this, ID_DIALOG_IMPORT_BUTTON, wxT("Import"), wxDefaultPosition, wxDefaultSize, 0 );
bSizer5->Add( import_button, 0, wxALL, 5 );
main_sizer->Add( bSizer5, 0, wxEXPAND, 5 );
this->SetSizer( main_sizer );
this->Layout();
this->Centre( wxBOTH );
}
AppendGameDialogBF::~AppendGameDialogBF()
{
}

View file

@ -0,0 +1,51 @@
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version 3.10.1-40-g8042f487)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////
#pragma once
#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/string.h>
#include <wx/stattext.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/listbox.h>
#include <wx/button.h>
#include <wx/bitmap.h>
#include <wx/image.h>
#include <wx/icon.h>
#include <wx/sizer.h>
#include <wx/dialog.h>
///////////////////////////////////////////////////////////////////////////
#define ID_DIALOG_CANCEL_BUTTON 1000
#define ID_DIALOG_IMPORT_BUTTON 1001
///////////////////////////////////////////////////////////////////////////////
/// Class AppendGameDialogBF
///////////////////////////////////////////////////////////////////////////////
class AppendGameDialogBF : public wxDialog
{
private:
protected:
wxStaticText* main_label;
wxListBox* game_list;
wxButton* cancel_button;
wxButton* import_button;
public:
AppendGameDialogBF( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 388,263 ), long style = wxDEFAULT_DIALOG_STYLE );
~AppendGameDialogBF();
};

View file

@ -36,21 +36,12 @@ BasePanelBF::BasePanelBF( wxWindow* parent, wxWindowID id, const wxPoint& pos, c
wxBoxSizer* bottom_sizer;
bottom_sizer = new wxBoxSizer( wxHORIZONTAL );
append_choice_label = new wxStaticText( this, wxID_ANY, wxT("Import games from:"), wxDefaultPosition, wxDefaultSize, 0 );
append_choice_label->Wrap( -1 );
bottom_sizer->Add( append_choice_label, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
wxArrayString append_choiceChoices;
append_choice = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, append_choiceChoices, 0 );
append_choice->SetSelection( 0 );
bottom_sizer->Add( append_choice, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
append_button = new wxButton( this, wxID_ANY, wxT("Append"), wxDefaultPosition, wxDefaultSize, 0 );
bottom_sizer->Add( append_button, 0, wxALL, 5 );
bottom_sizer->Add( 0, 0, 1, wxEXPAND, 5 );
import_button = new wxButton( this, ID_IMPORT_BUTTON, wxT("Import games"), wxDefaultPosition, wxDefaultSize, 0 );
bottom_sizer->Add( import_button, 0, wxALL, 5 );
delete_button = new wxButton( this, ID_DELETE_BUTTON, wxT("Delete selection"), wxDefaultPosition, wxDefaultSize, 0 );
bottom_sizer->Add( delete_button, 0, wxALL, 5 );

View file

@ -21,7 +21,6 @@
#include <wx/icon.h>
#include <wx/sizer.h>
#include <wx/statline.h>
#include <wx/choice.h>
#include <wx/listctrl.h>
#include <wx/panel.h>
@ -29,7 +28,8 @@
#define ID_SAVE_BUTTON 1000
#define ID_EXPORT_BUTTON 1001
#define ID_DELETE_BUTTON 1002
#define ID_IMPORT_BUTTON 1002
#define ID_DELETE_BUTTON 1003
///////////////////////////////////////////////////////////////////////////////
/// Class BasePanelBF
@ -43,9 +43,7 @@ class BasePanelBF : public wxPanel
wxButton* save_button;
wxButton* export_button;
wxStaticLine* separator_1;
wxStaticText* append_choice_label;
wxChoice* append_choice;
wxButton* append_button;
wxButton* import_button;
wxButton* delete_button;
wxListCtrl* game_list;

View file

@ -1,5 +1,6 @@
#include "BaseTab.hpp"
#include <wx/filename.h>
#include "AppendGameDialog.hpp"
BaseTab::BaseTab(wxFrame *parent, std::string base_file)
: BasePanelBF(parent), base_file(base_file), TabInfos(TabInfos::BASE),
@ -17,10 +18,17 @@ BaseTab::BaseTab(wxFrame *parent, std::string base_file)
this->Bind(wxEVT_BUTTON, &BaseTab::OnSave, this, ID_SAVE_BUTTON);
this->Bind(wxEVT_BUTTON, &BaseTab::OnExport, this, ID_EXPORT_BUTTON);
this->Bind(wxEVT_LIST_ITEM_ACTIVATED, &BaseTab::OnOpenGame, this, wxID_ANY);
this->Bind(wxEVT_BUTTON, &BaseTab::OnImport, this, ID_IMPORT_BUTTON);
current_base->SetLabel(base_file);
LoadFile();
}
void BaseTab::OnImport(wxCommandEvent &event) {
AppendGameDialog *dia=new AppendGameDialog(this,base);
dia->ShowModal();
}
void BaseTab::OnDelete(wxCommandEvent &event) {
long selected =
game_list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
@ -77,7 +85,7 @@ void BaseTab::OnExport(wxCommandEvent &event) {
GameBase *base;
if (ext == "pgn") {
base = new PGNGameBase(path);
base->Save(this->base);
base->Export(this->base);
delete base;
}
}
@ -109,4 +117,4 @@ void BaseTab::LoadFile() {
wxCommandEvent event(REFRESH_TAB_TITLE, GetId());
event.SetEventObject(this);
ProcessEvent(event);
}
}

View file

@ -22,6 +22,7 @@ public:
void OnSave(wxCommandEvent &event);
void OnExport(wxCommandEvent &event);
void OnOpenGame(wxListEvent &event);
void OnImport(wxCommandEvent &event);
void *GetGame() { return (NULL); }
void *GetBase() { return (base); };
};

View file

@ -14,5 +14,10 @@ public:
virtual bool NextGame() = 0;
virtual std::string GetTag(std::string tag) = 0;
virtual void Reset() = 0;
virtual void Save(GameBase *base) = 0;
/**
* @brief Save the given base into current base format (export)
*
* @param base
*/
virtual void Export(GameBase *base) = 0;
};

View file

@ -98,7 +98,7 @@ void PGNGameBase::Save(std::vector<std::uint32_t> to_ignore,
wxRemoveFile(tmp);
}
void PGNGameBase::Save(GameBase *base) {
void PGNGameBase::Export(GameBase *base) {
wxFile new_pgn(file, wxFile::write);
base->Reset();

View file

@ -16,5 +16,5 @@ public:
std::vector<GameBase *> new_games_bases,
std::vector<Game *> new_games);
void Reset();
void Save(GameBase *base);
void Export(GameBase *base);
};