~phoenix1987/gtumbler/trunk

« back to all changes in this revision

Viewing changes to gtumbler/GtumblerWindow.py

  • Committer: Gabriele N. Tornetta
  • Date: 2015-04-13 22:11:01 UTC
  • Revision ID: phoenix1987@gmail.com-20150413221101-on5p7eo205wsncwe
Resuming work on new version

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
from gtumbler.AboutGtumblerDialog import AboutGtumblerDialog
32
32
from gtumbler.PreferencesGtumblerDialog import PreferencesGtumblerDialog
33
33
 
34
 
from style import style
 
34
from .style import style
35
35
 
36
 
import rsvg
 
36
from gi.repository import Rsvg as rsvg
37
37
 
38
38
RightPaneType = enum('BOUNDING_BOXES')
39
39
 
40
40
# See gtumbler_lib.Window.py for more details about how this class works
41
41
class GtumblerWindow(Window):
42
 
    __gtype_name__ = "GtumblerWindow"
43
 
    
44
 
    def finish_initializing(self, builder): # pylint: disable=E1002
45
 
        """Set up the main window"""
46
 
        super(GtumblerWindow, self).finish_initializing(builder)
47
 
 
48
 
        self.AboutDialog = AboutGtumblerDialog
49
 
        self.PreferencesDialog = PreferencesGtumblerDialog
50
 
 
51
 
        # Code for other initialization actions should be added here.
52
 
 
53
 
        # Styling
54
 
        css_provider = Gtk.CssProvider()
55
 
        css_provider.load_from_data(style)
56
 
        context = Gtk.StyleContext()
57
 
        context.add_provider_for_screen(Gdk.Screen.get_default(),
58
 
                                        css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER) 
59
 
        self.ui.tb_main.get_style_context().add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
60
 
 
61
 
        for widget in self.ui._widget_dict:
62
 
            getattr(self.ui._widget_dict[widget], 'set_name', lambda a: None)(widget)
63
 
 
64
 
        # Managing Action Contexts
65
 
        self.ac_document_opened = ActionContext()
66
 
        self.ac_document_opened.append(self.ui.action_bounding_boxes)
67
 
 
68
 
        # Installing the BoundingBoxView widget
69
 
        self.ui.bounding_box_view = BoundingBoxView()
70
 
        self.ui.bounding_boxes.add(self.ui.bounding_box_view)
71
 
 
72
 
        # Connecting signals for the Document View
73
 
        self.ui.da_doc_view.connect('document-changed', self.on_da_doc_view_document_changed)
74
 
        self.ui.da_doc_view.connect('page-changed', self.on_da_doc_view_page_changed)
75
 
        self.ui.da_doc_view.connect('zoom', self.on_da_doc_view_zoom)
76
 
        
77
 
        # Connecting signals for the Bounding Box View
78
 
        self.ui.bounding_box_view.connect('box-changed', self.on_bounding_box_view_box_changed)
79
 
        self.ui.bounding_box_view.connect('value-changed', self.on_bounding_box_view_value_changed)
80
 
    
81
 
    ### BEGIN HELPERS
82
 
 
83
 
    def lock_paned2(self):
84
 
        self.ui.paned2.child_set_property(self.ui.paned2.get_child2(), 'shrink', True)
85
 
        self.ui.paned2.set_position(9999)
86
 
 
87
 
    def release_paned2(self):
88
 
        self.ui.paned2.child_set_property(self.ui.paned2.get_child2(), 'shrink', False)
89
 
        self.ui.paned2.set_position(9998)
90
 
 
91
 
    def reposition_after_zoom(self, x, y, s):
92
 
        ax = self.ui.sw_doc_view.get_hadjustment().get_value()
93
 
        ay = self.ui.sw_doc_view.get_vadjustment().get_value()
94
 
        s = self.ui.da_doc_view.get_zoom() / s
95
 
        ax = ax + (s - 1) * (x - 6)
96
 
        ay = ay + (s - 1) * (y - 6)
97
 
        
98
 
        try:
99
 
            self.ui.sw_doc_view.get_hadjustment().set_value(ax)
100
 
        except:
101
 
            print "H panick"
102
 
 
103
 
        try:
104
 
            self.ui.sw_doc_view.get_vadjustment().set_value(ay)
105
 
        except:
106
 
            print "V panick"
107
 
 
108
 
    def reposition_at_middle(self, s):
109
 
        x = 6 + self.ui.sw_doc_view.get_hadjustment().get_value() \
110
 
              + self.ui.sw_doc_view.get_hadjustment().get_page_size() / 2
111
 
 
112
 
        y = 6 + self.ui.sw_doc_view.get_vadjustment().get_value() \
113
 
              + self.ui.sw_doc_view.get_vadjustment().get_page_size() / 2
114
 
 
115
 
        self.reposition_after_zoom(x, y, s)
116
 
 
117
 
    def toggle_zoom(self, active):
118
 
        self.ui.tb_main_zoom_best_fit.set_active(active)
119
 
        self.ui.tb_main_zoom_fit_page.set_active(active)
120
 
 
121
 
    ### END HELPERS
122
 
 
123
 
 
124
 
 
125
 
    ### BEGIN SIGNAL HANDLERS
126
 
 
127
 
    def on_action_open_activate(self, action, *args):
128
 
        open_dialog = Gtk.FileChooserDialog("Select a document", self,
 
42
        __gtype_name__ = "GtumblerWindow"
 
43
        
 
44
        def finish_initializing(self, builder): # pylint: disable=E1002
 
45
                """Set up the main window"""
 
46
                super(GtumblerWindow, self).finish_initializing(builder)
 
47
 
 
48
                self.AboutDialog = AboutGtumblerDialog
 
49
                self.PreferencesDialog = PreferencesGtumblerDialog
 
50
 
 
51
                # Code for other initialization actions should be added here.
 
52
 
 
53
                # Styling
 
54
                css_provider = Gtk.CssProvider()
 
55
                css_provider.load_from_data(style)
 
56
                context = Gtk.StyleContext()
 
57
                context.add_provider_for_screen(Gdk.Screen.get_default(),
 
58
                                                                                css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER) 
 
59
                self.ui.tb_main.get_style_context().add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
 
60
 
 
61
                for widget in self.ui._widget_dict:
 
62
                        getattr(self.ui._widget_dict[widget], 'set_name', lambda a: None)(widget)
 
63
 
 
64
                # Managing Action Contexts
 
65
                self.ac_document_opened = ActionContext()
 
66
                self.ac_document_opened.append(self.ui.action_bounding_boxes)
 
67
 
 
68
                # Installing the BoundingBoxView widget
 
69
                self.ui.bounding_box_view = BoundingBoxView()
 
70
                self.ui.bounding_boxes.add(self.ui.bounding_box_view)
 
71
 
 
72
                # Connecting signals for the Document View
 
73
                self.ui.da_doc_view.connect('document-changed', self.on_da_doc_view_document_changed)
 
74
                self.ui.da_doc_view.connect('page-changed', self.on_da_doc_view_page_changed)
 
75
                self.ui.da_doc_view.connect('zoom', self.on_da_doc_view_zoom)
 
76
                
 
77
                # Connecting signals for the Bounding Box View
 
78
                self.ui.bounding_box_view.connect('box-changed', self.on_bounding_box_view_box_changed)
 
79
                self.ui.bounding_box_view.connect('value-changed', self.on_bounding_box_view_value_changed)
 
80
        
 
81
        ### BEGIN HELPERS
 
82
 
 
83
        def lock_paned2(self):
 
84
                self.ui.paned2.child_set_property(self.ui.paned2.get_child2(), 'shrink', True)
 
85
                self.ui.paned2.set_position(9999)
 
86
 
 
87
        def release_paned2(self):
 
88
                self.ui.paned2.child_set_property(self.ui.paned2.get_child2(), 'shrink', False)
 
89
                self.ui.paned2.set_position(9998)
 
90
 
 
91
        def reposition_after_zoom(self, x, y, s):
 
92
                ax = self.ui.sw_doc_view.get_hadjustment().get_value()
 
93
                ay = self.ui.sw_doc_view.get_vadjustment().get_value()
 
94
                s = self.ui.da_doc_view.get_zoom() / s
 
95
                ax = ax + (s - 1) * (x - 6)
 
96
                ay = ay + (s - 1) * (y - 6)
 
97
                
 
98
                try:
 
99
                        self.ui.sw_doc_view.get_hadjustment().set_value(ax)
 
100
                except:
 
101
                        print("H panick")
 
102
 
 
103
                try:
 
104
                        self.ui.sw_doc_view.get_vadjustment().set_value(ay)
 
105
                except:
 
106
                        print("V panick")
 
107
 
 
108
        def reposition_at_middle(self, s):
 
109
                x = 6 + self.ui.sw_doc_view.get_hadjustment().get_value() \
 
110
                          + self.ui.sw_doc_view.get_hadjustment().get_page_size() / 2
 
111
 
 
112
                y = 6 + self.ui.sw_doc_view.get_vadjustment().get_value() \
 
113
                          + self.ui.sw_doc_view.get_vadjustment().get_page_size() / 2
 
114
 
 
115
                self.reposition_after_zoom(x, y, s)
 
116
 
 
117
        def toggle_zoom(self, active):
 
118
                self.ui.tb_main_zoom_best_fit.set_active(active)
 
119
                self.ui.tb_main_zoom_fit_page.set_active(active)
 
120
 
 
121
        ### END HELPERS
 
122
 
 
123
 
 
124
 
 
125
        ### BEGIN SIGNAL HANDLERS
 
126
 
 
127
        def on_action_open_activate(self, action, *args):
 
128
                open_dialog = Gtk.FileChooserDialog("Select a document", self,
129
129
                                                                                        buttons = (Gtk.STOCK_OK,
130
 
                                                       Gtk.ResponseType.OK,
 
130
                                                                                                           Gtk.ResponseType.OK,
131
131
                                                                                                           Gtk.STOCK_CANCEL,
132
 
                                                       Gtk.ResponseType.CANCEL))
133
 
 
134
 
        ret = open_dialog.run()
135
 
        if ret == Gtk.ResponseType.OK:
136
 
            try:
137
 
                self.ui.da_doc_view.open(open_dialog.get_filename())
138
 
                self.ac_document_opened.set_sensitive(True)
139
 
            except:
140
 
                pass # TODO: Add notification of failure
141
 
        open_dialog.destroy()
142
 
 
143
 
    def on_bounding_box_view_box_changed(self, widget, new_box):
144
 
        pass
145
 
 
146
 
    def on_bounding_box_view_value_changed(self, widget, *args):
147
 
        pass
148
 
 
149
 
    def on_btn_right_pane_close_clicked(self, widget, *args):
150
 
        self.lock_paned2()
151
 
 
152
 
    def on_da_doc_view_button_press_event(self, widget, event, *args):
153
 
        pass
154
 
 
155
 
    def on_da_doc_view_document_changed(self, widget):
156
 
        self.ui.lbl_n_pages.set_text("%d " % widget.document.get_n_pages())
157
 
 
158
 
    def on_da_doc_view_page_changed(self, widget, new_page):
159
 
        self.ui.txt_page.set_text(str(new_page + 1))
160
 
 
161
 
    def on_da_doc_view_scroll_event(self, widget, event, *args):
162
 
        if event.type == Gdk.EventType.SCROLL and Gdk.ModifierType.CONTROL_MASK & event.state:
163
 
            if self.ui.da_doc_view.get_state():
164
 
                s = self.ui.da_doc_view.get_zoom()
165
 
 
166
 
                if event.direction == Gdk.ScrollDirection.UP:
167
 
                    self.ui.da_doc_view.zoom_in(0.25)
168
 
                elif event.direction == Gdk.ScrollDirection.DOWN:
169
 
                    self.ui.da_doc_view.zoom_out(0.25)
170
 
                self.reposition_after_zoom(event.x, event.y, s)
171
 
 
172
 
            return True
173
 
 
174
 
    def on_da_doc_view_zoom(self, widget, zoom):
175
 
        self.ui.txt_zoom.set_text('%d %%' % int(zoom * 100))
176
 
 
177
 
    def on_da_tumbler_draw(self, widget, event, *args):
178
 
        # Showing the tumbler shadow
179
 
        cr = widget.get_window().cairo_create()
180
 
        svg = rsvg.Handle(file='data/media/tumbler.svg')
181
 
        svg.render_cairo(cr)
182
 
 
183
 
    def on_action_bounding_boxes_activate(self, widget, *args):
184
 
        self.ui.lbl_right_pane_title.set_markup('<b>Bounding Boxes</b>')
185
 
        self.ui.nb_right_pane.set_current_page(RightPaneType.BOUNDING_BOXES)
186
 
        self.release_paned2()
187
 
 
188
 
    def on_paned1_button_press_event(self, widget, event, *args):
189
 
        return not self.ui.tb_sidepane.get_active()
190
 
 
191
 
    def on_paned2_button_press_event(self, widget, event, *args):
192
 
        try:
193
 
            return not self.ui.nb_right_pane.get_window().is_visible()
194
 
        except:
195
 
            return True
196
 
  
197
 
    def on_tb_main_first_clicked(self, widget, *args):
198
 
        self.ui.da_doc_view.page_first()  
199
 
 
200
 
    def on_tb_main_last_clicked(self, widget, *args):
201
 
        self.ui.da_doc_view.page_last()
202
 
    
203
 
    def on_tb_main_next_clicked(self, widget, *args):
204
 
        self.ui.da_doc_view.page_next()
205
 
 
206
 
    def on_tb_main_prev_clicked(self, widget, *args):
207
 
        self.ui.da_doc_view.page_previous()  
208
 
 
209
 
    def on_tb_main_zoom_best_fit_toggled(self, widget, *args):
210
 
        self.ui.da_doc_view.set_best_fit(widget.get_active())
211
 
        if widget.get_active():
212
 
            self.ui.tb_main_zoom_fit_page.set_active(False)
213
 
    
214
 
    def on_tb_main_zoom_fit_page_clicked(self, widget, *args):
215
 
        self.ui.da_doc_view.set_fit_page(widget.get_active())
216
 
        if widget.get_active():
217
 
            self.ui.tb_main_zoom_best_fit.set_active(False)
218
 
 
219
 
    def on_tb_main_zoom_in_clicked(self, widget, *args):
220
 
        s = self.ui.da_doc_view.get_zoom()
221
 
        self.ui.da_doc_view.zoom_in(0.25)
222
 
        self.reposition_at_middle(s)
223
 
        self.toggle_zoom(False)
224
 
 
225
 
    def on_tb_main_zoom_out_clicked(self, widget, *args):
226
 
        s = self.ui.da_doc_view.get_zoom()
227
 
        self.ui.da_doc_view.zoom_out(0.25)
228
 
        self.reposition_at_middle(s)
229
 
        self.toggle_zoom(False)
230
 
 
231
 
    ### END SIGNAL HANDLERS
 
132
                                                                                                           Gtk.ResponseType.CANCEL))
 
133
 
 
134
                ret = open_dialog.run()
 
135
                if ret == Gtk.ResponseType.OK:
 
136
                        try:
 
137
                                self.ui.da_doc_view.open(open_dialog.get_filename())
 
138
                                self.ac_document_opened.set_sensitive(True)
 
139
                        except Exception as e:
 
140
                                # TODO: Add notification of failure
 
141
                                print("Failed to open document")
 
142
                                print("Reason: %s", e)
 
143
                                
 
144
                open_dialog.destroy()
 
145
 
 
146
        def on_bounding_box_view_box_changed(self, widget, new_box):
 
147
                pass
 
148
 
 
149
        def on_bounding_box_view_value_changed(self, widget, *args):
 
150
                pass
 
151
 
 
152
        def on_btn_right_pane_close_clicked(self, widget, *args):
 
153
                self.lock_paned2()
 
154
 
 
155
        def on_da_doc_view_button_press_event(self, widget, event, *args):
 
156
                pass
 
157
 
 
158
        def on_da_doc_view_document_changed(self, widget):
 
159
                self.ui.lbl_n_pages.set_text("%d " % widget.document.get_n_pages())
 
160
 
 
161
        def on_da_doc_view_page_changed(self, widget, new_page):
 
162
                self.ui.txt_page.set_text(str(new_page + 1))
 
163
 
 
164
        def on_da_doc_view_scroll_event(self, widget, event, *args):
 
165
                if event.type == Gdk.EventType.SCROLL and Gdk.ModifierType.CONTROL_MASK & event.state:
 
166
                        if self.ui.da_doc_view.get_state():
 
167
                                s = self.ui.da_doc_view.get_zoom()
 
168
 
 
169
                                if event.direction == Gdk.ScrollDirection.UP:
 
170
                                        self.ui.da_doc_view.zoom_in(0.25)
 
171
                                elif event.direction == Gdk.ScrollDirection.DOWN:
 
172
                                        self.ui.da_doc_view.zoom_out(0.25)
 
173
                                self.reposition_after_zoom(event.x, event.y, s)
 
174
                        
 
175
                        return True
 
176
                
 
177
                return False
 
178
        def on_da_doc_view_zoom(self, widget, zoom):
 
179
                self.ui.txt_zoom.set_text('%d %%' % int(zoom * 100))
 
180
 
 
181
        def on_da_tumbler_draw(self, widget, event, *args):
 
182
                # Showing the tumbler shadow
 
183
                cr = widget.get_window().cairo_create()
 
184
                svg = rsvg.Handle.new_from_file('data/media/tumbler.svg')
 
185
                svg.render_cairo(cr)
 
186
 
 
187
        def on_action_bounding_boxes_activate(self, widget, *args):
 
188
                self.ui.lbl_right_pane_title.set_markup('<b>Bounding Boxes</b>')
 
189
                self.ui.nb_right_pane.set_current_page(RightPaneType.BOUNDING_BOXES)
 
190
                self.release_paned2()
 
191
 
 
192
        def on_paned1_button_press_event(self, widget, event, *args):
 
193
                return not self.ui.tb_sidepane.get_active()
 
194
 
 
195
        def on_paned2_button_press_event(self, widget, event, *args):
 
196
                try:
 
197
                        return not self.ui.nb_right_pane.get_window().is_visible()
 
198
                except:
 
199
                        return True
 
200
 
 
201
        def on_tb_main_first_clicked(self, widget, *args):
 
202
                self.ui.da_doc_view.page_first()  
 
203
 
 
204
        def on_tb_main_last_clicked(self, widget, *args):
 
205
                self.ui.da_doc_view.page_last()
 
206
        
 
207
        def on_tb_main_next_clicked(self, widget, *args):
 
208
                self.ui.da_doc_view.page_next()
 
209
 
 
210
        def on_tb_main_prev_clicked(self, widget, *args):
 
211
                self.ui.da_doc_view.page_previous()  
 
212
 
 
213
        def on_tb_main_zoom_best_fit_toggled(self, widget, *args):
 
214
                self.ui.da_doc_view.set_best_fit(widget.get_active())
 
215
                if widget.get_active():
 
216
                        self.ui.tb_main_zoom_fit_page.set_active(False)
 
217
        
 
218
        def on_tb_main_zoom_fit_page_clicked(self, widget, *args):
 
219
                self.ui.da_doc_view.set_fit_page(widget.get_active())
 
220
                if widget.get_active():
 
221
                        self.ui.tb_main_zoom_best_fit.set_active(False)
 
222
 
 
223
        def on_tb_main_zoom_in_clicked(self, widget, *args):
 
224
                s = self.ui.da_doc_view.get_zoom()
 
225
                self.ui.da_doc_view.zoom_in(0.25)
 
226
                self.reposition_at_middle(s)
 
227
                self.toggle_zoom(False)
 
228
 
 
229
        def on_tb_main_zoom_out_clicked(self, widget, *args):
 
230
                s = self.ui.da_doc_view.get_zoom()
 
231
                self.ui.da_doc_view.zoom_out(0.25)
 
232
                self.reposition_at_middle(s)
 
233
                self.toggle_zoom(False)
 
234
 
 
235
        ### END SIGNAL HANDLERS