ctkDICOMModel.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. // Qt includes
  2. #include <QStringList>
  3. #include <QSqlDriver>
  4. #include <QSqlError>
  5. #include <QSqlQuery>
  6. #include <QSqlQueryModel>
  7. #include <QSqlRecord>
  8. #include <QTime>
  9. #include <QDebug>
  10. // ctkDICOM includes
  11. #include "ctkDICOMModel.h"
  12. struct Node;
  13. //------------------------------------------------------------------------------
  14. class ctkDICOMModelPrivate:public qCTKPrivate<ctkDICOMModel>
  15. {
  16. public:
  17. ctkDICOMModelPrivate();
  18. virtual ~ctkDICOMModelPrivate();
  19. void init();
  20. enum IndexType{
  21. RootType,
  22. PatientType,
  23. StudyType,
  24. SeriesType,
  25. ImageType
  26. };
  27. void fetch(const QModelIndex& indexValue, int limit);
  28. Node* createNode(int row, const QModelIndex& parentValue)const;
  29. Node* nodeFromIndex(const QModelIndex& indexValue)const;
  30. //QModelIndexList indexListFromNode(const Node* node)const;
  31. //QModelIndexList modelIndexList(Node* node = 0)const;
  32. //int childrenCount(Node* node = 0)const;
  33. // move it in the Node struct
  34. QVariant value(Node* parentValue, int row, int field)const;
  35. QVariant value(const QModelIndex& indexValue, int row, int field)const;
  36. QString generateQuery(const QString& fields, const QString& table, const QString& conditions = QString())const;
  37. void updateQueries(Node* node)const;
  38. Node* RootNode;
  39. QSqlDatabase DataBase;
  40. QStringList Headers;
  41. QString Sort;
  42. };
  43. //------------------------------------------------------------------------------
  44. // 1 node per row
  45. struct Node
  46. {
  47. ~Node()
  48. {
  49. foreach(Node* node, this->Children)
  50. {
  51. delete node;
  52. }
  53. this->Children.clear();
  54. }
  55. ctkDICOMModelPrivate::IndexType Type;
  56. Node* Parent;
  57. QVector<Node*> Children;
  58. int Row;
  59. QSqlQuery Query;
  60. QString UID;
  61. int RowCount;
  62. bool AtEnd;
  63. bool Fetching;
  64. };
  65. //------------------------------------------------------------------------------
  66. ctkDICOMModelPrivate::ctkDICOMModelPrivate()
  67. {
  68. this->RootNode = 0;
  69. }
  70. //------------------------------------------------------------------------------
  71. ctkDICOMModelPrivate::~ctkDICOMModelPrivate()
  72. {
  73. delete this->RootNode;
  74. this->RootNode = 0;
  75. }
  76. //------------------------------------------------------------------------------
  77. void ctkDICOMModelPrivate::init()
  78. {
  79. this->Headers = QStringList() << "Name" << "Age" << "Scan" << "Date" << "Subject ID"
  80. << "Number" << "Institution" << "Referrer" << "Performer";
  81. }
  82. //------------------------------------------------------------------------------
  83. Node* ctkDICOMModelPrivate::nodeFromIndex(const QModelIndex& indexValue)const
  84. {
  85. return indexValue.isValid() ? reinterpret_cast<Node*>(indexValue.internalPointer()) : this->RootNode;
  86. }
  87. /*
  88. //------------------------------------------------------------------------------
  89. QModelIndexList ctkDICOMModelPrivate::indexListFromNode(const Node* node)const
  90. {
  91. QCTK_P(const ctkDICOMModel);
  92. Q_ASSERT(node);
  93. QModelIndexList indexList;
  94. Node* parentNode = node->Parent;
  95. if (parentNode == 0)
  96. {
  97. return indexList;
  98. }
  99. int field = parentNode->Query.record().indexOf("UID");
  100. int row = -1;
  101. for (row = 0; row < parentNode->RowCount; ++row)
  102. {
  103. QString uid = this->value(parentNode, row, field).toString();
  104. if (uid == node->UID)
  105. {
  106. break;
  107. }
  108. }
  109. if (row >= parentNode->RowCount)
  110. {
  111. return indexList;
  112. }
  113. for (int column = 0 ; column < this->Headers.size(); ++column)
  114. {
  115. indexList.append(p->createIndex(row, column, parentNode));
  116. }
  117. return indexList;
  118. }
  119. //------------------------------------------------------------------------------
  120. QModelIndexList ctkDICOMModelPrivate::modelIndexList(Node* node)const
  121. {
  122. QModelIndexList list;
  123. if (node == 0)
  124. {
  125. node = this->RootNode;
  126. }
  127. foreach(Node* child, node->Children)
  128. {
  129. list.append(this->indexListFromNode(child));
  130. }
  131. foreach(Node* child, node->Children)
  132. {
  133. list.append(this->modelIndexList(child));
  134. }
  135. return list;
  136. }
  137. //------------------------------------------------------------------------------
  138. int ctkDICOMModelPrivate::childrenCount(Node* node)const
  139. {
  140. int count = 0;
  141. if (node == 0)
  142. {
  143. node = this->RootNode;
  144. }
  145. count += node->Children.size();
  146. foreach(Node* child, node->Children)
  147. {
  148. count += this->childrenCount(child);
  149. }
  150. return count;
  151. }
  152. */
  153. //------------------------------------------------------------------------------
  154. Node* ctkDICOMModelPrivate::createNode(int row, const QModelIndex& parentValue)const
  155. {
  156. Node* node = new Node;
  157. Node* nodeParent = 0;
  158. if (row == -1)
  159. {// root node
  160. node->Type = ctkDICOMModelPrivate::RootType;
  161. node->Parent = 0;
  162. }
  163. else
  164. {
  165. nodeParent = this->nodeFromIndex(parentValue);
  166. nodeParent->Children.push_back(node);
  167. node->Parent = nodeParent;
  168. node->Type = ctkDICOMModelPrivate::IndexType(nodeParent->Type + 1);
  169. }
  170. node->Row = row;
  171. if (node->Type != ctkDICOMModelPrivate::RootType)
  172. {
  173. int field = nodeParent->Query.record().indexOf("UID");
  174. node->UID = this->value(parentValue, row, field).toString();
  175. }
  176. node->RowCount = 0;
  177. node->AtEnd = false;
  178. node->Fetching = false;
  179. this->updateQueries(node);
  180. return node;
  181. }
  182. //------------------------------------------------------------------------------
  183. QVariant ctkDICOMModelPrivate::value(const QModelIndex& parentValue, int row, int column) const
  184. {
  185. Node* node = this->nodeFromIndex(parentValue);
  186. if (row >= node->RowCount)
  187. {
  188. const_cast<ctkDICOMModelPrivate *>(this)->fetch(parentValue, row + 256);
  189. }
  190. return this->value(node, row, column);
  191. }
  192. //------------------------------------------------------------------------------
  193. QVariant ctkDICOMModelPrivate::value(Node* parentValue, int row, int column) const
  194. {
  195. Q_ASSERT(row < parentValue->RowCount);
  196. if (!parentValue->Query.seek(row))
  197. {
  198. qDebug() << parentValue->Query.lastError();
  199. Q_ASSERT(parentValue->Query.seek(row));
  200. return QVariant();
  201. }
  202. QVariant res = parentValue->Query.value(column);
  203. Q_ASSERT(res.isValid());
  204. return res;
  205. }
  206. //------------------------------------------------------------------------------
  207. QString ctkDICOMModelPrivate::generateQuery(const QString& fields, const QString& table, const QString& conditions)const
  208. {
  209. QString res = QString("SELECT ") + fields + QString(" FROM ") + table;
  210. if (!conditions.isEmpty())
  211. {
  212. res += QString(" WHERE ") + conditions;
  213. }
  214. if (!this->Sort.isEmpty())
  215. {
  216. res += QString(" ORDER BY ") + this->Sort;
  217. }
  218. return res;
  219. }
  220. //------------------------------------------------------------------------------
  221. void ctkDICOMModelPrivate::updateQueries(Node* node)const
  222. {
  223. // are you kidding me, it should be virtualized here :-)
  224. QString query;
  225. switch(node->Type)
  226. {
  227. default:
  228. Q_ASSERT(node->Type == ctkDICOMModelPrivate::RootType);
  229. break;
  230. case ctkDICOMModelPrivate::RootType:
  231. //query = QString("SELECT FROM ");
  232. query = this->generateQuery("UID as UID, PatientsName as Name, PatientsAge as Age, PatientsBirthDate as Date, PatientID as \"Subject ID\"","Patients");
  233. break;
  234. case ctkDICOMModelPrivate::PatientType:
  235. //query = QString("SELECT FROM Studies WHERE PatientsUID='%1'").arg(node->UID);
  236. query = this->generateQuery("StudyInstanceUID as UID, StudyDescription as Name, ModalitiesInStudy as Scan, StudyDate as Date, AccessionNumber as Number, ReferringPhysician as Institution, ReferringPhysician as Referrer, PerformingPysiciansName as Performer", "Studies",QString("PatientsUID='%1'").arg(node->UID));
  237. break;
  238. case ctkDICOMModelPrivate::StudyType:
  239. //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);
  240. query = this->generateQuery("SeriesInstanceUID as UID, SeriesDescription as Name, BodyPartExamined as Scan, SeriesDate as Date, AcquisitionNumber as Number","Series",QString("StudyInstanceUID='%1'").arg(node->UID));
  241. break;
  242. case ctkDICOMModelPrivate::SeriesType:
  243. //query = QString("SELECT Filename as UID, Filename as Name, SeriesInstanceUID as Date FROM Images WHERE SeriesInstanceUID='%1'").arg(node->UID);
  244. query = this->generateQuery("Filename as UID, Filename as Name, SeriesInstanceUID as Date", "Images", QString("SeriesInstanceUID='%1'").arg(node->UID));
  245. break;
  246. case ctkDICOMModelPrivate::ImageType:
  247. break;
  248. }
  249. node->Query = QSqlQuery(query, this->DataBase);
  250. foreach(Node* child, node->Children)
  251. {
  252. this->updateQueries(child);
  253. }
  254. }
  255. //------------------------------------------------------------------------------
  256. void ctkDICOMModelPrivate::fetch(const QModelIndex& indexValue, int limit)
  257. {
  258. QCTK_P(ctkDICOMModel);
  259. Node* node = this->nodeFromIndex(indexValue);
  260. if (node->AtEnd || limit <= node->RowCount || node->Fetching/*|| bottom.column() == -1*/)
  261. {
  262. return;
  263. }
  264. node->Fetching = true;
  265. int newRowCount;
  266. const int oldRowCount = node->RowCount;
  267. // try to seek directly
  268. if (node->Query.seek(limit - 1))
  269. {
  270. newRowCount = limit;
  271. }
  272. else
  273. {
  274. newRowCount = qMax(oldRowCount, 1);
  275. if (node->Query.seek(newRowCount - 1))
  276. {
  277. while (node->Query.next())
  278. {
  279. ++newRowCount;
  280. }
  281. }
  282. else
  283. {
  284. // empty or invalid query
  285. newRowCount = 0;
  286. }
  287. node->AtEnd = true; // this is the end.
  288. }
  289. if (newRowCount > 0 && newRowCount > node->RowCount)
  290. {
  291. p->beginInsertRows(indexValue, node->RowCount, newRowCount - 1);
  292. node->RowCount = newRowCount;
  293. node->Fetching = false;
  294. p->endInsertRows();
  295. }
  296. else
  297. {
  298. node->RowCount = newRowCount;
  299. node->Fetching = false;
  300. }
  301. }
  302. //------------------------------------------------------------------------------
  303. ctkDICOMModel::ctkDICOMModel(QObject* parentValue)
  304. {
  305. QCTK_INIT_PRIVATE(ctkDICOMModel);
  306. qctk_d()->init();
  307. }
  308. //------------------------------------------------------------------------------
  309. ctkDICOMModel::~ctkDICOMModel()
  310. {
  311. }
  312. //------------------------------------------------------------------------------
  313. bool ctkDICOMModel::canFetchMore ( const QModelIndex & parentValue ) const
  314. {
  315. QCTK_D(const ctkDICOMModel);
  316. Node* node = d->nodeFromIndex(parentValue);
  317. return !node->AtEnd;
  318. }
  319. //------------------------------------------------------------------------------
  320. int ctkDICOMModel::columnCount ( const QModelIndex & _parent ) const
  321. {
  322. QCTK_D(const ctkDICOMModel);
  323. Q_UNUSED(_parent);
  324. return d->RootNode != 0 ? d->Headers.size() : 0;
  325. }
  326. //------------------------------------------------------------------------------
  327. QVariant ctkDICOMModel::data ( const QModelIndex & indexValue, int role ) const
  328. {
  329. QCTK_D(const ctkDICOMModel);
  330. if (role & ~(Qt::DisplayRole | Qt::EditRole))
  331. {
  332. return QVariant();
  333. }
  334. QModelIndex indexParent = this->parent(indexValue);
  335. Node* parentNode = d->nodeFromIndex(indexParent);
  336. if (indexValue.row() >= parentNode->RowCount)
  337. {
  338. const_cast<ctkDICOMModelPrivate *>(d)->fetch(indexValue, indexValue.row());
  339. }
  340. /*
  341. if (!node->Query.seek(indexValue.row()))
  342. {
  343. qDebug() << node->Query.lastError();
  344. return QVariant();
  345. }
  346. */
  347. int field = parentNode->Query.record().indexOf(d->Headers[indexValue.column()]);
  348. if (field < 0)
  349. {
  350. return QString();
  351. }
  352. return d->value(indexParent, indexValue.row(), field);
  353. //return node->Query.value(field);
  354. }
  355. //------------------------------------------------------------------------------
  356. void ctkDICOMModel::fetchMore ( const QModelIndex & parentValue )
  357. {
  358. QCTK_D(ctkDICOMModel);
  359. Node* node = d->nodeFromIndex(parentValue);
  360. d->fetch(parentValue, qMax(node->RowCount, 0) + 256);
  361. }
  362. //------------------------------------------------------------------------------
  363. Qt::ItemFlags ctkDICOMModel::flags ( const QModelIndex & indexValue ) const
  364. {
  365. return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  366. }
  367. //------------------------------------------------------------------------------
  368. bool ctkDICOMModel::hasChildren ( const QModelIndex & parentValue ) const
  369. {
  370. QCTK_D(const ctkDICOMModel);
  371. if (parentValue.column() > 0)
  372. {
  373. return false;
  374. }
  375. Node* node = d->nodeFromIndex(parentValue);
  376. if (!node)
  377. {
  378. return false;
  379. }
  380. if (node->RowCount == 0 && !node->AtEnd)
  381. {
  382. //const_cast<qCTKDCMTKModelPrivate*>(d)->fetch(parentValue, 1);
  383. return node->Query.seek(0);
  384. }
  385. return node->RowCount > 0;
  386. }
  387. //------------------------------------------------------------------------------
  388. QVariant ctkDICOMModel::headerData(int section, Qt::Orientation orientation, int role)const
  389. {
  390. QCTK_D(const ctkDICOMModel);
  391. if (role & ~(Qt::DisplayRole | Qt::EditRole))
  392. {
  393. return QVariant();
  394. }
  395. if (orientation == Qt::Vertical)
  396. {
  397. return section;
  398. }
  399. Q_ASSERT(orientation == Qt::Horizontal);
  400. Q_ASSERT(section < d->Headers.size());
  401. return d->Headers[section];
  402. }
  403. //------------------------------------------------------------------------------
  404. QModelIndex ctkDICOMModel::index ( int row, int column, const QModelIndex & parentValue ) const
  405. {
  406. QCTK_D(const ctkDICOMModel);
  407. if (d->RootNode == 0 || parentValue.column() > 0)
  408. {
  409. return QModelIndex();
  410. }
  411. Node* parentNode = d->nodeFromIndex(parentValue);
  412. int field = parentNode->Query.record().indexOf("UID");
  413. QString uid = d->value(parentValue, row, field).toString();
  414. Node* node = 0;
  415. foreach(Node* tmpNode, parentNode->Children)
  416. {
  417. if (tmpNode->UID == uid)
  418. {
  419. node = tmpNode;
  420. break;
  421. }
  422. }
  423. if (node == 0)
  424. {
  425. node = d->createNode(row, parentValue);
  426. }
  427. return this->createIndex(row, column, node);
  428. }
  429. //------------------------------------------------------------------------------
  430. QModelIndex ctkDICOMModel::parent ( const QModelIndex & indexValue ) const
  431. {
  432. QCTK_D(const ctkDICOMModel);
  433. Node* node = d->nodeFromIndex(indexValue);
  434. Q_ASSERT(node);
  435. Node* parentNode = node->Parent;
  436. if (parentNode == 0)
  437. {// node is root
  438. return QModelIndex();
  439. }
  440. return parentNode == d->RootNode ? QModelIndex() : this->createIndex(parentNode->Row, 0, parentNode);
  441. /* need to recalculate the parent row
  442. Node* greatParentNode = parentNode->Parent;
  443. if (greatParentNode == 0)
  444. {
  445. return QModelIndex();
  446. }
  447. int field = greatParentNode->Query.record().indexOf("UID");
  448. int row = -1;
  449. for (row = 0; row < greatParentNode->RowCount; ++row)
  450. {
  451. QString uid = d->value(greatParentNode, row, field).toString();
  452. if (uid == parentNode->UID)
  453. {
  454. break;
  455. }
  456. }
  457. Q_ASSERT(row < greatParentNode->RowCount);
  458. return this->createIndex(row, 0, parentNode);
  459. */
  460. }
  461. //------------------------------------------------------------------------------
  462. int ctkDICOMModel::rowCount ( const QModelIndex & parentValue ) const
  463. {
  464. QCTK_D(const ctkDICOMModel);
  465. if (d->RootNode == 0 || parentValue.column() > 0)
  466. {
  467. return 0;
  468. }
  469. Node* node = d->nodeFromIndex(parentValue);
  470. Q_ASSERT(node);
  471. if (node->RowCount == 0 && !node->AtEnd)
  472. {
  473. //const_cast<ctkDICOMModelPrivate*>(d)->fetch(parentValue, 256);
  474. }
  475. return node->RowCount;
  476. }
  477. //------------------------------------------------------------------------------
  478. void ctkDICOMModel::setDatabase(const QSqlDatabase &db)
  479. {
  480. QCTK_D(ctkDICOMModel);
  481. this->beginResetModel();
  482. d->DataBase = db;
  483. delete d->RootNode;
  484. d->RootNode = 0;
  485. if (d->DataBase.tables().empty())
  486. {
  487. //Q_ASSERT(d->DataBase.isOpen());
  488. this->endResetModel();
  489. return;
  490. }
  491. d->RootNode = d->createNode(-1, QModelIndex());
  492. this->endResetModel();
  493. // TODO, use hasQuerySize everywhere, not only in setDataBase()
  494. bool hasQuerySize = d->RootNode->Query.driver()->hasFeature(QSqlDriver::QuerySize);
  495. if (hasQuerySize && d->RootNode->Query.size() > 0)
  496. {
  497. int newRowCount= d->RootNode->Query.size();
  498. beginInsertRows(QModelIndex(), 0, qMax(0, newRowCount - 1));
  499. d->RootNode->RowCount = newRowCount;
  500. d->RootNode->AtEnd = true;
  501. endInsertRows();
  502. }
  503. d->fetch(QModelIndex(), 256);
  504. }
  505. //------------------------------------------------------------------------------
  506. void ctkDICOMModel::sort(int column, Qt::SortOrder order)
  507. {
  508. QCTK_D(ctkDICOMModel);
  509. /* The following would work if there is no fetch involved.
  510. ORDER BY doesn't just apply on the fetched item. By sorting
  511. new items can show up in the model, and we need to be more
  512. careful
  513. emit layoutAboutToBeChanged();
  514. QModelIndexList oldIndexList = d->modelIndexList();
  515. d->Sort = QString("\"%1\" %2")
  516. .arg(d->Headers[column])
  517. .arg(order == Qt::AscendingOrder ? "ASC" : "DESC");
  518. d->updateQueries(d->RootNode);
  519. QModelIndexList newIndexList = d->modelIndexList();
  520. Q_ASSERT(oldIndexList.count() == newIndexList.count());
  521. this->changePersistentIndexList(oldIndexList, newIndexList);
  522. emit layoutChanged();
  523. */
  524. this->beginResetModel();
  525. delete d->RootNode;
  526. d->RootNode = 0;
  527. d->Sort = QString("\"%1\" %2")
  528. .arg(d->Headers[column])
  529. .arg(order == Qt::AscendingOrder ? "ASC" : "DESC");
  530. d->RootNode = d->createNode(-1, QModelIndex());
  531. this->endResetModel();
  532. }
  533. //------------------------------------------------------------------------------
  534. bool ctkDICOMModel::setHeaderData ( int section, Qt::Orientation orientation, const QVariant & value, int role)
  535. {
  536. QCTK_D(ctkDICOMModel);
  537. if (role & ~(Qt::DisplayRole | Qt::EditRole))
  538. {
  539. return false;
  540. }
  541. if (orientation == Qt::Vertical)
  542. {
  543. return false;
  544. }
  545. if (value.toString() == d->Headers[section])
  546. {
  547. return false;
  548. }
  549. d->Headers[section] = value.toString();
  550. emit this->headerDataChanged(orientation, section, section);
  551. return true;
  552. }