File: tasks_find_what_dlg.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, 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 (159 lines) | stat: -rw-r--r-- 5,163 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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// file name            : tasks_find_what_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 "tasks_find_what_dlg.h"
#include <wx/msgdlg.h>
#include <wx/regex.h>
#include "editor_config.h"
#include "taskspaneldata.h"
#include "windowattrmanager.h"

class TaskData : public wxClientData
{
public:
    wxString m_regex;

public:
    TaskData(const wxString& regex)
        : m_regex(regex)
    {
    }
    virtual ~TaskData() {}
};

TasksFindWhatDlg::TasksFindWhatDlg(wxWindow* parent)
    : TasksFindWhatDlgBase(parent)
{
    m_list->InsertColumn(0, _("Enabled"));
    m_list->InsertColumn(1, _("Task"));
    m_list->SetColumnWidth(1, 200);

    // Load all info from disk
    TasksPanelData data;
    EditorConfigST::Get()->ReadObject(wxT("TasksPanelData"), &data);

    wxStringMap_t::const_iterator iter = data.GetTasks().begin();
    for(; iter != data.GetTasks().end(); iter++) {
        DoAddLine(iter->first, iter->second, data.GetEnabledItems().Index(iter->first) != wxNOT_FOUND);
    }
    SetName("TasksFindWhatDlg");
    WindowAttrManager::Load(this);
}

TasksFindWhatDlg::~TasksFindWhatDlg() {}

void TasksFindWhatDlg::OnNewTask(wxCommandEvent& event)
{
    NewTaskDialog dlg(this);
    WindowAttrManager::Load(this);

    dlg.SetLabel(_("New Task"));
    if(dlg.ShowModal() == wxID_OK) {
        wxRegEx re(dlg.m_regex->GetValue());
        if(re.IsValid() == false) {
            wxMessageBox(wxString::Format(_("'%s' is not a valid regular expression"), dlg.m_regex->GetValue().c_str()),
                         _("CodeLite"),
                         wxICON_WARNING | wxOK);
            return;
        }
        DoAddLine(dlg.m_name->GetValue(), dlg.m_regex->GetValue(), true);
    }
}

void TasksFindWhatDlg::OnDeleteTask(wxCommandEvent& event)
{
    int selection = m_list->GetSelection();
    if(selection == wxNOT_FOUND) return;

    int answer =
        wxMessageBox(_("Are you sure you want to delete this entry?"), _("Confirm"), wxICON_QUESTION | wxYES_NO);
    if(answer == wxYES) {
        m_list->DeleteItem(selection);
    }
}

void TasksFindWhatDlg::OnDeleteTaskUI(wxUpdateUIEvent& event)
{
    event.Enable(m_list->GetItemCount() && m_list->GetSelection() != wxNOT_FOUND);
}

void TasksFindWhatDlg::OnEditTask(wxCommandEvent& event)
{
    int selection = m_list->GetSelection();
    if(selection != wxNOT_FOUND) {

        NewTaskDialog dlg(this);
        WindowAttrManager::Load(this);

        dlg.SetLabel(_("Edit Task"));
        dlg.m_name->SetValue(m_list->GetText(selection, 1));

        TaskData* data = (TaskData*)m_list->GetItemData(selection);
        dlg.m_regex->SetValue(data->m_regex);
        if(dlg.ShowModal() == wxID_OK) {
            m_list->SetTextColumn(selection, 1, dlg.m_name->GetValue());
            m_list->SetItemClientData(selection, new TaskData(dlg.m_regex->GetValue()));
        }
    }
}

void TasksFindWhatDlg::OnEditTaskUI(wxUpdateUIEvent& event)
{
    event.Enable(m_list->GetItemCount() && m_list->GetSelection() != wxNOT_FOUND);
}

void TasksFindWhatDlg::DoAddLine(const wxString& name, const wxString& regex, bool enabled)
{
    long item = m_list->AppendRow();
    m_list->SetCheckboxRow(item, enabled);
    m_list->SetTextColumn(item, 1, name);
    m_list->SetItemClientData(item, new TaskData(regex));
}

void TasksFindWhatDlg::DoSaveList()
{
    // Save all items
    TasksPanelData data;
    wxStringMap_t items;
    wxArrayString enabledItems;

    for(int i = 0; i < m_list->GetItemCount(); i++) {
        wxString name = m_list->GetText(i, 1);
        TaskData* clientData = (TaskData*)m_list->GetItemData(i);
        items[name] = clientData->m_regex;

        if(m_list->IsChecked(i)) enabledItems.Add(name);
    }

    data.SetEnabledItems(enabledItems);
    data.SetTasks(items);
    EditorConfigST::Get()->WriteObject(wxT("TasksPanelData"), &data);
}

void TasksFindWhatDlg::OnButtonOk(wxCommandEvent& event)
{
    DoSaveList();
    event.Skip();
}