ctkDICOMModel.cpp 21 KB

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