ctkDICOMModel.cpp 22 KB

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