Ejemplo n.º 1
0
    def __init__(self, parent=None, structure=None):
        super(MainDialog, self).__init__(parent=parent)
        self.setupUi(self)

        self.structure = structure

        self.mode = 'Create'

        if self.structure:
            self.mode = 'Update'

        self.dialog_label.setText('%s Structure' % self.mode)

        # create name_line_edit
        from anima.ui.widgets import ValidatedLineEdit
        self.name_line_edit = ValidatedLineEdit(
            message_field=self.name_validator_label
        )
        self.name_line_edit.setPlaceholderText('Enter Name')
        self.name_fields_verticalLayout.insertWidget(
            0, self.name_line_edit
        )

        # create DoubleListWidget
        from anima.ui.widgets import DoubleListWidget
        self.filename_templates_double_list_widget = DoubleListWidget(
            dialog=self,
            parent_layout=self.filename_template_fields_verticalLayout,
            primary_label_text='Templates From DB',
            secondary_label_text='Selected Templates'
        )
        # set the tooltip
        self.filename_templates_double_list_widget\
            .primary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )
        self.filename_templates_double_list_widget\
            .secondary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )

        self._setup_signals()

        self._set_defaults()

        if self.structure:
            self.fill_ui_with_structure(self.structure)
Ejemplo n.º 2
0
    def __init__(self, parent=None, structure=None):
        super(MainDialog, self).__init__(parent=parent)
        self.setupUi(self)

        self.structure = structure

        self.mode = 'Create'

        if self.structure:
            self.mode = 'Update'

        self.dialog_label.setText('%s Structure' % self.mode)

        # create name_lineEdit
        from anima.ui.widgets import ValidatedLineEdit
        self.name_lineEdit = ValidatedLineEdit(
            message_field=self.name_validator_label
        )
        self.name_lineEdit.setPlaceholderText('Enter Name')
        self.name_fields_verticalLayout.insertWidget(
            0, self.name_lineEdit
        )

        # create DoubleListWidget
        from anima.ui.widgets import DoubleListWidget
        self.filename_templates_double_list_widget = DoubleListWidget(
            dialog=self,
            parent_layout=self.filename_template_fields_verticalLayout,
            primary_label_text='Templates From DB',
            secondary_label_text='Selected Templates'
        )
        # set the tooltip
        self.filename_templates_double_list_widget\
            .primary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )
        self.filename_templates_double_list_widget\
            .secondary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )

        self._setup_signals()

        self._set_defaults()

        if self.structure:
            self.fill_ui_with_structure(self.structure)
Ejemplo n.º 3
0
class MainDialog(QtWidgets.QDialog, structure_dialog_UI.Ui_Dialog, AnimaDialogBase):
    """The structure Dialog
    """

    def __init__(self, parent=None, structure=None):
        super(MainDialog, self).__init__(parent=parent)
        self.setupUi(self)

        self.structure = structure

        self.mode = 'Create'

        if self.structure:
            self.mode = 'Update'

        self.dialog_label.setText('%s Structure' % self.mode)

        # create name_line_edit
        from anima.ui.widgets import ValidatedLineEdit
        self.name_line_edit = ValidatedLineEdit(
            message_field=self.name_validator_label
        )
        self.name_line_edit.setPlaceholderText('Enter Name')
        self.name_fields_verticalLayout.insertWidget(
            0, self.name_line_edit
        )

        # create DoubleListWidget
        from anima.ui.widgets import DoubleListWidget
        self.filename_templates_double_list_widget = DoubleListWidget(
            dialog=self,
            parent_layout=self.filename_template_fields_verticalLayout,
            primary_label_text='Templates From DB',
            secondary_label_text='Selected Templates'
        )
        # set the tooltip
        self.filename_templates_double_list_widget\
            .primary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )
        self.filename_templates_double_list_widget\
            .secondary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )

        self._setup_signals()

        self._set_defaults()

        if self.structure:
            self.fill_ui_with_structure(self.structure)

    def _setup_signals(self):
        """create the signals
        """
        # name_line_edit is changed
        QtCore.QObject.connect(
            self.name_line_edit,
            QtCore.SIGNAL('textChanged(QString)'),
            self.name_line_edit_changed
        )

        # add context menu for primary items in DoubleListWidget
        self.filename_templates_double_list_widget\
            .primary_list_widget\
            .setContextMenuPolicy(QtCore.Qt.CustomContextMenu)

        QtCore.QObject.connect(
            self.filename_templates_double_list_widget.primary_list_widget,
            QtCore.SIGNAL("customContextMenuRequested(const QPoint&)"),
            self.show_primary_filename_template_context_menu
        )

        # add context menu for secondary items in DoubleListWidget
        self.filename_templates_double_list_widget\
            .secondary_list_widget\
            .setContextMenuPolicy(QtCore.Qt.CustomContextMenu)

        QtCore.QObject.connect(
            self.filename_templates_double_list_widget.secondary_list_widget,
            QtCore.SIGNAL("customContextMenuRequested(const QPoint&)"),
            self.show_secondary_filename_template_context_menu
        )

    def _set_defaults(self):
        """sets the default values
        """
        # fill filename_templates_from_db_listWidget
        # add all the other filename templates from the database
        from stalker import FilenameTemplate
        fts = FilenameTemplate.query.all()

        self.filename_templates_double_list_widget.clear()
        self.filename_templates_double_list_widget.add_primary_items(
            map(
                lambda x: '%s (%s) (%s)' % (x.name,
                                            x.target_entity_type,
                                            x.id),
                fts
            )
        )

    def name_line_edit_changed(self, text):
        """runs when the name_line_edit text has changed
        """
        if re.findall(r'[^a-zA-Z0-9\-_ ]+', text):
            self.name_line_edit.set_invalid('Invalid character')
        else:
            if text == '':
                self.name_line_edit.set_invalid('Enter a name')
            else:
                self.name_line_edit.set_valid()

    def fill_ui_with_structure(self, structure):
        """fills the UI with the given structure

        :param structure: A Stalker ImageFormat instance
        :return:
        """
        if False:
            from stalker import Structure
            assert isinstance(structure, Structure)

        self.structure = structure
        self.name_line_edit.setText(self.structure.name)
        self.name_line_edit.set_valid()

        self.custom_template_plainTextEdit.setPlainText(
            self.structure.custom_template
        )

        # add the structure templates to the secondary list of the double list
        self.filename_templates_double_list_widget.clear()
        self.filename_templates_double_list_widget.add_secondary_items(
            map(
                lambda x: '%s (%s) (%s)' % (x.name,
                                            x.target_entity_type,
                                            x.id),
                self.structure.templates
            )
        )

        # add all the other filename templates from the database
        from stalker import FilenameTemplate
        fts = FilenameTemplate.query\
            .filter(
                ~FilenameTemplate.id.in_(
                    map(lambda x: x.id, self.structure.templates)
                )
            )\
            .all()

        self.filename_templates_double_list_widget.add_primary_items(
            map(
                lambda x: '%s (%s) (%s)' % (x.name,
                                            x.target_entity_type,
                                            x.id),
                fts
            )
        )

    def show_primary_filename_template_context_menu(self, position):
        """shows the custom context menu for primary list widget

        :param position:
        :return:
        """
        self.show_filename_template_context_menu(
            self.filename_templates_double_list_widget.primary_list_widget,
            position
        )

    def show_secondary_filename_template_context_menu(self, position):
        """shows the custom context menu for secondary list widget

        :param position:
        :return:
        """
        self.show_filename_template_context_menu(
            self.filename_templates_double_list_widget.secondary_list_widget,
            position
        )

    def show_filename_template_context_menu(self, list_widget, position):
        """shows a context menu for the given list_widget

        :param list_widget: QListWidget instance
        :param position: the mouse click position
        :return:
        """
        item = list_widget.itemAt(position)

        menu = QtWidgets.QMenu()
        menu.addAction('Create FilenameTemplate...')
        if item:
            menu.addAction('Update FilenameTemplate...')

        global_position = list_widget.mapToGlobal(position)
        selected_item = menu.exec_(global_position)

        try:
            # PySide
            accepted = QtWidgets.QDialog.DialogCode.Accepted
        except AttributeError:
            # PyQt4
            accepted = QtWidgets.QDialog.Accepted

        if selected_item:
            choice = selected_item.text()
            if choice == 'Create FilenameTemplate...':
                from anima.ui import filename_template_dialog
                create_filename_template_dialog = \
                    filename_template_dialog.MainDialog(parent=self)
                create_filename_template_dialog.exec_()

                if create_filename_template_dialog.result() == accepted:
                    ft = create_filename_template_dialog.filename_template
                    list_widget.addItem(
                        '%s (%s) (%s)' % (ft.name,
                                          ft.target_entity_type,
                                          ft.id)
                    )
                create_filename_template_dialog.deleteLater()

            elif choice == 'Update FilenameTemplate...':
                ft_id = int(item.text().split('(')[-1].split(')')[0])
                if not ft_id:
                    return

                from stalker import FilenameTemplate
                ft = FilenameTemplate.query.get(ft_id)
    
                from anima.ui import filename_template_dialog
                update_filename_template_dialog = \
                    filename_template_dialog.MainDialog(
                        parent=self,
                        filename_template=ft
                    )
                try:
                    update_filename_template_dialog.exec_()
                    if update_filename_template_dialog.result() == accepted:
                        # update the text of the item
                        ft = update_filename_template_dialog.filename_template
                        item.setText(
                            '%s (%s) (%s)' % (ft.name,
                                              ft.target_entity_type,
                                              ft.id)
                        )
    
                    update_filename_template_dialog.deleteLater()
                except Exception as e:
                    QtWidgets.QMessageBox.warning(
                        self,
                        "Error",
                        str(e)
                    )
                    return

    def accept(self):
        """overridden accept method
        """
        if not self.name_line_edit.is_valid:
            QtWidgets.QMessageBox.critical(
                self,
                'Error',
                'Please fix <b>name</b> field!'
            )
            return
        name = self.name_line_edit.text()

        custom_template = self.custom_template_plainTextEdit.toPlainText()

        filename_template_items = \
            self.filename_templates_double_list_widget.secondary_items()
        filename_template_ids = []
        for item in filename_template_items:
            filename_template_id = \
                int(item.text().split('(')[-1].split(')')[0])
            filename_template_ids.append(filename_template_id)

        from stalker import FilenameTemplate
        filename_templates = FilenameTemplate.query\
            .filter(FilenameTemplate.id.in_(filename_template_ids)).all()

        from stalker import Structure
        from stalker.db.session import DBSession
        logged_in_user = self.get_logged_in_user()
        if self.mode == 'Create':
            # Create a new Structure
            try:
                structure = Structure(
                    name=name,
                    templates=filename_templates,
                    custom_template=custom_template,
                    created_by=logged_in_user
                )
                self.structure = structure
                DBSession.add(structure)
                DBSession.commit()
            except Exception as e:
                DBSession.rollback()
                QtWidgets.QMessageBox.critical(
                    self,
                    'Error',
                    str(e)
                )
                return

        elif self.mode == 'Update':
            # Update the structure
            try:
                self.structure.name = name
                self.structure.templates = filename_templates
                self.structure.custom_template = custom_template
                self.structure.updated_by = logged_in_user
                DBSession.add(self.structure)
                DBSession.commit()
            except Exception as e:
                DBSession.rollback()
                QtWidgets.QMessageBox.critical(
                    self,
                    'Error',
                    str(e)
                )
                return

        super(MainDialog, self).accept()
Ejemplo n.º 4
0
    def _setup_ui(self):
        """create UI elements
        """
        self.resize(520, 550)
        self.vertical_layout = QtWidgets.QVBoxLayout(self)

        # -------------------------
        # Dialog Label
        self.dialog_label = QtWidgets.QLabel(self)
        self.dialog_label.setText('Set Project Users')
        self.dialog_label.setStyleSheet(
            "color: rgb(71, 143, 202);\nfont: 18pt;")
        self.vertical_layout.addWidget(self.dialog_label)
        self.line = QtWidgets.QFrame(self)
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.vertical_layout.addWidget(self.line)

        self.form_layout = QtWidgets.QFormLayout()
        self.form_layout.setLabelAlignment(QtCore.Qt.AlignRight
                                           | QtCore.Qt.AlignTrailing
                                           | QtCore.Qt.AlignVCenter)
        self.vertical_layout.addLayout(self.form_layout)

        i = -1

        # create Project Combo box
        i += 1
        self.projects_label = QtWidgets.QLabel(self)
        self.projects_label.setText('Projects')
        self.form_layout.setWidget(i, QtWidgets.QFormLayout.LabelRole,
                                   self.projects_label)

        self.projects_combo_box = QtWidgets.QComboBox(self)
        self.form_layout.setWidget(i, QtWidgets.QFormLayout.FieldRole,
                                   self.projects_combo_box)

        # create DoubleListWidget
        i += 1
        self.users_label = QtWidgets.QLabel(self)
        self.users_label.setText('Users')
        self.form_layout.setWidget(i, QtWidgets.QFormLayout.LabelRole,
                                   self.users_label)

        self.users_fields_vertical_layout = QtWidgets.QVBoxLayout()
        self.form_layout.setLayout(i, QtWidgets.QFormLayout.FieldRole,
                                   self.users_fields_vertical_layout)

        from anima.ui.widgets import DoubleListWidget
        self.users_double_list_widget = DoubleListWidget(
            dialog=self,
            parent_layout=self.users_fields_vertical_layout,
            primary_label_text='Users From DB',
            secondary_label_text='Selected Users')
        # set the tooltip
        self.users_double_list_widget\
            .primary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )
        self.users_double_list_widget\
            .secondary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )

        # Button Box
        self.button_box = QtWidgets.QDialogButtonBox(self)
        self.button_box.setOrientation(QtCore.Qt.Horizontal)
        self.button_box.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel
                                           | QtWidgets.QDialogButtonBox.Ok)
        self.vertical_layout.addWidget(self.button_box)
        self.vertical_layout.setStretch(2, 1)
Ejemplo n.º 5
0
class MainDialog(QtWidgets.QDialog, AnimaDialogBase):
    """The Project Users Dialog
    """
    def __init__(self, parent=None, project=None):
        super(MainDialog, self).__init__(parent)

        self.project = project

        self._setup_ui()
        self._setup_signals()
        self._set_defaults()

        if self.project:
            self.fill_ui_with_project(self.project)

    def _setup_ui(self):
        """create UI elements
        """
        self.resize(520, 550)
        self.vertical_layout = QtWidgets.QVBoxLayout(self)

        # -------------------------
        # Dialog Label
        self.dialog_label = QtWidgets.QLabel(self)
        self.dialog_label.setText('Set Project Users')
        self.dialog_label.setStyleSheet(
            "color: rgb(71, 143, 202);\nfont: 18pt;")
        self.vertical_layout.addWidget(self.dialog_label)
        self.line = QtWidgets.QFrame(self)
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.vertical_layout.addWidget(self.line)

        self.form_layout = QtWidgets.QFormLayout()
        self.form_layout.setLabelAlignment(QtCore.Qt.AlignRight
                                           | QtCore.Qt.AlignTrailing
                                           | QtCore.Qt.AlignVCenter)
        self.vertical_layout.addLayout(self.form_layout)

        i = -1

        # create Project Combo box
        i += 1
        self.projects_label = QtWidgets.QLabel(self)
        self.projects_label.setText('Projects')
        self.form_layout.setWidget(i, QtWidgets.QFormLayout.LabelRole,
                                   self.projects_label)

        self.projects_combo_box = QtWidgets.QComboBox(self)
        self.form_layout.setWidget(i, QtWidgets.QFormLayout.FieldRole,
                                   self.projects_combo_box)

        # create DoubleListWidget
        i += 1
        self.users_label = QtWidgets.QLabel(self)
        self.users_label.setText('Users')
        self.form_layout.setWidget(i, QtWidgets.QFormLayout.LabelRole,
                                   self.users_label)

        self.users_fields_vertical_layout = QtWidgets.QVBoxLayout()
        self.form_layout.setLayout(i, QtWidgets.QFormLayout.FieldRole,
                                   self.users_fields_vertical_layout)

        from anima.ui.widgets import DoubleListWidget
        self.users_double_list_widget = DoubleListWidget(
            dialog=self,
            parent_layout=self.users_fields_vertical_layout,
            primary_label_text='Users From DB',
            secondary_label_text='Selected Users')
        # set the tooltip
        self.users_double_list_widget\
            .primary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )
        self.users_double_list_widget\
            .secondary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )

        # Button Box
        self.button_box = QtWidgets.QDialogButtonBox(self)
        self.button_box.setOrientation(QtCore.Qt.Horizontal)
        self.button_box.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel
                                           | QtWidgets.QDialogButtonBox.Ok)
        self.vertical_layout.addWidget(self.button_box)
        self.vertical_layout.setStretch(2, 1)

    def _setup_signals(self):
        """setup ui signals
        """
        QtCore.QObject.connect(self.projects_combo_box,
                               QtCore.SIGNAL("currentIndexChanged(QString)"),
                               self.project_changed)

        QtCore.QObject.connect(self.button_box, QtCore.SIGNAL("accepted()"),
                               self.accept)
        QtCore.QObject.connect(self.button_box, QtCore.SIGNAL("rejected()"),
                               self.reject)

    def _set_defaults(self):
        """set defaults
        """
        # fill with projects
        from stalker.db.session import DBSession
        from stalker import Project
        projects_data = \
            DBSession\
                .query(Project.id, Project.name)\
                .order_by(Project.name)\
                .all()

        self.projects_combo_box.clear()
        for p_data in projects_data:
            self.projects_combo_box.addItem(p_data.name, p_data.id)

    def fill_ui_with_project(self, project):
        """

        :param project: A Stalker Project instance
        :return:
        """
        # no project no gain
        if project is None:
            return

        # select the given project in the UI
        index = self.projects_combo_box.findData(project.id)
        if index:
            self.projects_combo_box.setCurrentIndex(index)

    def project_changed(self, project_name):
        """runs when the project in the combo box changed
        """
        try:
            project_id = self.projects_combo_box.currentData()
        except AttributeError:
            index = self.projects_combo_box.currentIndex()
            project_id = self.projects_combo_box.itemData(index)

        # refresh the items on the double list widget
        self.users_double_list_widget.clear()

        # get users not in the project
        from stalker.db.session import DBSession
        from stalker import User
        from stalker.models.project import ProjectUser

        project_users = DBSession.query(User.id, User.name).join(ProjectUser)\
            .filter(ProjectUser.project_id == project_id)\
            .filter(User.id == ProjectUser.user_id)\
            .all()

        project_user_ids = [u.id for u in project_users]

        if project_user_ids:
            users_not_in_project = [
                u.name for u in DBSession.query(User.name).filter(
                    ~User.id.in_(project_user_ids)).all()
            ]
        else:
            users_not_in_project = [
                u.name for u in DBSession.query(User.name).all()
            ]

        self.users_double_list_widget.add_primary_items(users_not_in_project)

        users_in_project = \
            [u.name for u in project_users]

        self.users_double_list_widget.add_secondary_items(users_in_project)

    def accept(self):
        """overridden accept method
        """
        # get the project
        try:
            project_id = self.projects_combo_box.currentData()
        except AttributeError:
            index = self.projects_combo_box.currentIndex()
            project_id = self.projects_combo_box.itemData(index)

        from stalker import Project
        project = Project.query.get(project_id)

        # get the users
        user_names = [
            item.text()
            for item in self.users_double_list_widget.secondary_items()
        ]

        from stalker import User
        if user_names:
            users = User.query.filter(User.name.in_(user_names))
        else:
            users = []

        # set the project users
        project.users = users
        from stalker.db.session import DBSession
        DBSession.commit()

        super(MainDialog, self).accept()
Ejemplo n.º 6
0
    def _setup_ui(self):
        """create UI elements
        """
        self.resize(520, 550)
        self.vertical_layout = QtWidgets.QVBoxLayout(self)

        # -------------------------
        # Dialog Label
        self.dialog_label = QtWidgets.QLabel(self)
        self.dialog_label.setText('Set Project Users')
        self.dialog_label.setStyleSheet(
            "color: rgb(71, 143, 202);\nfont: 18pt;"
        )
        self.vertical_layout.addWidget(self.dialog_label)
        self.line = QtWidgets.QFrame(self)
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.vertical_layout.addWidget(self.line)

        self.form_layout = QtWidgets.QFormLayout()
        self.form_layout.setLabelAlignment(
            QtCore.Qt.AlignRight |
            QtCore.Qt.AlignTrailing |
            QtCore.Qt.AlignVCenter
        )
        self.vertical_layout.addLayout(self.form_layout)

        i = -1

        # create Project Combo box
        i += 1
        self.projects_label = QtWidgets.QLabel(self)
        self.projects_label.setText('Projects')
        self.form_layout.setWidget(
            i,
            QtWidgets.QFormLayout.LabelRole,
            self.projects_label
        )

        self.projects_combo_box = QtWidgets.QComboBox(self)
        self.form_layout.setWidget(
            i,
            QtWidgets.QFormLayout.FieldRole,
            self.projects_combo_box
        )

        # create DoubleListWidget
        i += 1
        self.users_label = QtWidgets.QLabel(self)
        self.users_label.setText('Users')
        self.form_layout.setWidget(
            i,
            QtWidgets.QFormLayout.LabelRole,
            self.users_label
        )

        self.users_fields_vertical_layout = QtWidgets.QVBoxLayout()
        self.form_layout.setLayout(
            i,
            QtWidgets.QFormLayout.FieldRole,
            self.users_fields_vertical_layout
        )

        from anima.ui.widgets import DoubleListWidget
        self.users_double_list_widget = DoubleListWidget(
            dialog=self,
            parent_layout=self.users_fields_vertical_layout,
            primary_label_text='Users From DB',
            secondary_label_text='Selected Users'
        )
        # set the tooltip
        self.users_double_list_widget\
            .primary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )
        self.users_double_list_widget\
            .secondary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )

        # Button Box
        self.button_box = QtWidgets.QDialogButtonBox(self)
        self.button_box.setOrientation(QtCore.Qt.Horizontal)
        self.button_box.setStandardButtons(
            QtWidgets.QDialogButtonBox.Cancel | QtWidgets.QDialogButtonBox.Ok
        )
        self.vertical_layout.addWidget(self.button_box)
        self.vertical_layout.setStretch(2, 1)
Ejemplo n.º 7
0
class MainDialog(QtWidgets.QDialog, AnimaDialogBase):
    """The Project Users Dialog
    """

    def __init__(self, parent=None, project=None):
        super(MainDialog, self).__init__(parent)

        self.project = project

        self._setup_ui()
        self._setup_signals()
        self._set_defaults()

        if self.project:
            self.fill_ui_with_project(self.project)

    def _setup_ui(self):
        """create UI elements
        """
        self.resize(520, 550)
        self.vertical_layout = QtWidgets.QVBoxLayout(self)

        # -------------------------
        # Dialog Label
        self.dialog_label = QtWidgets.QLabel(self)
        self.dialog_label.setText('Set Project Users')
        self.dialog_label.setStyleSheet(
            "color: rgb(71, 143, 202);\nfont: 18pt;"
        )
        self.vertical_layout.addWidget(self.dialog_label)
        self.line = QtWidgets.QFrame(self)
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.vertical_layout.addWidget(self.line)

        self.form_layout = QtWidgets.QFormLayout()
        self.form_layout.setLabelAlignment(
            QtCore.Qt.AlignRight |
            QtCore.Qt.AlignTrailing |
            QtCore.Qt.AlignVCenter
        )
        self.vertical_layout.addLayout(self.form_layout)

        i = -1

        # create Project Combo box
        i += 1
        self.projects_label = QtWidgets.QLabel(self)
        self.projects_label.setText('Projects')
        self.form_layout.setWidget(
            i,
            QtWidgets.QFormLayout.LabelRole,
            self.projects_label
        )

        self.projects_combo_box = QtWidgets.QComboBox(self)
        self.form_layout.setWidget(
            i,
            QtWidgets.QFormLayout.FieldRole,
            self.projects_combo_box
        )

        # create DoubleListWidget
        i += 1
        self.users_label = QtWidgets.QLabel(self)
        self.users_label.setText('Users')
        self.form_layout.setWidget(
            i,
            QtWidgets.QFormLayout.LabelRole,
            self.users_label
        )

        self.users_fields_vertical_layout = QtWidgets.QVBoxLayout()
        self.form_layout.setLayout(
            i,
            QtWidgets.QFormLayout.FieldRole,
            self.users_fields_vertical_layout
        )

        from anima.ui.widgets import DoubleListWidget
        self.users_double_list_widget = DoubleListWidget(
            dialog=self,
            parent_layout=self.users_fields_vertical_layout,
            primary_label_text='Users From DB',
            secondary_label_text='Selected Users'
        )
        # set the tooltip
        self.users_double_list_widget\
            .primary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )
        self.users_double_list_widget\
            .secondary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )

        # Button Box
        self.button_box = QtWidgets.QDialogButtonBox(self)
        self.button_box.setOrientation(QtCore.Qt.Horizontal)
        self.button_box.setStandardButtons(
            QtWidgets.QDialogButtonBox.Cancel | QtWidgets.QDialogButtonBox.Ok
        )
        self.vertical_layout.addWidget(self.button_box)
        self.vertical_layout.setStretch(2, 1)

    def _setup_signals(self):
        """setup ui signals
        """
        QtCore.QObject.connect(
            self.projects_combo_box,
            QtCore.SIGNAL("currentIndexChanged(QString)"),
            self.project_changed
        )

        QtCore.QObject.connect(
            self.button_box,
            QtCore.SIGNAL("accepted()"),
            self.accept
        )
        QtCore.QObject.connect(
            self.button_box,
            QtCore.SIGNAL("rejected()"),
            self.reject
        )

    def _set_defaults(self):
        """set defaults
        """
        # fill with projects
        from stalker.db.session import DBSession
        from stalker import Project
        projects_data = \
            DBSession\
                .query(Project.id, Project.name)\
                .order_by(Project.name)\
                .all()

        self.projects_combo_box.clear()
        for p_data in projects_data:
            self.projects_combo_box.addItem(p_data.name, p_data.id)

    def fill_ui_with_project(self, project):
        """

        :param project: A Stalker Project instance
        :return:
        """
        # no project no gain
        if project is None:
            return

        # select the given project in the UI
        index = self.projects_combo_box.findData(project.id)
        if index:
            self.projects_combo_box.setCurrentIndex(index)

    def project_changed(self, project_name):
        """runs when the project in the combo box changed
        """
        try:
            project_id = self.projects_combo_box.currentData()
        except AttributeError:
            index = self.projects_combo_box.currentIndex()
            project_id = self.projects_combo_box.itemData(index)

        # refresh the items on the double list widget
        self.users_double_list_widget.clear()

        # get users not in the project
        from stalker.db.session import DBSession
        from stalker import User
        from stalker.models.project import ProjectUser

        project_users = DBSession.query(User.id, User.name).join(ProjectUser)\
            .filter(ProjectUser.project_id == project_id)\
            .filter(User.id == ProjectUser.user_id)\
            .all()

        project_user_ids = [u.id for u in project_users]

        if project_user_ids:
            users_not_in_project = [
                u.name
                for u in DBSession.query(User.name)
                    .filter(~User.id.in_(project_user_ids)).all()
            ]
        else:
            users_not_in_project = [
                u.name
                for u in DBSession.query(User.name).all()
            ]

        self.users_double_list_widget.add_primary_items(
            users_not_in_project
        )

        users_in_project = \
            [u.name for u in project_users]

        self.users_double_list_widget.add_secondary_items(
            users_in_project
        )

    def accept(self):
        """overridden accept method
        """
        # get the project
        try:
            project_id = self.projects_combo_box.currentData()
        except AttributeError:
            index = self.projects_combo_box.currentIndex()
            project_id = self.projects_combo_box.itemData(index)

        from stalker import Project
        project = Project.query.get(project_id)

        # get the users
        user_names = [
            item.text() for item in
            self.users_double_list_widget.secondary_items()
        ]

        from stalker import User
        if user_names:
            users = User.query.filter(User.name.in_(user_names))
        else:
            users = []

        # set the project users
        project.users = users
        from stalker.db.session import DBSession
        DBSession.commit()

        super(MainDialog, self).accept()
Ejemplo n.º 8
0
class MainDialog(QtWidgets.QDialog, structure_dialog_UI.Ui_Dialog, AnimaDialogBase):
    """The structure Dialog
    """

    def __init__(self, parent=None, structure=None):
        super(MainDialog, self).__init__(parent=parent)
        self.setupUi(self)

        self.structure = structure

        self.mode = 'Create'

        if self.structure:
            self.mode = 'Update'

        self.dialog_label.setText('%s Structure' % self.mode)

        # create name_lineEdit
        from anima.ui.widgets import ValidatedLineEdit
        self.name_lineEdit = ValidatedLineEdit(
            message_field=self.name_validator_label
        )
        self.name_lineEdit.setPlaceholderText('Enter Name')
        self.name_fields_verticalLayout.insertWidget(
            0, self.name_lineEdit
        )

        # create DoubleListWidget
        from anima.ui.widgets import DoubleListWidget
        self.filename_templates_double_list_widget = DoubleListWidget(
            dialog=self,
            parent_layout=self.filename_template_fields_verticalLayout,
            primary_label_text='Templates From DB',
            secondary_label_text='Selected Templates'
        )
        # set the tooltip
        self.filename_templates_double_list_widget\
            .primary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )
        self.filename_templates_double_list_widget\
            .secondary_list_widget.setToolTip(
                "Right Click to Create/Update FilenameTemplates"
            )

        self._setup_signals()

        self._set_defaults()

        if self.structure:
            self.fill_ui_with_structure(self.structure)

    def _setup_signals(self):
        """create the signals
        """
        # name_lineEdit is changed
        QtCore.QObject.connect(
            self.name_lineEdit,
            QtCore.SIGNAL('textChanged(QString)'),
            self.name_line_edit_changed
        )

        # add context menu for primary items in DoubleListWidget
        self.filename_templates_double_list_widget\
            .primary_list_widget\
            .setContextMenuPolicy(QtCore.Qt.CustomContextMenu)

        QtCore.QObject.connect(
            self.filename_templates_double_list_widget.primary_list_widget,
            QtCore.SIGNAL("customContextMenuRequested(const QPoint&)"),
            self.show_primary_filename_template_context_menu
        )

        # add context menu for secondary items in DoubleListWidget
        self.filename_templates_double_list_widget\
            .secondary_list_widget\
            .setContextMenuPolicy(QtCore.Qt.CustomContextMenu)

        QtCore.QObject.connect(
            self.filename_templates_double_list_widget.secondary_list_widget,
            QtCore.SIGNAL("customContextMenuRequested(const QPoint&)"),
            self.show_secondary_filename_template_context_menu
        )

    def _set_defaults(self):
        """sets the default values
        """
        # fill filename_templates_from_db_listWidget
        # add all the other filename templates from the database
        from stalker import FilenameTemplate
        fts = FilenameTemplate.query.all()

        self.filename_templates_double_list_widget.clear()
        self.filename_templates_double_list_widget.add_primary_items(
            map(
                lambda x: '%s (%s) (%s)' % (x.name,
                                            x.target_entity_type,
                                            x.id),
                fts
            )
        )

    def name_line_edit_changed(self, text):
        """runs when the name_lineEdit text has changed
        """
        if re.findall(r'[^a-zA-Z0-9\-_ ]+', text):
            self.name_lineEdit.set_invalid('Invalid character')
        else:
            if text == '':
                self.name_lineEdit.set_invalid('Enter a name')
            else:
                self.name_lineEdit.set_valid()

    def fill_ui_with_structure(self, structure):
        """fills the UI with the given structure

        :param structure: A Stalker ImageFormat instance
        :return:
        """
        if False:
            from stalker import Structure
            assert isinstance(structure, Structure)

        self.structure = structure
        self.name_lineEdit.setText(self.structure.name)
        self.name_lineEdit.set_valid()

        self.custom_template_plainTextEdit.setPlainText(
            self.structure.custom_template
        )

        # add the structure templates to the secondary list of the double list
        self.filename_templates_double_list_widget.clear()
        self.filename_templates_double_list_widget.add_secondary_items(
            map(
                lambda x: '%s (%s) (%s)' % (x.name,
                                            x.target_entity_type,
                                            x.id),
                self.structure.templates
            )
        )

        # add all the other filename templates from the database
        from stalker import FilenameTemplate
        fts = FilenameTemplate.query\
            .filter(
                ~FilenameTemplate.id.in_(
                    map(lambda x: x.id, self.structure.templates)
                )
            )\
            .all()

        self.filename_templates_double_list_widget.add_primary_items(
            map(
                lambda x: '%s (%s) (%s)' % (x.name,
                                            x.target_entity_type,
                                            x.id),
                fts
            )
        )

    def show_primary_filename_template_context_menu(self, position):
        """shows the custom context menu for primary list widget

        :param position:
        :return:
        """
        self.show_filename_template_context_menu(
            self.filename_templates_double_list_widget.primary_list_widget,
            position
        )

    def show_secondary_filename_template_context_menu(self, position):
        """shows the custom context menu for secondary list widget

        :param position:
        :return:
        """
        self.show_filename_template_context_menu(
            self.filename_templates_double_list_widget.secondary_list_widget,
            position
        )

    def show_filename_template_context_menu(self, list_widget, position):
        """shows a context menu for the given list_widget

        :param list_widget: QListWidget instance
        :param position: the mouse click position
        :return:
        """
        item = list_widget.itemAt(position)

        menu = QtWidgets.QMenu()
        menu.addAction('Create FilenameTemplate...')
        if item:
            menu.addAction('Update FilenameTemplate...')

        global_position = list_widget.mapToGlobal(position)
        selected_item = menu.exec_(global_position)

        try:
            # PySide
            accepted = QtWidgets.QDialog.DialogCode.Accepted
        except AttributeError:
            # PyQt4
            accepted = QtWidgets.QDialog.Accepted

        if selected_item:
            choice = selected_item.text()
            if choice == 'Create FilenameTemplate...':
                from anima.ui import filename_template_dialog
                create_filename_template_dialog = \
                    filename_template_dialog.MainDialog(parent=self)
                create_filename_template_dialog.exec_()

                if create_filename_template_dialog.result() == accepted:
                    ft = create_filename_template_dialog.filename_template
                    list_widget.addItem(
                        '%s (%s) (%s)' % (ft.name,
                                          ft.target_entity_type,
                                          ft.id)
                    )
                create_filename_template_dialog.deleteLater()

            elif choice == 'Update FilenameTemplate...':
                ft_id = int(item.text().split('(')[-1].split(')')[0])
                if not ft_id:
                    return

                from stalker import FilenameTemplate
                ft = FilenameTemplate.query.get(ft_id)
    
                from anima.ui import filename_template_dialog
                update_filename_template_dialog = \
                    filename_template_dialog.MainDialog(
                        parent=self,
                        filename_template=ft
                    )
                try:
                    update_filename_template_dialog.exec_()
                    if update_filename_template_dialog.result() == accepted:
                        # update the text of the item
                        ft = update_filename_template_dialog.filename_template
                        item.setText(
                            '%s (%s) (%s)' % (ft.name,
                                              ft.target_entity_type,
                                              ft.id)
                        )
    
                    update_filename_template_dialog.deleteLater()
                except Exception as e:
                    QtWidgets.QMessageBox.warning(
                        self,
                        "Error",
                        str(e)
                    )
                    return

    def accept(self):
        """overridden accept method
        """
        if not self.name_lineEdit.is_valid:
            QtWidgets.QMessageBox.critical(
                self,
                'Error',
                'Please fix <b>name</b> field!'
            )
            return
        name = self.name_lineEdit.text()

        custom_template = self.custom_template_plainTextEdit.toPlainText()

        filename_template_items = \
            self.filename_templates_double_list_widget.secondary_items()
        filename_template_ids = []
        for item in filename_template_items:
            filename_template_id = \
                int(item.text().split('(')[-1].split(')')[0])
            filename_template_ids.append(filename_template_id)

        from stalker import FilenameTemplate
        filename_templates = FilenameTemplate.query\
            .filter(FilenameTemplate.id.in_(filename_template_ids)).all()

        from stalker import Structure
        from stalker.db.session import DBSession
        logged_in_user = self.get_logged_in_user()
        if self.mode == 'Create':
            # Create a new Structure
            try:
                structure = Structure(
                    name=name,
                    templates=filename_templates,
                    custom_template=custom_template,
                    created_by=logged_in_user
                )
                self.structure = structure
                DBSession.add(structure)
                DBSession.commit()
            except Exception as e:
                DBSession.rollback()
                QtWidgets.QMessageBox.critical(
                    self,
                    'Error',
                    str(e)
                )
                return

        elif self.mode == 'Update':
            # Update the structure
            try:
                self.structure.name = name
                self.structure.templates = filename_templates
                self.structure.custom_template = custom_template
                self.structure.updated_by = logged_in_user
                DBSession.add(self.structure)
                DBSession.commit()
            except Exception as e:
                DBSession.rollback()
                QtWidgets.QMessageBox.critical(
                    self,
                    'Error',
                    str(e)
                )
                return

        super(MainDialog, self).accept()