CSC400 3DVis start.py

From CSclasswiki
Jump to: navigation, search
# start.py
# Kelly Dwan
# Version 1.0; 5.6

import sys, os, re
from PyQt4 import QtCore, QtGui
from ScanPrompt import Ui_MainWindow

# # #
# StartQT4()
# Start the GUI
class StartQT4(QtGui.QMainWindow):
	def __init__(self, parent=None):
		QtGui.QWidget.__init__(self, parent)
		self.saved = []				#A list of saved points
		self.ui = Ui_MainWindow() 		#the instance of the GUI
		self.dir = sys.argv[1]			#scan image directory
		self.scan = []				#list of scans in directory
		self.scan.append("sagittal")		#initializing space in the list for the main sagittal scan
		self.scan.append("coronal")		#initializing space in the list for the main coronal scan
		self.scan.append("horizontal")		#initializing space in the list for the main horizontal scan
		self.i = 0				#index in scan list
		if len(self.dir)==0:			#if dir = '', we want current directory.
			self.dir = "."
		for root, dirs, files in os.walk(self.dir):
			for file in files:
				#regex that matches scan filenames - so they need to be properly named!
				match = r'[scan_]?(horizontal|sagittal|coronal)_([A-Z0-9]*)[_]?(C)?.(jpg|png|gif)'
				ma = re.search(match, file, re.IGNORECASE)
				if ma != None:
					if (ma.group(3) == "C"):				#the 'C' indicated 'center' scan- see filenaming in wiki
						if (ma.group(1) == "sagittal"):
							self.scan[0] = file
						elif (ma.group(1) == "coronal"):
							self.scan[1] = file
						elif (ma.group(1) == "horizontal"):
							self.scan[2] = file
					else:
						self.scan.append(ma.group())
		self.showWin(self.dir,self.scan[self.i])


	# # #
	# showWin(dir,scan)
	# Call to set up the UI, connect the buttons with their actions.
	# -dir: directory of scans
	# -scan: scan file name
	def showWin(self, dir, scan):
		self.ui.setupUi(self, dir, scan)
		QtCore.QObject.connect(self.ui.saveButton,QtCore.SIGNAL("clicked()"), self.exit)
		QtCore.QObject.connect(self.ui.nextButton,QtCore.SIGNAL("clicked()"), self.next_image)

	# # #
	# file_save()
	# Save the points to <directory>/scandata.txt
	# Note: If the file exists already, this appends the saved array to the 
	#  file, rather than wiping and rewriting it.
	# This function is called from the exit() function.
	def file_save(self):
		self.filename = str(self.dir) + "scandata.txt"
		file = open(self.filename, 'a')
		for locations in self.saved:
			if (len(locations) != 0 and len(locations[0]) == 1):
				file.write(str(locations)+"\n")
			else:
				for line in locations:
					file.write(str(line)+"\n")
			
		file.close()
		print("Saved to "+str(self.filename))
		sys.exit()

	# # #
	# exit()
	# This saves the current points to the saved array, then exits.
	def exit(self):
		self.savePrev()
		self.file_save()

	# # #
	# savePrev()
	# This appends the current scan's mouseLocation points to the saved array
	def savePrev(self):
		self.saved.append(self.scan[self.i])
		self.saved.append(self.ui.widget1.mouseLocations)

	# # #
	# next_image()
	# This saves the mouseLocations to saved, then sends the next image to showWin()
	# This is called when 'Next' is clicked.
	# If on the last image and clicked, the program will save and exit.
	def next_image(self):
		if (self.i < len(self.scan)-1):
			self.savePrev()
			self.i += 1
			self.nextscan = self.scan[self.i]
		else:
			print "You've run out of scans. Saving and exiting."
			self.savePrev()
			self.file_save()
		print("Now showing: "+self.nextscan)
		self.showWin(self.dir, self.nextscan)

if __name__ == "__main__":
	if len(sys.argv) != 2:
		print("Insufficient arguments. Proper format is start.py <directory or scans>\ \n")
		sys.exit()
	app = QtGui.QApplication(sys.argv)
	myapp = StartQT4()
	myapp.show()
	app.exec_()