Esempio n. 1
0
# Wait to capture a mouse click
def mouseClick():
	curses.curs_set(0)
	ch = scr.getch()
	return ch == curses.KEY_MOUSE

# Create main menu
mainMenuNames = [" Connect to Database ", " Help ", " Quit "]
mainMenuY = 2
mainMenu = Menu(mainMenuY, curses.COLS/4, len(mainMenuNames), curses.COLS, mainMenuNames, False, SELECTED_COLOR)

# Create window to use to clear everything but the tab bar area
helpWin = curses.newwin(curses.LINES - mainMenuY + 3, curses.COLS, mainMenuY + 3, 0)

# Create menu for previously connected databases (then hide it)
prevDatabaseNames = getNicknames()
prevDatabasesMaxX = 20
mainMenuY += 4
prevDatabasesMenu = Menu(mainMenuY, 0, curses.LINES - 9, prevDatabasesMaxX, prevDatabaseNames, True, SELECTED_COLOR)
prevDatabasesMenu.hide()

# Create input boxes
inputPrompts = ["\nusername:"******"\npassword:"******"\n db name:", "\nhostname:", "\nnickname:"]
databaseInputBoxes = []
databaseInputBoxesX = prevDatabasesMaxX + 2
databaseInputBoxesY = []
for i in range(0, 5): 
	databaseInputBoxesY.append(mainMenuY + i*3)
	databaseInputBoxes.append(InputBox(inputPrompts[i], databaseInputBoxesY[i], databaseInputBoxesX, 1))
	databaseInputBoxes[i].hide()
def mainMenuClick(x, y, mainMenu, mainMenuNames, databaseInputBoxes, prevDatabasesMenu, prevConnect, prevDelete, newsqlConnect, newpsqlConnect, helpWin):
	itemName = mainMenuNav(mainMenu, x, y) 
	(temp, databaseInputBoxesX) = databaseInputBoxes[0].getboxyx()
	databaseInputBoxesY = []
	for i in range(0, 5):
		(inputY, temp) = databaseInputBoxes[i].getboxyx()
		databaseInputBoxesY.append(inputY)
	if itemName == mainMenuNames[0]:
		helpWin.clear()
		helpWin.refresh()
		prevDatabasesMenu.unhide();
		for i in range(0, 5): 
			databaseInputBoxes[i].unhide()
		prevConnect.unhide()
		prevDelete.unhide()
		newsqlConnect.unhide()
		newpsqlConnect.unhide()
	elif itemName == mainMenuNames[1]: # help
		helpWin.clear()
		helpWin.refresh()
		hide(databaseInputBoxes)
		hide([prevDatabasesMenu, prevConnect, newsqlConnect, newpsqlConnect])
		helpWin.addstr(0, 0, getHelp("menu"))
		helpWin.refresh()
	elif itemName == mainMenuNames[2]:
		return "quit"
	elif prevConnect.itemAt(y, x):
		if prevDatabasesMenu.getSelected():
			db = useDB(prevDatabasesMenu.getSelected())
			if db is not None:
				hide(databaseInputBoxes)
				hide([prevDatabasesMenu, prevConnect, newsqlConnect, newpsqlConnect])
				helpWin.clear()
				helpWin.refresh()
				return db
			return "failedOldConnect"
	elif prevDelete.itemAt(y, x):
		if prevDatabasesMenu.getSelected() is not None:
			deleteDBInfo(prevDatabasesMenu.getSelected())
			prevDatabasesMenu.deSelectAll()
			prevDatabasesMenu.setItems(getNicknames())
			prevDatabasesMenu.hide()
			prevDatabasesMenu.unhide()
	elif newsqlConnect.itemAt(y, x):
		if connectVia("sql", databaseInputBoxes):
			db = useDB(databaseInputBoxes[4].gather())
			if db is not None:
				helpWin.clear()
				helpWin.refresh()
				return db			
			else:
				deleteDBInfo(databaseInputBoxes[4].gather())
		return "failedConnect"	
	elif newpsqlConnect.itemAt(y, x):
		if connectVia("Psql", databaseInputBoxes):
			db = useDB(databaseInputBoxes[4].gather())
			if db is not None:
				helpWin.clear()
				helpWin.refresh()
				return db			
			else:
				deleteDBInfo(databaseInputBoxes[4].gather())
		return "failedConnect"	
	elif prevDatabasesMenu.itemAt(y, x):
		prevDatabasesMenu.selectOnlyItem(prevDatabasesMenu.itemAt(y, x))
	elif x > databaseInputBoxesX: 
		for i in range(4, -1, -1):
			if y > databaseInputBoxesY[i]:
				databaseInputBoxes[i].edit()
				break
	return None