Rabbit Remote Control 0.1.0-bate5
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_szPrefix(szPrefix)
11 , m_bModified(false)
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 qDebug(log) << "Load configure file:" << szFile;
40 if(szFile.isEmpty()) return -1;
41 QSettings set(szFile, QSettings::IniFormat);
42 int nRet = Load(set);
43 SetModified(false);
44 return nRet;
45}
46
47int CParameter::Save(QString szFile, bool bForce)
48{
49 if(!GetModified() && !bForce) return 0;
50 if(szFile.isEmpty())
51 szFile = RabbitCommon::CDir::Instance()->GetFileUserConfigure();
52 qDebug(log) << "Save configure file:" << szFile;
53 if(szFile.isEmpty()) return -1;
54 QSettings set(szFile, QSettings::IniFormat);
55 int nRet = Save(set);
56 SetModified(false);
57 return nRet;
58}
59
60int CParameter::Load(QSettings &set)
61{
62 int nRet = 0;
63
64 if(!GetPrefix().isEmpty())
65 set.beginGroup(GetPrefix());
66
67 nRet = OnLoad(set);
68
69 if(!nRet) {
70 foreach (auto p, m_Category) {
71 nRet = p->Load(set);
72 if(nRet) break;
73 }
74 }
75
76 if(!GetPrefix().isEmpty())
77 set.endGroup();
78 return nRet;
79}
80
81int CParameter::Save(QSettings &set, bool bForce)
82{
83 int nRet = 0;
84 if(!GetModified() && !bForce) return 0;
85
86 if(!GetPrefix().isEmpty())
87 set.beginGroup(GetPrefix());
88
89 nRet = OnSave(set);
90
91 if(!nRet) {
92 foreach (auto p, m_Category) {
93 nRet = p->Save(set);
94 if(nRet) break;
95 }
96 }
97
98 if(!GetPrefix().isEmpty())
99 set.endGroup();
100 return nRet;
101}
102
104{
105 bool bRet = true;
106 foreach (auto p, m_Category) {
107 bRet = p->OnCheckValidity();
108 if(!bRet) break;
109 }
110 if(bRet)
111 bRet = OnCheckValidity();
112 return bRet;
113}
114
116{
117 qDebug(log) << " Not implemented CParameter::onCheckValidity()";
118 return true;
119}
120
122{
123 return m_bModified;
124}
125
126int CParameter::SetModified(bool bModified)
127{
128 if(m_bModified == bModified)
129 return 0;
130 m_bModified = bModified;
131 return 0;
132}
133
135{
136 if(!m_Category.contains(p))
137 m_Category.push_back(p);
138 return 0;
139}
Parameter interface.
Definition Parameter.h:209
QVector< CParameter * > m_Category
Category.
Definition Parameter.h:340
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:334
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:47
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