source: orange/Orange/OrangeCanvas/preview/previewdialog.py @ 11139:3cab9e06f6e5

Revision 11139:3cab9e06f6e5, 2.8 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Fixes for unicode support.

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("Recent Schemes")
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).setDefault(True)
45
46        # The QDialogButtonsWidget messes with the layout if it is
47        # contained directly in the QDialog. So we create an extra
48        # layer of indirection.
49        buttons = QWidget(objectName="button-container")
50        buttons_l = QVBoxLayout()
51        buttons_l.setContentsMargins(12, 0, 12, 12)
52        buttons.setLayout(buttons_l)
53
54        buttons_l.addWidget(self.__buttons)
55
56        layout.addWidget(self.__heading)
57        layout.addWidget(self.__browser)
58
59        layout.addWidget(buttons)
60
61        self.__buttons.accepted.connect(self.accept)
62        self.__buttons.rejected.connect(self.reject)
63        self.__browser.currentIndexChanged.connect(self.currentIndexChanged)
64
65        layout.setSizeConstraint(QVBoxLayout.SetFixedSize)
66        self.setLayout(layout)
67        self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
68
69    def setItems(self, items):
70        """Set the items (a list of strings) for preview/selection.
71        """
72        model = QStringListModel(items)
73        self.setModel(model)
74
75    def setModel(self, model):
76        """Set the model for preview/selection.
77        """
78        self.__browser.setModel(model)
79
80    def model(self):
81        """Return the model.
82        """
83        return self.__browser.model()
84
85    def currentIndex(self):
86        return self.__browser.currentIndex()
87
88    def setCurrentIndex(self, index):
89        """Set the current selected (shown) index.
90        """
91        self.__browser.setCurrentIndex(index)
Note: See TracBrowser for help on using the repository browser.