views.remote: Give focus to the topmost widget
[git-cola.git] / cola / views / remote.py
blobb80d56852796b9af87794eb1b8ad429fd9cfdd62
1 from PyQt4 import QtCore
2 from PyQt4 import QtGui
3 from PyQt4.QtCore import SIGNAL
5 from cola.views import standard
8 class RemoteView(standard.StandardDialog):
9 """A dialog for choosing branches."""
11 def __init__(self, parent, action):
12 """Customizes the dialog based on the remote action
13 """
14 standard.StandardDialog.__init__(self, parent=parent)
16 self.resize(666, 606)
17 self._main_vbox_layt = QtGui.QVBoxLayout(self)
19 # Local branch section
20 self._local_branch_hbox_layt = QtGui.QHBoxLayout()
21 # Exposed
22 self.local_label = QtGui.QLabel(self)
23 self.local_label.setText(self.tr('Local Branches'))
24 # Exposed
25 self.local_branch = QtGui.QLineEdit(self)
26 # Exposed
27 self.local_branches = QtGui.QListWidget(self)
29 # Remote branch section
30 self._remote_branch_hbox_layt = QtGui.QHBoxLayout()
31 self._remote_label = QtGui.QLabel(self)
32 self._remote_label.setText(self.tr('Remote'))
33 # Exposed
34 self.remotename = QtGui.QLineEdit(self)
35 # Exposed
36 self.remotes = QtGui.QListWidget(self)
38 self._remote_branches_hbox_layt = QtGui.QHBoxLayout()
39 # Exposed
40 self.remote_label = QtGui.QLabel(self)
41 self.remote_label.setText(self.tr('Remote Branch'))
42 # Exposed
43 self.remote_branch = QtGui.QLineEdit(self)
44 self.remote_branches = QtGui.QListWidget(self)
46 self._options_hbox_layt = QtGui.QHBoxLayout()
47 # Exposed
48 self.ffwd_only_checkbox = QtGui.QCheckBox(self)
49 self.ffwd_only_checkbox.setText(self.tr('Fast Forward Only'))
50 self.ffwd_only_checkbox.setChecked(True)
51 self.ffwd_only_checkbox.setObjectName("ffwd_only_checkbox")
53 # Exposed
54 self.tags_checkbox = QtGui.QCheckBox(self)
55 self.tags_checkbox.setText(self.tr('Include tags'))
57 self.rebase_checkbox = QtGui.QCheckBox(self)
58 self.rebase_checkbox.setText(self.tr('Rebase'))
60 self._options_spacer = QtGui.QSpacerItem(1, 1,
61 QtGui.QSizePolicy.Expanding,
62 QtGui.QSizePolicy.Minimum)
63 self._options_hbox_layt.addItem(self._options_spacer)
64 # Exposed
65 self.action_button = QtGui.QPushButton(self)
66 self.action_button.setText(self.tr('Push'))
67 # Exposed
68 self.cancel_button = QtGui.QPushButton(self)
69 self.cancel_button.setText(self.tr('Cancel'))
71 # connections
72 self.connect(self.cancel_button, SIGNAL('released()'), self.reject)
74 if action:
75 self.action_button.setText(action.title())
76 self.setWindowTitle(action.title())
78 if action == 'pull':
79 self.tags_checkbox.hide()
80 self.ffwd_only_checkbox.hide()
81 self.local_label.hide()
82 self.local_branch.hide()
83 self.local_branches.hide()
84 self.remote_branch.setFocus(True)
85 else:
86 self.rebase_checkbox.hide()
88 # Action-dependent focus
89 if action == 'fetch':
90 self.remotename.setFocus(True)
92 if action == 'push':
93 self.local_branch.setFocus(True)
95 if action == 'fetch':
96 self.layout_remotes()
97 self.layout_remote_branches()
98 self.layout_local_branches()
99 elif action == 'pull':
100 self.layout_remote_branches()
101 self.layout_remotes()
102 self.layout_local_branches()
103 else:
104 self.layout_local_branches()
105 self.layout_remotes()
106 self.layout_remote_branches()
108 self.layout_options()
110 def layout_options(self):
111 self._options_hbox_layt.addWidget(self.ffwd_only_checkbox)
112 self._options_hbox_layt.addWidget(self.tags_checkbox)
113 self._options_hbox_layt.addWidget(self.rebase_checkbox)
114 self._options_hbox_layt.addWidget(self.action_button)
115 self._options_hbox_layt.addWidget(self.cancel_button)
116 self._main_vbox_layt.addLayout(self._options_hbox_layt)
118 def layout_local_branches(self):
119 self._local_branch_hbox_layt.addWidget(self.local_label)
120 self._local_branch_hbox_layt.addWidget(self.local_branch)
121 self._main_vbox_layt.addLayout(self._local_branch_hbox_layt)
122 self._main_vbox_layt.addWidget(self.local_branches)
124 def layout_remotes(self):
125 self._remote_branch_hbox_layt.addWidget(self._remote_label)
126 self._remote_branch_hbox_layt.addWidget(self.remotename)
127 self._main_vbox_layt.addLayout(self._remote_branch_hbox_layt)
128 self._main_vbox_layt.addWidget(self.remotes)
130 def layout_remote_branches(self):
131 self._remote_branches_hbox_layt.addWidget(self.remote_label)
132 self._remote_branches_hbox_layt.addWidget(self.remote_branch)
133 self._main_vbox_layt.addLayout(self._remote_branches_hbox_layt)
134 self._main_vbox_layt.addWidget(self.remote_branches)
136 def select_first_remote(self):
137 """Selects the first remote in the list view"""
138 return self.select_remote(0)
140 def select_remote(self, idx):
141 """Selects a remote by index"""
142 item = self.remotes.item(idx)
143 if item:
144 self.remotes.setItemSelected(item, True)
145 self.remotes.setCurrentItem(item)
146 self.remotename.setText(item.text())
147 return True
148 else:
149 return False
151 def select_local_branch(self, idx):
152 """Selects a local branch by index in the list view"""
153 item = self.local_branches.item(idx)
154 if not item:
155 return False
156 self.local_branches.setItemSelected(item, True)
157 self.local_branches.setCurrentItem(item)
158 self.local_branch.setText(item.text())
159 return True
162 if __name__ == "__main__":
163 import sys
164 app = QtGui.QApplication(sys.argv)
165 remote = RemoteView()
166 remote.show()
167 sys.exit(app.exec_())