Apply clang format on all GUI files
This commit is contained in:
parent
836e6bbcb2
commit
575d0cabdb
|
@ -432,16 +432,16 @@ bool MainWindow::openFile(const QString &path, bool readOnly)
|
||||||
}
|
}
|
||||||
|
|
||||||
// show error message
|
// show error message
|
||||||
const QString errmsg
|
const QString errmsg = tr("An IO error occured when opening the specified file \"%1\".\n\n(%2)").arg(path, QString::fromLocal8Bit(ioError));
|
||||||
= tr("An IO error occured when opening the specified file \"%1\".\n\n(%2)").arg(path, QString::fromLocal8Bit(ioError));
|
|
||||||
m_ui->statusBar->showMessage(errmsg, 5000);
|
m_ui->statusBar->showMessage(errmsg, 5000);
|
||||||
QMessageBox::critical(this, QApplication::applicationName(), errmsg);
|
QMessageBox::critical(this, QApplication::applicationName(), errmsg);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// warn before loading a very big file
|
// warn before loading a very big file
|
||||||
if (m_file.size() > 10485760 && QMessageBox::warning(this, QApplication::applicationName(),
|
if (m_file.size() > 10485760
|
||||||
tr("The file you want to load seems to be very big. Do you really want to open it?"), QMessageBox::Yes, QMessageBox::No)
|
&& QMessageBox::warning(this, QApplication::applicationName(),
|
||||||
|
tr("The file you want to load seems to be very big. Do you really want to open it?"), QMessageBox::Yes, QMessageBox::No)
|
||||||
== QMessageBox::No) {
|
== QMessageBox::No) {
|
||||||
m_file.clear();
|
m_file.clear();
|
||||||
return false;
|
return false;
|
||||||
|
@ -942,8 +942,8 @@ void MainWindow::addEntry(EntryType type)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
bool result;
|
bool result;
|
||||||
const QString text = QInputDialog::getText(this, type == EntryType::Account ? tr("Add account") : tr("Add category"),
|
const QString text = QInputDialog::getText(this, type == EntryType::Account ? tr("Add account") : tr("Add category"), tr("Enter the entry name"),
|
||||||
tr("Enter the entry name"), QLineEdit::Normal, tr("new entry"), &result);
|
QLineEdit::Normal, tr("new entry"), &result);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,8 @@
|
||||||
|
|
||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace QtGui {
|
namespace QtGui {
|
||||||
|
|
||||||
|
|
|
@ -13,19 +13,20 @@ namespace QtGui {
|
||||||
/*!
|
/*!
|
||||||
* \brief Constructs a new filter entry model.
|
* \brief Constructs a new filter entry model.
|
||||||
*/
|
*/
|
||||||
EntryFilterModel::EntryFilterModel(QObject *parent) :
|
EntryFilterModel::EntryFilterModel(QObject *parent)
|
||||||
QSortFilterProxyModel(parent)
|
: QSortFilterProxyModel(parent)
|
||||||
{}
|
{
|
||||||
|
}
|
||||||
|
|
||||||
bool EntryFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
|
bool EntryFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
|
||||||
{
|
{
|
||||||
// just use default implementation
|
// just use default implementation
|
||||||
if(QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent)) {
|
if (QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// also accept rows where the direct parent is accepted
|
// also accept rows where the direct parent is accepted
|
||||||
if(sourceParent.isValid() && QSortFilterProxyModel::filterAcceptsRow(sourceParent.row(), sourceParent.parent())) {
|
if (sourceParent.isValid() && QSortFilterProxyModel::filterAcceptsRow(sourceParent.row(), sourceParent.parent())) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,12 +36,12 @@ bool EntryFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &source
|
||||||
|
|
||||||
bool EntryFilterModel::hasAcceptedChildren(const QModelIndex &index) const
|
bool EntryFilterModel::hasAcceptedChildren(const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
for(int i = 0, rowCount = sourceModel()->rowCount(index); i < rowCount; ++i) {
|
for (int i = 0, rowCount = sourceModel()->rowCount(index); i < rowCount; ++i) {
|
||||||
if(filterAcceptsRow(i, index)) {
|
if (filterAcceptsRow(i, index)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace QtGui
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
#include "./entrymodel.h"
|
#include "./entrymodel.h"
|
||||||
|
|
||||||
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
||||||
# include "./gui/undocommands.h"
|
#include "./gui/undocommands.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <passwordfile/io/entry.h>
|
#include <passwordfile/io/entry.h>
|
||||||
|
|
||||||
#include <c++utilities/io/catchiofailure.h>
|
#include <c++utilities/io/catchiofailure.h>
|
||||||
|
|
||||||
#include <QIcon>
|
|
||||||
#include <QBuffer>
|
#include <QBuffer>
|
||||||
#include <QMimeData>
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
#include <QIcon>
|
||||||
|
#include <QMimeData>
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
@ -32,11 +32,12 @@ namespace QtGui {
|
||||||
/*!
|
/*!
|
||||||
* \brief Constructs a new entry model.
|
* \brief Constructs a new entry model.
|
||||||
*/
|
*/
|
||||||
EntryModel::EntryModel(QObject *parent) :
|
EntryModel::EntryModel(QObject *parent)
|
||||||
QAbstractItemModel(parent),
|
: QAbstractItemModel(parent)
|
||||||
m_rootEntry(nullptr),
|
, m_rootEntry(nullptr)
|
||||||
m_insertType(EntryType::Node)
|
, m_insertType(EntryType::Node)
|
||||||
{}
|
{
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
||||||
/*!
|
/*!
|
||||||
|
@ -44,12 +45,13 @@ EntryModel::EntryModel(QObject *parent) :
|
||||||
*
|
*
|
||||||
* This constructor is only available when PASSWORD_MANAGER_GUI_QTWIDGETS is defined.
|
* This constructor is only available when PASSWORD_MANAGER_GUI_QTWIDGETS is defined.
|
||||||
*/
|
*/
|
||||||
EntryModel::EntryModel(QUndoStack *undoStack, QObject *parent) :
|
EntryModel::EntryModel(QUndoStack *undoStack, QObject *parent)
|
||||||
QAbstractItemModel(parent),
|
: QAbstractItemModel(parent)
|
||||||
StackSupport(undoStack),
|
, StackSupport(undoStack)
|
||||||
m_rootEntry(nullptr),
|
, m_rootEntry(nullptr)
|
||||||
m_insertType(EntryType::Node)
|
, m_insertType(EntryType::Node)
|
||||||
{}
|
{
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -75,18 +77,18 @@ Entry *EntryModel::entry(const QModelIndex &index)
|
||||||
QList<Entry *> EntryModel::takeEntries(int row, int count, const QModelIndex &parent)
|
QList<Entry *> EntryModel::takeEntries(int row, int count, const QModelIndex &parent)
|
||||||
{
|
{
|
||||||
Entry *const parentEntry = entry(parent);
|
Entry *const parentEntry = entry(parent);
|
||||||
if(!parentEntry || parentEntry->type() != EntryType::Node) {
|
if (!parentEntry || parentEntry->type() != EntryType::Node) {
|
||||||
return QList<Entry *>();
|
return QList<Entry *>();
|
||||||
}
|
}
|
||||||
QList<Entry *> res;
|
QList<Entry *> res;
|
||||||
NodeEntry *const parentNodeEntry = static_cast<NodeEntry *>(parentEntry);
|
NodeEntry *const parentNodeEntry = static_cast<NodeEntry *>(parentEntry);
|
||||||
int lastIndex = row + count - 1;
|
int lastIndex = row + count - 1;
|
||||||
const vector<Entry *> &children = parentNodeEntry->children();
|
const vector<Entry *> &children = parentNodeEntry->children();
|
||||||
if(lastIndex < 0 || static_cast<size_t>(lastIndex) >= children.size()) {
|
if (lastIndex < 0 || static_cast<size_t>(lastIndex) >= children.size()) {
|
||||||
lastIndex = children.size() - 1;
|
lastIndex = children.size() - 1;
|
||||||
}
|
}
|
||||||
beginRemoveRows(parent, row, lastIndex);
|
beginRemoveRows(parent, row, lastIndex);
|
||||||
for(int index = lastIndex; index >= row; --index) {
|
for (int index = lastIndex; index >= row; --index) {
|
||||||
Entry *const child = children[index];
|
Entry *const child = children[index];
|
||||||
child->setParent(nullptr);
|
child->setParent(nullptr);
|
||||||
res << child;
|
res << child;
|
||||||
|
@ -104,20 +106,20 @@ QList<Entry *> EntryModel::takeEntries(int row, int count, const QModelIndex &pa
|
||||||
*/
|
*/
|
||||||
bool EntryModel::insertEntries(int row, const QModelIndex &parent, const QList<Entry *> &entries)
|
bool EntryModel::insertEntries(int row, const QModelIndex &parent, const QList<Entry *> &entries)
|
||||||
{
|
{
|
||||||
if(entries.isEmpty()) {
|
if (entries.isEmpty()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Entry *const parentEntry = entry(parent);
|
Entry *const parentEntry = entry(parent);
|
||||||
if(!parentEntry || parentEntry->type() != EntryType::Node) {
|
if (!parentEntry || parentEntry->type() != EntryType::Node) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
NodeEntry *const parentNodeEntry = static_cast<NodeEntry *>(parentEntry);
|
NodeEntry *const parentNodeEntry = static_cast<NodeEntry *>(parentEntry);
|
||||||
const vector<Entry *> &children = parentNodeEntry->children();
|
const vector<Entry *> &children = parentNodeEntry->children();
|
||||||
if(row < 0 || static_cast<size_t>(row) > children.size()) {
|
if (row < 0 || static_cast<size_t>(row) > children.size()) {
|
||||||
row = children.size();
|
row = children.size();
|
||||||
}
|
}
|
||||||
beginInsertRows(parent, row, row + entries.size() - 1);
|
beginInsertRows(parent, row, row + entries.size() - 1);
|
||||||
for(Entry *const entry : entries) {
|
for (Entry *const entry : entries) {
|
||||||
entry->setParent(parentNodeEntry, row);
|
entry->setParent(parentNodeEntry, row);
|
||||||
++row;
|
++row;
|
||||||
}
|
}
|
||||||
|
@ -127,25 +129,25 @@ bool EntryModel::insertEntries(int row, const QModelIndex &parent, const QList<E
|
||||||
|
|
||||||
QModelIndex EntryModel::index(int row, int column, const QModelIndex &parent) const
|
QModelIndex EntryModel::index(int row, int column, const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
if(!parent.isValid()) {
|
if (!parent.isValid()) {
|
||||||
if(m_rootEntry && row == 0) {
|
if (m_rootEntry && row == 0) {
|
||||||
return createIndex(row, column, m_rootEntry);
|
return createIndex(row, column, m_rootEntry);
|
||||||
}
|
}
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
}
|
}
|
||||||
const auto *const parentEntry = static_cast<const Entry *>(parent.internalPointer());
|
const auto *const parentEntry = static_cast<const Entry *>(parent.internalPointer());
|
||||||
if(!parentEntry) {
|
if (!parentEntry) {
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
}
|
}
|
||||||
switch(parentEntry->type()) {
|
switch (parentEntry->type()) {
|
||||||
case EntryType::Node: {
|
case EntryType::Node: {
|
||||||
const std::vector<Entry *> &children = static_cast<const NodeEntry *>(parentEntry)->children();
|
const std::vector<Entry *> &children = static_cast<const NodeEntry *>(parentEntry)->children();
|
||||||
if(row >= 0 && static_cast<size_t>(row) < children.size()) {
|
if (row >= 0 && static_cast<size_t>(row) < children.size()) {
|
||||||
return createIndex(row, column, children[static_cast<size_t>(row)]);
|
return createIndex(row, column, children[static_cast<size_t>(row)]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
} case EntryType::Account:
|
}
|
||||||
;
|
case EntryType::Account:;
|
||||||
}
|
}
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
}
|
}
|
||||||
|
@ -156,7 +158,7 @@ QModelIndex EntryModel::index(int row, int column, const QModelIndex &parent) co
|
||||||
*/
|
*/
|
||||||
QModelIndex EntryModel::index(Entry *entry) const
|
QModelIndex EntryModel::index(Entry *entry) const
|
||||||
{
|
{
|
||||||
if(entry->parent()) {
|
if (entry->parent()) {
|
||||||
return createIndex(entry->index(), 0, entry);
|
return createIndex(entry->index(), 0, entry);
|
||||||
} else {
|
} else {
|
||||||
return createIndex(0, 0, m_rootEntry);
|
return createIndex(0, 0, m_rootEntry);
|
||||||
|
@ -165,15 +167,15 @@ QModelIndex EntryModel::index(Entry *entry) const
|
||||||
|
|
||||||
QModelIndex EntryModel::parent(const QModelIndex &child) const
|
QModelIndex EntryModel::parent(const QModelIndex &child) const
|
||||||
{
|
{
|
||||||
if(!child.isValid()) {
|
if (!child.isValid()) {
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
}
|
}
|
||||||
const auto *const entry = static_cast<Entry *>(child.internalPointer());
|
const auto *const entry = static_cast<Entry *>(child.internalPointer());
|
||||||
if(!entry) {
|
if (!entry) {
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
}
|
}
|
||||||
NodeEntry *const parent = entry->parent();
|
NodeEntry *const parent = entry->parent();
|
||||||
if(parent && (child.row() >= 0 && static_cast<size_t>(child.row()) < parent->children().size())) {
|
if (parent && (child.row() >= 0 && static_cast<size_t>(child.row()) < parent->children().size())) {
|
||||||
return createIndex(parent->index() > 0 ? parent->index() : 0, 0, parent);
|
return createIndex(parent->index() > 0 ? parent->index() : 0, 0, parent);
|
||||||
}
|
}
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
|
@ -181,7 +183,7 @@ QModelIndex EntryModel::parent(const QModelIndex &child) const
|
||||||
|
|
||||||
bool EntryModel::hasChildren(const QModelIndex &parent) const
|
bool EntryModel::hasChildren(const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
if(!parent.isValid()) {
|
if (!parent.isValid()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
const auto *const entry = static_cast<Entry *>(parent.internalPointer());
|
const auto *const entry = static_cast<Entry *>(parent.internalPointer());
|
||||||
|
@ -194,7 +196,7 @@ bool EntryModel::hasChildren(const QModelIndex &parent) const
|
||||||
*/
|
*/
|
||||||
bool EntryModel::isNode(const QModelIndex &parent) const
|
bool EntryModel::isNode(const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
if(!parent.isValid()) {
|
if (!parent.isValid()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const auto *const entry = static_cast<const Entry *>(parent.internalPointer());
|
const auto *const entry = static_cast<const Entry *>(parent.internalPointer());
|
||||||
|
@ -203,46 +205,43 @@ bool EntryModel::isNode(const QModelIndex &parent) const
|
||||||
|
|
||||||
QVariant EntryModel::data(const QModelIndex &index, int role) const
|
QVariant EntryModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
if(!index.isValid()) {
|
if (!index.isValid()) {
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
const auto *const entry = static_cast<const Entry *>(index.internalPointer());
|
const auto *const entry = static_cast<const Entry *>(index.internalPointer());
|
||||||
if(!entry) {
|
if (!entry) {
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
switch(role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
case Qt::EditRole:
|
case Qt::EditRole:
|
||||||
switch(index.column()) {
|
switch (index.column()) {
|
||||||
case 0:
|
case 0:
|
||||||
return QString::fromStdString(entry->label());
|
return QString::fromStdString(entry->label());
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Qt::DecorationRole:
|
case Qt::DecorationRole:
|
||||||
if(index.column() == 0 && entry->type() == EntryType::Node) {
|
if (index.column() == 0 && entry->type() == EntryType::Node) {
|
||||||
static const QVariant folderIcon = QIcon::fromTheme(QStringLiteral("folder"));
|
static const QVariant folderIcon = QIcon::fromTheme(QStringLiteral("folder"));
|
||||||
return folderIcon;
|
return folderIcon;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SerializedRole: {
|
case SerializedRole: {
|
||||||
stringstream ss(stringstream::in | stringstream::out | stringstream::binary);
|
stringstream ss(stringstream::in | stringstream::out | stringstream::binary);
|
||||||
ss.exceptions(std::stringstream::failbit | std::stringstream::badbit);
|
ss.exceptions(std::stringstream::failbit | std::stringstream::badbit);
|
||||||
try {
|
try {
|
||||||
entry->make(ss);
|
entry->make(ss);
|
||||||
const auto str(ss.str());
|
const auto str(ss.str());
|
||||||
return QByteArray(str.data(), str.size());
|
return QByteArray(str.data(), str.size());
|
||||||
} catch(...) {
|
} catch (...) {
|
||||||
IoUtilities::catchIoFailure();
|
IoUtilities::catchIoFailure();
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
} break;
|
||||||
case DefaultExpandedRole:
|
case DefaultExpandedRole:
|
||||||
return entry->type() == EntryType::Node && static_cast<const NodeEntry *>(entry)->isExpandedByDefault();
|
return entry->type() == EntryType::Node && static_cast<const NodeEntry *>(entry)->isExpandedByDefault();
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
@ -250,67 +249,65 @@ QVariant EntryModel::data(const QModelIndex &index, int role) const
|
||||||
QMap<int, QVariant> EntryModel::itemData(const QModelIndex &index) const
|
QMap<int, QVariant> EntryModel::itemData(const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
return QMap<int, QVariant>{
|
return QMap<int, QVariant>{
|
||||||
{Qt::DisplayRole, data(index, Qt::DisplayRole)},
|
{ Qt::DisplayRole, data(index, Qt::DisplayRole) },
|
||||||
{SerializedRole, data(index, SerializedRole)},
|
{ SerializedRole, data(index, SerializedRole) },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||||
{
|
{
|
||||||
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
||||||
if(undoStack()) {
|
if (undoStack()) {
|
||||||
return push(new EntryModelSetValueCommand(this, index, value, role));
|
return push(new EntryModelSetValueCommand(this, index, value, role));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if(!index.isValid()) {
|
if (!index.isValid()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto *const entry = static_cast<Entry *>(index.internalPointer());
|
auto *const entry = static_cast<Entry *>(index.internalPointer());
|
||||||
if(!entry) {
|
if (!entry) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
switch(role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
case Qt::EditRole:
|
case Qt::EditRole:
|
||||||
switch(index.column()) {
|
switch (index.column()) {
|
||||||
case 0:
|
case 0:
|
||||||
entry->setLabel(value.toString().toStdString());
|
entry->setLabel(value.toString().toStdString());
|
||||||
emit dataChanged(index, index, QVector<int>() << role);
|
emit dataChanged(index, index, QVector<int>() << role);
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case SerializedRole: {
|
case SerializedRole: {
|
||||||
NodeEntry *parent = entry->parent();
|
NodeEntry *parent = entry->parent();
|
||||||
QModelIndex parentIndex = index.parent();
|
QModelIndex parentIndex = index.parent();
|
||||||
if(!parent || !parentIndex.isValid()) {
|
if (!parent || !parentIndex.isValid()) {
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
stringstream ss(stringstream::in | stringstream::out | stringstream::binary);
|
|
||||||
ss.exceptions(std::stringstream::failbit | std::stringstream::badbit);
|
|
||||||
QByteArray array = value.toByteArray();
|
|
||||||
if(array.isEmpty()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
ss.write(array.data(), array.size());
|
|
||||||
Entry *newEntry = Entry::parse(ss);
|
|
||||||
int row = entry->index();
|
|
||||||
beginRemoveRows(parentIndex, row, row);
|
|
||||||
delete entry;
|
|
||||||
endRemoveRows();
|
|
||||||
beginInsertRows(parentIndex, row, row);
|
|
||||||
newEntry->setParent(parent, row);
|
|
||||||
endInsertRows();
|
|
||||||
return true;
|
|
||||||
} catch(...) {
|
|
||||||
IoUtilities::catchIoFailure();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
stringstream ss(stringstream::in | stringstream::out | stringstream::binary);
|
||||||
|
ss.exceptions(std::stringstream::failbit | std::stringstream::badbit);
|
||||||
|
QByteArray array = value.toByteArray();
|
||||||
|
if (array.isEmpty()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
ss.write(array.data(), array.size());
|
||||||
|
Entry *newEntry = Entry::parse(ss);
|
||||||
|
int row = entry->index();
|
||||||
|
beginRemoveRows(parentIndex, row, row);
|
||||||
|
delete entry;
|
||||||
|
endRemoveRows();
|
||||||
|
beginInsertRows(parentIndex, row, row);
|
||||||
|
newEntry->setParent(parent, row);
|
||||||
|
endInsertRows();
|
||||||
|
return true;
|
||||||
|
} catch (...) {
|
||||||
|
IoUtilities::catchIoFailure();
|
||||||
|
}
|
||||||
|
} break;
|
||||||
case DefaultExpandedRole:
|
case DefaultExpandedRole:
|
||||||
switch(entry->type()) {
|
switch (entry->type()) {
|
||||||
case EntryType::Account:
|
case EntryType::Account:
|
||||||
return false;
|
return false;
|
||||||
case EntryType::Node:
|
case EntryType::Node:
|
||||||
|
@ -319,15 +316,14 @@ bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int ro
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool EntryModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
|
bool EntryModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
|
||||||
{
|
{
|
||||||
for(QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) {
|
for (QMap<int, QVariant>::ConstIterator it = roles.constBegin(); it != roles.constEnd(); ++it) {
|
||||||
setData(index, it.value(), it.key());
|
setData(index, it.value(), it.key());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -335,46 +331,41 @@ bool EntryModel::setItemData(const QModelIndex &index, const QMap<int, QVariant>
|
||||||
|
|
||||||
Qt::ItemFlags EntryModel::flags(const QModelIndex &index) const
|
Qt::ItemFlags EntryModel::flags(const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
return isNode(index)
|
return isNode(index) ? QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled
|
||||||
? QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled
|
: QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
|
||||||
: QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant EntryModel::headerData(int section, Qt::Orientation orientation, int role) const
|
QVariant EntryModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||||
{
|
{
|
||||||
switch(orientation) {
|
switch (orientation) {
|
||||||
case Qt::Horizontal:
|
case Qt::Horizontal:
|
||||||
switch(role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
switch(section) {
|
switch (section) {
|
||||||
case 0:
|
case 0:
|
||||||
return tr("Name");
|
return tr("Name");
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
int EntryModel::rowCount(const QModelIndex &parent) const
|
int EntryModel::rowCount(const QModelIndex &parent) const
|
||||||
{
|
{
|
||||||
if(parent.isValid()) {
|
if (parent.isValid()) {
|
||||||
if(Entry *parentEntry = static_cast<Entry *>(parent.internalPointer())) {
|
if (Entry *parentEntry = static_cast<Entry *>(parent.internalPointer())) {
|
||||||
switch(parentEntry->type()) {
|
switch (parentEntry->type()) {
|
||||||
case EntryType::Node:
|
case EntryType::Node:
|
||||||
return static_cast<NodeEntry *>(parentEntry)->children().size();
|
return static_cast<NodeEntry *>(parentEntry)->children().size();
|
||||||
case EntryType::Account:
|
case EntryType::Account:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if(m_rootEntry) {
|
} else if (m_rootEntry) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -388,21 +379,21 @@ int EntryModel::columnCount(const QModelIndex &) const
|
||||||
bool EntryModel::insertRows(int row, int count, const QModelIndex &parent)
|
bool EntryModel::insertRows(int row, int count, const QModelIndex &parent)
|
||||||
{
|
{
|
||||||
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
||||||
if(undoStack()) {
|
if (undoStack()) {
|
||||||
return push(new EntryModelInsertRowsCommand(this, row, count, parent));
|
return push(new EntryModelInsertRowsCommand(this, row, count, parent));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if(!parent.isValid()) {
|
if (!parent.isValid()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto *const parentEntry = static_cast<Entry *>(parent.internalPointer());
|
auto *const parentEntry = static_cast<Entry *>(parent.internalPointer());
|
||||||
if(!parentEntry || parentEntry->type() != EntryType::Node) {
|
if (!parentEntry || parentEntry->type() != EntryType::Node) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
beginInsertRows(parent, row, row + count - 1);
|
beginInsertRows(parent, row, row + count - 1);
|
||||||
for(int end = row + count; row < end; ++row) {
|
for (int end = row + count; row < end; ++row) {
|
||||||
Entry *newEntry;
|
Entry *newEntry;
|
||||||
switch(m_insertType) {
|
switch (m_insertType) {
|
||||||
case EntryType::Node:
|
case EntryType::Node:
|
||||||
newEntry = new NodeEntry;
|
newEntry = new NodeEntry;
|
||||||
break;
|
break;
|
||||||
|
@ -421,15 +412,15 @@ bool EntryModel::insertRows(int row, int count, const QModelIndex &parent)
|
||||||
bool EntryModel::removeRows(int row, int count, const QModelIndex &parent)
|
bool EntryModel::removeRows(int row, int count, const QModelIndex &parent)
|
||||||
{
|
{
|
||||||
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
||||||
if(undoStack()) {
|
if (undoStack()) {
|
||||||
return push(new EntryModelRemoveRowsCommand(this, row, count, parent));
|
return push(new EntryModelRemoveRowsCommand(this, row, count, parent));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if(!parent.isValid() || count <= 0) {
|
if (!parent.isValid() || count <= 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto *const parentEntry = static_cast<Entry *>(parent.internalPointer());
|
auto *const parentEntry = static_cast<Entry *>(parent.internalPointer());
|
||||||
if(!parentEntry || parentEntry->type() != EntryType::Node) {
|
if (!parentEntry || parentEntry->type() != EntryType::Node) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
beginRemoveRows(parent, row, row + count - 1);
|
beginRemoveRows(parent, row, row + count - 1);
|
||||||
|
@ -441,40 +432,39 @@ bool EntryModel::removeRows(int row, int count, const QModelIndex &parent)
|
||||||
bool EntryModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
|
bool EntryModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
|
||||||
{
|
{
|
||||||
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
||||||
if(undoStack()) {
|
if (undoStack()) {
|
||||||
return push(new EntryModelMoveRowsCommand(this, sourceParent, sourceRow, count, destinationParent, destinationChild));
|
return push(new EntryModelMoveRowsCommand(this, sourceParent, sourceRow, count, destinationParent, destinationChild));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
// check validation of specified arguments
|
// check validation of specified arguments
|
||||||
if(!sourceParent.isValid() || !destinationParent.isValid()
|
if (!sourceParent.isValid() || !destinationParent.isValid() || sourceRow < 0 || count <= 0
|
||||||
|| sourceRow < 0 || count <= 0
|
|| entry(sourceParent)->type() != EntryType::Node // source and destination parent entries
|
||||||
|| entry(sourceParent)->type() != EntryType::Node // source and destination parent entries
|
|| entry(destinationParent)->type() != EntryType::Node) { // need to be node entries
|
||||||
|| entry(destinationParent)->type() != EntryType::Node) { // need to be node entries
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// determine the source parent entry and dest parent entry as node entries
|
// determine the source parent entry and dest parent entry as node entries
|
||||||
auto *const srcParentEntry = static_cast<NodeEntry *>(sourceParent.internalPointer());
|
auto *const srcParentEntry = static_cast<NodeEntry *>(sourceParent.internalPointer());
|
||||||
auto *const destParentEntry = static_cast<NodeEntry *>(destinationParent.internalPointer());
|
auto *const destParentEntry = static_cast<NodeEntry *>(destinationParent.internalPointer());
|
||||||
// source rows must be within the valid range
|
// source rows must be within the valid range
|
||||||
if(static_cast<size_t>(sourceRow + count) > srcParentEntry->children().size()
|
if (static_cast<size_t>(sourceRow + count) > srcParentEntry->children().size()
|
||||||
// if source and destination parent are the same the destination child mustn't be in the source range
|
// if source and destination parent are the same the destination child mustn't be in the source range
|
||||||
|| !(srcParentEntry != destParentEntry || (destinationChild < sourceRow || (sourceRow + count) < destinationChild))) {
|
|| !(srcParentEntry != destParentEntry || (destinationChild < sourceRow || (sourceRow + count) < destinationChild))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// do not move a row to one of its own children! -> check before
|
// do not move a row to one of its own children! -> check before
|
||||||
for(int index = 0; index < count; ++index) {
|
for (int index = 0; index < count; ++index) {
|
||||||
Entry *toMove = srcParentEntry->children()[static_cast<size_t>(sourceRow + index)];
|
Entry *toMove = srcParentEntry->children()[static_cast<size_t>(sourceRow + index)];
|
||||||
if(toMove->type() == EntryType::Node) {
|
if (toMove->type() == EntryType::Node) {
|
||||||
if(destParentEntry->isIndirectChildOf(static_cast<NodeEntry *>(toMove))) {
|
if (destParentEntry->isIndirectChildOf(static_cast<NodeEntry *>(toMove))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// actually perform the move operation
|
// actually perform the move operation
|
||||||
beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild);
|
beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild);
|
||||||
for(int index = 0; index < count; ++index) {
|
for (int index = 0; index < count; ++index) {
|
||||||
Entry *toMove = srcParentEntry->children()[static_cast<size_t>(sourceRow + index)];
|
Entry *toMove = srcParentEntry->children()[static_cast<size_t>(sourceRow + index)];
|
||||||
if(srcParentEntry == destParentEntry && sourceRow < destinationChild) {
|
if (srcParentEntry == destParentEntry && sourceRow < destinationChild) {
|
||||||
toMove->setParent(destParentEntry, destinationChild + index - 1);
|
toMove->setParent(destParentEntry, destinationChild + index - 1);
|
||||||
} else {
|
} else {
|
||||||
toMove->setParent(destParentEntry, destinationChild + index);
|
toMove->setParent(destParentEntry, destinationChild + index);
|
||||||
|
@ -491,25 +481,25 @@ QStringList EntryModel::mimeTypes() const
|
||||||
|
|
||||||
QMimeData *EntryModel::mimeData(const QModelIndexList &indexes) const
|
QMimeData *EntryModel::mimeData(const QModelIndexList &indexes) const
|
||||||
{
|
{
|
||||||
if(indexes.count() <= 0) {
|
if (indexes.count() <= 0) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
QStringList types = mimeTypes();
|
QStringList types = mimeTypes();
|
||||||
if(types.isEmpty()) {
|
if (types.isEmpty()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
QMimeData *data = new QMimeData();
|
QMimeData *data = new QMimeData();
|
||||||
QStringList plainTextParts;
|
QStringList plainTextParts;
|
||||||
QByteArray encoded;
|
QByteArray encoded;
|
||||||
QDataStream dataStream(&encoded, QIODevice::WriteOnly);
|
QDataStream dataStream(&encoded, QIODevice::WriteOnly);
|
||||||
for(const QModelIndex &index : indexes) {
|
for (const QModelIndex &index : indexes) {
|
||||||
if(!index.isValid()) {
|
if (!index.isValid()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const auto *const entry = static_cast<const Entry *>(index.internalPointer());
|
const auto *const entry = static_cast<const Entry *>(index.internalPointer());
|
||||||
const auto path(entry->path());
|
const auto path(entry->path());
|
||||||
dataStream << static_cast<quint32>(path.size());
|
dataStream << static_cast<quint32>(path.size());
|
||||||
for(const string &part : path) {
|
for (const string &part : path) {
|
||||||
dataStream << QString::fromStdString(part);
|
dataStream << QString::fromStdString(part);
|
||||||
}
|
}
|
||||||
plainTextParts << QString::fromStdString(entry->label());
|
plainTextParts << QString::fromStdString(entry->label());
|
||||||
|
@ -521,42 +511,42 @@ QMimeData *EntryModel::mimeData(const QModelIndexList &indexes) const
|
||||||
|
|
||||||
bool EntryModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
bool EntryModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
||||||
{
|
{
|
||||||
if(!m_rootEntry || !data || action != Qt::MoveAction) {
|
if (!m_rootEntry || !data || action != Qt::MoveAction) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
QStringList types = mimeTypes();
|
QStringList types = mimeTypes();
|
||||||
if(types.isEmpty()) {
|
if (types.isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
QString format = types.at(0);
|
QString format = types.at(0);
|
||||||
if(!data->hasFormat(format)) {
|
if (!data->hasFormat(format)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(row > rowCount(parent) || row < 0) {
|
if (row > rowCount(parent) || row < 0) {
|
||||||
row = rowCount(parent);
|
row = rowCount(parent);
|
||||||
}
|
}
|
||||||
if(column > columnCount(parent) || column < 0) {
|
if (column > columnCount(parent) || column < 0) {
|
||||||
column = 0;
|
column = 0;
|
||||||
}
|
}
|
||||||
// decode and insert
|
// decode and insert
|
||||||
QByteArray encoded(data->data(format));
|
QByteArray encoded(data->data(format));
|
||||||
QDataStream stream(&encoded, QIODevice::ReadOnly);
|
QDataStream stream(&encoded, QIODevice::ReadOnly);
|
||||||
int moved = 0;
|
int moved = 0;
|
||||||
while(!stream.atEnd()) {
|
while (!stream.atEnd()) {
|
||||||
quint32 size;
|
quint32 size;
|
||||||
stream >> size;
|
stream >> size;
|
||||||
list<string> path;
|
list<string> path;
|
||||||
for(quint32 i = 0; i < size; ++i) {
|
for (quint32 i = 0; i < size; ++i) {
|
||||||
QString part;
|
QString part;
|
||||||
stream >> part;
|
stream >> part;
|
||||||
path.push_back(part.toStdString());
|
path.push_back(part.toStdString());
|
||||||
}
|
}
|
||||||
auto *const entry = m_rootEntry->entryByPath(path, true);
|
auto *const entry = m_rootEntry->entryByPath(path, true);
|
||||||
if(!entry) {
|
if (!entry) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
auto *const srcParentEntry = entry->parent();
|
auto *const srcParentEntry = entry->parent();
|
||||||
if(srcParentEntry && moveRows(index(srcParentEntry), entry->index(), 1, parent, row)) {
|
if (srcParentEntry && moveRows(index(srcParentEntry), entry->index(), 1, parent, row)) {
|
||||||
++moved;
|
++moved;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -568,4 +558,4 @@ Qt::DropActions EntryModel::supportedDropActions() const
|
||||||
return Qt::MoveAction;
|
return Qt::MoveAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace QtGui
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include "./fieldmodel.h"
|
#include "./fieldmodel.h"
|
||||||
|
|
||||||
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
||||||
# include "./gui/undocommands.h"
|
#include "./gui/undocommands.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <passwordfile/io/field.h>
|
#include <passwordfile/io/field.h>
|
||||||
|
@ -26,12 +26,13 @@ namespace QtGui {
|
||||||
/*!
|
/*!
|
||||||
* \brief Constructs a new field model.
|
* \brief Constructs a new field model.
|
||||||
*/
|
*/
|
||||||
FieldModel::FieldModel(QObject *parent) :
|
FieldModel::FieldModel(QObject *parent)
|
||||||
QAbstractTableModel(parent),
|
: QAbstractTableModel(parent)
|
||||||
m_accountEntry(nullptr),
|
, m_accountEntry(nullptr)
|
||||||
m_fields(nullptr),
|
, m_fields(nullptr)
|
||||||
m_passwordVisibility(PasswordVisibility::OnlyWhenEditing)
|
, m_passwordVisibility(PasswordVisibility::OnlyWhenEditing)
|
||||||
{}
|
{
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
||||||
/*!
|
/*!
|
||||||
|
@ -39,12 +40,13 @@ FieldModel::FieldModel(QObject *parent) :
|
||||||
*
|
*
|
||||||
* This constructor is only available when PASSWORD_MANAGER_GUI_QTWIDGETS is defined.
|
* This constructor is only available when PASSWORD_MANAGER_GUI_QTWIDGETS is defined.
|
||||||
*/
|
*/
|
||||||
FieldModel::FieldModel(QUndoStack *undoStack, QObject *parent) :
|
FieldModel::FieldModel(QUndoStack *undoStack, QObject *parent)
|
||||||
QAbstractTableModel(parent),
|
: QAbstractTableModel(parent)
|
||||||
StackSupport(undoStack),
|
, StackSupport(undoStack)
|
||||||
m_accountEntry(nullptr),
|
, m_accountEntry(nullptr)
|
||||||
m_fields(nullptr)
|
, m_fields(nullptr)
|
||||||
{}
|
{
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -55,11 +57,11 @@ FieldModel::FieldModel(QUndoStack *undoStack, QObject *parent) :
|
||||||
*/
|
*/
|
||||||
void FieldModel::setAccountEntry(AccountEntry *entry)
|
void FieldModel::setAccountEntry(AccountEntry *entry)
|
||||||
{
|
{
|
||||||
if(entry == m_accountEntry) {
|
if (entry == m_accountEntry) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
if((m_accountEntry = entry)) {
|
if ((m_accountEntry = entry)) {
|
||||||
m_fields = &entry->fields();
|
m_fields = &entry->fields();
|
||||||
} else {
|
} else {
|
||||||
m_fields = nullptr;
|
m_fields = nullptr;
|
||||||
|
@ -69,48 +71,43 @@ void FieldModel::setAccountEntry(AccountEntry *entry)
|
||||||
|
|
||||||
QVariant FieldModel::data(const QModelIndex &index, int role) const
|
QVariant FieldModel::data(const QModelIndex &index, int role) const
|
||||||
{
|
{
|
||||||
if(!index.isValid() || !m_fields || index.row() < 0) {
|
if (!index.isValid() || !m_fields || index.row() < 0) {
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
// return data for existent field
|
// return data for existent field
|
||||||
if(static_cast<size_t>(index.row()) < m_fields->size()) {
|
if (static_cast<size_t>(index.row()) < m_fields->size()) {
|
||||||
switch(role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
case Qt::EditRole:
|
case Qt::EditRole:
|
||||||
switch(index.column()) {
|
switch (index.column()) {
|
||||||
case 0:
|
case 0:
|
||||||
return QString::fromStdString((*m_fields)[static_cast<size_t>(index.row())].name());
|
return QString::fromStdString((*m_fields)[static_cast<size_t>(index.row())].name());
|
||||||
case 1:
|
case 1:
|
||||||
return (m_passwordVisibility == PasswordVisibility::Always
|
return (m_passwordVisibility == PasswordVisibility::Always || role == Qt::EditRole
|
||||||
|| role == Qt::EditRole
|
|| (*m_fields)[static_cast<size_t>(index.row())].type() != FieldType::Password)
|
||||||
|| (*m_fields)[static_cast<size_t>(index.row())].type() != FieldType::Password)
|
? QString::fromStdString((*m_fields)[static_cast<size_t>(index.row())].value())
|
||||||
? QString::fromStdString((*m_fields)[static_cast<size_t>(index.row())].value())
|
: QString((*m_fields)[static_cast<size_t>(index.row())].value().size(), QChar(0x2022));
|
||||||
: QString((*m_fields)[static_cast<size_t>(index.row())].value().size(), QChar(0x2022));
|
default:;
|
||||||
default:
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case FieldTypeRole:
|
case FieldTypeRole:
|
||||||
return static_cast<int>((*m_fields)[static_cast<size_t>(index.row())].type());
|
return static_cast<int>((*m_fields)[static_cast<size_t>(index.row())].type());
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
// return data for empty field at the end which enables the user to append fields
|
// return data for empty field at the end which enables the user to append fields
|
||||||
} else if(static_cast<size_t>(index.row()) == m_fields->size()) {
|
} else if (static_cast<size_t>(index.row()) == m_fields->size()) {
|
||||||
switch(role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
case Qt::EditRole:
|
case Qt::EditRole:
|
||||||
switch(index.column()) {
|
switch (index.column()) {
|
||||||
case 0:
|
case 0:
|
||||||
return QString();
|
return QString();
|
||||||
case 1:
|
case 1:
|
||||||
return QString();
|
return QString();
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return QVariant();
|
return QVariant();
|
||||||
|
@ -120,7 +117,7 @@ QMap<int, QVariant> FieldModel::itemData(const QModelIndex &index) const
|
||||||
{
|
{
|
||||||
static const auto roleMap = [this, index] {
|
static const auto roleMap = [this, index] {
|
||||||
QMap<int, QVariant> roleMap;
|
QMap<int, QVariant> roleMap;
|
||||||
for(const auto role : initializer_list<int>{Qt::EditRole, FieldTypeRole}) {
|
for (const auto role : initializer_list<int>{ Qt::EditRole, FieldTypeRole }) {
|
||||||
const auto variantData(data(index, role));
|
const auto variantData(data(index, role));
|
||||||
if (variantData.isValid()) {
|
if (variantData.isValid()) {
|
||||||
roleMap.insert(role, variantData);
|
roleMap.insert(role, variantData);
|
||||||
|
@ -134,19 +131,19 @@ QMap<int, QVariant> FieldModel::itemData(const QModelIndex &index) const
|
||||||
bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||||
{
|
{
|
||||||
#if PASSWORD_MANAGER_GUI_QTWIDGETS
|
#if PASSWORD_MANAGER_GUI_QTWIDGETS
|
||||||
if(undoStack()) {
|
if (undoStack()) {
|
||||||
return push(new FieldModelSetValueCommand(this, index, value, role));
|
return push(new FieldModelSetValueCommand(this, index, value, role));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if(!index.isValid() || !m_fields || index.row() < 0) {
|
if (!index.isValid() || !m_fields || index.row() < 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
QVector<int> roles;
|
QVector<int> roles;
|
||||||
if(static_cast<size_t>(index.row()) < m_fields->size()) {
|
if (static_cast<size_t>(index.row()) < m_fields->size()) {
|
||||||
// set data for existing field
|
// set data for existing field
|
||||||
switch(role) {
|
switch (role) {
|
||||||
case Qt::EditRole:
|
case Qt::EditRole:
|
||||||
switch(index.column()) {
|
switch (index.column()) {
|
||||||
case 0:
|
case 0:
|
||||||
m_fields->at(index.row()).setName(value.toString().toStdString());
|
m_fields->at(index.row()).setName(value.toString().toStdString());
|
||||||
roles << role;
|
roles << role;
|
||||||
|
@ -155,33 +152,32 @@ bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int ro
|
||||||
m_fields->at(index.row()).setValue(value.toString().toStdString());
|
m_fields->at(index.row()).setValue(value.toString().toStdString());
|
||||||
roles << role;
|
roles << role;
|
||||||
break;
|
break;
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case FieldTypeRole: {
|
case FieldTypeRole: {
|
||||||
bool ok;
|
bool ok;
|
||||||
int fieldType = value.toInt(&ok);
|
int fieldType = value.toInt(&ok);
|
||||||
if(ok && Field::isValidType(fieldType)) {
|
if (ok && Field::isValidType(fieldType)) {
|
||||||
roles << role;
|
roles << role;
|
||||||
m_fields->at(index.row()).setType(static_cast<FieldType>(fieldType));
|
m_fields->at(index.row()).setType(static_cast<FieldType>(fieldType));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
} default:
|
}
|
||||||
;
|
default:;
|
||||||
}
|
}
|
||||||
// remove last field if empty, showing an empty field at the end to enabled appending new rows is provided by the data method
|
// remove last field if empty, showing an empty field at the end to enabled appending new rows is provided by the data method
|
||||||
if(!roles.isEmpty() && static_cast<size_t>(index.row()) == m_fields->size() - 1 && m_fields->at(index.row()).isEmpty()) {
|
if (!roles.isEmpty() && static_cast<size_t>(index.row()) == m_fields->size() - 1 && m_fields->at(index.row()).isEmpty()) {
|
||||||
beginRemoveRows(index.parent(), index.row(), index.row());
|
beginRemoveRows(index.parent(), index.row(), index.row());
|
||||||
m_fields->pop_back();
|
m_fields->pop_back();
|
||||||
endRemoveRows();
|
endRemoveRows();
|
||||||
}
|
}
|
||||||
} else if(static_cast<size_t>(index.row()) == m_fields->size() && !value.toString().isEmpty()) {
|
} else if (static_cast<size_t>(index.row()) == m_fields->size() && !value.toString().isEmpty()) {
|
||||||
// set data for a new field emplaced at the end of the field list
|
// set data for a new field emplaced at the end of the field list
|
||||||
switch(role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
case Qt::EditRole:
|
case Qt::EditRole:
|
||||||
switch(index.column()) {
|
switch (index.column()) {
|
||||||
case 0:
|
case 0:
|
||||||
beginInsertRows(index.parent(), rowCount(), rowCount());
|
beginInsertRows(index.parent(), rowCount(), rowCount());
|
||||||
m_fields->emplace_back(m_accountEntry);
|
m_fields->emplace_back(m_accountEntry);
|
||||||
|
@ -196,28 +192,25 @@ bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int ro
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
roles << role;
|
roles << role;
|
||||||
break;
|
break;
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// return false if nothing could be changed
|
// return false if nothing could be changed
|
||||||
if(roles.isEmpty()) {
|
if (roles.isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// some roles affect other roles
|
// some roles affect other roles
|
||||||
switch(role) {
|
switch (role) {
|
||||||
case Qt::EditRole:
|
case Qt::EditRole:
|
||||||
roles << Qt::DisplayRole;
|
roles << Qt::DisplayRole;
|
||||||
break;
|
break;
|
||||||
case FieldTypeRole:
|
case FieldTypeRole:
|
||||||
roles << Qt::DisplayRole << Qt::EditRole;
|
roles << Qt::DisplayRole << Qt::EditRole;
|
||||||
break;
|
break;
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
// emit data changed signal on sucess
|
// emit data changed signal on sucess
|
||||||
emit dataChanged(index, index, roles);
|
emit dataChanged(index, index, roles);
|
||||||
|
@ -231,25 +224,22 @@ Qt::ItemFlags FieldModel::flags(const QModelIndex &index) const
|
||||||
|
|
||||||
QVariant FieldModel::headerData(int section, Qt::Orientation orientation, int role) const
|
QVariant FieldModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||||
{
|
{
|
||||||
switch(orientation) {
|
switch (orientation) {
|
||||||
case Qt::Horizontal:
|
case Qt::Horizontal:
|
||||||
switch(role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
switch(section) {
|
switch (section) {
|
||||||
case 0:
|
case 0:
|
||||||
return tr("Name");
|
return tr("Name");
|
||||||
case 1:
|
case 1:
|
||||||
return tr("Value");
|
return tr("Value");
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
@ -267,11 +257,11 @@ int FieldModel::columnCount(const QModelIndex &parent) const
|
||||||
bool FieldModel::insertRows(int row, int count, const QModelIndex &parent)
|
bool FieldModel::insertRows(int row, int count, const QModelIndex &parent)
|
||||||
{
|
{
|
||||||
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
||||||
if(undoStack()) {
|
if (undoStack()) {
|
||||||
return push(new FieldModelInsertRowsCommand(this, row, count));
|
return push(new FieldModelInsertRowsCommand(this, row, count));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if(parent.isValid() || row < 0 || count <= 0 || static_cast<size_t>(row + count) > m_fields->size()) {
|
if (parent.isValid() || row < 0 || count <= 0 || static_cast<size_t>(row + count) > m_fields->size()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
beginInsertRows(parent, row, row + count - 1);
|
beginInsertRows(parent, row, row + count - 1);
|
||||||
|
@ -283,11 +273,11 @@ bool FieldModel::insertRows(int row, int count, const QModelIndex &parent)
|
||||||
bool FieldModel::removeRows(int row, int count, const QModelIndex &parent)
|
bool FieldModel::removeRows(int row, int count, const QModelIndex &parent)
|
||||||
{
|
{
|
||||||
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
|
||||||
if(undoStack()) {
|
if (undoStack()) {
|
||||||
return push(new FieldModelRemoveRowsCommand(this, row, count));
|
return push(new FieldModelRemoveRowsCommand(this, row, count));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if(parent.isValid() || row < 0 || count <= 0 || static_cast<size_t>(row + count) > m_fields->size()) {
|
if (parent.isValid() || row < 0 || count <= 0 || static_cast<size_t>(row + count) > m_fields->size()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
beginRemoveRows(parent, row, row + count - 1);
|
beginRemoveRows(parent, row, row + count - 1);
|
||||||
|
@ -297,7 +287,7 @@ bool FieldModel::removeRows(int row, int count, const QModelIndex &parent)
|
||||||
|
|
||||||
bool FieldModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
bool FieldModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
||||||
{
|
{
|
||||||
if(!QAbstractTableModel::dropMimeData(data, action, row, column, parent) && data->hasText()) {
|
if (!QAbstractTableModel::dropMimeData(data, action, row, column, parent) && data->hasText()) {
|
||||||
return setData(parent, data->text(), Qt::EditRole);
|
return setData(parent, data->text(), Qt::EditRole);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -311,11 +301,11 @@ QStringList FieldModel::mimeTypes() const
|
||||||
QMimeData *FieldModel::mimeData(const QModelIndexList &indexes) const
|
QMimeData *FieldModel::mimeData(const QModelIndexList &indexes) const
|
||||||
{
|
{
|
||||||
QMimeData *data = QAbstractTableModel::mimeData(indexes);
|
QMimeData *data = QAbstractTableModel::mimeData(indexes);
|
||||||
if(indexes.isEmpty()) {
|
if (indexes.isEmpty()) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
QStringList result;
|
QStringList result;
|
||||||
for(const QModelIndex &index : indexes) {
|
for (const QModelIndex &index : indexes) {
|
||||||
result << index.data(Qt::EditRole).toString();
|
result << index.data(Qt::EditRole).toString();
|
||||||
}
|
}
|
||||||
data->setText(result.join(QChar('\n')));
|
data->setText(result.join(QChar('\n')));
|
||||||
|
@ -329,12 +319,10 @@ QMimeData *FieldModel::mimeData(const QModelIndexList &indexes) const
|
||||||
*/
|
*/
|
||||||
const Field *FieldModel::field(size_t row) const
|
const Field *FieldModel::field(size_t row) const
|
||||||
{
|
{
|
||||||
if(m_fields && row < m_fields->size()) {
|
if (m_fields && row < m_fields->size()) {
|
||||||
return &(*m_fields)[row];
|
return &(*m_fields)[row];
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace QtGui
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue