Add AutoTypeAssociationsModel class.

This commit is contained in:
Felix Geyer 2012-07-16 17:39:20 +02:00
parent 3c913f196f
commit af166a66b3
5 changed files with 229 additions and 0 deletions

View File

@ -76,6 +76,7 @@ set(keepassx_SOURCES
gui/MainWindow.cpp
gui/SettingsWidget.cpp
gui/WelcomeWidget.cpp
gui/entry/AutoTypeAssociationsModel.cpp
gui/entry/EditEntryWidget.cpp
gui/entry/EntryAttachmentsModel.cpp
gui/entry/EntryAttributesModel.cpp
@ -125,6 +126,7 @@ set(keepassx_MOC
gui/MainWindow.h
gui/SettingsWidget.h
gui/WelcomeWidget.h
gui/entry/AutoTypeAssociationsModel.h
gui/entry/EditEntryWidget.h
gui/entry/EntryAttachmentsModel.h
gui/entry/EntryAttributesModel.h

View File

@ -0,0 +1,137 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
*
* 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 2 or (at your option)
* version 3 of the License.
*
* 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 "AutoTypeAssociationsModel.h"
AutoTypeAssociationsModel::AutoTypeAssociationsModel(QObject* parent)
: QAbstractListModel(parent)
, m_autoTypeAssociations(Q_NULLPTR)
{
}
void AutoTypeAssociationsModel::setAutoTypeAssociations(AutoTypeAssociations* autoTypeAssociations)
{
beginResetModel();
if (m_autoTypeAssociations) {
m_autoTypeAssociations->disconnect(this);
}
m_autoTypeAssociations = autoTypeAssociations;
if (m_autoTypeAssociations) {
connect(m_autoTypeAssociations, SIGNAL(dataChanged(int)), SLOT(associationChange(int)));
connect(m_autoTypeAssociations, SIGNAL(aboutToAdd(int)), SLOT(associationAboutToAdd(int)));
connect(m_autoTypeAssociations, SIGNAL(added(int)), SLOT(associationAdd()));
connect(m_autoTypeAssociations, SIGNAL(aboutToRemove(int)), SLOT(associationAboutToRemove(int)));
connect(m_autoTypeAssociations, SIGNAL(removed(int)), SLOT(associationRemove()));
connect(m_autoTypeAssociations, SIGNAL(aboutToReset()), SLOT(aboutToReset()));
connect(m_autoTypeAssociations, SIGNAL(reset()), SLOT(reset()));
}
endResetModel();
}
int AutoTypeAssociationsModel::rowCount(const QModelIndex& parent) const
{
if (!m_autoTypeAssociations || parent.isValid()) {
return 0;
}
else {
return m_autoTypeAssociations->size();
}
}
int AutoTypeAssociationsModel::columnCount(const QModelIndex& parent) const
{
Q_UNUSED(parent);
return 2;
}
QVariant AutoTypeAssociationsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole)) {
if (section == 0) {
return tr("Window");
}
else {
return tr("Sequence");
}
}
else {
return QVariant();
}
}
QVariant AutoTypeAssociationsModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
if (role == Qt::DisplayRole) {
if (index.column() == 0) {
return m_autoTypeAssociations->get(index.row()).window;
}
else {
QString sequence = m_autoTypeAssociations->get(index.row()).sequence;
if (sequence.isEmpty()) {
sequence = tr("Default sequence");
}
return sequence;
}
}
else {
return QVariant();
}
}
void AutoTypeAssociationsModel::associationChange(int i)
{
Q_EMIT dataChanged(index(i, 0), index(i, columnCount() - 1));
}
void AutoTypeAssociationsModel::associationAboutToAdd(int i)
{
beginInsertRows(QModelIndex(), i, i);
}
void AutoTypeAssociationsModel::associationAdd()
{
endInsertRows();
}
void AutoTypeAssociationsModel::associationAboutToRemove(int i)
{
beginRemoveRows(QModelIndex(), i, i);
}
void AutoTypeAssociationsModel::associationRemove()
{
endRemoveRows();
}
void AutoTypeAssociationsModel::aboutToReset()
{
beginResetModel();
}
void AutoTypeAssociationsModel::reset()
{
endResetModel();
}

View File

@ -0,0 +1,52 @@
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
*
* 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 2 or (at your option)
* version 3 of the License.
*
* 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 KEEPASSX_AUTOTYPEASSOCIATIONSMODEL_H
#define KEEPASSX_AUTOTYPEASSOCIATIONSMODEL_H
#include <QtCore/QAbstractListModel>
#include "core/AutoTypeAssociations.h"
class EntryAttributes;
class AutoTypeAssociationsModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit AutoTypeAssociationsModel(QObject* parent = Q_NULLPTR);
void setAutoTypeAssociations(AutoTypeAssociations* autoTypeAssociations);
int rowCount(const QModelIndex& parent = QModelIndex()) const Q_DECL_OVERRIDE;
int columnCount(const QModelIndex& parent = QModelIndex()) const Q_DECL_OVERRIDE;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
public Q_SLOTS:
void associationChange(int i);
void associationAboutToAdd(int i);
void associationAdd();
void associationAboutToRemove(int i);
void associationRemove();
void aboutToReset();
void reset();
private:
AutoTypeAssociations* m_autoTypeAssociations;
};
#endif // KEEPASSX_AUTOTYPEASSOCIATIONSMODEL_H

View File

@ -27,6 +27,7 @@
#include "core/Group.h"
#include "crypto/Crypto.h"
#include "gui/IconModels.h"
#include "gui/entry/AutoTypeAssociationsModel.h"
#include "gui/entry/EntryModel.h"
#include "gui/entry/EntryAttachmentsModel.h"
#include "gui/entry/EntryAttributesModel.h"
@ -227,4 +228,40 @@ void TestEntryModel::testCustomIconModel()
delete model;
}
void TestEntryModel::testAutoTypeAssociationsModel()
{
AutoTypeAssociationsModel* model = new AutoTypeAssociationsModel(this);
ModelTest* modelTest = new ModelTest(model, this);
QCOMPARE(model->rowCount(), 0);
AutoTypeAssociations* assocications = new AutoTypeAssociations(this);
model->setAutoTypeAssociations(assocications);
QCOMPARE(model->rowCount(), 0);
AutoTypeAssociations::Association assoc;
assoc.window = "1";
assoc.sequence = "2";
assocications->add(assoc);
QCOMPARE(model->rowCount(), 1);
QCOMPARE(model->data(model->index(0, 0)).toString(), QString("1"));
QCOMPARE(model->data(model->index(0, 1)).toString(), QString("2"));
assoc.window = "3";
assoc.sequence = "4";
assocications->update(0, assoc);
QCOMPARE(model->data(model->index(0, 0)).toString(), QString("3"));
QCOMPARE(model->data(model->index(0, 1)).toString(), QString("4"));
assocications->add(assoc);
assocications->remove(0);
QCOMPARE(model->rowCount(), 1);
delete modelTest;
delete model;
delete assocications;
}
KEEPASSX_QTEST_CORE_MAIN(TestEntryModel)

View File

@ -31,6 +31,7 @@ private Q_SLOTS:
void testAttributesModel();
void testDefaultIconModel();
void testCustomIconModel();
void testAutoTypeAssociationsModel();
};
#endif // KEEPASSX_TESTENTRYMODEL_H