First pass for imports (with OFX imports)
This commit is contained in:
246
src/view/ImportPanel.cpp
Normal file
246
src/view/ImportPanel.cpp
Normal file
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
Copyright 2010-2011 Grégory Soutadé
|
||||
|
||||
This file is part of KissCount.
|
||||
|
||||
KissCount is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
KissCount is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with KissCount. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ImportPanel.h"
|
||||
|
||||
enum {OPEN_FILE_ID=1, BUTTON_OPEN_ID, NAME_ID, BUTTON_LOAD_ID, BUTTON_INTEGRATE_ID, OPS_GRID_ID};
|
||||
|
||||
BEGIN_EVENT_TABLE(ImportPanel, wxPanel)
|
||||
EVT_GRID_CMD_CELL_CHANGE(OPS_GRID_ID, ImportPanel::OnOperationModified)
|
||||
EVT_BUTTON(BUTTON_OPEN_ID, ImportPanel::OnFile)
|
||||
EVT_TEXT_ENTER(OPEN_FILE_ID, ImportPanel::OnFileEnter)
|
||||
EVT_BUTTON(BUTTON_LOAD_ID, ImportPanel::OnLoadOperations)
|
||||
EVT_SHOW(ImportPanel::OnShow)
|
||||
END_EVENT_TABLE()
|
||||
|
||||
ImportPanel::ImportPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, parent)
|
||||
{
|
||||
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
|
||||
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
|
||||
wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL);
|
||||
wxButton* buttonOpen;
|
||||
wxRect rect = wxDisplay().GetGeometry();
|
||||
int w, h;
|
||||
wxStaticBox* staticAccount = new wxStaticBox(this, wxID_ANY, _("Unresolved accounts"));
|
||||
|
||||
SetSizer(vbox);
|
||||
|
||||
_fileTxt = new wxTextCtrl(this, OPEN_FILE_ID);
|
||||
_fileTxt->SetWindowStyle(_fileTxt->GetWindowStyle() | wxTE_PROCESS_ENTER);
|
||||
_fileTxt->GetSize(&w, &h);
|
||||
wxSize size(rect.width/3, h);
|
||||
_fileTxt->SetMinSize(size);
|
||||
buttonOpen = new wxButton(this, BUTTON_OPEN_ID, wxT("..."));
|
||||
|
||||
_buttonLoadOperations = new wxButton(this, BUTTON_LOAD_ID, wxT("Load operations"));
|
||||
_buttonLoadOperations->Disable();
|
||||
|
||||
_buttonIntegrate = new wxButton(this, BUTTON_INTEGRATE_ID, wxT("Integrate operations"));
|
||||
_buttonIntegrate->Disable();
|
||||
|
||||
hbox->Add(_fileTxt, 0, wxGROW|wxALL, 5);
|
||||
hbox->Add(buttonOpen, 0, wxALL, 5);
|
||||
hbox->Add(_buttonLoadOperations, 0, wxALL, 5);
|
||||
hbox->Add(_buttonIntegrate, 0, wxALL, 5);
|
||||
|
||||
vbox->Add(hbox, 0);
|
||||
|
||||
_accountsGrid = new wxGrid(this, wxID_ANY);
|
||||
_accountsGrid->CreateGrid(0, 2);
|
||||
_accountsGrid->SetRowLabelSize(0);
|
||||
_accountsGrid->SetColLabelValue(0, _("File account"));
|
||||
_accountsGrid->SetColLabelValue(1, _("Internal account"));
|
||||
_accountsGrid->Fit();
|
||||
|
||||
wxStaticBoxSizer* staticBoxSizer = new wxStaticBoxSizer (staticAccount, wxVERTICAL);
|
||||
staticBoxSizer->Add(_accountsGrid, 0, wxGROW|wxALL, 2);
|
||||
|
||||
_operationsGrid = new GridAccount(kiss, this, OPS_GRID_ID, false, false, false);
|
||||
|
||||
hbox2->Add(staticBoxSizer, 0, wxGROW|wxALL, 15);
|
||||
hbox2->Add(_operationsGrid, 0, wxGROW|wxALL, 15);
|
||||
|
||||
vbox->Add(hbox2, wxGROW);
|
||||
|
||||
Fit();
|
||||
|
||||
SetMinSize(wxSize(rect.width-rect.x-15, rect.height-rect.y-128-25));
|
||||
SetMaxSize(wxSize(rect.width-rect.x-15, rect.height-rect.y-128-25));
|
||||
SetScrollbars(10, 10, 100/10, 100/10);
|
||||
}
|
||||
|
||||
KissPanel* ImportPanel::CreatePanel()
|
||||
{
|
||||
return new ImportPanel(_kiss, _wxUI);
|
||||
}
|
||||
|
||||
wxBitmapButton* ImportPanel::GetButton(int id)
|
||||
{
|
||||
if (!_KissButton)
|
||||
_KissButton = new wxBitmapButton(_wxUI, id, wxBitmap(wxT(PREFS_ICON), wxBITMAP_TYPE_PNG), wxDefaultPosition, wxSize(128, 128));
|
||||
|
||||
return _KissButton;
|
||||
}
|
||||
|
||||
wxString ImportPanel::GetToolTip()
|
||||
{
|
||||
return _("Import");
|
||||
}
|
||||
|
||||
void ImportPanel::OnShow(wxShowEvent& event)
|
||||
{
|
||||
_wxUI->SetTitle(_("KissCount - Import"));
|
||||
}
|
||||
|
||||
void ImportPanel::OnFile(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
wxFileDialog openFileDialog(this, _("Choose a database to open"), wxT(""), wxT(""),
|
||||
_kiss->GetImportEngineExtensions(), wxFD_OPEN|wxFD_FILE_MUST_EXIST);
|
||||
|
||||
if (openFileDialog.ShowModal() == wxID_CANCEL)
|
||||
return;
|
||||
|
||||
_fileTxt->Clear();
|
||||
|
||||
*_fileTxt << openFileDialog.GetPath();
|
||||
|
||||
ProcessFile();
|
||||
}
|
||||
|
||||
void ImportPanel::OnFileEnter(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
ProcessFile();
|
||||
}
|
||||
|
||||
void ImportPanel::ProcessFile()
|
||||
{
|
||||
std::vector<wxString> accounts;
|
||||
User* user = _kiss->GetUser();
|
||||
int i;
|
||||
wxGridCellChoiceEditor* accountEditor;
|
||||
wxString* userAccounts;
|
||||
std::map<wxString, wxString> resolvedAccounts;
|
||||
wxCommandEvent event;
|
||||
|
||||
wxString path = _fileTxt->GetLineText(0);
|
||||
|
||||
_buttonLoadOperations->Disable();
|
||||
_buttonIntegrate->Disable();
|
||||
_accountsGrid->ClearGrid();
|
||||
_operationsGrid->ClearGrid();
|
||||
|
||||
_importEngine = _kiss->GetImportEngine(path);
|
||||
|
||||
if (!_importEngine)
|
||||
{
|
||||
wxMessageBox(_("Any engine can process this file !"), wxT("KissCount"), wxICON_INFORMATION | wxOK);
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
accounts = _importEngine->ParseFile();
|
||||
|
||||
if (accounts.size())
|
||||
{
|
||||
int nb_accounts = user->GetAccountsNumber();
|
||||
userAccounts = new wxString[nb_accounts+1];
|
||||
|
||||
userAccounts[0] = _("Create one");
|
||||
|
||||
for(i=0; i<nb_accounts; i++)
|
||||
userAccounts[i+1] = user->_accounts[i].name;
|
||||
|
||||
accountEditor = new wxGridCellChoiceEditor(nb_accounts+1, userAccounts, false);
|
||||
|
||||
_buttonLoadOperations->Enable();
|
||||
|
||||
// std::cout << "Insert " << accounts.size() << " rows\n";
|
||||
_accountsGrid->AppendRows(accounts.size());
|
||||
|
||||
for (i=0; i<(int)accounts.size(); i++)
|
||||
{
|
||||
_accountsGrid->SetCellValue(i, 0, accounts[i]);
|
||||
_accountsGrid->SetReadOnly(i, 0);
|
||||
_accountsGrid->SetCellValue(i, 1, userAccounts[0]);
|
||||
|
||||
_accountsGrid->SetCellEditor(i, 1, accountEditor);
|
||||
}
|
||||
|
||||
_accountsGrid->AutoSize();
|
||||
_accountsGrid->Layout();
|
||||
}
|
||||
else
|
||||
{
|
||||
OnLoadOperations(event);
|
||||
}
|
||||
Layout();
|
||||
}
|
||||
|
||||
void ImportPanel::OnLoadOperations(wxCommandEvent& WXUNUSED(event))
|
||||
{
|
||||
std::map<wxString, wxString> resolvedAccounts;
|
||||
int i;
|
||||
User* user = _kiss->GetUser();
|
||||
|
||||
for(i=0; i<_accountsGrid->GetNumberRows(); i++)
|
||||
{
|
||||
resolvedAccounts[_accountsGrid->GetCellValue(i, 0)] =
|
||||
user->GetAccountId(_accountsGrid->GetCellValue(i, 1));
|
||||
}
|
||||
|
||||
_operations = _importEngine->GetOperations(resolvedAccounts);
|
||||
|
||||
if (_operations->size())
|
||||
{
|
||||
_operationsGrid->LoadOperations(_operations, 0, 0);
|
||||
_buttonIntegrate->Enable();
|
||||
|
||||
Fit();
|
||||
}
|
||||
else
|
||||
{
|
||||
wxMessageBox(_("No operation found into this file"), wxT("KissCount"), wxICON_INFORMATION | wxOK);
|
||||
}
|
||||
}
|
||||
|
||||
void ImportPanel::OnOperationModified(wxGridEvent& event)
|
||||
{
|
||||
int col = event.GetCol();
|
||||
int row;
|
||||
static bool update = false;
|
||||
|
||||
if (col != DESCRIPTION && col != CATEGORY && col != ACCOUNT) return ;
|
||||
|
||||
if (update) return;
|
||||
|
||||
update = true;
|
||||
|
||||
row = event.GetRow();
|
||||
|
||||
_operationsGrid->ClearGrid();
|
||||
|
||||
if (_importEngine->UpdatePattern(row-1) > 1)
|
||||
_operationsGrid->LoadOperations(_operations, 0, 0);
|
||||
|
||||
// sleep(1);
|
||||
|
||||
Fit();
|
||||
|
||||
update = false;
|
||||
}
|
Reference in New Issue
Block a user