File indexing completed on 2024-04-21 05:54:06

0001 /**
0002  * SPDX-FileCopyrightText: 2020 by Alexander Stippich <a.stippich@gmx.net>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 #include <KSaneCore/DeviceInformation>
0007 
0008 #include "DevicesModel.h"
0009 
0010 class DevicesModelPrivate
0011 {
0012 public:
0013     QList<KSaneCore::DeviceInformation *> mDeviceslist;
0014 
0015     int mSelectedDevice = 0;
0016 };
0017 
0018 DevicesModel::DevicesModel(QObject *parent)
0019     : QAbstractListModel(parent)
0020     , d(std::make_unique<DevicesModelPrivate>())
0021 {
0022 }
0023 
0024 DevicesModel::~DevicesModel()
0025 {
0026 }
0027 
0028 QHash<int, QByteArray> DevicesModel::roleNames() const
0029 {
0030     QHash<int, QByteArray> roles;
0031     roles[NameRole] = "name";
0032     roles[VendorRole] = "vendor";
0033     roles[ModelRole] = "model";
0034     roles[TypeRole] = "type";
0035     return roles;
0036 }
0037 
0038 int DevicesModel::rowCount(const QModelIndex &) const
0039 {
0040     return d->mDeviceslist.count();
0041 }
0042 
0043 QVariant DevicesModel::data(const QModelIndex &index, int role) const
0044 {
0045     if (!index.isValid()) {
0046         return QVariant();
0047     }
0048 
0049     if (index.row() >= d->mDeviceslist.size() || index.row() < 0) {
0050         return QVariant();
0051     }
0052 
0053     switch (role) {
0054     case NameRole:
0055         return d->mDeviceslist.at(index.row())->name();
0056         break;
0057     case VendorRole:
0058         return d->mDeviceslist.at(index.row())->vendor();
0059         break;
0060     case ModelRole:
0061         return d->mDeviceslist.at(index.row())->model();
0062         break;
0063     case TypeRole:
0064         return d->mDeviceslist.at(index.row())->type();
0065         break;
0066     default:
0067         break;
0068     }
0069     return QVariant();
0070 }
0071 
0072 void DevicesModel::updateDevicesList(const QList<KSaneCore::DeviceInformation *> &deviceList)
0073 {
0074     beginResetModel();
0075     d->mDeviceslist = deviceList;
0076     endResetModel();
0077     Q_EMIT rowCountChanged();
0078 }
0079 
0080 QString DevicesModel::getSelectedDeviceName() const
0081 {
0082     if (d->mSelectedDevice >= 0 && d->mSelectedDevice < d->mDeviceslist.count()) {
0083         return d->mDeviceslist.at(d->mSelectedDevice)->name();
0084     }
0085     return QString();
0086 }
0087 
0088 void DevicesModel::selectDevice(int i)
0089 {
0090     d->mSelectedDevice = i;
0091 }
0092 
0093 QDebug operator<<(QDebug d, KSaneCore::DeviceInformation *deviceInfo)
0094 {
0095     d << "Device name: " << deviceInfo->name() << "\n";
0096     d << "Device vendor: " << deviceInfo->vendor() << "\n";
0097     d << "Device model: " << deviceInfo->model() << "\n";
0098     d << "Device type: " << deviceInfo->type() << "\n";
0099     return d;
0100 }
0101 
0102 #include "moc_DevicesModel.cpp"