00001
00002 #include <QtCore/qabstractitemmodel.h>
00003
00004 #include <QtCore/qabstractitemmodel.h>
00005
00006 #include "knowtreemodel.h"
00007 #include "main.h"
00008 #include "treeitem.h"
00009 #include "treemodel.h"
00010
00011
00012
00013 knowtreemodel::knowtreemodel(const QStringList &headers, QDomDocument dommodel, QObject *parent)
00014 {
00015 QMap<QString, QString> rootData;
00016
00017
00018
00019 rootData["id"]="0";
00020 rootData["name"]="";
00021
00022
00023 rootItem = new TreeItem(rootData);
00024
00025
00026 setupModelData(dommodel, rootItem);
00027 }
00028
00029
00030
00031
00032
00033 knowtreemodel::~knowtreemodel()
00034 {
00035 delete rootItem;
00036 }
00037
00038
00039
00040 void knowtreemodel::setupModelData(QDomDocument dommodel, TreeItem *parent)
00041 {
00042 QDomElement contentrootnode=dommodel.documentElement().firstChildElement("content").firstChildElement("node");
00043
00044 if(contentrootnode.isNull())
00045 {
00046 qDebug() << "Unable load xml tree, first content node not found.";
00047 return;
00048 }
00049
00050 parsenodeelement(contentrootnode,parent);
00051
00052 return;
00053 }
00054
00055
00056
00057 void knowtreemodel::parsenodeelement(QDomElement n, TreeItem *parent)
00058 {
00059 TreeItem *prnt = parent;
00060
00061
00062
00063 prnt->recordtable_init(n);
00064
00065
00066
00067 while(!n.isNull())
00068 {
00069 if(n.tagName()=="node")
00070 {
00071 QString line1,line_name,line_id;
00072 line1=n.tagName();
00073 line_name=n.attribute("name");
00074 line_id=n.attribute("id");
00075
00076
00077
00078
00079 prnt->insertChildren(prnt->childCount(), 1, 1);
00080
00081
00082 prnt->child(prnt->childCount()-1)->setData("id",line_id);
00083
00084
00085 prnt->child(prnt->childCount()-1)->setData("name",line_name);
00086
00087
00088
00089 parsenodeelement(n.firstChildElement(), prnt->child(prnt->childCount()-1) );
00090
00091 }
00092 n = n.nextSiblingElement();
00093 }
00094
00095 }
00096
00097
00098
00099 QDomElement knowtreemodel::export_fullmodeldata_to_dom(TreeItem *root)
00100 {
00101 QDomDocument doc;
00102 QDomElement elm=doc.createElement("content");
00103
00104
00105
00106 parsetreetodom(&elm,root);
00107
00108
00109
00110 return elm;
00111 }
00112
00113
00114
00115 void knowtreemodel::parsetreetodom(QDomElement *xmldata,TreeItem *curritem)
00116 {
00117
00118
00119
00120 if(curritem->recordtable_getrowcount() > 0)
00121 {
00122
00123
00124
00125 QDomDocument rectab=curritem->recordtable_export_data_to_dom();
00126
00127
00128
00129
00130 QDomElement e = rectab.documentElement();
00131
00132
00133
00134 if(!e.isNull()) xmldata->appendChild(e.cloneNode());
00135 else qDebug() << "No convert QDomDocument to element for recordtable";
00136 }
00137
00138
00139 int i;
00140 for(i=0;i<curritem->childCount();i++)
00141 {
00142
00143 QVariant vname=curritem->child(i)->data("name");
00144 QString branch_name=vname.toString();
00145
00146
00147 QVariant vid=curritem->child(i)->data("id");
00148 QString branch_id=vid.toString();
00149
00150
00151
00152
00153 QDomDocument doc;
00154 QDomElement tempelement = doc.createElement("node");
00155
00156
00157 tempelement.setAttribute("name",branch_name);
00158
00159
00160 tempelement.setAttribute("id",branch_id);
00161
00162
00163 xmldata->appendChild(tempelement);
00164
00165
00166
00167
00168 parsetreetodom(&(xmldata->lastChildElement()), curritem->child(i) );
00169 }
00170
00171 }
00172
00173
00174
00175 void knowtreemodel::add_new_child_branch(const QModelIndex &index, QString id, QString name)
00176 {
00177
00178 TreeItem *parent=getItem(index);
00179
00180 beginInsertRows(index, parent->childCount(), parent->childCount());
00181 add_new_branch(parent,id,name);
00182 endInsertRows();
00183 }
00184
00185
00186
00187 void knowtreemodel::add_new_sibling_branch(const QModelIndex &index, QString id, QString name)
00188 {
00189
00190 TreeItem *current=getItem(index);
00191 TreeItem *parent=current->parent();
00192
00193 beginInsertRows(index.parent(), parent->childCount(), parent->childCount());
00194 add_new_branch(parent,id,name);
00195 endInsertRows();
00196 }
00197
00198
00199
00200 void knowtreemodel::add_new_branch(TreeItem *parent, QString id, QString name)
00201 {
00202
00203
00204 parent->addChildren();
00205
00206
00207 parent->child(parent->childCount()-1)->setData("id",id);
00208
00209
00210 parent->child(parent->childCount()-1)->setData("name",name);
00211 }
00212
00213
00214
00215 QModelIndex knowtreemodel::move_up_branch(const QModelIndex &index)
00216 {
00217 return move_updn_branch(index,1);
00218 }
00219
00220
00221
00222 QModelIndex knowtreemodel::move_dn_branch(const QModelIndex &index)
00223 {
00224 return move_updn_branch(index,-1);
00225 }
00226
00227
00228
00229 QModelIndex knowtreemodel::move_updn_branch(const QModelIndex &index,int direction)
00230 {
00231
00232 QModelIndex swap_index=index.sibling(index.row()-direction,0);
00233
00234
00235 if(!swap_index.isValid())
00236 return QModelIndex();
00237
00238
00239
00240 int swpidx_row=swap_index.row();
00241 int swpidx_column=swap_index.column();
00242 QModelIndex swpidx_parent=swap_index.parent();
00243
00244
00245 TreeItem *current=getItem(index);
00246
00247
00248 emit layoutAboutToBeChanged();
00249
00250 bool moveok;
00251 if(direction==1) moveok=current->move_up();
00252 else moveok=current->move_dn();
00253
00254 if(moveok)
00255 {
00256 changePersistentIndex(swap_index,index);
00257 changePersistentIndex(index,swap_index);
00258 }
00259
00260 emit layoutChanged();
00261
00262
00263 if(moveok) return this->index(swpidx_row, swpidx_column, swpidx_parent);
00264 else return QModelIndex();
00265 }
00266
00267
00268
00269
00270 QModelIndex knowtreemodel::indexChildren(const QModelIndex &parent, int n) const
00271 {
00272
00273
00274 if(!parent.isValid())
00275 {
00276 qDebug() << "In indexChildren() unavailable model index";
00277 return QModelIndex();
00278 }
00279
00280
00281 TreeItem *item = getItem(parent);
00282
00283
00284
00285 if( n<0 || n>(item->childCount()-1) )
00286 {
00287 qDebug() << "In indexChildren() unavailable children number";
00288 return QModelIndex();
00289 }
00290
00291
00292 TreeItem *childitem = item->child(n);
00293
00294
00295 if(childitem)
00296 {
00297
00298
00299
00300 return index(n,0,parent);
00301 }
00302 else
00303 {
00304 qDebug() << "In indexChildren() empty child element";
00305 return QModelIndex();
00306 }
00307 }
00308
00309
00310
00311 QModelIndex knowtreemodel::get_item_index(TreeItem *item)
00312 {
00313 int itemrow=item->childNumber();
00314
00315 return this->createIndex(itemrow,0,item);
00316 }
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398