Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
Parameter.cpp
1#include "Parameter.h"
2#include "RabbitCommonDir.h"
3
4#include <QLoggingCategory>
5
6static Q_LOGGING_CATEGORY(log, "Client.Parameter")
7
8CParameter::CParameter(QObject* parent, const QString& szPrefix)
9 : QObject(parent),
10 m_bModified(false),
11 m_szPrefix(szPrefix)
12{
13 CParameter* p = qobject_cast<CParameter*>(parent);
14 if(p) {
15 p->AddCategory(this);
16 }
17}
18
19CParameter::~CParameter()
20{}
21
22QString CParameter::GetPrefix() const
23{
24 return m_szPrefix;
25}
26
27int CParameter::SetPrefix(const QString& szName)
28{
29 m_szPrefix = szName;
30 return 0;
31}
32
33int CParameter::Load(QString szFile)
34{
35 if(szFile.isEmpty())
36 szFile = RabbitCommon::CDir::Instance()->GetFileUserConfigure();
37 if(szFile.isEmpty()) return -1;
38 QSettings set(szFile, QSettings::IniFormat);
39 int nRet = Load(set);
40 SetModified(false);
41 return nRet;
42}
43
44int CParameter::Save(QString szFile, bool bForce)
45{
46 if(!GetModified() && !bForce) return 0;
47 if(szFile.isEmpty())
48 szFile = RabbitCommon::CDir::Instance()->GetFileUserConfigure();
49 if(szFile.isEmpty()) return -1;
50 QSettings set(szFile, QSettings::IniFormat);
51 int nRet = Save(set);
52 SetModified(false);
53 return nRet;
54}
55
56int CParameter::Load(QSettings &set)
57{
58 int nRet = 0;
59
60 if(!GetPrefix().isEmpty())
61 set.beginGroup(GetPrefix());
62
63 nRet = OnLoad(set);
64
65 if(!nRet) {
66 foreach (auto p, m_Category) {
67 nRet = p->Load(set);
68 if(nRet) break;
69 }
70 }
71
72 if(!GetPrefix().isEmpty())
73 set.endGroup();
74 return nRet;
75}
76
77int CParameter::Save(QSettings &set, bool bForce)
78{
79 int nRet = 0;
80 if(!GetModified() && !bForce) return 0;
81
82 if(!GetPrefix().isEmpty())
83 set.beginGroup(GetPrefix());
84
85 nRet = OnSave(set);
86
87 if(!nRet) {
88 foreach (auto p, m_Category) {
89 nRet = p->Save(set);
90 if(nRet) break;
91 }
92 }
93
94 if(!GetPrefix().isEmpty())
95 set.endGroup();
96 return nRet;
97}
98
100{
101 bool bRet = true;
102 foreach (auto p, m_Category) {
103 bRet = p->OnCheckValidity();
104 if(!bRet) break;
105 }
106 if(bRet)
107 bRet = OnCheckValidity();
108 return bRet;
109}
110
112{
113 qDebug(log) << " Not implemented CParameter::onCheckValidity()";
114 return true;
115}
116
118{
119 return m_bModified;
120}
121
122int CParameter::SetModified(bool bModified)
123{
124 if(m_bModified == bModified)
125 return 0;
126 m_bModified = bModified;
127 return 0;
128}
129
131{
132 if(!m_Category.contains(p))
133 m_Category.push_back(p);
134 return 0;
135}
Parameter interface.
Definition Parameter.h:169
QVector< CParameter * > m_Category
Category.
Definition Parameter.h:302
int SetModified(bool bModified=true)
When setting parameters, if there is a modification, it is called.
bool m_bModified
If false, then don't save when save.
Definition Parameter.h:296
virtual bool CheckValidity()
Check whether the parameter is valid to decide whether to use or save the parameter.
Definition Parameter.cpp:99
int AddCategory(CParameter *p)
Instances of this class and its derived classes are members of the instance.
virtual int Save(QString szFile=QString(), bool bForce=true)
Save to file.
Definition Parameter.cpp:44
bool GetModified()
Whether the parameters have been modified.
virtual bool OnCheckValidity()
Check validity.
virtual int Load(QString szFile=QString())
Load from file.
Definition Parameter.cpp:33