source: orange/Orange/OrangeCanvas/preview/previewdialog.py @ 11215:a55eaa72e6ef

Revision 11215:a55eaa72e6ef, 3.6 KB checked in by Ales Erjavec <ales.erjavec@…>, 17 months ago (diff)

Select and accept a scheme with a double click on an item in the preview list.

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        self.__browser.activated.connect(self.__on_activated)
70
71        layout.setSizeConstraint(QVBoxLayout.SetFixedSize)
72        self.setLayout(layout)
73        self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
74
75    def setItems(self, items):
76        """Set the items (a list of strings) for preview/selection.
77        """
78        model = QStringListModel(items)
79        self.setModel(model)
80
81    def setModel(self, model):
82        """Set the model for preview/selection.
83        """
84        self.__browser.setModel(model)
85
86    def model(self):
87        """Return the model.
88        """
89        return self.__browser.model()
90
91    def currentIndex(self):
92        return self.__browser.currentIndex()
93
94    def setCurrentIndex(self, index):
95        """Set the current selected (shown) index.
96        """
97        self.__browser.setCurrentIndex(index)
98
99    def setHeading(self, heading):
100        """Set `heading` as the heading string ('<h3>Preview</h3>'
101        by default).
102
103        """
104        self.__heading.setText(heading)
105
106    def heading(self):
107        """Return the heading string.
108        """
109    def __on_currentIndexChanged(self, index):
110        button = self.__buttons.button(QDialogButtonBox.Open)
111        button.setEnabled(index >= 0)
112        self.currentIndexChanged.emit(index)
113
114    def __on_activated(self, index):
115        if self.currentIndex() != index:
116            self.setCurrentIndex(index)
117
118        self.accept()
Note: See TracBrowser for help on using the repository browser.