summaryrefslogtreecommitdiff
path: root/cad/src/commands/CommentProperties/CommentProp.py
blob: 0afbeffdccb3c99e52961a36b155648bdcf0362a (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
# Copyright 2004-2008 Nanorex, Inc.  See LICENSE file for details. 
"""
CommentProp.py - 

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

History:

060520 mark: New feature for Alpha 8. Stores a comment in the MMP file,
accessible from the Model Tree as a node.

060522 bruce: minor changes, including changing representation of comment text.

080717 bruce, revise for coding standards
"""

from PyQt4.Qt import QDialog, QTextEdit, SIGNAL
from model.Comment import Comment
from commands.CommentProperties.CommentPropDialog import Ui_CommentPropDialog
import time
import foundation.env as env
from utilities.Log import redmsg, orangemsg, greenmsg, quote_html
from utilities.qt4transition import qt4todo
from utilities.debug import print_compact_traceback

cmd = greenmsg("Insert Comment: ") # TODO: rename this global

class CommentProp(QDialog, Ui_CommentPropDialog):
    """
    The Comment dialog allows the user to add a comment to the Model Tree, which
    is saved in the MMP file.
    """
    def __init__(self, win):
        QDialog.__init__(self, win) # win is parent.
        self.setupUi(self)
        self.win = win
        self.comment = None
        self.action = None
            # REVIEW, re self.action:
            # - if used only in messages (true in this file, but not yet
            #   analyzed re external files), should change initial value to "".
            # - if not used in external files, should make private.
            # [bruce 080717 comment]
    
    def setup(self, comment = None):
        """
        Show Comment dialog with current Comment text, for editing
        properties of an existing Comment node or creating a new one.
        
        @param comment: the comment node to edit, or None to create a new one.
        """
        self.comment = comment
        
        if self.comment:
            self.comment_textedit.setPlainText(self.comment.get_text())
                # Load comment text.
            qt4todo("self.comment_textedit.moveCursor(QTextEdit.MoveEnd, False)")
                # Sets cursor position to the end of the textedit document.
        
        QDialog.exec_(self)

    ###### Private methods ###############################
    
    def _create_comment(self):
        comment_text = self.comment_textedit.toPlainText()
        if not self.comment:
            self.comment = Comment(self.win.assy, None, comment_text)
            self.win.assy.addnode(self.comment)
            self.action = 'added'
        else: 
            self.comment.set_text(comment_text)
            self.action = 'updated'
            
    def _remove_comment(self):
        if self.comment:
            self.comment.kill()
            self.win.mt.mt_update()
            self.comment = None
            
    def insert_date_time_stamp(self):
        """
        Insert a date/time stamp in the comment at the current position.

        @note: slot method for self.date_time_btn, SIGNAL("clicked()").
        """
        timestr = "%s " % time.strftime("%m/%d/%Y %I:%M %p")
        self.comment_textedit.insertPlainText(timestr)
        
    def _done_history_msg(self):
        env.history.message(cmd + quote_html("%s %s." % (self.comment.name, self.action)))
                #bruce Qt4 070502 precaution: use quote_html
    
    #################
    # Cancel Button
    #################
    def reject(self):
        QDialog.reject(self)
        self.comment = None
        self.comment_textedit.setPlainText("") # Clear text.

    #################
    # OK Button
    #################
    def accept(self):
        """
        Slot for the OK button
        """
        try:
            self._create_comment()
            self._done_history_msg()
            self.comment = None
        except Exception, e:
            print_compact_traceback("Bug: exception in CommentProp.accept: ") #bruce Qt4 070502
            env.history.message(cmd + redmsg("Bug: " + quote_html(" - ".join(map(str, e.args)))))
                #bruce Qt4 070502 bugfixes: use quote_html, say it's a bug (could say "internal error" if desired)
            self._remove_comment()
        self.win.mt.mt_update()
        QDialog.accept(self)
        self.comment_textedit.setPlainText("") # Clear text.

    pass # end of class CommentProp

# end