Connection manager, editing working, hooked up more widgets.

This commit is contained in:
Eelke Klein 2017-01-14 22:29:12 +01:00
parent c235169304
commit 30dbc59e41
3 changed files with 66 additions and 14 deletions

View file

@ -67,7 +67,7 @@ int ConnectionListModel::rowCount(const QModelIndex &parent) const
int ConnectionListModel::columnCount(const QModelIndex &/*parent*/) const
{
return 2;
return 4;
}
QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
@ -76,17 +76,60 @@ QVariant ConnectionListModel::data(const QModelIndex &index, int role) const
if (role == Qt::DisplayRole || role == Qt::EditRole) {
int row = index.row();
int col = index.column();
// if (col == 0) {
result = makeLongDescription(m_connections.at(row));
// }
//else {
//result = QString("other col");
//}
const ConnectionConfig& cfg = m_connections.at(row);
switch (col) {
case 0:
result = makeLongDescription(cfg);
break;
case 1:
result = stdStrToQ(cfg.description());
break;
case 2:
result = stdStrToQ(cfg.host());
break;
case 3:
result = cfg.port();
break;
}
}
return result;
}
bool ConnectionListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::EditRole) {
int row = index.row();
int col = index.column();
ConnectionConfig& cfg = m_connections.at(row);
switch (col) {
case 0:
break;
case 1:
cfg.setDescription( qStrToStd(value.toString()) );
emit dataChanged(index, index);
break;
case 2:
cfg.setHost( qStrToStd(value.toString()) );
emit dataChanged(index, index);
break;
case 3:
cfg.setPort( value.toInt() );
emit dataChanged(index, index);
break;
}
}
return true;
}
Qt::ItemFlags ConnectionListModel::flags(const QModelIndex &index) const
{
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
}
QString ConnectionListModel::makeLongDescription(const ConnectionConfig &cfg)
{
std::string result(cfg.description());
@ -94,8 +137,18 @@ QString ConnectionListModel::makeLongDescription(const ConnectionConfig &cfg)
result += cfg.user();
result += "@";
result += cfg.host();
result += ":";
result += std::to_string(cfg.port());
result += "/";
result += cfg.dbname();
result += ")";
return stdStrToQ(result);
}
void ConnectionListModel::add(const ConnectionConfig &cfg)
{
m_connections.push_back(cfg);
auto idx = createIndex(m_connections.size()-1, 0);
emit dataChanged(idx, idx);
}