File: tweaks_settings.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (191 lines) | stat: -rw-r--r-- 5,644 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// file name            : tweaks_settings.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include "tweaks_settings.h"
#include <wx/settings.h>
#include "workspace.h"

#define TWEAKS_CONF_FILE "tweaks-settings"

// --------------------------------------------------------
// TweaksSettings
// --------------------------------------------------------

TweaksSettings::TweaksSettings()
    : clConfigItem(TWEAKS_CONF_FILE)
    , m_enableTweaks(false)
    , m_flags(0)
{
}

TweaksSettings::~TweaksSettings()
{
}

void TweaksSettings::FromJSON(const JSONElement& json)
{
    m_projects.clear();
    m_globalBgColour = json.namedObject("m_globalBgColour").toColour();
    m_globalFgColour = json.namedObject("m_globalFgColour").toColour();
    m_enableTweaks = json.namedObject("m_enableTweaks").toBool(false);
    m_flags = json.namedObject("m_flags").toSize_t();
    
    JSONElement arr = json.namedObject("projects");
    int size = arr.arraySize();
    for(int i=0; i<size; ++i) {
        ProjectTweaks tw;
        tw.FromJSON( arr.arrayItem(i) );
        m_projects.insert( std::make_pair(tw.GetProjectName(), tw) );
    }
}

JSONElement TweaksSettings::ToJSON() const
{
    JSONElement e = JSONElement::createObject(GetName());
    
    e.addProperty("m_globalBgColour", m_globalBgColour);
    e.addProperty("m_globalFgColour", m_globalFgColour);
    e.addProperty("m_enableTweaks", m_enableTweaks);
    e.addProperty("m_flags", m_flags);
    JSONElement arr = JSONElement::createArray("projects");
    e.append( arr );
    
    ProjectTweaks::Map_t::const_iterator iter = m_projects.begin();
    for( ; iter != m_projects.end(); ++iter ) {
        arr.arrayAppend( iter->second.ToJSON() );
    }
    return e;
}

TweaksSettings& TweaksSettings::Load()
{
    // Get the file name
    if(!clCxxWorkspaceST::Get()->IsOpen()) {
        return *this;
    }
    
    wxFileName fn(clCxxWorkspaceST::Get()->GetPrivateFolder(), "tweaks.conf");
    clConfig conf( fn.GetFullPath() );
    conf.ReadItem( this );
    return *this;
}

void TweaksSettings::Save()
{
    if(!clCxxWorkspaceST::Get()->IsOpen()) {
        return;
    }
    wxFileName fn(clCxxWorkspaceST::Get()->GetPrivateFolder(), "tweaks.conf");
    clConfig conf( fn.GetFullPath() );
    conf.WriteItem( this );
}

const ProjectTweaks& TweaksSettings::GetProjectTweaks(const wxString& project) const
{
    if ( m_projects.count(project) ) {
        return m_projects.find(project)->second;
    }
    static ProjectTweaks DUMMY;
    return DUMMY;
}

ProjectTweaks& TweaksSettings::GetProjectTweaks(const wxString& project)
{
    if ( m_projects.count(project) == 0 ) {
        // add an entry
        ProjectTweaks pt;
        pt.SetProjectName(project);
        m_projects.insert( std::make_pair(project, pt) );
    }
    return m_projects.find( project )->second;
}

void TweaksSettings::UpdateProject(const ProjectTweaks& pt)
{
    DeleteProject( pt.GetProjectName() );
    m_projects.insert( std::make_pair(pt.GetProjectName(), pt) );
}

void TweaksSettings::Clear()
{
    m_projects.clear();
    ResetColours();
    m_enableTweaks = false;
    m_flags = 0x0;
}

void TweaksSettings::DeleteProject(const wxString& name)
{
    if ( m_projects.count(name) ) {
        m_projects.erase(name);
    }
}

void TweaksSettings::ResetColours()
{
    m_globalBgColour = wxColour();
    m_globalFgColour = wxColour();
    ProjectTweaks::Map_t::iterator iter = m_projects.begin();
    for(; iter != m_projects.end(); ++iter ) {
        iter->second.ResetColours();
    }
}

// --------------------------------------------------------
// ProjectTweaks
// --------------------------------------------------------

ProjectTweaks::ProjectTweaks()
    : clConfigItem("")
{
}

ProjectTweaks::~ProjectTweaks()
{
}

void ProjectTweaks::FromJSON(const JSONElement& json)
{
    m_tabBgColour = json.namedObject("m_tabBgColour").toColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
    m_tabFgColour = json.namedObject("m_tabFgColour").toColour(*wxBLACK);
    m_projectName = json.namedObject("m_projectName").toString();
    m_bitmapFilename = json.namedObject("m_bitmapFilename").toString();
}

JSONElement ProjectTweaks::ToJSON() const
{
    JSONElement e = JSONElement::createObject();
    e.addProperty("m_tabFgColour",      m_tabFgColour);
    e.addProperty("m_tabBgColour",      m_tabBgColour);
    e.addProperty("m_projectName",      m_projectName);
    e.addProperty("m_bitmapFilename",   m_bitmapFilename);
    return e;
}

void ProjectTweaks::ResetColours()
{
    m_tabBgColour = wxColour();
    m_tabFgColour = wxColour();
}