Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/applicationgtk3.py
blob: f5ce04bbfb17657c84dcfe0141be9d20e6ba05d6 (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
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
288
289
290
291
292
# Copyright (C) 2012-2013 S. Daniel Francis <francis@sugarlabs.org>
#
# 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 3 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 Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

# This file is modified for Graph Plotter


import logging
logger = logging.getLogger('application')

import os
import sys
import info
appname = info.name

import sweetener

sys.path.append(os.path.abspath('./gtk3modules'))
sys.path.append(os.path.abspath('./sweetenergtk3'))

logger.debug('gtk3 application loading')

from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf

from options import Options
from canvas import Canvas
try:
    from sweetener.alerts import Alert
except:
    pass

this_dir = os.path.split('__file__')[:-1]
if len(this_dir) == 1 and this_dir[0] == '':
    this_dir = os.path.abspath('./')
else:
    this_dir = os.path.join(this_dir)
os.chdir(this_dir)

from gettext import gettext as _

if 'programabspath' in os.environ['DATADIR']:
    datadir = os.environ['DATADIR'].replace('programabspath',
                                            this_dir)
else:
    datadir = os.environ['DATADIR']

if os.environ['ICONDIR'] != 'SYSTEM':
    logger.debug(this_dir)
    icondir = os.environ['ICONDIR'].replace('programabspath',
                                            this_dir)
    icon_theme = Gtk.IconTheme.get_for_screen(Gdk.Screen.get_default())
    icon_theme.append_search_path(icondir)
    icon_theme.append_search_path(os.path.join(this_dir, 'icons'))


class UnfullscreenButton(Gtk.Window):

    def __init__(self):
        GObject.GObject.__init__(self)

        self.set_decorated(False)
        self.set_resizable(False)
        self.set_type_hint(Gdk.WindowTypeHint.DIALOG)

        self.set_border_width(0)

        self.props.accept_focus = False

        #Setup estimate of width, height
        valid, w, h = Gtk.icon_size_lookup(Gtk.IconSize.LARGE_TOOLBAR)
        self._width = w
        self._height = h

        screen = self.get_screen()
        screen.connect('size-changed', self._screen_size_changed_cb)

        self._button = Gtk.Button(stock=Gtk.STOCK_LEAVE_FULLSCREEN)
        self._button.set_relief(Gtk.ReliefStyle.NONE)
        self._button.show()
        self.add(self._button)

    def connect_button_press(self, cb):
        self._button.connect('button-press-event', cb)

    def _reposition(self):
        x = Gdk.Screen.width() - self._width
        self.move(x, 0)

    def do_get_preferred_width(self):
        minimum, natural = Gtk.Window.do_get_preferred_width(self)
        self._width = minimum
        self._reposition()
        return minimum, natural

    def _screen_size_changed_cb(self, screen):
        self._reposition()


class Application(Gtk.Window):
    def __init__(self):
        GObject.GObject.__init__(self)
        self.set_size_request(800, 600)
        self.save_type = info.io_mode
        self.started = False
        self.file_path = None
        if info.file_filter_name:
            self.filter = Gtk.FileFilter()
            self.filter.set_name(info.file_filter_name)
            if info.file_filter_mime:
                self.filter.add_mime_type(info.file_filter_mime)
            for i in info.file_filter_patterns:
                self.filter.add_pattern(i)
        self.accel_group = Gtk.AccelGroup()
        self.add_accel_group(self.accel_group)
        self.set_title(appname)
        logger.debug(info.lower_name)
        self.set_icon(GdkPixbuf.Pixbuf.new_from_file(os.path.join(datadir,
                                                        'appicon.svg')))
        self.connect('delete-event', lambda w, e: self.stop())
        self.maximize()
        self._vbox = Gtk.VBox()
        self.options = Options(self)
        self.options.show()
        self._vbox.pack_start(self.options, False, True, 0)
        self.canvas = Canvas(self.options, self)
        self.canvas.show()
        self._vbox.pack_start(self.canvas, True, True, 0)
        self.add(self._vbox)
        self._vbox.show()

        self._is_fullscreen = False
        self.set_events(Gdk.EventMask.ALL_EVENTS_MASK)
        self.connect('motion-notify-event', self.motion_cb)

    def do_draw(self, extra):
        Gtk.Window.do_draw(self, extra)
        #logger.debug('Exposing')
        if not self.started:
            if self.file_path is not None:
                self.canvas.read_file(self.file_path)
                self.set_title(os.path.abspath('%s - %s' % (self.file_path,
                                                            appname)))
            else:
                self.new()
            self.started = True

    def motion_cb(self, widget, event):
        if self._is_fullscreen:
            state, x, y, mask = self.get_window().get_pointer()
            width, height = self.get_size()
            button_width, button_height = self._unfullscreen_button.get_size()
            if x >= width - button_width or y <= button_height:
                self._unfullscreen_button.show()
            else:
                self._unfullscreen_button.hide()

    def add_alert(self, alert):
        alert.run()

    def fullscreen(self):
        self.options.hide()
        self._is_fullscreen = True
        self._unfullscreen_button = UnfullscreenButton()
        self._unfullscreen_button.set_transient_for(self)
        self._unfullscreen_button.connect_button_press(
            lambda w, e: self.unfullscreen())
        self._unfullscreen_button.show()
        Gtk.Window.fullscreen(self)

    def unfullscreen(self):
        self.options.show()
        self._is_fullscreen = False
        self._unfullscreen_button.destroy()
        # The following line is an adition for Graph Plotter
        self.canvas._resize_queue = self.canvas.previous_size
        # Part of the sweetener code
        Gtk.Window.unfullscreen(self)

    def export(self, widget, data):
        format_name = data[3]
        filter_mime = data[2]
        mime_type = data[1]
        filechooser = Gtk.FileChooserDialog(_("Export..."), self,
                                        Gtk.FileChooserAction.SAVE,
                                        (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                                         Gtk.STOCK_OK, Gtk.ResponseType.OK))
        file_filter = Gtk.FileFilter()
        file_filter.set_name(format_name)
        file_filter.add_mime_type(filter_mime)
        filechooser.add_filter(file_filter)
        filechooser.set_do_overwrite_confirmation(True)
        filechooser.run()
        file_path = filechooser.get_filename()
        filechooser.destroy()
        self.canvas.exports[mime_type](file_path)

    def new(self):
        self.file_path = None
        self.canvas.new()
        self.set_title(appname)

    def stop(self):
        if info.io_mode == info.CONFIG:
            self.file_path = os.path.join(os.environ['HOME'],
                                          '.' + info.lower_name)
            self.save()
        Gtk.main_quit()

    def open(self):
        filechooser = Gtk.FileChooserDialog(_('Open...'), self,
                                        Gtk.FileChooserAction.OPEN,
                                        (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                                         Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
        filechooser.add_filter(self.filter)
        response = filechooser.run()
        self.file_path = filechooser.get_filename()
        filechooser.destroy()
        if response == Gtk.ResponseType.OK:
            logger.debug('Read file %s' % self.file_path)
            self.canvas.read_file(self.file_path)
            self.set_title('%s - %s' % (self.file_path, appname))

    def save(self):
        if self.file_path is None:
            self.save_as()
        else:
            logger.debug('Write file %s' % self.file_path)
            self.canvas.write_file(self.file_path)

    def save_as(self):
        filechooser = Gtk.FileChooserDialog(_('Save...'), self,
                                            Gtk.FileChooserAction.SAVE,
                                        (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                                         Gtk.STOCK_OK, Gtk.ResponseType.OK))
        filechooser.add_filter(self.filter)
        filechooser.set_do_overwrite_confirmation(True)
        response = filechooser.run()
        self.file_path = filechooser.get_filename()
        filechooser.destroy()
        if response == Gtk.ResponseType.OK:
            logger.debug('Save file as %s' % self.file_path)
            self.canvas.write_file(self.file_path)
            self.set_title('%s - %s' % (self.file_path, appname))

    def start(self):
        self.show()
        Gtk.main()


if __name__ == '__main__':
    logger.debug('Initializing %s' % info.name)
    import sys
    if info.io_mode == info.DOCUMENT:
        if len(sys.argv) > 1:
            logger.debug('Open from args %s' % sys.argv[1])
            if os.path.exists(sys.argv[1]):
                file_path = sys.argv[1]
                logger.debug('Found file')
            else:
                file_path = None
                logger.error('Could not find file')
        else:
            file_path = None
            logger.debug('Create new file')
    else:
        if os.path.exists(os.path.join(os.environ['HOME'],
                                       '.' + info.lower_name)):
            file_path = os.path.join(os.environ['HOME'], '.' + info.lower_name)
        else:
            file_path = None
    window = Application()
    window.file_path = file_path
    window.show()
    Gtk.main()
    logger.debug('Closing %s' % info.name)
    exit()