Verified Commit 019f687a authored by Ingo Klöcker's avatar Ingo Klöcker Committed by Ingo Klöcker
Browse files

Store selected keys instead of their indexes

The indexes become invalid if the key list model is reset, e.g. when the
keys are reloaded. To avoid problems caused by invalid indexes we store
the selected keys instead of their indexes.

GnuPG-bug-id: 6157
parent c963a702
......@@ -166,19 +166,23 @@ void Command::setView(QAbstractItemView *view)
return;
}
d->view_ = view;
if (!view || !d->indexes_.empty()) {
if (!view || !d->keys_.empty()) {
return;
}
const auto *const keyListModel = dynamic_cast<KeyListModelInterface *>(view->model());
if (!keyListModel) {
qCWarning(KLEOPATRA_LOG) << "view" << view << "has not key list model";
return;
}
const QItemSelectionModel *const sm = view->selectionModel();
if (!sm) {
qCWarning(KLEOPATRA_LOG) << "view " << (void *)view << " has no selectionModel!";
qCWarning(KLEOPATRA_LOG) << "view" << view << "has no selection model";
return;
}
const QList<QModelIndex> selected = sm->selectedRows();
if (!selected.empty()) {
std::copy(selected.begin(), selected.end(), std::back_inserter(d->indexes_));
return;
}
std::transform(selected.begin(), selected.end(), std::back_inserter(d->keys_), [keyListModel](const auto &idx) {
return keyListModel->key(idx);
});
}
void Command::setKey(const Key &key)
......
......@@ -54,27 +54,17 @@ public:
{
return parentWId_;
}
KeyListModelInterface *model() const
{
return view_ ? dynamic_cast<KeyListModelInterface *>(view_->model()) : nullptr;
}
KeyListController *controller() const
{
return controller_;
}
QList<QModelIndex> indexes() const
{
QList<QModelIndex> result;
std::copy(indexes_.begin(), indexes_.end(), std::back_inserter(result));
return result;
}
GpgME::Key key() const
{
return keys_.empty() ? model() && !indexes_.empty() ? model()->key(indexes_.front()) : GpgME::Key::null : keys_.front();
return keys_.empty() ? GpgME::Key{} : keys_.front();
}
std::vector<GpgME::Key> keys() const
{
return keys_.empty() ? model() ? model()->keys(indexes()) : std::vector<GpgME::Key>() : keys_;
return keys_;
}
void finished()
......@@ -127,7 +117,6 @@ private:
bool autoDelete : 1;
bool warnWhenRunningAtShutdown : 1;
std::vector<GpgME::Key> keys_;
QList<QPersistentModelIndex> indexes_;
QPointer<QAbstractItemView> view_;
QPointer<QWidget> parentWidget_;
WId parentWId_ = 0;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment