QtHPConnect/treemodel.cpp

343 lines
8.2 KiB
C++
Raw Normal View History

2020-01-16 04:38:13 +01:00
//
// Model to contain the calculator data structure
//
2019-02-10 14:32:15 +01:00
#include "treemodel.h"
2019-02-10 14:43:00 +01:00
#include "hptreeitem.h"
2019-04-02 21:14:31 +02:00
#include <QStringListModel>
#include <QMimeData>
2019-02-10 14:32:15 +01:00
2020-01-18 19:27:42 +01:00
2020-01-16 04:38:13 +01:00
//Constructor
2019-02-10 14:43:00 +01:00
treeModel::treeModel(QObject *parent)
:QStandardItemModel(parent)
2019-02-10 14:32:15 +01:00
{
2019-02-10 14:43:00 +01:00
setItemPrototype(new hpTreeItem());
createRoot();
2019-04-02 21:14:31 +02:00
setParent(parent);
2020-01-16 04:38:13 +01:00
}
2019-02-10 14:43:00 +01:00
2020-01-16 04:38:13 +01:00
//Create the start of the tree
2019-02-10 14:43:00 +01:00
int treeModel::createRoot()
{
rootNode = invisibleRootItem();
return 0;
}
2020-01-18 19:27:42 +01:00
//Create Item
AbstractData * treeModel::createData(hp_Data data_in) {
AbstractData * obj=nullptr;
log("TreeModel::Creating Data Stucture");
switch (data_in.type) {
case HP_APP: {
qDebug()<<"hpCalcData::recvData - Application";
Application * obj = new Application(data_in.name,data_in.type);
return obj;
}
break;
case HP_LIST: {
List * obj = new List(data_in.name,data_in.type);
return obj;
}
break;
case HP_MATRIX: {
qDebug()<<"hpCalcData::recvData - Matrix";
Matrix * obj = new Matrix(data_in.name,data_in.type);
return obj;
}
break;
}
return obj;
}
2019-02-10 14:43:00 +01:00
//Rework - name should be calc name
int treeModel::addCalculator(QString name, hpusb * handle){
2020-01-18 19:27:42 +01:00
hpCalcData * hpData = new hpCalcData(name, handle);
2019-02-10 14:43:00 +01:00
hpTreeItem * hpCalc = new hpTreeItem(name,hpData,0);
2019-02-10 19:23:04 +01:00
hpCalc->setType(HP_MAIN);
hpCalc->setIcon(QIcon(":/icons/monitor_32x32.png"));
hpCalc->setToolTip(QString("Calculator contents"));
2019-02-10 14:43:00 +01:00
QObject::connect(hpData, SIGNAL(dataChanged(hp_Change)),hpCalc, SLOT(dataChange(hp_Change)));
2019-02-17 17:24:52 +01:00
if (parent()!=nullptr)
QObject::connect(hpData, SIGNAL(dataChanged(hp_Change)),parent(), SLOT(dataChange(hp_Change)));
2019-02-10 14:43:00 +01:00
setHpCalcData(name,hpData,hpCalc);
rootNode->appendRow(hpCalc);
hpData->readInfo();
2019-09-15 17:23:44 +02:00
return 0;
2019-02-10 14:43:00 +01:00
}
2020-01-16 04:38:13 +01:00
//return the calculator data within the model
2019-02-10 14:43:00 +01:00
hpCalcData * treeModel::getCalculator(QString name){
hpDataLink hplink;
hpCalcData * hpdata = nullptr;
2019-03-09 15:39:38 +01:00
if (!hpCalcList.isEmpty()) {
QMap<QString, hpDataLink>::const_iterator i = hpCalcList.find(name);
2019-02-10 14:43:00 +01:00
2019-03-09 15:39:38 +01:00
if (i!=hpCalcList.end()) {
hplink = i.value();
hpdata= hplink.dataItem;
}
2019-02-10 14:43:00 +01:00
}
return hpdata;
}
2019-02-10 14:32:15 +01:00
2020-01-18 19:27:42 +01:00
//return the calculator data within the model
hpTreeItem * treeModel::getCalculatorItem(QString name){
hpDataLink hplink;
hpTreeItem * hpitem = nullptr;
if (!hpCalcList.isEmpty()) {
QMap<QString, hpDataLink>::const_iterator i = hpCalcList.find(name);
if (i!=hpCalcList.end()) {
hplink = i.value();
hpitem= hplink.treeItem;
}
}
return hpitem;
}
2020-01-16 04:38:13 +01:00
//index system for data retrieval
//review QStandardItemModel should already have one in place
2019-02-10 14:43:00 +01:00
QString treeModel::getLastDataKey() {
2019-03-09 15:39:38 +01:00
if (hpCalcList.isEmpty())
return QStringLiteral("");
else
return hpCalcList.lastKey();
2019-02-10 14:32:15 +01:00
}
2019-02-10 14:43:00 +01:00
//manage link between tree and data
//A map stores the treeItem, dataItem and in future perhaps the handle in a list
// retrievable by a string key
hpCalcData * treeModel::getHpCalcData(QString name) {
hpDataLink hplink;
2019-03-09 15:39:38 +01:00
hpCalcData * hpdata=nullptr;
2019-02-10 14:43:00 +01:00
2019-03-09 15:39:38 +01:00
if (!hpCalcList.isEmpty()) {
QMap<QString, hpDataLink>::const_iterator i = hpCalcList.find(name);
2019-02-10 14:43:00 +01:00
2019-03-09 15:39:38 +01:00
if (i!=hpCalcList.end()) {
hplink = i.value();
hpdata= hplink.dataItem;
}
2019-02-10 14:43:00 +01:00
}
return hpdata;
}
//manage link between tree and data
void treeModel::setHpCalcData(QString name, hpCalcData * data, hpTreeItem * tree ){
hpDataLink hplink;
hplink.dataItem=data;
hplink.treeItem=tree;
hpCalcList.insert(name,hplink);
return;
}
2020-01-16 04:38:13 +01:00
//Part the the drag and drop system
2019-04-02 21:14:31 +02:00
Qt::DropActions treeModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction | Qt::TargetMoveAction;
}
//Get and pass on the data to be dragged
QMimeData* treeModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData *mimeDataPtr = new QMimeData();
QByteArray mydata;
qDebug()<<"treeModel::mimeData";
mimeDataPtr->setData("application/x-qabstractmodeldatalist",mydata);
/* Store row id list */
QList<int> rowIdList;
int rowId;
foreach (QModelIndex index, indexes) {
if (index.isValid()) {
rowId = index.row();
if (!rowIdList.contains(rowId)) {
rowIdList << rowId;
}
}
}
return mimeDataPtr;
}
//Allow drop in location
bool treeModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
{
return true;
}
//Process the drop action
2020-01-16 04:38:13 +01:00
bool treeModel::dropMimeData(const QMimeData* md_data, Qt::DropAction action, int row,
int column, const QModelIndex &index)
2019-04-02 21:14:31 +02:00
{
2020-01-16 04:38:13 +01:00
QByteArray data_in;
2020-01-18 19:27:42 +01:00
AbstractData * absitem;
2020-01-16 04:38:13 +01:00
qDebug()<<"treemodel::DropMineData "<<row<<" "<<column;
2019-04-02 21:14:31 +02:00
if (action == Qt::IgnoreAction) {
2020-01-16 04:38:13 +01:00
qDebug()<<"treemodel::IgnoreAction";
2019-04-02 21:14:31 +02:00
return true;
}
2020-01-18 19:27:42 +01:00
hpTreeItem * item=nullptr;
item = static_cast<hpTreeItem *>(itemFromIndex(index));
if (item!=nullptr) {
2020-01-19 17:55:14 +01:00
hp_DataType type=HP_MAIN;
QStringList formatList;
QString mimeType;
formatList=md_data->formats();
foreach(const QString& format, formatList) {
// qDebug()<<format;
for(int i = HP_MAIN; i < HP_SETTINGS; i++) {
mimeType=mimetypes[i][1];
// qDebug()<<mimeType;
if( mimeType==format) {
type=static_cast<hp_DataType>(i);
break;
}
}
}
if ( type!=HP_MAIN) {
QString name=md_data->text();
data_in=md_data->data(mimetypes[type][1]);
2020-01-18 19:27:42 +01:00
2020-01-19 17:55:14 +01:00
QDataStream in(&data_in,QIODevice::ReadOnly);
2020-01-18 19:27:42 +01:00
2020-01-19 17:55:14 +01:00
switch(type) {
2020-01-18 19:27:42 +01:00
2020-01-19 17:55:14 +01:00
case HP_PROG: {
absitem = new Program(name, HP_PROG, QStringLiteral(""));
absitem->parseData(in);
break;
}
}
2020-01-18 19:27:42 +01:00
QString calc = item->getCalculatorName();
addItem(calc,absitem);
2020-01-19 17:55:14 +01:00
// qDebug()<<"treemodel::dropMimeData End";
}
else {
qDebug()<<"treemodel::sropMimeData type not found "<<type;
return false;
}
2020-01-18 19:27:42 +01:00
}
return true;
}
//Find the hpTreeItem of a type in a calculator
hpTreeItem * treeModel::findTypeRoot(QString calcName, hp_DataType type) {
qDebug()<<calcName;
hpTreeItem * calc=getCalculatorItem(calcName);
hpTreeItem *item;
2020-01-19 17:55:14 +01:00
// qDebug()<<calc->getGroupName();
2020-01-18 19:27:42 +01:00
QModelIndex in = calc->index();
for (int e = 0; e<calc->rowCount(); e++) {
QModelIndex si=index(e,0,in);
item = static_cast<hpTreeItem *>(itemFromIndex(si));
if(item->getType()==type) {
return item;
}
2019-04-02 21:14:31 +02:00
}
2020-01-18 19:27:42 +01:00
return nullptr;
}
//add an object to the correct place in the tree of a calaculator
int treeModel::addItem(QString calc, AbstractData * obj) {
2019-04-02 21:14:31 +02:00
2020-01-18 19:27:42 +01:00
hp_DataType type;
hpCalcData * hpdata;
if (obj!=nullptr) {
type = obj->getType();
}
else {
return -1;
2019-04-02 21:14:31 +02:00
}
2020-01-18 19:27:42 +01:00
hpTreeItem * rootitem = findTypeRoot(calc,type);
if(rootitem!=nullptr) {
hpdata=rootitem->getDataStore();
}
else {
return -1;
2020-01-16 04:38:13 +01:00
}
2020-01-18 19:27:42 +01:00
rootitem->addChild(obj);
hpdata->addData(obj);
2020-01-16 04:38:13 +01:00
2020-01-18 19:27:42 +01:00
return 0;
}
2020-01-16 04:38:13 +01:00
2019-04-02 21:14:31 +02:00
2020-01-18 19:27:42 +01:00
int treeModel::deleteItem(hpCalcData* hpcalc, AbstractData * obj) {
2019-04-02 21:14:31 +02:00
}
2020-01-18 19:27:42 +01:00
2019-04-02 21:14:31 +02:00
Qt::ItemFlags treeModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags defaultFlags = QStandardItemModel::flags(index);
if (index.isValid())
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
else
return Qt::ItemIsDropEnabled | defaultFlags;
}
2020-01-19 17:55:14 +01:00
int deletCalculator(QString name, hpusb * handle) {
}
int treeModel::deleteAllCalculators() {
hpDataLink hplink;
hpCalcData * hpdata = nullptr;
foreach(QString key, hpCalcList.keys()) {
QMap<QString, hpDataLink>::const_iterator i = hpCalcList.find(key);
hplink=i.value();
delete(hplink.dataItem);
hpCalcList.remove(key);
}
hpCalcList.clear();
return 0;
}
2019-02-10 14:43:00 +01:00
treeModel::~treeModel() {
2020-01-19 17:55:14 +01:00
deleteAllCalculators();
2020-01-18 19:27:42 +01:00
2019-09-15 17:23:44 +02:00
qDebug()<<"treeModel:: delete";
2019-02-10 14:43:00 +01:00
}