1
0
forked from blue/squawk
squawk/ui/accounts.cpp

80 lines
2.3 KiB
C++
Raw Normal View History

2019-03-29 14:54:34 +00:00
#include "accounts.h"
#include "ui_accounts.h"
2019-03-30 20:13:13 +00:00
#include <QDebug>
Accounts::Accounts(Models::Accounts* p_model, QWidget *parent) :
m_ui(new Ui::Accounts),
model(p_model),
editing(false)
2019-03-29 14:54:34 +00:00
{
m_ui->setupUi(this);
2019-03-30 20:13:13 +00:00
connect(m_ui->addButton, SIGNAL(clicked(bool)), this, SLOT(onAddButton(bool)));
connect(m_ui->editButton, SIGNAL(clicked(bool)), this, SLOT(onEditButton(bool)));
2019-04-03 18:15:36 +00:00
m_ui->tableView->setModel(model);
connect(m_ui->tableView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
this, SLOT(onSelectionChanged(const QItemSelection&, const QItemSelection&)));
2019-03-29 14:54:34 +00:00
}
Accounts::~Accounts() = default;
2019-03-30 20:13:13 +00:00
void Accounts::onAddButton(bool clicked)
{
Account* acc = new Account();
connect(acc, SIGNAL(accepted()), this, SLOT(onAccountAccepted()));
connect(acc, SIGNAL(rejected()), this, SLOT(onAccountRejected()));
acc->exec();
}
void Accounts::onAccountAccepted()
{
Account* acc = static_cast<Account*>(sender());
QMap<QString, QVariant> map = acc->value();
if (editing) {
emit changeAccount(map);
} else {
emit newAccount(map);
}
2019-03-30 20:13:13 +00:00
editing = false;
2019-03-30 20:13:13 +00:00
}
void Accounts::onAccountRejected()
{
Account* acc = static_cast<Account*>(sender());
acc->deleteLater();
editing = false;
}
void Accounts::onEditButton(bool clicked)
{
Account* acc = new Account();
const Models::Account* mAcc = model->getAccount(m_ui->tableView->selectionModel()->selectedRows().at(0).row());
acc->setData({
{"login", mAcc->getLogin()},
{"password", mAcc->getPassword()},
{"server", mAcc->getServer()},
{"name", mAcc->getName()},
{"resource", mAcc->getResource()}
});
acc->lockId();
connect(acc, SIGNAL(accepted()), this, SLOT(onAccountAccepted()));
connect(acc, SIGNAL(rejected()), this, SLOT(onAccountRejected()));
editing = true;
acc->exec();
}
void Accounts::onSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
{
const QItemSelection& selection = m_ui->tableView->selectionModel()->selection();
if (selection.size() == 0) {
m_ui->editButton->setEnabled(false);
} else if (selection.size() == 1) {
m_ui->editButton->setEnabled(true);
} else {
m_ui->editButton->setEnabled(false);
}
2019-03-30 20:13:13 +00:00
}