#include "ConnectionListModel.h" #include "ConnectionList.h" #include "ScopeGuard.h" #include "util.h" #include 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 ColCount; } 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 Description: result = makeLongDescription(cfg); break; case Name: result = stdStrToQ(cfg.name()); break; case Host: result = stdStrToQ(cfg.host()); break; case Port: result = cfg.port(); break; case User: result = stdStrToQ(cfg.user()); break; case Password: result = stdStrToQ(cfg.password()); break; case DbName: 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 Description: break; case Name: cfg.setName( qStrToStd(value.toString()) ); break; case Host: cfg.setHost( qStrToStd(value.toString()) ); break; case Port: cfg.setPort( value.toInt() ); break; case User: cfg.setUser( qStrToStd(value.toString()) ); break; case Password: cfg.setPassword( qStrToStd(value.toString()) ); break; case DbName: 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::ItemIsEnabled; if (index.column() != Description) result |= Qt::ItemIsEditable; } 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 ConnectionListModel::get(size_t row) { if (row < m_connections->size()) { return m_connections->getConfigByIdx(row); } return Expected::fromException(std::out_of_range("Invalid row")); } #include template size_t as_size_t(T t); template <> size_t as_size_t(int t) { BOOST_ASSERT(t >= 0); return static_cast(t); } template <> size_t as_size_t(long t) { BOOST_ASSERT(t >= 0); return static_cast(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); }