ckb-next  v0.2.8 at branch master
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
kbprofile.cpp
Go to the documentation of this file.
1 #include "kbprofile.h"
2 #include "kb.h"
3 
4 KbProfile::KbProfile(Kb* parent, const KeyMap& keyMap, const KbProfile& other) :
5  QObject(parent), _currentMode(0), _name(other._name), _id(other._id), _keyMap(keyMap), _needsSave(true)
6 {
7  foreach(KbMode* mode, other.modes()){
8  KbMode* newMode = new KbMode(parent, keyMap, *mode);
9  if(!_currentMode || mode == other._currentMode)
10  _currentMode = newMode;
11  _modes.append(newMode);
12  }
13 }
14 
15 KbProfile::KbProfile(Kb* parent, const KeyMap& keyMap, const QString& guid, const QString& modified) :
16  QObject(parent), _currentMode(0), _id(guid, modified.toUInt(0, 16)), _keyMap(keyMap), _needsSave(true)
17 {
18  if(_id.guid.isNull())
19  _id.guid = QUuid::createUuid();
20 }
21 
22 KbProfile::KbProfile(Kb* parent, const KeyMap& keyMap, CkbSettings& settings, const QString& guid) :
23  QObject(parent), _currentMode(0), _id(guid, 0), _keyMap(keyMap), _needsSave(false)
24 {
25  // Load data from preferences
26  SGroup group(settings, guid);
27  _name = settings.value("Name").toString().trimmed();
28  if(_name == "")
29  _name = "Unnamed";
30  _id.modifiedString(settings.value("Modified").toString());
31  if(settings.contains("HwModified"))
32  _id.hwModifiedString(settings.value("HwModified").toString());
33  else
35  QUuid current = settings.value("CurrentMode").toString().trimmed();
36  // Load modes
37  uint count = settings.value("ModeCount").toUInt();
38  for(uint i = 0; i < count; i++){
39  SGroup group(settings, QString::number(i));
40  KbMode* mode = new KbMode(parent, _keyMap, settings);
41  _modes.append(mode);
42  // Set currentMode to the mode matching the current GUID, or the first mode in case it's not found
43  if(current == mode->id().guid || !_currentMode)
44  _currentMode = mode;
45  }
46 }
47 
48 void KbProfile::save(CkbSettings& settings){
49  _needsSave = false;
50  _id.newModified();
51  // Save data to preferences
52  SGroup group(settings, id().guidString());
53  settings.setValue("Name", name());
54  settings.setValue("Modified", _id.modifiedString());
55  settings.setValue("HwModified", _id.hwModifiedString());
56  if(_currentMode)
57  settings.setValue("CurrentMode", _currentMode->id().guidString());
58  // Save modes
59  uint count = modeCount();
60  settings.setValue("ModeCount", count);
61  for(uint i = 0; i < count; i++){
62  SGroup group(settings, QString::number(i));
63  KbMode* mode = _modes.at(i);
64  mode->save(settings);
65  }
66 }
67 
68 bool KbProfile::needsSave() const {
69  if(_needsSave)
70  return true;
71  foreach(KbMode* mode, _modes){
72  if(mode->needsSave())
73  return true;
74  }
75  return false;
76 }
77 
79  _needsSave = true;
80  _id = UsbId();
81  foreach(KbMode* mode, _modes)
82  mode->newId();
83 }
84 
85 void KbProfile::keyMap(const KeyMap& newKeyMap){
86  _keyMap = newKeyMap;
87  foreach(KbMode* mode, _modes)
88  mode->keyMap(newKeyMap);
90 }
KbMode * _currentMode
Definition: kbprofile.h:58
QString _name
Definition: kbprofile.h:59
void setValue(const QString &key, const QVariant &value)
const ModeList & modes() const
Definition: kbprofile.h:42
UsbId & id()
Definition: kbmode.h:52
QUuid guid
Definition: kbmode.h:12
rgb * current
Definition: main.c:46
QString modifiedString() const
Definition: kbmode.h:22
bool needsSave() const
Definition: kbprofile.cpp:68
KbProfile(Kb *parent, const KeyMap &keyMap, const KbProfile &other)
Definition: kbprofile.cpp:4
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
bool _needsSave
Definition: kbprofile.h:63
KeyMap _keyMap
Definition: kbprofile.h:61
bool contains(const QString &key) const
Definition: kbmode.h:11
ModeList _modes
Definition: kbprofile.h:62
QString guidString() const
Definition: kbmode.h:20
void save(CkbSettings &settings)
Definition: kbprofile.cpp:48
quint32 modified
Definition: kbmode.h:13
UsbId _id
Definition: kbprofile.h:60
void keyMap(const KeyMap &keyMap)
Definition: kbmode.cpp:60
bool needsSave() const
Definition: kbmode.cpp:78
Definition: kbmode.h:36
int modeCount() const
Definition: kbprofile.h:49
Definition: kb.h:11
void newModified()
Definition: kbmode.h:29
void setNeedsUpdate()
Definition: kbprofile.h:66
quint32 hwModified
Definition: kbmode.h:14
const KeyMap & keyMap() const
Definition: kbprofile.h:37
Definition: keymap.h:49
QString name() const
Definition: kbprofile.h:28
void save(CkbSettings &settings)
Definition: kbmode.cpp:66
void newId()
Definition: kbprofile.cpp:78
void newId()
Definition: kbmode.cpp:52
QString hwModifiedString() const
Definition: kbmode.h:24