Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COMP: Fix QModelIndex::child() deprecation warnings #1011

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ctkCmdLineModuleExplorerTreeWidget.h"
#include "ctkCmdLineModuleExplorerShowXmlAction.h"

#include <ctkUtils.h>
#include <ctkCmdLineModuleFrontend.h>
#include <ctkCmdLineModuleBackend.h>
#include <ctkCmdLineModuleFrontendFactory.h>
Expand Down Expand Up @@ -277,8 +278,7 @@ ctkCmdLineModuleFrontend* ctkCmdLineModuleExplorerTreeWidget::createFrontend(con
bool ModuleSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);

QModelIndex childIndex = index.child(0, 0);
QModelIndex childIndex = ctk::modelChildIndex(sourceModel(), index, 0, 0);
if (childIndex.isValid())
{
int i = 0;
Expand All @@ -288,7 +288,7 @@ bool ModuleSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelInd
accept = this->filterAcceptsRow(childIndex.row(), index);
if (accept) return true;

childIndex = index.child(++i, 0);
childIndex = ctk::modelChildIndex(sourceModel(), index, ++i, 0);
}
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion Libs/Core/ctkModelTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

// CTK includes
#include "ctkModelTester.h"
#include "ctkUtils.h"

//-----------------------------------------------------------------------------
class ctkModelTesterPrivate
Expand Down Expand Up @@ -292,7 +293,7 @@ void ctkModelTester::testParent(const QModelIndex& vparent)const
for (int j = 0; j < d->Model->columnCount(vparent); ++j)
{
this->test(d->Model->hasIndex(i, j, vparent), "hasIndex should return true for int range {0->rowCount(), 0->columnCount()}");
QModelIndex child = vparent.child(i, j);
QModelIndex child = ctk::modelChildIndex(d->Model, vparent, i, j);
this->test(child.row() == i, "A child's row must be the same as given");
this->test(child.column() == j, "A child's column must be the same as given");
QModelIndex childParent = child.parent();
Expand Down
15 changes: 15 additions & 0 deletions Libs/Core/ctkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,18 @@ QTextStream& ctk::endl(QTextStream &stream)
return stream << QLatin1Char('\n') << ctk::flush;
#endif
}

//------------------------------------------------------------------------------
QModelIndex ctk::modelChildIndex(QAbstractItemModel* item, const QModelIndex &parent, int row, int colum)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
if (!item)
{
return QModelIndex();
}
return item->index(row, colum, parent);
#else
Q_UNUSED(item);
return parent.child(row, colum);
#endif
}
12 changes: 11 additions & 1 deletion Libs/Core/ctkUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
#define __ctkUtils_h

// Qt includes
#include <QStringList>
#include <QAbstractItemModel>
#include <QDateTime>
#include <QDebug>
#include <QModelIndex>
#include <QStringList>

// STD includes
#include <vector>
Expand Down Expand Up @@ -214,6 +216,14 @@ CTK_CORE_EXPORT QTextStream &flush(QTextStream &stream);
///
/// For Qt >= 5.14, it is equivalent to \a Qt::endl;
CTK_CORE_EXPORT QTextStream & endl(QTextStream &stream);


/// \ingroup Core
/// \brief Returns the child of the model index that is stored in the given row and column.
///
/// This method was added so that the same code compiles without deprecation warnings
/// pre and post Qt 5.8.
CTK_CORE_EXPORT QModelIndex modelChildIndex(QAbstractItemModel* item, const QModelIndex &parent, int row, int colum);
}

#endif
10 changes: 7 additions & 3 deletions Libs/DICOM/Core/ctkDICOMModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
// dcmtk includes
#include "dcmtk/dcmdata/dcvrpn.h"

// ctkCore includes
#include "ctkUtils.h"

// ctkDICOMCore includes
#include "ctkDICOMModel.h"
#include "ctkLogger.h"
Expand Down Expand Up @@ -742,7 +745,7 @@ bool ctkDICOMModel::setData(const QModelIndex &index, const QVariant &value, int

for(int i=0; i<node->Children.count(); i++)
{
this->setChildData(index.child(i,0), value, role);
this->setChildData(ctk::modelChildIndex(this, index, i, 0), value, role);
}

if(index.parent().isValid())
Expand Down Expand Up @@ -771,7 +774,7 @@ bool ctkDICOMModel::setChildData(const QModelIndex &index, const QVariant &value

for(int i=0; i<node->Children.count(); i++)
{
this->setData(index.child(i,0), value, role);
this->setData(ctk::modelChildIndex(this, index, i, 0), value, role);
}

return true;
Expand Down Expand Up @@ -801,7 +804,8 @@ bool ctkDICOMModel::setParentData(const QModelIndex &index, const QVariant &valu

for(int i=0; i<index.model()->rowCount(index); i++)
{
Node* childNode = d->nodeFromIndex(index.child(i,0));
QModelIndex childIndex = ctk::modelChildIndex(this, index, i, 0);
Node* childNode = d->nodeFromIndex(childIndex);
if(childNode->Data[Qt::CheckStateRole].toUInt() == Qt::Checked)
{
checkedExist = true;
Expand Down
13 changes: 7 additions & 6 deletions Libs/DICOM/Widgets/ctkDICOMItemView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// CTK includes
#include "ctkLogger.h"
#include "ctkQImageView.h"
#include "ctkUtils.h"

// ctkDICOMCore includes
#include "ctkDICOMFilterProxyModel.h"
Expand Down Expand Up @@ -148,12 +149,12 @@ void ctkDICOMItemViewPrivate::onPatientModelSelected(const QModelIndex &index){
if(model){
QModelIndex patientIndex = index;
model->fetchMore(patientIndex);
QModelIndex studyIndex = patientIndex.child(0,0);
QModelIndex studyIndex = ctk::modelChildIndex(model, patientIndex, 0, 0);
model->fetchMore(studyIndex);
QModelIndex seriesIndex = studyIndex.child(0,0);
QModelIndex seriesIndex = ctk::modelChildIndex(model, studyIndex, 0, 0);
model->fetchMore(seriesIndex);
int imageCount = model->rowCount(seriesIndex);
QModelIndex imageIndex = seriesIndex.child(imageCount/2,0);
QModelIndex imageIndex = ctk::modelChildIndex(model, seriesIndex, imageCount/2, 0);

this->setImage(imageIndex);
}else{
Expand All @@ -170,10 +171,10 @@ void ctkDICOMItemViewPrivate::onStudyModelSelected(const QModelIndex &index){
if(model){
QModelIndex studyIndex = index;
model->fetchMore(studyIndex);
QModelIndex seriesIndex = studyIndex.child(0,0);
QModelIndex seriesIndex = ctk::modelChildIndex(model, studyIndex, 0, 0);
model->fetchMore(seriesIndex);
int imageCount = model->rowCount(seriesIndex);
QModelIndex imageIndex = seriesIndex.child(imageCount/2,0);
QModelIndex imageIndex = ctk::modelChildIndex(model, seriesIndex, imageCount/2, 0);

this->setImage(imageIndex);
}else{
Expand All @@ -191,7 +192,7 @@ void ctkDICOMItemViewPrivate::onSeriesModelSelected(const QModelIndex &index){
QModelIndex seriesIndex = index;
model->fetchMore(seriesIndex);
int imageCount = model->rowCount(seriesIndex);
QModelIndex imageIndex = seriesIndex.child(imageCount/2,0);
QModelIndex imageIndex = ctk::modelChildIndex(model, seriesIndex, imageCount/2, 0);

this->setImage(imageIndex);
}else{
Expand Down
13 changes: 7 additions & 6 deletions Libs/DICOM/Widgets/ctkDICOMThumbnailListWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

// ctk includes
#include "ctkLogger.h"
#include "ctkUtils.h"

// ctkWidgets includes
#include "ctkFlowLayout.h"
Expand Down Expand Up @@ -106,11 +107,11 @@ ::addPatientThumbnails(const QModelIndex &index)

for(int i=0; i<studyCount; i++)
{
QModelIndex studyIndex = patientIndex.child(i, 0);
QModelIndex seriesIndex = studyIndex.child(0, 0);
QModelIndex studyIndex = ctk::modelChildIndex(model, patientIndex, i, 0);
QModelIndex seriesIndex = ctk::modelChildIndex(model, studyIndex, 0, 0);
model->fetchMore(seriesIndex);
const int imageCount = model->rowCount(seriesIndex);
QModelIndex imageIndex = seriesIndex.child(imageCount/2, 0);
QModelIndex imageIndex = ctk::modelChildIndex(model, seriesIndex, imageCount/2, 0);
QString study = model->data(studyIndex, Qt::DisplayRole).toString();
this->addThumbnailWidget(imageIndex, studyIndex, study);
}
Expand All @@ -134,10 +135,10 @@ ::addStudyThumbnails(const QModelIndex &index)

for(int i=0; i<seriesCount; i++)
{
QModelIndex seriesIndex = studyIndex.child(i, 0);
QModelIndex seriesIndex = ctk::modelChildIndex(model, studyIndex, i, 0);
model->fetchMore(seriesIndex);
int imageCount = model->rowCount(seriesIndex);
QModelIndex imageIndex = seriesIndex.child(imageCount/2, 0);
QModelIndex imageIndex = ctk::modelChildIndex(model, seriesIndex, imageCount/2, 0);
this->addThumbnailWidget(imageIndex, seriesIndex, model->data(seriesIndex, Qt::DisplayRole).toString());
}
}
Expand All @@ -160,7 +161,7 @@ ::addSeriesThumbnails(const QModelIndex &index)
logger.debug(QString("Thumbs: %1").arg(imageCount));
for (int i = 0 ; i < imageCount ; i++ )
{
QModelIndex imageIndex = seriesIndex.child(i,0);
QModelIndex imageIndex = ctk::modelChildIndex(model, seriesIndex, i, 0);

this->addThumbnailWidget(imageIndex, imageIndex, QString("Image %1").arg(i));
}
Expand Down