Abstract means of representing data in an application, used to decouple data and data changes from UI elements.
CYIAbstractDataModel represents arbitrary data as a hierarchy of nested tables. This table contains rows and columns referring to cells which may themselves contain tables. These cells are accessed using CYIDataModelIndex objects which can be acquired via CYIAbstractDataModel::GetIndex. Changes to the data model are signalled to the user through the use of CYISignal. To be used in model-view-controller application architecture.
CYIDataModelIndex may refer to a sub-model and/or to some data. Adding data at an index is accomplished via CYIAbstractDataModel::SetItemData, given an index. Retrieving data is accomplished via CYIAbstractDataModel::GetItemData, again given an index. If an index is referring to a sub-model, indices from that sub-model are also accessed through CYIAbstractDataModel::GetIndex but with the parent index provided.
A data model with no nested tables and with only one column is a list:
int32_t nItems = 10;
CYIAbstractDataModel model(nItems, 1); // nItems rows, 1 column
for (int32_t i = 0; i < nItems; i++)
{
model.SetItemData(model.GetIndex(i, 0), CYIAny(CYIString::FromValue<int32_t>(i)));
}
for (int32_t i = 0; i < nItems; i++)
{
CYIAny listData = model.GetItemData(model.GetIndex(i, 0));
YI_LOGD("DATAMODEL", "Contains %s at index %d", listData.Get<CYIString>().GetData(), i);
}
A data model with no nested tables and with multiple rows and columns is a table:
int32_t nRows = 10;
int32_t nColumns = 10;
CYIAbstractDataModel model(nRows, nColumns);
for (int32_t i = 0; i < nRows; i++)
{
for (int32_t j = 0; j < nColumns; j++)
{
CYIString cellData = "(" + CYIString::FromValue<int32_t>(i) +" , " + CYIString::FromValue<int32_t>(j) +")";
model.SetItemData(model.GetIndex(i, j), CYIAny(cellData));
}
}
for (int32_t i = 0; i < nRows; i++)
{
for (int32_t j = 0; j < nColumns; j++)
{
CYIString cellData = model.GetItemData(model.GetIndex(i, j)).Get<CYIString>();
YI_LOGD("DATAMODEL", "Table contains %s at (%d, %d)", cellData.GetData(), i, j);
}
}
A hierarchical data model containing a list with sub-lists is a tree:
int32_t nNodes = 10;
int32_t nSubNodes = 10;
// The root has 10 nodes, and each one of those has 10 of its own.
CYIAbstractDataModel model(nNodes, 1);
for (int32_t i = 0; i < nNodes; i++)
{
CYIDataModelIndex parentNode = model.GetIndex(i, 0);
model.SetItemData(parentNode, CYIAny(CYIString("ParentNode") + CYIString::FromValue<int32_t>(i)));
for (int32_t j = 0; j < nSubNodes; j++)
{
model.InsertRow(j, parentNode); // Adding a row to the parent
model.SetItemData(model.GetIndex(j, 0, parentNode), CYIAny(CYIString("LeafNode") + CYIString::FromValue<int32_t>(j)));
}
}
for (int32_t i = 0; i < nNodes; i++)
{
CYIDataModelIndex parentNode = model.GetIndex(i, 0);
CYIString parentData = model.GetItemData(parentNode).Get<CYIString>();
YI_LOGD("DATAMODEL", "Node contains: %s", parentData.GetData());
for (int32_t j = 0; j < nSubNodes; j++)
{
CYIString leafData = model.GetItemData(model.GetIndex(j, 0, parentNode)).Get<CYIString>();
YI_LOGD("DATAMODEL", " Leaf node contains: %s", leafData.GetData());
}
}Classes | |
| class | CYIAbstractDataModel |
| Represents arbitrary data as a hierarchy of tables. More... | |
| class | CYIDataModelIndex |
| This class is used to locate data in a CYIAbstractDataModel. More... | |