ctkDICOMModel.cxx 18 KB

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