From d6a724a9b2e8bab1ee35e637887b8968ee66bc26 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Tue, 7 Jul 2020 20:19:41 +0200 Subject: QCalendarWidget: code tidies for the internal model The table model inside a QCalendarWidget was violating a few QAIM principles: * returning illegal values from row/columnCount by not checking the parent index * not keeping invariants on row/column manipulation. Pick-to: 5.15 Change-Id: I2c51e59ea2d89e73884bad20f3c06fbb808a26c5 Reviewed-by: Samuel Gaist --- src/widgets/widgets/qcalendarwidget.cpp | 51 +++++++++++++-------------------- 1 file changed, 20 insertions(+), 31 deletions(-) (limited to 'src/widgets') diff --git a/src/widgets/widgets/qcalendarwidget.cpp b/src/widgets/widgets/qcalendarwidget.cpp index 830216ef72..be3d275375 100644 --- a/src/widgets/widgets/qcalendarwidget.cpp +++ b/src/widgets/widgets/qcalendarwidget.cpp @@ -860,38 +860,23 @@ class QCalendarModel : public QAbstractTableModel public: QCalendarModel(QObject *parent = nullptr); - int rowCount(const QModelIndex &) const override - { return RowCount + m_firstRow; } - int columnCount(const QModelIndex &) const override - { return ColumnCount + m_firstColumn; } - QVariant data(const QModelIndex &index, int role) const override; - Qt::ItemFlags flags(const QModelIndex &index) const override; - - bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override - { - beginInsertRows(parent, row, row + count - 1); - endInsertRows(); - return true; - } - bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override + int rowCount(const QModelIndex &parent) const override { - beginInsertColumns(parent, column, column + count - 1); - endInsertColumns(); - return true; - } - bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override - { - beginRemoveRows(parent, row, row + count - 1); - endRemoveRows(); - return true; + if (parent.isValid()) + return 0; + return RowCount + m_firstRow; } - bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override + + int columnCount(const QModelIndex &parent) const override { - beginRemoveColumns(parent, column, column + count - 1); - endRemoveColumns(); - return true; + if (parent.isValid()) + return 0; + return ColumnCount + m_firstColumn; } + QVariant data(const QModelIndex &index, int role) const override; + Qt::ItemFlags flags(const QModelIndex &index) const override; + void showMonth(int year, int month); void setDate(QDate d); @@ -1303,11 +1288,13 @@ void QCalendarModel::setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeader int oldFormat = m_horizontalHeaderFormat; m_horizontalHeaderFormat = format; if (oldFormat == QCalendarWidget::NoHorizontalHeader) { + beginInsertRows(QModelIndex(), 0, 0); m_firstRow = 1; - insertRow(0); + endInsertRows(); } else if (m_horizontalHeaderFormat == QCalendarWidget::NoHorizontalHeader) { + beginRemoveRows(QModelIndex(), 0, 0); m_firstRow = 0; - removeRow(0); + endRemoveRows(); } internalUpdate(); } @@ -1338,11 +1325,13 @@ void QCalendarModel::setWeekNumbersShown(bool show) m_weekNumbersShown = show; if (show) { + beginInsertColumns(QModelIndex(), 0, 0); m_firstColumn = 1; - insertColumn(0); + endInsertColumns(); } else { + beginRemoveColumns(QModelIndex(), 0, 0); m_firstColumn = 0; - removeColumn(0); + endRemoveColumns(); } internalUpdate(); } -- cgit v1.2.3