CopperSpice API
1.9.2
|
The CopperSpice drag and drop system is fully supported by the model/view architecture. Items in lists, tables, and trees can be dragged within the views and data can be imported and exported as MIME-encoded data. Drag and drop operations are not initially enabled.
To allow items to be dragged, certain properties of the view need to be enabled and the model must also allow dragging.
For a model to support dragging requires implementing one method and to support dropping required implementing three additional methods. These methods are all implemented in the QStandardItemModel.
Refer to the Inheriting from a Model for more information about enabling drag and drop support in custom models.
For custom models the method for QAbstractItemModel::flags() must be implemented. The return value is an enum which indicates if the item is a valid source for dragging or a target for dropping. For example, a custom model which inherits from QAbstractListModel can enable drag and drop for every item by returning flags containing Qt::ItemIsDragEnabled and Qt::ItemIsDropEnabled.
Items can be dropped into the top level of a view but dragging is only enabled for valid items.
When a drag and drop operation in a view is requested, the model will be queried to determine which operations it supports and the MIME types it will accept. This query is made by calling the QAbstractItemModel::supportedDropActions() and QAbstractItemModel::mimeTypes() methods. If a model does not provide an implementation of these methods the default implementation from QAbstractItemModel will be used and only supports copy operations and the default internal MIME type.
When serialized data is dropped in a view the data is inserted in the current model by calling QAbstractItemModel::dropMimeData(). The default implementation of this method will never overwrite data in the model. Instead, this method inserts the new item(s) as a sibling of a existing item or as a child of an existing item.
To support drag operations it will be necessary to implement the following methods which remove data from the model.
To use the QAbstractItemModel default implementation and support the built-in MIME type, custom models must implement the following four methods.
insertRows() | Enables the model to insert new data |
insertColumns() | |
setData() | Allows the new rows and columns to be populated with items |
setItemData() | Provides efficient support for populating new items |
To accept other MIME types these methods must be implemented.
supportedDropActions() | Return a combination of the Qt::DropAction values which indicate the types of drag and drop operations this model accepts |
mimeTypes() | Return a list of MIME types which the model will accept |
dropMimeData() | Decodes the data received by the drop operation, determines where in the model it should be added, and inserts new rows and columns |
If the implementation of dropMimeData() changes the contents of a model, by inserting or removing rows or columns, or by modifying existing data, the model must emit all relevant signals.
The following example shows an implementation of QAbstractItemModel::dropMimeData() which is part of a custom model. The class MyDragDropListModel inherits from QStringListModel and the view would be a simple one column list of strings.
The implementation of this method first checks if the operation is valid and the data was supplied in a format which can be used. If the data does not have the correct built-in MIME type or column number for the drop is invalid, the method returns false and the drop does not happen. In this example the dropped data can be inserted between existing items, before the first item, or after the last item.
Refer to Decode Imported Data
When items are dragged from a model they must be encoded into some format corresponding to one or more MIME types. Models declare the MIME types they can export by implementing the QAbstractItemModel::mimeTypes() method which must return a list of MIME types.
This example shows a model which only provides support for the plain text MIME type.
The model must also implement the method mimeData() to encode the data in the required format. The following code shows how each item of data, corresponding to a given list of indexes, is encoded as plain text and stored in a QMimeData object.
Since a list of model indexes is supplied this approach is general enough to be used in both hierarchical and non-hierarchical models. Custom data types must be declared as meta objects and stream operators must be implemented for them.
The last part of implementing the MyDragDropListModel::dropMimeData() method is to decode the dropped data and then insert it into the model. The following code shows only these two parts.