summaryrefslogtreecommitdiff
path: root/cad/src/ne1_ui/NE1_QWidgetAction.py
blob: 144b9f1118990854fc3a96372b3de90613e82726 (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
# Copyright 2008 Nanorex, Inc.  See LICENSE file for details. 
"""

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

History:
Created on 2008-07-15 . The main purpose of this class was a workaround to
new Qt4.3.2 BUG 2916 in the QToolbar extension popup indicator. 

TODO:
"""

from PyQt4.Qt import QWidgetAction

from PyQt4.Qt import QToolButton
from PyQt4.Qt import Qt


def truncateText(text, length = 12, truncateSymbol = '...'):
    """
    Truncates the tooltip text with the given truncation symbol
    (three dots) in the case 
    """
        
    #ninad 070201 This is a temporary fix. Ideally it should show the whole
    #text in the  toolbutton. But there are some layout / size policy 
    #problems because of which the toolbar height increases after you print
    #tooltip text on two or more lines. (undesirable effect) 
        
    if not text:
        print "no text to truncate. Returning"
        return 
    
    truncatedLength  = length - len(truncateSymbol)
    
    if len(text) > length:
        return text[:truncatedLength] + truncateSymbol
    else:
        return text
    
    
def wrapToolButtonText(text):
    """
    Add a newline character at the end of each word in the toolbutton text
    """
    #ninad 070126 QToolButton lacks this method. This is not really a 
    #'word wrap' but OK for now. 
    
    #@@@ ninad 070126. Not calling this method as it is creating an annoying
    #resizing problem in the Command toolbar layout. Possible solution is 
    #to add a spacer item in a vbox layout to the command toolbar layout
    
    stringlist = text.split(" ", QString.SkipEmptyParts)
    text2 = QString()
    if len(stringlist) > 1:
        for l in stringlist:
            text2.append(l)
            text2.append("\n")
        return text2
            
    return None
    
    

_superclass = QWidgetAction

class NE1_QWidgetAction(_superclass):
    def __init__(self, parent, win = None):        
        _superclass.__init__(self, parent)
        self.win = win
        self._toolButtonPalette = None
            
    def createWidget(self, parent):
        
        cmdToolbar = self.win.commandToolbar
        
        btn = None
        if cmdToolbar:
            flyoutToolBar = cmdToolbar.flyoutToolBar  
            if flyoutToolBar:
                if 0:
                    print "***self.text() =%s, parent = %s"%(self.text(), 
                                                             parent)
        
            if parent is flyoutToolBar:   
                btn = self._createToolButtonWidget(parent)                

        return btn
    
    def _createToolButtonWidget(self, parent):
        """
        @see: self.createWidget()
        """
        btn = QToolButton(parent)
        btn.setAutoFillBackground(True)
        btn.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)      
        btn.setMinimumWidth(75)
        btn.setMaximumWidth(75)
        btn.setMinimumHeight(62) 
        btn.setAutoRaise(True)
        
        btn.setCheckable(True)   
        btn.setDefaultAction(self)
        
        text = truncateText(self.text())
        btn.setText(text)
        
        if self._toolButtonPalette:
            btn.setPalette(self._toolButtonPalette)
            
        #@@@ ninad070125 The following function 
        #adds a newline character after each word in toolbutton text. 
        #but the changes are reflected only on 'mode' toolbuttons 
        #on the flyout toolbar (i.e.only Checkable buttons..don't know 
        #why. Disabling its use for now. 
        debug_wrapText = False

        if debug_wrapText:
            text = wrapToolButtonText(action.text())
            if text:    
                action.setText(text) 
                
        return btn
                

    def setToolButtonPalette(self, palette):
        """
        @see: CommandToolbar._createFlyoutToolbar()
        """
        self._toolButtonPalette = palette