ctkDICOMModel.cpp 22 KB

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