QmlDesigner: Fix build

Yes, it is possible to set a unsigend integer to -1. It is defined that
a unsigned integer is underflowing and then you get the largest integer.
With https://en.wikipedia.org/wiki/Two%27s_complement it exactly the
same bit pattern as a -1 signed integer too.

But many compiler like the mix of signed and unsigned because it can
lead to bugs. So just use the unsigned interId() data type and set the
special value to the maximal integer.

Change-Id: Ie2ee1b5e2b476ec12abb5a48eee3eae2ae8cd14b
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Marco Bubke
2023-09-08 18:40:33 +02:00
committed by Thomas Hartmann
parent 5f0556536e
commit 1d884898be
3 changed files with 25 additions and 26 deletions

View File

@@ -759,7 +759,7 @@ void ConnectionModelBackendDelegate::setCurrentRow(int i)
m_currentRow = i; m_currentRow = i;
m_propertyTreeModel.resetModel(); m_propertyTreeModel.resetModel();
m_propertyListProxyModel.setRowAndInternalId(0, -1); m_propertyListProxyModel.setRowAndInternalId(0, internalRootIndex);
//setup //setup

View File

@@ -160,7 +160,7 @@ QString stripQualification(const QString &string)
} }
QVariant PropertyTreeModel::data(const QModelIndex &index, int role) const QVariant PropertyTreeModel::data(const QModelIndex &index, int role) const
{ {
int internalId = index.internalId(); auto internalId = index.internalId();
if (role == InternalIdRole) if (role == InternalIdRole)
return internalId; return internalId;
@@ -173,7 +173,7 @@ QVariant PropertyTreeModel::data(const QModelIndex &index, int role) const
if (!index.isValid()) if (!index.isValid())
return {}; return {};
if (internalId < 0) if (internalId == internalRootIndex)
return "--root item--"; return "--root item--";
QTC_ASSERT(internalId < m_indexCount, return {"assert"}); QTC_ASSERT(internalId < m_indexCount, return {"assert"});
@@ -218,26 +218,24 @@ Qt::ItemFlags PropertyTreeModel::flags(const QModelIndex &) const
QModelIndex PropertyTreeModel::index(int row, int column, const QModelIndex &parent) const QModelIndex PropertyTreeModel::index(int row, int column, const QModelIndex &parent) const
{ {
int internalId = parent.internalId(); auto internalId = parent.internalId();
if (!m_connectionView->isAttached()) if (!m_connectionView->isAttached())
return {}; return {};
const int rootId = -1;
if (!parent.isValid()) if (!parent.isValid())
return createIndex(0, 0, rootId); return createIndex(0, 0, internalRootIndex);
if (!hasIndex(row, column, parent)) if (!hasIndex(row, column, parent))
return {}; return {};
if (internalId == rootId) { //root level model node if (internalId == internalRootIndex) { //root level model node
const ModelNode modelNode = m_nodeList[row]; const ModelNode modelNode = m_nodeList[row];
return ensureModelIndex(modelNode, row); return ensureModelIndex(modelNode, row);
} }
//property //property
QTC_ASSERT(internalId >= 0, return {}); QTC_ASSERT(internalId != internalRootIndex, return {});
DataCacheItem item = m_indexHash[internalId]; DataCacheItem item = m_indexHash[internalId];
QTC_ASSERT(item.modelNode.isValid(), return {}); QTC_ASSERT(item.modelNode.isValid(), return {});
@@ -262,9 +260,9 @@ QModelIndex PropertyTreeModel::parent(const QModelIndex &index) const
if (!index.isValid()) if (!index.isValid())
return {}; return {};
int internalId = index.internalId(); auto internalId = index.internalId();
if (internalId == m_internalRootIndex) if (internalId == internalRootIndex)
return {}; return {};
QTC_ASSERT(internalId < m_indexCount, return {}); QTC_ASSERT(internalId < m_indexCount, return {});
@@ -273,7 +271,7 @@ QModelIndex PropertyTreeModel::parent(const QModelIndex &index) const
// no property means the parent is the root item // no property means the parent is the root item
if (item.propertyName.isEmpty()) if (item.propertyName.isEmpty())
return createIndex(0, 0, -1); return createIndex(0, 0, internalRootIndex);
if (item.propertyName.contains(".")) { if (item.propertyName.contains(".")) {
auto list = item.propertyName.split('.'); auto list = item.propertyName.split('.');
@@ -294,7 +292,7 @@ QModelIndex PropertyTreeModel::parent(const QModelIndex &index) const
return ensureModelIndex(item.modelNode, row); return ensureModelIndex(item.modelNode, row);
} }
QPersistentModelIndex PropertyTreeModel::indexForInternalIdAndRow(int internalId, int row) QPersistentModelIndex PropertyTreeModel::indexForInternalIdAndRow(quintptr internalId, int row)
{ {
return createIndex(row, 0, internalId); return createIndex(row, 0, internalId);
} }
@@ -307,9 +305,9 @@ int PropertyTreeModel::rowCount(const QModelIndex &parent) const
if (!parent.isValid()) if (!parent.isValid())
return 1; //m_nodeList.size(); return 1; //m_nodeList.size();
int internalId = parent.internalId(); auto internalId = parent.internalId();
if (internalId == -1) if (internalId == internalRootIndex)
return m_nodeList.size(); return m_nodeList.size();
QTC_ASSERT(internalId < m_indexCount, return 0); QTC_ASSERT(internalId < m_indexCount, return 0);
@@ -791,12 +789,12 @@ void PropertyListProxyModel::resetModel()
endResetModel(); endResetModel();
} }
void PropertyListProxyModel::setRowAndInternalId(int row, int internalId) void PropertyListProxyModel::setRowAndInternalId(int row, quintptr internalId)
{ {
qDebug() << Q_FUNC_INFO << row << internalId; qDebug() << Q_FUNC_INFO << row << internalId;
QTC_ASSERT(m_treeModel, return ); QTC_ASSERT(m_treeModel, return );
if (internalId == -1) if (internalId == internalRootIndex)
m_parentIndex = m_treeModel->index(0, 0); m_parentIndex = m_treeModel->index(0, 0);
else else
m_parentIndex = m_treeModel->index(row, 0, m_parentIndex); m_parentIndex = m_treeModel->index(row, 0, m_parentIndex);
@@ -837,7 +835,7 @@ void PropertyListProxyModel::goUp()
{ {
qDebug() << Q_FUNC_INFO; qDebug() << Q_FUNC_INFO;
if (m_parentIndex.internalId() == -1) if (m_parentIndex.internalId() == internalRootIndex)
return; return;
m_parentIndex = m_treeModel->parent(m_parentIndex); m_parentIndex = m_treeModel->parent(m_parentIndex);
@@ -848,7 +846,7 @@ void PropertyListProxyModel::goUp()
void PropertyListProxyModel::reset() void PropertyListProxyModel::reset()
{ {
setRowAndInternalId(0, -1); // TODO ??? setRowAndInternalId(0, internalRootIndex); // TODO ???
emit parentNameChanged(); emit parentNameChanged();
} }
@@ -871,7 +869,7 @@ PropertyTreeModelDelegate::PropertyTreeModelDelegate(ConnectionView *parent) : m
void PropertyTreeModelDelegate::setPropertyType(PropertyTreeModel::PropertyTypes type) void PropertyTreeModelDelegate::setPropertyType(PropertyTreeModel::PropertyTypes type)
{ {
m_model.setPropertyType(type); m_model.setPropertyType(type);
setupNameComboBox(m_idCombboBox.currentText(), m_nameCombboBox.currentText(), 0); setupNameComboBox(m_idCombboBox.currentText(), m_nameCombboBox.currentText(), nullptr);
} }
void PropertyTreeModelDelegate::setup(const QString &id, const QString &name, bool *nameExists) void PropertyTreeModelDelegate::setup(const QString &id, const QString &name, bool *nameExists)
@@ -899,7 +897,7 @@ void PropertyTreeModelDelegate::setupNameComboBox(const QString &id,
return QString::fromUtf8(name); return QString::fromUtf8(name);
}); });
QStringList nameList; QStringList nameList;
nameList.reserve(nameVector.size()); nameList.reserve(Utils::ssize(nameVector));
std::copy(nameVector.begin(), nameVector.end(), std::back_inserter(nameList)); std::copy(nameVector.begin(), nameVector.end(), std::back_inserter(nameList));
if (!nameList.contains(name)) { if (!nameList.contains(name)) {

View File

@@ -15,6 +15,8 @@
namespace QmlDesigner { namespace QmlDesigner {
inline constexpr quintptr internalRootIndex = std::numeric_limits<quintptr>::max();
class AbstractProperty; class AbstractProperty;
class ModelNode; class ModelNode;
class BindingProperty; class BindingProperty;
@@ -58,7 +60,7 @@ public:
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override; QModelIndex parent(const QModelIndex &index) const override;
QPersistentModelIndex indexForInternalIdAndRow(int internalId, int row); QPersistentModelIndex indexForInternalIdAndRow(quintptr internalId, int row);
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override;
@@ -67,7 +69,7 @@ public:
{ {
ModelNode modelNode; ModelNode modelNode;
PropertyName propertyName; PropertyName propertyName;
int internalIndex = -1; std::size_t internalIndex = internalRootIndex;
}; };
void setPropertyType(PropertyTypes type); void setPropertyType(PropertyTypes type);
@@ -118,12 +120,11 @@ private:
mutable std::set<DataCacheItem> m_indexCache; mutable std::set<DataCacheItem> m_indexCache;
mutable std::vector<DataCacheItem> m_indexHash; mutable std::vector<DataCacheItem> m_indexHash;
mutable int m_indexCount = 0; mutable std::size_t m_indexCount = 0;
QList<ModelNode> m_nodeList; QList<ModelNode> m_nodeList;
PropertyTypes m_type = AllTypes; PropertyTypes m_type = AllTypes;
QString m_filter; QString m_filter;
mutable QHash<ModelNode, std::vector<PropertyName>> m_sortedAndFilteredPropertyNamesSignalsSlots; mutable QHash<ModelNode, std::vector<PropertyName>> m_sortedAndFilteredPropertyNamesSignalsSlots;
int m_internalRootIndex = -1;
}; };
class PropertyListProxyModel : public QAbstractListModel class PropertyListProxyModel : public QAbstractListModel
@@ -137,7 +138,7 @@ public:
void resetModel(); void resetModel();
void setRowAndInternalId(int row, int internalId); void setRowAndInternalId(int row, quintptr internalId);
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;