source: orange/Orange/OrangeCanvas/application/schemeinfo.py @ 11189:6e8b14ab7b9a

Revision 11189:6e8b14ab7b9a, 4.9 KB checked in by Ales Erjavec <ales.erjavec@…>, 17 months ago (diff)

Preserve empty scheme title, display 'untitled' placeholders where appropriate.

Line 
1"""
2Scheme Info editor widget.
3
4"""
5
6from PyQt4.QtGui import (
7    QWidget, QDialog, QLabel, QTextEdit, QCheckBox, QFormLayout,
8    QVBoxLayout, QHBoxLayout, QDialogButtonBox, QSizePolicy
9)
10
11from PyQt4.QtCore import Qt
12
13from ..gui.lineedit import LineEdit
14from ..gui.utils import StyledWidget_paintEvent, StyledWidget
15
16
17class SchemeInfoEdit(QWidget):
18    """Scheme info editor widget.
19    """
20    def __init__(self, *args, **kwargs):
21        QWidget.__init__(self, *args, **kwargs)
22        self.scheme = None
23        self.__setupUi()
24
25    def __setupUi(self):
26        layout = QFormLayout()
27        layout.setRowWrapPolicy(QFormLayout.WrapAllRows)
28        layout.setFieldGrowthPolicy(QFormLayout.ExpandingFieldsGrow)
29
30        self.name_edit = LineEdit(self)
31        self.name_edit.setPlaceholderText(self.tr("untitled"))
32        self.name_edit.setSizePolicy(QSizePolicy.Expanding,
33                                     QSizePolicy.Fixed)
34        self.desc_edit = QTextEdit(self)
35
36        layout.addRow(self.tr("Name"), self.name_edit)
37        layout.addRow(self.tr("Description"), self.desc_edit)
38
39        self.__schemeIsUntitled = True
40
41        self.setLayout(layout)
42
43    def setScheme(self, scheme):
44        """Set the scheme to display/edit
45
46        """
47        self.scheme = scheme
48        if not scheme.title:
49            self.name_edit.setText(self.tr("untitled"))
50            self.__schemeIsUntitled = True
51        else:
52            self.name_edit.setText(scheme.title)
53            self.__schemeIsUntitled = False
54        self.desc_edit.setPlainText(scheme.description or "")
55
56    def commit(self):
57        """Commit the current contents of the editor widgets
58        back to the scheme.
59
60        """
61        if self.__schemeIsUntitled and \
62            self.name_edit.text() == self.tr("untitled"):
63            # 'untitled' text was not changed
64            name = ""
65        else:
66            name = unicode(self.name_edit.text()).strip()
67
68        description = unicode(self.desc_edit.toPlainText()).strip()
69        self.scheme.title = name
70        self.scheme.description = description
71
72    def paintEvent(self, event):
73        return StyledWidget_paintEvent(self, event)
74
75
76class SchemeInfoDialog(QDialog):
77    def __init__(self, *args, **kwargs):
78        QDialog.__init__(self, *args, **kwargs)
79        self.scheme = None
80        self.__setupUi()
81
82    def __setupUi(self):
83        layout = QVBoxLayout()
84        layout.setContentsMargins(0, 0, 0, 0)
85        layout.setSpacing(0)
86
87        self.editor = SchemeInfoEdit(self)
88        self.editor.layout().setContentsMargins(20, 20, 20, 20)
89        self.editor.layout().setSpacing(15)
90        self.editor.setSizePolicy(QSizePolicy.MinimumExpanding,
91                                  QSizePolicy.MinimumExpanding)
92
93        heading = self.tr("Scheme Info")
94        heading = "<h3>{0}</h3>".format(heading)
95        self.heading = QLabel(heading, self, objectName="heading")
96
97        # Insert heading
98        self.editor.layout().insertRow(0, self.heading)
99
100        self.buttonbox = QDialogButtonBox(
101            QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
102            Qt.Horizontal,
103            self
104            )
105
106        # Insert button box
107        self.editor.layout().addRow(self.buttonbox)
108
109        widget = StyledWidget(self, objectName="auto-show-container")
110        check_layout = QHBoxLayout()
111        check_layout.setContentsMargins(20, 10, 20, 10)
112        self.__dontShowAtNewSchemeCheck = \
113            QCheckBox(self.tr("Don't show again when I make a New Scheme."),
114                      self,
115                      objectName="auto-show-check",
116                      checked=False,
117                      )
118
119        check_layout.addWidget(self.__dontShowAtNewSchemeCheck)
120        check_layout.addWidget(
121               QLabel(self.tr("You can edit and add Scheme Info later at the "
122                              "bottom of the menu"),
123                      self,
124                      objectName="auto-show-info"),
125               alignment=Qt.AlignRight)
126        widget.setLayout(check_layout)
127        widget.setSizePolicy(QSizePolicy.MinimumExpanding,
128                             QSizePolicy.Fixed)
129
130        self.buttonbox.accepted.connect(self.editor.commit)
131        self.buttonbox.accepted.connect(self.accept)
132        self.buttonbox.rejected.connect(self.reject)
133
134        layout.addWidget(self.editor, stretch=10)
135        layout.addWidget(widget)
136
137        self.setLayout(layout)
138
139    def setDontShowAtNewScheme(self, checked):
140        """Set the 'Dont show at new scheme' check state.
141        """
142        self.__dontShowAtNewSchemeCheck.setChecked(checked)
143
144    def dontShowAtNewScheme(self):
145        """Return the check state of the 'Dont show at new scheme' check box.
146        """
147        return self.__dontShowAtNewSchemeCheck.isChecked()
148
149    def setScheme(self, scheme):
150        """Set the scheme to display/edit.
151        """
152        self.scheme = scheme
153        self.editor.setScheme(scheme)
Note: See TracBrowser for help on using the repository browser.