File: configuration_manager_dlg.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 (325 lines) | stat: -rw-r--r-- 11,959 bytes parent folder | download
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2008 by Eran Ifrah
// file name            : configuration_manager_dlg.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 "windowattrmanager.h"
#include "configuration_manager_dlg.h"
#include "manager.h"
#include "new_configuration_dlg.h"
#include "edit_configuration.h"
#include "edit_workspace_conf_dlg.h"
#include "macros.h"
#include <algorithm>
#include <wx/wupdlock.h>

//----------------------------------------------------------------------------
// Configuration Manager
//----------------------------------------------------------------------------

static int wxStringCmpFunc(const wxString& item1, const wxString& item2) { return item1.CmpNoCase(item2); }

ConfigurationManagerDlg::ConfigurationManagerDlg(wxWindow* parent)
    : ConfigManagerBaseDlg(parent)
    , m_dirty(false)
{
    PopulateConfigurations();
    InitDialog();
    CentreOnParent();
    SetName("ConfigurationManagerDlg");
    WindowAttrManager::Load(this);
}

ConfigEntry ConfigurationManagerDlg::DoCreateChoicesForProject(
    const wxString& projectName, const wxString& selectedConf, wxPGProperty* prop)
{
    wxPGChoices choices;

    // Get all configuration of the project
    int defaultValue = 0;
    int counter = 0;
    ProjectSettingsPtr settings = ManagerST::Get()->GetProjectSettings(projectName);
    if(settings) {
        ProjectSettingsCookie cookie;
        BuildConfigPtr bldConf = settings->GetFirstBuildConfiguration(cookie);
        while(bldConf) {
            choices.Add(bldConf->GetName());
            if(bldConf->GetName() == selectedConf) {
                defaultValue = counter;
            }
            bldConf = settings->GetNextBuildConfiguration(cookie);
            ++counter;
        }
    }

    choices.Add(wxGetTranslation(clCMD_NEW));
    choices.Add(wxGetTranslation(clCMD_EDIT));
    wxPGProperty* p = prop;
    if(!p) {
        p = m_pgMgr->Append(new wxEnumProperty(projectName, wxPG_LABEL, choices, defaultValue));
    } else {
        p->SetChoices(choices);
        p->SetValueFromString(selectedConf);
    }
    ConfigEntry entry;
    entry.project = projectName;
    entry.projectSettings = settings;
    entry.choiceControl = p;
    if(m_projectPropertiesMap.count(projectName)) {
        m_projectPropertiesMap.erase(projectName);
    }
    m_projectPropertiesMap.insert(std::make_pair(projectName, entry));
    if(m_projectPropertiesMap.size() % 2 == 0) {
        p->SetBackgroundColour("#E0E0E0");
    }
    return entry;
}

void ConfigurationManagerDlg::AddEntry(const wxString& projectName, const wxString& selectedConf)
{
    DoCreateChoicesForProject(projectName, selectedConf);
}

void ConfigurationManagerDlg::PopulateConfigurations()
{
    m_pgMgr->GetGrid()->Clear();
    // popuplate the configurations
    BuildMatrixPtr matrix = ManagerST::Get()->GetWorkspaceBuildMatrix();
    if(!matrix) {
        return;
    }

    wxWindowUpdateLocker locker(this);
    m_projectPropertiesMap.clear();

    std::list<WorkspaceConfigurationPtr> configs = matrix->GetConfigurations();
    m_choiceConfigurations->Clear();
    std::for_each(configs.begin(), configs.end(),
        [&](WorkspaceConfigurationPtr config) { m_choiceConfigurations->Append(config->GetName()); });

    // append the 'New' & 'Delete' commands
    m_choiceConfigurations->Append(wxGetTranslation(clCMD_NEW));
    m_choiceConfigurations->Append(wxGetTranslation(clCMD_EDIT));

    int sel = m_choiceConfigurations->FindString(matrix->GetSelectedConfigurationName());
    if(sel != wxNOT_FOUND) {
        m_choiceConfigurations->SetSelection(sel);
    } else if(m_choiceConfigurations->GetCount() > 2) {
        m_choiceConfigurations->SetSelection(2);
    } else {
        m_choiceConfigurations->Append(_("Debug"));
        m_choiceConfigurations->SetSelection(2);
    }

    // keep the current workspace configuration
    m_currentWorkspaceConfiguration = m_choiceConfigurations->GetStringSelection();

    wxArrayString projects;
    ManagerST::Get()->GetProjectList(projects);
    projects.Sort(wxStringCmpFunc);
    for(size_t i = 0; i < projects.GetCount(); i++) {
        wxString selConf = matrix->GetProjectSelectedConf(matrix->GetSelectedConfigurationName(), projects.Item(i));
        AddEntry(projects.Item(i), selConf);
    }
}

void ConfigurationManagerDlg::LoadWorkspaceConfiguration(const wxString& confName)
{
    BuildMatrixPtr matrix = ManagerST::Get()->GetWorkspaceBuildMatrix();
    if(!matrix) {
        return;
    }

    m_choiceConfigurations->SetStringSelection(confName);
    std::map<wxString, ConfigEntry>::iterator iter = m_projectPropertiesMap.begin();
    for(; iter != m_projectPropertiesMap.end(); ++iter) {
        wxString selConf = matrix->GetProjectSelectedConf(confName, iter->second.project);
        iter->second.choiceControl->SetValueFromString(selConf);
    }
}

void ConfigurationManagerDlg::LoadProjectConfiguration(const wxString& projectName)
{
    std::map<wxString, ConfigEntry>::iterator iter = m_projectPropertiesMap.begin();
    for(; iter != m_projectPropertiesMap.end(); iter++) {
        if(iter->second.project == projectName) {
            wxPGProperty* prop = iter->second.choiceControl;
            ProjectSettingsPtr proSet = ManagerST::Get()->GetProjectSettings(projectName);
            if(proSet) {
                // select the build configuration according to the build matrix
                BuildMatrixPtr matrix = ManagerST::Get()->GetWorkspaceBuildMatrix();
                if(!matrix) {
                    return;
                }
                wxString configName =
                    matrix->GetProjectSelectedConf(m_choiceConfigurations->GetStringSelection(), projectName);
                DoCreateChoicesForProject(projectName, configName, prop);
                return;
            }
        }
    }
}

void ConfigurationManagerDlg::InitDialog() {}

void ConfigurationManagerDlg::OnWorkspaceConfigSelected(wxCommandEvent& event)
{
    if(event.GetString() == wxGetTranslation(clCMD_NEW)) {
        OnButtonNew(event);
    } else if(event.GetString() == wxGetTranslation(clCMD_EDIT)) {
        // popup the delete dialog for configurations
        EditWorkspaceConfDlg* dlg = new EditWorkspaceConfDlg(this);
        dlg->ShowModal();
        dlg->Destroy();

        // once done, restore dialog
        PopulateConfigurations();
    } else {
        if(m_dirty) {
            if(wxMessageBox(
                   wxString::Format(
                       _("Settings for workspace configuration '%s' have changed, would you like to save them?"),
                       m_currentWorkspaceConfiguration.GetData()),
                   _("CodeLite"), wxYES_NO | wxICON_QUESTION) == wxYES) {
                SaveCurrentSettings();
            }
            m_dirty = false;
        }
        m_currentWorkspaceConfiguration = event.GetString();
        LoadWorkspaceConfiguration(event.GetString());
    }
}

void ConfigurationManagerDlg::OnButtonNew(wxCommandEvent& event)
{
    wxUnusedVar(event);
    wxTextEntryDialog* dlg = new wxTextEntryDialog(this, _("Enter New Configuration Name:"), _("New Configuration"));
    if(dlg->ShowModal() == wxID_OK) {
        wxString value = dlg->GetValue();
        TrimString(value);
        if(value.IsEmpty() == false) {
            BuildMatrixPtr matrix = ManagerST::Get()->GetWorkspaceBuildMatrix();
            if(!matrix) {
                return;
            }

            WorkspaceConfigurationPtr conf(new WorkspaceConfiguration(NULL));
            conf->SetName(value);
            conf->SetConfigMappingList(GetCurrentSettings());
            matrix->SetConfiguration(conf);
            // save changes
            ManagerST::Get()->SetWorkspaceBuildMatrix(matrix);
        }
    }
    PopulateConfigurations();
    dlg->Destroy();
}

void ConfigurationManagerDlg::OnButtonApply(wxCommandEvent& event)
{
    wxUnusedVar(event);
    SaveCurrentSettings();
}

WorkspaceConfiguration::ConfigMappingList ConfigurationManagerDlg::GetCurrentSettings()
{
    // return the current settings as described by the dialog
    WorkspaceConfiguration::ConfigMappingList list;

    std::map<wxString, ConfigEntry>::iterator iter = m_projectPropertiesMap.begin();
    for(; iter != m_projectPropertiesMap.end(); ++iter) {

        wxString value = iter->second.choiceControl->GetValueAsString();
        if(value != wxGetTranslation(clCMD_NEW) && value != wxGetTranslation(clCMD_EDIT)) {
            ConfigMappingEntry entry(iter->second.project, value);
            list.push_back(entry);
        }
    }
    return list;
}

void ConfigurationManagerDlg::OnButtonOK(wxCommandEvent& event)
{
    OnButtonApply(event);
    EndModal(wxID_OK);
}

void ConfigurationManagerDlg::SaveCurrentSettings()
{
    m_currentWorkspaceConfiguration = m_currentWorkspaceConfiguration.Trim().Trim(false);

    BuildMatrixPtr matrix = ManagerST::Get()->GetWorkspaceBuildMatrix();
    if(!matrix) {
        return;
    }

    matrix->SetSelectedConfigurationName(m_currentWorkspaceConfiguration);

    WorkspaceConfigurationPtr conf = matrix->GetConfigurationByName(m_currentWorkspaceConfiguration);
    if(!conf) {
        // create new configuration
        conf = new WorkspaceConfiguration(NULL);
        conf->SetName(m_currentWorkspaceConfiguration);
        matrix->SetConfiguration(conf);
    }

    conf->SetConfigMappingList(GetCurrentSettings());

    // save changes
    ManagerST::Get()->SetWorkspaceBuildMatrix(matrix);
    m_dirty = false;
}

void ConfigurationManagerDlg::OnButtonApplyUI(wxUpdateUIEvent& event) { event.Enable(m_dirty); }

ConfigurationManagerDlg::~ConfigurationManagerDlg() {}

void ConfigurationManagerDlg::OnValueChanged(wxPropertyGridEvent& event)
{
    wxString projectName = event.GetProperty()->GetLabel();
    wxString selection = event.GetProperty()->GetValueAsString();
    if(selection == wxGetTranslation(clCMD_NEW)) {
        // popup the 'New Configuration' dialog
        NewConfigurationDlg* dlg = new NewConfigurationDlg(this, projectName);
        dlg->ShowModal();
        dlg->Destroy();

        // repopulate the configurations
        LoadProjectConfiguration(projectName);

        // clCMD_NEW does not mark the page as dirty !

    } else if(selection == wxGetTranslation(clCMD_EDIT)) {
        EditConfigurationDialog* dlg = new EditConfigurationDialog(this, projectName);
        dlg->ShowModal();
        dlg->Destroy();

        // repopulate the configurations
        LoadProjectConfiguration(projectName);

        m_dirty = true;

    } else {
        // just mark the page as dirty
        m_dirty = true;
    }
}