Changeset View
Changeset View
Standalone View
Standalone View
src/core/model/accountschannelsmodel.cpp
Show First 20 Lines • Show All 76 Lines • ▼ Show 20 Line(s) | 32 | { | |||
---|---|---|---|---|---|
77 | mapModelToIndex(acctsProxy, [] { | 77 | mapModelToIndex(acctsProxy, [] { | ||
78 | return QModelIndex(); | 78 | return QModelIndex(); | ||
79 | }); | 79 | }); | ||
80 | for (int i = 0, count = acctsProxy->rowCount(); i < count; ++i) { | 80 | for (int i = 0, count = acctsProxy->rowCount(); i < count; ++i) { | ||
81 | mapRoomsModel(i); | 81 | mapRoomsModel(i); | ||
82 | } | 82 | } | ||
83 | } | 83 | } | ||
84 | 84 | | |||
85 | void AccountsChannelsModel::setFilterString(const QString &filter) | ||||
86 | { | ||||
87 | mFiltered = !filter.isEmpty(); | ||||
88 | visitProxied<RoomFilterProxyModel>([&](RoomFilterProxyModel *m, const QModelIndex &){ | ||||
89 | m->setFilterString(filter); | ||||
90 | return Continue; | ||||
91 | }); | ||||
92 | } | ||||
93 | | ||||
94 | bool AccountsChannelsModel::isFiltered() const | ||||
95 | { | ||||
96 | return mFiltered; | ||||
97 | } | ||||
98 | | ||||
85 | QModelIndex AccountsChannelsModel::index(int row, int column, const QModelIndex &parent) const | 99 | QModelIndex AccountsChannelsModel::index(int row, int column, const QModelIndex &parent) const | ||
86 | { | 100 | { | ||
87 | if (auto model = rootModel(parent)) { | 101 | const auto model = rootModel(parent); | ||
102 | if (model && column == 0 && row >= 0 && row < model->rowCount()) { | ||||
88 | return createIndex(row, column, model); | 103 | return createIndex(row, column, model); | ||
89 | } | 104 | } | ||
90 | return {}; | 105 | return {}; | ||
91 | } | 106 | } | ||
92 | 107 | | |||
93 | QModelIndex AccountsChannelsModel::parent(const QModelIndex &child) const | 108 | QModelIndex AccountsChannelsModel::parent(const QModelIndex &child) const | ||
94 | { | 109 | { | ||
95 | if (!child.isValid()) { | 110 | if (!child.isValid()) { | ||
Show All 29 Lines | 135 | { | |||
125 | const auto model = static_cast<QAbstractItemModel *>(index.internalPointer()); | 140 | const auto model = static_cast<QAbstractItemModel *>(index.internalPointer()); | ||
126 | if (!model) { | 141 | if (!model) { | ||
127 | return {}; | 142 | return {}; | ||
128 | } | 143 | } | ||
129 | 144 | | |||
130 | return model->index(index.row(), index.column()).data(role); | 145 | return model->index(index.row(), index.column()).data(role); | ||
131 | } | 146 | } | ||
132 | 147 | | |||
148 | QModelIndex AccountsChannelsModel::findRoomById(const QString &roomId) const | ||||
149 | { | ||||
150 | return findRoomByRole(RoomModel::RoomID, roomId); | ||||
151 | } | ||||
152 | | ||||
153 | QModelIndex AccountsChannelsModel::findRoomByName(const QString &roomName) const | ||||
154 | { | ||||
155 | return findRoomByRole(RoomModel::RoomName, roomName); | ||||
156 | } | ||||
157 | | ||||
158 | QModelIndex AccountsChannelsModel::findRoomByRole(int role, const QVariant &value) const | ||||
159 | { | ||||
160 | QModelIndex found; | ||||
161 | visitProxied<RoomFilterProxyModel>([&](RoomFilterProxyModel *m, const QModelIndex &root){ | ||||
162 | for (int i = 0, count = m->rowCount(); i < count; ++i) | ||||
163 | { | ||||
164 | if (m->index(i, 0).data(role) == value) | ||||
165 | { | ||||
166 | found = index(i, 0, root); | ||||
167 | return Abort; | ||||
168 | } | ||||
169 | } | ||||
170 | return Continue; | ||||
171 | }); | ||||
172 | return found; | ||||
173 | } | ||||
174 | | ||||
175 | QString AccountsChannelsModel::accountForIndex(const QModelIndex &index) const | ||||
176 | { | ||||
177 | if (!index.isValid()) | ||||
178 | return {}; | ||||
179 | | ||||
180 | const auto parent = index.parent(); | ||||
181 | const auto accountIndex = parent.isValid() ? parent : index; | ||||
182 | return accountIndex.data(RocketChatAccountModel::Name).toString(); | ||||
183 | } | ||||
184 | | ||||
133 | QModelIndex AccountsChannelsModel::modelRoot(QAbstractItemModel *model) const | 185 | QModelIndex AccountsChannelsModel::modelRoot(QAbstractItemModel *model) const | ||
134 | { | 186 | { | ||
135 | const auto find = [model](const ProxyIndex &i){ | 187 | const auto find = [model](const ProxyIndex &i){ | ||
136 | return i.model == model; | 188 | return i.model == model; | ||
137 | }; | 189 | }; | ||
138 | const auto it = std::find_if(mProxied.begin(), mProxied.end(), find); | 190 | const auto it = std::find_if(mProxied.begin(), mProxied.end(), find); | ||
139 | return (it == mProxied.end()) ? QModelIndex() : it->root(); | 191 | return (it == mProxied.end()) ? QModelIndex() : it->root(); | ||
140 | } | 192 | } | ||
Show All 10 Lines | |||||
151 | void AccountsChannelsModel::mapModelToIndex(QAbstractItemModel *model, const std::function<QModelIndex()> &root) | 203 | void AccountsChannelsModel::mapModelToIndex(QAbstractItemModel *model, const std::function<QModelIndex()> &root) | ||
152 | { | 204 | { | ||
153 | connect(model, &QAbstractItemModel::rowsAboutToBeInserted, this, | 205 | connect(model, &QAbstractItemModel::rowsAboutToBeInserted, this, | ||
154 | [this, model](const QModelIndex &parent, int first, int last) { | 206 | [this, model](const QModelIndex &parent, int first, int last) { | ||
155 | Q_ASSERT(!parent.isValid()); | 207 | Q_ASSERT(!parent.isValid()); | ||
156 | beginInsertRows(modelRoot(model), first, last); | 208 | beginInsertRows(modelRoot(model), first, last); | ||
157 | }); | 209 | }); | ||
158 | connect(model, &QAbstractItemModel::rowsInserted, this, &AccountsChannelsModel::endInsertRows); | 210 | connect(model, &QAbstractItemModel::rowsInserted, this, &AccountsChannelsModel::endInsertRows); | ||
211 | connect(model, &QAbstractItemModel::rowsInserted, this, &AccountsChannelsModel::modelChanged); | ||||
159 | 212 | | |||
160 | connect(model, &QAbstractItemModel::rowsAboutToBeRemoved, this, | 213 | connect(model, &QAbstractItemModel::rowsAboutToBeRemoved, this, | ||
161 | [this, model](const QModelIndex &parent, int first, int last) { | 214 | [this, model](const QModelIndex &parent, int first, int last) { | ||
162 | Q_ASSERT(!parent.isValid()); | 215 | Q_ASSERT(!parent.isValid()); | ||
163 | beginRemoveRows(modelRoot(model), first, last); | 216 | beginRemoveRows(modelRoot(model), first, last); | ||
164 | }); | 217 | }); | ||
165 | connect(model, &QAbstractItemModel::rowsRemoved, this, &AccountsChannelsModel::endRemoveRows); | 218 | connect(model, &QAbstractItemModel::rowsRemoved, this, &AccountsChannelsModel::endRemoveRows); | ||
219 | connect(model, &QAbstractItemModel::rowsRemoved, this, &AccountsChannelsModel::modelChanged); | ||||
166 | 220 | | |||
167 | connect(model, &QAbstractItemModel::rowsAboutToBeMoved, this, | 221 | connect(model, &QAbstractItemModel::rowsAboutToBeMoved, this, | ||
168 | [this, model](const QModelIndex &src, int sf, int sl, const QModelIndex &dst, int df) { | 222 | [this, model](const QModelIndex &src, int sf, int sl, const QModelIndex &dst, int df) { | ||
169 | Q_ASSERT(!src.isValid() && !dst.isValid()); | 223 | Q_ASSERT(!src.isValid() && !dst.isValid()); | ||
170 | const auto idx = modelRoot(model); | 224 | const auto idx = modelRoot(model); | ||
171 | beginMoveRows(idx, sf, sl, idx, df); | 225 | beginMoveRows(idx, sf, sl, idx, df); | ||
172 | }); | 226 | }); | ||
173 | connect(model, &QAbstractItemModel::rowsMoved, this, &AccountsChannelsModel::endMoveRows); | 227 | connect(model, &QAbstractItemModel::rowsMoved, this, &AccountsChannelsModel::endMoveRows); | ||
228 | connect(model, &QAbstractItemModel::rowsMoved, this, &AccountsChannelsModel::modelChanged); | ||||
174 | 229 | | |||
175 | connect(model, &QAbstractItemModel::modelAboutToBeReset, this, &AccountsChannelsModel::beginResetModel); | 230 | connect(model, &QAbstractItemModel::modelAboutToBeReset, this, &AccountsChannelsModel::beginResetModel); | ||
176 | connect(model, &QAbstractItemModel::modelReset, this, &AccountsChannelsModel::endResetModel); | 231 | connect(model, &QAbstractItemModel::modelReset, this, &AccountsChannelsModel::endResetModel); | ||
232 | connect(model, &QAbstractItemModel::modelReset, this, &AccountsChannelsModel::modelChanged); | ||||
177 | 233 | | |||
178 | connect(model, &QAbstractItemModel::layoutAboutToBeChanged, this, &AccountsChannelsModel::layoutAboutToBeChanged); | 234 | connect(model, &QAbstractItemModel::layoutAboutToBeChanged, this, &AccountsChannelsModel::layoutAboutToBeChanged); | ||
179 | connect(model, &QAbstractItemModel::layoutChanged, this, &AccountsChannelsModel::layoutChanged); | 235 | connect(model, &QAbstractItemModel::layoutChanged, this, &AccountsChannelsModel::layoutChanged); | ||
236 | connect(model, &QAbstractItemModel::layoutChanged, this, &AccountsChannelsModel::modelChanged); | ||||
180 | 237 | | |||
181 | connect(model, &QAbstractItemModel::dataChanged, this, | 238 | connect(model, &QAbstractItemModel::dataChanged, this, | ||
182 | [this, model](const QModelIndex &tl, const QModelIndex &br) { | 239 | [this, model](const QModelIndex &tl, const QModelIndex &br) { | ||
183 | const auto parent = modelRoot(model); | 240 | const auto parent = modelRoot(model); | ||
184 | Q_EMIT dataChanged(index(tl.row(), tl.column(), parent), index(br.row(), br.column(), parent)); | 241 | Q_EMIT dataChanged(index(tl.row(), tl.column(), parent), index(br.row(), br.column(), parent)); | ||
185 | }); | 242 | }); | ||
186 | 243 | | |||
187 | mProxied.append({model, root}); | 244 | mProxied.append({model, root}); | ||
Show All 13 Lines |