From e4a43a77bd853e21f19bbf9fde6a766b76ba070b Mon Sep 17 00:00:00 2001 From: eelke Date: Mon, 19 Aug 2019 19:26:39 +0200 Subject: [PATCH] Disable editing of the description of the connections in the connection ListView. Also replace the integer literals defining the columns for enum to make more readable. --- pglab/ConnectionListModel.cpp | 34 ++++++++++++++++++---------------- pglab/ConnectionListModel.h | 12 ++++++++++++ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/pglab/ConnectionListModel.cpp b/pglab/ConnectionListModel.cpp index 051e661..7297db7 100644 --- a/pglab/ConnectionListModel.cpp +++ b/pglab/ConnectionListModel.cpp @@ -25,7 +25,7 @@ int ConnectionListModel::rowCount(const QModelIndex &parent) const int ConnectionListModel::columnCount(const QModelIndex &/*parent*/) const { - return 7; + return ColCount; } QVariant ConnectionListModel::data(const QModelIndex &index, int role) const @@ -36,25 +36,25 @@ QVariant ConnectionListModel::data(const QModelIndex &index, int role) const int col = index.column(); const ConnectionConfig& cfg = m_connections->getConfigByIdx(row); switch (col) { - case 0: + case Description: result = makeLongDescription(cfg); break; - case 1: + case Name: result = stdStrToQ(cfg.name()); break; - case 2: + case Host: result = stdStrToQ(cfg.host()); break; - case 3: + case Port: result = cfg.port(); break; - case 4: + case User: result = stdStrToQ(cfg.user()); break; - case 5: + case Password: result = stdStrToQ(cfg.password()); break; - case 6: + case DbName: result = stdStrToQ(cfg.dbname()); break; } @@ -77,24 +77,24 @@ bool ConnectionListModel::setData(const QModelIndex &index, const QVariant &valu result = true; } switch (col) { - case 0: + case Description: break; - case 1: + case Name: cfg.setName( qStrToStd(value.toString()) ); break; - case 2: + case Host: cfg.setHost( qStrToStd(value.toString()) ); break; - case 3: + case Port: cfg.setPort( value.toInt() ); break; - case 4: + case User: cfg.setUser( qStrToStd(value.toString()) ); break; - case 5: + case Password: cfg.setPassword( qStrToStd(value.toString()) ); break; - case 6: + case DbName: cfg.setDbname( qStrToStd(value.toString()) ); break; } @@ -110,7 +110,9 @@ 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; + result = Qt::ItemIsSelectable | Qt::ItemIsEnabled; + if (index.column() != Description) + result |= Qt::ItemIsEditable; } return result; } diff --git a/pglab/ConnectionListModel.h b/pglab/ConnectionListModel.h index fccce79..be61202 100644 --- a/pglab/ConnectionListModel.h +++ b/pglab/ConnectionListModel.h @@ -18,6 +18,18 @@ class ConnectionList; class ConnectionListModel : public QAbstractListModel { Q_OBJECT public: + enum Columns { + Description, + Name, + Host, + Port, + User, + Password, + DbName, + + ColCount + }; + ConnectionListModel(ConnectionList *conns, QObject *parent); ConnectionListModel(const ConnectionListModel&) = delete; ~ConnectionListModel() override;