summaryrefslogtreecommitdiffstats
path: root/src/ui/dialog/template-load-tab.cpp
blob: 5e3a16a3f1419efc1d4001494bb83094d7628541 (plain)
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
#include "template-load-tab.h"

#include <gtkmm/alignment.h>
#include <iostream>

#include "src/interface.h"
#include "src/file.h"
#include "src/path-prefix.h"
#include "src/preferences.h"
#include "src/inkscape.h"


namespace Inkscape {
namespace UI {
    

TemplateLoadTab::TemplateLoadTab()
    : _keywords_combo(true)
    , _current_keyword("")
{
    set_border_width(10);

    Gtk::Label *title;
    title = manage(new Gtk::Label("Search Tags:"));
    _templates_column.pack_start(*title, Gtk::PACK_SHRINK, 10);
    
    _templates_column.pack_start(_keywords_combo, Gtk::PACK_SHRINK, 0);
    
    title = manage(new Gtk::Label("Templates"));
    _templates_column.pack_start(*title, Gtk::PACK_SHRINK, 10);
    
    title = manage(new Gtk::Label("Selected template"));
    _template_info_column.pack_start(*title, Gtk::PACK_SHRINK, 10);
    
    add(_main_box);
    _main_box.pack_start(_templates_column, Gtk::PACK_SHRINK, 20);
    _main_box.pack_start(_template_info_column, Gtk::PACK_EXPAND_WIDGET, 10);
    
    _templates_column.pack_start(_templates_view, Gtk::PACK_SHRINK, 5);

    Glib::RefPtr<Gtk::TreeSelection> templateSelectionRef =
    _templates_view.get_selection();
    
    templateSelectionRef->signal_changed().connect(
    sigc::mem_fun(*this, &TemplateLoadTab::_displayTemplateInfo));
    
    _keywords_combo.signal_changed().connect(
    sigc::mem_fun(*this, &TemplateLoadTab::_keywordSelected));
    this->show_all();
}


TemplateLoadTab::~TemplateLoadTab()
{
}


void TemplateLoadTab::createTemplate()
{
    std::cout << "Default Template Tab" << std::endl;
}


void TemplateLoadTab::_displayTemplateInfo()
{
    Glib::RefPtr<Gtk::TreeSelection> templateSelectionRef = _templates_view.get_selection();
    if (templateSelectionRef->get_selected()) {
        _current_template = (*templateSelectionRef->get_selected())[_templates_columns.textValue];
    }
}


void TemplateLoadTab::_initKeywordsList()
{
    _keywords_combo.append("All");
    
    for (int i = 0 ; i < 10 ; ++i) {
        _keywords_combo.append( "Keyword" + Glib::ustring::format(i));
    }
}


void TemplateLoadTab::_initLists()
{
    _templates_ref = Gtk::ListStore::create(_templates_columns);
    _templates_view.set_model(_templates_ref);
    _templates_view.append_column("", _templates_columns.textValue);
    _templates_view.set_headers_visible(false);
    
    _initKeywordsList();
    _refreshTemplatesList();
}


void TemplateLoadTab::_keywordSelected()
{
    _current_keyword = _keywords_combo.get_active_text();
    _refreshTemplatesList();
}


void TemplateLoadTab::_refreshTemplatesList()
{
     _templates_ref->clear();
    
    for (std::map<Glib::ustring, TemplateData>::iterator it = _templates.begin() ; it != _templates.end() ; ++it) {
        Gtk::TreeModel::iterator iter = _templates_ref->append();
        Gtk::TreeModel::Row row = *iter;
        row[_templates_columns.textValue]  = it->first;
    }
} 


void TemplateLoadTab::_loadTemplates()
{
    // user's local dir
    _getTemplatesFromDir(profile_path("templates") + _loading_path);

    // system templates dir
    _getTemplatesFromDir(INKSCAPE_TEMPLATESDIR + _loading_path);
}


TemplateLoadTab::TemplateData TemplateLoadTab::_processTemplateFile(Glib::ustring path)
{
    TemplateData result;
    result.path = path;
    result.display_name = Glib::path_get_basename(path);
    result.short_description = "LaLaLaLa";
    result.author = "JAASDASD";
    
    return result;
}


void TemplateLoadTab::_getTemplatesFromDir(Glib::ustring path)
{
    if ( !Glib::file_test(path, Glib::FILE_TEST_EXISTS) ||
         !Glib::file_test(path, Glib::FILE_TEST_IS_DIR))
        return;
    
    Glib::Dir dir(path);
    path += "/";
    Glib::ustring file = path + dir.read_name();
    while (file != path){
        if (Glib::str_has_suffix(file, ".svg")){
            TemplateData tmp = _processTemplateFile(file);
            _templates[Glib::path_get_basename(file)] = tmp;
        }
        file = path + dir.read_name();
    }
}

}
}