added caching of servers and containers in models
This commit is contained in:
parent
68d9394d9f
commit
c3f39ad24d
8 changed files with 69 additions and 70 deletions
|
@ -21,23 +21,25 @@ bool ContainersModel::setData(const QModelIndex &index, const QVariant &value, i
|
|||
DockerContainer container = ContainerProps::allContainers().at(index.row());
|
||||
|
||||
switch (role) {
|
||||
case NameRole:
|
||||
// return ContainerProps::containerHumanNames().value(container);
|
||||
case DescRole:
|
||||
// return ContainerProps::containerDescriptions().value(container);
|
||||
case ConfigRole:
|
||||
m_settings->setContainerConfig(m_currentlyProcessedServerIndex,
|
||||
container,
|
||||
value.toJsonObject());
|
||||
case ServiceTypeRole:
|
||||
// return ContainerProps::containerService(container);
|
||||
case DockerContainerRole:
|
||||
// return container;
|
||||
case IsInstalledRole:
|
||||
// return m_settings->containers(m_currentlyProcessedServerIndex).contains(container);
|
||||
case IsDefaultRole:
|
||||
m_settings->setDefaultContainer(m_currentlyProcessedServerIndex, container);
|
||||
emit defaultContainerChanged();
|
||||
case NameRole:
|
||||
// return ContainerProps::containerHumanNames().value(container);
|
||||
case DescRole:
|
||||
// return ContainerProps::containerDescriptions().value(container);
|
||||
case ConfigRole:
|
||||
m_settings->setContainerConfig(m_currentlyProcessedServerIndex,
|
||||
container,
|
||||
value.toJsonObject());
|
||||
case ServiceTypeRole:
|
||||
// return ContainerProps::containerService(container);
|
||||
case DockerContainerRole:
|
||||
// return container;
|
||||
case IsInstalledRole:
|
||||
// return m_settings->containers(m_currentlyProcessedServerIndex).contains(container);
|
||||
case IsDefaultRole: {
|
||||
m_settings->setDefaultContainer(m_currentlyProcessedServerIndex, container);
|
||||
m_defaultContainerIndex = container;
|
||||
emit defaultContainerChanged();
|
||||
}
|
||||
}
|
||||
|
||||
emit dataChanged(index, index);
|
||||
|
@ -58,8 +60,10 @@ QVariant ContainersModel::data(const QModelIndex &index, int role) const
|
|||
return ContainerProps::containerHumanNames().value(container);
|
||||
case DescRole:
|
||||
return ContainerProps::containerDescriptions().value(container);
|
||||
case ConfigRole:
|
||||
return m_settings->containerConfig(m_currentlyProcessedServerIndex, container);
|
||||
case ConfigRole: {
|
||||
if (container == DockerContainer::None) return QJsonObject();
|
||||
return m_containers.value(container);
|
||||
}
|
||||
case ServiceTypeRole:
|
||||
return ContainerProps::containerService(container);
|
||||
case DockerContainerRole:
|
||||
|
@ -71,11 +75,11 @@ QVariant ContainersModel::data(const QModelIndex &index, int role) const
|
|||
case EasySetupDescriptionRole:
|
||||
return ContainerProps::easySetupDescription(container);
|
||||
case IsInstalledRole:
|
||||
return m_settings->containers(m_currentlyProcessedServerIndex).contains(container);
|
||||
return m_containers.contains(container);
|
||||
case IsCurrentlyInstalledRole:
|
||||
return container == static_cast<DockerContainer>(m_currentlyInstalledContainerIndex);
|
||||
case IsDefaultRole:
|
||||
return container == m_settings->defaultContainer(m_currentlyProcessedServerIndex);
|
||||
return container == m_defaultContainerIndex;
|
||||
case IsSupportedRole:
|
||||
return ContainerProps::isSupportedByCurrentPlatform(container);
|
||||
}
|
||||
|
@ -87,6 +91,8 @@ void ContainersModel::setCurrentlyProcessedServerIndex(int index)
|
|||
{
|
||||
beginResetModel();
|
||||
m_currentlyProcessedServerIndex = index;
|
||||
m_containers = m_settings->containers(m_currentlyProcessedServerIndex);
|
||||
m_defaultContainerIndex = m_settings->defaultContainer(m_currentlyProcessedServerIndex);
|
||||
endResetModel();
|
||||
emit defaultContainerChanged();
|
||||
}
|
||||
|
@ -98,12 +104,12 @@ void ContainersModel::setCurrentlyInstalledContainerIndex(int index)
|
|||
|
||||
DockerContainer ContainersModel::getDefaultContainer()
|
||||
{
|
||||
return m_settings->defaultContainer(m_currentlyProcessedServerIndex);
|
||||
return m_defaultContainerIndex;
|
||||
}
|
||||
|
||||
QString ContainersModel::getDefaultContainerName()
|
||||
{
|
||||
return ContainerProps::containerHumanNames().value(getDefaultContainer());
|
||||
return ContainerProps::containerHumanNames().value(m_defaultContainerIndex);
|
||||
}
|
||||
|
||||
int ContainersModel::getCurrentlyInstalledContainerIndex()
|
||||
|
|
|
@ -55,8 +55,13 @@ protected:
|
|||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
private:
|
||||
QMap<DockerContainer, QJsonObject> m_containers;
|
||||
|
||||
|
||||
int m_currentlyProcessedServerIndex;
|
||||
int m_currentlyInstalledContainerIndex;
|
||||
DockerContainer m_defaultContainerIndex;
|
||||
|
||||
std::shared_ptr<Settings> m_settings;
|
||||
};
|
||||
|
||||
|
|
|
@ -2,24 +2,28 @@
|
|||
|
||||
ServersModel::ServersModel(std::shared_ptr<Settings> settings, QObject *parent) : m_settings(settings), QAbstractListModel(parent)
|
||||
{
|
||||
|
||||
m_servers = m_settings->serversArray();
|
||||
m_defaultServerIndex = m_settings->defaultServerIndex();
|
||||
}
|
||||
|
||||
int ServersModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
return static_cast<int>(m_settings->serversCount());
|
||||
return static_cast<int>(m_servers.size());
|
||||
}
|
||||
|
||||
bool ServersModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0
|
||||
|| index.row() >= static_cast<int>(m_settings->serversCount())) {
|
||||
|| index.row() >= static_cast<int>(m_servers.size())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (role) {
|
||||
case IsDefaultRole: m_settings->setDefaultServer(index.row());
|
||||
case IsDefaultRole: {
|
||||
m_settings->setDefaultServer(index.row());
|
||||
m_defaultServerIndex = m_settings->defaultServerIndex();
|
||||
}
|
||||
default: return true;
|
||||
}
|
||||
|
||||
|
@ -29,12 +33,11 @@ bool ServersModel::setData(const QModelIndex &index, const QVariant &value, int
|
|||
|
||||
QVariant ServersModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= static_cast<int>(m_settings->serversCount())) {
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= static_cast<int>(m_servers.size())) {
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
const QJsonArray &servers = m_settings->serversArray();
|
||||
const QJsonObject server = servers.at(index.row()).toObject();
|
||||
const QJsonObject server = m_servers.at(index.row()).toObject();
|
||||
|
||||
switch (role) {
|
||||
case NameRole: {
|
||||
|
@ -49,7 +52,7 @@ QVariant ServersModel::data(const QModelIndex &index, int role) const
|
|||
case CredentialsRole:
|
||||
return QVariant::fromValue(m_settings->serverCredentials(index.row()));
|
||||
case IsDefaultRole:
|
||||
return index.row() == m_settings->defaultServerIndex();
|
||||
return index.row() == m_defaultServerIndex;
|
||||
case IsCurrentlyProcessedRole:
|
||||
return index.row() == m_currenlyProcessedServerIndex;
|
||||
}
|
||||
|
@ -59,12 +62,12 @@ QVariant ServersModel::data(const QModelIndex &index, int role) const
|
|||
|
||||
const int ServersModel::getDefaultServerIndex()
|
||||
{
|
||||
return m_settings->defaultServerIndex();
|
||||
return m_defaultServerIndex;
|
||||
}
|
||||
|
||||
const int ServersModel::getServersCount()
|
||||
{
|
||||
return m_settings->serversCount();
|
||||
return m_servers.count();
|
||||
}
|
||||
|
||||
void ServersModel::setCurrentlyProcessedServerIndex(int index)
|
||||
|
@ -74,7 +77,7 @@ void ServersModel::setCurrentlyProcessedServerIndex(int index)
|
|||
|
||||
bool ServersModel::isDefaultServerCurrentlyProcessed()
|
||||
{
|
||||
return m_settings->defaultServerIndex() == m_currenlyProcessedServerIndex;
|
||||
return m_defaultServerIndex == m_currenlyProcessedServerIndex;
|
||||
}
|
||||
|
||||
ServerCredentials ServersModel::getCurrentlyProcessedServerCredentials()
|
||||
|
@ -86,6 +89,7 @@ void ServersModel::addServer(const QJsonObject &server)
|
|||
{
|
||||
beginResetModel();
|
||||
m_settings->addServer(server);
|
||||
m_servers = m_settings->serversArray();
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
|
@ -93,6 +97,7 @@ void ServersModel::removeServer()
|
|||
{
|
||||
beginResetModel();
|
||||
m_settings->removeServer(m_currenlyProcessedServerIndex);
|
||||
m_servers = m_settings->serversArray();
|
||||
|
||||
if (m_settings->defaultServerIndex() == m_currenlyProcessedServerIndex) {
|
||||
m_settings->setDefaultServer(0);
|
||||
|
|
|
@ -46,8 +46,11 @@ protected:
|
|||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
private:
|
||||
QJsonArray m_servers;
|
||||
|
||||
std::shared_ptr<Settings> m_settings;
|
||||
|
||||
int m_defaultServerIndex;
|
||||
int m_currenlyProcessedServerIndex;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue