B  @`)ã@s|dZddlmZmZmZdZddlZddlZyddlm Z Wne k rXdd„Z YnXddl m m mZddlmZe e¡ZddlTdd lmZdd lmZdd lmZdd lmZej  e!¡Z"ye# $ej %e"d ¡¡\Z&Z'Wn6e (d¡Gdd„de)ƒZ&Gdd„de)ƒZ'YnXe*sRxe+D]Z,e -e,¡q,We -d¡e dƒ‚ddd„Z.ddd„Z/Gdd„de'e&ƒZ0dS)z. A simple GUI viewer/editor for Pyomo models. é)ÚdivisionÚprint_functionÚabsolute_importz John EslickN)Ú get_ipythoncCs tdƒ‚dS)NzIPython not available)ÚAttributeError©rrú;/tmp/pip-unpacked-wheel-bi3529v6/pyomo/contrib/viewer/ui.pyrsr)Ú*)Ú ModelBrowser)Ú ResidualTable)Ú ModelSelect)ÚUIDatazmain.uizFailed to load UI files.c@s eZdZdS)Ú _MainWindowUIN)Ú__name__Ú __module__Ú __qualname__rrrrr,src@s eZdZdS)Ú _MainWindowN)rrrrrrrr.srz.Qt is not available. Cannot create UI classes.zCould not import PyQt4 or PyQt5TFcCs`|dkrtjdd}t||d}ytƒj d|j¡Wntk rJYnX|rX| ¡||fS)a0 Create a UI MainWindow. Args: model: A Pyomo model to work with show: show the window after it is created Returns: (ui, model): ui is the MainWindow widget, and model is the linked Pyomo model. If no model is provided a new ConcreteModel is created NZDefault)Úname)ÚmodelÚtestingZ post_execute) ÚpyoZ ConcreteModelÚ MainWindowrÚeventsÚregisterÚrefresh_on_executerÚshow)rrrZuirrrÚget_mainwindow7s   rcCst|||dS)N)rrr)r)rrrrrrÚget_mainwindow_nbLsrcseZdZ‡fdd„Zdd„Zefdd„Zdd„Zd d „Zd d „Z d d„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zdd„Z‡ZS)rcs¤| dd¡}|_| dd¡}|_| dd¡|_| dd¡}t|d|_tt|ƒj||Ž| |¡|  |j ¡g|_ d|_ d|_ d|_d|_d|_| ¡|jj |j¡|jj |j¡|jj |j¡|jj |j¡|jj |j¡|jj |j¡|jj |j ¡|j!j |j"¡|j#j |j$¡|j%j |jj&¡|j'j |jj(¡|j)j |j j*¡|j+j |j j,¡|j-j |j.¡d|_/d|_0|j  1t2j3¡dS)NrÚmainrFÚflagsr)r)4Úpoprrr Úui_dataÚsuperrÚ__init__ZsetupUiZsetCentralWidgetÚmdiAreaÚ _refresh_listÚ variablesÚ constraintsÚ expressionsÚ parametersÚ residualsÚ update_modelÚupdatedÚconnectZactionModel_SelectorZ triggeredÚshow_model_selectZ exec_refreshrZactionRestart_Variable_ViewÚvariables_restartZactionRestart_Constraint_ViewÚconstraints_restartZactionRestart_Parameter_ViewÚparameters_restartZactionRestart_Expression_ViewÚexpressions_restartZactionRestart_Residual_TableÚresiduals_restartZactionInformationÚmodel_informationZactionCalculateConstraintsZcalculate_constraintsZactionCalculateExpressionsZcalculate_expressionsZ actionTileZtileSubWindowsZ actionCascadeZcascadeSubWindowsZ actionTabsÚ toggle_tabsÚ_dialogÚ_dialog_test_buttonÚ setViewModeÚQMdiAreaÚ TabbedView)ÚselfÚargsÚkwargsrrr)Ú __class__rrr#QsN      zMainWindow.__init__cCsD|j ¡tjkr |j tj¡n |j ¡tjkr@|j tj¡ndS)N)r$ZviewModer9Z SubWindowViewr8r:)r;rrrr5s zMainWindow.toggle_tabscKs¢y|j |¡Wntk r$YnXy4y|j | ¡¡Wntk rPYnX~d}Wntk rnYnX|f|Ž}|j |¡| ¡  ¡|j  |¡|S)z4 Start/Restart the variables window N) r%ÚremoveÚ ValueErrorr$ZremoveSubWindowÚparentÚ RuntimeErrorrZ addSubWindowrÚappend)r;ÚwÚclsr=rrrÚ _tree_restart‰s$    zMainWindow._tree_restartcCs|j|jd|jd|_dS)NZVar)rDÚstandardr!)rFr&r!)r;rrrr/ szMainWindow.variables_restartcCs|j|jd|jd|_dS)NZ Expression)rDrGr!)rFr(r!)r;rrrr2¤szMainWindow.expressions_restartcCs|j|jd|jd|_dS)NZParam)rDrGr!)rFr)r!)r;rrrr1¨szMainWindow.parameters_restartcCs|j|jd|jd|_dS)NZ Constraint)rDrGr!)rFr'r!)r;rrrr0¬szMainWindow.constraints_restartcCs|j|jt|jd|_dS)N)rDrEr!)rFr*r r!)r;rrrr3°szMainWindow.residuals_restartcCs ||j_dS)N)r!r)r;rrrrÚ set_model´szMainWindow.set_modelcCsF| ¡| ¡| ¡| ¡|j |j ¡¡| ¡| ¡dS)z] Play it safe by restarting all the tree view widgets when the model updates N) r/r2r0r1r$ZsetActiveSubWindowr&rAr5)r;rrrr+·szMainWindow.update_modelc Csžt |jj¡}t |jj¡}t |jj¡}||}|dkr@d}nd}tƒ}| d¡||_|  d¡|  d  |||||¡¡|  tj ¡| d¡| ¡dS) aô Put some useful model information into a message box Displays: * number of active equality constraints * number of free variables in equality constraints * degrees of freedom Other things that could be added * number of deactivated equalities * number of active inequality constraints * number of deactivated inequality constratins * number of free variables not appearing in active constraints * number of fixed variables not appearing in active constraints * number of free variables not appearing in any constraints * number of fixed variables not appearing in any constraints * number of fixed variables appearing in constraints éZDegreeZDegreeszQLabel{min-width: 600px;}zModel InformationzY{} -- Active Constraints {} -- Active Equalities {} -- Free Variables {} -- {} of FreedomFN)ÚrptZcount_equality_constraintsr!rZcount_free_variablesZcount_constraintsÚ QMessageBoxZ setStyleSheetr6ÚsetWindowTitleÚsetTextÚformatÚsetStandardButtonsZOkZsetModalr)r;Z active_eqZ free_varsZconsZdofZdoftextÚmsgrrrr4Ãs     zMainWindow.model_informationc Cs6x0|jD]&}y | ¡Wqtk r,YqXqWdS)zî This is the call back function that happens when code is executed in the ipython kernel. The main purpose of this right now it to refresh the UI display so that it matches the current state of the model. N)r%ZrefreshrB)r;rDrrrrìs   zMainWindow.refresh_on_executecCs"t||jd}| ¡| ¡dS)N)rAr!)r r!Z update_modelsr)r;Z model_selectrrrr.øszMainWindow.show_model_selectcCs | ¡dS)zY Selecting exit from the UI, triggers the close event on this mainwindow N)Úclose)r;rrrÚ exit_actionýszMainWindow.exit_actioncCsttƒ}||_| tj¡| d¡| d¡| tjtjB¡|j rL|j }n|  ¡}|tjkrh|  ¡n|  ¡dS)zC Handle the close event by asking for confirmation zMAre you sure you want to close this window? You can reopen it with ui.show().zClose?N)rKr6ZsetIconZQuestionrMrLrOZYesZNorr7Úexec_ÚacceptÚignore)r;ÚeventrPÚresultrrrÚ closeEvents     zMainWindow.closeEvent)rrrr#r5r rFr/r2r1r0r3rHr+r4rr.rRrXÚ __classcell__rr)r>rrPs .  ) r)NTF)NTF)1Ú__doc__Ú __future__rrrÚ __author__ÚosÚloggingZIPythonrÚ ImportErrorZpyomo.contrib.viewer.reportÚcontribZviewerÚreportrJZ pyomo.environÚenvironrÚ getLoggerrÚ_logZpyomo.contrib.viewer.qtZ"pyomo.contrib.viewer.model_browserr Z#pyomo.contrib.viewer.residual_tabler Z!pyomo.contrib.viewer.model_selectr Zpyomo.contrib.viewer.ui_datar ÚpathÚdirnameÚ__file__Z_mypathZuicZ loadUiTypeÚjoinrrÚ exceptionÚobjectZ qt_availableZqt_import_errorsZ_errÚerrorrrrrrrrÚ s>