Ejemplo n.º 1
0
	def about(self):
		self.about_window = QtGui.QWidget()
		self.about = Ui_About()
		self.about.setupUi(self.about_window)
		self.about_window.setStyleSheet(self.style)
		self.about.btn_close.pressed.connect(self.about_window.close)
		self.about_window.setWindowFlags(QtCore.Qt.FramelessWindowHint)
		self.about_window.show()
Ejemplo n.º 2
0
class EDStatusWindow(QtGui.QMainWindow):
	def __init__(self, cmdr, settings, style):  # If called with no argument, this becomes the main window
		"""
		Main GUI class for the application. Generates a Frame which contains all data in one place.
		"""
		QtGui.QMainWindow.__init__(self)
		self.cmdr = cmdr
		self.settings = settings
		self.style = style

		self.status_window = Ui_MainWindow()
		self.status_window.setupUi(self)
		self.setStyleSheet(self.style)

		# File menu
		self.status_window.actionUpdate_EDDB_data.triggered.connect(self.update_eddb)
		self.status_window.actionSettings.triggered.connect(self.settings.open_settings_window)
		self.status_window.actionExit.triggered.connect(self.close)
		self.status_window.actionAbout.triggered.connect(self.about)

		# Buttons
		self.status_window.btn_quit.pressed.connect(self.close)
		self.status_window.btn_refresh.pressed.connect(self.cmdr.refresh_api)
		self.status_window.btn_statistics.pressed.connect(self.close)

		self.add_api_table()  # Populates our table with data from the API

		self.show()


	def add_api_table(self):
		"""
		Frame with categories and values (not using QTableWidget, but build it from labels in grid.
		"""
		palette = QtGui.QPalette()
		palette.setColor(QtGui.QPalette.Foreground,QtCore.Qt.darkYellow)
		for n in range(len(self.cmdr.values)):
			category = self.cmdr.categories[n]
			methodToCall = getattr(self.status_window, 'lbl_value_' + str(n))  # Creates method names that we can call
			if category == 'Current system':
				url = 'http://eddb.io/system/' + self.cmdr.systems[self.cmdr.api.profile['lastSystem']['name']]
				value = '<A href="' + url + '" style="color:#ff7f27;">' + self.cmdr.values[n] + '</a>'
			elif category == 'Last docked':
				url = 'http://eddb.io/station/' + self.cmdr.stations[self.cmdr.api.profile['lastStarport']['name']]
				value = '<A href="' + url + '" style="color:#ff7f27;">' + self.cmdr.values[n] + '</a>'
			else:
				value = self.cmdr.values[n]

			methodToCall.setText(value + ' ')  # An extra padding for italics underline links

	def update_eddb(self):  # TODO: change window to Qt Designer
		explanation = (
			'This will download systems.json and stations.json from EDDB.io,' +
			'which enables hyperlinks on station and system names.\n\n' +
		    'You typically only need to do this once, or if you are in some '+
			'exotic location in space which has only been recently discovered.'
		)

		reply = QtGui.QMessageBox.question(self, 'Message',
		         explanation, QtGui.QMessageBox.Yes, QtGui.QMessageBox.Cancel)

		if reply == QtGui.QMessageBox.Cancel:
			return

		self.win_download = QtGui.QWidget()
		self.win_download.setWindowTitle('Updating EDDB data')
		self.win_download.setStyleSheet(self.style)
		self.win_download.resize(230, 165)
		self.win_download.move(300, 300)

		grid = QtGui.QGridLayout(self.win_download)
		grid.setColumnMinimumWidth(0, 200)
		grid.setColumnMinimumWidth(1, 40)

		# For animated GIF, we use QMovie
		try:
			gif_busy = QtGui.QMovie('icons/throbber.gif')
			gif_check = QtGui.QPixmap(':icons/check-2x.png')
		except FileNotFoundError as err:
			print(err)
			exit(1)

		self.lbl_systems_update = QtGui.QLabel()
		self.lbl_stations_update = QtGui.QLabel()
		self.lbl_systems_conversion = QtGui.QLabel()
		self.lbl_stations_conversion = QtGui.QLabel()

		grid.addWidget(QtGui.QLabel('Systems update downloading'), 0, 0)
		grid.addWidget(QtGui.QLabel('Stations update downloading'), 1, 0)
		grid.addWidget(QtGui.QLabel('Converting systems JSON data'), 2, 0)
		grid.addWidget(QtGui.QLabel('Converting stations JSON data'), 3, 0)

		grid.addWidget(self.lbl_systems_update, 0, 1)
		grid.addWidget(self.lbl_stations_update, 1, 1)
		grid.addWidget(self.lbl_systems_conversion, 2, 1)
		grid.addWidget(self.lbl_stations_conversion, 3, 1)

		gif_busy.start()  # Starting animation
		self.win_download.show()

		""" We use threading to run download/conversion in the background to allow animated GIFs """
		self.pool = ThreadPool(processes=1)  # This is threads _not_ processes

		self.stations_json_path = os.path.dirname(os.path.abspath(self.settings.stationsDataFile))
		self.systems_json_path = os.path.dirname(os.path.abspath(self.settings.systemsDataFile))

		# Updating labels
		self.lbl_systems_update.setMovie(gif_busy)

		# Running systems download in thread
		self.async_result = self.pool.apply_async(self._download_systems)
		while not hasattr(self.async_result, '_success'):
			QtGui.QApplication.processEvents()

		# Updating labels
		self.lbl_systems_update.setPixmap(gif_check)
		self.lbl_stations_update.setMovie(gif_busy)

		# Running stations download in thread
		self.async_result = self.pool.apply_async(self._download_stations)
		while not hasattr(self.async_result, '_success'):
			QtGui.QApplication.processEvents()

		# Updating labels
		self.lbl_stations_update.setPixmap(gif_check)
		self.lbl_systems_conversion.setMovie(gif_busy)

		# Running systems conversion in thread
		self.async_result = self.pool.apply_async(self._convert_systems)
		while not hasattr(self.async_result, '_success'):
			QtGui.QApplication.processEvents()

		# Updating labels
		self.lbl_systems_conversion.setPixmap(gif_check)
		self.lbl_stations_conversion.setMovie(gif_busy)

		# Running stations conversion in thread
		self.async_result = self.pool.apply_async(self._convert_stations)
		while not hasattr(self.async_result, '_success'):
			QtGui.QApplication.processEvents()

		# Updating labels
		self.lbl_stations_conversion.setPixmap(gif_check)
		QtGui.QApplication.processEvents()

		time.sleep(1)  # Giving the user time to see confirmation
		self.win_download.close()

	def _download_systems(self):
		"""
		Download and save the systems datafile from www.eddb.io/api (gzipped)
		"""
		with open (self.systems_json_path + '/systems_raw.json', 'wb') as self.systems_raw_file:
			self.response = requests.get(self.settings.systemsDataURL, stream=True, headers={'Accept-Encoding': 'gzip, deflate, sdch'})
			self.systems_raw_file.write(self.response.content)

	def _download_stations(self):
		"""
		Download and save the stations datafile from www.eddb.io/api (gzipped)
		"""
		with open (self.stations_json_path + '/stations_raw.json', 'wb') as self.stations_raw_file:
			self.response = requests.get(self.settings.stationsDataURL, stream=True, headers={'Accept-Encoding': 'gzip, deflate, sdch'})
			self.stations_raw_file.write(self.response.content)

	def _convert_stations(self):
		"""
		Convert the stations JSON file into a smaller one with only name and id (strip excess data)
		"""
		stations_list = []
		for station in json.loads(open(self.stations_json_path + '/stations_raw.json').read()):
			stations_list.append({'id': station['id'], 'name':station['name']})
		with open(self.settings.stationsDataFile, 'w') as json_file:
			json.dump(stations_list, json_file, ensure_ascii=False)

	def _convert_systems(self):
		"""
		Convert the systems JSON file into a smaller one with only name and id (strip excess data)
		"""
		systems_list = []
		for system in json.loads(open(self.systems_json_path + '/systems_raw.json').read()):
			systems_list.append({'id': system['id'], 'name':system['name']})
		with open(self.settings.systemsDataFile, 'w') as json_file:
			json.dump(systems_list, json_file, ensure_ascii=False)

	def about(self):
		self.about_window = QtGui.QWidget()
		self.about = Ui_About()
		self.about.setupUi(self.about_window)
		self.about_window.setStyleSheet(self.style)
		self.about.btn_close.pressed.connect(self.about_window.close)
		self.about_window.setWindowFlags(QtCore.Qt.FramelessWindowHint)
		self.about_window.show()