pgLab/pglab/ConnectionListModel.cpp
2018-12-16 15:38:32 +01:00

205 lines
4.1 KiB
C++

#include "ConnectionListModel.h"
#include "ConnectionList.h"
#include "ScopeGuard.h"
#include "util.h"
#include <botan/cryptobox.h>
ConnectionListModel::ConnectionListModel(ConnectionList *conns, QObject *parent)
: QAbstractListModel(parent)
, m_connections(conns)
{
}
ConnectionListModel::~ConnectionListModel() = default;
int ConnectionListModel::rowCount(const QModelIndex &parent) const
{
int result = 0;
if (parent == QModelIndex()) {
result = m_connections->size();
}
return result;
}
int ConnectionListModel::columnCount(const QModelIndex &/*parent*/) const
{
return 7;
}
QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
{
QVariant result;
if (role == Qt::DisplayRole || role == Qt::EditRole) {
int row = index.row();
int col = index.column();
const ConnectionConfig& cfg = m_connections->getConfigByIdx(row);
switch (col) {
case 0:
result = makeLongDescription(cfg);
break;
case 1:
result = stdStrToQ(cfg.name());
break;
case 2:
result = stdStrToQ(cfg.host());
break;
case 3:
result = cfg.port();
break;
case 4:
result = stdStrToQ(cfg.user());
break;
case 5:
result = stdStrToQ(cfg.password());
break;
case 6:
result = stdStrToQ(cfg.dbname());
break;
}
}
return result;
}
bool ConnectionListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
bool result = false;
if (role == Qt::EditRole) {
int row = index.row();
int col = index.column();
// auto& elem = m_connections.at(row);
// elem.m_dirty = true;
// ConnectionConfig& cfg = elem.m_config;
ConnectionConfig& cfg = m_connections->getConfigByIdx(row);
if (col > 0) {
result = true;
}
switch (col) {
case 0:
break;
case 1:
cfg.setName( qStrToStd(value.toString()) );
break;
case 2:
cfg.setHost( qStrToStd(value.toString()) );
break;
case 3:
cfg.setPort( value.toInt() );
break;
case 4:
cfg.setUser( qStrToStd(value.toString()) );
break;
case 5:
cfg.setPassword( qStrToStd(value.toString()) );
break;
case 6:
cfg.setDbname( qStrToStd(value.toString()) );
break;
}
}
if (result) {
emit dataChanged(index, index);
}
return result;
}
Qt::ItemFlags ConnectionListModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags result;
int row = index.row();
if (row >= 0 && row < (int)m_connections->size()) {
result = Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
}
return result;
}
QString ConnectionListModel::makeLongDescription(const ConnectionConfig &cfg)
{
std::string result(cfg.name());
result += " (";
result += cfg.user();
result += "@";
result += cfg.host();
result += ":";
result += std::to_string(cfg.port());
result += "/";
result += cfg.dbname();
result += ")";
return stdStrToQ(result);
}
void ConnectionListModel::newItem()
{
int i = m_connections->createNew();
auto idx = createIndex(i, 0);
emit dataChanged(idx, idx);
}
Expected<ConnectionConfig> ConnectionListModel::get(size_t row)
{
if (row < m_connections->size()) {
return m_connections->getConfigByIdx(row);
}
return Expected<ConnectionConfig>::fromException(std::out_of_range("Invalid row"));
}
#include <boost/assert.hpp>
template <typename T>
size_t as_size_t(T t);
template <>
size_t as_size_t(int t)
{
BOOST_ASSERT(t >= 0);
return static_cast<size_t>(t);
}
template <>
size_t as_size_t(long t)
{
BOOST_ASSERT(t >= 0);
return static_cast<size_t>(t);
}
//void ConnectionListModel::del(const int idx)
bool ConnectionListModel::removeRows(int row, int count, const QModelIndex &parent)
{
bool result = false;
if (row >= 0 && row < (int)m_connections->size()) {
beginRemoveRows(parent, row, row + count -1);
SCOPE_EXIT { endRemoveRows(); };
m_connections->remove(as_size_t(row), as_size_t(count));
result = true;
}
return result;
}
//void ConnectionListModel::load()
//{
// m_connections->load();
//}
void ConnectionListModel::save()
{
m_connections->save();
}
void ConnectionListModel::save(size_t index)
{
m_connections->save(index);
}
void ConnectionListModel::save(size_t index, const ConnectionConfig &cc)
{
m_connections->setConfigByIdx(index, cc);
m_connections->save(index);
}