|
virtual void | sAdd () |
| Moves selected items from the "available" tree to the "selected" tree.
|
|
virtual void | sAddAll () |
| Moves all items from the "available" tree to the "selected" tree.
|
|
virtual void | sCancel () |
| Emits cancelClicked() signal.
|
|
virtual int | sCommitChanges () |
| Calls functions to execute the appropriate INSERT and DELETE queries.
|
|
virtual void | sMove (XTreeWidgetItem *pXtitem, bool pAdd) |
| Moves an item from one tree to another based on the value of pAdd.
|
|
virtual void | sRemove () |
| Moves selected items from the "selected" tree to the "available" tree.
|
|
virtual void | sRemoveAll () |
| Moves all items from the "selected" tree to the "available" tree.
|
|
virtual void | sSelect () |
| Emits selectClicked() signal.
|
|
|
ParameterList | _addConstraints |
| Name value pairs indicating column names and values to insert.
|
|
QList< XTreeWidgetItem * > | _added |
| List of items added to "available" tree since loading.
|
|
ParameterList | _equalityColumns |
| Name Value pairs indicating pairs of columns to be compared for equality.
|
|
QString | _modifyTableName |
| Name of table where items will be added or removed.
|
|
bool | _parentInTrans |
| When true, insert and delete statements will not be wrapped in a transaction block.
|
|
bool | _removeByAltId |
| When true, delete statements use the item's altId instead of id.
|
|
QString | _removeByIdTableColName |
| Column name used as a key by the delete statement to remove records.
|
|
QList< XTreeWidgetItem * > | _removed |
| List of items removed from the "selected" tree since loading.
|
|
Widget used to pick items from one list and add them to another.
The selection widget incorporates 2 XTreeWidgets, one as a source (or available) and one as a destination (or selected). Each tree will be populated from a different query but the data should be compatible with the fields on both trees otherwise moving items back and forth doesn't make sense.
The widget was designed to be adaptable and thus requires some configuration first. The following must be done before the widget will function properly:
-
Tree Setup:
The internal trees are exposed via the getAvail() and getSel() functions. Once you have pointers to the trees, they must be configured as is usual for XTreeWidgets.
-
Equality Columns:
Since each of the trees may present different attributes of the underlying data, there needs to be a way to determine equality between items on different trees. A list of name-value pairs must be passed to the setEqualityColumns() function. Each pair will contain the XTreeWidget column names to be considered equivalent. When the trees first load, any items in the "available" tree that are already present in the "selected" tree (based on the equality columns) will be filtered out of the available list.
-
Select and Cancel Buttons:
Select and cancel buttons can be shown or hidden depending on your use case with the hideCancel(), showCancel() and hideSelect(), showSelect(). These buttons don't do anything other than emit a clicked signal when clicked. They are shown by default.
The widget can either be automatic or handled manually depending on the situation.
-
Manual:
With the above setup in place, the widget will function properly and track selections. Various signals are emitted by the widget when buttons are clicked and before/after items are moved in either direction. At any point you can retrieve a list of changes from the getAdded() and getRemoved() functions. The getAdded function will return a list of items that are currently in the "selected" tree that were not there when the widget first loaded. getRemoved() returns items that were in the "selected" tree when the widget first loaded and are no longer there. Between the accessor functions and the signals, you can process the changes in whatever way suits your needs.
-
Automatic:
The widget can automatically build and execute SQL queries to add and remove items from the underlying table. In order to do that, the following must be configured:
-
Set Modification Table:
The table where new items will be added (and from which removed items will be deleted) must be passed to setModifyTableName(). Ideally this will be the table from which the "selected" tree was populated though this is not necessary. The only requirement is that calling id() or altId() on the XTreeWidget item will return an id that is sufficient to identify a single record in the table passed to setModifyTableName().
-
Set Insert Criteria:
In order to insert records into an arbitrary table, a list of column names and values to insert must be provided. A parameter list must be passed to setAddConstraints(). This list will contain one entry for each column to be populated and must, at a minimum, contain entries for each required column in the table passed to setModifyTableName(). In each entry, the parameter name must be the table column name. The parameter value can either be a literal value, the name of a column in the "selected" XTreeWidget, or the strings "id" or "altId". If the parameter value is the string "id", then the id of the row will be inserted, and if the parameter value is the string "altId", then the altId of the row will be inserted. If the parameter value matches a column name other than "id" or "altId", the contents of that column will be inserted. Otherwise the parameter value will be inserted directly. Do not name any columns to be inserted "id" or "altId" in the XTreeWidget because the id or altId of the row will be inserted instead.
-
Set Delete Criteria:
In order to delete records from an arbitrary table, a column name must be passed to setRemoveByIdTableColName() to be used as a key to identify a specific record. The widget will create a delete query which will remove records if the value returned by the XTreeWidgetItem's id() or altId() function matches the value in the column provided. id() is used by default and can be changed by calling the setRemoveByAltId() function.
- Todo
-
add a way to add a new item directly to the "selected" tree.
-
replace queries with metasql
int SelectionWidget::execAddQuery |
( |
XSqlQuery & | outQry | ) |
|
|
protected |
Builds and executes an INSERT query for each item in the _added list.
This function builds INSERT statements using _modifyTableName and _addConstraints in order to create flexible queries for any table needed. Values to be inserted are bound to "parameterN" for N = 0 to addConstraints.count() - 1. Returns the query executed to allow for error reporting outside of a transaction block.
The entries in _addConstraints will determine how the query is built. For each entry in _addConstraints, the parameter name specifies the column where the value will be inserted. the parameter value will either be a column name in the "available" tree or a literal value. If the parameter value matches a column name, the value in that column will be inserted, otherwise the parameter value itself will be inserted directly.
- Parameters
-
[out] | outQry | returns the last executed query before either detecting an error or completing. |
Moves an item from one tree to another based on the value of pAdd.
Based on the value of pAdd, one tree is designated as a source, the other as a destination. Likewise _added and _removed are designated as either a check list or an append list. pXtitem is removed from the source tree. If pXtitem is NOT in the check list, it means this is a new change we must track. pXtitem is then added to the append list. If pXtitem IS present in the check list, it means the item was previously moved, and this move is merely undoing that action. Therefore, pXtitem is only removed from the check list and not added to the append list. Finally, pXtitem is added to the destination tree. The key idea here is that depending on pAdd, the append and check lists will switch places. So the list we add an item to in one direction, will be the list we check against when going in the opposite direction.
- Parameters
-
pXtitem | XTreeWidgetItem to be moved from one tree to another. |
pAdd | True when moving from "available" tree to "selected" tree, false otherwise. |