ctkDICOMModel.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QStringList>
  16. #include <QSqlDriver>
  17. #include <QSqlError>
  18. #include <QSqlQuery>
  19. #include <QSqlQueryModel>
  20. #include <QSqlRecord>
  21. #include <QSqlResult>
  22. #include <QTime>
  23. #include <QDebug>
  24. // ctkDICOMCore includes
  25. #include "ctkDICOMModel.h"
  26. #include "ctkLogger.h"
  27. static ctkLogger logger ( "org.commontk.dicom.DICOMModel" );
  28. struct Node;
  29. Q_DECLARE_METATYPE(Qt::CheckState);
  30. Q_DECLARE_METATYPE(QStringList);
  31. //------------------------------------------------------------------------------
  32. class ctkDICOMModelPrivate
  33. {
  34. Q_DECLARE_PUBLIC(ctkDICOMModel);
  35. protected:
  36. ctkDICOMModel* const q_ptr;
  37. public:
  38. ctkDICOMModelPrivate(ctkDICOMModel&);
  39. virtual ~ctkDICOMModelPrivate();
  40. void init();
  41. void fetch(const QModelIndex& indexValue, int limit);
  42. Node* createNode(int row, const QModelIndex& parentValue)const;
  43. Node* nodeFromIndex(const QModelIndex& indexValue)const;
  44. //QModelIndexList indexListFromNode(const Node* node)const;
  45. //QModelIndexList modelIndexList(Node* node = 0)const;
  46. //int childrenCount(Node* node = 0)const;
  47. // move it in the Node struct
  48. QVariant value(Node* parentValue, int row, int field)const;
  49. QVariant value(const QModelIndex& indexValue, int row, int field)const;
  50. QString generateQuery(const QString& fields, const QString& table, const QString& conditions = QString())const;
  51. void updateQueries(Node* node)const;
  52. Node* RootNode;
  53. QSqlDatabase DataBase;
  54. QList<QMap<int, QVariant> > Headers;
  55. QString Sort;
  56. QMap<QString, QVariant> SearchParameters;
  57. ctkDICOMModel::IndexType displayLevel;
  58. };
  59. //------------------------------------------------------------------------------
  60. // 1 node per row
  61. // TBD: should probably use the QStandardItems instead.
  62. struct Node
  63. {
  64. ~Node()
  65. {
  66. foreach(Node* node, this->Children)
  67. {
  68. delete node;
  69. }
  70. this->Children.clear();
  71. }
  72. ctkDICOMModel::IndexType Type;
  73. Node* Parent;
  74. QVector<Node*> Children;
  75. int Row;
  76. QSqlQuery Query;
  77. QString UID;
  78. int RowCount;
  79. bool AtEnd;
  80. bool Fetching;
  81. QMap<int, QVariant> Data;
  82. };
  83. //------------------------------------------------------------------------------
  84. ctkDICOMModelPrivate::ctkDICOMModelPrivate(ctkDICOMModel& o):q_ptr(&o)
  85. {
  86. this->RootNode = 0;
  87. this->displayLevel = ctkDICOMModel::ImageType;
  88. }
  89. //------------------------------------------------------------------------------
  90. ctkDICOMModelPrivate::~ctkDICOMModelPrivate()
  91. {
  92. delete this->RootNode;
  93. this->RootNode = 0;
  94. }
  95. //------------------------------------------------------------------------------
  96. void ctkDICOMModelPrivate::init()
  97. {
  98. QMap<int, QVariant> data;
  99. data[Qt::DisplayRole] = QString("Name");
  100. this->Headers << data;
  101. data[Qt::DisplayRole] = QString("Age");
  102. this->Headers << data;
  103. data[Qt::DisplayRole] = QString("Scan");
  104. this->Headers << data;
  105. data[Qt::DisplayRole] = QString("Date");
  106. this->Headers << data;
  107. data[Qt::DisplayRole] = QString("Subject ID");
  108. this->Headers << data;
  109. data[Qt::DisplayRole] = QString("Number");
  110. this->Headers << data;
  111. data[Qt::DisplayRole] = QString("Institution");
  112. this->Headers << data;
  113. data[Qt::DisplayRole] = QString("Referrer");
  114. this->Headers << data;
  115. data[Qt::DisplayRole] = QString("Performer");
  116. this->Headers << data;
  117. }
  118. //------------------------------------------------------------------------------
  119. Node* ctkDICOMModelPrivate::nodeFromIndex(const QModelIndex& indexValue)const
  120. {
  121. return indexValue.isValid() ? reinterpret_cast<Node*>(indexValue.internalPointer()) : this->RootNode;
  122. }
  123. /*
  124. //------------------------------------------------------------------------------
  125. QModelIndexList ctkDICOMModelPrivate::indexListFromNode(const Node* node)const
  126. {
  127. Q_Q(const ctkDICOMModel);
  128. Q_ASSERT(node);
  129. QModelIndexList indexList;
  130. Node* parentNode = node->Parent;
  131. if (parentNode == 0)
  132. {
  133. return indexList;
  134. }
  135. int field = parentNode->Query.record().indexOf("UID");
  136. int row = -1;
  137. for (row = 0; row < parentNode->RowCount; ++row)
  138. {
  139. QString uid = this->value(parentNode, row, field).toString();
  140. if (uid == node->UID)
  141. {
  142. break;
  143. }
  144. }
  145. if (row >= parentNode->RowCount)
  146. {
  147. return indexList;
  148. }
  149. for (int column = 0 ; column < this->Headers.size(); ++column)
  150. {
  151. indexList.append(q->createIndex(row, column, parentNode));
  152. }
  153. return indexList;
  154. }
  155. //------------------------------------------------------------------------------
  156. QModelIndexList ctkDICOMModelPrivate::modelIndexList(Node* node)const
  157. {
  158. QModelIndexList list;
  159. if (node == 0)
  160. {
  161. node = this->RootNode;
  162. }
  163. foreach(Node* child, node->Children)
  164. {
  165. list.append(this->indexListFromNode(child));
  166. }
  167. foreach(Node* child, node->Children)
  168. {
  169. list.append(this->modelIndexList(child));
  170. }
  171. return list;
  172. }
  173. //------------------------------------------------------------------------------
  174. int ctkDICOMModelPrivate::childrenCount(Node* node)const
  175. {
  176. int count = 0;
  177. if (node == 0)
  178. {
  179. node = this->RootNode;
  180. }
  181. count += node->Children.size();
  182. foreach(Node* child, node->Children)
  183. {
  184. count += this->childrenCount(child);
  185. }
  186. return count;
  187. }
  188. */
  189. //------------------------------------------------------------------------------
  190. Node* ctkDICOMModelPrivate::createNode(int row, const QModelIndex& parentValue)const
  191. {
  192. Node* node = new Node;
  193. Node* nodeParent = 0;
  194. if (row == -1)
  195. {// root node
  196. node->Type = ctkDICOMModel::RootType;
  197. node->Parent = 0;
  198. node->Data[Qt::CheckStateRole] = Qt::Unchecked;
  199. }
  200. else
  201. {
  202. nodeParent = this->nodeFromIndex(parentValue);
  203. nodeParent->Children.push_back(node);
  204. node->Parent = nodeParent;
  205. node->Type = ctkDICOMModel::IndexType(nodeParent->Type + 1);
  206. }
  207. node->Row = row;
  208. if (node->Type != ctkDICOMModel::RootType)
  209. {
  210. int field = 0;//nodeParent->Query.record().indexOf("UID");
  211. node->UID = this->value(parentValue, row, field).toString();
  212. node->Data[Qt::CheckStateRole] = node->Parent->Data[Qt::CheckStateRole];
  213. }
  214. node->RowCount = 0;
  215. node->AtEnd = false;
  216. node->Fetching = false;
  217. this->updateQueries(node);
  218. return node;
  219. }
  220. //------------------------------------------------------------------------------
  221. QVariant ctkDICOMModelPrivate::value(const QModelIndex& parentValue, int row, int column) const
  222. {
  223. Node* node = this->nodeFromIndex(parentValue);
  224. if (row >= node->RowCount)
  225. {
  226. const_cast<ctkDICOMModelPrivate *>(this)->fetch(parentValue, row + 256);
  227. }
  228. return this->value(node, row, column);
  229. }
  230. //------------------------------------------------------------------------------
  231. QVariant ctkDICOMModelPrivate::value(Node* parentNode, int row, int column) const
  232. {
  233. if (row < 0 || column < 0 || !parentNode || row >= parentNode->RowCount)
  234. {
  235. return QVariant();
  236. }
  237. if (!parentNode->Query.seek(row))
  238. {
  239. qDebug() << parentNode->Query.lastError();
  240. Q_ASSERT(parentNode->Query.seek(row));
  241. return QVariant();
  242. }
  243. QVariant res = parentNode->Query.value(column);
  244. Q_ASSERT(res.isValid());
  245. return res;
  246. }
  247. //------------------------------------------------------------------------------
  248. QString ctkDICOMModelPrivate::generateQuery(const QString& fields, const QString& table, const QString& conditions)const
  249. {
  250. QString res = QString("SELECT ") + fields + QString(" FROM ") + table;
  251. if (!conditions.isEmpty())
  252. {
  253. res += QString(" WHERE ") + conditions;
  254. }
  255. if (!this->Sort.isEmpty())
  256. {
  257. res += QString(" ORDER BY ") + this->Sort;
  258. }
  259. logger.debug ( "ctkDICOMModelPrivate::generateQuery: query is: " + res );
  260. return res;
  261. }
  262. //------------------------------------------------------------------------------
  263. void ctkDICOMModelPrivate::updateQueries(Node* node)const
  264. {
  265. // are you kidding me, it should be virtualized here :-)
  266. QString query;
  267. QString condition;
  268. switch(node->Type)
  269. {
  270. default:
  271. Q_ASSERT(node->Type == ctkDICOMModel::RootType);
  272. break;
  273. case ctkDICOMModel::RootType:
  274. //query = QString("SELECT FROM ");
  275. if(this->SearchParameters["Name"].toString() != ""){
  276. condition.append("PatientsName LIKE \"%" + this->SearchParameters["Name"].toString() + "%\"");
  277. }
  278. query = this->generateQuery("UID as UID, PatientsName as Name, PatientsAge as Age, PatientsBirthDate as Date, PatientID as \"Subject ID\"","Patients", condition);
  279. logger.debug ( "ctkDICOMModelPrivate::updateQueries for Root: query is: " + query );
  280. break;
  281. case ctkDICOMModel::PatientType:
  282. //query = QString("SELECT FROM Studies WHERE PatientsUID='%1'").arg(node->UID);
  283. if(this->SearchParameters["Study"].toString() != "")
  284. {
  285. condition.append("StudyDescription LIKE \"%" + this->SearchParameters["Study"].toString() + "%\"" + " AND ");
  286. }
  287. if(this->SearchParameters["Modalities"].value<QStringList>().count() > 0)
  288. {
  289. condition.append("ModalitiesInStudy IN (\"" + this->SearchParameters["Modalities"].value<QStringList>().join("\",\"") + "\") AND ");
  290. }
  291. if(this->SearchParameters["StartDate"].toString() != "" &&
  292. this->SearchParameters["EndDate"].toString() != "")
  293. {
  294. condition.append(" ( StudyDate BETWEEN \'" + QDate::fromString(this->SearchParameters["StartDate"].toString(), "yyyyMMdd").toString("yyyy-MM-dd")
  295. + "\' AND \'" + QDate::fromString(this->SearchParameters["EndDate"].toString(), "yyyyMMdd").toString("yyyy-MM-dd") + "\' ) AND ");
  296. }
  297. query = this->generateQuery("StudyInstanceUID as UID, StudyDescription as Name, ModalitiesInStudy as Scan, StudyDate as Date, AccessionNumber as Number, ReferringPhysician as Institution, ReferringPhysician as Referrer, PerformingPhysiciansName as Performer", "Studies", condition + QString("PatientsUID='%1'").arg(node->UID));
  298. logger.debug ( "ctkDICOMModelPrivate::updateQueries for Patient: query is: " + query );
  299. break;
  300. case ctkDICOMModel::StudyType:
  301. //query = QString("SELECT SeriesInstanceUID as UID, SeriesDescription as Name, BodyPartExamined as Scan, SeriesDate as Date, AcquisitionNumber as Number FROM Series WHERE StudyInstanceUID='%1'").arg(node->UID);
  302. if(this->SearchParameters["Series"].toString() != "")
  303. {
  304. condition.append("SeriesDescription LIKE \"%" + this->SearchParameters["Series"].toString() + "%\"" + " AND ");
  305. }
  306. query = this->generateQuery("SeriesInstanceUID as UID, SeriesDescription as Name, BodyPartExamined as Scan, SeriesDate as Date, AcquisitionNumber as Number","Series",condition + QString("StudyInstanceUID='%1'").arg(node->UID));
  307. logger.debug ( "ctkDICOMModelPrivate::updateQueries for Study: query is: " + query );
  308. break;
  309. case ctkDICOMModel::SeriesType:
  310. if(this->SearchParameters["ID"].toString() != "")
  311. {
  312. condition.append("SOPInstanceUID LIKE \"%" + this->SearchParameters["ID"].toString() + "%\"" + " AND ");
  313. }
  314. //query = QString("SELECT Filename as UID, Filename as Name, SeriesInstanceUID as Date FROM Images WHERE SeriesInstanceUID='%1'").arg(node->UID);
  315. query = this->generateQuery("SOPInstanceUID as UID, Filename as Name, SeriesInstanceUID as Date", "Images", condition + QString("SeriesInstanceUID='%1'").arg(node->UID));
  316. logger.debug ( "ctkDICOMModelPrivate::updateQueries for Series: query is: " + query );
  317. break;
  318. case ctkDICOMModel::ImageType:
  319. break;
  320. }
  321. node->Query = QSqlQuery(query, this->DataBase);
  322. foreach(Node* child, node->Children)
  323. {
  324. this->updateQueries(child);
  325. }
  326. }
  327. //------------------------------------------------------------------------------
  328. void ctkDICOMModelPrivate::fetch(const QModelIndex& indexValue, int limit)
  329. {
  330. Q_Q(ctkDICOMModel);
  331. Node* node = this->nodeFromIndex(indexValue);
  332. if (node->AtEnd || limit <= node->RowCount || node->Fetching/*|| bottom.column() == -1*/)
  333. {
  334. return;
  335. }
  336. node->Fetching = true;
  337. int newRowCount;
  338. const int oldRowCount = node->RowCount;
  339. // try to seek directly
  340. if (node->Query.seek(limit - 1))
  341. {
  342. newRowCount = limit;
  343. }
  344. else
  345. {
  346. newRowCount = qMax(oldRowCount, 1);
  347. if (node->Query.seek(newRowCount - 1))
  348. {
  349. while (node->Query.next())
  350. {
  351. ++newRowCount;
  352. }
  353. }
  354. else
  355. {
  356. // empty or invalid query
  357. newRowCount = 0;
  358. }
  359. node->AtEnd = true; // this is the end.
  360. }
  361. if (newRowCount > 0 && newRowCount > node->RowCount)
  362. {
  363. q->beginInsertRows(indexValue, node->RowCount, newRowCount - 1);
  364. node->RowCount = newRowCount;
  365. node->Fetching = false;
  366. q->endInsertRows();
  367. }
  368. else
  369. {
  370. node->RowCount = newRowCount;
  371. node->Fetching = false;
  372. }
  373. }
  374. //------------------------------------------------------------------------------
  375. ctkDICOMModel::ctkDICOMModel(QObject* parentObject)
  376. : Superclass(parentObject)
  377. , d_ptr(new ctkDICOMModelPrivate(*this))
  378. {
  379. Q_D(ctkDICOMModel);
  380. d->init();
  381. }
  382. //------------------------------------------------------------------------------
  383. ctkDICOMModel::~ctkDICOMModel()
  384. {
  385. }
  386. //------------------------------------------------------------------------------
  387. bool ctkDICOMModel::canFetchMore ( const QModelIndex & parentValue ) const
  388. {
  389. Q_D(const ctkDICOMModel);
  390. Node* node = d->nodeFromIndex(parentValue);
  391. return node ? !node->AtEnd : false;
  392. }
  393. //------------------------------------------------------------------------------
  394. int ctkDICOMModel::columnCount ( const QModelIndex & _parent ) const
  395. {
  396. Q_D(const ctkDICOMModel);
  397. Q_UNUSED(_parent);
  398. return d->RootNode != 0 ? d->Headers.size() : 0;
  399. }
  400. //------------------------------------------------------------------------------
  401. QVariant ctkDICOMModel::data ( const QModelIndex & dataIndex, int role ) const
  402. {
  403. Q_D(const ctkDICOMModel);
  404. if ( role == UIDRole )
  405. {
  406. Node* node = d->nodeFromIndex(dataIndex);
  407. return node ? node->UID : QString() ;
  408. }
  409. else if ( role == TypeRole )
  410. {
  411. Node* node = d->nodeFromIndex(dataIndex);
  412. return node ? node->Type : 0;
  413. }
  414. else if ( dataIndex.column() == 0 && role == Qt::CheckStateRole)
  415. {
  416. Node* node = d->nodeFromIndex(dataIndex);
  417. return node ? node->Data[Qt::CheckStateRole] : 0;
  418. }
  419. if (role != Qt::DisplayRole && role != Qt::EditRole)
  420. {
  421. if (dataIndex.column() != 0)
  422. {
  423. return QVariant();
  424. }
  425. Node* node = d->nodeFromIndex(dataIndex);
  426. if (!node)
  427. {
  428. return QVariant();
  429. }
  430. return node->Data[role];
  431. }
  432. QModelIndex parentIndex = this->parent(dataIndex);
  433. Node* parentNode = d->nodeFromIndex(parentIndex);
  434. if (dataIndex.row() >= parentNode->RowCount)
  435. {
  436. const_cast<ctkDICOMModelPrivate *>(d)->fetch(dataIndex, dataIndex.row());
  437. }
  438. QString columnName = d->Headers[dataIndex.column()][Qt::DisplayRole].toString();
  439. int field = parentNode->Query.record().indexOf(columnName);
  440. if (field < 0)
  441. {
  442. // Not all the columns are in the record, it's ok to have no field here.
  443. // Return an empty string in that case (not a QVariant() that means it's
  444. // invalid).
  445. return QString();
  446. }
  447. return d->value(parentIndex, dataIndex.row(), field);
  448. }
  449. //------------------------------------------------------------------------------
  450. void ctkDICOMModel::fetchMore ( const QModelIndex & parentValue )
  451. {
  452. Q_D(ctkDICOMModel);
  453. Node* node = d->nodeFromIndex(parentValue);
  454. d->fetch(parentValue, qMax(node->RowCount, 0) + 256);
  455. }
  456. //------------------------------------------------------------------------------
  457. Qt::ItemFlags ctkDICOMModel::flags ( const QModelIndex & modelIndex ) const
  458. {
  459. Q_D(const ctkDICOMModel);
  460. Qt::ItemFlags indexFlags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  461. if (modelIndex.column() != 0)
  462. {
  463. return indexFlags;
  464. }
  465. Node* node = d->nodeFromIndex(modelIndex);
  466. if (!node)
  467. {
  468. return indexFlags;
  469. }
  470. bool checkable = true;
  471. node->Data[Qt::CheckStateRole].toInt(&checkable);
  472. indexFlags = indexFlags | (checkable ? Qt::ItemIsUserCheckable : Qt::NoItemFlags);
  473. return indexFlags;
  474. }
  475. //------------------------------------------------------------------------------
  476. bool ctkDICOMModel::hasChildren ( const QModelIndex & parentIndex ) const
  477. {
  478. Q_D(const ctkDICOMModel);
  479. // only items in the first columns have index, shortcut the following for
  480. // speed issues.
  481. if (parentIndex.column() > 0)
  482. {
  483. return false;
  484. }
  485. Node* node = d->nodeFromIndex(parentIndex);
  486. if (!node)
  487. {
  488. return false;
  489. }
  490. // We want to show only until displayLevel
  491. if(node->Type >= d->displayLevel)return false;
  492. // It's not because we don't have row that we don't have children, maybe it
  493. // just means that the children haven't been fetched yet
  494. if (node->RowCount == 0 && !node->AtEnd)
  495. {
  496. // We don't want to fetch the data because we don't want to add children
  497. // to the index yet (it would be a mess to add rows inside a hasChildren)
  498. //const_cast<qCTKDCMTKModelPrivate*>(d)->fetch(parentIndex, 1);
  499. bool res = node->Query.seek(0);
  500. if (!res)
  501. {
  502. // now we know there is no children to the node, don't try next time.
  503. node->AtEnd = true;
  504. }
  505. return res;
  506. }
  507. return node->RowCount > 0;
  508. }
  509. //------------------------------------------------------------------------------
  510. QVariant ctkDICOMModel::headerData(int section, Qt::Orientation orientation, int role)const
  511. {
  512. Q_D(const ctkDICOMModel);
  513. if (orientation == Qt::Vertical)
  514. {
  515. if (role != Qt::DisplayRole)
  516. {
  517. return QVariant();
  518. }
  519. return section;
  520. }
  521. if (section < 0 || section >= d->Headers.size())
  522. {
  523. return QVariant();
  524. }
  525. return d->Headers[section][role];
  526. }
  527. //------------------------------------------------------------------------------
  528. QModelIndex ctkDICOMModel::index ( int row, int column, const QModelIndex & parentIndex ) const
  529. {
  530. Q_D(const ctkDICOMModel);
  531. // only the first column has children
  532. if (d->RootNode == 0 || parentIndex.column() > 0)
  533. {
  534. return QModelIndex();
  535. }
  536. Node* parentNode = d->nodeFromIndex(parentIndex);
  537. int field = 0;// always 0//parentNode->Query.record().indexOf("UID");
  538. QString uid = d->value(parentIndex, row, field).toString();
  539. Node* node = 0;
  540. foreach(Node* tmpNode, parentNode->Children)
  541. {
  542. if (tmpNode->UID == uid)
  543. {
  544. node = tmpNode;
  545. break;
  546. }
  547. }
  548. // TODO: Here it is assumed that ctkDICOMModel::index is called with valid
  549. // arguments, we should probably be a bit more careful.
  550. if (node == 0)
  551. {
  552. node = d->createNode(row, parentIndex);
  553. }
  554. return this->createIndex(row, column, node);
  555. }
  556. //------------------------------------------------------------------------------
  557. QModelIndex ctkDICOMModel::parent ( const QModelIndex & indexValue ) const
  558. {
  559. Q_D(const ctkDICOMModel);
  560. Node* node = d->nodeFromIndex(indexValue);
  561. Q_ASSERT(node);
  562. Node* parentNode = node->Parent;
  563. if (parentNode == 0)
  564. {// node is root
  565. return QModelIndex();
  566. }
  567. return parentNode == d->RootNode ? QModelIndex() : this->createIndex(parentNode->Row, 0, parentNode);
  568. /* need to recalculate the parent row
  569. Node* greatParentNode = parentNode->Parent;
  570. if (greatParentNode == 0)
  571. {
  572. return QModelIndex();
  573. }
  574. int field = greatParentNode->Query.record().indexOf("UID");
  575. int row = -1;
  576. for (row = 0; row < greatParentNode->RowCount; ++row)
  577. {
  578. QString uid = d->value(greatParentNode, row, field).toString();
  579. if (uid == parentNode->UID)
  580. {
  581. break;
  582. }
  583. }
  584. Q_ASSERT(row < greatParentNode->RowCount);
  585. return this->createIndex(row, 0, parentNode);
  586. */
  587. }
  588. //------------------------------------------------------------------------------
  589. int ctkDICOMModel::rowCount ( const QModelIndex & parentValue ) const
  590. {
  591. Q_D(const ctkDICOMModel);
  592. if (d->RootNode == 0 || parentValue.column() > 0)
  593. {
  594. return 0;
  595. }
  596. Node* node = d->nodeFromIndex(parentValue);
  597. Q_ASSERT(node);
  598. // Returns the amount of rows currently cached on the client.
  599. return node ? node->RowCount : 0;
  600. }
  601. //------------------------------------------------------------------------------
  602. bool ctkDICOMModel::setData(const QModelIndex &index, const QVariant &value, int role)
  603. {
  604. Q_D(const ctkDICOMModel);
  605. if (role != Qt::CheckStateRole)
  606. {
  607. return false;
  608. }
  609. Node* node = d->nodeFromIndex(index);
  610. if (!node || node->Data[role] == value)
  611. {
  612. return false;
  613. }
  614. node->Data[role] = value;
  615. emit dataChanged(index, index);
  616. for(int i=0; i<node->Children.count(); i++)
  617. {
  618. this->setChildData(index.child(i,0), value, role);
  619. }
  620. if(index.parent().isValid())
  621. {
  622. this->setParentData(index.parent(), value, role);
  623. }
  624. return true;
  625. }
  626. //------------------------------------------------------------------------------
  627. bool ctkDICOMModel::setChildData(const QModelIndex &index, const QVariant &value, int role)
  628. {
  629. Q_D(const ctkDICOMModel);
  630. if (role != Qt::CheckStateRole)
  631. {
  632. return false;
  633. }
  634. Node* node = d->nodeFromIndex(index);
  635. if (!node || node->Data[role] == value)
  636. {
  637. return false;
  638. }
  639. node->Data[role] = value;
  640. emit dataChanged(index, index);
  641. for(int i=0; i<node->Children.count(); i++)
  642. {
  643. this->setData(index.child(i,0), value, role);
  644. }
  645. return true;
  646. }
  647. //------------------------------------------------------------------------------
  648. bool ctkDICOMModel::setParentData(const QModelIndex &index, const QVariant &value, int role)
  649. {
  650. Q_D(const ctkDICOMModel);
  651. if(!index.isValid()){
  652. return false;
  653. }
  654. if (role != Qt::CheckStateRole)
  655. {
  656. return false;
  657. }
  658. else
  659. {
  660. Node* node = d->nodeFromIndex(index);
  661. bool checkedExist = false;
  662. bool partiallyCheckedExist = false;
  663. bool uncheckedExist = false;
  664. for(int i=0; i<index.model()->rowCount(index); i++)
  665. {
  666. Node* childNode = d->nodeFromIndex(index.child(i,0));
  667. if(childNode->Data[Qt::CheckStateRole] == Qt::Checked)
  668. {
  669. checkedExist = true;
  670. }
  671. else if(childNode->Data[Qt::CheckStateRole] == Qt::PartiallyChecked)
  672. {
  673. partiallyCheckedExist = true;
  674. }
  675. else if(childNode->Data[Qt::CheckStateRole] == Qt::Unchecked)
  676. {
  677. uncheckedExist = true;
  678. }
  679. }
  680. if(partiallyCheckedExist || (checkedExist && uncheckedExist))
  681. {
  682. node->Data[Qt::CheckStateRole] = Qt::PartiallyChecked;
  683. }
  684. else if(checkedExist)
  685. {
  686. node->Data[Qt::CheckStateRole] = Qt::Checked;
  687. }
  688. else if(uncheckedExist)
  689. {
  690. node->Data[Qt::CheckStateRole] = Qt::Unchecked;
  691. }
  692. else
  693. {
  694. node->Data[Qt::CheckStateRole] = Qt::Unchecked;
  695. }
  696. emit dataChanged(index, index);
  697. this->setParentData(index.parent(), value, role);
  698. }
  699. return true;
  700. }
  701. //------------------------------------------------------------------------------
  702. void ctkDICOMModel::setDatabase(const QSqlDatabase &db)
  703. {
  704. Q_D(ctkDICOMModel);
  705. this->beginResetModel();
  706. d->DataBase = db;
  707. delete d->RootNode;
  708. d->RootNode = 0;
  709. if (d->DataBase.tables().empty())
  710. {
  711. //Q_ASSERT(d->DataBase.isOpen());
  712. this->endResetModel();
  713. return;
  714. }
  715. d->RootNode = d->createNode(-1, QModelIndex());
  716. this->endResetModel();
  717. // TODO, use hasQuerySize everywhere, not only in setDataBase()
  718. bool hasQuerySize = d->RootNode->Query.driver()->hasFeature(QSqlDriver::QuerySize);
  719. if (hasQuerySize && d->RootNode->Query.size() > 0)
  720. {
  721. int newRowCount= d->RootNode->Query.size();
  722. beginInsertRows(QModelIndex(), 0, qMax(0, newRowCount - 1));
  723. d->RootNode->RowCount = newRowCount;
  724. d->RootNode->AtEnd = true;
  725. endInsertRows();
  726. }
  727. d->fetch(QModelIndex(), 256);
  728. }
  729. //------------------------------------------------------------------------------
  730. void ctkDICOMModel::setDatabase(const QSqlDatabase &db,const QMap<QString, QVariant>& parameters)
  731. {
  732. Q_D(ctkDICOMModel);
  733. this->beginResetModel();
  734. d->DataBase = db;
  735. d->SearchParameters = parameters;
  736. delete d->RootNode;
  737. d->RootNode = 0;
  738. if (d->DataBase.tables().empty())
  739. {
  740. //Q_ASSERT(d->DataBase.isOpen());
  741. this->endResetModel();
  742. return;
  743. }
  744. d->RootNode = d->createNode(-1, QModelIndex());
  745. this->endResetModel();
  746. // TODO, use hasQuerySize everywhere, not only in setDataBase()
  747. bool hasQuerySize = d->RootNode->Query.driver()->hasFeature(QSqlDriver::QuerySize);
  748. if (hasQuerySize && d->RootNode->Query.size() > 0)
  749. {
  750. int newRowCount= d->RootNode->Query.size();
  751. beginInsertRows(QModelIndex(), 0, qMax(0, newRowCount - 1));
  752. d->RootNode->RowCount = newRowCount;
  753. d->RootNode->AtEnd = true;
  754. endInsertRows();
  755. }
  756. d->fetch(QModelIndex(), 256);
  757. }
  758. void ctkDICOMModel::setDisplayLevel(ctkDICOMModel::IndexType level){
  759. Q_D(ctkDICOMModel);
  760. d->displayLevel = level;
  761. }
  762. //------------------------------------------------------------------------------
  763. void ctkDICOMModel::reset()
  764. {
  765. Q_D(ctkDICOMModel);
  766. // this could probably be done in a more elegant way
  767. this->setDatabase(d->DataBase);
  768. }
  769. //------------------------------------------------------------------------------
  770. void ctkDICOMModel::sort(int column, Qt::SortOrder order)
  771. {
  772. Q_D(ctkDICOMModel);
  773. /* The following would work if there is no fetch involved.
  774. ORDER BY doesn't just apply on the fetched item. By sorting
  775. new items can show up in the model, and we need to be more
  776. careful
  777. emit layoutAboutToBeChanged();
  778. QModelIndexList oldIndexList = d->modelIndexList();
  779. d->Sort = QString("\"%1\" %2")
  780. .arg(d->Headers[column])
  781. .arg(order == Qt::AscendingOrder ? "ASC" : "DESC");
  782. d->updateQueries(d->RootNode);
  783. QModelIndexList newIndexList = d->modelIndexList();
  784. Q_ASSERT(oldIndexList.count() == newIndexList.count());
  785. this->changePersistentIndexList(oldIndexList, newIndexList);
  786. emit layoutChanged();
  787. */
  788. this->beginResetModel();
  789. delete d->RootNode;
  790. d->RootNode = 0;
  791. d->Sort = QString("\"%1\" %2")
  792. .arg(d->Headers[column][Qt::DisplayRole].toString())
  793. .arg(order == Qt::AscendingOrder ? "ASC" : "DESC");
  794. d->RootNode = d->createNode(-1, QModelIndex());
  795. this->endResetModel();
  796. }
  797. //------------------------------------------------------------------------------
  798. bool ctkDICOMModel::setHeaderData ( int section, Qt::Orientation orientation, const QVariant & value, int role)
  799. {
  800. Q_D(ctkDICOMModel);
  801. if (orientation == Qt::Vertical)
  802. {
  803. return false;
  804. }
  805. if (section < 0 || section >= d->Headers.size() ||
  806. d->Headers[section][role] == value)
  807. {
  808. return false;
  809. }
  810. d->Headers[section][role] = value;
  811. emit this->headerDataChanged(orientation, section, section);
  812. return true;
  813. }