|
| 1 | +#include "PackageListProxyModel_p.h" |
| 2 | + |
| 3 | +#include <uishell/USDef.h> |
| 4 | + |
| 5 | +namespace UIShell { |
| 6 | + |
| 7 | + PackageListProxyModel::PackageListProxyModel(QObject *parent) |
| 8 | + : QIdentityProxyModel(parent) { |
| 9 | + } |
| 10 | + |
| 11 | + PackageListProxyModel::~PackageListProxyModel() = default; |
| 12 | + |
| 13 | + QHash<int, QByteArray> PackageListProxyModel::roleNames() const { |
| 14 | + static const QHash<int, QByteArray> m{ |
| 15 | + {USDef::PR_IdRole, "id"}, |
| 16 | + {USDef::PR_VersionRole, "version"}, |
| 17 | + {USDef::PR_PathRole, "path"}, |
| 18 | + {USDef::PR_InstallationTimeRole, "installationTime"}, |
| 19 | + {USDef::PR_NameRole, "name"}, |
| 20 | + {USDef::PR_DescriptionRole, "description"}, |
| 21 | + {USDef::PR_VendorRole, "vendor"}, |
| 22 | + {USDef::PR_ReadmePathRole, "readmePath"}, |
| 23 | + {USDef::PR_LicensePathRole, "licensePath"}, |
| 24 | + {USDef::PR_UrlRole, "url"}, |
| 25 | + {USDef::PR_ClassNameRole, "className"}, |
| 26 | + {USDef::PR_AvatarPathRole, "avatarPath"}, |
| 27 | + {USDef::PR_BackgroundPathRole, "backgroundPath"}, |
| 28 | + {USDef::PR_ImportInferenceIdRole, "importInferenceId"}, |
| 29 | + }; |
| 30 | + return m; |
| 31 | + } |
| 32 | + |
| 33 | + QModelIndex PackageListProxyModel::packageModelIndex(int index) const { |
| 34 | + return this->index(index, 0); |
| 35 | + } |
| 36 | + |
| 37 | + QModelIndex PackageListProxyModel::entryIndex(const QModelIndex &index) const { |
| 38 | + const auto singerRootIndex = this->singerRootIndexForIndex(index); |
| 39 | + if (!singerRootIndex.isValid() || rowCount(singerRootIndex) == 0) { |
| 40 | + const auto inferenceRootIndex = this->inferenceRootIndexForIndex(index); |
| 41 | + if (!inferenceRootIndex.isValid() || rowCount(inferenceRootIndex) == 0) { |
| 42 | + return singerRootIndex; |
| 43 | + } |
| 44 | + return inferenceRootIndex; |
| 45 | + } |
| 46 | + return this->index(0, 0, singerRootIndex); |
| 47 | + } |
| 48 | + |
| 49 | + QModelIndex PackageListProxyModel::singerModelIndexForIndex(const QModelIndex &index) const { |
| 50 | + if (isSingerIndex(index)) { |
| 51 | + return index; |
| 52 | + } |
| 53 | + |
| 54 | + const auto singerRootIndex = this->singerRootIndexForIndex(index); |
| 55 | + if (!singerRootIndex.isValid() || rowCount(singerRootIndex) == 0) { |
| 56 | + return singerRootIndex; |
| 57 | + } |
| 58 | + |
| 59 | + return this->index(0, 0, singerRootIndex); |
| 60 | + } |
| 61 | + |
| 62 | + QModelIndex PackageListProxyModel::packageModelIndexForIndex(const QModelIndex &index) const { |
| 63 | + if (!index.isValid() || index.model() != this) { |
| 64 | + return {}; |
| 65 | + } |
| 66 | + |
| 67 | + auto packageIndex = index; |
| 68 | + while (packageIndex.parent().isValid()) { |
| 69 | + packageIndex = packageIndex.parent(); |
| 70 | + } |
| 71 | + |
| 72 | + return packageIndex; |
| 73 | + } |
| 74 | + |
| 75 | + QModelIndex PackageListProxyModel::dependencyRootIndexForIndex(const QModelIndex &index) const { |
| 76 | + const auto packageIndex = packageModelIndexForIndex(index); |
| 77 | + return packageIndex.isValid() ? this->index(USDef::PI_Dependencies, 0, packageIndex) : QModelIndex(); |
| 78 | + } |
| 79 | + |
| 80 | + QModelIndex PackageListProxyModel::singerRootIndexForIndex(const QModelIndex &index) const { |
| 81 | + const auto packageIndex = packageModelIndexForIndex(index); |
| 82 | + return packageIndex.isValid() ? this->index(USDef::PI_Singers, 0, packageIndex) : QModelIndex(); |
| 83 | + } |
| 84 | + |
| 85 | + QModelIndex PackageListProxyModel::inferenceRootIndexForIndex(const QModelIndex &index) const { |
| 86 | + const auto packageIndex = packageModelIndexForIndex(index); |
| 87 | + return packageIndex.isValid() ? this->index(USDef::PI_Inferences, 0, packageIndex) : QModelIndex(); |
| 88 | + } |
| 89 | + |
| 90 | + QModelIndex PackageListProxyModel::importRootIndexForSingerIndex(const QModelIndex &index) const { |
| 91 | + return isSingerIndex(index) ? this->index(USDef::PI_SingerImports, 0, index) : QModelIndex(); |
| 92 | + } |
| 93 | + |
| 94 | + QModelIndex PackageListProxyModel::demoAudioRootIndexForSingerIndex(const QModelIndex &index) const { |
| 95 | + return isSingerIndex(index) ? this->index(USDef::PI_SingerDemoAudioList, 0, index) : QModelIndex(); |
| 96 | + } |
| 97 | + |
| 98 | + bool PackageListProxyModel::isSingerOrSingerRootIndex(const QModelIndex &index) const { |
| 99 | + if (!index.isValid() || index.model() != this) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + if (isSingerIndex(index)) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + return index.row() == USDef::PI_Singers && |
| 108 | + index.parent().isValid() && |
| 109 | + !index.parent().parent().isValid(); |
| 110 | + } |
| 111 | + |
| 112 | + bool PackageListProxyModel::isInferenceOrInferenceRootIndex(const QModelIndex &index) const { |
| 113 | + if (!index.isValid() || index.model() != this) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + if (isInferenceIndex(index)) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + return index.row() == USDef::PI_Inferences && |
| 122 | + index.parent().isValid() && |
| 123 | + !index.parent().parent().isValid(); |
| 124 | + } |
| 125 | + |
| 126 | + bool PackageListProxyModel::isPackageIndex(const QModelIndex &index) const { |
| 127 | + return index.isValid() && |
| 128 | + index.model() == this && |
| 129 | + !index.parent().isValid(); |
| 130 | + } |
| 131 | + |
| 132 | + bool PackageListProxyModel::isDependencyRootIndex(const QModelIndex &index) const { |
| 133 | + return index.isValid() && |
| 134 | + index.model() == this && |
| 135 | + index.row() == USDef::PI_Dependencies && |
| 136 | + index.parent().isValid() && |
| 137 | + !index.parent().parent().isValid(); |
| 138 | + } |
| 139 | + |
| 140 | + int PackageListProxyModel::findPackageIndex(const QString &id, const QString &version) const { |
| 141 | + for (int i = 0; i < rowCount(); ++i) { |
| 142 | + const auto packageIndex = index(i, 0); |
| 143 | + if (data(packageIndex, USDef::PR_IdRole).toString() == id && |
| 144 | + data(packageIndex, USDef::PR_VersionRole).toString() == version) { |
| 145 | + return i; |
| 146 | + } |
| 147 | + } |
| 148 | + return -1; |
| 149 | + } |
| 150 | + |
| 151 | + QModelIndex PackageListProxyModel::findInferenceIndex(const QString &id, const QString &version, const QString &inferenceId) const { |
| 152 | + const int packageRow = findPackageIndex(id, version); |
| 153 | + if (packageRow < 0) { |
| 154 | + return {}; |
| 155 | + } |
| 156 | + |
| 157 | + const auto packageIndex = index(packageRow, 0); |
| 158 | + const auto inferencesIndex = index(USDef::PI_Inferences, 0, packageIndex); |
| 159 | + if (!inferencesIndex.isValid()) { |
| 160 | + return {}; |
| 161 | + } |
| 162 | + |
| 163 | + for (int i = 0; i < rowCount(inferencesIndex); ++i) { |
| 164 | + const auto inferenceIndex = index(i, 0, inferencesIndex); |
| 165 | + if (data(inferenceIndex, USDef::PR_IdRole).toString() == inferenceId) { |
| 166 | + return inferenceIndex; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return {}; |
| 171 | + } |
| 172 | + |
| 173 | + int PackageListProxyModel::packageIndexForIndex(const QModelIndex &index) const { |
| 174 | + if (!index.isValid() || index.model() != this) { |
| 175 | + return -1; |
| 176 | + } |
| 177 | + |
| 178 | + auto packageIndex = index; |
| 179 | + while (packageIndex.parent().isValid()) { |
| 180 | + packageIndex = packageIndex.parent(); |
| 181 | + } |
| 182 | + |
| 183 | + return packageIndex.row(); |
| 184 | + } |
| 185 | + |
| 186 | + bool PackageListProxyModel::isSingerIndex(const QModelIndex &index) const { |
| 187 | + if (!index.isValid() || index.model() != this) { |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | + const auto parentIndex = index.parent(); |
| 192 | + return parentIndex.isValid() && |
| 193 | + parentIndex.row() == USDef::PI_Singers && |
| 194 | + parentIndex.parent().isValid() && |
| 195 | + !parentIndex.parent().parent().isValid(); |
| 196 | + } |
| 197 | + |
| 198 | + bool PackageListProxyModel::isInferenceIndex(const QModelIndex &index) const { |
| 199 | + if (!index.isValid() || index.model() != this) { |
| 200 | + return false; |
| 201 | + } |
| 202 | + |
| 203 | + const auto parentIndex = index.parent(); |
| 204 | + return parentIndex.isValid() && |
| 205 | + parentIndex.row() == USDef::PI_Inferences && |
| 206 | + parentIndex.parent().isValid() && |
| 207 | + !parentIndex.parent().parent().isValid(); |
| 208 | + } |
| 209 | + |
| 210 | +} |
0 commit comments