summaryrefslogtreecommitdiffstats
path: root/host/pymoistcontrol/commwidgets.py
blob: e57c6ac4247db39d01203e8ac7ce55377b15dd88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#
# Moisture control - Serial communication widgets
#
# Copyright (c) 2013 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

from pymoistcontrol.util import *

import os


class SerialOpenDialog(QDialog):
	"""Serial port connection dialog."""

	def __init__(self, parent):
		"""Class constructor."""

		QDialog.__init__(self, parent)
		self.setLayout(QGridLayout(self))

		self.setWindowTitle("Select serial port")

		self.portCombo = QComboBox(self)
		if os.name.lower() == "posix":
			# Unix operating system
			# Add all serial devices from /dev to
			# the combo box.
			devNodes = QDir("/dev").entryInfoList(QDir.System,
							      QDir.Name)
			select = None
			for node in devNodes:
				name = node.fileName()
				if not name.startswith("ttyS") and\
				   not name.startswith("ttyUSB"):
					continue
				path = node.filePath()
				self.portCombo.addItem(path, path)
				if select is None and\
				   name.startswith("ttyUSB"):
					# Select the first ttyUSB by default.
					select = self.portCombo.count() - 1
			if select is not None:
				self.portCombo.setCurrentIndex(select)
		elif os.name.lower() in ("nt", "ce"):
			# Windows operating system
			# Add 8 COM ports to the combo box.
			for i in range(8):
				port = "COM%d" % (i + 1)
				self.portCombo.addItem(port, port)
		else:
			raise Error("Operating system not supported")
		self.layout().addWidget(self.portCombo, 0, 0, 1, 2)

		self.okButton = QPushButton("&Ok", self)
		self.layout().addWidget(self.okButton, 1, 0)

		self.cancelButton = QPushButton("&Cancel", self)
		self.layout().addWidget(self.cancelButton, 1, 1)

		self.okButton.released.connect(self.accept)
		self.cancelButton.released.connect(self.reject)

	def getSelectedPort(self):
		"""Get the selected port name."""

		index = self.portCombo.currentIndex()
		if index < 0:
			return None
		return self.portCombo.itemData(index)
bues.ch cgit interface