started refactoring of the VCard UI

This commit is contained in:
Blue 2023-02-01 18:56:00 +03:00
parent bb304ce774
commit 4af16b75bf
Signed by untrusted user: blue
GPG key ID: 9B203B252A63EE38
32 changed files with 1250 additions and 166 deletions

View file

@ -0,0 +1,4 @@
target_sources(squawk PRIVATE
keys.cpp
keys.h
)

View file

@ -0,0 +1,138 @@
// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "keys.h"
const QHash<int, QByteArray> Models::Keys::roles = {
{Label, "label"},
{FingerPrint, "fingerPrint"},
{TrustLevel, "trustLevel"}
};
Models::Keys::Keys(QObject* parent):
QAbstractListModel(parent),
keys(),
modified() {}
Models::Keys::~Keys() {
for (Shared::KeyInfo* key : keys) {
delete key;
}
for (std::pair<const int, Shared::KeyInfo*>& pair: modified) {
delete pair.second;
}
}
std::deque<Shared::KeyInfo> Models::Keys::modifiedKeys() const {
std::deque<Shared::KeyInfo> response(modified.size());
int i = 0;
for (const std::pair<const int, Shared::KeyInfo*>& pair: modified) {
response[i] = *(pair.second);
++i;
}
return response;
}
void Models::Keys::addKey(const Shared::KeyInfo& info) {
beginInsertRows(QModelIndex(), keys.size(), keys.size());
keys.push_back(new Shared::KeyInfo(info));
endInsertRows();
}
QVariant Models::Keys::data(const QModelIndex& index, int role) const {
int i = index.row();
const Shared::KeyInfo* info;
bool dirty;
std::map<int, Shared::KeyInfo*>::const_iterator itr = modified.find(i);
if (itr != modified.end()) {
info = itr->second;
dirty = true;
} else {
dirty = false;
info = keys[i];
}
QVariant answer;
switch (role) {
case Qt::DisplayRole:
case Label:
answer = info->label;
break;
case FingerPrint:
answer = info->fingerPrint;
break;
case TrustLevel:
answer = static_cast<uint8_t>(info->trustLevel);
break;
case LastInteraction:
answer = info->lastInteraction;
break;
case Dirty:
answer = dirty;
break;
}
return answer;
}
int Models::Keys::rowCount(const QModelIndex& parent) const {
return keys.size();
}
QHash<int, QByteArray> Models::Keys::roleNames() const {return roles;}
QModelIndex Models::Keys::index(int row, int column, const QModelIndex& parent) const {
if (!hasIndex(row, column, parent)) {
return QModelIndex();
}
return createIndex(row, column, keys[row]);
}
void Models::Keys::revertKey(int row) {
std::map<int, Shared::KeyInfo*>::const_iterator itr = modified.find(row);
if (itr != modified.end()) {
modified.erase(itr);
QModelIndex index = createIndex(row, 0, keys[row]);
dataChanged(index, index);
}
}
void Models::Keys::setTrustLevel(int row, Shared::TrustLevel level) {
std::map<int, Shared::KeyInfo*>::const_iterator itr = modified.find(row);
Shared::KeyInfo* info;
if (itr == modified.end()) {
if (row < rowCount()) {
info = new Shared::KeyInfo(*(keys[row]));
modified.insert(std::make_pair(row, info));
} else {
return;
}
} else {
info = itr->second;
}
info->trustLevel = level;
QModelIndex index = createIndex(row, 0, info);
dataChanged(index, index, {Keys::Dirty});
}

View file

@ -0,0 +1,67 @@
// Squawk messenger.
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef MODELS_KEYS_H
#define MODELS_KEYS_H
#include <QAbstractListModel>
#include <shared/keyinfo.h>
namespace Models {
/**
* @todo write docs
*/
class Keys : public QAbstractListModel
{
public:
Keys(QObject *parent = nullptr);
~Keys();
void addKey(const Shared::KeyInfo& info);
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override;
QModelIndex index(int row, int column, const QModelIndex & parent) const override;
std::deque<Shared::KeyInfo> modifiedKeys() const;
enum Roles {
Label = Qt::UserRole + 1,
FingerPrint,
TrustLevel,
LastInteraction,
Dirty
};
public slots:
void revertKey(int index);
void setTrustLevel(int index, Shared::TrustLevel level);
private:
std::deque<Shared::KeyInfo*> keys;
std::map<int, Shared::KeyInfo*> modified;
private:
static const QHash<int, QByteArray> roles;
};
}
#endif // MODELS_KEYS_H