Death to .ui: Add a 'ComboView' class
[git-cola.git] / cola / views / combo.py
blob9c1111a20d0e6ed27864591f9970222287c41e1f
1 from PyQt4 import QtCore
2 from PyQt4 import QtGui
3 from PyQt4.QtCore import SIGNAL
5 from cola.views import standard
8 class ComboView(standard.StandardDialog):
9 """A dialog for choosing branches."""
11 def __init__(self, parent=None, title='', items=None):
12 standard.StandardDialog.__init__(self, parent=parent)
14 self.setWindowTitle(title)
15 self.resize(400, 73)
16 self._main_layt = QtGui.QVBoxLayout(self)
18 # Exposed
19 self.items_widget = QtGui.QComboBox(self)
20 self.items_widget.setEditable(True)
22 self._main_layt.addWidget(self.items_widget)
24 self.button_box = QtGui.QDialogButtonBox(self)
25 self.button_box.setOrientation(QtCore.Qt.Horizontal)
26 self.button_box.setStandardButtons(QtGui.QDialogButtonBox.Ok |
27 QtGui.QDialogButtonBox.Cancel)
29 self._main_layt.addWidget(self.button_box)
30 self.setTabOrder(self.items_widget, self.button_box)
32 if items:
33 self.items_widget.addItems(items)
35 self.connect(self.button_box, SIGNAL('accepted()'), self.accept)
36 self.connect(self.button_box, SIGNAL('rejected()'), self.reject)
38 def idx(self):
39 return self.items_widget.currentIndex()
41 def value(self):
42 return str(self.items_widget.currentText())
44 def selected(self):
45 """Present the dialog and return the chosen item."""
46 geom = QtGui.QApplication.instance().desktop().screenGeometry()
47 width = geom.width()
48 height = geom.height()
49 if self.parent():
50 x = self.parent().x() + self.parent().width()/2 - self.width()/2
51 y = self.parent().y() + self.parent().height()/3 - self.height()/2
52 self.move(x, y)
53 self.show()
54 if self.exec_() == QtGui.QDialog.Accepted:
55 return self.value()
56 else:
57 return None
60 if __name__ == "__main__":
61 import sys
62 app = QtGui.QApplication(sys.argv)
63 combo = ComboView()
64 combo.show()
65 sys.exit(app.exec_())