Rabbit Remote Control 0.0.36
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 bool check = connect(this, SIGNAL(sigChanged()), p, SIGNAL(sigChanged()));
17 Q_ASSERT(check);
18 }
19}
20
21CParameter::~CParameter()
22{}
23
24QString CParameter::GetPrefix() const
25{
26 return m_szPrefix;
27}
28
29int CParameter::SetPrefix(const QString& szName)
30{
31 m_szPrefix = szName;
32 return 0;
33}
34
35int CParameter::Load(QString szFile)
36{
37 if(szFile.isEmpty())
38 szFile = RabbitCommon::CDir::Instance()->GetFileUserConfigure();
39 if(szFile.isEmpty()) return -1;
40 QSettings set(szFile, QSettings::IniFormat);
41 int nRet = Load(set);
42 SetModified(false);
43 return nRet;
44}
45
46int CParameter::Save(QString szFile, bool bForce)
47{
48 if(!GetModified() && !bForce) return 0;
49 if(szFile.isEmpty())
50 szFile = RabbitCommon::CDir::Instance()->GetFileUserConfigure();
51 if(szFile.isEmpty()) return -1;
52 QSettings set(szFile, QSettings::IniFormat);
53 int nRet = Save(set);
54 SetModified(false);
55 return nRet;
56}
57
58int CParameter::Load(QSettings &set)
59{
60 int nRet = 0;
61
62 if(!GetPrefix().isEmpty())
63 set.beginGroup(GetPrefix());
64
65 nRet = OnLoad(set);
66
67 if(!nRet) {
68 foreach (auto p, m_Category) {
69 nRet = p->Load(set);
70 if(nRet) break;
71 }
72 }
73
74 if(!GetPrefix().isEmpty())
75 set.endGroup();
76 return nRet;
77}
78
79int CParameter::Save(QSettings &set, bool bForce)
80{
81 int nRet = 0;
82 if(!GetModified() && !bForce) return 0;
83
84 if(!GetPrefix().isEmpty())
85 set.beginGroup(GetPrefix());
86
87 nRet = OnSave(set);
88
89 if(!nRet) {
90 foreach (auto p, m_Category) {
91 nRet = p->Save(set);
92 if(nRet) break;
93 }
94 }
95
96 if(!GetPrefix().isEmpty())
97 set.endGroup();
98 return nRet;
99}
100
102{
103 bool bRet = true;
104 foreach (auto p, m_Category) {
105 bRet = p->OnCheckValidity();
106 if(!bRet) break;
107 }
108 if(bRet)
109 bRet = OnCheckValidity();
110 return bRet;
111}
112
114{
115 qDebug(log) << " Not implemented CParameter::onCheckValidity()";
116 return true;
117}
118
120{
121 return m_bModified;
122}
123
124int CParameter::SetModified(bool bModified)
125{
126 if(m_bModified == bModified)
127 return 0;
128 m_bModified = bModified;
129 return 0;
130}
131
133{
134 if(!m_Category.contains(p))
135 m_Category.push_back(p);
136 return 0;
137}
Parameter interface.
Definition Parameter.h:169
QVector< CParameter * > m_Category
Category.
Definition Parameter.h:300
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:294
virtual bool CheckValidity()
Check whether the parameter is valid to decide whether to use or save the parameter.
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:46
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:35