source: orange/Orange/OrangeCanvas/preview/previewdialog.py @ 11206:49f2a7576167

Revision 11206:49f2a7576167, 3.4 KB checked in by Ales Erjavec <ales.erjavec@…>, 17 months ago (diff)

Added Tutorial dialog.

Line 
1"""
2A dialog widget for selecting an item.
3"""
4
5from PyQt4.QtGui import QDialog, QWidget, QVBoxLayout, QDialogButtonBox, \
6                        QStringListModel, QLabel, QSizePolicy
7
8from PyQt4.QtCore import Qt
9from PyQt4.QtCore import pyqtSignal as Signal
10
11from . import previewbrowser
12
13
14class PreviewDialog(QDialog):
15    """A Dialog for selecting an item from a PreviewItem.
16    """
17    currentIndexChanged = Signal(int)
18
19    def __init__(self, parent=None, flags=Qt.WindowFlags(0),
20                 model=None, **kwargs):
21        QDialog.__init__(self, parent, flags, **kwargs)
22
23        self.__setupUi()
24        if model is not None:
25            self.setModel(model)
26
27    def __setupUi(self):
28        layout = QVBoxLayout()
29        layout.setContentsMargins(0, 0, 0, 0)
30        self.setContentsMargins(0, 0, 0, 0)
31
32        heading = self.tr("Preview")
33        heading = u"<h3>{0}</h3>".format(heading)
34        self.__heading = QLabel(heading, self,
35                                objectName="heading")
36
37        self.__heading.setContentsMargins(12, 12, 12, 0)
38
39        self.__browser = previewbrowser.PreviewBrowser(self)
40
41        self.__buttons = QDialogButtonBox(QDialogButtonBox.Open | \
42                                          QDialogButtonBox.Cancel,
43                                          Qt.Horizontal,)
44        self.__buttons.button(QDialogButtonBox.Open).setAutoDefault(True)
45
46        # Set the Open dialog as disabled until the current index changes
47        self.__buttons.button(QDialogButtonBox.Open).setEnabled(False)
48
49        # The QDialogButtonsWidget messes with the layout if it is
50        # contained directly in the QDialog. So we create an extra
51        # layer of indirection.
52        buttons = QWidget(objectName="button-container")
53        buttons_l = QVBoxLayout()
54        buttons_l.setContentsMargins(12, 0, 12, 12)
55        buttons.setLayout(buttons_l)
56
57        buttons_l.addWidget(self.__buttons)
58
59        layout.addWidget(self.__heading)
60        layout.addWidget(self.__browser)
61
62        layout.addWidget(buttons)
63
64        self.__buttons.accepted.connect(self.accept)
65        self.__buttons.rejected.connect(self.reject)
66        self.__browser.currentIndexChanged.connect(
67            self.__on_currentIndexChanged
68        )
69
70        layout.setSizeConstraint(QVBoxLayout.SetFixedSize)
71        self.setLayout(layout)
72        self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
73
74    def setItems(self, items):
75        """Set the items (a list of strings) for preview/selection.
76        """
77        model = QStringListModel(items)
78        self.setModel(model)
79
80    def setModel(self, model):
81        """Set the model for preview/selection.
82        """
83        self.__browser.setModel(model)
84
85    def model(self):
86        """Return the model.
87        """
88        return self.__browser.model()
89
90    def currentIndex(self):
91        return self.__browser.currentIndex()
92
93    def setCurrentIndex(self, index):
94        """Set the current selected (shown) index.
95        """
96        self.__browser.setCurrentIndex(index)
97
98    def setHeading(self, heading):
99        """Set `heading` as the heading string ('<h3>Preview</h3>'
100        by default).
101
102        """
103        self.__heading.setText(heading)
104
105    def heading(self):
106        """Return the heading string.
107        """
108    def __on_currentIndexChanged(self, index):
109        button = self.__buttons.button(QDialogButtonBox.Open)
110        button.setEnabled(index >= 0)
111        self.currentIndexChanged.emit(index)
Note: See TracBrowser for help on using the repository browser.