Added classes to manage simple tabular data of known structure using a
popup containing a wxGrid object.
BasePicker : Abstract class adding a popup to wxPickerBase styled with
a wxTextCtrl.
BaseGridPicker : Abstract class adding a wxGrid in the popup.
JsonGridPickerCtrl : UI control storing the tabular data in a JSON
array.
LBoundJsonGridPicker : connect JsonGridPicker to database.
LGridJsonCellEditor : use LBoundJsonGridPicker in other wxGrid objects.
LGridJsonCellRenderer : render cell JSON data.
XmlGridPickerCtrl : UI control storing the tabular data in as XML
document.
LBoundXmlGridPicker : connect XmlGridPicker to database.
LGridXmlCellEditor : grid editor for LBoundXmlGridPicker.
LGridXmlCellRenderer : grid renderer for LBoundXmlGridPicker.
JsonHelper and XmlHelper : for applications to quickly get intent value
from database data..
The structure of managed tabular data :
Column 1 : Intent - this is what we want to store/edit. This can be
telephone numbers, email addresses, instant messaging addresses...
any single line piece of information that can exist many times for one
entity (person, company...).
Column 2 : Type - A short description of the intent : 'Home, Work,
Mobile, Fax, Other...'. It is displayed in a non editable wxComboBox.
Column 3 : Preferred - One line of data can be selected as the preferred
one. It is not mandatory, but it must be a single choice.
Column 4 : Notes - single line notes.
Adjust sql scripts and L7.dox.
Applied ANSI formatting style to all files.
Other changes :
Work around a nasty misbehavior.
Grid columns edited by a translated combobox expect full string
data as cell values. LResultSet::BEData() will report these mapped strings,
instead of database real data. LBoundComboBox::IsDirty() will always be
true even if the editor is unchanged once created.
Simplest workaround : disconnect m_BoundComboBox if unchanged.
LGridTextEditor::ProvideFormEditor() : set the form editor's value
explicitely.; wxTextCtrl does not interpret data it receives
LBoundControl::SetNull must be void.
LBoundGrid : Unbind:: instructions should limit to the widget's id, like Bind::
LConnection::GetReturnedKeys should return NULL.
LConnection::SetData should return void.
Notes : wxJSON must be configured with the same prefix as wxWidgets,
here /usr/local/{wxWidgets,wxWidgets-Release}.
This commit is contained in:
171
L7/special/JsonGridPickerCtrl.cpp
Normal file
171
L7/special/JsonGridPickerCtrl.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* File: JsonGridPickerCtrl.cpp
|
||||
* Author: SET - nmset@netcourrier.com
|
||||
* License : LGPL version 2.1
|
||||
* Copyright SET, M. D. - © 2014
|
||||
*
|
||||
* Created on December 7, 2019, 10:01 PM
|
||||
*/
|
||||
|
||||
/*
|
||||
* [
|
||||
{
|
||||
"Intent" : "a@b.com",
|
||||
"Type" : "Home",
|
||||
"Preferred" : 0,
|
||||
"Notes" : "Do not use"
|
||||
},
|
||||
{
|
||||
"Intent" : "c@d.com",
|
||||
"Type" : "Work",
|
||||
"Preferred" : 1,
|
||||
"Notes" : "Neither"
|
||||
}
|
||||
]
|
||||
|
||||
*/
|
||||
#include "JsonGridPickerCtrl.h"
|
||||
#include <wx/jsonreader.h>
|
||||
#include <wx/jsonwriter.h>
|
||||
|
||||
IMPLEMENT_CLASS(JsonGridPickerCtrl, BaseGridPicker)
|
||||
|
||||
#define EMPTY_ARR _T("[]")
|
||||
|
||||
JsonGridPickerCtrl::JsonGridPickerCtrl(wxWindow *parent,
|
||||
wxWindowID id,
|
||||
const wxArrayString& types,
|
||||
wxSize popupSize,
|
||||
const wxString& text,
|
||||
const wxPoint& pos,
|
||||
const wxSize& size,
|
||||
long style,
|
||||
const wxValidator& validator,
|
||||
const wxString& name)
|
||||
: BaseGridPicker(parent, id, types, popupSize, text, pos, size, style, validator, name)
|
||||
{
|
||||
/*
|
||||
* Don't create the grid here. Let SetValue() do it, deleting the previous
|
||||
* one every time new data is fed in.
|
||||
*/
|
||||
SetValue(wxEmptyString);
|
||||
}
|
||||
|
||||
JsonGridPickerCtrl::~JsonGridPickerCtrl()
|
||||
{
|
||||
}
|
||||
|
||||
void JsonGridPickerCtrl::SetValue(const wxString& value)
|
||||
{
|
||||
if (value.IsEmpty())
|
||||
{
|
||||
m_value = EMPTY_ARR;
|
||||
BaseGridPicker::CreateGrid(); // +++
|
||||
GetTextCtrl()->SetValue(INVALID_INTENT);
|
||||
return;
|
||||
}
|
||||
m_value = value;
|
||||
BaseGridPicker::CreateGrid(); // +++
|
||||
GetTextCtrl()->SetValue(INVALID_INTENT);
|
||||
|
||||
wxJSONReader reader(wxJSONREADER_STRICT);
|
||||
wxJSONValue root;
|
||||
uint nbErr = reader.Parse(m_value, &root);
|
||||
if (nbErr > 0)
|
||||
{
|
||||
const wxArrayString& errors = reader.GetErrors();
|
||||
for (int i = 0; i < nbErr; i++)
|
||||
{
|
||||
wxASSERT_MSG(nbErr == 0, errors[i]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!root.IsArray())
|
||||
{
|
||||
wxASSERT_MSG(root.IsArray(), _("JSON data is not an array"));
|
||||
return;
|
||||
}
|
||||
for (uint row = 0; row < root.Size(); row++)
|
||||
{
|
||||
if (root[row]["Preferred"].IsValid()
|
||||
&& !root[row]["Preferred"].AsString().IsEmpty()
|
||||
&& root[row]["Preferred"].AsInt() != 0)
|
||||
{
|
||||
GetTextCtrl()->SetValue(root[row]["Intent"].AsString());
|
||||
return;
|
||||
}
|
||||
}
|
||||
GetTextCtrl()->SetValue(INVALID_INTENT);
|
||||
}
|
||||
|
||||
wxString JsonGridPickerCtrl::GetValue()
|
||||
{
|
||||
DumpGrid();
|
||||
/*
|
||||
* wxJSONWriter adds quotes and line breaks to EMPTY_ARR !
|
||||
* m_value is set in DumpGrid(). If there is no grid data, m_value is set
|
||||
* to \"[]\"\n
|
||||
*/
|
||||
wxJSONValue empty(EMPTY_ARR);
|
||||
wxJSONWriter writer;
|
||||
wxString sempty;
|
||||
writer.Write(empty, sempty);
|
||||
if (m_value == sempty)
|
||||
return wxEmptyString;
|
||||
return m_value;
|
||||
}
|
||||
|
||||
void JsonGridPickerCtrl::FillGrid()
|
||||
{
|
||||
wxASSERT_MSG(m_grid != NULL, _("m_grid IS NULL"));
|
||||
wxJSONReader reader(wxJSONREADER_STRICT);
|
||||
wxJSONValue root;
|
||||
uint nbErr = reader.Parse(m_value, &root);
|
||||
if (nbErr > 0)
|
||||
{
|
||||
const wxArrayString& errors = reader.GetErrors();
|
||||
for (int i = 0; i < nbErr; i++)
|
||||
{
|
||||
wxASSERT_MSG(nbErr == 0, errors[i]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!root.IsArray())
|
||||
{
|
||||
wxASSERT_MSG(root.IsArray(), _("JSON data is not an array"));
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint row = 0; row < root.Size(); row++)
|
||||
{
|
||||
m_grid->InsertRows(row);
|
||||
m_grid->SetCellValue(row, 0, root[row]["Intent"].AsString());
|
||||
m_grid->SetCellValue(row, 1, root[row]["Type"].AsString());
|
||||
m_grid->SetCellValue(row, 2, root[row]["Preferred"].AsString());
|
||||
m_grid->SetCellValue(row, 3, root[row]["Notes"].AsString());
|
||||
}
|
||||
}
|
||||
|
||||
void JsonGridPickerCtrl::DumpGrid()
|
||||
{
|
||||
wxASSERT_MSG(m_grid != NULL, _("m_grid IS NULL"));
|
||||
wxASSERT_MSG(m_stringTable != NULL, _("m_stringTable IS NULL"));
|
||||
if (!m_editable || !m_stringTable || !m_grid)
|
||||
return;
|
||||
wxJSONWriter writer;
|
||||
wxJSONValue root(EMPTY_ARR);
|
||||
uint row = 0;
|
||||
uint jrow = 0;
|
||||
for (row; row < m_stringTable->GetRowsCount(); row++)
|
||||
{
|
||||
if (m_stringTable->GetValue(row, 0).IsEmpty())
|
||||
continue;
|
||||
root[jrow]["Intent"] = m_stringTable->GetValue(row, 0);
|
||||
root[jrow]["Type"] = m_stringTable->GetValue(row, 1);
|
||||
root[jrow]["Preferred"] = (m_stringTable->GetValue(row, 2) == _T("0")
|
||||
|| m_stringTable->GetValue(row, 2).IsEmpty()) ? 0 : 1;
|
||||
root[jrow]["Notes"] = m_stringTable->GetValue(row, 3);
|
||||
jrow++;
|
||||
}
|
||||
writer.Write(root, m_value); // May be EMPTY_ARR
|
||||
}
|
||||
Reference in New Issue
Block a user