summaryrefslogtreecommitdiff
path: root/cad/src/graphics/drawing/drawDnaLabels.py
blob: b2f5262f4d0106d6243969c5969d55aadfcb74ae (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
# Copyright 2008 Nanorex, Inc.  See LICENSE file for details. 
"""

@author: Ninad
@copyright: 2008 Nanorex, Inc.  See LICENSE file for details.
@version:$Id$

History:
2008-08-14 created.
Originally created in BreakOrJoinstrands_GraphicsMode. Moved into its own 
module as its a general helper function used by many GraphicsModes

TODO:
- allow prefs_keys as an argument for draw_dnaBaseNumberLabels
- refactor. some code (sub def func in draw_dnaBaseNumberLabels)
  may better be in a dna/model class?
"""
import foundation.env as env
from PyQt4.Qt import QFont, QString

from utilities.prefs_constants import dnaBaseNumberLabelColor_prefs_key
from utilities.prefs_constants import dnaBaseNumberingOrder_prefs_key
from utilities.prefs_constants import dnaBaseNumberLabelChoice_prefs_key


def _correct_baseatom_order_for_dnaStrand(strand, baseatoms):
    """
    See a TODO comment in this method body.
    @see: _draw_dnaBaseNumberLabels()
    
    """
    #@TODO: REVISE this. Its only called from _draw_dnaBaseNumberLabels()
    #See if this method should be a moved to DnaStrand class and 
    #some portion of the ._draw_dnaBaseNumberLabels() that returns 
    #baseatoms to class DnaStrandOrSegment. Issue with this refactoring: 
    #there is a method in DnaStrand class that returns baseatoms in bond
    #direction. May be it needs to be revised/ replaced with 
    #wholechain.get_all_base_atoms_in_order()
    #-- Ninad 2008-08-06
    numberingOrder = env.prefs[dnaBaseNumberingOrder_prefs_key]
    five_prime_end = strand.get_five_prime_end_base_atom()
    if five_prime_end: 
        if numberingOrder == 0:
            if not five_prime_end is baseatoms[0]:
                baseatoms.reverse()
        elif numberingOrder == 1:
            if five_prime_end is baseatoms[0]:
                baseatoms.reverse()
    return baseatoms


def draw_dnaBaseNumberLabels(glpane):
    """
    Draw the DNA basee number labels.        
    
    baseNumLabelChoice:(obtained from command class)
    0 =  None
    1 = Strands and Segments
    2 =  Strands Only
    3 =  Segments Only
    
    @see: _correct_baseatom_order_for_dnaStrand()
    @see: BuildDna_GraphicsMode._drawLabels()
    """
    
    win = glpane.win
    
    baseNumLabelChoice = env.prefs[dnaBaseNumberLabelChoice_prefs_key]
    
    if glpane.scale > 65.0:
        fontSize = 9
    else:
        fontSize = 12
    
    if baseNumLabelChoice == 0:
        return 
    
       
    segments = win.assy.part.get_topmost_subnodes_of_class(win.assy.DnaSegment)
    strands =  win.assy.part.get_topmost_subnodes_of_class(win.assy.DnaStrand)
    
    font = QFont( QString("Helvetica"), fontSize)
    textColor = env.prefs[dnaBaseNumberLabelColor_prefs_key]
    # WARNING: Anything smaller than 9 pt on Mac OS X results in 
    # un-rendered text.
                
    
    def func(strandOrSegmentList):        
        for strandOrSegment in strandOrSegmentList:
            #Don't draw the labels if the strand or segment is hidden
            if strandOrSegment.all_content_is_hidden():
                continue
            whole_chain = strandOrSegment.get_wholechain()
            if whole_chain is None:
                continue
            baseatoms = whole_chain.get_all_baseatoms_in_order()
            
            if isinstance(strandOrSegment, win.assy.DnaStrand):
                baseatoms = _correct_baseatom_order_for_dnaStrand(
                    strandOrSegment,
                    baseatoms)
                
            i = 1
            for atm in baseatoms:
                text = "%d" %(i)
                highlighting_radius = atm.highlighting_radius()
                if highlighting_radius < 1.2:
                    highlighting_radius = 4.0
                pos = atm.posn() + (0.03+ highlighting_radius)*glpane.out ##+ (glpane.right + glpane.up)
                
                glpane.renderTextAtPosition(pos, 
                                            text, 
                                            textColor = textColor, 
                                            textFont = font)
               
                i += 1
                
    if baseNumLabelChoice in (1, 2):
        func(strands)
    if baseNumLabelChoice in (1, 3):
        func(segments)