示例#1
0
    def create_menus(self):

        ## ACTIONS ##
        ##
        newAction = QtWidgets.QAction("&New", self)
        newAction.setShortcuts(QtGui.QKeySequence.New)
        newAction.setStatusTip("Create a new file")
        newAction.setIcon(QtGui.QIcon.fromTheme("document-new"))
        newAction.triggered.connect(self.new_file)

        openAction = QtWidgets.QAction("&Open", self)
        openAction.setShortcuts(QtGui.QKeySequence.Open)
        openAction.setStatusTip("Open a file")
        openAction.setIcon(QtGui.QIcon.fromTheme("document-open"))
        openAction.triggered.connect(self.open)

        saveAction = QtWidgets.QAction("&Save", self)
        saveAction.setShortcuts(QtGui.QKeySequence.Save)
        saveAction.setStatusTip("Save file")
        saveAction.setIcon(QtGui.QIcon.fromTheme("document-save"))
        saveAction.triggered.connect(self.save)

        saveAsAction = QtWidgets.QAction("Save_&As", self)
        saveAsAction.setShortcuts(QtGui.QKeySequence.SaveAs)
        saveAsAction.setStatusTip("Save file as...")
        saveAsAction.setIcon(QtGui.QIcon.fromTheme("document-save-as"))
        saveAsAction.triggered.connect(self.save_as)

        undoAction = self.editor.undo_stack.createUndoAction(
            self, self.tr("&Undo"))
        undoAction.setShortcuts(QtGui.QKeySequence.Undo)
        undoAction.setIcon(QtGui.QIcon.fromTheme("edit-undo"))
        redoAction = self.editor.undo_stack.createRedoAction(
            self, self.tr("&Redo"))
        redoAction.setShortcuts(QtGui.QKeySequence.Redo)
        redoAction.setIcon(QtGui.QIcon.fromTheme("edit-redo"))

        ## Menu
        # file_menu = self.widget.menuBar.addMenu("&File")
        # file_menu.addAction(newAction)
        # file_menu.addAction(openAction)
        # file_menu.addAction(saveAction)
        # file_menu.addAction(saveAsAction)
        #
        # edit_menu = self.widget.menuBar.addMenu("&Edit")
        #
        # edit_menu.addAction(undoAction)
        # edit_menu.addAction(redoAction)

        ## Tool bar
        tool_bar = self.widget.mainToolBar

        undoButton = QtWidgets.QToolButton()
        undoButton.setDefaultAction(undoAction)
        redoButton = QtWidgets.QToolButton()
        redoButton.setDefaultAction(redoAction)

        tool_bar.addAction(newAction)
        tool_bar.addAction(openAction)
        tool_bar.addAction(saveAction)
        tool_bar.addAction(saveAsAction)
        tool_bar.addSeparator()
        tool_bar.addWidget(undoButton)
        tool_bar.addWidget(redoButton)
 def setupButtons(self, yaml_file):
     """
     Parse yaml file and setup Buttons. Format of the yaml file should be:
     - name: 'button name' (required)
       image: 'path to image for icon' (optional)
       image_size: 'width and height of icon' (optional)
       command: 'command' (required)
       column: 'column index' (optional, defaults to 0)
     """
     self.buttons = []
     with open(yaml_file) as f:
         yaml_data = yaml.load(f)
         # lookup colum direction
         direction = 'vertical'
         for d in yaml_data:
             if d.has_key('direction'):
                 if d['direction'] == 'horizontal':
                     direction = 'horizontal'
                 else: # d['direction'] == 'vertical':
                     direction = 'vertical'
                 yaml_data.remove(d)
                 break
         # lookup column num
         column_indices = [d['column'] for d in yaml_data]
         max_column_index = max(*column_indices)
         if direction == 'vertical':
             self.layout = QtWidgets.QHBoxLayout()
             self.layout_boxes = [QtWidgets.QVBoxLayout()
                                  for i in range(max_column_index + 1)]
         else: # direction == 'horizontal'
             self.layout = QtWidgets.QVBoxLayout()
             self.layout_boxes = [QtWidgets.QHBoxLayout()
                                  for i in range(max_column_index + 1)]
         self.button_groups = [QtWidgets.QGroupBox()
                              for i in range(max_column_index + 1)]
         for button_data in yaml_data:
             # check if all the field is available
             if not button_data.has_key("name"):
                 self.showError("name field is missed in yaml")
                 raise Exception("name field is missed in yaml")
             if not button_data.has_key("command"):
                 self.showError("command field is missed in yaml")
                 raise Exception("command field is missed in yaml")
             if self.button_type == "push":
                 button = QtWidgets.QToolButton()
             else: # self.button_type == "Radio":
                 button = QtWidgets.QRadioButton()
             button.setSizePolicy(QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred))
             if button_data.has_key("image"):
                 image_file = get_filename(button_data["image"])[len("file://"):]
                 if os.path.exists(image_file):
                     icon = QtGui.QIcon(image_file)
                     button.setIcon(icon)
                     if button_data.has_key("image_size"):
                         button.setIconSize(QSize(button_data["image_size"][0], button_data["image_size"][1]))
                     else:
                         button.setIconSize(QSize(100, 100))
             if button_data.has_key("name"):
                 name = button_data['name']
                 button.setText(name)
             button.clicked.connect(self.buttonCallback(button_data['command']))
             if self.button_type == "push":
                 button.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
             else: # self.button_type == "Radio":
                 if button_data.has_key("default_value") and button_data['default_value']:
                     button.setChecked(True)
             self.layout_boxes[button_data['column']].addWidget(button)
             self.buttons.append(button)
         for i in range(len(self.button_groups)):
             self.button_groups[i].setLayout(self.layout_boxes[i])
         for group in self.button_groups:
             self.layout.addWidget(group)
         self.setLayout(self.layout)