~grabli66-r/+junk/translator

« back to all changes in this revision

Viewing changes to src/TranslatorWindow.vala

  • Committer: grabli66 at gmail
  • Date: 2014-10-04 10:51:37 UTC
  • Revision ID: grabli66@gmail.com-20141004105137-7ytpw5xrzymxov6s
+ init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
public class TranslateWindow : Gtk.ApplicationWindow {
 
2
    private TranslateService service;
 
3
 
 
4
    private Gtk.HeaderBar header;
 
5
    private Gtk.Button changeButton;
 
6
    private Gtk.TextView topText;
 
7
    private Gtk.TextView bottomText;
 
8
    private Gtk.ComboBox leftLangCombo;
 
9
    private Gtk.ComboBox rightLangCombo;
 
10
    private Gtk.ListStore langStore;
 
11
    
 
12
    private static int DEFAULT_WIDTH = 0;
 
13
    private static int DEFAULT_HEIGHT = 640;
 
14
    
 
15
    private LangInfo[] langs;
 
16
        
 
17
    private string leftLang;
 
18
    private string rightLang;
 
19
    
 
20
    public TranslateWindow() {
 
21
        langs = GlobalSettings.getLangs();
 
22
    
 
23
        service = new TranslateService();
 
24
        service.result.connect(onTranslate);
 
25
    
 
26
        this.window_position = Gtk.WindowPosition.CENTER;
 
27
        this.set_gravity(Gdk.Gravity.CENTER);
 
28
        this.set_resizable(false);
 
29
        
 
30
        Gdk.RGBA bgColor = Gdk.RGBA();
 
31
        bgColor.red = 1;
 
32
        bgColor.green = 1;
 
33
        bgColor.blue = 1;
 
34
        bgColor.alpha = 1;        
 
35
        
 
36
        header = new Gtk.HeaderBar ();
 
37
        header.set_show_close_button (true);                                
 
38
                                
 
39
        leftLangCombo = new Gtk.ComboBox();
 
40
        var renderer = new Gtk.CellRendererText ();
 
41
                leftLangCombo.pack_start (renderer, true);
 
42
                leftLangCombo.add_attribute (renderer, "text", 0);
 
43
                leftLangCombo.active = 0;
 
44
                                
 
45
                
 
46
                rightLangCombo = new Gtk.ComboBox(); 
 
47
        rightLangCombo.set_margin_right(10);
 
48
                rightLangCombo.pack_start (renderer, true);
 
49
                rightLangCombo.add_attribute (renderer, "text", 0);
 
50
                rightLangCombo.active = 0;                              
 
51
        
 
52
        changeButton = new Gtk.Button();        
 
53
        changeButton.set_image(Assets.getImage("images/loop.svg"));
 
54
        changeButton.set_margin_left(20);        
 
55
        changeButton.clicked.connect(onSwap);
 
56
        
 
57
        header.pack_start(leftLangCombo);
 
58
        header.pack_end(rightLangCombo);
 
59
        header.set_custom_title(changeButton);
 
60
        
 
61
        this.set_titlebar (header);
 
62
        this.set_size_request (DEFAULT_WIDTH, DEFAULT_HEIGHT); 
 
63
        rightLangCombo.set_size_request (110, 30);
 
64
        leftLangCombo.set_size_request (110, 30);
 
65
        
 
66
        var paned = new Gtk.Paned(Gtk.Orientation.VERTICAL);
 
67
        paned.override_background_color(Gtk.StateFlags.NORMAL, bgColor);
 
68
        var fd = new Pango.FontDescription();
 
69
        fd.set_size(11000);
 
70
        
 
71
        topText = new Gtk.TextView();        
 
72
        topText.set_margin_left(7);
 
73
        topText.set_margin_top(7);
 
74
        topText.set_margin_right(7);
 
75
        topText.override_font(fd);
 
76
        topText.set_wrap_mode(Gtk.WrapMode.WORD);
 
77
        topText.buffer.changed.connect(onUpdate);
 
78
        var topScroll = new Gtk.ScrolledWindow (null, null);       
 
79
        topScroll.set_policy (Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
 
80
        topScroll.set_size_request(100, 30);
 
81
        topScroll.add (topText);
 
82
        
 
83
        bottomText = new Gtk.TextView();
 
84
        bottomText.set_editable(false);
 
85
        bottomText.set_margin_top(7);
 
86
        bottomText.set_margin_right(7);
 
87
        bottomText.set_margin_left(7);
 
88
        bottomText.override_font(fd);
 
89
        bottomText.set_cursor_visible(false);
 
90
        bottomText.set_wrap_mode(Gtk.WrapMode.WORD);
 
91
        
 
92
        var bottomScroll = new Gtk.ScrolledWindow (null, null);       
 
93
        bottomScroll.set_policy (Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
 
94
        bottomScroll.add (bottomText);
 
95
        
 
96
        paned.pack1(topScroll, true, true);
 
97
        paned.pack2(bottomScroll, true, true);
 
98
        
 
99
        this.add(paned);                 
 
100
        
 
101
        populateLangs();        
 
102
    }
 
103
    
 
104
    private void populateLangs() {
 
105
        langStore = new Gtk.ListStore (2, typeof (string), typeof (string));
 
106
                Gtk.TreeIter iter;
 
107
        
 
108
        foreach (var l in langs) {
 
109
            langStore.append (out iter);
 
110
            langStore.@set (iter, 0, l.name, 1, l.id);
 
111
        }
 
112
        
 
113
        leftLangCombo.set_model(langStore);
 
114
        rightLangCombo.set_model(langStore);                
 
115
        
 
116
        leftLangCombo.set_id_column(1);
 
117
        rightLangCombo.set_id_column(1);
 
118
        
 
119
        leftLangCombo.active = 0;
 
120
        rightLangCombo.active = 1;
 
121
        
 
122
        leftLangCombo.changed.connect(onLeftComboChange);
 
123
                rightLangCombo.changed.connect(onRightComboChange);
 
124
                                
 
125
        leftLang = getLeftId();        
 
126
        rightLang = getRightId();            
 
127
    }
 
128
    
 
129
    private string getLeftId() {
 
130
        Value val;
 
131
        Gtk.TreeIter iter;
 
132
        leftLangCombo.get_active_iter (out iter);
 
133
        langStore.get_value(iter, 1, out val);
 
134
        return (string)val;
 
135
    }
 
136
    
 
137
    private string getRightId() {
 
138
        Value val;
 
139
        Gtk.TreeIter iter;
 
140
        rightLangCombo.get_active_iter (out iter);
 
141
        langStore.get_value(iter, 1, out val);
 
142
        return (string)val;
 
143
    }
 
144
    
 
145
    private void onLangChange(bool isRight) {
 
146
        var leftId = getLeftId();
 
147
        var rightId = getRightId();        
 
148
        
 
149
        var needUpdate = true;                    
 
150
        
 
151
        if (leftId == rightId) {
 
152
            needUpdate = false;
 
153
            if (isRight) {
 
154
                leftLang = rightLang;
 
155
                leftLangCombo.set_active_id(rightLang);
 
156
            } else {
 
157
                rightLang = leftLang;
 
158
                rightLangCombo.set_active_id(leftLang);
 
159
            }
 
160
            
 
161
            topText.buffer.text = bottomText.buffer.text;           
 
162
        }                
 
163
        
 
164
        if (needUpdate) {
 
165
            leftLang = leftId;
 
166
            rightLang = rightId; 
 
167
            onUpdate();   
 
168
        }
 
169
    }
 
170
    
 
171
    private void onLeftComboChange() {                    
 
172
        onLangChange(false);
 
173
    }
 
174
    
 
175
    private void onRightComboChange() {        
 
176
        onLangChange(true);
 
177
    }
 
178
    
 
179
    private void onSwap() {
 
180
        var id = getLeftId();
 
181
        rightLangCombo.set_active_id(id);
 
182
    }
 
183
    
 
184
    private void onUpdate() {
 
185
        if (topText.buffer.text.length < 1) {
 
186
            bottomText.buffer.text = "";
 
187
            return;
 
188
        }
 
189
        if ((leftLang == null) || (rightLang == null)) return;                
 
190
        service.translate(leftLang, rightLang, topText.buffer.text);
 
191
    }
 
192
    
 
193
    private void onTranslate(string[] text) {
 
194
        if ((text == null) || (text.length < 1)) return;
 
195
        bottomText.buffer.text = string.joinv("", text);
 
196
    }
 
197
}
 
 
b'\\ No newline at end of file'