Skip to content

Instantly share code, notes, and snippets.

@marodrig
Last active October 2, 2015 02:47
Show Gist options
  • Save marodrig/eac26e223aebef21bc14 to your computer and use it in GitHub Desktop.
Save marodrig/eac26e223aebef21bc14 to your computer and use it in GitHub Desktop.
PySide case manager.
# Python modules
import os
import shutil
# PySide modules
from PySide import QtCore, QtGui
# Other modules
# Genie modules
from case_widget import CaseWidget
from helper.helper_dialogBox import GMessageBox
class CaseManager(QMdiArea):
'''
Serves as the central widget for the entire application. It is simply a tab widget which
will handle top level activities for the case.
'''
noActiveCasesOpen = QtCore.Signal()
def __init__(self, mw=None, parent=None):
super(CaseManager, self).__init__(parent)
self.mw = mw
self.setActivationOrder(QMdiArea.StackingOrder)
self.caseCount = 0
@QtCore.Slot()
def get_active_case(self):
'''
Returns active case Widget.
'''
if self.currentSubWindow():
return self.currentSubWindow().widget()
else:
return self.currentSubWindow()
@QtCore.Slot()
def has_active_case(self):
'''
Check if any case is open.
'''
return self.caseCount
@QtCore.Slot()
def close_case(self, cw):
'''
Close the case, prompt with save if necessary. Finally close the window.
'''
if not self.mw.getIsBatchMode():
if cw.case.modifiedFlag == True:
result = GMessageBox.warning(self, self.tr("Unsaved Case Data"),
self.tr("You have made changes to '%s'.\n\nDiscard changes?" % cw.case.name),
QMessageBox.Yes)
if result != QMessageBox.Yes:
return -1# Do not close.
# Destroy all plot dialogs for case.
cw.removeAllPlotDlgs()
# Stop timers.
cw.run_timer.stop()
cw.method_add_timer.stop()
cw.undo_timer.stop()
# Clean up the working directory
wd = cw.case.workingdir
if os.path.isdir(wd):
try:
shutil.rmtree(wd)
except Exception, e:
# On rare occasions (usually when a process is killed in Windows),
# parts of the working directory cannot be removed.
print str(e)
# Unlock case
cw.case.unlockCase()
self.caseCount -= 1
# Check the number of tabs currently open. If there are no more open
# tabs then emit a signal
if len( self.subWindowList() ) <= 1:
self.noActiveCasesOpen.emit();
return 1
@QtCore.Slot()
def add_case(self, case, dataOnly=False):
'''
Add a case to the tabbed widgets. This will create a specific case widget
with all options available for the case.
'''
self.caseCount += 1
cw = CaseWidget(case, self, dataOnly=dataOnly)
window = self.addSubWindow(cw)
cw.show()
window.setWindowTitle(case.name)
return cw
def search_for_variable(self, variable, method):
'''
Search for the variable in the active case
'''
#case_widget = self.currentWidget()
if self.currentSubWindow():
case_widget = self.currentSubWindow().widget()
case_widget.findVariable(variable, method)
@QtCore.Slot()
def run_case_widget_method(self, function, *args):
'''
A generic method which will call the function for each case widget
'''
for window in self.subWindowList():
case_widget = window.widget()
# If the widget is not a CaseWidget, then move on
if not isinstance(case_widget, CaseWidget):
continue
# Call the function
_function = getattr(case_widget, function)
_function(*args)
@QtCore.Slot()
def set_active_window_by_index(self, idx):
'''
Set active subwindow by list index in subWindowList.
'''
windows = self.subWindowList()
if idx >= 0 and idx < len(windows):
self.setActiveSubWindow(windows[idx])
def toggle_case_problem_widget(self):
'''
Either show or hide the case's problem widget
'''
self.runCaseWidgetMethod('toggleProblemWidget')
def toggle_runlog_widget(self):
'''
Either show or hide the case's run log widget
'''
self.runCaseWidgetMethod('toggleRunLogWidget')
def show_unit_labels(self, show):
'''
If show is true then the unit labels will be shown for all case's input widgets. If False,
then no unit labels will be shown.
'''
self.runCaseWidgetMethod('showUnitLabels', show)
def show_unit_as(self, default):
'''
For each case widget, update all of the MethodWidget's InputWidget variables to reflect
units in the SI/USCS unit system.
*default*
Either 0 for USCS, or 1 for SI
'''
self.runCaseWidgetMethod('showUnitsAs', default)
def sort_input_vars(self):
'''
For each case widget's method widgets, sort the input variables based on the
saved settings
'''
self.runCaseWidgetMethod('sortInputVars')
def update_vars_from_settings(self):
'''
For each case widget's method widgets, update variable widget attributes.
'''
self.runCaseWidgetMethod('updateVarsFromSettings')
@QtCore.Slot()
def tile_windows(self):
'''
Homebrew window tiling function.
Windows inside the MDI area are arranged in a
2 column, X row grid.
'''
winList = self.subWindowList()
numWindows = len(winList)
widthOffSet = self.size().width()/2
heightOffset = (2*self.size().height())/numWindows
# print "Width offset: %d" % widthOffSet
# print "Height offset: %d" % heightOffset
x = 0
y = 0
count = 0
for window in winList:
window.setGeometry(x, y, widthOffSet, heightOffset)
count += 1
if (widthOffSet + x) < self.size().width():
x += widthOffSet + 1
else:
x = 0
# offset Y coordinate for once we get to a even number window.
if (count % 2) != 1:
y += heightOffset + 1
return
#
# Test widget
#
if __name__=='__main__':
import sys
from genie import Case
# Create a case
c = Case()
c.name = "Test Case"
# Create the dialog
app = QApplication(sys.argv)
dlg = QDialog()
lout = QVBoxLayout()
cm = CaseManager()
lout.addWidget(cm)
cm.add_case(c)
dlg.setLayout(lout)
dlg.show()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment