Created
June 7, 2017 20:30
-
-
Save mgrady3/5e84b7d748941b9e8da6decddf33ef2b to your computer and use it in GitHub Desktop.
QuantumWise job app example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""QuantumWise GUI Software Developer Application. | |
Maxwell Grady | |
June 2017 | |
Usage: | |
python example.py | |
""" | |
import sys | |
from PyQt4 import QtCore, QtGui | |
class ControlWindow(QtGui.QWidget): | |
"""Window for circle drawing controls. | |
Subclass QWidget to allow override for closeEvent. | |
This prevent the control window from being closed. | |
Main closing the main window will quit the application. | |
""" | |
def __init__(self, parent=None): | |
super(QtGui.QWidget, self).__init__(parent) | |
def closeEvent(self, event): | |
event.ignore() | |
class Example(QtGui.QGraphicsView): | |
"""Subclass QGraphicsView to provide a 'paint' interface .""" | |
def __init__(self, parent=None): | |
"""Setup.""" | |
super(QtGui.QGraphicsView, self).__init__(parent) | |
self.scene = QtGui.QGraphicsScene(self) | |
self.setScene(self.scene) | |
self.setSceneRect(0, 0, 640, 480) | |
self.setFixedSize(640, 480) # adjust for alternate size displays | |
self.circles = [] | |
self.initControls() | |
def initControls(self): | |
"""Make Controls Window.""" | |
self.cwin = ControlWindow() | |
layout = QtGui.QVBoxLayout() | |
self.color_label = QtGui.QLabel("Select Color") | |
self.color_menu = QtGui.QComboBox(self) | |
self.color_menu.addItem("Red") | |
self.color_menu.addItem("Blue") | |
self.color_menu.addItem("Green") | |
color_hbox = QtGui.QHBoxLayout() | |
color_hbox.addWidget(self.color_label) | |
color_hbox.addWidget(self.color_menu) | |
layout.addLayout(color_hbox) | |
self.size_label = QtGui.QLabel("Select Size") | |
self.size_menu = QtGui.QComboBox(self) | |
self.size_menu.addItem("10") | |
self.size_menu.addItem("20") | |
self.size_menu.addItem("50") | |
size_hbox = QtGui.QHBoxLayout() | |
size_hbox.addWidget(self.size_label) | |
size_hbox.addWidget(self.size_menu) | |
layout.addLayout(size_hbox) | |
self.make_circle_button = QtGui.QPushButton("Make Circle", self) | |
self.make_circle_button.clicked.connect(self.makeCircle) | |
layout.addWidget(self.make_circle_button) | |
self.cwin.setLayout(layout) | |
self.cwin.show() | |
def makeCircle(self): | |
"""Draw Circle in center of GraphicsView.""" | |
current_color = self.color_menu.currentText() | |
if str(current_color) == "Red": | |
current_color = QtGui.QColor(QtCore.Qt.red) | |
elif str(current_color) == "Blue": | |
current_color = QtGui.QColor(QtCore.Qt.blue) | |
elif str(current_color) == "Green": | |
current_color = QtGui.QColor(QtCore.Qt.green) | |
else: | |
# no selection | |
print("Please select a color from the menu.") | |
return | |
w = self.size().width() | |
h = self.size().height() | |
center = (w//2, h//2) # (x, y) | |
# print("Center: x={0}, y={1}".format(center[0], center[1])) | |
circle_size = self.size_menu.currentText() | |
if str(circle_size) == "10": | |
circle_size = 10 | |
elif str(circle_size) == "20": | |
circle_size = 20 | |
elif str(circle_size) == "50": | |
circle_size = 50 | |
else: | |
# no selection | |
print("Please select size from menu.") | |
return | |
pen = QtGui.QPen(current_color) | |
brush = QtGui.QBrush(pen.color(), | |
QtCore.Qt.SolidPattern) | |
circle = self.scene.addEllipse(center[0], | |
center[1], | |
circle_size, | |
circle_size, | |
pen=pen, | |
brush=brush) | |
circle.setFlag(QtGui.QGraphicsItem.ItemIsMovable) | |
self.circles.append(circle) | |
def closeEvent(self, event): | |
"""Close app on main window close.""" | |
QtCore.QCoreApplication.instance().quit() | |
def main(): | |
"""Run app.""" | |
app = QtGui.QApplication(sys.argv) | |
ex = Example() | |
ex.show() | |
sys.exit(app.exec_()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment