File indexing completed on 2024-03-24 17:21:53

0001 /* generated by rust_qt_binding_generator */
0002 #include "test_tree_rust.h"
0003 
0004 namespace {
0005 
0006     struct option_quintptr {
0007     public:
0008         quintptr value;
0009         bool some;
0010         operator QVariant() const {
0011             if (some) {
0012                 return QVariant::fromValue(value);
0013             }
0014             return QVariant();
0015         }
0016     };
0017     static_assert(std::is_pod<option_quintptr>::value, "option_quintptr must be a POD type.");
0018 
0019     typedef void (*qstring_set)(QString* val, const char* utf8, int nbytes);
0020     void set_qstring(QString* val, const char* utf8, int nbytes) {
0021         *val = QString::fromUtf8(utf8, nbytes);
0022     }
0023 
0024     struct qmodelindex_t {
0025         int row;
0026         quintptr id;
0027     };
0028     inline QVariant cleanNullQVariant(const QVariant& v) {
0029         return (v.isNull()) ?QVariant() :v;
0030     }
0031 }
0032 extern "C" {
0033     void persons_data_user_name(const Persons::Private*, quintptr, QString*, qstring_set);
0034     bool persons_set_data_user_name(Persons::Private*, quintptr, const ushort* s, int len);
0035     void persons_sort(Persons::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder);
0036 
0037     int persons_row_count(const Persons::Private*, option_quintptr);
0038     bool persons_can_fetch_more(const Persons::Private*, option_quintptr);
0039     void persons_fetch_more(Persons::Private*, option_quintptr);
0040     quintptr persons_index(const Persons::Private*, option_quintptr, int);
0041     qmodelindex_t persons_parent(const Persons::Private*, quintptr);
0042     int persons_row(const Persons::Private*, quintptr);
0043     option_quintptr persons_check_row(const Persons::Private*, quintptr, int);
0044 }
0045 int Persons::columnCount(const QModelIndex &) const
0046 {
0047     return 1;
0048 }
0049 
0050 bool Persons::hasChildren(const QModelIndex &parent) const
0051 {
0052     return rowCount(parent) > 0;
0053 }
0054 
0055 int Persons::rowCount(const QModelIndex &parent) const
0056 {
0057     if (parent.isValid() && parent.column() != 0) {
0058         return 0;
0059     }
0060     const option_quintptr rust_parent = {
0061         parent.internalId(),
0062         parent.isValid()
0063     };
0064     return persons_row_count(m_d, rust_parent);
0065 }
0066 
0067 bool Persons::insertRows(int, int, const QModelIndex &)
0068 {
0069     return false; // not supported yet
0070 }
0071 
0072 bool Persons::removeRows(int, int, const QModelIndex &)
0073 {
0074     return false; // not supported yet
0075 }
0076 
0077 QModelIndex Persons::index(int row, int column, const QModelIndex &parent) const
0078 {
0079     if (row < 0 || column < 0 || column >= 1) {
0080         return QModelIndex();
0081     }
0082     if (parent.isValid() && parent.column() != 0) {
0083         return QModelIndex();
0084     }
0085     if (row >= rowCount(parent)) {
0086         return QModelIndex();
0087     }
0088     const option_quintptr rust_parent = {
0089         parent.internalId(),
0090         parent.isValid()
0091     };
0092     const quintptr id = persons_index(m_d, rust_parent, row);
0093     return createIndex(row, column, id);
0094 }
0095 
0096 QModelIndex Persons::parent(const QModelIndex &index) const
0097 {
0098     if (!index.isValid()) {
0099         return QModelIndex();
0100     }
0101     const qmodelindex_t parent = persons_parent(m_d, index.internalId());
0102     return parent.row >= 0 ?createIndex(parent.row, 0, parent.id) :QModelIndex();
0103 }
0104 
0105 bool Persons::canFetchMore(const QModelIndex &parent) const
0106 {
0107     if (parent.isValid() && parent.column() != 0) {
0108         return false;
0109     }
0110     const option_quintptr rust_parent = {
0111         parent.internalId(),
0112         parent.isValid()
0113     };
0114     return persons_can_fetch_more(m_d, rust_parent);
0115 }
0116 
0117 void Persons::fetchMore(const QModelIndex &parent)
0118 {
0119     const option_quintptr rust_parent = {
0120         parent.internalId(),
0121         parent.isValid()
0122     };
0123     persons_fetch_more(m_d, rust_parent);
0124 }
0125 void Persons::updatePersistentIndexes() {
0126     const auto from = persistentIndexList();
0127     auto to = from;
0128     auto len = to.size();
0129     for (int i = 0; i < len; ++i) {
0130         auto index = to.at(i);
0131         auto row = persons_check_row(m_d, index.internalId(), index.row());
0132         if (row.some) {
0133             to[i] = createIndex(row.value, index.column(), index.internalId());
0134         } else {
0135             to[i] = QModelIndex();
0136         }
0137     }
0138     changePersistentIndexList(from, to);
0139 }
0140 
0141 void Persons::sort(int column, Qt::SortOrder order)
0142 {
0143     persons_sort(m_d, column, order);
0144 }
0145 Qt::ItemFlags Persons::flags(const QModelIndex &i) const
0146 {
0147     auto flags = QAbstractItemModel::flags(i);
0148     if (i.column() == 0) {
0149         flags |= Qt::ItemIsEditable;
0150     }
0151     return flags;
0152 }
0153 
0154 QString Persons::userName(const QModelIndex& index) const
0155 {
0156     QString s;
0157     persons_data_user_name(m_d, index.internalId(), &s, set_qstring);
0158     return s;
0159 }
0160 
0161 bool Persons::setUserName(const QModelIndex& index, const QString& value)
0162 {
0163     bool set = false;
0164     set = persons_set_data_user_name(m_d, index.internalId(), value.utf16(), value.length());
0165     if (set) {
0166         Q_EMIT dataChanged(index, index);
0167     }
0168     return set;
0169 }
0170 
0171 QVariant Persons::data(const QModelIndex &index, int role) const
0172 {
0173     Q_ASSERT(rowCount(index.parent()) > index.row());
0174     switch (index.column()) {
0175     case 0:
0176         switch (role) {
0177         case Qt::DisplayRole:
0178         case Qt::EditRole:
0179         case Qt::UserRole + 0:
0180             return QVariant::fromValue(userName(index));
0181         }
0182         break;
0183     }
0184     return QVariant();
0185 }
0186 
0187 int Persons::role(const char* name) const {
0188     auto names = roleNames();
0189     auto i = names.constBegin();
0190     while (i != names.constEnd()) {
0191         if (i.value() == name) {
0192             return i.key();
0193         }
0194         ++i;
0195     }
0196     return -1;
0197 }
0198 QHash<int, QByteArray> Persons::roleNames() const {
0199     QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
0200     names.insert(Qt::UserRole + 0, "userName");
0201     return names;
0202 }
0203 QVariant Persons::headerData(int section, Qt::Orientation orientation, int role) const
0204 {
0205     if (orientation != Qt::Horizontal) {
0206         return QVariant();
0207     }
0208     return m_headerData.value(qMakePair(section, (Qt::ItemDataRole)role), role == Qt::DisplayRole ?QString::number(section + 1) :QVariant());
0209 }
0210 
0211 bool Persons::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
0212 {
0213     if (orientation != Qt::Horizontal) {
0214         return false;
0215     }
0216     m_headerData.insert(qMakePair(section, (Qt::ItemDataRole)role), value);
0217     return true;
0218 }
0219 
0220 bool Persons::setData(const QModelIndex &index, const QVariant &value, int role)
0221 {
0222     if (index.column() == 0) {
0223         if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole + 0) {
0224             if (value.canConvert(qMetaTypeId<QString>())) {
0225                 return setUserName(index, value.value<QString>());
0226             }
0227         }
0228     }
0229     return false;
0230 }
0231 
0232 extern "C" {
0233     Persons::Private* persons_new(Persons*,
0234         void (*)(const Persons*, option_quintptr),
0235         void (*)(Persons*),
0236         void (*)(Persons*),
0237         void (*)(Persons*, quintptr, quintptr),
0238         void (*)(Persons*),
0239         void (*)(Persons*),
0240         void (*)(Persons*, option_quintptr, int, int),
0241         void (*)(Persons*),
0242         void (*)(Persons*, option_quintptr, int, int, option_quintptr, int),
0243         void (*)(Persons*),
0244         void (*)(Persons*, option_quintptr, int, int),
0245         void (*)(Persons*));
0246     void persons_free(Persons::Private*);
0247 };
0248 
0249 Persons::Persons(bool /*owned*/, QObject *parent):
0250     QAbstractItemModel(parent),
0251     m_d(nullptr),
0252     m_ownsPrivate(false)
0253 {
0254     initHeaderData();
0255 }
0256 
0257 Persons::Persons(QObject *parent):
0258     QAbstractItemModel(parent),
0259     m_d(persons_new(this,
0260         [](const Persons* o, option_quintptr id) {
0261             if (id.some) {
0262                 int row = persons_row(o->m_d, id.value);
0263                 Q_EMIT o->newDataReady(o->createIndex(row, 0, id.value));
0264             } else {
0265                 Q_EMIT o->newDataReady(QModelIndex());
0266             }
0267         },
0268         [](Persons* o) {
0269             Q_EMIT o->layoutAboutToBeChanged();
0270         },
0271         [](Persons* o) {
0272             o->updatePersistentIndexes();
0273             Q_EMIT o->layoutChanged();
0274         },
0275         [](Persons* o, quintptr first, quintptr last) {
0276             quintptr frow = persons_row(o->m_d, first);
0277             quintptr lrow = persons_row(o->m_d, first);
0278             o->dataChanged(o->createIndex(frow, 0, first),
0279                        o->createIndex(lrow, 0, last));
0280         },
0281         [](Persons* o) {
0282             o->beginResetModel();
0283         },
0284         [](Persons* o) {
0285             o->endResetModel();
0286         },
0287         [](Persons* o, option_quintptr id, int first, int last) {
0288             if (id.some) {
0289                 int row = persons_row(o->m_d, id.value);
0290                 o->beginInsertRows(o->createIndex(row, 0, id.value), first, last);
0291             } else {
0292                 o->beginInsertRows(QModelIndex(), first, last);
0293             }
0294         },
0295         [](Persons* o) {
0296             o->endInsertRows();
0297         },
0298         [](Persons* o, option_quintptr sourceParent, int first, int last, option_quintptr destinationParent, int destination) {
0299             QModelIndex s;
0300             if (sourceParent.some) {
0301                 int row = persons_row(o->m_d, sourceParent.value);
0302                 s = o->createIndex(row, 0, sourceParent.value);
0303             }
0304             QModelIndex d;
0305             if (destinationParent.some) {
0306                 int row = persons_row(o->m_d, destinationParent.value);
0307                 d = o->createIndex(row, 0, destinationParent.value);
0308             }
0309             o->beginMoveRows(s, first, last, d, destination);
0310         },
0311         [](Persons* o) {
0312             o->endMoveRows();
0313         },
0314         [](Persons* o, option_quintptr id, int first, int last) {
0315             if (id.some) {
0316                 int row = persons_row(o->m_d, id.value);
0317                 o->beginRemoveRows(o->createIndex(row, 0, id.value), first, last);
0318             } else {
0319                 o->beginRemoveRows(QModelIndex(), first, last);
0320             }
0321         },
0322         [](Persons* o) {
0323             o->endRemoveRows();
0324         }
0325 )),
0326     m_ownsPrivate(true)
0327 {
0328     connect(this, &Persons::newDataReady, this, [this](const QModelIndex& i) {
0329         this->fetchMore(i);
0330     }, Qt::QueuedConnection);
0331     initHeaderData();
0332 }
0333 
0334 Persons::~Persons() {
0335     if (m_ownsPrivate) {
0336         persons_free(m_d);
0337     }
0338 }
0339 void Persons::initHeaderData() {
0340     m_headerData.insert(qMakePair(0, Qt::DisplayRole), QVariant("userName"));
0341 }