def __createTableView__(self, pageLayout):
        self.tableViewComposite = CompositeWidget(self, layout=QVBoxLayout())

        datasource_page = self.wizard().page(self.datasource_page_id)
        model = CheckStateProxySortFilterModel(self)
        model.setSourceModel(datasource_page.getDatasourceModel())
        model.setDynamicSortFilter(True)

        self.filesTableView = FilesTableView(self.tableViewComposite,
                                        model=model,
                                        onClickedAction=self.onClickedAction,
                                        wizardButtons=(QWizard.NextButton,),
                                        wizard_handler=self.wizard,
                                        sorting=True)
        self.filesTableView.setColumnHidden(0, True)

        self.filePreviewButton = PushButtonWidget(self.tableViewComposite,
                            i18n="datasource.file.preview.button",
                            i18n_def="File preview",
                            enabled=False,
                            clicked_handler=self.filePreviewAction)

        self.separatorWidget = GlobalSeparatorWidget(self.tableViewComposite,
                                separatorHandler=self.__separatorHandler__,
                                globalHandler=self.__globalSeparatorHandler__,
                                enabled=False)

        self.__createHeaderPreviewGroup__()

        self.__activity__ = ActivityWidget(self)

        self.__timeUnitsGroup__ = self.__createTimeUnitsGroup__()
 def __createTableView__(self, parent):
     self.filesTableView = FilesTableView(
         parent,
         model=QStandardItemModel(self),
         onClickedAction=self.onClickedAction,
         wizardButtons=(QWizard.NextButton, ),
         wizard_handler=self.wizard,
         sorting=True,
         enabled_precheck_handler=self.enabledPrecheckHandler)
 def __createTableView__(self, parent):
     self.filesTableView = FilesTableView(parent,
                     model=QStandardItemModel(self),
                     onClickedAction=self.onClickedAction,
                     wizardButtons=(QWizard.NextButton,),
                     wizard_handler=self.wizard,
                     sorting=True,
                     enabled_precheck_handler=self.enabledPrecheckHandler)
class ChooseColumnsDataPage(QWizardPage):

    def __init__(self, _parent, datasource_page_id):
        QWizardPage.__init__(self, parent=_parent)
        self.datasource_page_id = datasource_page_id
        self.pageLayout = None
        self.headersTablePreview = None

        self.__dataFilesHeaders__ = {}
        self.__signalColumnIndexes__ = {}  # includes selected signal column indexes @IgnorePep8
        self.__annotationColumnIndexes__ = {}  # includes selected annotation column indexes @IgnorePep8
        self.__timeColumnIndexes__ = {}  # includes selected [optional] time column indexes @IgnorePep8

        self.__globalSeparator__ = None
        self.__globalCheckBox__ = None
        self.__globalIndex__ = GlobalIndex(None, None, None)

        self.__headerWidgets__ = []
        self.__widgetsHorizontalHeader__ = None

        self.__signal_header_element__ = HeaderElement('signal', 'signal', self.__signalWidgetHandler__) # @IgnorePep8
        self.__annotation_header_element__ = HeaderElement('annotation', 'annotation', self.__annotationWidgetHandler__) # @IgnorePep8
        self.__time_header_element__ = HeaderElement('time', 'time', self.__timeWidgetHandler__) # @IgnorePep8

    def initializePage(self):
        self.setTitle('Choose column data')
        #self.setSubTitle('Choose column specific data')

        if self.pageLayout == None:
            self.pageLayout = QVBoxLayout()
            self.setLayout(self.pageLayout)
            self.__createTableView__(self.pageLayout)
        #select the first row
        self.filesTableView.selectRow(0)
        self.separatorWidget.setGlobalSeparatorAsDefault()

    def __createTableView__(self, pageLayout):
        self.tableViewComposite = CompositeWidget(self, layout=QVBoxLayout())

        datasource_page = self.wizard().page(self.datasource_page_id)
        model = CheckStateProxySortFilterModel(self)
        model.setSourceModel(datasource_page.getDatasourceModel())
        model.setDynamicSortFilter(True)

        self.filesTableView = FilesTableView(self.tableViewComposite,
                                        model=model,
                                        onClickedAction=self.onClickedAction,
                                        wizardButtons=(QWizard.NextButton,),
                                        wizard_handler=self.wizard,
                                        sorting=True)
        self.filesTableView.setColumnHidden(0, True)

        self.filePreviewButton = PushButtonWidget(self.tableViewComposite,
                            i18n="datasource.file.preview.button",
                            i18n_def="File preview",
                            enabled=False,
                            clicked_handler=self.filePreviewAction)

        self.separatorWidget = GlobalSeparatorWidget(self.tableViewComposite,
                                separatorHandler=self.__separatorHandler__,
                                globalHandler=self.__globalSeparatorHandler__,
                                enabled=False)

        self.__createHeaderPreviewGroup__()

        self.__activity__ = ActivityWidget(self)

        self.__timeUnitsGroup__ = self.__createTimeUnitsGroup__()

    def __createTimeUnitsGroup__(self):
        return TimeUnitsWidget(self.tableViewComposite,
                               i18n_def='Signal time unit')

    def onClickedAction(self, idx):
        self.filePreviewButton.setEnabled(True)
        self.filesTableView.onClickedAction(
                                self.filesTableView.model().mapToSource(idx))
        #global separator could be None
        self.__createFileHeadersPreview__(self.__globalSeparator__)
        if self.__globalSeparator__:
            self.separatorWidget.setSeparator(self.__globalSeparator__)
        else:
            self.separatorWidget.setSeparator(self.__getSelectedSeparator__())
        self.separatorWidget.setEnabled(True)

    def filePreviewAction(self):
        showFilesPreviewDialog(
                        self.filesTableView.getSelectedPathAndFilename())

    def __separatorHandler__(self, _separator):
        self.__createFileHeadersPreview__(_separator)

    def __globalSeparatorHandler__(self, _checked, _separator=None):
        self.__globalSeparator__ = _separator

    def __createFileHeadersPreview__(self, _separator=None):

        dataFileHeader = self.__getDataFileHeader__(_separator)
        if dataFileHeader == None:
            return

        self.__createHeadersTablePreview__()

        colCount = dataFileHeader.getHeadersCount()
        model = self.__createHeadersTablePreviewModel__(colCount)

        self.__widgetsHorizontalHeader__ = WidgetsHorizontalHeader(
                                                    self.headersTablePreview)

        # create header line
        pathFile = self.filesTableView.getSelectedPathAndFilename()
        self.__headerWidgets__ = []
        for headerLine in dataFileHeader.getHeadersLines(1):
            for num, header in enumerate(headerLine):
                widget = HeaderWidget(self.__widgetsHorizontalHeader__,
                                      header,
                                      [self.__signal_header_element__,
                                       self.__annotation_header_element__,
                                       self.__time_header_element__])
                self.__headerWidgets__.append(widget)
                # some value of global indicator have to be not None
                if not nvl(*self.__globalIndex__) == None:
                    widget.enabledAll(False)
                    if self.__globalIndex__.signal == num:  # signal index
                        widget.check(self.__signal_header_element__.name)
                    elif self.__globalIndex__.annotation == num:  # annotation index @IgnorePep8
                        widget.check(self.__annotation_header_element__.name)
                    if self.__globalIndex__.time == num:  # time index
                        widget.check(self.__time_header_element__.name)
                elif self.__signalColumnIndexes__.get(pathFile) == num:
                    widget.check(self.__signal_header_element__.name)
                elif self.__annotationColumnIndexes__.get(pathFile) == num:
                    widget.check(self.__annotation_header_element__.name)
                elif self.__timeColumnIndexes__.get(pathFile) == num:
                    widget.check(self.__time_header_element__.name)
        self.__widgetsHorizontalHeader__.setWidgets(self.__headerWidgets__)

        # create data lines
        for rowData in dataFileHeader.getDataLines():
            modelData = list()
            for idx in range(colCount):
                modelData.append(QStandardItem(rowData[idx]
                                        if colCount <= len(rowData) else ""))
            model.appendRow(modelData)

        self.fileHeaderPreviewGroup.setEnabled(True)
        self.fileHeaderPreviewGroup.show()

    def __getDataFileHeader__(self, _separator=None):
        pathFile = self.filesTableView.getSelectedPathAndFilename()
        if pathFile == None:
            ErrorWindow(message="The file must be selected !")
        else:
            return self.__createDataFileHeader__(pathFile, _separator)

    def __createHeadersTablePreviewModel__(self, colNumber):
        model = PreviewDataViewModel(self.headersTablePreview)
        labels = QStringList(create_list("", colNumber))
        model.setHorizontalHeaderLabels(labels)
        self.headersTablePreview.setModel(model)
        return model

    def __createHeadersTablePreview__(self):

        self.__createHeaderPreviewGroup__()

        self.headersTablePreview = TableViewWidget(self.fileHeaderPreviewGroup,
                            selectionBehavior=QAbstractItemView.SelectRows,
                            selectionMode=QAbstractItemView.SingleSelection)

        self.__globalCheckBox__ = CheckBoxWidget(
                                    self.fileHeaderPreviewGroup,
                                    i18n="global.data.column.index",
                                    i18n_def="Global columns indexes",
                                    clicked_handler=self.__globalClicked__)
        if self.__globalIndex__.signal:
            self.__globalCheckBox__.setChecked(True)

    def __createDataFileHeader__(self, pathFile, _separator):
        dataFileHeader = self.__dataFilesHeaders__.get(pathFile, None)
        if dataFileHeader == None:
            dataFileHeader = DataFileHeader(pathFile)
            self.__dataFilesHeaders__[pathFile] = dataFileHeader

        if _separator == None:
            _separator = dataFileHeader.getSeparator()

        if _separator == None:
            #try to discover a separator based on file data
            _separator = dataFileHeader.getSeparator(generate=True)

        dataFileHeader.setSeparator(_separator)
        return dataFileHeader

    def __getSelectedSeparator__(self):
        pathFile = self.filesTableView.getSelectedPathAndFilename()
        if pathFile:
            dataFileHeader = self.__dataFilesHeaders__.get(pathFile)
            if dataFileHeader:
                return dataFileHeader.getSeparator()

    # for future use
    def __setSeparatorForAll__(self, _separator=None):
        model = self.filesTableView.model()
        for row in range(model.rowCount()):
            #it's necessity to alter model index of QSortFilterProxyModel type
            #into source model index of QStandardItemModel type
            idx = model.mapToSource(model.index(row, 0))
            pathFile = self.filesTableView.getPathAndFilename(idx)
            if pathFile:
                self.__createDataFileHeader__(pathFile, _separator)

    def __signalWidgetHandler__(self, _widget):
        self.__widgetHandler__(_widget, self.__signal_header_element__.name)

    def __annotationWidgetHandler__(self, _widget):
        self.__widgetHandler__(_widget, self.__annotation_header_element__.name)  # @IgnorePep8

    def __timeWidgetHandler__(self, _widget):
        self.__widgetHandler__(_widget, self.__time_header_element__.name)

    def __widgetHandler__(self, _widget, _type):
        checked = _widget.isChecked(_type)
        pathFile = self.filesTableView.getSelectedPathAndFilename()
        if checked:
            for num, widget in enumerate(self.__headerWidgets__):
                if widget == _widget:
                    if _type == self.__signal_header_element__.name:
                        _widget.uncheck(self.__annotation_header_element__.name) # @IgnorePep8
                        self.__signalColumnIndexes__[pathFile] = num
                    elif _type == self.__annotation_header_element__.name:
                        _widget.uncheck(self.__signal_header_element__.name)
                        _widget.uncheck(self.__time_header_element__.name)
                        self.__annotationColumnIndexes__[pathFile] = num
                    elif _type == self.__time_header_element__.name:
                        _widget.uncheck(self.__annotation_header_element__.name) # @IgnorePep8
                        self.__timeColumnIndexes__[pathFile] = num
                else:
                    if _type == self.__signal_header_element__.name:
                        widget.uncheck(self.__signal_header_element__.name)
                    elif _type == self.__annotation_header_element__.name:
                        widget.uncheck(self.__annotation_header_element__.name)
                    elif _type == self.__time_header_element__.name:
                        widget.uncheck(self.__time_header_element__.name)

            self.__globalCheckBox__.setEnabled(True)
        else:
            if _type == self.__signal_header_element__.name:
                self.__signalColumnIndexes__[pathFile] = None
            elif _type == self.__annotation_header_element__.name:
                self.__annotationColumnIndexes__[pathFile] = None
            elif _type == self.__time_header_element__.name:
                self.__timeColumnIndexes__[pathFile] = None

    def __globalClicked__(self):

        _signal = None
        _annotation = None
        _time = None
        if self.__globalCheckBox__.isChecked():
            for num, widget in enumerate(self.__headerWidgets__):
                if widget.isChecked(self.__signal_header_element__.name):
                    _signal = num
                if widget.isChecked(self.__annotation_header_element__.name):
                    _annotation = num
                if widget.isChecked(self.__time_header_element__.name):
                    _time = num
            index = GlobalIndex(_signal, _annotation, _time)
            if index.signal == None:
                self.__globalCheckBox__.setChecked(False)
                InformationWindow(None,
                        message='At least signal column has to be selected !')
                return
            else:
                for num, widget in enumerate(self.__headerWidgets__):
                    widget.enabledAll(False)
                self.__globalIndex__ = index
                self.__setGlobalIndexForAll__(self.__globalIndex__)
        else:
            for widget in self.__headerWidgets__:
                widget.enabledAll(True)
            self.__globalIndex__ = GlobalIndex(None, None, None)

    def __setGlobalIndexForAll__(self, _globalIndex):
        model = self.filesTableView.model()
        for row in range(model.rowCount()):
            #it's necessity to alter model index of QSortFilterProxyModel type
            #into source model index of QStandardItemModel type
            idx = model.mapToSource(model.index(row, 0))
            pathFile = self.filesTableView.getPathAndFilename(idx)
            if pathFile:
                self.__signalColumnIndexes__[pathFile] = _globalIndex.signal

                if _globalIndex.annotation:  # annontation global index
                    self.__annotationColumnIndexes__[pathFile] = \
                                                    _globalIndex.annotation
                if _globalIndex.time:  # time global index
                    self.__timeColumnIndexes__[pathFile] = \
                                                    _globalIndex.time

    def __createHeaderPreviewGroup__(self):
        if hasattr(self, 'fileHeaderPreviewGroup'):
            self.fileHeaderPreviewGroup.deleteLater()
        self.fileHeaderPreviewGroup = GroupBoxWidget(self.tableViewComposite,
                                    i18n="datasource.file.header.preview",
                                    i18n_def="Header preview",
                                    layout=QVBoxLayout(),
                                    hidden=True,
                                    enabled=False)

    def validatePage(self):
        filesSpecificationModel = DatasourceFilesSpecificationModel()
        for (_path, _filename, _signalIndex, _annotationIndex, _timeIndex,
             _separator) in \
            self.__getFilesSpec__(indexes=True, separators=True):
                if _signalIndex == None:
                    ErrorWindow(message=("No signal column for the file %s !"
                                         % (_filename)))
                    return False
                if _separator == None:
                    ErrorWindow(message=("No separator for file %s !"
                                         % (_filename)))
                    return False
                filesSpecificationModel.appendRow(_path, _filename,
                    _signalIndex, _annotationIndex, _timeIndex, _separator,
                    self.__timeUnitsGroup__.getUnit())

        PluginsManager.invokePlugin(PluginsNames.TACHOGRAM_PLOT_PLUGIN_NAME,
                    inspect.stack(),
                    model=filesSpecificationModel.getAsFilesSpecifications(),
                    activity_description=self.__activity__.description(),
                    activity_params=['model'])
        return True

    def __getFilesSpec__(self, _pathfile=True,
                         indexes=False, use_global_index=True,
                         separators=False, use_global_separator=True):
        filesSpec = []
        model = self.filesTableView.model()
        for row in range(model.rowCount()):
            #it's necessity to alter model index of QSortFilterProxyModel type
            #into source model index of QStandardItemModel type
            idx = model.mapToSource(model.index(row, 0))
            pathFile = self.filesTableView.getPathAndFilename(idx)

            if pathFile:
                fileSpec = []
                if _pathfile == True:
                    fileSpec.extend(pathFile)

                if indexes == True:
                    signalIndex = self.__signalColumnIndexes__.get(pathFile)
                    if signalIndex == None and use_global_index == True and \
                        not self.__globalIndex__.signal == None:
                        signalIndex = self.__globalIndex__.signal
                    fileSpec.append(signalIndex)

                    annotationIndex = self.__annotationColumnIndexes__.get(
                                                                   pathFile)
                    if annotationIndex == None and use_global_index == True \
                        and not self.__globalIndex__.annotation == None:
                        annotationIndex = self.__globalIndex__.annotation
                    fileSpec.append(annotationIndex)

                    timeIndex = self.__timeColumnIndexes__.get(pathFile)
                    if timeIndex == None and use_global_index == True \
                        and not self.__globalIndex__.time == None:
                        timeIndex = self.__globalIndex__.time
                    fileSpec.append(timeIndex)

                if separators == True:
                    separator = None
                    dataFileHeader = self.__dataFilesHeaders__.get(pathFile)
                    if not dataFileHeader == None:
                        separator = dataFileHeader.getSeparator()
                    if separator == None and use_global_separator == True:
                        separator = self.__globalSeparator__
                    fileSpec.append(separator)
                filesSpec.append(tuple(fileSpec))
        return filesSpec
class ChooseDatasourcePage(QWizardPage):
    def __init__(self, _parent):
        QWizardPage.__init__(self, parent=_parent)

    def initializePage(self):
        title_I18N(self, "datasource.page.title", "Datasource chooser")
        #self.setSubTitle('Setup frame specific data')
        pageLayout = QVBoxLayout()
        self.setLayout(pageLayout)
        self.__createFilesGroupBox(pageLayout)

        #to force call of isComplete(self) method by the Wizard framework
        #which causes state next button to be updated
        self.emit(WIZARD_COMPLETE_CHANGED_SIGNAL)
        self.rootDir = None

    def __createFilesGroupBox(self, pageLayout):
        self.filesGroupBox = GroupBoxWidget(
            self,
            i18n="datasource.files.group.title",
            i18n_def="Files",
            layout=QVBoxLayout())

        self.__createFileConstraintsComposite__(self.filesGroupBox)

        self.__createReloadButton__(self.filesGroupBox)

        self.__createTableView__(self.filesGroupBox)

        self.__createFilesOperationsComposite__(self.filesGroupBox)

        self.changeEnablemend(False)
        self.chooseRootDirButton.setEnabled(True)

    def __createFileConstraintsComposite__(self, parent):
        fileConstraintsComposite = CompositeWidget(parent,
                                                   layout=QHBoxLayout())

        self.chooseRootDirButton = PushButtonWidget(
            fileConstraintsComposite,
            i18n="datasource.datasource.choose.root.dir.button",
            i18n_def="Choose root dir",
            clicked_handler=self.chooseRootDirAction)

        LabelWidget(fileConstraintsComposite,
                    i18n="datasource.root.dir.label",
                    i18n_def="Root dir:")
        sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        self.rootDirLabel = LabelWidget(fileConstraintsComposite,
                                        i18n="datasource.root.dir.label",
                                        i18n_def="[Not set]",
                                        sizePolicy=sizePolicy,
                                        stretch_after_widget=1)

        LabelWidget(fileConstraintsComposite,
                    i18n="datasource.file.name.filter.label",
                    i18n_def="Files name filter")

        self.filesExtension = LineEditWidget(
            fileConstraintsComposite,
            maxLength=15,
            width=get_width_of_n_letters(14),
            text="*",
            enabled_precheck_handler=self.enabledPrecheckHandler)

        self.recursively = CheckBoxWidget(
            fileConstraintsComposite,
            i18n="datasource.search.files.recursively.label",
            i18n_def="Search files recursively",
            clicked_handler=self.reload,
            enabled_precheck_handler=self.enabledPrecheckHandler)

        self.onlyKnownTypes = CheckBoxWidget(
            fileConstraintsComposite,
            i18n="datasource.only.known.types.checkbox",
            i18n_def="Only known types",
            checked=True,
            clicked_handler=self.reload,
            enabled_precheck_handler=self.enabledPrecheckHandler)

    def __createReloadButton__(self, parent):
        self.reloadButton = PushButtonWidget(
            parent,
            i18n="datasource.reload.button",
            i18n_def="Reload",
            clicked_handler=self.reload,
            alignment=Qt.AlignRight,
            enabled_precheck_handler=self.enabledPrecheckHandler)

    def __createTableView__(self, parent):
        self.filesTableView = FilesTableView(
            parent,
            model=QStandardItemModel(self),
            onClickedAction=self.onClickedAction,
            wizardButtons=(QWizard.NextButton, ),
            wizard_handler=self.wizard,
            sorting=True,
            enabled_precheck_handler=self.enabledPrecheckHandler)

    def __createFilesOperationsComposite__(self, parent):
        filesOperations = CompositeWidget(parent, layout=QHBoxLayout())

        self.filePreviewButton = PushButtonWidget(
            filesOperations,
            i18n="datasource.file.preview.button",
            i18n_def="File preview",
            stretch_after_widget=1,
            clicked_handler=self.filePreviewAction,
            enabled_precheck_handler=self.enabledPrecheckHandler)

        self.selectAllButton = PushButtonWidget(
            filesOperations,
            i18n="datasource.accept.select.all.button",
            i18n_def="Select all",
            enabled=False,
            clicked_handler=self.__select_all_handler__,
            enabled_precheck_handler=self.enabledPrecheckHandler)

        self.unselectAllButton = PushButtonWidget(
            filesOperations,
            i18n="datasource.accept.unselect.all.button",
            i18n_def="Unselect all",
            enabled=False,
            clicked_handler=self.__unselect_all_handler__,
            enabled_precheck_handler=self.enabledPrecheckHandler)

    def chooseRootDirAction(self):
        SettingsFactory.loadSettings(self, Setter(rootDir=None))

        rootDir = QFileDialog.getExistingDirectory(
            self,
            caption=self.chooseRootDirButton.text(),
            directory=self.rootDir)
        if rootDir:
            self.rootDir = rootDir + os.path.sep
            SettingsFactory.saveSettings(self, Setter(rootDir=self.rootDir))
            self.rootDirLabel.setText(self.rootDir)
            self.reload()

    def __select_all_handler__(self):
        self.__change_selection__(True)

    def __unselect_all_handler__(self):
        self.__change_selection__(False)

    def __change_selection__(self, _select):
        self.changeEnablemend(False)
        count = self.filesTableView.getRowCount()
        progressManager = ProgressDialogManager(
            self,
            label_text=("Selecting..." if _select else "Unselecting..."),
            max_value=count)
        with progressManager as progress:
            for idx in range(count):
                if (progress.wasCanceled()):
                    break
                progress.increaseCounter()
                if _select:
                    self.filesTableView.setCheckedRowState(idx, Qt.Checked)
                else:
                    self.filesTableView.setCheckedRowState(idx, Qt.Unchecked)
        self.filesTableView.changeCompleteState()
        self.changeEnablemend(True)

    def filePreviewAction(self):
        showFilesPreviewDialog(
            self.filesTableView.getSelectedPathAndFilename())

    def reload(self):
        self.changeEnablemend(True)
        if self.rootDirLabel.text():

            self.filesTableView.clear()
            self.changeEnablemend(False)

            iteratorFlag = QDirIterator.Subdirectories \
                            if self.recursively.isChecked() \
                            else QDirIterator.NoIteratorFlags
            nameFilters = QStringList(
                    self.filesExtension.text()) \
                    if len(self.filesExtension.text()) > 0 \
                            and not self.filesExtension.text() \
                                in ("*", "*.*", "", None) \
                    else QStringList()
            self.iterator = QDirIterator(self.rootDirLabel.text(), nameFilters,
                                         QDir.Filters(QDir.Files),
                                         iteratorFlag)

            with ProgressDialogManager(self) as progress:
                while (self.iterator.next()):
                    if (progress.wasCanceled()):
                        break
                    fileInfo = self.iterator.fileInfo()
                    if fileInfo.size() == 0 or fileInfo.isFile() == False:
                        continue
                    if self.filesExtension.text() in ("*", "*.*", "", None):
                        if (sys.platform == 'win32' \
                            and fileInfo.isExecutable() == True) \
                            or is_text_file(fileInfo.filePath(),
                                    self.onlyKnownTypes.checkState()) == False:
                            continue
                    progress.increaseCounter()

                    checkable = QStandardItem("")
                    checkable.setCheckable(True)
                    filename = QStandardItem(fileInfo.fileName())
                    progress.setLabelText(fileInfo.fileName())
                    size = QStandardItem(str(fileInfo.size()))
                    path = QStandardItem(fileInfo.path())
                    self.filesTableView.addRow(
                        (checkable, filename, size, path))  # @IgnorePep8

            self.filesTableView.resizeColumnsToContents()
            self.filesExtension.setFocus()
            self.changeEnablemend(True)

    def onClickedAction(self, idx):
        self.filePreviewButton.setEnabled(True)
        self.filesTableView.onClickedAction(idx)

    def isComplete(self):
        """
        method used by QWizard to check if next/previous buttons have to
        be disabled/enabled (when False/True is returned)
        """
        return self.filesTableView.isCompletedCount()

    def changeEnablemend(self, enabled):
        self.emit(SIGNAL(ENABLED_SIGNAL_NAME), enabled)
        self.chooseRootDirButton.setEnabled(enabled)

    def enabledPrecheckHandler(self, widget):
        """
        only interested widgets return bool value others none value
        """
        if widget in (self.selectAllButton, self.unselectAllButton):
            return self.filesTableView.count() > 0
        elif widget == self.filePreviewButton:
            return self.filesTableView.getSelectedRowCount() > 0

    def getDatasourceModel(self):
        return self.filesTableView.getModel()
class ChooseDatasourcePage(QWizardPage):

    def __init__(self, _parent):
        QWizardPage.__init__(self, parent=_parent)

    def initializePage(self):
        title_I18N(self, "datasource.page.title", "Datasource chooser")
        #self.setSubTitle('Setup frame specific data')
        pageLayout = QVBoxLayout()
        self.setLayout(pageLayout)
        self.__createFilesGroupBox(pageLayout)

        #to force call of isComplete(self) method by the Wizard framework
        #which causes state next button to be updated
        self.emit(WIZARD_COMPLETE_CHANGED_SIGNAL)
        self.rootDir = None

    def __createFilesGroupBox(self, pageLayout):
        self.filesGroupBox = GroupBoxWidget(self,
                                    i18n="datasource.files.group.title",
                                    i18n_def="Files",
                                    layout=QVBoxLayout())

        self.__createFileConstraintsComposite__(self.filesGroupBox)

        self.__createReloadButton__(self.filesGroupBox)

        self.__createTableView__(self.filesGroupBox)

        self.__createFilesOperationsComposite__(self.filesGroupBox)

        self.changeEnablemend(False)
        self.chooseRootDirButton.setEnabled(True)

    def __createFileConstraintsComposite__(self, parent):
        fileConstraintsComposite = CompositeWidget(parent,
                                                   layout=QHBoxLayout())

        self.chooseRootDirButton = PushButtonWidget(fileConstraintsComposite,
                        i18n="datasource.datasource.choose.root.dir.button",
                        i18n_def="Choose root dir",
                        clicked_handler=self.chooseRootDirAction)

        LabelWidget(fileConstraintsComposite,
                     i18n="datasource.root.dir.label",
                     i18n_def="Root dir:")
        sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        self.rootDirLabel = LabelWidget(fileConstraintsComposite,
                                        i18n="datasource.root.dir.label",
                                        i18n_def="[Not set]",
                                        sizePolicy=sizePolicy,
                                        stretch_after_widget=1)

        LabelWidget(fileConstraintsComposite,
                    i18n="datasource.file.name.filter.label",
                    i18n_def="Files name filter")

        self.filesExtension = LineEditWidget(fileConstraintsComposite,
                        maxLength=15,
                        width=get_width_of_n_letters(14),
                        text="*",
                        enabled_precheck_handler=self.enabledPrecheckHandler)

        self.recursively = CheckBoxWidget(fileConstraintsComposite,
                        i18n="datasource.search.files.recursively.label",
                        i18n_def="Search files recursively",
                        clicked_handler=self.reload,
                        enabled_precheck_handler=self.enabledPrecheckHandler)

        self.onlyKnownTypes = CheckBoxWidget(fileConstraintsComposite,
                        i18n="datasource.only.known.types.checkbox",
                        i18n_def="Only known types",
                        checked=True,
                        clicked_handler=self.reload,
                        enabled_precheck_handler=self.enabledPrecheckHandler)

    def __createReloadButton__(self, parent):
        self.reloadButton = PushButtonWidget(parent,
                        i18n="datasource.reload.button",
                        i18n_def="Reload",
                        clicked_handler=self.reload,
                        alignment=Qt.AlignRight,
                        enabled_precheck_handler=self.enabledPrecheckHandler)

    def __createTableView__(self, parent):
        self.filesTableView = FilesTableView(parent,
                        model=QStandardItemModel(self),
                        onClickedAction=self.onClickedAction,
                        wizardButtons=(QWizard.NextButton,),
                        wizard_handler=self.wizard,
                        sorting=True,
                        enabled_precheck_handler=self.enabledPrecheckHandler)

    def __createFilesOperationsComposite__(self, parent):
        filesOperations = CompositeWidget(parent,
                                            layout=QHBoxLayout())

        self.filePreviewButton = PushButtonWidget(filesOperations,
                        i18n="datasource.file.preview.button",
                        i18n_def="File preview",
                        stretch_after_widget=1,
                        clicked_handler=self.filePreviewAction,
                        enabled_precheck_handler=self.enabledPrecheckHandler)

        self.selectAllButton = PushButtonWidget(filesOperations,
                        i18n="datasource.accept.select.all.button",
                        i18n_def="Select all",
                        enabled=False,
                        clicked_handler=self.__select_all_handler__,
                        enabled_precheck_handler=self.enabledPrecheckHandler)

        self.unselectAllButton = PushButtonWidget(filesOperations,
                        i18n="datasource.accept.unselect.all.button",
                        i18n_def="Unselect all",
                        enabled=False,
                        clicked_handler=self.__unselect_all_handler__,
                        enabled_precheck_handler=self.enabledPrecheckHandler)

    def chooseRootDirAction(self):
        SettingsFactory.loadSettings(self, Setter(rootDir=None))

        rootDir = QFileDialog.getExistingDirectory(self,
                                    caption=self.chooseRootDirButton.text(),
                                    directory=self.rootDir)
        if rootDir:
            self.rootDir = rootDir + os.path.sep
            SettingsFactory.saveSettings(self, Setter(rootDir=self.rootDir))
            self.rootDirLabel.setText(self.rootDir)
            self.reload()

    def __select_all_handler__(self):
        self.__change_selection__(True)

    def __unselect_all_handler__(self):
        self.__change_selection__(False)

    def __change_selection__(self, _select):
        self.changeEnablemend(False)
        count = self.filesTableView.getRowCount()
        progressManager = ProgressDialogManager(self,
                label_text=("Selecting..." if _select else "Unselecting..."),
                max_value=count)
        with progressManager as progress:
            for idx in range(count):
                if (progress.wasCanceled()):
                    break
                progress.increaseCounter()
                if _select:
                    self.filesTableView.setCheckedRowState(idx, Qt.Checked)
                else:
                    self.filesTableView.setCheckedRowState(idx, Qt.Unchecked)
        self.filesTableView.changeCompleteState()
        self.changeEnablemend(True)

    def filePreviewAction(self):
        showFilesPreviewDialog(
                    self.filesTableView.getSelectedPathAndFilename())

    def reload(self):
        self.changeEnablemend(True)
        if self.rootDirLabel.text():

            self.filesTableView.clear()
            self.changeEnablemend(False)

            iteratorFlag = QDirIterator.Subdirectories \
                            if self.recursively.isChecked() \
                            else QDirIterator.NoIteratorFlags
            nameFilters = QStringList(
                    self.filesExtension.text()) \
                    if len(self.filesExtension.text()) > 0 \
                            and not self.filesExtension.text() \
                                in ("*", "*.*", "", None) \
                    else QStringList()
            self.iterator = QDirIterator(
                                self.rootDirLabel.text(),
                                nameFilters,
                                QDir.Filters(QDir.Files),
                                iteratorFlag)

            with ProgressDialogManager(self) as progress:
                while(self.iterator.next()):
                    if (progress.wasCanceled()):
                        break
                    fileInfo = self.iterator.fileInfo()
                    if fileInfo.size() == 0 or fileInfo.isFile() == False:
                        continue
                    if self.filesExtension.text() in ("*", "*.*", "", None):
                        if (sys.platform == 'win32' \
                            and fileInfo.isExecutable() == True) \
                            or is_text_file(fileInfo.filePath(),
                                    self.onlyKnownTypes.checkState()) == False:
                            continue
                    progress.increaseCounter()

                    checkable = QStandardItem("")
                    checkable.setCheckable(True)
                    filename = QStandardItem(fileInfo.fileName())
                    progress.setLabelText(fileInfo.fileName())
                    size = QStandardItem(str(fileInfo.size()))
                    path = QStandardItem(fileInfo.path())
                    self.filesTableView.addRow((checkable, filename, size, path)) # @IgnorePep8

            self.filesTableView.resizeColumnsToContents()
            self.filesExtension.setFocus()
            self.changeEnablemend(True)

    def onClickedAction(self, idx):
        self.filePreviewButton.setEnabled(True)
        self.filesTableView.onClickedAction(idx)

    def isComplete(self):
        """
        method used by QWizard to check if next/previous buttons have to
        be disabled/enabled (when False/True is returned)
        """
        return self.filesTableView.isCompletedCount()

    def changeEnablemend(self, enabled):
        self.emit(SIGNAL(ENABLED_SIGNAL_NAME), enabled)
        self.chooseRootDirButton.setEnabled(enabled)

    def enabledPrecheckHandler(self, widget):
        """
        only interested widgets return bool value others none value
        """
        if widget in (self.selectAllButton, self.unselectAllButton):
            return self.filesTableView.count() > 0
        elif widget == self.filePreviewButton:
            return self.filesTableView.getSelectedRowCount() > 0

    def getDatasourceModel(self):
        return self.filesTableView.getModel()