Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GUI_Tags.cpp « GUI « slic3r « src - github.com/supermerill/SuperSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 162a1c0b081ddd53d9c0113cad8b418e0aa5ae9e (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
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
#include "GUI_Tags.hpp"

#include <stdexcept>
#include <cmath>

#include <wx/sizer.h>

#include <boost/algorithm/string/replace.hpp>


#include "libslic3r/AppConfig.hpp"
#include "libslic3r/Config.hpp"

#include "BitmapCache.hpp"
#include "GUI.hpp"
#include "GUI_App.hpp"
#include "GUI_ObjectList.hpp"
#include "libslic3r/Config.hpp"
#include "I18N.hpp"
#include "GUI_Utils.hpp"
#include "Plater.hpp"
#include "../Utils/MacDarkMode.hpp"
#include "BitmapComboBox.hpp"

namespace Slic3r{
namespace GUI{

// ----------------------------------------------------------------------------
// ModeButton
// ----------------------------------------------------------------------------

ModeButton::ModeButton( wxWindow *          parent,
                        wxWindowID          id,
                        const std::string&  icon_name   /* = ""*/,
                        const wxString&     mode        /* = wxEmptyString*/,
                        const wxSize&       size        /* = wxDefaultSize*/,
                        const wxPoint&      pos         /* = wxDefaultPosition*/) :
    ScalableButton(parent, id, icon_name, mode, size, pos, wxBU_EXACTFIT)
{
    Init(mode);
}

ModeButton::ModeButton(wxWindow* parent,
    const wxString& mode/* = wxEmptyString*/,
    const std::string& icon_name/* = ""*/,
    int                 px_cnt/* = 16*/) :
    ScalableButton(parent, wxID_ANY, ScalableBitmap(parent, icon_name, px_cnt), mode, wxBU_EXACTFIT)
{
    Init(mode);
}

ModeButton::ModeButton(wxWindow* parent,
    const wxString&     mode,
    wxBitmap*           bitmap,
    int                 px_cnt /* = 16*/ ):
    ScalableButton(parent, wxID_ANY, ScalableBitmap(parent, *bitmap, px_cnt), mode, wxBU_EXACTFIT)
{
    Init(mode);
}

void ModeButton::Init(const wxString &mode)
{
    std::string mode_str = std::string(mode.ToUTF8());
    m_tt_focused  = Slic3r::GUI::from_u8((boost::format(_utf8(L("Switch to the %s mode"))) % mode_str).str());
    m_tt_selected = Slic3r::GUI::from_u8((boost::format(_utf8(L("Current mode is %s"))) % mode_str).str());

    SetBitmapMargins(3, 0);

    //button events
    Bind(wxEVT_BUTTON,          &ModeButton::OnButton, this);
    Bind(wxEVT_ENTER_WINDOW,    &ModeButton::OnEnterBtn, this);
    Bind(wxEVT_LEAVE_WINDOW,    &ModeButton::OnLeaveBtn, this);
}

void ModeButton::OnButton(wxCommandEvent& event)
{
    m_is_selected = true;
    focus_button(m_is_selected);

    event.Skip();
}

void ModeButton::SetState(const bool state)
{
    m_is_selected = state;
    focus_button(m_is_selected);
    SetToolTip(state ? m_tt_selected : m_tt_focused);
}

void ModeButton::focus_button(const bool focus)
{
    const wxFont& new_font = focus ? 
                             Slic3r::GUI::wxGetApp().bold_font() : 
                             Slic3r::GUI::wxGetApp().normal_font();

    SetFont(new_font);
#ifdef _WIN32
    GetParent()->Refresh(); // force redraw a background of the selected mode button
#else
    SetForegroundColour(wxSystemSettings::GetColour(focus ? wxSYS_COLOUR_BTNTEXT : 
#if defined (__linux__) && defined (__WXGTK3__)
        wxSYS_COLOUR_GRAYTEXT
#elif defined (__linux__) && defined (__WXGTK2__)
        wxSYS_COLOUR_BTNTEXT
#else 
        wxSYS_COLOUR_BTNSHADOW
#endif    
    ));
#endif /* no _WIN32 */

    Refresh();
    Update();
}


// ----------------------------------------------------------------------------
// ModeSizer
// ----------------------------------------------------------------------------

int mode_icon_px_size()
{
#ifdef __APPLE__
    return 10;
#else
    return 12;
#endif
}


ModeSizer::ModeSizer(wxWindow *parent, int hgap, int max_col) :
    wxFlexGridSizer(3, 0, hgap),
    m_parent(parent),
    m_hgap_unscaled((double)(hgap)/em_unit(parent))
{
    static BitmapCache cache;
    SetFlexibleDirection(wxHORIZONTAL);

    std::vector<std::pair<std::string, std::string>> name_2_color;
    //try to load colors from ui file
    boost::property_tree::ptree tree_colors;
    boost::filesystem::path path_colors = boost::filesystem::path(resources_dir()) / "ui_layout" / "colors.ini";
    try {
        boost::nowide::ifstream ifs;
        ifs.imbue(boost::locale::generator()("en_US.UTF-8"));
        ifs.open(path_colors.string());
        boost::property_tree::read_ini(ifs, tree_colors);

        for (const auto& it : tree_colors) {
            if (boost::starts_with(it.first, "Tag_")) {
                std::string color_code = tree_colors.get<std::string>(it.first);
                if (!color_code.empty()) {
                    std::string tag = it.first.substr(4);
                    name_2_color.emplace_back(tag, color_code[0] == '#' ? color_code : ("#"+ color_code));

                    // get/set into ConfigOptionDef
                    auto it = ConfigOptionDef::names_2_tag_mode.find(tag);
                    if (it == ConfigOptionDef::names_2_tag_mode.end()) {
                        if (ConfigOptionDef::names_2_tag_mode.size() > 62) { //full
                            continue;
                        }
                        ConfigOptionDef::names_2_tag_mode[tag] = (ConfigOptionMode)(((uint64_t)1) << ConfigOptionDef::names_2_tag_mode.size());
                        it = ConfigOptionDef::names_2_tag_mode.find(tag);
                    }
                    m_bt_mode.push_back(it->second);
                }
            }
        }
    }
    catch (const std::ifstream::failure& err) {
        trace(1, (std::string("The color file cannot be loaded. Reason: ") + err.what(), path_colors.string()).c_str());
    }
    catch (const std::runtime_error& err) {
        trace(1, (std::string("Failed loading the color file. Reason: ") + err.what(), path_colors.string()).c_str());
    }
//
//
//    std::vector < std::pair < wxString, std::string >> buttons = {
//        {_L("Simple"),    "mode_simple"},
////        {_(L("Advanced")),  "mode_advanced"},
//        {_L("Advanced") /*_CTX(L_CONTEXT("Advanced", "Mode") , "Mode")*/, "mode_advanced"},
//        {_L("Expert"),    "mode_expert"},
//    };

    auto modebtnfn = [this](wxCommandEvent &event, int mode_idx) {
        Slic3r::GUI::wxGetApp().save_mode(this->m_bt_mode[mode_idx]);
        event.Skip();
    };
    
    m_mode_btns.reserve(name_2_color.size());
    this->SetCols(max_col != 0 ? std::min(max_col, (int)name_2_color.size()) : (int)name_2_color.size());
    for (const auto& button : name_2_color) {
        // create bitmap
        AppConfig::hsv colorToDarken = AppConfig::rgb2hsv(AppConfig::int2rgb(AppConfig::hex2int(button.second)));
        colorToDarken.v *= 0.7;
        std::map<std::string, std::string> color_replace;
        color_replace["#E70000"] = button.second;
        color_replace["#D30000"] = AppConfig::int2hex(AppConfig::rgb2int(AppConfig::hsv2rgb(colorToDarken)));
        int px_cnt = (int)(em_unit(parent) * mode_icon_px_size() * 0.1f + 0.5f);
        wxBitmap* icon = cache.load_svg("mode_expert", 0, (unsigned int)px_cnt, color_replace);
        // create bt
        m_mode_btns.push_back(new ModeButton(parent, _(button.first), icon, px_cnt));
        // add event
        m_mode_btns.back()->Bind(wxEVT_BUTTON, std::bind(modebtnfn, std::placeholders::_1, int(m_mode_btns.size() - 1)));
        Add(m_mode_btns.back());
    }
}

void ModeSizer::SetMode(ConfigOptionMode mode)
{
    for (size_t m = 0; m < m_mode_btns.size(); m++)
        m_mode_btns[m]->SetState( (m_bt_mode[m] & mode) != 0);
}

void ModeSizer::set_items_flag(int flag)
{
    for (wxSizerItem* item : this->GetChildren())
        item->SetFlag(flag);
}

void ModeSizer::set_items_border(int border)
{
    for (wxSizerItem* item : this->GetChildren())
        item->SetBorder(border);
}

void ModeSizer::msw_rescale()
{
    this->SetHGap(std::lround(m_hgap_unscaled * em_unit(m_parent)));
    for (size_t m = 0; m < m_mode_btns.size(); m++)
        m_mode_btns[m]->msw_rescale();
}


} } //namespace Slic3r GUI