Source code for diffpy.pdfgui.gui.sgconstraindialog

#!/usr/bin/env python
# -*- coding: ISO-8859-1 -*-
##############################################################################
#
# PDFgui            by DANSE Diffraction group
#                   Simon J. L. Billinge
#                   (c) 2006 trustees of the Michigan State University.
#                   All rights reserved.
#
# File coded by:    Chris Farrow
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE.txt for license information.
#
##############################################################################

# generated by wxGlade 0.9.3 on Fri Jul 19 16:06:06 2019

import math

import wx

from diffpy.pdfgui.control.controlerrors import ControlValueError
from diffpy.pdfgui.gui.pdfpanel import PDFPanel


[docs] class SGConstrainDialog(wx.Dialog, PDFPanel): def __init__(self, *args, **kwds): PDFPanel.__init__(self) # begin wxGlade: SGConstrainDialog.__init__ kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_DIALOG_STYLE wx.Dialog.__init__(self, *args, **kwds) self.SetTitle("Space Group Constraints") sizer_2 = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, "Space Group Constraints"), wx.VERTICAL) self.numConstrainedLabel = wx.StaticText(self, wx.ID_ANY, "") sizer_2.Add(self.numConstrainedLabel, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 5) sizer_3 = wx.BoxSizer(wx.HORIZONTAL) sizer_2.Add(sizer_3, 0, wx.EXPAND, 0) self.sgLabel = wx.StaticText(self, wx.ID_ANY, "Space Group") sizer_3.Add(self.sgLabel, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5) self.sgComboBox = wx.ComboBox(self, wx.ID_ANY, choices=["P1"], style=0) self.sgComboBox.SetSelection(0) sizer_3.Add(self.sgComboBox, 0, wx.ALL, 5) sizer_4 = wx.BoxSizer(wx.HORIZONTAL) sizer_2.Add(sizer_4, 0, wx.EXPAND, 0) self.offsetLabel = wx.StaticText(self, wx.ID_ANY, "Origin Offset") sizer_4.Add(self.offsetLabel, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5) self.offsetTextCtrlX = wx.TextCtrl(self, wx.ID_ANY, "0") sizer_4.Add(self.offsetTextCtrlX, 0, wx.ALL, 5) self.offsetTextCtrlY = wx.TextCtrl(self, wx.ID_ANY, "0") sizer_4.Add(self.offsetTextCtrlY, 0, wx.ALL, 5) self.offsetTextCtrlZ = wx.TextCtrl(self, wx.ID_ANY, "0") sizer_4.Add(self.offsetTextCtrlZ, 0, wx.ALL, 5) self.positionCheckBox = wx.CheckBox(self, wx.ID_ANY, "constrain positions") self.positionCheckBox.SetValue(1) sizer_2.Add(self.positionCheckBox, 0, wx.ALL, 5) self.tfCheckBox = wx.CheckBox(self, wx.ID_ANY, "constrain temperature factors") self.tfCheckBox.SetValue(1) sizer_2.Add(self.tfCheckBox, 0, wx.ALL, 5) self.static_line_1 = wx.StaticLine(self, wx.ID_ANY) sizer_2.Add(self.static_line_1, 0, wx.EXPAND, 0) sizer_4_copy = wx.BoxSizer(wx.HORIZONTAL) sizer_2.Add(sizer_4_copy, 0, wx.EXPAND, 0) sizer_4_copy.Add((0, 0), 1, wx.EXPAND, 0) self.cancelButton = wx.Button(self, wx.ID_CANCEL, "Cancel") sizer_4_copy.Add(self.cancelButton, 0, wx.ALL, 5) self.okButton = wx.Button(self, wx.ID_OK, "OK") sizer_4_copy.Add(self.okButton, 0, wx.ALL, 5) self.SetSizer(sizer_2) sizer_2.Fit(self) self.Layout() self.Bind(wx.EVT_COMBOBOX, self.onSGSelect, self.sgComboBox) self.Bind(wx.EVT_TEXT_ENTER, self.onSGTextEnter, self.sgComboBox) self.Bind(wx.EVT_TEXT_ENTER, self.onOXTextEnter, self.offsetTextCtrlX) self.Bind(wx.EVT_TEXT_ENTER, self.onOYTextEnter, self.offsetTextCtrlY) self.Bind(wx.EVT_TEXT_ENTER, self.onOZTextEnter, self.offsetTextCtrlZ) self.Bind(wx.EVT_CHECKBOX, self.onPosFlag, self.positionCheckBox) self.Bind(wx.EVT_CHECKBOX, self.onTempFlag, self.tfCheckBox) self.Bind(wx.EVT_BUTTON, self.onCancel, self.cancelButton) self.Bind(wx.EVT_BUTTON, self.onOk, self.okButton) # end wxGlade self.__customProperties() # ######################################################################### def __customProperties(self): """Set the custom properties.""" # setting of combo box items was deferred to updateSpaceGroupList() self.spacegroup = None self.offset = [0.0, 0.0, 0.0] self.posflag = True self.tempflag = True self.structure = None self.indices = [] self.textCtrls = [ self.offsetTextCtrlX, self.offsetTextCtrlY, self.offsetTextCtrlZ, ] # Set the focus events. for textctrl in self.textCtrls: textctrl.Bind(wx.EVT_KILL_FOCUS, self.onKillFocus) self.sgComboBox.Bind(wx.EVT_KILL_FOCUS, self.onKillFocus) return
[docs] def updateSpaceGroupList(self): """Update space group choices in combobox according to self.structure.getSpaceGroupList(). Requires that structure attribute is defined. """ self.sgComboBox.Clear() sglist = self.structure.getSpaceGroupList() self.spacegroup = self.structure.getSpaceGroup("P1") for sg in sglist: self.sgComboBox.Append(sg.short_name) return
[docs] def setStructure(self, structure): """Set the structure and update the widgets.""" self.structure = structure self.updateSpaceGroupList() sgname = self.structure.pdffit.get("spcgr") offset = self.structure.pdffit.get("sgoffset") if sgname: self.sgComboBox.SetValue(sgname) if offset: self.offsetTextCtrlX.SetValue(str(offset[0])) self.offsetTextCtrlY.SetValue(str(offset[1])) self.offsetTextCtrlZ.SetValue(str(offset[2])) self.updateWidgets() return
[docs] def getSpaceGroup(self): """Get the current space group.""" return self.spacegroup
[docs] def getOffset(self): """Get the offset.""" return self.offset
[docs] def getPosFlag(self): """Get whether the positions should be constrained.""" return self.posflag
[docs] def getTempFlag(self): """Get whether the positions should be constrained.""" return self.tempflag
[docs] def updateWidgets(self): """Update the widgets.""" # Update space group sgname = self.sgComboBox.GetValue() try: self.spacegroup = self.structure.getSpaceGroup(sgname) error = None except ValueError: error = "Space group %s does not exist." % sgname # This changes list box value to the short_name of the new spacegroup # or to the name of previous spacegroup when getSpaceGroup failed. self.sgComboBox.SetValue(self.spacegroup.short_name) # Update offset for i in range(3): textctrl = self.textCtrls[i] val = textctrl.GetValue() # make sure the value is meaningful try: val = float(eval("1.0*" + val, dict(math.__dict__))) except (NameError, TypeError, SyntaxError): val = 0 textctrl.SetValue("%s" % val) self.offset[i] = val # newatoms = len(stemp) - len(self.structure) s = "" if len(self.indices) != 1: s = "s" message = "%i atom%s selected." % (len(self.indices), s) self.numConstrainedLabel.SetLabel(message) # Raise an error if we had to change the space group if error: raise ControlValueError(error) return
# # Events
[docs] def onKillFocus(self, event): """Check value of widgets and update the dialog message.""" self.updateWidgets() event.Skip() return
[docs] def onSGTextEnter(self, event): # wxGlade: SGConstrainDialog.<event_handler> self.updateWidgets() return
[docs] def onOXTextEnter(self, event): # wxGlade: SGConstrainDialog.<event_handler> self.updateWidgets() return
[docs] def onOYTextEnter(self, event): # wxGlade: SGConstrainDialog.<event_handler> self.updateWidgets() return
[docs] def onOZTextEnter(self, event): # wxGlade: SGConstrainDialog.<event_handler> self.updateWidgets() return
[docs] def onSGSelect(self, event): # wxGlade: SGConstrainDialog.<event_handler> self.updateWidgets() return
[docs] def onOk(self, event): # wxGlade: SGConstrainDialog.<event_handler> # check to see if the space group is consistent if not self.structure.isSpaceGroupPossible(self.spacegroup): message = "The chosen space group is not consistent\n" message += "with the lattice parameters.\n" message += "Would you like to proceed anyways?" d = wx.MessageDialog(self, message, "Inconsistent space group", wx.YES_NO) code = d.ShowModal() if code == wx.ID_YES: self.EndModal(wx.ID_OK) else: self.EndModal(wx.ID_OK) return
[docs] def onCancel(self, event): # wxGlade: SGConstrainDialog.<event_handler> event.Skip() return
[docs] def onPosFlag(self, event): # wxGlade: SGConstrainDialog.<event_handler> self.posflag = self.positionCheckBox.GetValue() return
[docs] def onTempFlag(self, event): # wxGlade: SGConstrainDialog.<event_handler> self.tempflag = self.tfCheckBox.GetValue() return
# end of class SGConstrainDialog