"Fossies" - the Fresh Open Source Software Archive

Member "sk1-2.0rc4/src/sk1/document/controllers/editor_text.py" (25 May 2019, 8485 Bytes) of package /linux/misc/sk1-2.0rc4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "editor_text.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0rc3_vs_2.0rc4.

    1 # -*- coding: utf-8 -*-
    2 #
    3 #  Copyright (C) 2016 by Igor E. Novikov
    4 #
    5 #  This program is free software: you can redistribute it and/or modify
    6 #  it under the terms of the GNU General Public License as published by
    7 #  the Free Software Foundation, either version 3 of the License, or
    8 #  (at your option) any later version.
    9 #
   10 #  This program is distributed in the hope that it will be useful,
   11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 #  GNU General Public License for more details.
   14 #
   15 #  You should have received a copy of the GNU General Public License
   16 #  along with this program.  If not, see <https://www.gnu.org/licenses/>.
   17 
   18 from copy import deepcopy
   19 
   20 from uc2 import libgeom
   21 
   22 from sk1 import _, modes, config, events
   23 from generic import AbstractController
   24 
   25 
   26 class TextEditor(AbstractController):
   27     mode = modes.TEXT_EDITOR_MODE
   28     target = None
   29     selected_obj = None
   30 
   31     points = []
   32     selected_points = []
   33     spoint = None
   34     trafos = {}
   35     trafo_mode = modes.ET_MOVING_MODE
   36     move_flag = False
   37     initial_start = None
   38 
   39     def __init__(self, canvas, presenter):
   40         AbstractController.__init__(self, canvas, presenter)
   41 
   42     def start_(self):
   43         self.snap = self.presenter.snap
   44         self.target = self.selection.objs[0]
   45         self.selected_obj = None
   46         self.api.set_mode()
   47         self.update_points()
   48         self.selection.clear()
   49         self.trafo_mode = modes.ET_MOVING_MODE
   50         msg = _('Text in shaping')
   51         events.emit(events.APP_STATUS, msg)
   52 
   53     def stop_(self):
   54         if not self.selected_obj:
   55             self.selection.set([self.target, ])
   56         else:
   57             self.selection.set([self.selected_obj, ])
   58         self.target = None
   59         self.selected_obj = None
   60         self.points = []
   61 
   62     def escape_pressed(self):
   63         self.canvas.set_mode()
   64 
   65     def update_points(self):
   66         cv = self.canvas
   67         self.points = []
   68         index = 0
   69         for item in self.target.cache_layout_data:
   70             if index < len(self.target.cache_cpath) and \
   71                     self.target.cache_cpath[index]:
   72                 x = item[0]
   73                 y = item[4]
   74                 trafo = self.target.trafo
   75                 flag = False
   76                 if index in self.target.trafos.keys():
   77                     trafo = self.target.trafos[index]
   78                     flag = True
   79                 self.points.append(ControlPoint(cv, index, [x, y], trafo, flag))
   80             index += 1
   81 
   82     def set_mode(self, mode):
   83         if mode in modes.ET_MODES and not mode == self.trafo_mode:
   84             pass
   85 
   86     # ----- REPAINT
   87     def repaint(self):
   88         bbox = self.target.cache_layout_bbox
   89         self.canvas.renderer.draw_text_frame(bbox, self.target.trafo)
   90         for item in self.points:
   91             selected = False
   92             if item in self.selected_points:
   93                 selected = True
   94             item.repaint(selected)
   95 
   96     def repaint_frame(self):
   97         self.canvas.renderer.cdc_draw_frame(self.start, self.end, True)
   98 
   99     def on_timer(self):
  100         if self.draw:
  101             self.repaint_frame()
  102 
  103     # ----- MOUSE CONTROLLING
  104 
  105     def mouse_down(self, event):
  106         self.timer.stop()
  107         self.initial_start = []
  108         self.start = []
  109         self.end = []
  110         self.draw = False
  111         self.move_flag = False
  112         self.start = event.get_point()
  113         self.initial_start = event.get_point()
  114         self.spoint = self.select_point_by_click(self.start)
  115         if not self.spoint:
  116             self.timer.start()
  117 
  118     def mouse_move(self, event):
  119         if not self.start:
  120             return
  121         if not self.move_flag and not self.spoint:
  122             self.end = event.get_point()
  123             self.draw = True
  124         elif self.move_flag:
  125             self.end = event.get_point()
  126             trafo = self.get_trafo(self.start, self.end,
  127                                    event.is_ctrl(), event.is_shift())
  128             self.apply_trafo_to_selected(trafo)
  129             self.start = self.end
  130             self.canvas.selection_redraw()
  131         elif self.spoint:
  132             if self.spoint not in self.selected_points:
  133                 self.set_selected_points([self.spoint, ])
  134             self.move_flag = True
  135             self.trafos = deepcopy(self.target.trafos)
  136             self.canvas.selection_redraw()
  137 
  138     def mouse_up(self, event):
  139         self.timer.stop()
  140         self.end = event.get_point()
  141         if self.draw:
  142             self.draw = False
  143             points = self.select_points_by_bbox(self.start + self.end)
  144             self.set_selected_points(points, event.is_shift())
  145             self.canvas.selection_redraw()
  146         elif self.move_flag:
  147             self.move_flag = False
  148             trafo = self.get_trafo(self.start, self.end,
  149                                    event.is_ctrl(), event.is_shift())
  150             self.apply_trafo_to_selected(trafo, True)
  151             self.canvas.selection_redraw()
  152         elif self.spoint:
  153             self.set_selected_points([self.spoint, ], event.is_shift())
  154             self.canvas.selection_redraw()
  155         else:
  156             objs = self.canvas.pick_at_point(self.end)
  157             if objs and objs[0].is_primitive and not objs[0].is_pixmap:
  158                 self.selected_obj = objs[0]
  159                 self.start = []
  160                 self.canvas.set_mode(modes.SHAPER_MODE)
  161                 return
  162             else:
  163                 self.set_selected_points([])
  164                 self.canvas.selection_redraw()
  165         self.start = []
  166 
  167     # ----- POINT PROCESSING
  168 
  169     def set_selected_points(self, points, add=False):
  170         if add:
  171             for item in points:
  172                 if item not in self.selected_points:
  173                     self.selected_points.append(item)
  174                 else:
  175                     self.selected_points.remove(item)
  176         else:
  177             self.selected_points = points
  178 
  179     def select_points_by_bbox(self, bbox):
  180         ret = []
  181         bbox = libgeom.normalize_bbox(bbox)
  182         for item in self.points:
  183             if libgeom.is_point_in_bbox(item.get_screen_point(), bbox):
  184                 ret.append(item)
  185         return ret
  186 
  187     def select_point_by_click(self, point):
  188         for item in self.points:
  189             ipoint = item.get_screen_point()
  190             bbox = libgeom.bbox_for_point(ipoint, config.point_sensitivity_size)
  191             if libgeom.is_point_in_bbox(point, bbox):
  192                 return item
  193         return None
  194 
  195     def get_trafo(self, start, end, ctrl=False, shift=False):
  196         trafo = [1.0, 0.0, 0.0, 1.0]
  197         dchange = [0.0, 0.0]
  198         dstart = self.canvas.win_to_doc(start)
  199         dend = self.canvas.win_to_doc(end)
  200         if self.trafo_mode == modes.ET_MOVING_MODE:
  201             dchange = libgeom.sub_points(dend, dstart)
  202             if ctrl:
  203                 change = libgeom.sub_points(end, self.initial_start)
  204                 if abs(change[0]) > abs(change[1]):
  205                     dchange = [dchange[0], 0.0]
  206                 else:
  207                     dchange = [0.0, dchange[1]]
  208         return trafo + dchange
  209 
  210     def apply_trafo_to_selected(self, trafo, final=False):
  211         trafos = deepcopy(self.target.trafos)
  212         for item in self.selected_points:
  213             index = item.index
  214             if index in trafos.keys():
  215                 trafos[index] = libgeom.multiply_trafo(trafos[index], trafo)
  216             else:
  217                 trafos[index] = libgeom.multiply_trafo(self.target.trafo, trafo)
  218             item.apply_trafo(trafo)
  219         if not final:
  220             self.api.set_temp_text_trafos(self.target, trafos)
  221         else:
  222             self.api.set_text_trafos(self.target, trafos, self.trafos)
  223             self.trafos = {}
  224 
  225 
  226 class ControlPoint:
  227     canvas = None
  228     index = 0
  229     point = []
  230     trafo = []
  231     modified = False
  232 
  233     def __init__(self, canvas, index, point, trafo, modified=False):
  234         self.canvas = canvas
  235         self.index = index
  236         self.point = point
  237         self.trafo = trafo
  238         self.modified = modified
  239 
  240     def apply_trafo(self, trafo):
  241         self.trafo = libgeom.multiply_trafo(self.trafo, trafo)
  242 
  243     def get_point(self):
  244         return libgeom.apply_trafo_to_point(self.point, self.trafo)
  245 
  246     def get_screen_point(self):
  247         return self.canvas.point_doc_to_win(self.get_point())
  248 
  249     def repaint(self, selected=False):
  250         self.canvas.renderer.draw_text_point(self.get_screen_point(), selected)