Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/toolbar.py
blob: d7188bb3ff5697c3b7047c30d56885e9561b6c39 (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
# -*- coding: utf-8 -*-
#
# Copyright 2012 Manuel QuiƱones
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import sys
sys.path.append('mypaint')

from gettext import gettext as _
import logging

import gtk

from sugar.graphics.toolbarbox import ToolbarBox
from sugar.activity.widgets import ActivityToolbarButton
from sugar.activity.widgets import StopButton
from sugar.graphics.colorbutton import ColorToolButton
from sugar.graphics.radiotoolbutton import RadioToolButton
from sugar.graphics.palette import Palette
from sugar.graphics.toolbutton import ToolButton

from mypaint.brushlib import brushsettings


SETTING_NAMES = ['radius_logarithmic', 'slow_tracking', 'opaque', 'hardness']


class BrushesPalette(Palette):
    def __init__(self, toolbar):
        self._toolbar = toolbar

        super(BrushesPalette, self).__init__()
        self.set_primary_text(_("Brushes"))

        table = gtk.Table()
        self.set_content(table)
        table.show()

        self.brushes_by_name = {}
        self._brushes_group = None

        def create_brush(brush_name, row, col):
            brush = gtk.RadioButton()
            brush.props.group = self._brushes_group
            if self._brushes_group is None:
                self._brushes_group = brush
            brush.connect('clicked', self._toolbar.brush_clicked_cb,
                          brush_name)
            table.attach(brush, col, col+1, row, row+1, gtk.FILL)
            brush.show()

            hbox = gtk.HBox()
            brush.add(hbox)
            hbox.show()

            preview_filename = 'brushes/%s_prev.png' % brush_name
            preview_pixbuf = gtk.gdk.pixbuf_new_from_file(preview_filename)

            image = gtk.Image()
            image.set_from_pixbuf(preview_pixbuf)
            hbox.pack_start(image)
            image.show()

            return brush

        row = 0
        col = 0
        for brush_name in ['brush', 'charcoal', 'pen', 'modelling2', 'blur']:
            brush = create_brush(brush_name, row, col)
            self.brushes_by_name[brush_name] = brush
            if col > 2:
                row += 1
                col = 0
            else:
                col += 1


class SettingsPalette(Palette):
    def __init__(self, toolbar):
        self._toolbar = toolbar

        super(SettingsPalette, self).__init__()
        self.set_primary_text(_("Brush Settings"))

        table = gtk.Table()
        self.set_content(table)
        table.show()

        self.controls = {}

        def create_control(setting_name, row):
            setting = brushsettings.settings_dict[setting_name]

            label = gtk.Label(_('%s: ' % setting.name))
            table.attach(label, 0, 1, row, row+1, gtk.FILL)
            label.show()

            adj = gtk.Adjustment(value=setting.default,
                                 lower=setting.min, upper=setting.max,
                                 step_incr=0.01, page_incr=0.1)
            control = gtk.HScale(adj)
            control.set_draw_value(False)
            control.set_update_policy(gtk.UPDATE_CONTINUOUS)
            control.set_size_request(250, -1)
            control.connect('value-changed', self._toolbar.control_changed_cb,
                            setting_name)
            table.attach(control, 1, 2, row, row+1, gtk.FILL|gtk.EXPAND)
            control.show()
            return control

        row = 0
        for setting_name in SETTING_NAMES:
            control = create_control(setting_name, row)
            self.controls[setting_name] = control
            row += 1


class MyPaintToolbar(ToolbarBox):
    def __init__(self, activity):
        """Make activity toolbar."""
        super(MyPaintToolbar, self).__init__()

        self._drawing = None
        self._skip_brushbuttons_update = False
        self._skip_color_update = False
        self._skip_setting_update = {}
        for setting_name in SETTING_NAMES:
            self._skip_setting_update[setting_name] = False

        activity_button = ActivityToolbarButton(activity)
        self.toolbar.insert(activity_button, -1)
        activity_button.show()

        # Brush color selector:

        self._color_button = ColorToolButton()
        self._color_button.connect('color-set', self._color_cb)
        self.toolbar.insert(self._color_button, -1)
        self._color_button.show_all()

        # Brushes palette:

        brushes_button = ToolButton('tool-brush')
        self._brushes_palette = BrushesPalette(self)
        brushes_button.set_palette(self._brushes_palette)
        self.toolbar.insert(brushes_button, -1)
        brushes_button.show()

        # Settings palette:

        settings_button = ToolButton('view-source')
        self._settings_palette = SettingsPalette(self)
        settings_button.set_palette(self._settings_palette)
        self.toolbar.insert(settings_button, -1)
        settings_button.show()

        # Blank space (separator) and Stop button at the end:

        separator = gtk.SeparatorToolItem()
        separator.props.draw = False
        separator.set_expand(True)
        self.toolbar.insert(separator, -1)
        separator.show()

        stop_button = StopButton(activity)
        self.toolbar.insert(stop_button, -1)
        stop_button.show()

    def set_drawing(self, drawing):
        self._drawing = drawing
        self._drawing.connect('setting-changed', self._setting_changed_cb)
        self._drawing.connect('color-changed', self._color_changed_cb)
        self._drawing.connect('brush-changed', self._brush_changed_cb)
        self._update_buttons()

    def _update_setting(self, setting_name, value):
        self._settings_palette.controls[setting_name].set_value(value)

    def _update_color_button(self, color):
        color = gtk.gdk.Color(color)
        self._color_button.props.color = color

    def _update_brushes_buttons(self, brush_name):
        brush_button = self._brushes_palette.brushes_by_name[brush_name]
        brush_button.props.active = True

    def _update_buttons(self):
        for setting_name in SETTING_NAMES:
            value = self._drawing.get_setting(setting_name)
            self._update_setting(setting_name, value)
        color = self._drawing.get_stroke_color()
        self._update_color_button(color)

    def _setting_changed_cb(self, widget, setting_name, value):
        if not self._skip_setting_update[setting_name]:
            self._update_setting(setting_name, value)

    def _color_changed_cb(self, widget, color):
        if not self._skip_color_update:
            self._update_color_button(color)

    def _brush_changed_cb(self, widget, brush_name):
        if not self._skip_brushbuttons_update:
            self._update_brushes_buttons(brush_name)

    def _color_cb(self, widget):
        self._skip_color_update = True
        color = widget.get_color()
        self._drawing.set_stroke_color(color)
        self._skip_color_update = False

    def brush_clicked_cb(self, event, brush_name):
        self._skip_brushbuttons_update = True
        self._drawing.set_brush(brush_name)
        self._skip_brushbuttons_update = False

    def control_changed_cb(self, control, setting_name):
        self._skip_setting_update[setting_name] = True
        value = control.get_value()
        self._drawing.set_setting(setting_name, value)
        self._skip_setting_update[setting_name] = False