~onboard/onboard/trunk

« back to all changes in this revision

Viewing changes to Onboard/KeyCommon.py

  • Committer: marmuta
  • Date: 2017-02-10 10:33:11 UTC
  • mto: This revision was merged to the branch mainline in revision 2275.
  • Revision ID: marmvta@gmail.com-20170210103311-vthzd1ao0pg3vv9b
Initial emoji & symbol palette for Compact layout.

Not quite complete yet, but usable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
from Onboard.utils import Rect, LABEL_MODIFIERS, Modifiers, \
34
34
                          polygon_to_rounded_path
35
35
 
36
 
from Onboard.Layout import LayoutItem
 
36
from Onboard.Layout import DrawingItem
37
37
 
38
38
### Logging ###
39
39
import logging
168
168
    NORMAL = 0
169
169
    ACTIVE = 1
170
170
 
171
 
class KeyCommon(LayoutItem):
 
171
class KeyCommon(DrawingItem):
172
172
    """
173
173
    library-independent key class. Specific rendering options
174
174
    are stored elsewhere.
175
175
    """
176
176
 
177
 
    # extended id for key specific theme tweaks
178
 
    # e.g. theme_id=DELE.numpad (with id=DELE)
179
 
    theme_id = None
180
 
 
181
 
    # extended id for layout specific tweaks
182
 
    # e.g. "hide.wordlist", for hide button in wordlist mode
183
 
    svg_id = None
184
 
 
185
177
    # optional id of a sublayout used as long-press popup
186
178
    popup_id = None
187
179
 
260
252
###################
261
253
 
262
254
    def __init__(self):
263
 
        LayoutItem.__init__(self)
 
255
        super(KeyCommon, self).__init__()
264
256
 
265
257
    def configure_label(self, mod_mask):
266
258
        SHIFT = Modifiers.SHIFT
342
334
    def is_active(self):
343
335
        return not self.type is None
344
336
 
345
 
    def get_id(self):
346
 
        return ""
347
 
 
348
 
    def get_svg_id(self):
349
 
        return ""
350
 
 
351
 
    def set_id(self, id, theme_id = None, svg_id = None):
352
 
        self.theme_id, self.id = self.parse_id(id)
353
 
        if theme_id:
354
 
            self.theme_id = theme_id
355
 
        self.svg_id = self.id if not svg_id else svg_id
356
 
 
357
 
    @staticmethod
358
 
    def parse_id(value):
359
 
        """
360
 
        The theme id has the form <id>.<arbitrary identifier>, where
361
 
        the identifier should be a description of the location of
362
 
        the key relative to its surroundings, e.g. 'DELE.next-to-backspace'.
363
 
        Don't use layout names or layer ids for the theme id, they lose
364
 
        their meaning when layouts are copied or renamed by users.
365
 
        """
366
 
        theme_id = value
367
 
        id = value.split(".")[0]
368
 
        return theme_id, id
369
 
 
370
 
    @staticmethod
371
 
    def split_theme_id(theme_id):
372
 
        """
373
 
        Simple split in prefix (id) before the dot and suffix after the dot.
374
 
        """
375
 
        components = theme_id.split(".")
376
 
        if len(components) == 1:
377
 
            return components[0], ""
378
 
        return components[0], components[1]
379
 
 
380
 
    @staticmethod
381
 
    def build_theme_id(prefix, postfix):
382
 
        if postfix:
383
 
            return prefix + "." + postfix
384
 
        return prefix
385
 
 
386
 
    def get_similar_theme_id(self, prefix = None):
387
 
        if prefix is None:
388
 
            prefix = self.id
389
 
        theme_id = prefix
390
 
        comps = self.theme_id.split(".")[1:]
391
 
        if comps:
392
 
            theme_id += "." + comps[0]
393
 
        return theme_id
394
 
 
395
337
    def is_layer_button(self):
396
338
        return bool(self.target_layer_id) or self.id.startswith("layer")
397
339
 
419
361
                           "middleclick",
420
362
                           "doubleclick",
421
363
                           "dragclick"]
 
364
 
422
365
    def is_button(self):
423
366
        return self.type == BUTTON_TYPE
424
367
 
425
368
    def is_pressed_only(self):
426
 
        return self.pressed and not (self.active or \
427
 
                                     self.locked or \
 
369
        return self.pressed and not (self.active or
 
370
                                     self.locked or
428
371
                                     self.scanned)
429
372
 
 
373
    def is_active_only(self):
 
374
        return self.active and not (self.pressed or
 
375
                                    self.locked or
 
376
                                    self.scanned)
 
377
 
430
378
    def is_text_changing(self):
431
379
        if not self.is_modifier() and \
432
380
               self.type in [KEYCODE_TYPE,
478
426
    def get_target_layer_id(self):
479
427
        return self.target_layer_id
480
428
 
 
429
    def get_target_layer_parent_id(self):
 
430
        """
 
431
        Split off the parent path of the (dot-separated)
 
432
        target_layer_id.
 
433
        """
 
434
        layer_id = self.target_layer_id
 
435
        pos = layer_id.rfind(".")
 
436
        if pos >= 0:
 
437
            return layer_id[:pos]
 
438
        else:
 
439
            return None
 
440
 
481
441
    def get_popup_layout(self):
482
442
        if self.popup_id:
483
443
            return self.find_sublayout(self.popup_id)
517
477
    def __init__(self, id, border_rect):
518
478
        KeyCommon.__init__(self)
519
479
        self.id = id
520
 
        self.colors = {}
521
480
        self.context.log_rect = border_rect \
522
481
                                if not border_rect is None else Rect()
523
482
 
524
 
    def get_id(self):
525
 
        return self.id
526
 
 
527
483
    def get_svg_id(self):
528
484
        return self.svg_id
529
485
 
584
540
    def get_light_direction(self):
585
541
        return config.theme_settings.key_gradient_direction * pi / 180.0
586
542
 
587
 
    def get_fill_color(self):
588
 
        return self._get_color("fill")
589
 
 
590
 
    def get_stroke_color(self):
591
 
        return self._get_color("stroke")
592
 
 
593
 
    def get_label_color(self):
594
 
        return self._get_color("label")
595
 
 
596
 
    def get_secondary_label_color(self):
597
 
        return self._get_color("secondary-label")
598
 
 
599
 
    def get_dwell_progress_color(self):
600
 
        return self._get_color("dwell-progress")
601
 
 
602
543
    def get_dwell_progress_canvas_rect(self):
603
544
        rect = self.get_label_rect().inflate(0.5)
604
545
        return self.context.log_to_canvas_rect(rect)
605
546
 
606
 
    def _get_color(self, element):
607
 
        color_key = (element, self.prelight, self.pressed,
608
 
                              self.active, self.locked,
609
 
                              self.sensitive, self.scanned)
610
 
        rgba = self.colors.get(color_key)
611
 
        if not rgba:
612
 
            if self.color_scheme:
613
 
                rgba = self.color_scheme.get_key_rgba(self, element)
614
 
            elif element == "label":
615
 
                rgba = [0.0, 0.0, 0.0, 1.0]
616
 
            else:
617
 
                rgba = [1.0, 1.0, 1.0, 1.0]
618
 
            self.colors[color_key] = rgba
619
 
        return rgba
 
547
    def get_color(self, element, state = None):
 
548
        color_key = (element,
 
549
                     self.prelight, self.pressed,
 
550
                     self.active, self.locked,
 
551
                     self.sensitive, self.scanned)
 
552
        try:
 
553
            return self.colors[color_key]
 
554
        except KeyError:
 
555
            return self.cache_color(element, color_key, state)
620
556
 
621
557
    def get_fullsize_rect(self):
622
558
        """ Get bounding box of the key at 100% size in logical coordinates """
623
 
        return LayoutItem.get_rect(self)
 
559
        return DrawingItem.get_rect(self)
624
560
 
625
561
    def get_canvas_fullsize_rect(self):
626
562
        """ Get bounding box of the key at 100% size in canvas coordinates """