Beispiel #1
0
 def __init__(self, parent=None):
     super(ToolDefWidget, self).__init__(parent)
     # the active tool def
     self.toolDef = None
     # library load/save layout
     libSaveLayout = QHBoxLayout()
     self.openLibButton = QPushButton("Open Lib", self)
     self.connect(self.openLibButton, SIGNAL("clicked()"), self.openToolLib)
     libSaveLayout.addWidget(self.openLibButton)
     self.saveLibButton = QPushButton("Save Lib", self)
     self.connect(self.saveLibButton, SIGNAL("clicked()"), self.saveToolLib)
     libSaveLayout.addWidget(self.saveLibButton)
     libSaveLayout.insertStretch(2, 1)
     # tool load/save metric layout
     toolSaveLayout = QHBoxLayout()
     self.loadToolButton = QPushButton("Load Tool", self)
     self.connect(self.loadToolButton, SIGNAL("clicked()"),
                  self.showToolBrowserView)
     toolSaveLayout.addWidget(self.loadToolButton)
     # Button will be disable if current tool has not been modified
     self.saveToolButton = QPushButton("Save Tool", self)
     self.connect(self.saveToolButton, SIGNAL("clicked()"),
                  self.saveCurrentTool)
     toolSaveLayout.addWidget(self.saveToolButton)
     toolSaveLayout.insertStretch(2, 1)
     self.metricCheckBox = QCheckBox("Metric", self)
     self.metricCheckBox.setChecked(False)
     self.connect(self.metricCheckBox, SIGNAL("toggled(bool)"),
                  self.onMetricToggle)
     toolSaveLayout.addWidget(self.metricCheckBox)
     # vlayout
     self.vLayout = QVBoxLayout(self)
     self.vLayout.setContentsMargins(3, 3, 3, 3)
     self.vLayout.addLayout(toolSaveLayout)
     self.vLayout.addLayout(libSaveLayout)
     self.tdefScene = ToolDefScene()
     self.tdefView = ToolDefView(self.tdefScene, self)
     self.connect(
         self.tdefView.dimBox,
         SIGNAL("returnPressed()"),
         lambda box=self.tdefView.dimBox: self.onEditBoxReturn(box))
     self.connect(
         self.tdefView.commentBox,
         SIGNAL("returnPressed()"),
         lambda box=self.tdefView.commentBox: self.onEditBoxReturn(box))
     self.vLayout.addWidget(self.tdefView)
     # tool browser
     self.toolBrowser = ToolBrowserView()
     self.connect(self.toolBrowser,
                  SIGNAL('itemClicked(QTreeWidgetItem*, int)'),
                  self.loadTool)
     self.connect(self.toolBrowser, SIGNAL('escKeyPressed()'),
                  self.showToolDefView)
     self.showToolBrowserView()
Beispiel #2
0
class ToolDefWidget(QWidget):
    """Define and edit tools

    Load Button
      Show a QTreeView from which the user can select a tool
    Save Button
      Show the same QTreeView, but allow saving the current tool
    Metric CheckBox
      Toggle the current tool's units.
    Tool Definition View
      Display the profile of the current tool along with editable dimensions.
    Tool Browser View
      A QTreeView to display all available tools. When a tool is clicked, the
      tool def view is shown with that tool loaded. If Escape is pressed, the
      tool def view is shown with no change.
    """
    def __init__(self, parent=None):
        super(ToolDefWidget, self).__init__(parent)
        # the active tool def
        self.toolDef = None
        # library load/save layout
        libSaveLayout = QHBoxLayout()
        self.openLibButton = QPushButton("Open Lib", self)
        self.connect(self.openLibButton, SIGNAL("clicked()"), self.openToolLib)
        libSaveLayout.addWidget(self.openLibButton)
        self.saveLibButton = QPushButton("Save Lib", self)
        self.connect(self.saveLibButton, SIGNAL("clicked()"), self.saveToolLib)
        libSaveLayout.addWidget(self.saveLibButton)
        libSaveLayout.insertStretch(2, 1)
        # tool load/save metric layout
        toolSaveLayout = QHBoxLayout()
        self.loadToolButton = QPushButton("Load Tool", self)
        self.connect(self.loadToolButton, SIGNAL("clicked()"),
                     self.showToolBrowserView)
        toolSaveLayout.addWidget(self.loadToolButton)
        # Button will be disable if current tool has not been modified
        self.saveToolButton = QPushButton("Save Tool", self)
        self.connect(self.saveToolButton, SIGNAL("clicked()"),
                     self.saveCurrentTool)
        toolSaveLayout.addWidget(self.saveToolButton)
        toolSaveLayout.insertStretch(2, 1)
        self.metricCheckBox = QCheckBox("Metric", self)
        self.metricCheckBox.setChecked(False)
        self.connect(self.metricCheckBox, SIGNAL("toggled(bool)"),
                     self.onMetricToggle)
        toolSaveLayout.addWidget(self.metricCheckBox)
        # vlayout
        self.vLayout = QVBoxLayout(self)
        self.vLayout.setContentsMargins(3, 3, 3, 3)
        self.vLayout.addLayout(toolSaveLayout)
        self.vLayout.addLayout(libSaveLayout)
        self.tdefScene = ToolDefScene()
        self.tdefView = ToolDefView(self.tdefScene, self)
        self.connect(
            self.tdefView.dimBox,
            SIGNAL("returnPressed()"),
            lambda box=self.tdefView.dimBox: self.onEditBoxReturn(box))
        self.connect(
            self.tdefView.commentBox,
            SIGNAL("returnPressed()"),
            lambda box=self.tdefView.commentBox: self.onEditBoxReturn(box))
        self.vLayout.addWidget(self.tdefView)
        # tool browser
        self.toolBrowser = ToolBrowserView()
        self.connect(self.toolBrowser,
                     SIGNAL('itemClicked(QTreeWidgetItem*, int)'),
                     self.loadTool)
        self.connect(self.toolBrowser, SIGNAL('escKeyPressed()'),
                     self.showToolDefView)
        self.showToolBrowserView()

    def loadTool(self, item):
        """Load a tool into ToolDefView.

        item -- a QTreeWidgetItem
        
        Called when the user clicks a tool in the ToolBrowserView.
        """
        if not item.parent():
            return  # category clicked
        category, specs = self.toolBrowser.getToolData(item)
        if self.toolDef:
            self.tdefScene.removeItem(self.toolDef)
        self.toolDef = CAT2TDEF[category](specs)
        self.tdefScene.addItem(self.toolDef)
        self.toolDef.config(specs)
        self.showToolDefView()
        self.emit(SIGNAL('toolLoaded()'))

    def showToolBrowserView(self):
        """Replace the ToolDefView with the ToolBrowserView
        
        loading -- True if loading a tool, False if saving a tool
        """
        if self.toolBrowser.isVisible():
            return
        if self.toolDef and self.toolDef.isDirty():
            result = QMessageBox.question(
                self, "machtool", "The tool was modified. Save it?",
                QMessageBox.Yes | QMessageBox.No
                | QMessageBox.Cancel)
            if result == QMessageBox.Yes:
                self.saveCurrentTool()
            elif result == QMessageBox.Cancel:
                return
        self.openLibButton.show()
        self.saveLibButton.show()
        self.saveLibButton.setEnabled(self.toolBrowser.isDirty())
        self.metricCheckBox.hide()
        self.loadToolButton.hide()
        self.saveToolButton.hide()
        self.tdefView.hide()
        self.tdefView.dimBox.hide()
        self.tdefView.commentBox.hide()
        self.vLayout.removeWidget(self.tdefView)
        self.vLayout.addWidget(self.toolBrowser)
        self.toolBrowser.show()
        self.toolBrowser.setFocus()

    def showToolDefView(self):
        """Replace the ToolBrowserView with the ToolDefView
        """
        if self.tdefView.isVisible():
            return
        self.loadToolButton.setEnabled(True)
        self.saveToolButton.setEnabled(False)
        self.metricCheckBox.setEnabled(True)
        self.openLibButton.hide()
        self.saveLibButton.hide()
        self.metricCheckBox.show()
        self.loadToolButton.show()
        self.saveToolButton.show()
        self.toolBrowser.hide()
        self.vLayout.removeWidget(self.toolBrowser)
        # this will trigger a resize event which will trigger a fitAll
        self.vLayout.addWidget(self.tdefView)
        self.tdefView.show()
        self.tdefView.setFocus()
        self.metricCheckBox.setChecked(self.toolDef.isMetric())

    def onMetricToggle(self, state):
        self.toolDef.config({'metric': state})
        self.tdefView.fitAll()
        self.saveToolButton.setEnabled(self.toolDef.isDirty())

    def onEditBoxReturn(self, box):
        """Update the tool's dimension or comment and fit it.

        box -- the EditBox the user just updated
        """
        box.hide()
        self.toolDef.config({str(box.item.toolTip()): box.textValue()})
        self.tdefView.setFocus()
        self.tdefView.fitAll()
        self.saveToolButton.setEnabled(self.toolDef.isDirty())
        self.emit(SIGNAL('toolModified()'))

    def saveCurrentTool(self):
        result = self.toolBrowser.addTool(self.toolDef)
        if result:
            self.toolDef.setDirty(False)
            self.saveToolButton.setEnabled(False)

    def openToolLib(self):
        if self.toolBrowser.isDirty():
            result = QMessageBox.question(
                self, "machtool", 'The current tool library has been'
                ' modified. Save it?', QMessageBox.Yes | QMessageBox.No
                | QMessageBox.Cancel)
            if result == QMessageBox.Yes:
                self.saveToolLib()
                self.saveToolButton.setEnabled(False)
            elif result == QMessageBox.Cancel:
                return
        self.toolBrowser.openToolLib()

    def saveToolLib(self):
        self.toolBrowser.writeToolLib()
        self.saveToolButton.setEnabled(False)

    def minimumSizeHint(self):
        return QSize(300, 300)