UI: ZoneListModel: Simplify wrappers

This commit is contained in:
Nodir Temirkhodjaev 2024-10-05 12:22:26 +05:00
parent 81f14cfa1a
commit 12ffbc894e
7 changed files with 57 additions and 24 deletions

View File

@ -35,9 +35,7 @@ void ZoneEditDialog::initialize(const ZoneRow &zoneRow)
retranslateUi();
const QString sourceCode =
isEmpty() ? ZoneSourceWrapper::defaultSourceCode() : zoneRow.sourceCode;
const ZoneSourceWrapper zoneSource(zoneListModel()->zoneSourceByCode(sourceCode));
const ZoneSourceWrapper zoneSource(zoneListModel()->zoneSourceByCode(zoneRow.sourceCode));
m_editName->setStartText(zoneRow.zoneName);
m_comboSources->setCurrentIndex(zoneSource.id());

View File

@ -165,14 +165,26 @@ const ZoneRow &ZoneListModel::zoneRowAt(int row) const
return m_zoneRow;
}
QVariant ZoneListModel::zoneTypeById(int typeId) const
{
return zoneTypes().value(typeId);
}
QVariant ZoneListModel::zoneTypeByCode(const QString &typeCode) const
{
return m_zoneTypesMap.value(typeCode);
const int typeId = ZoneTypeWrapper::idByCode(typeCode);
return zoneTypeById(typeId);
}
QVariant ZoneListModel::zoneSourceById(int sourceId) const
{
return zoneSources().value(sourceId);
}
QVariant ZoneListModel::zoneSourceByCode(const QString &sourceCode) const
{
return m_zoneSourcesMap.value(sourceCode);
const int sourceId = ZoneSourceWrapper::idByCode(sourceCode);
return zoneSourceById(sourceId);
}
bool ZoneListModel::updateTableRow(const QVariantHash &vars, int /*row*/) const
@ -241,8 +253,7 @@ void ZoneListModel::setupZoneTypes()
int index = 0;
for (const auto &typeVar : zoneTypes) {
ZoneTypeWrapper zoneType(typeVar);
zoneType.setIndex(index++);
m_zoneTypesMap.insert(zoneType.code(), zoneType.map());
zoneType.setId(index++);
m_zoneTypes.append(zoneType.map());
}
}
@ -264,7 +275,6 @@ void ZoneListModel::setupZoneSources()
for (const auto &sourceVar : zoneSources) {
ZoneSourceWrapper zoneSource(sourceVar);
zoneSource.setId(index++);
m_zoneSourcesMap.insert(zoneSource.code(), zoneSource.map());
m_zoneSources.append(zoneSource.map());
}
}

View File

@ -39,8 +39,11 @@ public:
const ZoneRow &zoneRowAt(int row) const;
QVariant zoneTypeById(int typeId) const;
QVariant zoneTypeByCode(const QString &typeCode) const;
const QVariantList &zoneTypes() const { return m_zoneTypes; }
QVariant zoneSourceById(int sourceId) const;
QVariant zoneSourceByCode(const QString &sourceCode) const;
const QVariantList &zoneSources() const { return m_zoneSources; }
@ -67,10 +70,7 @@ private:
private:
QVariantList m_zoneTypes;
QVariantHash m_zoneTypesMap;
QVariantList m_zoneSources;
QVariantHash m_zoneSourcesMap;
mutable ZoneRow m_zoneRow;
};

View File

@ -41,10 +41,18 @@ QString ZoneSourceWrapper::formData() const
bool ZoneSourceWrapper::isTextInline() const
{
return code() == defaultSourceCode();
return id() == SourceText;
}
QString ZoneSourceWrapper::defaultSourceCode()
int ZoneSourceWrapper::idByCode(const QString &code)
{
return "text";
static const QHash<QString, int> map = {
{ "text", SourceText },
{ "file", SourceFile },
{ "winspyblock", SourceWinSpyBlock },
{ "firehol_level1", SourceFireHol_Level1 },
{ "tasix", SourceTasix },
};
return map.value(code, SourceText);
}

View File

@ -10,11 +10,11 @@ public:
SourceText = 0,
SourceFile,
SourceWinSpyBlock,
SourceFireHolLevel1,
SourceFireHol_Level1,
SourceTasix,
};
explicit ZoneSourceWrapper(const QVariant &var = QVariant());
explicit ZoneSourceWrapper(const QVariant &var = {});
int id() const;
void setId(int id);
@ -27,7 +27,7 @@ public:
bool isTextInline() const;
static QString defaultSourceCode();
static int idByCode(const QString &code);
};
#endif // ZONESOURCEWRAPPER_H

View File

@ -2,14 +2,14 @@
ZoneTypeWrapper::ZoneTypeWrapper(const QVariant &var) : MapWrapper(var) { }
int ZoneTypeWrapper::index() const
int ZoneTypeWrapper::id() const
{
return valueInt("index");
return valueInt("id");
}
void ZoneTypeWrapper::setIndex(int index)
void ZoneTypeWrapper::setId(int id)
{
setValue("index", index);
setValue("id", id);
}
QString ZoneTypeWrapper::code() const
@ -41,3 +41,13 @@ int ZoneTypeWrapper::emptyNetMask() const
{
return valueInt("emptyNetMask");
}
int ZoneTypeWrapper::idByCode(const QString &code)
{
static const QHash<QString, int> map = {
{ "gen", TypeGeneric },
{ "bgp", TypeBgp },
};
return map.value(code, TypeGeneric);
}

View File

@ -6,10 +6,15 @@
class ZoneTypeWrapper : public MapWrapper
{
public:
explicit ZoneTypeWrapper(const QVariant &var = QVariant());
enum TypeId {
TypeGeneric = 0,
TypeBgp,
};
int index() const;
void setIndex(int index);
explicit ZoneTypeWrapper(const QVariant &var = {});
int id() const;
void setId(int index);
QString code() const;
QString title() const;
@ -17,6 +22,8 @@ public:
bool sort() const;
QString pattern() const;
int emptyNetMask() const;
static int idByCode(const QString &code);
};
#endif // ZONEWRAPPER_H