ctkDICOMModel.cpp 21 KB

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