예제 #1
0
class ProjectTreeColumn(QDialog):

    def __init__(self, parent=None):
        super(ProjectTreeColumn, self).__init__(parent,
                                                Qt.WindowStaysOnTopHint)
        self._layout = QVBoxLayout()
        self._layout.setSizeConstraint(QVBoxLayout.SetDefaultConstraint)
        self._layout.setContentsMargins(0, 0, 0, 0)
        self._layout.setSpacing(0)
        self.setLayout(self._layout)
        self._vbox = QVBoxLayout()
        self._vbox.setContentsMargins(0, 0, 0, 0)
        self._vbox.setSpacing(0)
        self._vbox.setSizeConstraint(QVBoxLayout.SetDefaultConstraint)
        self._buttons = []

        self._projects_area = QWidget()
        logger.debug("This is the projects area")
        logger.debug(self._projects_area)
        self._projects_area.setLayout(self._vbox)

        self._scroll_area = QScrollArea()
        self.layout().addWidget(self._scroll_area)
        self._scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self._scroll_area.setWidgetResizable(True)
        self._scroll_area.setEnabled(True)
        self._scroll_area.setWidget(self._projects_area)
        self._scroll_area.setGeometry(self.geometry())
        self._vbox.setGeometry(self.geometry())
        self.projects = []
        self._active_project = None

        connections = (
            {'target': 'main_container',
             'signal_name': 'addToProject(QString)',
             'slot': self._add_file_to_project},
            {'target': 'main_container',
             'signal_name': 'showFileInExplorer(QString)',
             'slot': self._show_file_in_explorer},
        )
        IDE.register_service('projects_explorer', self)
        IDE.register_signals('projects_explorer', connections)
        ExplorerContainer.register_tab(translations.TR_TAB_PROJECTS, self)

        #FIXME: Should have a ninja settings object that stores tree state
        #FIXME: Or bettter, application data object
        #TODO: check this:
        #self.connect(ide, SIGNAL("goingDown()"),
            #self.tree_projects.shutdown)

        #def close_project_signal():
            #self.emit(SIGNAL("updateLocator()"))

    def install_tab(self):
        ide = IDE.get_service('ide')
        ui_tools.install_shortcuts(self, actions.PROJECTS_TREE_ACTIONS, ide)

        self.connect(ide, SIGNAL("goingDown()"), self.close)

    def load_session_projects(self, projects):
        for project in projects:
            if os.path.exists(project):
                self._open_project_folder(project)

    def open_project_folder(self, folderName=None):
        if settings.WORKSPACE:
            directory = settings.WORKSPACE
        else:
            directory = os.path.expanduser("~")

        if folderName is None:
            folderName = QFileDialog.getExistingDirectory(
                self, translations.TR_OPEN_PROJECT_DIRECTORY, directory)
            logger.debug("Choosing Foldername")
        if folderName:
            logger.debug("Opening %s" % folderName)
            self._open_project_folder(folderName)

    def _open_project_folder(self, folderName):
        ninjaide = IDE.get_service("ide")
        project = NProject(folderName)
        qfsm = ninjaide.filesystem.open_project(project)
        if qfsm:
            self.add_project(project)
            self.emit(SIGNAL("updateLocator()"))
            self.save_recent_projects(folderName)
            main_container = IDE.get_service('main_container')
            if main_container:
                main_container.show_editor_area()

    def _add_file_to_project(self, path):
        """Add the file for 'path' in the project the user choose here."""
        if self._active_project:
            pathProject = [self._active_project.project]
            addToProject = add_to_project.AddToProject(pathProject, self)
            addToProject.exec_()
            if not addToProject.pathSelected:
                return
            main_container = IDE.get_service('main_container')
            if not main_container:
                return
            editorWidget = main_container.get_current_editor()
            if not editorWidget.file_path:
                name = QInputDialog.getText(None,
                                            translations.TR_ADD_FILE_TO_PROJECT,
                                            translations.TR_FILENAME + ": ")[0]
                if not name:
                    QMessageBox.information(
                        self,
                        translations.TR_INVALID_FILENAME,
                        translations.TR_INVALID_FILENAME_ENTER_A_FILENAME)
                    return
            else:
                name = file_manager.get_basename(editorWidget.file_path)
            new_path = file_manager.create_path(addToProject.pathSelected, name)
            ide_srv = IDE.get_service("ide")
            old_file = ide_srv.get_or_create_nfile(path)
            new_file = old_file.save(editorWidget.get_text(), new_path)
            #FIXME: Make this file replace the original in the open tab
        else:
            pass
            # Message about no project

    def _show_file_in_explorer(self, path):
        '''Iterate through the list of available projects and show
        the current file in the explorer view for the first
        project that contains it (i.e. if the same file is
        included in multiple open projects, the path will be
        expanded for the first project only).
        Note: This slot is connected to the main container's
        "showFileInExplorer(QString)" signal.'''
        for project in self.projects:
            index = project.model().index(path)
            if index.isValid():
                # Show the explorer if it is currently hidden
                central = IDE.get_service('central_container')
                if central and not central.is_lateral_panel_visible():
                    central.change_lateral_visibility()
                # This highlights the index in the tree for us
                project.setCurrentIndex(index)
                # Loop through the parents to expand the tree
                # all the way up to the selected index.
                while index.isValid():
                    project.expand(index)
                    index = index.parent()
                break

    @property
    def children(self):
        return self._projects_area.layout().count()

    def add_project(self, project):
        if project not in self.projects:
            ptree = TreeProjectsWidget(project)
            ptree.setParent(self)
            self.connect(ptree, SIGNAL("setActiveProject(PyQt_PyObject)"),
                         self._set_active_project)
            self.connect(ptree, SIGNAL("closeProject(PyQt_PyObject)"),
                         self._close_project)
            pmodel = project.model
            ptree.setModel(pmodel)
            ptree.header().title = project.name
            ptree.header().path = project.path
            pindex = pmodel.index(pmodel.rootPath())
            ptree.setRootIndex(pindex)
            #self._widget.layout().addWidget(scrollable_wrapper(ptree))
            self._projects_area.layout().addWidget(ptree)
            if self._active_project is None:
                ptree.set_default_project()
            self.projects.append(ptree)
            ptree.setGeometry(self.geometry())

    def _close_project(self, widget):
        """Close the project related to the tree widget."""
        self.projects.remove(widget)
        if self._active_project == widget and len(self.projects) > 0:
            self.projects[0].set_default_project()
        self._layout.removeWidget(widget)
        ninjaide = IDE.get_service('ide')
        ninjaide.filesystem.close_project(widget.project.path)
        widget.deleteLater()

    def _set_active_project(self, tree_proj):
        if self._active_project is not None:
            self._active_project.set_default_project(False)
        self._active_project = tree_proj

    def close_opened_projects(self):
        for project in reversed(self.projects):
            self._close_project(project)

    def save_project(self):
        """Save all the opened files that belongs to the actual project."""
        if self._active_project:
            path = self._active_project.project.path
            main_container = IDE.get_service('main_container')
            if path and main_container:
                main_container.save_project(path)

    def create_new_project(self):
        wizard = new_project_manager.NewProjectManager(self)
        wizard.show()

    @property
    def current_project(self):
        if self._active_project:
            return self._active_project.project

    @property
    def current_tree(self):
        return self._active_project

    def save_recent_projects(self, folder):
        settings = IDE.data_settings()
        recent_project_list = settings.value('recentProjects', {})
        #if already exist on the list update the date time
        projectProperties = json_manager.read_ninja_project(folder)
        name = projectProperties.get('name', '')
        description = projectProperties.get('description', '')

        if name == '':
            name = file_manager.get_basename(folder)

        if description == '':
            description = translations.TR_NO_DESCRIPTION

        if folder in recent_project_list:
            properties = recent_project_list[folder]
            properties["lastopen"] = QDateTime.currentDateTime()
            properties["name"] = name
            properties["description"] = description
            recent_project_list[folder] = properties
        else:
            recent_project_list[folder] = {
                "name": name,
                "description": description,
                "isFavorite": False, "lastopen": QDateTime.currentDateTime()}
            #if the length of the project list it's high that 10 then delete
            #the most old
            #TODO: add the length of available projects to setting
            if len(recent_project_list) > 10:
                del recent_project_list[self.find_most_old_open(
                    recent_project_list)]
        settings.setValue('recentProjects', recent_project_list)

    def find_most_old_open(self, recent_project_list):
        listFounder = []
        for recent_project_path, content in list(recent_project_list.items()):
            listFounder.append((recent_project_path, int(
                content["lastopen"].toString("yyyyMMddHHmmzzz"))))
        listFounder = sorted(listFounder, key=lambda date: listFounder[1],
                             reverse=True)   # sort by date last used
        return listFounder[0][0]

    def reject(self):
        if self.parent() is None:
            self.emit(SIGNAL("dockWidget(PyQt_PyObject)"), self)

    def closeEvent(self, event):
        self.emit(SIGNAL("dockWidget(PyQt_PyObject)"), self)
        event.ignore()
class ProjectTreeColumn(QWidget):

    def __init__(self, *args, **kwargs):
        super(ProjectTreeColumn, self).__init__(*args, **kwargs)
        #self._widget = QWidget()
        self._layout = QVBoxLayout()
        self._layout.setSizeConstraint(QVBoxLayout.SetDefaultConstraint)
        self._layout.setContentsMargins(0, 0, 0, 0)
        self._layout.setSpacing(0)
        self.setLayout(self._layout)
        self._vbox = QVBoxLayout()
        self._vbox.setContentsMargins(0, 0, 0, 0)
        self._vbox.setSpacing(0)
        self._vbox.setSizeConstraint(QVBoxLayout.SetDefaultConstraint)
        self._buttons = []
        
        self._projects_area = QWidget()
        logger.debug("This is the projects area")
        logger.debug(self._projects_area)
        self._projects_area.setLayout(self._vbox)
        
        self._scroll_area = QScrollArea()
        self.layout().addWidget(self._scroll_area)
        self._scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self._scroll_area.setWidgetResizable(True)
        self._scroll_area.setEnabled(True)
        self._scroll_area.setWidget(self._projects_area)
        self._scroll_area.setGeometry(self.geometry())
        #self._projects_area.setGeometry(self.geometry())
        self._vbox.setGeometry(self.geometry())
        self.projects = []
        self._active_project = None
        #for each_test in range(50):
        #    button = QPushButton('Test%d' % each_test)
        #    self._buttons.append(button)
        #    self._projects_area.layout().addWidget(button)

        connections = (
            {'target': 'main_container',
            'signal_name': 'addToProject(QString)',
            'slot': self._add_file_to_project},
        )
        IDE.register_service('projects_explorer', self)
        IDE.register_signals('projects_explorer', connections)
        ExplorerContainer.register_tab(translations.TR_TAB_PROJECTS, self)

        #TODO: check this:
        #self.connect(ide, SIGNAL("goingDown()"),
            #self.tree_projects.shutdown)
        #self.connect(self.tree_projects,
            #SIGNAL("addProjectToConsole(QString)"),
            #self._add_project_to_console)
        #self.connect(self.tree_projects,
            #SIGNAL("removeProjectFromConsole(QString)"),
            #self._remove_project_from_console)

        #def close_project_signal():
            #self.emit(SIGNAL("updateLocator()"))

        #def close_files_related_to_closed_project(project):
            #if project:
                #self.emit(SIGNAL("projectClosed(QString)"), project)
        #self.connect(self.tree_projects, SIGNAL("closeProject(QString)"),
            #close_project_signal)
        #self.connect(self.tree_projects, SIGNAL("refreshProject()"),
            #close_project_signal)
        #self.connect(self.tree_projects,
            #SIGNAL("closeFilesFromProjectClosed(QString)"),
            #close_files_related_to_closed_project)


    def install_tab(self):
        ide = IDE.get_service('ide')
        ui_tools.install_shortcuts(self, actions.PROJECTS_TREE_ACTIONS, ide)

    def open_project_folder(self):
        if settings.WORKSPACE:
            directory = settings.WORKSPACE
        else:
            directory = os.path.expanduser("~")

        folderName = QFileDialog.getExistingDirectory(self,
                self.tr("Open Project Directory"), directory)
        logger.debug("Choosing Foldername")
        if folderName:
            logger.debug("Opening %s" % folderName)
            ninjaide = IDE.get_service("ide")
            project = NProject(folderName)
            qfsm = ninjaide.filesystem.open_project(project)
            if qfsm:
                self.add_project(project)

    def _add_file_to_project(self, path):
        """Add the file for 'path' in the project the user choose here."""
        if self._active_project:
            pathProject = [self._active_project.project]
            addToProject = add_to_project.AddToProject(pathProject, self)
            addToProject.exec_()
            if not addToProject.pathSelected:
                return
            main_container = IDE.get_service('main_container')
            if not main_container:
                return
            editorWidget = main_container.get_current_editor()
            if not editorWidget.file_path:
                name = QInputDialog.getText(None,
                    self.tr("Add File To Project"), self.tr("File Name:"))[0]
                if not name:
                    QMessageBox.information(self, self.tr("Invalid Name"),
                        self.tr("The file name is empty, please enter a name"))
                    return
            else:
                name = file_manager.get_basename(editorWidget.file_path)
            path = file_manager.create_path(addToProject.pathSelected, name)
            try:
                #FIXME
                path = file_manager.store_file_content(
                    path, editorWidget.get_text(), newFile=True)
                editorWidget.nfile = path
                self.emit(SIGNAL("changeWindowTitle(QString)"), path)
                name = file_manager.get_basename(path)
                main_container.actualTab.setTabText(
                    main_container.actualTab.currentIndex(), name)
                editorWidget._file_saved()
            except file_manager.NinjaFileExistsException as ex:
                QMessageBox.information(self, self.tr("File Already Exists"),
                    (self.tr("Invalid Path: the file '%s' already exists.") %
                        ex.filename))
        else:
            pass
            # Message about no project

    def add_project(self, project):
        if project not in self.projects:
            ptree = TreeProjectsWidget(project)
            self.connect(ptree, SIGNAL("setActiveProject(PyQt_PyObject)"),
                self._set_active_project)
            self.connect(ptree, SIGNAL("closeProject(PyQt_PyObject)"),
                self._close_project)
            pmodel = project.model
            ptree.setModel(pmodel)
            ptree.header().title = project.name
            pindex = pmodel.index(pmodel.rootPath())
            ptree.setRootIndex(pindex)
            #self._widget.layout().addWidget(scrollable_wrapper(ptree))
            self._projects_area.layout().addWidget(ptree)
            if self._active_project is None:
                ptree.set_default_project()
            self.projects.append(ptree)
            ptree.setGeometry(self.geometry())

    def _close_project(self, widget):
        """Close the project related to the tree widget."""
        self.projects.remove(widget)
        if self._active_project == widget and len(self.projects) > 0:
            self.projects[0].set_default_project()
        self._widget.layout().removeWidget(widget)
        widget.deleteLater()

    def _set_active_project(self, tree_proj):
        if self._active_project is not None:
            self._active_project.set_default_project(False)
        self._active_project = tree_proj

    def close_opened_projects(self):
        for project in reversed(self.projects):
            self._close_project(project)

    def save_project(self):
        """Save all the opened files that belongs to the actual project."""
        path = self._active_project.project.path
        main_container = IDE.get_service('main_container')
        if path and main_container:
            main_container.save_project(path)

    def create_new_project(self):
        if not self.tree_projects:
            QMessageBox.information(self, self.tr("Projects Disabled"),
                self.tr("Project support has been disabled from Preferences"))
            return
        wizard = wizard_new_project.WizardNewProject(self)
        wizard.show()
예제 #3
0
class TrainDialog(QDialog):
    def __init__(self, parent=None):
        super(TrainDialog, self).__init__(parent)
        self.setupUi()

    # 选择图片
    def select_image(self):
        image_types = [
            '*.jpg', '*.bmp', '*.png', '*.pgm', '*.ppm', '*.tiff', '*.tif',
            '*.gif'
        ]
        format_str = ' '.join(image_types)
        fname = QFileDialog.getOpenFileName(
            self, "select training source", '.',
            "Media files (%s)" % (format_str, ))
        if fname is None or fname == '':
            return
        self.image_path = os.path.abspath(fname)
        self._image_label.setText(os.path.basename(self.image_path))

    # 选择目录
    def select_directory(self):
        directory = QFileDialog.getExistingDirectory(self)
        if directory is None:
            return
        self._collect_label.setText(directory)

    # 生成数据
    def generate(self):
        # 训练源
        search_name = self._image_label.text()
        if search_name is None or search_name == '':
            QtGui.QMessageBox.warning(self, "Warning", '训练源不能为空',
                                      QtGui.QMessageBox.Ok)
            return
        # 生成目录
        search_dir = self._collect_label.text()
        if search_dir is None or search_dir == '':
            QtGui.QMessageBox.warning(self, "Warning", '采图夹不能为空',
                                      QtGui.QMessageBox.Ok)
            return
        # 训练集所占比例
        split_ratio = self._spin_box.value() / 100
        # 是否打乱
        do_shuffle = self._shuffle.isChecked()
        # 是否乘255/max
        multiply_flag = self._multiply.isChecked()
        # 允许全零的图
        enable_all_zero = self._enable_zero.isChecked()
        save_dir = QFileDialog.getExistingDirectory(self)
        if save_dir == '':
            return
        class2label = {}
        only_top = self._onlyTop.isChecked()
        for k, v in self.class_text.items():
            if v == 0:
                continue
            class2label[k] = v.value()
        ex.generate_sample(search_dir,
                           search_name,
                           save_dir,
                           class2label,
                           self.class2item,
                           split_ratio=split_ratio,
                           do_shuffle=do_shuffle,
                           multiply_flag=multiply_flag,
                           enable_all_zero=enable_all_zero,
                           only_top=only_top)

    def get_init_data(self, temp):
        self.init_dict = temp

    def get_init(self):
        temp = InitSelectDialog(self.get_init_data, self)
        temp.exec_()

    # 批量修改
    def modify(self):
        temp = MultiSelectDialog(self.label_list, self.setGray, self)
        temp.exec_()

    def setGray(self, temp_gray):
        for k, v in temp_gray.items():
            self.class_text[k].setValue(v)

    def setupUi(self):
        self.get_init()

        self.setWindowTitle('训练数据生成')
        self._train_layout = QVBoxLayout()
        self.setLayout(self._train_layout)

        # 训练源
        self._image_layout = QtGui.QHBoxLayout()
        self._image_layout.addWidget(QtGui.QLabel('训练源:'))
        self._image_label = QLineEdit('test.jpg')
        self._image_layout.addWidget(self._image_label)
        # 采图夹
        self._collect_layout = QtGui.QHBoxLayout()
        self._collect_layout.addWidget(QtGui.QLabel('采图夹:'))
        self._collect_label = QtGui.QLabel('')
        self._collect_btn = QPushButton('...')
        self._collect_btn.clicked.connect(self.select_directory)
        self._collect_layout.addWidget(self._collect_label)
        self._collect_layout.addWidget(self._collect_btn)
        # 训练集占比
        self._spin_box = QtGui.QDoubleSpinBox()
        self._spin_box.setMaximum(100)
        self._spin_box.setValue(80.0)

        self.modify_btn = QPushButton('批量修改灰度值')
        self.modify_btn.clicked.connect(self.modify)

        # 是否随机打乱
        self._shuffle = QtGui.QCheckBox("随机")
        # 等分
        self._multiply = QtGui.QCheckBox("255 / max(class2label.values())")
        # 允许全0的图片
        self._enable_zero = QtGui.QCheckBox('允许全0的图片')
        # 只搜索最外层
        self._onlyTop = QtGui.QCheckBox("只搜索最顶层")
        self._onlyTop.setChecked(True)
        # 选择文件夹
        self._file_button = QPushButton('生成')
        self._file_button.clicked.connect(self.generate)

        # 加入训练源
        self.temp_Widget = QWidget()
        self.temp_Widget.setLayout(self._image_layout)
        self._train_layout.addWidget(self.temp_Widget)
        # 加入采图夹
        self.collect_Widget = QWidget()
        self.collect_Widget.setLayout(self._collect_layout)
        self._train_layout.addWidget(self.collect_Widget)

        self._train_layout.addWidget(self._spin_box)
        self._train_layout.addWidget(self.modify_btn)
        self._train_layout.addWidget(self._shuffle)
        self._train_layout.addWidget(self._multiply)
        self._train_layout.addWidget(self._enable_zero)
        self._train_layout.addWidget(self._onlyTop)
        self._train_layout.addWidget(self._file_button)

        self.label_list = []
        json_conf = Main.get_json()
        # 缺陷对应图形
        self.class2item = {}
        # 缺陷的数值
        self.class_text = {}

        self._parea = QWidget()
        self._classbox = QScrollArea()
        self._classbox_layout = QVBoxLayout()
        self._parea.setLayout(self._classbox_layout)
        self._parea.setGeometry(0, 0, len(json_conf) * 35, 400)
        self._classbox.setWidget(self._parea)
        self._classbox.setGeometry(0, 0, 200, 300)

        self._train_layout.addWidget(self._classbox)

        for i, current_json in enumerate(json_conf):
            temp_class = current_json['attributes']['class']
            self.label_list.append(temp_class)
            self.class2item[temp_class] = current_json['item'].split('.')[-1]
            temp_layout = QtGui.QHBoxLayout()
            temp_layout.addWidget(QtGui.QLabel(temp_class))
            self.class_text[temp_class] = QtGui.QSpinBox()
            self.class_text[temp_class].setMinimum(0)
            self.class_text[temp_class].setMaximum(255)
            if self.init_dict is None:
                self.class_text[temp_class].setValue(i + 1)
            else:
                self.class_text[temp_class].setValue(
                    self.init_dict[temp_class])
            temp_layout.addWidget(self.class_text[temp_class])
            self._classbox_layout.addLayout(temp_layout)
예제 #4
0
class PropertyEditor(QWidget):
    # Signals
    insertionModeStarted = pyqtSignal(str)
    insertionModeEnded = pyqtSignal()
    insertionPropertiesChanged = pyqtSignal(object)
    editPropertiesChanged = pyqtSignal(object)

    def __init__(self, config, parent=None):
        QWidget.__init__(self, parent)
        self._class_config = {}
        self._class_items = {}
        self._class_prototypes = {}
        self._attribute_handlers = {}
        self._handler_factory = AttributeHandlerFactory()

        self._setupGUI()
        self._parea.setGeometry(0, 0, 200, 0)
        # (快捷键,button)
        self.shortcut2button = {}
        # (button,快捷键)
        self.label2shortcut = {}
        # Add label classes from config
        for label in config:
            self.addLabelClass(label)
        self.image_path = None

    def addLabelClassByPath(self, configs_path):
        # 读配置文件
        with open(configs_path, 'r') as f:
            configs = json5.load(f)
        # 写入当前配置文件的路径
        direct = os.path.dirname(sys.argv[0])
        with open(os.path.join(direct, 'sloth.txt'), 'w') as f:
            f.write(configs_path)
        self._parea.setGeometry(0, 0, 200, 0)
        for temp_json in configs:
            self.addLabelClass(temp_json)
            # 注册
            self._register('inserter', temp_json['attributes']['class'],
                           temp_json['inserter'])
            self._register('item', temp_json['attributes']['class'],
                           temp_json['item'])
            # add_txt的下拉框里也要添加
            self.combo_box.addItem(temp_json['attributes']['class'])
            self.items.append(temp_json['attributes']['class'])
            cf.LABELS.append(temp_json)

    def onModelChanged(self, new_model):
        attrs = set([
            k for k, v in self._attribute_handlers.items()
            if v.autoAddEnabled()
        ])
        if len(attrs) > 0:
            start = time.time()
            attr2vals = {}
            for item in new_model.iterator(AnnotationModelItem):
                for attr in attrs:
                    if attr in item:
                        if attr not in attr2vals:
                            attr2vals[attr] = set((item[attr], ))
                        else:
                            attr2vals[attr] |= set((item[attr], ))
            diff = time.time() - start
            LOG.info("Extracted annotation values from model in %.2fs" % diff)
            for attr, vals in attr2vals.items():
                h = self._attribute_handlers[attr]
                for val in vals:
                    h.addValue(val, True)

    # 设置右键菜单所在位置
    def showContextMenu(self, label_class):
        self.label_menu[label_class].exec_(QCursor.pos())

    # 删除所有的item
    def remove_all_item(self):
        for v in self.shortcut2button.values():
            v.setShortcut(QKeySequence())
        self.shortcut2button.clear()
        self.label2shortcut.clear()
        self.label_menu.clear()
        self.label_action.clear()
        self._class_config.clear()
        self.combo_box.clear()
        self.items.clear()
        temp_dict = copy.copy(self._class_buttons)
        for k, v in temp_dict.items():
            self._classbox_layout.removeWidget(v)
            # 下面这句很重要,不然相当于没删
            self._class_buttons[k].deleteLater()
        self._class_buttons.clear()
        cf.LABELS.clear()

        self._parea.setGeometry(0, 0, 200, 60)

    # 删除标签
    def remove_item(self, label_class):
        """
        删除标签
        :param label_class: 删除的标签名字
        """
        try:
            # 删除快捷键
            self.endInsertionMode()
            shortcut = self.label2shortcut[label_class]
            if shortcut in self.shortcut2button and \
                    self.shortcut2button[shortcut] is not None and \
                    self.shortcut2button[shortcut] == self._class_buttons[label_class]:
                self.shortcut2button[shortcut].setShortcut(QKeySequence())
                del self.shortcut2button[shortcut]
            # 删除菜单
            del self.label_menu[label_class]
            # 删除菜单的动作
            del self.label_action[label_class]
            # 删除视图中的按钮
            self._classbox_layout.removeWidget(
                self._class_buttons[label_class])
            self._class_buttons[label_class].deleteLater()
            self._class_buttons[label_class] = None
            del self._class_config[label_class]
            # 写回json
            direct = os.path.dirname(sys.argv[0])
            with open(os.path.join(direct, 'sloth.txt'), 'r') as f:
                label_path = f.read()
            try:
                with open(label_path, 'r') as f:
                    temp = json5.load(f)
                for i, current_json in enumerate(temp):
                    if current_json['attributes']['class'] == label_class:
                        temp.remove(current_json)
                        # 遍历combo box 找到要删的
                        for i in range(len(self.combo_box)):
                            current_label = self.combo_box.itemText(i)
                            if current_label == label_class:
                                print('removed', label_class)
                                self.combo_box.removeItem(i)
                                self.items.pop(i)
                                break
                        self._class_buttons.pop(label_class)
                        break

                with open(label_path, 'w') as f:
                    json5.dump(temp,
                               f,
                               quote_keys=True,
                               trailing_commas=False,
                               indent=4,
                               separators=(',', ': '),
                               sort_keys=True,
                               ensure_ascii=False)
                self._parea.setGeometry(
                    0, 0, 200, max(self._parea.geometry().height() - 40, 60))
            except Exception as e:
                print(e)
        except Exception as e:
            print(e)

    # 添加标签
    def addLabelClass(self, label_config):
        """
        添加标签
        :param label_config: 标签的json
        """
        # Check label configuration
        if 'attributes' not in label_config:
            raise ImproperlyConfigured("Label with no 'attributes' dict found")
        attrs = label_config['attributes']
        if 'class' not in attrs:
            raise ImproperlyConfigured("Labels must have an attribute 'class'")
        label_class = attrs['class']
        if label_class in self._class_config:
            raise ImproperlyConfigured(
                "Label with class '%s' defined more than once" % label_class)

        # Store config
        self._class_config[label_class] = label_config

        # Parse configuration and create handlers and item
        self.parseConfiguration(label_class, label_config)

        # Add label class button
        button_text = label_config['text']
        button = QPushButton(button_text)
        button.setCheckable(True)
        button.setFlat(True)
        button.clicked.connect(bind(self.onClassButtonPressed, label_class))
        self._class_buttons[label_class] = button
        self._parea.setGeometry(0, 0, 200,
                                self._parea.geometry().height() + 40)
        self._classbox_layout.addWidget(button)

        # 添加右键菜单
        self.label_menu[label_class] = QtGui.QMenu(self)
        self.label_action[label_class] = self.label_menu[
            label_class].addAction('删除')
        self.label_action[label_class].triggered.connect(
            bind(self.remove_item, label_class))
        self._class_buttons[label_class].setContextMenuPolicy(
            QtCore.Qt.CustomContextMenu)
        self._class_buttons[label_class].customContextMenuRequested.connect(
            bind(self.showContextMenu, label_class))

        # Add hotkey
        if 'hotkey' in label_config:
            # 快捷键
            hotkey = label_config['hotkey']
            # 快捷键已经存在,那就去掉原来的
            if hotkey in self.shortcut2button and self.shortcut2button[
                    hotkey] is not None:
                self.shortcut2button[hotkey].setShortcut(QKeySequence())
            # 设置快捷键
            button.setShortcut(QKeySequence(hotkey))
            self.shortcut2button[hotkey] = button
            self.label2shortcut[label_class] = hotkey

    def parseConfiguration(self, label_class, label_config):
        attrs = label_config['attributes']

        # Add prototype item for insertion
        self._class_items[label_class] = AnnotationModelItem(
            {'class': label_class})

        # Create attribute handler widgets or update their values
        for attr, vals in attrs.items():
            if attr in self._attribute_handlers:
                self._attribute_handlers[attr].updateValues(vals)
            else:
                handler = self._handler_factory.create(attr, vals)
                if handler is None:
                    self._class_items[label_class][attr] = vals
                else:
                    self._attribute_handlers[attr] = handler

        for attr in attrs:
            if attr in self._attribute_handlers:
                self._class_items[label_class].update(
                    self._attribute_handlers[attr].defaults())

    def getHandler(self, attribute):
        if attribute in self._attribute_handlers:
            return self._attribute_handlers[attribute]
        else:
            return None

    def getLabelClassAttributes(self, label_class):
        return self._class_config[label_class]['attributes'].keys()

    def onClassButtonPressed(self, label_class):
        if self._class_buttons[label_class].isChecked():
            self.startInsertionMode(label_class)
        else:
            self.endInsertionMode()

    def startInsertionMode(self, label_class):
        self.endInsertionMode(False)
        for lc, button in self._class_buttons.items():
            button.setChecked(lc == label_class)
        LOG.debug("Starting insertion mode for %s" % label_class)
        self._label_editor = LabelEditor([self._class_items[label_class]],
                                         self, True)
        # self._layout.insertWidget(1, self._label_editor, 0)
        self.insertionModeStarted.emit(label_class)

    def endInsertionMode(self, uncheck_buttons=True):
        if self._label_editor is not None:
            LOG.debug("Ending insertion/edit mode")
            self._label_editor.hide()
            # self._layout.removeWidget(self._label_editor)
            self._label_editor = None
            if uncheck_buttons:
                self.uncheckAllButtons()
            self.insertionModeEnded.emit()

    def uncheckAllButtons(self):
        for lc, button in self._class_buttons.items():
            button.setChecked(False)

    def markEditButtons(self, label_classes):
        for lc, button in self._class_buttons.items():
            button.setFlat(lc not in label_classes)

    def currentEditorProperties(self):
        if self._label_editor is None:
            return None
        else:
            return self._label_editor.currentProperties()

    def startEditMode(self, model_items):
        # If we're in insertion mode, ignore empty edit requests
        if self._label_editor is not None and self._label_editor.insertionMode() \
                and len(model_items) == 0:
            return

        self.endInsertionMode()
        LOG.debug("Starting edit mode for items: %s" % model_items)
        self._label_editor = LabelEditor(model_items, self)
        self.markEditButtons(self._label_editor.labelClasses())
        self._layout.insertWidget(1, self._label_editor, 0)

    # 添加txt
    def add_txt(self):
        if not Main.isConfig(Main.get_json()):
            QMessageBox.warning(self, "Warning", '当前的配置文件错误或者为空,无法添加txt',
                                QMessageBox.Ok)
        defect = self.combo_box.currentText()
        if defect is None or defect == '':
            return
        dir_path = QFileDialog.getExistingDirectory(self)
        Main.write_txt(dir_path, {defect}, 'defect')

    # 选择图片
    def select_image(self):
        image_types = [
            '*.jpg', '*.bmp', '*.png', '*.pgm', '*.ppm', '*.tiff', '*.tif',
            '*.gif'
        ]
        format_str = ' '.join(image_types)
        fname = QFileDialog.getOpenFileName(
            self, "select training source", '.',
            "Media files (%s)" % (format_str, ))
        if fname is None or fname == '':
            return
        self.image_path = os.path.abspath(fname)
        self._image_label.setText(os.path.basename(self.image_path))

    # 获得图片路径对应的json路径
    def image2json(self, path):
        temp = path.split('.')
        return ''.join(temp[:-1]) + '.json'

    # 获得图片路径转成的训练图片路径
    def image2cpimage(self, path, id, length):
        length = max(length, 5)
        temp = path.split('.')
        return ''.join(temp[:-1]) + str(id).zfill(length) + '.' + temp[-1]

    # 判断是否包含瑕疵
    def contains_defect(self, annotations, defect_type):
        defects = set()
        for annotation in annotations:
            if 'class' in annotation:
                defects.add(annotation['class'])
        return defect_type.issubset(defects)

    # 生成训练数据
    def generate(self):
        if not Main.isConfig(Main.get_json()):
            QMessageBox.warning(self, "Warning", '配置文件错误或者为空,无法生成训练数据',
                                QMessageBox.Ok)
            return
        a = TrainDialog(self)
        a.exec_()

    # 从labeltool中设置搜索按钮
    def setFunction(self, func):
        self._search_btn.clicked.connect(func)

    # 获得关键字
    def get_key_word(self):
        key_word = self._key_word.text()
        if key_word is None or key_word == '':
            key_word = self._key_word.placeholderText()
        return key_word

    # 获得文件类型
    def get_extension(self):
        extension = self._extension.text()
        # 为空则用默认的,否则用输入的
        if extension is None or extension == '':
            extension = self._extension.placeholderText()
        return extension

    # 返回一个含有权限类型的list
    def get_attributes_type(self):
        '''
        'Rect':('sloth.items.RectItem','sloth.items.RectItemInserter'),
        'Point':('sloth.items.PointItem','sloth.items.PointItemInserter'),
        'Polygon':('sloth.items.PolygonItem','sloth.items.PolygonItemInserter')
        '''
        return ['Rect', 'Point', 'Polygon']

    # 写回json
    def rewrite_json(self, temp_json):
        # json所在的txt
        direct = os.path.dirname(sys.argv[0])
        with open(os.path.join(direct, 'sloth.txt'), 'r') as f:
            label_path = f.read()
        try:
            # 读取旧json
            with open(label_path, 'r') as f:
                temp = json5.load(f)
            # 追加我们要写入的json
            temp.append(temp_json)
            # 写入
            with open(label_path, 'w') as f:
                json5.dump(temp,
                           f,
                           quote_keys=True,
                           trailing_commas=False,
                           indent=4,
                           separators=(',', ': '),
                           sort_keys=True,
                           ensure_ascii=False)
        except Exception as e:
            print(e)

    # 添加标签
    def add_attributes(self):
        if not Main.isConfig(Main.get_json()):
            QMessageBox.warning(self, "Warning", '当前配置文件错误或者为空,不能添加标签',
                                QMessageBox.Ok)
            return
        # 转换dict
        type_dict = {
            'Rect': ('sloth.items.RectItem', 'sloth.items.RectItemInserter'),
            'Point':
            ('sloth.items.PointItem', 'sloth.items.PointItemInserter'),
            'Polygon':
            ('sloth.items.PolygonItem', 'sloth.items.PolygonItemInserter')
        }
        # 获取添加的标签信息
        attributes = {'class': self.attributes_LineEdit.text()}
        attributes_item, attributes_inserter = type_dict[
            self.attributes_type.currentText()]
        attributes_hotkey = self.hotkey.text()
        attributes_text = self.text_LineEdit.text()
        global brush2idx
        brush_idx = str(brush2idx[self.brush_combo_box.currentText()])
        temp_json = {
            'attributes': attributes,
            'inserter': attributes_inserter,
            'item': attributes_item,
            'color': ','.join(map(str, self.color_info)),
            'brush': brush_idx,
            'text': attributes_text
        }

        # 快捷键
        if attributes_hotkey is not None and attributes_hotkey != '':
            temp_json['hotkey'] = attributes_hotkey
        print(temp_json)
        try:
            # 加入标签
            self.addLabelClass(temp_json)
            print(self._class_buttons.keys())
            # 注册
            self._register('inserter', temp_json['attributes']['class'],
                           temp_json['inserter'])
            self._register('item', temp_json['attributes']['class'],
                           temp_json['item'])
            # add_txt的下拉框里也要添加
            self.combo_box.addItem(temp_json['attributes']['class'])
            self.items.append(temp_json['attributes']['class'])
            cf.LABELS.append(temp_json)
            # 写回json
            self.rewrite_json(temp_json)
        except Exception as e:
            print(e)

    # 颜色对话框
    def color_dialog(self):
        col = QtGui.QColorDialog.getColor()
        if col.isValid():
            self.color_label.setStyleSheet("QWidget { background-color: %s }" %
                                           col.name())
        self.color_info = col.getRgb()[:-1]

    # 设置控件的隐藏状态
    def component_visible(self, component_name, state):
        if component_name == '添加标签':
            self._group_box_add_label.setVisible(state)
        elif component_name == 'add_txt':
            self._group_box_add_txt.setVisible(state)
        elif component_name == 'add_files':
            self._group_box_add_files.setVisible(state)

    def _setupGUI(self):
        self._class_buttons = {}
        self.label_menu = {}
        self.label_action = {}
        self._label_editor = None

        # Label class buttons
        self._parea = QGroupBox("Labels")
        self._classbox = QScrollArea()
        self._classbox_layout = FloatingLayout()
        self._parea.setLayout(self._classbox_layout)
        self._parea.setGeometry(0, 0, 200, 200)
        self._classbox.setWidget(self._parea)
        self._classbox.setGeometry(0, 0, 100, 100)
        # 添加txt模块
        self.combo_box = QComboBox()
        self._group_box_add_txt = QGroupBox('add_txt', self)
        self._group_box_add_txt_layout = QVBoxLayout()
        self._group_box_add_txt.setLayout(self._group_box_add_txt_layout)
        temp = cf.LABELS
        self.items = []
        # 获取所有的标签
        for i in temp:
            self.items.append(i['attributes']['class'])
        # 假如下拉框
        self.combo_box.addItems(self.items)
        self.add_txt_btn = QPushButton('add txt')
        self.add_txt_btn.clicked.connect(self.add_txt)
        # 加入下拉框和按钮
        self._group_box_add_txt_layout.addWidget(self.combo_box, 0)
        self._group_box_add_txt_layout.addWidget(self.add_txt_btn, 1)

        # 根据关键字搜索图片模块
        self._group_box_add_files = QGroupBox('add files', self)
        # 文件名包含的
        self._key_word = QLineEdit('')
        self._key_word.setPlaceholderText('Second')
        # 文件类型
        self._extension = QLineEdit('')
        self._extension.setPlaceholderText('bmp')
        self._search_btn = QPushButton('search files')
        self._group_box_add_files_layout = QVBoxLayout()
        # 加入控件
        self._group_box_add_files_layout.addWidget(self._key_word, 0)
        self._group_box_add_files_layout.addWidget(self._extension, 1)
        self._group_box_add_files_layout.addWidget(self._search_btn, 2)
        self._group_box_add_files.setLayout(self._group_box_add_files_layout)

        # 添加标签模块
        self._group_box_add_label = QGroupBox("添加标签", self)
        self._add_label_group_layout = QVBoxLayout()
        self._group_box_add_label.setLayout(self._add_label_group_layout)
        # 标签的class
        self.attributes_LineEdit = QLineEdit('')
        self.attributes_LineEdit.setPlaceholderText('attributes')
        # 标签画出来的类型
        self.attributes_type = QComboBox()
        self.attributes_type.addItems(self.get_attributes_type())
        # 快捷键,目前设置了只允许一个键
        self.hotkey = QLineEdit('')
        self.hotkey.setPlaceholderText('hotkey')
        self.regx = QRegExp("[a-z0-9]$")
        self.validator = QRegExpValidator(self.regx, self.hotkey)
        self.hotkey.setValidator(self.validator)
        # 标签显示
        self.text_LineEdit = QLineEdit('')
        self.text_LineEdit.setPlaceholderText('text')
        # 颜色
        color = QtGui.QColor(0, 0, 0)
        self.color_label = QtGui.QWidget()
        self.color_label.setStyleSheet("QWidget { background-color: %s }" %
                                       color.name())
        self.color_info = [0, 0, 0]
        self.color_layout = QtGui.QHBoxLayout()
        self.color_btn = QPushButton('选择颜色')
        self.color_btn.clicked.connect(self.color_dialog)
        self.color_layout.addWidget(self.color_label)
        self.color_layout.addWidget(self.color_btn)
        # 笔刷
        global brush2idx
        self.brush_combo_box = QComboBox()
        self.brush_combo_box.addItems(list(brush2idx.keys()))
        # 按钮
        self.attributes_add_btn = QPushButton('添加标签')
        self.attributes_add_btn.clicked.connect(self.add_attributes)
        # 加入控件
        self._add_label_group_layout.addWidget(self.attributes_LineEdit, 0)
        self._add_label_group_layout.addWidget(self.attributes_type, 1)
        self._add_label_group_layout.addWidget(self.hotkey, 2)
        self._add_label_group_layout.addWidget(self.text_LineEdit, 3)
        self._label_widget = QWidget()
        self._label_widget.setLayout(self.color_layout)
        self._add_label_group_layout.addWidget(self._label_widget, 4)
        self._add_label_group_layout.addWidget(self.brush_combo_box, 5)
        self._add_label_group_layout.addWidget(self.attributes_add_btn, 6)

        # 生成训练数据按钮
        self._file_button = QPushButton('生成训练数据')
        self._file_button.clicked.connect(self.generate)

        # Global widget
        self._layout = MyVBoxLayout()
        self.setLayout(self._layout)
        self._layout.addWidget(self._classbox, 1)
        self._layout.insertWidget(-1, self._group_box_add_label, 1)
        self._layout.insertWidget(-1, self._group_box_add_txt, 1)
        self._layout.insertWidget(-1, self._group_box_add_files, 1)
        self._layout.insertWidget(-1, self._file_button, 1)
예제 #5
0
class InitSelectDialog(QDialog):
    def __init__(self, func, parent=None):
        super(InitSelectDialog, self).__init__(parent)
        # 传递函数
        self.func = func
        self.setupUi()

    # 获得每一个缺陷的值
    def get_state(self):
        """
        获得每一个缺陷的值
        :return: {缺陷->值}
        """
        cnt = 0
        state = {}
        for k, v in self.class_check.items():
            if v.isChecked():
                cnt += 1
                state[k] = cnt
            else:
                state[k] = 0
        return state

    def setupUi(self):
        layout = QVBoxLayout()
        self.setLayout(layout)
        json_conf = Main.get_json()
        label_list = []
        for current_json in json_conf:
            label_list.append(current_json['attributes']['class'])

        self._parea = QWidget()
        self._classbox = QScrollArea()
        self._classbox_layout = QVBoxLayout()
        self._parea.setLayout(self._classbox_layout)
        self._parea.setGeometry(0, 0, len(label_list) * 35, 400)
        self._classbox.setWidget(self._parea)
        self._classbox.setGeometry(0, 0, 200, 300)

        layout.addWidget(self._classbox)

        self.class_check = {}
        for label in label_list:
            self.class_check[label] = QtGui.QCheckBox(label)
            self._classbox_layout.addWidget(self.class_check[label])

        self.ok_btn = QPushButton('确认')
        self.ok_btn.clicked.connect(self.close)
        layout.addWidget(self.ok_btn)

    # 关闭事件
    def closeEvent(self, event):
        reply = QtGui.QMessageBox.question(
            self, "确认", "确定好了吗", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No
            | QtGui.QMessageBox.Cancel)
        if QtGui.QMessageBox.Yes == reply:
            self.func(self.get_state())
        elif QtGui.QMessageBox.No == reply:
            self.func(None)
        else:
            event.ignore()
예제 #6
0
class MultiSelectDialog(QDialog):
    def __init__(self, label_list, setGray, parent=None):
        super(MultiSelectDialog, self).__init__(parent)
        self.setGray = setGray
        self.label_list = label_list
        self.setupUi(label_list)

    # 批量修改
    def all_modify(self):
        temp = {}
        # 获取spin的值
        v = self.spin_box.value()
        for label in self.label_list:
            if self.class_check[label].isChecked():
                temp[label] = v
        self.setGray(temp)

    def all_check(self):
        for v in self.class_check.values():
            v.stateChanged.disconnect()
            v.setChecked(self.all_check_box.isChecked())
            v.stateChanged.connect(self.change_all)

    def change_all(self):
        for v in self.class_check.values():
            if not v.isChecked():
                self.all_check_box.stateChanged.disconnect()
                self.all_check_box.setChecked(False)
                self.all_check_box.stateChanged.connect(self.all_check)
                return
        self.all_check_box.setChecked(True)

    def setupUi(self, label_list):
        self.setWindowTitle('批量修改')

        layout = QVBoxLayout()
        self.setLayout(layout)
        temp_layout = QtGui.QHBoxLayout()
        self.spin_box = QtGui.QSpinBox()
        self.spin_box.setMinimum(0)
        self.spin_box.setMaximum(255)
        self.modify_btn = QPushButton('批量修改')
        self.modify_btn.clicked.connect(self.all_modify)
        temp_layout.addWidget(self.spin_box)
        temp_layout.addWidget(self.modify_btn)
        layout.addLayout(temp_layout)
        self.all_check_box = QtGui.QCheckBox('全选')
        self.all_check_box.stateChanged.connect(self.all_check)
        layout.addWidget(self.all_check_box)

        self._parea = QWidget()
        self._classbox = QScrollArea()
        self._classbox_layout = QVBoxLayout()
        self._parea.setLayout(self._classbox_layout)
        self._parea.setGeometry(0, 0, len(label_list) * 50, 300)
        self._classbox.setWidget(self._parea)
        self._classbox.setGeometry(0, 0, 200, 200)

        layout.addWidget(self._classbox)

        self.class_check = {}
        for label in label_list:
            self.class_check[label] = QtGui.QCheckBox(label)
            self._classbox_layout.addWidget(self.class_check[label])
            self.class_check[label].stateChanged.connect(self.change_all)
예제 #7
0
class ProjectTreeColumn(QDialog):
    def __init__(self, parent=None):
        super(ProjectTreeColumn, self).__init__(parent,
                                                Qt.WindowStaysOnTopHint)
        self._layout = QVBoxLayout()
        self._layout.setSizeConstraint(QVBoxLayout.SetDefaultConstraint)
        self._layout.setContentsMargins(0, 0, 0, 0)
        self._layout.setSpacing(0)
        self.setLayout(self._layout)
        self._vbox = QVBoxLayout()
        self._vbox.setContentsMargins(0, 0, 0, 0)
        self._vbox.setSpacing(0)
        self._vbox.setSizeConstraint(QVBoxLayout.SetDefaultConstraint)
        self._buttons = []

        self._projects_area = QWidget()
        logger.debug("This is the projects area")
        logger.debug(self._projects_area)
        self._projects_area.setLayout(self._vbox)

        self._scroll_area = QScrollArea()
        self.layout().addWidget(self._scroll_area)
        self._scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self._scroll_area.setWidgetResizable(True)
        self._scroll_area.setEnabled(True)
        self._scroll_area.setWidget(self._projects_area)
        self._scroll_area.setGeometry(self.geometry())
        self._vbox.setGeometry(self.geometry())
        self.projects = []
        self._active_project = None

        connections = ({
            'target': 'main_container',
            'signal_name': 'addToProject(QString)',
            'slot': self._add_file_to_project
        }, )
        IDE.register_service('projects_explorer', self)
        IDE.register_signals('projects_explorer', connections)
        ExplorerContainer.register_tab(translations.TR_TAB_PROJECTS, self)

        #FIXME: Should have a ninja settings object that stores tree state
        #FIXME: Or bettter, application data object
        #TODO: check this:
        #self.connect(ide, SIGNAL("goingDown()"),
        #self.tree_projects.shutdown)

        #def close_project_signal():
        #self.emit(SIGNAL("updateLocator()"))

    def install_tab(self):
        ide = IDE.get_service('ide')
        ui_tools.install_shortcuts(self, actions.PROJECTS_TREE_ACTIONS, ide)

        self.connect(ide, SIGNAL("goingDown()"), self.close)

    def load_session_projects(self, projects):
        for project in projects:
            self._open_project_folder(project)

    def open_project_folder(self, folderName=None):
        if settings.WORKSPACE:
            directory = settings.WORKSPACE
        else:
            directory = os.path.expanduser("~")

        if folderName is None:
            folderName = QFileDialog.getExistingDirectory(
                self, self.tr("Open Project Directory"), directory)
            logger.debug("Choosing Foldername")
        if folderName:
            logger.debug("Opening %s" % folderName)
            self._open_project_folder(folderName)

    def _open_project_folder(self, folderName):
        ninjaide = IDE.get_service("ide")
        project = NProject(folderName)
        qfsm = ninjaide.filesystem.open_project(project)
        if qfsm:
            self.add_project(project)
            self.emit(SIGNAL("updateLocator()"))
            self.save_recent_projects(folderName)
            main_container = IDE.get_service('main_container')
            if main_container:
                main_container.show_editor_area()

    def _add_file_to_project(self, path):
        """Add the file for 'path' in the project the user choose here."""
        if self._active_project:
            pathProject = [self._active_project.project]
            addToProject = add_to_project.AddToProject(pathProject, self)
            addToProject.exec_()
            if not addToProject.pathSelected:
                return
            main_container = IDE.get_service('main_container')
            if not main_container:
                return
            editorWidget = main_container.get_current_editor()
            if not editorWidget.file_path:
                name = QInputDialog.getText(None,
                                            self.tr("Add File To Project"),
                                            self.tr("File Name:"))[0]
                if not name:
                    QMessageBox.information(
                        self, self.tr("Invalid Name"),
                        self.tr("The file name is empty, please enter a name"))
                    return
            else:
                name = file_manager.get_basename(editorWidget.file_path)
            new_path = file_manager.create_path(addToProject.pathSelected,
                                                name)
            ide_srv = IDE.get_service("ide")
            old_file = ide_srv.get_or_create_nfile(path)
            new_file = old_file.save(editorWidget.get_text(), new_path)
            #FIXME: Make this file replace the original in the open tab
        else:
            pass
            # Message about no project

    @property
    def children(self):
        return self._projects_area.layout().count()

    def add_project(self, project):
        if project not in self.projects:
            ptree = TreeProjectsWidget(project)
            ptree.setParent(self)
            self.connect(ptree, SIGNAL("setActiveProject(PyQt_PyObject)"),
                         self._set_active_project)
            self.connect(ptree, SIGNAL("closeProject(PyQt_PyObject)"),
                         self._close_project)
            pmodel = project.model
            ptree.setModel(pmodel)
            ptree.header().title = project.name
            ptree.header().path = project.path
            pindex = pmodel.index(pmodel.rootPath())
            ptree.setRootIndex(pindex)
            #self._widget.layout().addWidget(scrollable_wrapper(ptree))
            self._projects_area.layout().addWidget(ptree)
            if self._active_project is None:
                ptree.set_default_project()
            self.projects.append(ptree)
            ptree.setGeometry(self.geometry())

    def _close_project(self, widget):
        """Close the project related to the tree widget."""
        self.projects.remove(widget)
        if self._active_project == widget and len(self.projects) > 0:
            self.projects[0].set_default_project()
        self._layout.removeWidget(widget)
        ninjaide = IDE.get_service('ide')
        ninjaide.filesystem.close_project(widget.project.path)
        widget.deleteLater()

    def _set_active_project(self, tree_proj):
        if self._active_project is not None:
            self._active_project.set_default_project(False)
        self._active_project = tree_proj

    def close_opened_projects(self):
        for project in reversed(self.projects):
            self._close_project(project)

    def save_project(self):
        """Save all the opened files that belongs to the actual project."""
        if self._active_project:
            path = self._active_project.project.path
            main_container = IDE.get_service('main_container')
            if path and main_container:
                main_container.save_project(path)

    def create_new_project(self):
        wizard = wizard_new_project.WizardNewProject(self)
        wizard.show()

    @property
    def current_project(self):
        if self._active_project:
            return self._active_project.project

    @property
    def current_tree(self):
        return self._active_project

    def save_recent_projects(self, folder):
        settings = IDE.data_settings()
        recent_project_list = settings.value('recentProjects', {})
        #if already exist on the list update the date time
        projectProperties = json_manager.read_ninja_project(folder)
        name = projectProperties.get('name', '')
        description = projectProperties.get('description', '')

        if name == '':
            name = file_manager.get_basename(folder)

        if description == '':
            description = self.tr('no description available')

        if folder in recent_project_list:
            properties = recent_project_list[folder]
            properties["lastopen"] = QDateTime.currentDateTime()
            properties["name"] = name
            properties["description"] = description
            recent_project_list[folder] = properties
        else:
            recent_project_list[folder] = {
                "name": name,
                "description": description,
                "isFavorite": False,
                "lastopen": QDateTime.currentDateTime()
            }
            #if the length of the project list it's high that 10 then delete
            #the most old
            #TODO: add the length of available projects to setting
            if len(recent_project_list) > 10:
                del recent_project_list[self.find_most_old_open(
                    recent_project_list)]
        settings.setValue('recentProjects', recent_project_list)

    def find_most_old_open(self, recent_project_list):
        listFounder = []
        for recent_project_path, content in list(recent_project_list.items()):
            listFounder.append(
                (recent_project_path,
                 int(content["lastopen"].toString("yyyyMMddHHmmzzz"))))
        listFounder = sorted(listFounder,
                             key=lambda date: listFounder[1],
                             reverse=True)  # sort by date last used
        return listFounder[0][0]

    def closeEvent(self, event):
        self.emit(SIGNAL("dockWidget(PyQt_PyObject)"), self)
        event.ignore()
예제 #8
0
class ProjectTreeColumn(QWidget):
    def __init__(self, *args, **kwargs):
        super(ProjectTreeColumn, self).__init__(*args, **kwargs)
        self._layout = QVBoxLayout()
        self._layout.setSizeConstraint(QVBoxLayout.SetDefaultConstraint)
        self._layout.setContentsMargins(0, 0, 0, 0)
        self._layout.setSpacing(0)
        self.setLayout(self._layout)
        self._vbox = QVBoxLayout()
        self._vbox.setContentsMargins(0, 0, 0, 0)
        self._vbox.setSpacing(0)
        self._vbox.setSizeConstraint(QVBoxLayout.SetDefaultConstraint)
        self._buttons = []

        self._projects_area = QWidget()
        logger.debug("This is the projects area")
        logger.debug(self._projects_area)
        self._projects_area.setLayout(self._vbox)

        self._scroll_area = QScrollArea()
        self.layout().addWidget(self._scroll_area)
        self._scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self._scroll_area.setWidgetResizable(True)
        self._scroll_area.setEnabled(True)
        self._scroll_area.setWidget(self._projects_area)
        self._scroll_area.setGeometry(self.geometry())
        self._vbox.setGeometry(self.geometry())
        self.projects = []
        self._active_project = None

        connections = ({
            'target': 'main_container',
            'signal_name': 'addToProject(QString)',
            'slot': self._add_file_to_project
        }, )
        IDE.register_service('projects_explorer', self)
        IDE.register_signals('projects_explorer', connections)
        ExplorerContainer.register_tab(translations.TR_TAB_PROJECTS, self)

        #FIXME: Should have a ninja settings object that stores tree state
        #FIXME: Or bettter, application data object
        #TODO: check this:
        #self.connect(ide, SIGNAL("goingDown()"),
        #self.tree_projects.shutdown)
        #self.connect(self.tree_projects,
        #SIGNAL("addProjectToConsole(QString)"),
        #self._add_project_to_console)
        #self.connect(self.tree_projects,
        #SIGNAL("removeProjectFromConsole(QString)"),
        #self._remove_project_from_console)

        #def close_project_signal():
        #self.emit(SIGNAL("updateLocator()"))

        #def close_files_related_to_closed_project(project):
        #if project:
        #self.emit(SIGNAL("projectClosed(QString)"), project)
        #self.connect(self.tree_projects, SIGNAL("closeProject(QString)"),
        #close_project_signal)
        #self.connect(self.tree_projects, SIGNAL("refreshProject()"),
        #close_project_signal)
        #self.connect(self.tree_projects,
        #SIGNAL("closeFilesFromProjectClosed(QString)"),
        #close_files_related_to_closed_project)

    def install_tab(self):
        ide = IDE.get_service('ide')
        ui_tools.install_shortcuts(self, actions.PROJECTS_TREE_ACTIONS, ide)

    def load_session_projects(self, projects):
        for project in projects:
            self._open_project_folder(project)

    def open_project_folder(self):
        if settings.WORKSPACE:
            directory = settings.WORKSPACE
        else:
            directory = os.path.expanduser("~")

        folderName = QFileDialog.getExistingDirectory(
            self, self.tr("Open Project Directory"), directory)
        logger.debug("Choosing Foldername")
        if folderName:
            logger.debug("Opening %s" % folderName)
            self._open_project_folder(folderName)

    def _open_project_folder(self, folderName):
        ninjaide = IDE.get_service("ide")
        project = NProject(folderName)
        qfsm = ninjaide.filesystem.open_project(project)
        if qfsm:
            self.add_project(project)
            self.emit(SIGNAL("updateLocator()"))

    def _add_file_to_project(self, path):
        """Add the file for 'path' in the project the user choose here."""
        if self._active_project:
            pathProject = [self._active_project.project]
            addToProject = add_to_project.AddToProject(pathProject, self)
            addToProject.exec_()
            if not addToProject.pathSelected:
                return
            main_container = IDE.get_service('main_container')
            if not main_container:
                return
            editorWidget = main_container.get_current_editor()
            if not editorWidget.file_path:
                name = QInputDialog.getText(None,
                                            self.tr("Add File To Project"),
                                            self.tr("File Name:"))[0]
                if not name:
                    QMessageBox.information(
                        self, self.tr("Invalid Name"),
                        self.tr("The file name is empty, please enter a name"))
                    return
            else:
                name = file_manager.get_basename(editorWidget.file_path)
            new_path = file_manager.create_path(addToProject.pathSelected,
                                                name)
            ide_srv = IDE.get_service("ide")
            old_file, ide_srv.get_or_create_nfile(path)
            new_file = old_file.save(editorWidget.get_text(), path)
            #FIXME: Make this file replace the original in the open tab
        else:
            pass
            # Message about no project

    def add_project(self, project):
        if project not in self.projects:
            ptree = TreeProjectsWidget(project)
            self.connect(ptree, SIGNAL("setActiveProject(PyQt_PyObject)"),
                         self._set_active_project)
            self.connect(ptree, SIGNAL("closeProject(PyQt_PyObject)"),
                         self._close_project)
            pmodel = project.model
            ptree.setModel(pmodel)
            ptree.header().title = project.name
            ptree.header().path = project.path
            pindex = pmodel.index(pmodel.rootPath())
            ptree.setRootIndex(pindex)
            #self._widget.layout().addWidget(scrollable_wrapper(ptree))
            self._projects_area.layout().addWidget(ptree)
            if self._active_project is None:
                ptree.set_default_project()
            self.projects.append(ptree)
            ptree.setGeometry(self.geometry())

    def _close_project(self, widget):
        """Close the project related to the tree widget."""
        self.projects.remove(widget)
        if self._active_project == widget and len(self.projects) > 0:
            self.projects[0].set_default_project()
        self._layout.removeWidget(widget)
        ninjaide = IDE.get_service('ide')
        ninjaide.filesystem.close_project(widget.project.path)
        widget.deleteLater()

    def _set_active_project(self, tree_proj):
        if self._active_project is not None:
            self._active_project.set_default_project(False)
        self._active_project = tree_proj

    def close_opened_projects(self):
        for project in reversed(self.projects):
            self._close_project(project)

    def save_project(self):
        """Save all the opened files that belongs to the actual project."""
        if self._active_project:
            path = self._active_project.project.path
            main_container = IDE.get_service('main_container')
            if path and main_container:
                main_container.save_project(path)

    def create_new_project(self):
        if not self.tree_projects:
            QMessageBox.information(
                self, self.tr("Projects Disabled"),
                self.tr("Project support has been disabled from Preferences"))
            return
        wizard = wizard_new_project.WizardNewProject(self)
        wizard.show()

    @property
    def current_project(self):
        if self._active_project:
            return self._active_project.project

    @property
    def current_tree(self):
        return self._active_project
# coding: utf-8
from PyQt4.QtCore import QRect
from PyQt4.QtGui import (QDialog, QFrame, QLineEdit, QScrollArea, QSizePolicy,
                         QVBoxLayout, QWidget)
from qgis.gui import QgsCollapsibleGroupBoxBasic

new_dialog = QDialog()
new_dialog.resize(200, 100)

scroll_area = QScrollArea(new_dialog)
scroll_area.setFrameShape(QFrame.NoFrame)
scroll_area.setFrameShadow(QFrame.Plain)
scroll_area.setWidgetResizable(True)
scroll_area.setGeometry(QRect(10, 20, 170, 70))

scrollAreaWidgetContents = QWidget()
scrollAreaWidgetContents.setGeometry(QRect(0, 0, 170, 70))
vertical_layout = QVBoxLayout(scrollAreaWidgetContents)

collapsible_group_box_basic = QgsCollapsibleGroupBoxBasic(
    scrollAreaWidgetContents)
collapsible_group_box_basic.setTitle('Collapsible')
sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(
    collapsible_group_box_basic.sizePolicy().hasHeightForWidth())
collapsible_group_box_basic.setSizePolicy(sizePolicy)
collapsible_group_box_basic.setChecked(False)

vbox_layout = QVBoxLayout(collapsible_group_box_basic)
# coding: utf-8
from PyQt4.QtCore import QRect
from PyQt4.QtGui import (QDialog, QFrame, QLineEdit, QScrollArea,
                         QSizePolicy, QVBoxLayout, QWidget)
from qgis.gui import QgsCollapsibleGroupBox

new_dialog = QDialog()
new_dialog.resize(200, 100)

scroll_area = QScrollArea(new_dialog)
scroll_area.setFrameShape(QFrame.NoFrame)
scroll_area.setFrameShadow(QFrame.Plain)
scroll_area.setWidgetResizable(True)
scroll_area.setGeometry(QRect(10, 20, 170, 70))

scrollAreaWidgetContents = QWidget()
scrollAreaWidgetContents.setGeometry(QRect(0, 0, 170, 70))
vertical_layout = QVBoxLayout(scrollAreaWidgetContents)

collapsible_group_box = QgsCollapsibleGroupBox(scrollAreaWidgetContents)
collapsible_group_box.setTitle('Collapsible')
sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(
    collapsible_group_box.sizePolicy().hasHeightForWidth()
)
collapsible_group_box.setSizePolicy(sizePolicy)
collapsible_group_box.setChecked(False)

vbox_layout = QVBoxLayout(collapsible_group_box)
예제 #11
0
    def __init__(self):
        self.techniquesClass = {
            'caesar cipher': caesarCipher,
            'mono alphabetic cipher': monoAlphabeticCipher,
            'vigenere cipher': vigenereCipher,
            'vernan cipher': vernanCipher,
            'one time pad': oneTimePad
        }

        self.rowsview = []
        self.filesview = []
        self.techniques = []

        QDialog.__init__(self)
        self.setWindowTitle("CryptoSystems")
        self.resize(1024, 600)
        self.setMinimumSize(QSize(1024, 600))
        self.setMaximumSize(QSize(1024, 600))

        self.checkBox_2 = QCheckBox(self)
        self.checkBox_2.setGeometry(QRect(620, 10, 130, 20))
        self.checkBox_2.setText('Select All')
        self.checkBox_2.clicked.connect(self.__selectAllFiles)

        self.treeView = QTreeView(self)
        self.treeView.setGeometry(QRect(10, 10, 230, 580))
        self.treeView.setObjectName("treeView")

        self.fileSystemModel = QFileSystemModel(self.treeView)
        self.fileSystemModel.setReadOnly(False)

        self.fileSystemModel.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot)
        root = self.fileSystemModel.setRootPath("/")
        self.treeView.setModel(self.fileSystemModel)
        self.treeView.setRootIndex(root)
        self.treeView.hideColumn(1)
        self.treeView.hideColumn(2)
        self.treeView.hideColumn(3)
        self.treeView.clicked.connect(self.__eventDirectoryChanged)

        self.mygroupbox = QGroupBox(self)
        self.mygroupbox.setGeometry(QRect(0, 0, 1000, 1000))
        self.myform = QFormLayout()
        for j in list(range(100)):
            horizontalLayout = QHBoxLayout()
            self.myform.addRow(horizontalLayout)
            self.rowsview.append(horizontalLayout)

        self.mygroupbox.setLayout(self.myform)
        scroll = QScrollArea(self)
        scroll.setWidget(self.mygroupbox)
        scroll.setWidgetResizable(True)
        scroll.setGeometry(QRect(250, 30, 500, 580))
        scroll.setWidgetResizable(True)

        self.label_4 = QLabel(self)
        self.label_4.setGeometry(QRect(780, 30, 31, 16))
        self.label_4.setPixmap(QPixmap("images/key.png"))
        self.label_4.setScaledContents(True)

        self.lineEdit = QLineEdit(self)
        self.lineEdit.setGeometry(QRect(820, 30, 180, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setPlaceholderText(_fromUtf8('write your password'))
        self.lineEdit.setEchoMode(QLineEdit.Password)

        self.techniquesGroup = QGroupBox(self)
        self.tecniquesform = QFormLayout()
        self.techniquesGroup.setLayout(self.tecniquesform)

        self.techniquesScroll = QScrollArea(self)
        self.techniquesScroll.setGeometry(QRect(770, 100, 230, 300))
        self.techniquesScroll.setWidget(self.techniquesGroup)
        self.techniquesScroll.setWidgetResizable(True)

        self.rowsTechiques = []
        for i in list(range(8)):
            horizontalLayout = QHBoxLayout()
            self.tecniquesform.addRow(horizontalLayout)
            self.rowsTechiques.append(horizontalLayout)

        techniquesCombo = QComboBox()
        techniquesCombo.setGeometry(QRect(10, 50, 171, 22))
        techniquesCombo.addItems(self.techniquesClass.keys())
        self.techniques.append(techniquesCombo)
        self.rowsTechiques[0].addWidget(techniquesCombo)
        self.techniquesNumber = 1

        self.addTechnique = QPushButton()
        self.addTechnique.setGeometry(QRect(90, 90, 31, 21))
        self.addTechnique.setFixedSize(31, 21)
        self.addTechnique.setText('+')
        self.connect(self.addTechnique, SIGNAL("clicked()"),
                     self.__eventAddTechnique)
        self.rowsTechiques[len(self.rowsTechiques) - 1].addWidget(
            self.addTechnique)

        self.okButton = QPushButton(self)
        self.okButton.setGeometry(QRect(920, 560, 80, 20))
        self.okButton.setText('Start...')
        self.connect(self.okButton, SIGNAL("clicked()"),
                     self.__eventInitEncryption)
예제 #12
0
class EncryptDialog(QDialog):
    def __init__(self):
        self.techniquesClass = {
            'caesar cipher': caesarCipher,
            'mono alphabetic cipher': monoAlphabeticCipher,
            'vigenere cipher': vigenereCipher,
            'vernan cipher': vernanCipher,
            'one time pad': oneTimePad
        }

        self.rowsview = []
        self.filesview = []
        self.techniques = []

        QDialog.__init__(self)
        self.setWindowTitle("CryptoSystems")
        self.resize(1024, 600)
        self.setMinimumSize(QSize(1024, 600))
        self.setMaximumSize(QSize(1024, 600))

        self.checkBox_2 = QCheckBox(self)
        self.checkBox_2.setGeometry(QRect(620, 10, 130, 20))
        self.checkBox_2.setText('Select All')
        self.checkBox_2.clicked.connect(self.__selectAllFiles)

        self.treeView = QTreeView(self)
        self.treeView.setGeometry(QRect(10, 10, 230, 580))
        self.treeView.setObjectName("treeView")

        self.fileSystemModel = QFileSystemModel(self.treeView)
        self.fileSystemModel.setReadOnly(False)

        self.fileSystemModel.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot)
        root = self.fileSystemModel.setRootPath("/")
        self.treeView.setModel(self.fileSystemModel)
        self.treeView.setRootIndex(root)
        self.treeView.hideColumn(1)
        self.treeView.hideColumn(2)
        self.treeView.hideColumn(3)
        self.treeView.clicked.connect(self.__eventDirectoryChanged)

        self.mygroupbox = QGroupBox(self)
        self.mygroupbox.setGeometry(QRect(0, 0, 1000, 1000))
        self.myform = QFormLayout()
        for j in list(range(100)):
            horizontalLayout = QHBoxLayout()
            self.myform.addRow(horizontalLayout)
            self.rowsview.append(horizontalLayout)

        self.mygroupbox.setLayout(self.myform)
        scroll = QScrollArea(self)
        scroll.setWidget(self.mygroupbox)
        scroll.setWidgetResizable(True)
        scroll.setGeometry(QRect(250, 30, 500, 580))
        scroll.setWidgetResizable(True)

        self.label_4 = QLabel(self)
        self.label_4.setGeometry(QRect(780, 30, 31, 16))
        self.label_4.setPixmap(QPixmap("images/key.png"))
        self.label_4.setScaledContents(True)

        self.lineEdit = QLineEdit(self)
        self.lineEdit.setGeometry(QRect(820, 30, 180, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setPlaceholderText(_fromUtf8('write your password'))
        self.lineEdit.setEchoMode(QLineEdit.Password)

        self.techniquesGroup = QGroupBox(self)
        self.tecniquesform = QFormLayout()
        self.techniquesGroup.setLayout(self.tecniquesform)

        self.techniquesScroll = QScrollArea(self)
        self.techniquesScroll.setGeometry(QRect(770, 100, 230, 300))
        self.techniquesScroll.setWidget(self.techniquesGroup)
        self.techniquesScroll.setWidgetResizable(True)

        self.rowsTechiques = []
        for i in list(range(8)):
            horizontalLayout = QHBoxLayout()
            self.tecniquesform.addRow(horizontalLayout)
            self.rowsTechiques.append(horizontalLayout)

        techniquesCombo = QComboBox()
        techniquesCombo.setGeometry(QRect(10, 50, 171, 22))
        techniquesCombo.addItems(self.techniquesClass.keys())
        self.techniques.append(techniquesCombo)
        self.rowsTechiques[0].addWidget(techniquesCombo)
        self.techniquesNumber = 1

        self.addTechnique = QPushButton()
        self.addTechnique.setGeometry(QRect(90, 90, 31, 21))
        self.addTechnique.setFixedSize(31, 21)
        self.addTechnique.setText('+')
        self.connect(self.addTechnique, SIGNAL("clicked()"),
                     self.__eventAddTechnique)
        self.rowsTechiques[len(self.rowsTechiques) - 1].addWidget(
            self.addTechnique)

        self.okButton = QPushButton(self)
        self.okButton.setGeometry(QRect(920, 560, 80, 20))
        self.okButton.setText('Start...')
        self.connect(self.okButton, SIGNAL("clicked()"),
                     self.__eventInitEncryption)

    def __eventAddTechnique(self):
        techniquesCombo = QComboBox()
        techniquesCombo.setGeometry(QRect(10, 50, 171, 22))
        techniquesCombo.addItems(self.techniquesClass.keys())
        self.techniques.append(techniquesCombo)
        self.rowsTechiques[self.techniquesNumber].addWidget(techniquesCombo)
        self.techniquesNumber = self.techniquesNumber + 1
        if ((len(self.rowsTechiques) - 1) == self.techniquesNumber):
            self.addTechnique.setEnabled(False)

    def __eventDirectoryChanged(self):
        index = self.treeView.currentIndex()
        self.__changeDirectory(self.fileSystemModel.filePath(index))

    def __changeDirectory(self, path):
        for c in self.filesview:
            c.setParent(None)
            c.deleteLater()
        self.filesview = []
        self.checkBox_2.setChecked(False)

        self.progressBars = {}
        for f in self.__getFiles(path):
            try:
                group = QGroupBox(f, self)
                group.setGeometry(QRect(20, 20, 100, 150))
                group.setCheckable(True)
                group.setChecked(False)
                group.setFixedSize(100, 150)
                group.setFlat(True)
                group.setToolTip(f)

                label = QLabel(group)
                label.setScaledContents(True)
                label.setGeometry(QRect(5, 25, 90, 90))
                label.setToolTip(f)

                progressBar = QProgressBar(group)
                progressBar.setGeometry(QRect(0, 70, 111, 10))
                progressBar.setProperty("value", 0)
                progressBar.setTextVisible(False)
                progressBar.setToolTip('0%')
                progressBar.setVisible(False)
                self.progressBars[f] = progressBar

                self.filesview.append(group)
                from os.path import isfile
                if isfile(path + '/' + f):
                    ext = f.split('.')[-1]
                    if isfile('icons/' + ext.lower() + '.png'):
                        label.setPixmap(
                            QPixmap('icons/' + ext.lower() + '.png'))
                    else:
                        label.setPixmap(QPixmap('icons/default.png'))
                else:
                    label.setPixmap(QPixmap('icons/folder.png'))
                self.connect(group, SIGNAL("clicked()"), self.__deselectFile)
            except ValueError:
                pass

        i = 0
        for x in list(range(len(self.filesview))):
            if (x % 4) == 0:
                i = i + 1
            self.rowsview[i].addWidget(self.filesview[x])

    def __selectAllFiles(self):
        for o in self.filesview:
            o.setChecked(self.checkBox_2.isChecked())

    def __deselectFile(self):
        #print 'deselect'
        self.checkBox_2.setChecked(False)

    def __arrozconpollo(self):
        self.__obtainSelectedFIles()

    def __obtainSelectedFIles(self):
        files = []
        for o in self.filesview:
            if o.isChecked():
                files.append(str(o.title()))
                self.progressBars[str(o.title())].setVisible(True)
        return files

    def __getFiles(self, path):
        from os import listdir
        from os.path import isfile

        f = []

        for base in listdir(path):
            try:
                if isfile(path + '/' + base):
                    f.append(base)
            except ValueError:
                pass
        f.sort()
        return f

    def __eventInitEncryption(self):
        if len(self.__obtainSelectedFIles()) == 0:
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Critical)
            msg.setText("You must specify the files you want to encrypt")
            msg.setWindowTitle("CryptoSystems")
            msg.setStandardButtons(QMessageBox.Ok)
            msg.exec_()
            return

        if str(self.lineEdit.text()).strip() == '':
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Critical)
            msg.setText("You must specify a key")
            msg.setWindowTitle("CryptoSystems")
            msg.setStandardButtons(QMessageBox.Ok)
            msg.exec_()
            return

        self.okButton.setEnabled(False)
        self.techniquesGroup.setEnabled(False)
        self.lineEdit.setEnabled(False)

        index = self.treeView.currentIndex()

        path = self.fileSystemModel.filePath(index)
        selectedFiles = self.__obtainSelectedFIles()

        from os.path import getsize
        blockSize = 4096

        from hashlib import md5
        Hash = md5()
        Hash.update(str(self.lineEdit.text()))
        key = Hash.hexdigest()

        for f in selectedFiles:

            f_in = open(path + '/' + f, 'rb')
            f_out = open(
                path + '/' + reduceMd5(checksum(path + '/' + f)) + '.cry',
                'wb')

            f_out.write('CRYOGENESIS' + unhexlify('00') + 'ARCHIVE' +
                        unhexlify('01'))

            header_list = ''
            techniquesObjects = []
            for t in self.techniques:
                header_list = header_list + t.currentText() + ':'
                techniquesObjects.append(self.techniquesClass[str(
                    t.currentText())](key))

            file_header = str('header|' + str(f_in.name.split('/')[-1]) + '|' +
                              str(header_list) + '|' +
                              str(checksum(path + '/' + f)))

            aes = AES(key)
            f_out.write(aes.encrypt(file_header))
            f_out.write(unhexlify('02'))

            in_size = getsize(path + '/' + f)
            in_progress = 0.0

            block = f_in.read(blockSize)
            while (block):
                block_c = block
                for t in techniquesObjects:
                    block_c = t.encrypt(block_c)
                f_out.write(block_c)
                in_progress = in_progress + blockSize

                progress = (in_progress / in_size) * 100
                #print progress
                self.progressBars[str(f)].setProperty("value", int(progress))
                self.progressBars[str(f)].setToolTip(str(progress) + '%')
                block = f_in.read(blockSize)

            f_in.close()
            f_out.close()

        msg = QMessageBox()

        msg.setIcon(QMessageBox.Information)

        msg.setText("Encryption has successfully concluded")
        msg.setWindowTitle("CryptoSystems")
        msg.setStandardButtons(QMessageBox.Ok)
        msg.exec_()

        self.hide()
예제 #13
0
    def __init__(self):
        self.techniquesClass = {
            'caesar cipher': caesarCipher,
            'mono alphabetic cipher': monoAlphabeticCipher,
            'vigenere cipher': vigenereCipher,
            'vernan cipher': vernanCipher,
            'one time pad': oneTimePad
        }

        self.rowsview = []
        self.filesview = []
        #self.techniques = []

        QDialog.__init__(self)
        self.setWindowTitle("Desencriptador de Cryogenesis Systems.")
        self.resize(1024, 600)
        self.setMinimumSize(QSize(1024, 600))
        self.setMaximumSize(QSize(1024, 600))

        self.checkBox_2 = QCheckBox(self)
        self.checkBox_2.setGeometry(QRect(620, 10, 130, 20))
        self.checkBox_2.setText('seleccionar todos')
        self.checkBox_2.clicked.connect(self.__selectAllFiles)

        self.treeView = QTreeView(self)
        self.treeView.setGeometry(QRect(10, 10, 230, 580))
        self.treeView.setObjectName("treeView")

        self.fileSystemModel = QFileSystemModel(self.treeView)
        self.fileSystemModel.setReadOnly(False)

        self.fileSystemModel.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot)
        root = self.fileSystemModel.setRootPath("/")
        self.treeView.setModel(self.fileSystemModel)
        self.treeView.setRootIndex(root)
        self.treeView.hideColumn(1)
        self.treeView.hideColumn(2)
        self.treeView.hideColumn(3)
        self.treeView.clicked.connect(self.__eventDirectoryChanged)

        self.mygroupbox = QGroupBox(self)
        self.mygroupbox.setGeometry(QRect(0, 0, 1000, 1000))
        self.myform = QFormLayout()
        for j in list(range(100)):
            horizontalLayout = QHBoxLayout()
            self.myform.addRow(horizontalLayout)
            self.rowsview.append(horizontalLayout)

        self.mygroupbox.setLayout(self.myform)
        scroll = QScrollArea(self)
        scroll.setWidget(self.mygroupbox)
        scroll.setWidgetResizable(True)
        scroll.setGeometry(QRect(250, 30, 500, 580))
        scroll.setWidgetResizable(True)

        self.label_4 = QLabel(self)
        self.label_4.setGeometry(QRect(780, 30, 31, 16))
        self.label_4.setPixmap(QPixmap("images/key.png"))
        self.label_4.setScaledContents(True)

        self.lineEdit = QLineEdit(self)
        self.lineEdit.setGeometry(QRect(820, 30, 180, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setPlaceholderText(_fromUtf8('escriba su contraseña'))
        self.lineEdit.setEchoMode(QLineEdit.Password)

        self.okButton = QPushButton(self)
        self.okButton.setGeometry(QRect(920, 560, 80, 20))
        self.okButton.setText('Iniciar...')
        self.connect(self.okButton, SIGNAL("clicked()"),
                     self.__eventInitDecryption)