ctkDICOMModel.cpp 24 KB

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