File: ImportFilesDialogNew.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 (287 lines) | stat: -rw-r--r-- 10,207 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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// file name            : ImportFilesDialogNew.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 "ImportFilesDialogNew.h"
#include <wx/dir.h>
#include <wx/filefn.h>
#include "pluginmanager.h"
#include "bitmap_loader.h"
#include "importfilessettings.h"
#include "ctags_manager.h"
#include "pluginmanager.h"
#include "windowattrmanager.h"
#include "editor_config.h"
#include <wx/dirdlg.h>

class ImportFilesDlgData : public wxClientData
{
    wxString m_path;
    bool m_isChecked;
    bool m_isDummy;

public:
    ImportFilesDlgData(const wxString& path, bool checked, bool isDummy = false)
        : m_path(path)
        , m_isChecked(checked)
        , m_isDummy(isDummy)
    {
    }

    virtual ~ImportFilesDlgData() {}

    void SetIsChecked(bool isChecked) { this->m_isChecked = isChecked; }
    void SetPath(const wxString& path) { this->m_path = path; }
    bool IsChecked() const { return m_isChecked; }
    const wxString& GetPath() const { return m_path; }
    void SetIsDummy(bool isDummy) { this->m_isDummy = isDummy; }
    bool IsDummy() const { return m_isDummy; }
};

//--------------------------------------------------------------

ImportFilesDialogNew::ImportFilesDialogNew(wxWindow* parent)
    : ImportFilesDialogNewBase(parent)
{
    m_dataview->SetExpanderColumn(m_dataview->GetColumn(1));
    m_dataview->SetIndent(WXC_FROM_DIP(16));
    ImportFilesSettings options;
    if(!EditorConfigST::Get()->ReadObject(wxT("import_dir_options"), &options)) {
        // first time, read the settings from the ctags options
        options.SetFileMask(TagsManagerST::Get()->GetCtagsOptions().GetFileSpec());
        bool noExt = TagsManagerST::Get()->GetCtagsOptions().GetFlags() & CC_PARSE_EXT_LESS_FILES ? true : false;
        size_t flags(0);
        if(noExt) {
            flags |= IFS_INCLUDE_FILES_WO_EXT;
        }
        options.SetFlags(flags);
    }

    options.SetBaseDir(PluginManager::Get()->GetSelectedTreeItemInfo(TreeFileView).m_fileName.GetPath());
    if(options.GetBaseDir().IsEmpty()) {
        options.SetBaseDir(wxGetCwd());
    }

    m_textCtrlDir->ChangeValue(options.GetBaseDir());
    m_textCtrSpec->SetValue(options.GetFileMask());
    m_checkBoxFilesWOExt->SetValue(options.GetFlags() & IFS_INCLUDE_FILES_WO_EXT);
    SetName("ImportFilesDialogNew");
    WindowAttrManager::Load(this);

    CentreOnParent();
    DoBuildTree();
}

ImportFilesDialogNew::~ImportFilesDialogNew()
{

    ImportFilesSettings options;
    options.SetBaseDir(m_textCtrlDir->GetValue());
    options.SetFileMask(m_textCtrSpec->GetValue());

    size_t flags(0);
    if(m_checkBoxFilesWOExt->IsChecked()) flags |= IFS_INCLUDE_FILES_WO_EXT;
    options.SetFlags(flags);
    EditorConfigST::Get()->WriteObject(wxT("import_dir_options"), &options);
}

void ImportFilesDialogNew::DoBuildTree(const wxDataViewItem& parent, const wxDir& dir, bool initialState)
{
    wxString path;
    bool cont = dir.GetFirst(&path, "", wxDIR_DIRS);
    wxBitmap folderBmp = PluginManager::Get()->GetStdIcons()->LoadBitmap("mime/16/folder-yellow");
    while(cont) {

        wxVector<wxVariant> cols;
        cols.push_back(initialState);
        cols.push_back(MakeIconText(path, folderBmp));

        wxDir childDir(dir.GetNameWithSep() + path);
        wxDataViewItem child =
            m_dataviewModel->AppendItem(parent, cols, new ImportFilesDlgData(childDir.GetName(), initialState));

        // Add dummy columns
        if(childDir.IsOpened() && childDir.HasSubDirs()) {
            wxVector<wxVariant> dummyCols;
            dummyCols.push_back(false);
            dummyCols.push_back(MakeIconText("dummy", folderBmp));
            m_dataviewModel->AppendItem(child, dummyCols, new ImportFilesDlgData("", false, true));
        }
        cont = dir.GetNext(&path);
    }
}

void ImportFilesDialogNew::OnDirChanged(wxCommandEvent& event)
{
    wxUnusedVar(event);
    DoBuildTree();
}

void ImportFilesDialogNew::DoBuildTree()
{
    if(!wxFileName::DirExists(m_textCtrlDir->GetValue())) return;
    m_dataviewModel->Clear();

    wxString curpath = m_textCtrlDir->GetValue();
    if(!wxDir::Exists(curpath)) {
        m_textCtrlDir->ChangeValue(::wxGetCwd());
    }

    wxVector<wxVariant> cols;
    cols.push_back(false);
    cols.push_back(MakeIconText(m_textCtrlDir->GetValue(),
                                PluginManager::Get()->GetStdIcons()->LoadBitmap("mime/16/folder-yellow")));

    m_root =
        m_dataviewModel->AppendItem(wxDataViewItem(0), cols, new ImportFilesDlgData(m_textCtrlDir->GetValue(), false));

    // For performance, we add only the direct children of the root node
    wxDir dir(m_textCtrlDir->GetValue());
    if(dir.IsOpened() && dir.HasSubDirs()) {
        DoBuildTree(m_root, dir, false);
    }
    m_dataview->Expand(m_root);
}

void ImportFilesDialogNew::OnValueChanged(wxDataViewEvent& event)
{
    event.Skip();
    wxVector<wxVariant> cols = m_dataviewModel->GetItemColumnsData(event.GetItem());
    if(cols.size() > 1) {
        bool isChecked = cols.at(0).GetBool();
        ImportFilesDlgData* cd =
            reinterpret_cast<ImportFilesDlgData*>(m_dataviewModel->GetClientObject(event.GetItem()));
        if(cd) {
            cd->SetIsChecked(isChecked);
            DoCheckChildren(event.GetItem(), isChecked);
        }
#ifdef __WXGTK__
        Refresh();
#endif
    }
}

wxVariant ImportFilesDialogNew::MakeIconText(const wxString& text, const wxBitmap& bmp) const
{
    wxIcon icn;
    icn.CopyFromBitmap(bmp);
    wxDataViewIconText ict(text, icn);
    wxVariant v;
    v << ict;
    return v;
}

void ImportFilesDialogNew::DoCheckChildren(const wxDataViewItem& parent, bool check)
{
    if(m_dataviewModel->HasChildren(parent)) {
        wxDataViewItemArray children;
        m_dataviewModel->GetChildren(parent, children);
        for(size_t i = 0; i < children.GetCount(); ++i) {

            // First, update the UI by replacing the columns
            wxDataViewItem item = children.Item(i);
            m_dataviewModel->SetValue(wxVariant(check), item, 0);

            // Update the client data
            ImportFilesDlgData* cd = dynamic_cast<ImportFilesDlgData*>(m_dataviewModel->GetClientObject(item));
            if(cd) {
                cd->SetIsChecked(check);
            }

            // Check if this child has children
            if(m_dataviewModel->HasChildren(item)) {
                DoCheckChildren(item, check);
            }
        }
    }
}

void ImportFilesDialogNew::OnItemExpanding(wxDataViewEvent& event)
{
    event.Skip();
    wxDataViewItemArray children;
    if(m_dataviewModel->GetChildren(event.GetItem(), children)) {
        wxDataViewItem child = children.Item(0);
        ImportFilesDlgData* cd = dynamic_cast<ImportFilesDlgData*>(m_dataviewModel->GetClientObject(child));
        if(cd && cd->IsDummy()) {
            cd = dynamic_cast<ImportFilesDlgData*>(m_dataviewModel->GetClientObject(event.GetItem()));
            m_dataviewModel->DeleteItem(child);
            wxDir dir(cd->GetPath());
            DoBuildTree(event.GetItem(), dir, cd->IsChecked());
        }
    }
}

void ImportFilesDialogNew::GetDirectories(wxStringBoolMap_t& dirs) { DoGetCheckedDirs(m_root, dirs); }

void ImportFilesDialogNew::DoGetCheckedDirs(const wxDataViewItem& parent, wxStringBoolMap_t& dirs)
{
    wxDataViewItemArray children;
    bool itemExpanded = false;
    ImportFilesDlgData* cd = dynamic_cast<ImportFilesDlgData*>(m_dataviewModel->GetClientObject(parent));
    if(cd) {
        bool bParentFolderChecked = cd->IsChecked();
        wxString dirname = cd->GetPath();
        bool recurse = false;
        // check if this item was expanded before
        if(m_dataviewModel->HasChildren(parent) && m_dataviewModel->GetChildren(parent, children)) {
            wxDataViewItem child = children.Item(0);
            cd = dynamic_cast<ImportFilesDlgData*>(m_dataviewModel->GetClientObject(child));

            // If the directory is checked and it was never expanded
            // we should recurse into it
            if(cd && cd->IsDummy()) {
                recurse = true;

            } else if(cd) {
                itemExpanded = true;
            }
        }

        if(bParentFolderChecked) {
            dirs.insert(std::make_pair(dirname, recurse));
        }
    }

    // if the parent has children and it was expanded by the user, keep on recursing
    if(!children.IsEmpty() && itemExpanded) {
        for(size_t i = 0; i < children.GetCount(); ++i) {
            DoGetCheckedDirs(children.Item(i), dirs);
        }
    }
}

bool ImportFilesDialogNew::ExtlessFiles() { return m_checkBoxFilesWOExt->IsChecked(); }

wxString ImportFilesDialogNew::GetBaseDir() { return m_textCtrlDir->GetValue(); }

wxString ImportFilesDialogNew::GetFileMask() { return m_textCtrSpec->GetValue(); }
void ImportFilesDialogNew::OnBrowse(wxCommandEvent& event)
{
    wxString new_path = wxDirSelector(_("Select working directory:"), m_textCtrlDir->GetValue(), wxDD_DEFAULT_STYLE,
                                      wxDefaultPosition, this);
    if(new_path.IsEmpty()) return;
    m_textCtrlDir->ChangeValue(new_path);
    DoBuildTree();
}