def __init__(self, iface, dialog_ui, bbox_tool):
        """
        Constructor for the dialog tool
        :param iface: The QGIS Interface
        :param dialog_ui: The dialog GUI
        :param bbox_tool The bounding box tool
        :return: dialog tool
        """
        QObject.__init__(self, None)
        self.iface = iface
        self.dialog_ui = dialog_ui
        self.bbox_tool = bbox_tool

        self.progress_bar = None
        self.progress_message_bar = None
        self.progress_message_bar_widget = None
        self.search_thread_pool = QThreadPool()
        self.search_lock = Lock()
        self.export_thread_pool = QThreadPool()
        self.export_lock = Lock()
        self.query = None
        self.previous_credentials = None
        self.export_file = None
        self.footprint_layer = None

        self.filters = CatalogFilters(self.dialog_ui)

        self.dialog_ui.aoi_button.clicked.connect(self.aoi_button_clicked)
        self.dialog_ui.reset_button.clicked.connect(self.reset_button_clicked)
        self.dialog_ui.export_button.clicked.connect(self.export_button_clicked)
        self.bbox_tool.released.connect(self.search)
        self.model = None
Example #2
0
    def __init__(self, iface, dialog_ui, bbox_tool):
        """
        Constructor for the dialog tool
        :param iface: The QGIS Interface
        :param dialog_ui: The dialog GUI
        :param bbox_tool The bounding box tool
        :return: dialog tool
        """
        QObject.__init__(self, None)
        self.iface = iface
        self.dialog_ui = dialog_ui
        self.bbox_tool = bbox_tool

        self.progress_bar = None
        self.progress_message_bar = None
        self.progress_message_bar_widget = None
        self.search_thread_pool = QThreadPool()
        self.search_lock = Lock()
        self.export_thread_pool = QThreadPool()
        self.export_lock = Lock()
        self.query = None
        self.previous_credentials = None
        self.export_file = None
        self.footprint_layer = None

        self.filters = CatalogFilters(self.dialog_ui)

        self.dialog_ui.aoi_button.clicked.connect(self.aoi_button_clicked)
        self.dialog_ui.reset_button.clicked.connect(self.reset_button_clicked)
        self.dialog_ui.export_button.clicked.connect(
            self.export_button_clicked)
        self.bbox_tool.released.connect(self.search)
        self.model = None
Example #3
0
 def on_send_status_clicked(self):
     status = self.ui.status.toPlainText().__str__()
     self.ui.status.clear()
     self.ui.status.setReadOnly(True)
     sender = SenderCommand(self.client, status)
     sender.posted.connect(self.status_posted)
     command_processor = CommandProcessor(sender)
     QThreadPool.globalInstance().start(command_processor)
     
Example #4
0
    def process_states(self, states, use_optimizations, output_mode,
                       plot_results, output_graph):
        self._worker = Worker(self._process_states_on_thread, states,
                              use_optimizations, output_mode, plot_results,
                              output_graph)
        self._worker.signals.finished.connect(self.on_finished)
        self._worker.signals.error.connect(self.on_error)

        QThreadPool.globalInstance().start(self._worker)
Example #5
0
    def process(self, caller, func, *args, **kwargs):
        # Add the caller
        self._add_listener(caller)

        # Generate worker
        self._worker = Worker(func, *args, **kwargs)
        self._worker.signals.finished.connect(self.on_finished)
        self._worker.signals.error.connect(self.on_error)

        QThreadPool.globalInstance().start(self._worker)
Example #6
0
    def process(self, caller, func, *args, **kwargs):
        # Add the caller
        self._add_listener(caller)

        # Generate worker
        self._worker = Worker(func, *args, **kwargs)
        self._worker.signals.finished.connect(self.on_finished)
        self._worker.signals.error.connect(self.on_error)

        QThreadPool.globalInstance().start(self._worker)
Example #7
0
    def run_worker(self, task, callback):
        """Runs a task in another thread.

        The `task` must be an object that implements a `run()`
        method. Completion is notified to the given `callback` function.

        """
        worker = Worker(task)
        worker.finished.connect(callback)
        QThreadPool.globalInstance().start(worker)
Example #8
0
    def run_worker(self, task, callback):
        """Runs a task in another thread.

        The `task` must be an object that implements a `run()`
        method. Completion is notified to the given `callback` function.

        """
        worker = Worker(task)
        worker.finished.connect(callback)
        QThreadPool.globalInstance().start(worker)
Example #9
0
 def __init__(self, parent=None):
     QObject.__init__(self, parent)
     self.process = QProcess()
     self.process.setProcessChannelMode(QProcess.MergedChannels)
     self.process.finished.connect(self.emit_finished)
     self.process.stateChanged.connect(self.emit_state)
     self.process_type = 0  # 0 process, 1 runnable
     self.threadpool = QThreadPool()
     self.worker = Worker()
     self.worker.signals.state.connect(self.emit_state)
     self.queue = None
Example #10
0
def omap(fkt, par):
    """
    :type fkt: ()->
    :type par: list
    :rtype: list
    """
    # Die Anzahl der Prozesse ist ohne Aufwand auf einen idealen Wert beschränkt
    pool = QThreadPool()
    erg = [None] * len(par)
    for p in par:
        pool.start(QRunnable(fkt, p))
    def test_that_process_states_emits_row_processed_signal_after_each_row(self):
        self.batch_process_runner.row_processed_signal = mock.MagicMock()
        self.batch_process_runner.row_failed_signal = mock.MagicMock()
        self.batch_process_runner.process_states(self.states, False, OutputMode.Both, False, '')
        QThreadPool.globalInstance().waitForDone()

        self.assertEqual(self.batch_process_runner.row_processed_signal.emit.call_count, 3)
        self.batch_process_runner.row_processed_signal.emit.assert_any_call(0, [], [])
        self.batch_process_runner.row_processed_signal.emit.assert_any_call(1, [], [])
        self.batch_process_runner.row_processed_signal.emit.assert_any_call(2, [], [])
        self.assertEqual(self.batch_process_runner.row_failed_signal.emit.call_count, 0)
Example #12
0
 def query(self, key):
     """Emit query."""
     collect = Collect(
         list(map(lambda a: a.engine, filter(Group.selected, self.output_areas))),
         lambda r: self.record(key, r)
         )
     QThreadPool.globalInstance().start(collect)
     #for engine in self.engines:
         #QThreadPool.globalInstance().start(
                 #Runnable(lambda: engine.query(key), args=(object,)))
     async_actions(map(lambda e: partial(e.query, key), self.engines))
    def test_that_load_workspaces_emits_row_processed_signal_after_each_row(self):
        self.batch_process_runner.row_processed_signal = mock.MagicMock()
        self.batch_process_runner.row_failed_signal = mock.MagicMock()

        self.batch_process_runner.load_workspaces(self.states)
        QThreadPool.globalInstance().waitForDone()

        self.assertEqual(self.batch_process_runner.row_processed_signal.emit.call_count, 3)
        self.batch_process_runner.row_processed_signal.emit.assert_any_call(0, [], [])
        self.batch_process_runner.row_processed_signal.emit.assert_any_call(1, [], [])
        self.batch_process_runner.row_processed_signal.emit.assert_any_call(2, [], [])
        self.assertEqual(self.batch_process_runner.row_failed_signal.emit.call_count, 0)
Example #14
0
    def test_that_process_states_emits_row_processed_signal_after_each_row(self):
        self.batch_process_runner.row_processed_signal = mock.MagicMock()
        self.batch_process_runner.row_failed_signal = mock.MagicMock()

        self.batch_process_runner.process_states(self.states, False, OutputMode.Both, False, '')
        QThreadPool.globalInstance().waitForDone()

        self.assertEqual(self.batch_process_runner.row_processed_signal.emit.call_count, 3)
        self.batch_process_runner.row_processed_signal.emit.assert_any_call(0)
        self.batch_process_runner.row_processed_signal.emit.assert_any_call(1)
        self.batch_process_runner.row_processed_signal.emit.assert_any_call(2)
        self.assertEqual(self.batch_process_runner.row_failed_signal.emit.call_count, 0)
Example #15
0
class QWorker(QObject):

    """
    Starts the QRunnable in a QThreadPool,  inherits QObject because
    we need to connect signals from the objects we are creating to
    the functions that update the UI
    """

    def __init__(self):

        """
        This class creates new instances of QRunner, which is the class
        that directly interfaces with the external executable

        We need to create the QRunner/QWorker routine because a QRunnable
        does not inherit from QObject and therefore does not have access
        to the signal/slot mechanisms that we need in order to receive when
        things get done.

        QWorker inherits from QObject, so we need to directly call QObjects
        constructor to initialize the underlying C++ object, super() does not
        allow for this (seemingly) so we need to call it directly.
        """

        # init QObject
        QObject.__init__(self)
        # Our own threadpool, for adding QRunners
        self.threadpool = QThreadPool()
        self.jobs = []

    def create_job(self, ifname, ofname, cls):

        """
        This method takes three arguments:

        ifname is the input file name of the file to be converted
        ofname is a string which is to be the output of the filename
        cls is a reference to the calling class, so that we can connect
        a signal to a slot.
        """

        # Create the QRunner object and pass it filenames
        runner = QRunner(ifname, ofname, cls)

        # using our own connect method inherited from QObject
        # connect the QRunner created before and use it's QObject
        # to connect a signal to the slot
        self.connect(runner.q_object, SIGNAL(
                                         "finished()"), cls.update_progress_bar)

        # ask our threadpool to run the task
        self.threadpool.start(runner)
Example #16
0
    def __init__(self,
                 left,
                 top,
                 right,
                 bottom,
                 csv_filename,
                 username,
                 password,
                 client_id,
                 client_secret,
                 days_to_query=60):
        self.left = left
        self.top = top
        self.right = right
        self.bottom = bottom
        self.csv_filename = csv_filename
        self.days_to_query = days_to_query
        self.begin_date = None
        self.end_date = None

        self.username = username
        self.password = password
        self.client_id = client_id
        self.client_secret = client_secret

        # throw up a progress dialog
        min_progress = 0.0
        max_progress = ((self.right - self.left) / INCREMENTAL_INTERVAL) * \
                       ((self.top - self.bottom) / INCREMENTAL_INTERVAL)
        self.current_progress = min_progress

        self.progress_dialog = QProgressDialog("Building up CSV file", "Abort",
                                               int(min_progress),
                                               int(max_progress), None)
        self.progress_dialog.setCancelButton(None)
        self.progress_dialog.setWindowTitle("CSV Output")
        self.progress_dialog.setLabelText("Building up CSV file")
        self.progress_dialog.setMinimumDuration(0)
        self.progress_dialog.setValue(0)
        self.progress_dialog.show()

        self.csv_elements = []

        self.csv_generator_object = CSVGeneratorObject(self)

        self.vector_header_dict = {}

        self.pool = QThreadPool()

        self.finished_submissions = False

        self.lock = Lock()
Example #17
0
 def record(self, key, responses):
     """Record in background."""
     if not self.recorder is None:
         runnable = Runnable(lambda: self.recorder.add(
                 #word=s.word,
                 #pron=s.phonetic_symbol,
                 #trans='\n'.join(s.custom_translations),
                 #time=datetime.now()
                 key=key,
                 responses=responses
                 ))
         runnable.finished.connect(self.recorded)
         QThreadPool.globalInstance().start(runnable)
Example #18
0
    def test_that_process_states_emits_row_failed_signal_after_each_failed_row(self):
        self.batch_process_runner.row_processed_signal = mock.MagicMock()
        self.batch_process_runner.row_failed_signal = mock.MagicMock()
        self.sans_batch_instance.side_effect = Exception('failure')

        self.batch_process_runner.process_states(self.states, False, OutputMode.Both, False, '')
        QThreadPool.globalInstance().waitForDone()

        self.assertEqual(self.batch_process_runner.row_failed_signal.emit.call_count, 3)
        self.batch_process_runner.row_failed_signal.emit.assert_any_call(0, 'failure')
        self.batch_process_runner.row_failed_signal.emit.assert_any_call(1, 'failure')
        self.batch_process_runner.row_failed_signal.emit.assert_any_call(2, 'failure')
        self.assertEqual(self.batch_process_runner.row_processed_signal.emit.call_count, 0)
def push_to_website(payload):
    time = payload[SENSOR_TIMESTAMP]["val"]
    for key in payload:
        category = payload[key]["cat"]
        value = payload[key]["val"]

        if category == CAT_NONE:
            continue

        api = WebsiteAPI()
        api.set_password(WEB_PASSWORD)
        api.set_host(WEB_HOST)
        api.set_data(category, key, value, time)
        QThreadPool.globalInstance().start(api)
Example #20
0
    def start(self):
        self.idmap = {}
        self.entries = []

        pool = QThreadPool()
        pool.setMaxThreadCount(1)

        for label in LABELS:
            feed = Feed(label, self)
            pool.start(feed)
            imap = Imap(label, self)
            pool.start(imap)

        pool.waitForDone()
        self.done.emit()
Example #21
0
    def __init__(self, iface, db, validation_dia, plugin_dir, params):
        self.iface = iface
        self.db = db
        self.params = params
        self.validation_dia = validation_dia
        self.app_root = plugin_dir
        self.open_image = QPixmap(os.path.join(self.app_root,
                                               "image",
                                               "folder_open_icon.png"))
        self.validation_dia.ui.openPushButton.setIcon(QIcon(self.open_image))
        self.validation_dia.ui.openPushButton.setToolTip("Select File")
        self.export_globals = None
        self.validation_dk = None
        self.report_to_dialog = None
        self.re = QRegExp("CheckBox")
        self.check_boxes_names = []
        self.long_task = QThreadPool(None).globalInstance()
        self.summary_tables = {}
        self.model_navigation()
        self.form_load()
        self.file_dialog = QFileDialog()
        self.summary_functions = {}
        self.report_file_path = None
        self.home_dir = os.path.expanduser('~')
        self.org_name = database.get_from_gaz_metadata(db, "owner")
        self.report = ExportValidationReport("roadNet Validation Report",
                                             self.org_name,
                                             self.db, self.iface, None)
        self.list_check_boxes = []
        self.progress_win = QProgressDialog("", None, 0, 100, self.validation_dia)
        self.progress_win.setFixedSize(380, 100)
        self.progress_win.setModal(True)
        self.progress_win.setWindowTitle("Export Validation Report")

        self.summary_runnables = {'dupStreetCheckBox': [lambda: DupStreetDesc(), 0],
                                  'notStreetEsuCheckBox': [lambda: StreetsNoEsuDesc(), 1],
                                  'notType3CheckBox': [lambda: Type3Desc(False), 2],
                                  'incFootPathCheckBox': [lambda: Type3Desc(True), 2],
                                  'dupEsuRefCheckBox': [lambda: DupEsuRef(True), 3],
                                  'notEsuStreetCheckBox': [lambda: NoLinkEsuStreets(), 4],
                                  'invCrossRefCheckBox': [lambda: InvalidCrossReferences()],
                                  'startEndCheckBox': [lambda: CheckStartEnd(), 8],
                                  'tinyEsuCheckBox': [lambda: CheckTinyEsus("esu", 1)],
                                  'notMaintReinsCheckBox': [lambda: CheckMaintReins()],
                                  'asdStartEndCheckBox': [lambda: CheckAsdCoords()],
                                  'notMaintPolysCheckBox': [lambda: MaintNoPoly(), 16],
                                  'notPolysMaintCheckBox': [lambda: PolyNoMaint()],
                                  'tinyPolysCheckBox': [lambda: CheckTinyEsus("rd_poly", 1)]}
    def __init__(self, func=None, args=(), kwargs={}, thread=None,
                 threadPool=None, parent=None):
        QObject.__init__(self, parent)
        self.func = func
        self._args = args
        self._kwargs = kwargs
        self.threadPool = None

        self._connected = True
        self._cancelRequested = False
        self._started = False
        self._cancelled = False

        if thread is not None:
            self.moveToThread(thread)
        else:
            if threadPool is None:
                threadPool = QThreadPool.globalInstance()
            self.threadPool = threadPool
            self._runnable = _RunnableAsyncCall(self)
            self.threadPool.start(self._runnable)
            self._connected = False
            return

        self.connect(self, SIGNAL("_async_start()"), self.execute,
                     Qt.QueuedConnection)
Example #23
0
    def __init__(self,
                 func=None,
                 args=(),
                 kwargs={},
                 thread=None,
                 threadPool=None,
                 parent=None):
        QObject.__init__(self, parent)
        self.func = func
        self._args = args
        self._kwargs = kwargs
        self.threadPool = None

        self._connected = True
        self._cancelRequested = False
        self._started = False
        self._cancelled = False

        if thread is not None:
            self.moveToThread(thread)
        else:
            if threadPool is None:
                threadPool = QThreadPool.globalInstance()
            self.threadPool = threadPool
            self._runnable = _RunnableAsyncCall(self)
            self.threadPool.start(self._runnable)
            self._connected = False
            return

        self.connect(self, SIGNAL("_async_start()"), self.execute,
                     Qt.QueuedConnection)
Example #24
0
    def __init__(self,
      # A number *n* to create a pool of *n* simple threads, where each thread
      # lacks an event loop, so that it can emit but not receive signals.
      # This means that ``g`` may **not** be run in a thread of this pool,
      # without manually adding a event loop. If *n* < 1, the global thread pool
      # is used.
      maxThreadCount,

      # |parent|
      parent=None):

        super(AsyncPoolController, self).__init__(parent)
        if maxThreadCount < 1:
            self.threadPool = QThreadPool.globalInstance()
        else:
            self.threadPool = QThreadPool()
            self.threadPool.setMaxThreadCount(maxThreadCount)
 def __init__(self, control_system):
     """Make a instance of the ReaderAndWriterThread class.
     Args:
         protocol (SerialProtocol): It is a instance of a communication protocol.
     """
     QThread.__init__(self, control_system)
     self.writer_thread = QThreadPool(self)
     self.writer_thread.setMaxThreadCount(1)
Example #26
0
    def test_that_load_workspaces_emits_row_processed_signal_after_each_row(
            self):
        self.batch_process_runner.row_processed_signal = mock.MagicMock()
        self.batch_process_runner.row_failed_signal = mock.MagicMock()

        self.batch_process_runner.load_workspaces(self.states)
        QThreadPool.globalInstance().waitForDone()

        self.assertEqual(
            self.batch_process_runner.row_processed_signal.emit.call_count, 3)
        self.batch_process_runner.row_processed_signal.emit.assert_any_call(
            0, [], [])
        self.batch_process_runner.row_processed_signal.emit.assert_any_call(
            1, [], [])
        self.batch_process_runner.row_processed_signal.emit.assert_any_call(
            2, [], [])
        self.assertEqual(
            self.batch_process_runner.row_failed_signal.emit.call_count, 0)
Example #27
0
 def __init__(self, parent=None, threadPool=None):
     QObject.__init__(self, parent)
     if threadPool is None:
         threadPool = QThreadPool.globalInstance()
     self._threadPool = threadPool
     self._depot_thread = None
     self._futures = []
     self._shutdown = False
     self._state_lock = threading.Lock()
Example #28
0
    def start(self):
        self.idmap = {}
        self.entries = []

        pool = QThreadPool()
        pool.setMaxThreadCount(1)

        for label in LABELS:
            feed = Feed(label, self)
            pool.start(feed)
            imap = Imap(label, self)
            pool.start(imap)

        pool.waitForDone()
        self.done.emit()
Example #29
0
    def login(self):
        assert self.settings.username,\
            'Invalid username: %s' % self.settings.username

        def inner():
            try:
                self.recorder = ThreadSafeRecorder(Recorder(
                    username=self.settings.username,
                    password=self.settings.password,
                    deck=self.settings.deck
                    ))
            except:
                return False
            else:
                return True

        runnable = Runnable(inner)
        runnable.finished.connect(self.logined)
        QThreadPool.globalInstance().start(runnable)
Example #30
0
class PROCESS(QObject):
    queue_finished = pyqtSignal(int)
    prog_finished = pyqtSignal(tuple)
    stoped = pyqtSignal(int, int)  # 1: start, 0: finished, 2: error | queue
    state = pyqtSignal(int, str)

    def __init__(self, parent=None):
        QObject.__init__(self, parent)
        self.process = QProcess()
        self.process.setProcessChannelMode(QProcess.MergedChannels)
        self.process.finished.connect(self.emit_finished)
        self.process.stateChanged.connect(self.emit_state)
        self.process_type = 0  # 0 process, 1 runnable
        self.threadpool = QThreadPool()
        self.worker = Worker()
        self.worker.signals.state.connect(self.emit_state)
        self.queue = None

    def emit_state(self, state):
        self.state.emit(state, self.prog_name)

    def emit_finished(self, exitcode, exitstatus):
        self.prog_finished.emit((self.prog_name, exitcode, exitstatus))

    def set_queue(self, queue):
        self.queue = queue

    def start_process(self):
        try:
            obj = self.queue.get(False)
            self.prog_name = obj.keys()[0]
            if callable(obj.values()[0][0]):
                self.process_type = 1
                funct = obj.values()[0][0]
                args = obj.values()[0][1]
                self.worker.insert_function(funct, args, self.prog_name)
                self.threadpool.start(self.worker)
            else:
                self.process_type = 0
                self.process.start(obj.values()[0][0], obj.values()[0][1])
        except Queue.Empty:
            self.queue_finished.emit(self.queue.name)

    def force_finished(self):
        # for process (programs)
        self.stoped.emit(1, self.queue.name)
        if self.process_type == 0:
            self.process.terminate()
            if not self.process.waitForFinished(1000):
                self.process.kill()
        else:
            if self.threadpool.activeThreadCount():
                self.threadpool.clear()
                self.threadpool.waitForDone()
        with self.queue.mutex:
            self.queue.queue.clear()
        self.stoped.emit(0, self.queue.name)
Example #31
0
class QFileWorker(object):

    def __init__(self, deletions, cls):
        """
        This class handles deletion of temporary file objects once processing
        has completed.

        It takes a reference to the class because we re-enable it's button
        because we disabled it during processing
        """

        self.deletions = deletions
        self.threadpool = QThreadPool()
        self.cls = cls
        self._start()

    def _start(self):
        deleter = QFileCleaner(self.deletions, self.cls)
        self.threadpool.start(deleter)
        self.threadpool.waitForDone()
Example #32
0
class Tasks(QObject):
    def __init__(self, ar, process_result):
        super(Tasks, self).__init__()
        self.ar = ar
        self.process_result = process_result
        self.pool = QThreadPool()
        self.pool.setMaxThreadCount(10)

    # def process_result(self, rev):
    #     # print 'Receiving', rev
    #     self.ref.setText(rev)

    def start(self):
        self.factory = GenerWork(self.ar)

        self.factory.generateWorkers()
        workers = self.factory.get_workers()

        # print workers
        for worker in workers:
            worker.signals.result.connect(self.process_result)

            self.pool.start(worker)

        self.pool.waitForDone()
        return data

    def get_shell_data(self):
        return self.factory.get_shell_data()
Example #33
0
class AsyncPoolController(AsyncAbstractController):
    def __init__(self,
      # A number *n* to create a pool of *n* simple threads, where each thread
      # lacks an event loop, so that it can emit but not receive signals.
      # This means that ``g`` may **not** be run in a thread of this pool,
      # without manually adding a event loop. If *n* < 1, the global thread pool
      # is used.
      maxThreadCount,

      # |parent|
      parent=None):

        super(AsyncPoolController, self).__init__(parent)
        if maxThreadCount < 1:
            self.threadPool = QThreadPool.globalInstance()
        else:
            self.threadPool = QThreadPool()
            self.threadPool.setMaxThreadCount(maxThreadCount)

    # |_start|
    def _start(self, future):
        # Asynchronously invoke ``f``.
        apw = _AsyncPoolWorker(future)
        self.threadPool.start(apw)

    # |terminate|
    def _terminate(self):
        self.threadPool.waitForDone()
        del self.threadPool
Example #34
0
    def __init__(self, deletions, cls):
        """
        This class handles deletion of temporary file objects once processing
        has completed.

        It takes a reference to the class because we re-enable it's button
        because we disabled it during processing
        """

        self.deletions = deletions
        self.threadpool = QThreadPool()
        self.cls = cls
        self._start()
Example #35
0
    def __init__(self):
        QMainWindow.__init__(self)

        self.DownloadPath = os.path.expanduser("~") # get home dir
        self.pool = QThreadPool()
        self.pool.setMaxThreadCount(1)

        self.initVariables()

        self.resize(700, 400)
        self.initUI()

        self.line_downpath.setText(self.down_control.getDownloadPath())
Example #36
0
    def start_testing(self):
        """Tests all servers by using various thread workers (QRunnables) in a thread pool.

        This way testing is done asynchronously, since some servers may take some time to answer, so the UI is
        not blocked until the process is done and we can show a progress bar.

        See:
        https://martinfitzpatrick.name/article/multithreading-pyqt-applications-with-qthreadpool/
        https://nikolak.com/pyqt-threading-tutorial/
        """

        # A thread pool is a thread automatically hadling various tasks.
        self.threadpool = QThreadPool()
        self._n_tested = 0

        for data in self.servers_to_test:
            worker = CLineTestWorker(*data)
            # When each worker is done, `end_testing` is called.
            worker.signals.finished.connect(self.end_testing)

            # Executing the thread worker within the pool.
            self.threadpool.start(worker)
    def __init__(self, width, height, *args, **kwargs):
        super(PyQtPaint, self).__init__(*args, **kwargs)
        uic.loadUi('../ui/pyqtpaint.ui', self)

        self._paint_view = PaintView()
        self._paint_view.setRenderHints(QtGui.QPainter.HighQualityAntialiasing)

        self.paint_scene = PaintScene(0, 0, width, height, None)
        self._paint_view.setScene(self.paint_scene)

        # Numbers of layers
        self.num_layers = 0
        self.old_layers = -1

        # Timer to save images
        self.timer = QTimer()
        self.timer.setInterval(1000)
        self.timer.start()

        # Path to save imgs
        self.tmpFilepath = "../img/tmp/tmp.png"

        # Timer to predict shapes
        self.timer_predict = QTimer()
        self.timer_predict.setInterval(1000)
        self.timer_predict.start()

        # The app is not working on predict shapes
        self.working = False

        # Thread to run in background of paint
        self.threadpool = QThreadPool()

        # Setup all UI, and make the connctions between Signals & Slots
        self._setup_ui()
        self._create_actions()
        self._make_connections()
Example #38
0
 def __init__(self, parent=None):
     """Init Window"""
     QtGui.QMainWindow.__init__(self, parent)
     self.gui = Ui_MainWindow()
     self.gui.setupUi(self)
     self.gscriptpath = '"' +  os.getcwd() + r'\gs\gs9.02\bin'
     self.gui.progressBar.hide()
     self.single_output_dir = ''
     self.deletions = []
     self.work = QThreadPool()
     self.work.setMaxThreadCount(1)
     self.thread_number = 5
     self.setWindowIcon(QtGui.QIcon(":/ico.png"))
     self.resolution = 450
     self.mode = 'tiffg4'
    def __init__(self, iface, parent=None):
        """Constructor."""
        super(GridZoneGeneratorDialog, self).__init__(parent)
        # Set up the user interface from Designer.
        # After setupUI you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        self.setupUi(self)
        self.iface = iface

        self.crsLineEdit.setReadOnly(True)

        self.utmgrid = UtmGrid()

        self.setValidCharacters()

        self.setMask()

        self.threadpool = QThreadPool()
    def __init__(self, iface, parent=None):
        """Constructor."""
        super(GridZoneGeneratorDialog, self).__init__(parent)
        # Set up the user interface from Designer.
        # After setupUI you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        self.setupUi(self)
        self.iface = iface

        self.crsLineEdit.setReadOnly(True)
        
        self.utmgrid = UtmGrid()

        self.setValidCharacters()

        self.setMask()

        self.threadpool = QThreadPool()
Example #41
0
    def __init__(self, left, top, right, bottom, csv_filename, username, password, client_id, client_secret, days_to_query=60):
        self.left = left
        self.top = top
        self.right = right
        self.bottom = bottom
        self.csv_filename = csv_filename
        self.days_to_query = days_to_query
        self.begin_date = None
        self.end_date = None

        self.username = username
        self.password = password
        self.client_id = client_id
        self.client_secret = client_secret

        # throw up a progress dialog
        min_progress = 0.0
        max_progress = ((self.right - self.left) / INCREMENTAL_INTERVAL) * \
                       ((self.top - self.bottom) / INCREMENTAL_INTERVAL)
        self.current_progress = min_progress

        self.progress_dialog = QProgressDialog("Building up CSV file", "Abort", int(min_progress), int(max_progress),
                                               None)
        self.progress_dialog.setCancelButton(None)
        self.progress_dialog.setWindowTitle("CSV Output")
        self.progress_dialog.setLabelText("Building up CSV file")
        self.progress_dialog.setMinimumDuration(0)
        self.progress_dialog.setValue(0)
        self.progress_dialog.show()

        self.csv_elements = []

        self.csv_generator_object = CSVGeneratorObject(self)
        
        self.vector_header_dict = {}

        self.pool = QThreadPool()

        self.finished_submissions = False

        self.lock = Lock()
Example #42
0
    def __init__(self):

        """
        This class creates new instances of QRunner, which is the class
        that directly interfaces with the external executable

        We need to create the QRunner/QWorker routine because a QRunnable
        does not inherit from QObject and therefore does not have access
        to the signal/slot mechanisms that we need in order to receive when
        things get done.

        QWorker inherits from QObject, so we need to directly call QObjects
        constructor to initialize the underlying C++ object, super() does not
        allow for this (seemingly) so we need to call it directly.
        """

        # init QObject
        QObject.__init__(self)
        # Our own threadpool, for adding QRunners
        self.threadpool = QThreadPool()
        self.jobs = []
Example #43
0
 def __init__(self, parent=None, threadPool=None):
     QObject.__init__(self, parent)
     if threadPool is None:
         threadPool = QThreadPool.globalInstance()
     self._threadPool = threadPool
     self._depot_thread = None
class KISS_Thread(QtCore.QThread):
    """
        AX.25 Communication
    """
    packet_received = pyqtSignal("QString", name="packetReceived")

    def __init__(self, control_system):
        """Make a instance of the ReaderAndWriterThread class.
        Args:
            protocol (SerialProtocol): It is a instance of a communication protocol.
        """
        QThread.__init__(self, control_system)
        self.writer_thread = QThreadPool(self)
        self.writer_thread.setMaxThreadCount(1)

    def run(self):
        print "kiss started"
        self.kiss_connect()

    def read_callback(self, data):
        print "received packet, len", len(data)
        kiss_data = kiss.constants.FEND + kiss.util.escape_special_codes(data) + kiss.constants.FEND
        log_the_data("./log_files/telemetry_log_files/BSU_satellite.kss", kiss_data)
        data = data[1:]
        if len(data) < 15:
            print "bad packet"
            return
        dest = "".join([chr(ord(c) >> 1) for c in data[:6]])
        # print "Destination", dest
        src = "".join([chr(ord(c) >> 1) for c in data[7:13]])
        #print "Source", src
        if not dest.startswith(LOCAL_SSID):
            print "packet not for us"
            return
        start = 16
        ssid = ord(data[13]) & 1
        if ssid == 0:
            via = "".join([chr(ord(c) >> 1) for c in data[7:13]])
            start = 23
        size = len(data) - start
        if size == 0:
            print "packet is empty"
            return
        payload = data[start:]
        # self.control_system.on_packet_received(payload)
        self.packet_received.emit(payload)

    def kiss_connect(self):
        try:
            print "connect"
            self.kiss_prot = kiss.KISS(port=config.kiss_serial_name, speed=config.kiss_baudrate)
            self.kiss_prot.start()
            self.kiss_prot.read(self.read_callback)
        except:
            error_mesage = "CANNOT OPEN PORT"
            log_the_error(error_mesage)
            sound.play(error_mesage)

            print sys.exc_info()
            print "ax.25 is failed"


    def send_command(self, name, arg, device, time_of_execution):
        data = {
            "Timestamp": int(time.time()),
            "Schedule": time_of_execution,
            "Cmd": name,
            "Arg": arg,
            "Device": device
        }
        json_data = json.dumps(data)

        print "Formed packet", json_data
        writer = KissWriter(self.kiss_prot, LOCAL_SSID, REMOTE_SSID)
        writer.set_data(json_data)
        self.writer_thread.start(writer)
Example #45
0
 def __init__(self):
     self.thread_pool = None
     self._listener = {}
     self._worker = {}
     self.thread_pool = QThreadPool()
Example #46
0
class CSVGenerator:
    def __init__(self,
                 left,
                 top,
                 right,
                 bottom,
                 csv_filename,
                 username,
                 password,
                 client_id,
                 client_secret,
                 days_to_query=60):
        self.left = left
        self.top = top
        self.right = right
        self.bottom = bottom
        self.csv_filename = csv_filename
        self.days_to_query = days_to_query
        self.begin_date = None
        self.end_date = None

        self.username = username
        self.password = password
        self.client_id = client_id
        self.client_secret = client_secret

        # throw up a progress dialog
        min_progress = 0.0
        max_progress = ((self.right - self.left) / INCREMENTAL_INTERVAL) * \
                       ((self.top - self.bottom) / INCREMENTAL_INTERVAL)
        self.current_progress = min_progress

        self.progress_dialog = QProgressDialog("Building up CSV file", "Abort",
                                               int(min_progress),
                                               int(max_progress), None)
        self.progress_dialog.setCancelButton(None)
        self.progress_dialog.setWindowTitle("CSV Output")
        self.progress_dialog.setLabelText("Building up CSV file")
        self.progress_dialog.setMinimumDuration(0)
        self.progress_dialog.setValue(0)
        self.progress_dialog.show()

        self.csv_elements = []

        self.csv_generator_object = CSVGeneratorObject(self)

        self.vector_header_dict = {}

        self.pool = QThreadPool()

        self.finished_submissions = False

        self.lock = Lock()

    def generate_csv(self):
        # dates
        now = datetime.now()
        self.end_date = now
        self.begin_date = now - timedelta(days=self.days_to_query)

        current_x = self.left
        current_y = self.bottom

        serial_no = 1

        # get header dict
        vector_query = VectorQuery(username=self.username,
                                   password=self.password,
                                   client_id=self.client_id,
                                   client_secret=self.client_secret)
        vector_query.log_in()
        vector_params = InfoCubeVectorParams(top=self.top,
                                             bottom=self.bottom,
                                             left=self.left,
                                             right=self.right,
                                             time_begin=self.begin_date,
                                             time_end=self.end_date)
        header_result = vector_query.get_vector_result(vector_params)
        self.vector_header_dict = vector_query.get_vector_data(header_result)

        for next_x in drange(self.left + INCREMENTAL_INTERVAL, self.right,
                             INCREMENTAL_INTERVAL):
            for next_y in drange(self.bottom + INCREMENTAL_INTERVAL, self.top,
                                 INCREMENTAL_INTERVAL):

                username = self.username
                password = self.password
                client_id = self.client_id
                client_secret = self.client_secret

                csv_runnable = CSVRunnable(username, password, client_id,
                                           client_secret, serial_no, next_y,
                                           current_x, next_x, current_y,
                                           self.begin_date, self.end_date,
                                           self.vector_header_dict)
                csv_runnable.csv_object.new_csv_element.connect(
                    self.csv_generator_object.callback)
                self.pool.start(csv_runnable)

                serial_no += 1
                current_y = next_y

            current_y = self.bottom
            current_x = next_x

        self.finished_submissions = True

    def on_completion(self):
        self.csv_elements.sort(key=lambda element: element.serial_no)
        log.info("Sort complete")
        # write file
        csv_file = open(self.csv_filename, 'w')

        # write the header
        header = CSVOutput.get_csv_header()
        if self.vector_header_dict:
            for term in self.vector_header_dict:
                header = header + str(term) + ","
        header = header[:-1]
        csv_file.write(header)
        csv_file.write("\n")

        for csv_element in self.csv_elements:
            csv_file.write(str(csv_element))
            csv_file.write("\n")

        csv_file.close()
        log.info("Write complete")

        if self.progress_dialog:
            self.progress_dialog.close()

        self.csv_generator_object.message_complete.emit(self.csv_filename)
Example #47
0
    def __init__(self, font, parent=None):
        super(MainWindow, self).__init__(parent)
        self.font = font
        self.setWindowTitle("Meridien")
        central_widget = QtGui.QWidget(self)
        self.setCentralWidget(central_widget)

        #Center on screen
        resolution = QtGui.QDesktopWidget().screenGeometry()
        self.move((resolution.width() / 2) - (self.frameSize().width() / 2),
                  (resolution.height() / 2) - (self.frameSize().height() / 2))
        """
        Setting up menu bar
        """

        close_action = QtGui.QAction('Close', self)
        close_action.setShortcut("Ctrl+Q")
        close_action.setStatusTip('Leave the app')
        close_action.triggered.connect(lambda: self.close())

        open_refinement_folder = QtGui.QAction('Open Refinement Folder', self)
        open_refinement_folder.triggered.connect(self.open_refinement_folder)

        self.mainMenu = self.menuBar()
        self.fileMenu = self.mainMenu.addMenu('&File')
        self.fileMenu.addAction(open_refinement_folder)
        self.fileMenu.addAction(close_action)
        self.refinement_folder = ""

        create_new_fsc_plot = QtGui.QAction('&New FSC plot', self)
        create_new_fsc_plot.triggered.connect(
            self.event_ontriggered_show_fsc_plot)

        create_new_overview_plot = QtGui.QAction(
            '&New resolution overview plot', self)
        create_new_overview_plot.triggered.connect(
            self.event_show_resolution_overview_plot)
        self.plotMenu = self.mainMenu.addMenu('&Plot')
        self.plotMenu.addAction(create_new_fsc_plot)
        self.plotMenu.addAction(create_new_overview_plot)
        """
        Setup other components
        """
        self.layout = QGridLayout(central_widget)
        self.setMenuBar(self.mainMenu)

        self.tree = QTreeWidget(self)
        self.tree.setHeaderHidden(True)
        self.layout.addWidget(self.tree, 1, 0)

        self.root_items_path_dictionary = {}

        # Threads
        self.threadpool = QThreadPool()
        self.thread_list = []
        thr = QThread(self)
        thr.start()

        self.reader = DriverFileReader()
        self.reader.moveToThread(thr)
        self.thread_list.append(thr)
        self.timer = QTimer(self)

        # Connect signals
        self.reader.sig_sendfolders.connect(self.fill_tree)
        self.reader.sig_sendfsc.connect(self.show_dialog_fsc)
        self.tree.itemChanged.connect(self._event_select_deselect_all)
        self.sig_update_tree.connect(self.update_tree)
        self.sig_show_overview_plot.connect(
            self.event_show_resolution_overview_plot)
        self.show()
        self.open_refinement_folder()

        self.monitor = None
 def tearDownClass(cls):
     TestQgsPalLabeling.tearDownClass()
     cls.removeMapLayer(cls.layer)
     cls.layer = None
     # avoid crash on finish, probably related to https://bugreports.qt.io/browse/QTBUG-35760
     QThreadPool.globalInstance().waitForDone()
 def tearDownClass(cls):
     TestQgsPalLabeling.tearDownClass()
     # avoid crash on finish, probably related to https://bugreports.qt.io/browse/QTBUG-35760
     QThreadPool.globalInstance().waitForDone()
Example #50
0
    def __init__(self, ):
        # Consider using multiple QStringListModels
        # QVXYModelMapper can map columns or rows to an xy-model

        super(Main, self).__init__()

        dpiX = self.logicalDpiX()

        self.setupUi(self)

        self.action_Exit.triggered.connect(self.close)

        self.modelShape = TransducerShapeModel([
            "Linear", "Linear (focused)", "Curvelinear",
            "Curvelinear (focused)"
        ])

        # TODO: connect to model, which emits updated
        #self.cboxElePlacement.view().selectionModel().selectionChanged.connect(self.modelElevationPlacement.update)

        # TODO: update parameter, which is used for selecting inner or output
        #self.modelElevationPlacement.updated.connect(self.proxy.on_elevation_placement_select)

        self.cboxShape.setModel(self.modelShape)
        self.cboxShape.view().selectionModel().selectionChanged.connect(
            self.modelShape.test)  # model emits select

        # TODO: How to use a model for a comboBox???
        #self.modelElevationPlacement = ElevationPlacementModel(["Inner", "Outer"])
        #self.cboxElePlacement.setModel(self.modelElevationPlacement)
        #self.cboxElePlacement.view().selectionModel().selectionChanged.connect(self.update_image)

        # Temporary solution without model
        self.cboxElePlacement.setCurrentIndex(0)
        self.cboxElePlacement.currentIndexChanged[QString].connect(
            self.on_cbox_ele_placement_changed)

        self.delegate = TransducerElementDelegate(self)

        # Baffle is disabled for CW (default)
        self.cboxBaffle.setDisabled(1)

        # CW defaults
        defaults = [128, 3e-3, 5.0e-4, 3.5e-3, 61e-3, 5, 50e-3, 8.5e-2]
        # PW defaults
        defaults = [64, 1.8e-4, 2e-5, 2e-4, 61e-3, 5, 20e-3, 1.5e-2]
        self.model = TransducerElementModel(defaults, self)

        self.proxy = TransducerElementModelProxy(self)
        self.proxy.setSourceModel(self.model)

        self.modelShape.select.connect(
            self.proxy.on_transducer_shape_select)  # select triggers update

        self.tblElements.setModel(self.proxy)
        self.tblElements.setItemDelegate(self.delegate)

        self.tblElements.horizontalHeader().setResizeMode(
            QtGui.QHeaderView.Stretch)  #QtGui.QHeaderView.ResizeToContents
        self.tblElements.verticalHeader().setResizeMode(
            QtGui.QHeaderView.Fixed)

        self.tblElements.setVisible(False)
        self.tblElements.resizeColumnsToContents()
        self.tblElements.resizeRowsToContents()
        self.tblElements.setVisible(True)

        self.pool = QThreadPool.globalInstance()
        self.pool.setMaxThreadCount(4)
        self.active = False

        self.proxy.dataChanged.connect(self.update_image)
        #self.modelElevationPlacement.dataChanged.connect(self.update_image)
        #self.modelElevationPlacement.updated.connect(self.update_image)

        self.cboxShape.view().selectionModel().selectionChanged.connect(
            self.update_image)

        # TODO: Make model work
        #self.propModel = PropagatorModel(["Point", "Plane", "Sphere"], self)
        #self.propModel = PropagatorModel(["Sphere"], self)
        #self.cboxPropagator.setModel(self.propModel)

        self.sboxOrder.setValue(40)
        self.sboxOrder.setMinimum(2)
        self.sboxOrder.setMaximum(500)

        self.cboxDomain.setCurrentIndex(0)
        self.cboxDomain.currentIndexChanged[QString].connect(
            self.on_cbox_domain_changed)
        self.cboxDomain.setDisabled(True)
        self.leSampleFrq.setDisabled(True)
        self.leExcitationCycles.setDisabled(True)

        self.sboxOrder.valueChanged[int].connect(self.on_sbox_order_changed)

        # CW grid
        data = [[170, 1, 250], [0, 0, 125], [0.004, 0.1, 0.0036],
                [-0.338, 0.00, 0.0018], [0.3380, 0.00, 0.8982]]
        # PW grid
        data = [[100, 1, 100], [0, 0, 60], [6e-5, 0.10, 4e-4],
                [-0.003, 0.00, 0.00], [0.003, 0.00, 0.04]]

        self.gridXYZ = GridTableModel(data, ['X', 'Y', 'Z'],
                                      ['N', 'offset', 'delta', 'min', 'max'],
                                      self)

        self.tblGrid.setModel(self.gridXYZ)
        self.tblGrid.setItemDelegate(self.delegate)

        self.tblGrid.horizontalHeader().setResizeMode(
            QtGui.QHeaderView.Stretch)
        self.tblGrid.verticalHeader().setResizeMode(QtGui.QHeaderView.Fixed)

        self.tblGrid.setVisible(False)
        self.tblGrid.resizeColumnsToContents()
        self.tblGrid.resizeRowsToContents()

        self.gridDelegate = GridElementDelegate(self)
        self.readOnlyDelegate = ReadOnlyDelegate(self)
        self.tblGrid.setItemDelegateForRow(0, self.gridDelegate)
        self.tblGrid.setItemDelegateForRow(1, self.gridDelegate)
        self.tblGrid.setItemDelegateForRow(2, self.gridDelegate)
        self.tblGrid.setItemDelegateForRow(3, self.readOnlyDelegate)
        self.tblGrid.setItemDelegateForRow(4, self.readOnlyDelegate)
        self.tblGrid.setVisible(True)

        self.progressBar.setRange(0, 100)

        self.leFNumber.setAlignment(Qt.AlignRight)
        self.btnField.clicked.connect(self.on_calc_clicked)
class GridZoneGeneratorDialog(QtGui.QDialog, FORM_CLASS):
    def __init__(self, iface, parent=None):
        """Constructor."""
        super(GridZoneGeneratorDialog, self).__init__(parent)
        # Set up the user interface from Designer.
        # After setupUI you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        self.setupUi(self)
        self.iface = iface

        self.crsLineEdit.setReadOnly(True)
        
        self.utmgrid = UtmGrid()

        self.setValidCharacters()

        self.setMask()

        self.threadpool = QThreadPool()

    def setValidCharacters(self):
        self.chars = []

        chars = 'NS'
        self.chars.append(chars)
        chars = 'ABCDEFGHIJKLMNOPQRSTUVZ'
        self.chars.append(chars)
        chars = ['01','02','03','04','05','06','07','08','09','10',
                   '11','12','13','14','15','16','17','18','19','20',
                   '21','22','23','24','25','26','27','28','29','30',
                   '31','32','33','34','35','36','37','38','39','40',
                   '41','42','43','44','45','46','47','48','49','50',
                   '51','52','53','54','55','56','57','58','59','60']
        self.chars.append(chars)
        chars = 'VXYZ'
        self.chars.append(chars)
        chars = 'ABCD'
        self.chars.append(chars)
        chars = ['I','II','III','IV','V','VI']
        self.chars.append(chars)
        chars = '1234'
        self.chars.append(chars)
        chars = ['NO','NE','SO','SE']
        self.chars.append(chars)
        chars = 'ABCDEF'
        self.chars.append(chars)
        chars = ['I','II','III','IV']
        self.chars.append(chars)
        chars = '123456'
        self.chars.append(chars)
        chars = 'ABCD'
        self.chars.append(chars)
        
    def setMask(self):
        if self.scaleCombo.currentText() == '1000k':
            self.indexLineEdit.setInputMask('NN-NN')
        elif self.scaleCombo.currentText() == '500k':
            self.indexLineEdit.setInputMask('NN-NN-N')
        elif self.scaleCombo.currentText() == '250k':
            self.indexLineEdit.setInputMask('NN-NN-N-N')
        elif self.scaleCombo.currentText() == '100k':
            self.indexLineEdit.setInputMask('NN-NN-N-N-Nnn')
        elif self.scaleCombo.currentText() == '50k':
            self.indexLineEdit.setInputMask('NN-NN-N-N-Nnn-0')
        elif self.scaleCombo.currentText() == '25k':
            self.indexLineEdit.setInputMask('NN-NN-N-N-Nnn-0-NN')
        elif self.scaleCombo.currentText() == '10k':
            self.indexLineEdit.setInputMask('NN-NN-N-N-Nnn-0-NN-N')
        elif self.scaleCombo.currentText() == '5k':
            self.indexLineEdit.setInputMask('NN-NN-N-N-Nnn-0-NN-N-Nnn')
        elif self.scaleCombo.currentText() == '2k':
            self.indexLineEdit.setInputMask('NN-NN-N-N-Nnn-0-NN-N-Nnn-0')
        elif self.scaleCombo.currentText() == '1k':
            self.indexLineEdit.setInputMask('NN-NN-N-N-Nnn-0-NN-N-Nnn-0-N')
            
    def validateMI(self):
        mi = self.indexLineEdit.text()
        split = mi.split('-')
        for i in range(len(split)):
            word = str(split[i])
            if len(word) == 0:
                return False
            if i == 0:
                if word[0] not in self.chars[0]:
                    print word
                    return False
                if word[1] not in self.chars[1]:
                    print word
                    return False
            elif i == 1:
                if word not in self.chars[2]:
                    print word
                    return False
            elif i == 2:
                if word not in self.chars[3]:
                    print word
                    return False
            elif i == 3:
                if word not in self.chars[4]:
                    print word
                    return False
            elif i == 4:
                if word not in self.chars[5]:
                    print word
                    return False
            elif i == 5:
                if word not in self.chars[6]:
                    print word
                    return False
            elif i == 6:
                if word not in self.chars[7]:
                    print word
                    return False
            elif i == 7:
                if word not in self.chars[8]:
                    print word
                    return False
            elif i == 8:
                if word not in self.chars[9]:
                    print word
                    return False
            elif i == 9:
                if word not in self.chars[10]:
                    print word
                    return False
            elif i == 10:
                if word not in self.chars[11]:
                    print word
                    return False
        return True
    
    def disableAll(self):
        self.mirLineEdit.setEnabled(False)
        self.miLineEdit.setEnabled(False)
        self.indexLineEdit.setEnabled(False)

    @pyqtSlot(bool)
    def on_crsButton_clicked(self):
        projSelector = QgsGenericProjectionSelector()
        message = 'Select the Spatial Reference System!'
        projSelector.setMessage(theMessage=message)
        projSelector.exec_()
        try:
            epsg = int(projSelector.selectedAuthId().split(':')[-1])
            self.crs = QgsCoordinateReferenceSystem(epsg, QgsCoordinateReferenceSystem.EpsgCrsId)
            if self.crs:
                self.crsLineEdit.setText(self.crs.description())
        except:
            QMessageBox.warning(self, self.tr("Warning!"), self.tr(message))

    @pyqtSlot(str)
    def on_indexLineEdit_textEdited(self,s):
        if (s!=''):
            mi = self.utmgrid.getMIFromINomen(str(s))
            self.miLineEdit.setText(mi)

    @pyqtSlot(str)
    def on_miLineEdit_textEdited(self,s):
        if (s!=''):
            self.index = self.utmgrid.getINomenFromMI(str(s))
            self.indexLineEdit.setText(self.index)

    @pyqtSlot(str)
    def on_mirLineEdit_textEdited(self,s):
        if (s!=''):
            self.index = self.utmgrid.getINomenFromMIR(str(s))
            self.indexLineEdit.setText(self.index)

    @pyqtSlot(int)
    def on_scaleCombo_currentIndexChanged(self):
        self.setMask()

    @pyqtSlot(bool)
    def on_mirRadioButton_toggled(self, toggled):
        if toggled:
            self.mirLineEdit.setEnabled(True)
        else:
            self.mirLineEdit.setEnabled(False)

    @pyqtSlot(bool)
    def on_miRadioButton_toggled(self, toggled):
        if toggled:
            self.miLineEdit.setEnabled(True)
        else:
            self.miLineEdit.setEnabled(False)

    @pyqtSlot(bool)
    def on_indexRadioButton_toggled(self, toggled):
        if toggled:
            self.indexLineEdit.setEnabled(True)
        else:
            self.indexLineEdit.setEnabled(False)

    @pyqtSlot(bool)
    def on_saveButton_clicked(self):
        fileName = QFileDialog.getSaveFileName(parent=self, caption=self.tr('Save Output File'), filter='Shapefile (*.shp)')
        self.saveEdit.setText(fileName)
      
    @pyqtSlot()        
    def on_button_box_accepted(self):
        stopScale = int(self.stopScaleCombo.currentText().replace('k','')) 
        scale = int(self.scaleCombo.currentText().replace('k',''))
    
        if stopScale > scale:
            QMessageBox.warning(self, self.tr('Warning!'), self.tr('The stop scale denominator should not be bigger than \
                                                                    the scale denominator!'))
            return
        if self.createShapeBox.isChecked() and not self.saveEdit.text():
            QMessageBox.warning(self, self.tr('Warning!'), self.tr('A output file must be specified!'))
            return
        if not self.crsLineEdit.text():
            QMessageBox.warning(self, self.tr('Warning!'), self.tr('A CRS must be specified!'))
            return
        if not self.validateMI():
            QMessageBox.warning(self, self.tr('Warning!'), self.tr('Invalid Map Index!'))            
            return
        
        # Initiating processing
        gridThread = UtmGrid()
        
        if self.createShapeBox.isChecked():
            self.layer = None
        else:
            self.layer = gridThread.createGridLayer(self.tr('Grid Zones'), 'Multipolygon', self.crs.authid())
            
        gridThread.setParameters(self.indexLineEdit.text(), stopScale, self.miLineEdit.text(), self.crs, self.saveEdit.text(), self.layer)
        # Connecting end signal
        gridThread.aux.processFinished.connect(self.finishProcess)
        gridThread.aux.rangeCalculated.connect(self.setRange)
        gridThread.aux.errorOccurred.connect(self.showError)
        gridThread.aux.stepProcessed.connect(self.updateProgress)
        # Setting the progress bar
        self.progressMessageBar = self.iface.messageBar().createMessage(self.tr('Generating grid, please wait...'))
        self.progressBar = QtGui.QProgressBar()
        self.progressBar.setAlignment(Qt.AlignLeft|Qt.AlignVCenter)
        self.progressMessageBar.layout().addWidget(self.progressBar)
        self.iface.messageBar().pushWidget(self.progressMessageBar, self.iface.messageBar().INFO)
        self.progressBar.setRange(0, 0)
        self.progressMessageBar.destroyed.connect(gridThread.aux.cancel)
        # Starting process
        self.threadpool.start(gridThread)
        
    def setRange(self, maximum):
        self.progressBar.setRange(0, maximum)
        
    def showError(self, msg):
        QMessageBox.warning(self, self.tr('Warning!'), msg)
        
    def updateProgress(self):
        self.progressBar.setValue(self.progressBar.value() + 1)
        
    def finishProcess(self):
        self.progressBar.setValue(self.progressBar.maximum())
        
        if self.createShapeBox.isChecked():
            layer = self.iface.addVectorLayer(self.saveEdit.text(), self.tr('Grid Zones'), 'ogr')
            self.updateMapCanvas(layer)
        else:
            QgsMapLayerRegistry.instance().addMapLayer(self.layer)
            self.updateMapCanvas(self.layer)
            
        QMessageBox.information(self, self.tr('Success!'), self.tr('Grid Created successfully!'))
        
    def updateMapCanvas(self, layer):
        if layer:
            self.iface.setActiveLayer(layer)
            
        layer.updateExtents() 
        
        bbox = self.iface.mapCanvas().mapSettings().layerToMapCoordinates(layer, layer.extent())
        self.iface.mapCanvas().setExtent(bbox)
        self.iface.mapCanvas().refresh()
        
Example #52
0
class Validation:
    def __init__(self, iface, db, validation_dia, plugin_dir, params):
        self.iface = iface
        self.db = db
        self.params = params
        self.validation_dia = validation_dia
        self.app_root = plugin_dir
        self.open_image = QPixmap(os.path.join(self.app_root,
                                               "image",
                                               "folder_open_icon.png"))
        self.validation_dia.ui.openPushButton.setIcon(QIcon(self.open_image))
        self.validation_dia.ui.openPushButton.setToolTip("Select File")
        self.export_globals = None
        self.validation_dk = None
        self.report_to_dialog = None
        self.re = QRegExp("CheckBox")
        self.check_boxes_names = []
        self.long_task = QThreadPool(None).globalInstance()
        self.summary_tables = {}
        self.model_navigation()
        self.form_load()
        self.file_dialog = QFileDialog()
        self.summary_functions = {}
        self.report_file_path = None
        self.home_dir = os.path.expanduser('~')
        self.org_name = database.get_from_gaz_metadata(db, "owner")
        self.report = ExportValidationReport("roadNet Validation Report",
                                             self.org_name,
                                             self.db, self.iface, None)
        self.list_check_boxes = []
        self.progress_win = QProgressDialog("", None, 0, 100, self.validation_dia)
        self.progress_win.setFixedSize(380, 100)
        self.progress_win.setModal(True)
        self.progress_win.setWindowTitle("Export Validation Report")

        self.summary_runnables = {'dupStreetCheckBox': [lambda: DupStreetDesc(), 0],
                                  'notStreetEsuCheckBox': [lambda: StreetsNoEsuDesc(), 1],
                                  'notType3CheckBox': [lambda: Type3Desc(False), 2],
                                  'incFootPathCheckBox': [lambda: Type3Desc(True), 2],
                                  'dupEsuRefCheckBox': [lambda: DupEsuRef(True), 3],
                                  'notEsuStreetCheckBox': [lambda: NoLinkEsuStreets(), 4],
                                  'invCrossRefCheckBox': [lambda: InvalidCrossReferences()],
                                  'startEndCheckBox': [lambda: CheckStartEnd(), 8],
                                  'tinyEsuCheckBox': [lambda: CheckTinyEsus("esu", 1)],
                                  'notMaintReinsCheckBox': [lambda: CheckMaintReins()],
                                  'asdStartEndCheckBox': [lambda: CheckAsdCoords()],
                                  'notMaintPolysCheckBox': [lambda: MaintNoPoly(), 16],
                                  'notPolysMaintCheckBox': [lambda: PolyNoMaint()],
                                  'tinyPolysCheckBox': [lambda: CheckTinyEsus("rd_poly", 1)]}

    def init_functions(self, ref_class, tolerance):
        """
        initialise a dictionary which keys are names of check boxes
        and values are all functions that create the respective table on the screen report
        functions are lambda because they will be called upon report creation and
        int references to a dictionary in the validation_summary
        class to the associated table widget to populate
        :param ref_class: class, the class holding the relative function
        :return: void
        """

        self.summary_functions = {'dupStreetCheckBox': [lambda: ref_class.dup_street_desc(), 0],
                                  'notStreetEsuCheckBox': [lambda: ref_class.street_not_esu_desc(), 1],
                                  'notType3CheckBox': [lambda: ref_class.no_type3_desc(include_footpath=False), 2],
                                  'incFootPathCheckBox': [lambda: ref_class.no_type3_desc(include_footpath=True), 2],
                                  'dupEsuRefCheckBox': [lambda: ref_class.dup_esu_ref(), 3],
                                  'notEsuStreetCheckBox': [lambda: ref_class.no_link_esu_streets(), 4],
                                  'invCrossRefCheckBox': [lambda: ref_class.invalid_cross_references()],
                                  'startEndCheckBox': [lambda: ref_class.check_start_end(tolerance), 8],
                                  'tinyEsuCheckBox': [lambda: ref_class.check_tiny_esus("esu", 1)],
                                  'notMaintReinsCheckBox': [lambda: ref_class.check_maint_reinst()],
                                  'asdStartEndCheckBox': [lambda: ref_class.check_asd_coords()],
                                  'notMaintPolysCheckBox': [lambda: ref_class.maint_no_poly(), 16],
                                  'notPolysMaintCheckBox': [lambda: ref_class.poly_no_maint()],
                                  'tinyPolysCheckBox': [lambda: ref_class.check_tiny_esus("rd_poly", 1)]}

    def model_navigation(self):
        """
        events handler for buttons in the form
        :return: object
        """
        buttons = self.validation_dia.ui.okCancelButtons.buttons()
        buttons[0].clicked.connect(self.get_data)
        buttons[1].clicked.connect(self.close_browser)
        self.validation_dia.ui.openPushButton.clicked.connect(self.select_file)
        self.validation_dia.ui.screenRadioButton.toggled.connect(lambda: self.selection_handler(0))
        self.validation_dia.ui.fileRadioButton.toggled.connect(lambda: self.selection_handler(1))
        self.validation_dia.ui.notType3CheckBox.toggled.connect(self.check_no_type_click)
        self.validation_dia.ui.startEndCheckBox.toggled.connect(self.check_coords_click)
        self.validation_dia.ui.selectAllButton.clicked.connect(self.select_all)
        self.validation_dia.ui.clearAllButton.clicked.connect(self.clear_all)

    def form_load(self):
        # set the initial status of the form
        self.validation_dia.ui.screenRadioButton.setChecked(True)
        self.validation_dia.ui.filePathLineEdit.setEnabled(False)
        self.check_coords_click()
        self.check_no_type_click()
        self.validation_dia.ui.dupStreetCheckBox.setChecked(True)
        self.validation_dia.ui.notStreetEsuCheckBox.setChecked(True)
        self.validation_dia.ui.notType3CheckBox.setChecked(True)
        self.validation_dia.ui.dupEsuRefCheckBox.setChecked(True)
        self.validation_dia.ui.notEsuStreetCheckBox.setChecked(True)
        self.validation_dia.ui.invCrossRefCheckBox.setChecked(True)
        self.validation_dia.ui.startEndCheckBox.setChecked(False)
        self.validation_dia.ui.tinyEsuCheckBox.setChecked(True)
        if self.params['RNsrwr'].lower() == 'true':
            self.validation_dia.ui.notMaintReinsCheckBox.setChecked(True)
            self.validation_dia.ui.asdStartEndCheckBox.setChecked(True)
        if self.params['RNsrwr'].lower() == 'false':
            self.validation_dia.ui.notMaintReinsCheckBox.setChecked(False)
            self.validation_dia.ui.asdStartEndCheckBox.setChecked(False)
            self.validation_dia.ui.notMaintReinsCheckBox.setEnabled(False)
            self.validation_dia.ui.asdStartEndCheckBox.setEnabled(False)
        if self.params['RNPolyEdit'].lower() == 'true':
            self.validation_dia.ui.notMaintPolysCheckBox.setChecked(True)
            self.validation_dia.ui.notPolysMaintCheckBox.setChecked(True)
            self.validation_dia.ui.tinyPolysCheckBox.setChecked(True)
        if self.params['RNPolyEdit'].lower() == 'false':
            self.validation_dia.ui.notMaintPolysCheckBox.setChecked(False)
            self.validation_dia.ui.notPolysMaintCheckBox.setChecked(False)
            self.validation_dia.ui.tinyPolysCheckBox.setChecked(False)
            self.validation_dia.ui.notMaintPolysCheckBox.setEnabled(False)
            self.validation_dia.ui.notPolysMaintCheckBox.setEnabled(False)
            self.validation_dia.ui.tinyPolysCheckBox.setEnabled(False)

    def close_browser(self):
        # close the dialog
        self.validation_dia.close()

    def get_data(self):
        """
        produce the report according to the options specified by the user
        :return: validation report either on screen or as text file
        """
        if self.validation_dia.ui.fileRadioButton.isChecked():
            # alert the user if no path is specified
            if self.validation_dia.ui.filePathLineEdit.text() == "":
                no_path_msg_box = QMessageBox(QMessageBox.Warning, " ",
                                              "You must specify a path and filename for the report",
                                              QMessageBox.Ok, None)
                no_path_msg_box.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowTitleHint)
                no_path_msg_box.exec_()
                return
            # format the path and runs the export
            val_file_path = self.validation_dia.ui.filePathLineEdit.text()
            # checks if the export directory exists and it is valid
            if not os.path.isdir(os.path.dirname(val_file_path)):
                path_invalid_msg_box = QMessageBox(QMessageBox.Warning, " ", "A valid directory must be selected",
                                                   QMessageBox.Ok, None)
                path_invalid_msg_box.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowTitleHint)
                path_invalid_msg_box.exec_()
            else:
                self.report_to_file(val_file_path)
            # text file report = false, create a screen report, instantiate the report creator class
        if not self.validation_dia.ui.fileRadioButton.isChecked():
            self.report_to_screen()

    def select_file(self):
        """
        open the dialog window to select the file and print the path on the main line edit
        :return: void
        """
        self.file_dialog.setDirectory(self.home_dir)
        self.file_dialog.setFileMode(QFileDialog.ExistingFiles)
        filters = "Text files (*.txt)"
        self.file_dialog.setNameFilter(filters)
        save_file_name = self.file_dialog.getSaveFileName(self.file_dialog, "Export Validation Report",
                                                          self.home_dir,
                                                          filter="Text files (*.txt)")
        if save_file_name != "":
            self.validation_dia.ui.filePathLineEdit.setText(("{}.txt".format(save_file_name)))
        if save_file_name.endswith(".txt"):
            self.validation_dia.ui.filePathLineEdit.setText(save_file_name)

    def selection_handler(self, button_id):
        """
        change the status of the line edit according to the radio button selection
        :return:
        """
        # if screen report is selected disable line edit for the file path
        if button_id == 0:
            self.validation_dia.ui.filePathLineEdit.setEnabled(False)
            self.validation_dia.ui.openPushButton.setEnabled(False)
            self.check_boxes_names = []

        else:
            self.validation_dia.ui.filePathLineEdit.setEnabled(True)
            self.validation_dia.ui.openPushButton.setEnabled(True)
            self.check_boxes_names = []

    def check_coords_click(self):
        # show/hide tolerance metres spin box
        if self.validation_dia.ui.startEndCheckBox.isChecked():
            self.validation_dia.ui.metresSpinBox.setVisible(True)
            self.validation_dia.ui.toleranceLabel.setVisible(True)
        else:
            self.validation_dia.ui.metresSpinBox.setVisible(False)
            self.validation_dia.ui.toleranceLabel.setVisible(False)

    def check_no_type_click(self):
        # enable/disable footpath check box
        if self.validation_dia.ui.notType3CheckBox.isChecked():
            self.validation_dia.ui.incFootPathCheckBox.setEnabled(True)
        else:
            self.validation_dia.ui.notType3CheckBox.setChecked(False)
            self.validation_dia.ui.incFootPathCheckBox.setEnabled(False)

    def select_all(self):
        # reset the form to default status, select all
        self.form_load()

    def clear_all(self):
        # uncheck all checkboxes
        self.validation_dia.ui.dupStreetCheckBox.setChecked(False)
        self.validation_dia.ui.notStreetEsuCheckBox.setChecked(False)
        self.validation_dia.ui.notType3CheckBox.setChecked(False)
        self.validation_dia.ui.incFootPathCheckBox.setChecked(False)
        self.validation_dia.ui.incFootPathCheckBox.setEnabled(False)
        self.validation_dia.ui.dupEsuRefCheckBox.setChecked(False)
        self.validation_dia.ui.notEsuStreetCheckBox.setChecked(False)
        self.validation_dia.ui.invCrossRefCheckBox.setChecked(False)
        self.validation_dia.ui.startEndCheckBox.setChecked(False)
        self.validation_dia.ui.tinyEsuCheckBox.setChecked(False)
        self.validation_dia.ui.notMaintReinsCheckBox.setChecked(False)
        self.validation_dia.ui.asdStartEndCheckBox.setChecked(False)
        self.validation_dia.ui.notMaintPolysCheckBox.setChecked(False)
        self.validation_dia.ui.notPolysMaintCheckBox.setChecked(False)
        self.validation_dia.ui.tinyPolysCheckBox.setChecked(False)

    def report_to_file(self, val_file_path):
        """
        creates a text file report
        :return: void
        """
        # assign to the report class the file path property
        self.report_file_path = val_file_path
        # start writing
        # get all checked check-boxes
        if len(self.list_check_boxes) > 0:
            self.list_check_boxes = []
        self.list_check_boxes = self.validation_dia.findChildren(QCheckBox, self.re)
        if len(self.check_boxes_names) > 0:
            self.check_boxes_names = []
        for check_box in self.list_check_boxes:
            if check_box.isChecked():
                self.check_boxes_names.append(str(check_box.objectName()))
        if len(self.check_boxes_names) < 1:
            no_val_check_msg_box = QMessageBox(QMessageBox.Warning, " ",
                                               "At least one validation option must be selected", QMessageBox.Ok, None)
            no_val_check_msg_box.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowTitleHint)
            no_val_check_msg_box.exec_()
            return
        tolerance = self.validation_dia.ui.metresSpinBox.value()
        if self.validation_dia.ui.startEndCheckBox.isChecked() and (tolerance is None or tolerance == 0):
            no_tol_msg_box_file = QMessageBox(QMessageBox.Warning, " ", "You must specify a tolerance",
                                              QMessageBox.Ok, None)
            no_tol_msg_box_file.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowTitleHint)
            no_tol_msg_box_file.exec_()
            return
        self.progress_win.setWindowTitle("Export Validation Report")
        self.progress_win.show()
        if self.validation_dia.ui.notType3CheckBox.isChecked():
            if 'incFootPathCheckBox' in self.check_boxes_names:
                # locate the include footpath option and run just the appropriate function
                self.check_boxes_names.remove('notType3CheckBox')
        self.export_globals = InitGlobals(self.db, self.params, tolerance)
        self.long_task.setMaxThreadCount(1)
        start_report = StartReport(val_file_path, self.org_name)
        start_report.signals.result.connect(self.log_progress)
        end_report = EndReport()
        end_report.signals.result.connect(self.log_progress)
        end_report.signals.report_finished.connect(self.show_finished)
        self.long_task.start(start_report)
        for check_box_name in self.check_boxes_names:
            run_class = self.summary_runnables[check_box_name]
            runnable = run_class[0]()
            runnable.signals.result.connect(self.log_progress)
            self.long_task.start(runnable)
        self.long_task.start(end_report)

        self.list_check_boxes = []
        self.check_boxes_names = []

    @pyqtSlot()
    def show_finished(self):
        self.long_task.waitForDone()
        show_finished_msg_box = QMessageBox(QMessageBox.Information, " ",
                                            "Report successfully exported at \n {0}"
                                            .format(str(self.report_file_path))
                                            .replace("\\\\", "\\"), QMessageBox.Ok, None)
        show_finished_msg_box.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowTitleHint)
        show_finished_msg_box.exec_()

    @pyqtSlot(str, int)
    def log_progress(self, task, value):
        self.progress_win.setLabelText(task)
        self.progress_win.setValue(value)

    def report_to_screen(self):
        tolerance = self.validation_dia.ui.metresSpinBox.value()
        if self.validation_dia.ui.startEndCheckBox.isChecked() and (tolerance is None or tolerance == 0):
            no_tol_msg_box_screen = QMessageBox(QMessageBox.Warning, " ", "You must specify a tolerance",
                                                QMessageBox.Ok, None)
            no_tol_msg_box_screen.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowTitleHint)
            no_tol_msg_box_screen.exec_()
            return
        report_to_screen = self.report
        report_to_screen.validation_dia = self.validation_dia
        # in case the user selects a screen report immediately after the generation of
        # a file report create a new instance of the report class and None the file path
        # instantiate the report window, the parent is the main validation report dialog
        if self.validation_dk is None:
            self.validation_dk = ValidationSummaryDock(self.validation_dia)
            self.validation_dk.setWindowTitle("Validation Report Summary")
            self.validation_dk.setWindowFlags(Qt.WindowMaximizeButtonHint | Qt.WindowMinimizeButtonHint)
            rn_icon = QIcon()
            rn_icon.addPixmap(QPixmap(os.path.join(self.app_root,
                                                         "image",
                                                         "rn_logo_v2.png")))
            self.validation_dk.setWindowIcon(rn_icon)
        # instantiate the class handler (functions to create and format the screen report)
        # include the window in the instantiation
        report_to_dialog = ValidationSummary(self.validation_dk, self.iface, self.db, tolerance)
        # creates a list of the checked checkboxes to create the tables
        self.list_check_boxes = self.validation_dia.findChildren(QCheckBox, self.re)
        for check_box in self.list_check_boxes:
            if check_box.isChecked():
                self.check_boxes_names.append(str(check_box.objectName()))
        # check if at least one checkbox is checked before running the report
        if len(self.check_boxes_names) < 1:
            no_val_check_msg_box = QMessageBox(QMessageBox.Warning, " ",
                                               "At least one validation option must be selected", QMessageBox.Ok, None)
            no_val_check_msg_box.setWindowFlags(Qt.CustomizeWindowHint | Qt.WindowTitleHint)
            no_val_check_msg_box.exec_()
            return
        # initialises the functions
        self.init_functions(report_to_screen, tolerance)
        # runs the functions, each function passes a list and a table reference
        # to the handler class methods that create, populate and finally show the window
        if self.validation_dia.ui.notType3CheckBox.isChecked():
            if 'incFootPathCheckBox' in self.check_boxes_names:
                # locate the include footpath option and run just the appropriate function
                self.check_boxes_names.remove('notType3CheckBox')
                report_to_dialog.include_footpath = True
        self.report.start_report()
        for check_box_name in self.check_boxes_names:
            function = self.summary_functions[check_box_name][0]
            # handles multiple results for a unique check box (cross references)
            if check_box_name == 'invCrossRefCheckBox':
                function_list = function()
                report_to_dialog.set_table(function_list[0], 5)
                report_to_dialog.set_table(function_list[1], 6)
                report_to_dialog.set_table(function_list[2], 7)
            # handles multiple results for a unique check box (tiny/empty ESUs Polys)
            elif check_box_name == 'tinyEsuCheckBox':
                function_list = function()
                report_to_dialog.set_table(function_list[0], 9)
                report_to_dialog.set_table(function_list[1], 10)
            elif check_box_name == 'tinyPolysCheckBox':
                function_list = function()
                report_to_dialog.set_table(function_list[0], 19)
                report_to_dialog.set_table(function_list[1], 20)
            # handles multiple results for a unique check box (maint/reins asd streets)
            elif check_box_name == 'notMaintReinsCheckBox':
                function_list = function()
                report_to_dialog.set_table(function_list[0], 11)
                report_to_dialog.set_table(function_list[1], 12)
            # handles multiple results for a unique check box (maint/reins asd coords)
            elif check_box_name == 'asdStartEndCheckBox':
                function_list = function()
                report_to_dialog.set_table(function_list[0], 13)
                report_to_dialog.set_table(function_list[1], 14)
                report_to_dialog.set_table(function_list[2], 15)
            # handles multiple results for a unique check box (polygons with no link, multi maintenance assignation)
            elif check_box_name == 'notPolysMaintCheckBox':
                function_list = function()
                report_to_dialog.set_table(function_list[0], 17)
                report_to_dialog.set_table(function_list[1], 18)
            # all other normal cases
            else:
                table_id = self.summary_functions[check_box_name][1]
                report_to_dialog.set_table(function(), table_id)
        self.report.end_report(self.validation_dia)
        self.report = ExportValidationReport("roadNet Validation Report",
                                             self.org_name,
                                             self.db, self.iface, None)
        report_to_dialog.show_validation_widget()
Example #53
0
 def __init__(self, parent=None, threadPool=None):
     QObject.__init__(self, parent)
     if threadPool is None:
         threadPool = QThreadPool.globalInstance()
     self._threadPool = threadPool
     self._depot_thread = None
    def test_that_process_states_calls_batch_reduce_for_each_row(self):
        self.batch_process_runner.process_states(self.states, False, OutputMode.Both, False, '')
        QThreadPool.globalInstance().waitForDone()

        self.assertEqual(self.sans_batch_instance.call_count, 3)
    def test_that_process_states_calls_batch_reduce_for_each_row(self):
        self.batch_process_runner.process_states(self.states, False,
                                                 OutputMode.Both, False, '')
        QThreadPool.globalInstance().waitForDone()

        self.assertEqual(self.sans_batch_instance.call_count, 3)
Example #56
0
    def __init__(self, parent=None, signalManager=None,
                 name="Databases update", domains=None):
        OWWidget.__init__(self, parent, signalManager, name,
                          wantMainArea=False)

        self.searchString = ""
        self.accessCode = ""
        self.domains = domains or DOMAINS
        self.serverFiles = serverfiles.ServerFiles()

        fbox = gui.widgetBox(self.controlArea, "Filter")
        self.completer = TokenListCompleter(
            self, caseSensitivity=Qt.CaseInsensitive)
        self.lineEditFilter = QLineEdit(textChanged=self.SearchUpdate)
        self.lineEditFilter.setCompleter(self.completer)

        fbox.layout().addWidget(self.lineEditFilter)

        box = gui.widgetBox(self.controlArea, "Files")

        self.filesView = QTreeWidget(self)
        self.filesView.setHeaderLabels(
            ["", "Data Source", "Update", "Last Updated", "Size"])

        self.filesView.setRootIsDecorated(False)
        self.filesView.setUniformRowHeights(True)
        self.filesView.setSelectionMode(QAbstractItemView.NoSelection)
        self.filesView.setSortingEnabled(True)
        self.filesView.sortItems(1, Qt.AscendingOrder)
        self.filesView.setItemDelegateForColumn(
            0, UpdateOptionsItemDelegate(self.filesView))

        self.filesView.model().layoutChanged.connect(self.SearchUpdate)

        box.layout().addWidget(self.filesView)

        box = gui.widgetBox(self.controlArea, orientation="horizontal")
        self.updateButton = gui.button(
            box, self, "Update all",
            callback=self.UpdateAll,
            tooltip="Update all updatable files",
         )

        self.downloadButton = gui.button(
            box, self, "Download all",
            callback=self.DownloadFiltered,
            tooltip="Download all filtered files shown"
        )

        self.cancelButton = gui.button(
            box, self, "Cancel", callback=self.Cancel,
            tooltip="Cancel scheduled downloads/updates."
        )

        self.retryButton = gui.button(
            box, self, "Reconnect", callback=self.RetrieveFilesList
        )
        self.retryButton.hide()

        gui.rubber(box)

        gui.lineEdit(box, self, "accessCode", "Access Code",
                     orientation="horizontal",
                     callback=self.RetrieveFilesList)

        self.warning(0)

        box = gui.widgetBox(self.controlArea, orientation="horizontal")
        gui.rubber(box)

        self.infoLabel = QLabel()
        self.infoLabel.setAlignment(Qt.AlignCenter)

        self.controlArea.layout().addWidget(self.infoLabel)
        self.infoLabel.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)

        self.updateItems = []

        self.resize(800, 600)

        self.progress = ProgressState(self, maximum=3)
        self.progress.valueChanged.connect(self._updateProgress)
        self.progress.rangeChanged.connect(self._updateProgress)
        self.executor = ThreadExecutor(
            threadPool=QThreadPool(maxThreadCount=2)
        )

        task = Task(self, function=self.RetrieveFilesList)
        task.exceptionReady.connect(self.HandleError)
        task.start()

        self._tasks = []
        self._haveProgress = False
class CatalogDialogTool(QObject):
    """
    Tool for managing the search and export functionality
    """

    def __init__(self, iface, dialog_ui, bbox_tool):
        """
        Constructor for the dialog tool
        :param iface: The QGIS Interface
        :param dialog_ui: The dialog GUI
        :param bbox_tool The bounding box tool
        :return: dialog tool
        """
        QObject.__init__(self, None)
        self.iface = iface
        self.dialog_ui = dialog_ui
        self.bbox_tool = bbox_tool

        self.progress_bar = None
        self.progress_message_bar = None
        self.progress_message_bar_widget = None
        self.search_thread_pool = QThreadPool()
        self.search_lock = Lock()
        self.export_thread_pool = QThreadPool()
        self.export_lock = Lock()
        self.query = None
        self.previous_credentials = None
        self.export_file = None
        self.footprint_layer = None

        self.filters = CatalogFilters(self.dialog_ui)

        self.dialog_ui.aoi_button.clicked.connect(self.aoi_button_clicked)
        self.dialog_ui.reset_button.clicked.connect(self.reset_button_clicked)
        self.dialog_ui.export_button.clicked.connect(self.export_button_clicked)
        self.bbox_tool.released.connect(self.search)
        self.model = None

    def init_progress_bar(self, progress_max):
        """
        Sets up the progress bar for search functionality
        :return: None
        """
        if not self.progress_message_bar:
            self.progress_message_bar = self.iface.messageBar().createMessage("Querying for data")
            self.progress_bar = QProgressBar()
            self.progress_bar.setMinimum(0)
            self.progress_bar.setMaximum(progress_max)
            self.progress_bar.setAlignment(Qt.AlignLeft | Qt.AlignCenter)
            self.progress_message_bar.layout().addWidget(self.progress_bar)
            self.progress_message_bar_widget = self.iface.messageBar().pushWidget(self.progress_message_bar, self.iface.messageBar().INFO)

    def init_layers(self):
        """
        Sets up the layers for rendering the items
        :return: None
        """
        if self.footprint_layer:
            QgsMapLayerRegistry.instance().removeMapLayer(self.footprint_layer.id())

        self.footprint_layer = QgsVectorLayer("Polygon?crs=EPSG:4326", "Catalog Footprints", "memory")
        self.footprint_layer.setCrs(QgsCoordinateReferenceSystem(4326), True)
        self.footprint_layer.dataProvider().addAttributes(CatalogAcquisitionFeature.get_fields())
        QgsMapLayerRegistry.instance().addMapLayer(self.footprint_layer)

    def clear_widgets(self):
        """
        Clears the progress bar
        :return: None
        """
        self.progress_bar = None
        self.progress_message_bar = None
        if self.progress_message_bar_widget:
            self.iface.messageBar().popWidget(self.progress_message_bar_widget)
        self.progress_message_bar_widget = None

    def is_searching(self):
        """
        Check to see if the system is still searching (checks if there's work in the search thread pool)
        :return: True if searching; False otherwise
        """
        return self.get_search_active_thread_count() > 0

    def is_exporting(self):
        """
        Check to see if the system is still exporting (checks if there's work in the export thread pool)
        :return: True if searching; False otherwise
        """
        return self.get_export_active_thread_count() > 0

    def get_search_active_thread_count(self):
        """
        Gets the number of active threads in the search thread pool
        :return:
        """
        with self.search_lock:
            return self.search_thread_pool.activeThreadCount()

    def get_export_active_thread_count(self):
        """
        Gets the number of active threads in the export thread pool
        :return:
        """
        with self.export_lock:
            return self.export_thread_pool.activeThreadCount()

    def aoi_button_clicked(self):
        """
        Validates and runs the search if validation successful
        :return: None
        """
        # can't run search during export
        if self.is_exporting():
            self.iface.messageBar().pushMessage("Error", "Cannot run search while export is running.", level=QgsMessageBar.CRITICAL)
        # can't run multiple search
        elif self.is_searching():
            self.iface.messageBar().pushMessage("Error", "Cannot run a new search while a search is running.", level=QgsMessageBar.CRITICAL)
        else:
            self.bbox_tool.reset()
            self.iface.mapCanvas().setMapTool(self.bbox_tool)

    def reset_button_clicked(self):
        """
        Resets filters.
        :return: None
        """
        self.reset()

    def export_button_clicked(self):
        """
        Validates and runs the export if validation successful
        :return: None
        """
        # can't run export during search
        if self.is_searching():
            self.iface.messageBar().pushMessage("Error", "Cannot run export while search is running.", level=QgsMessageBar.CRITICAL)
        # can't run multiple exports
        elif self.is_exporting():
            self.iface.messageBar().pushMessage("Error", "Cannot run a new export while a export is running.", level=QgsMessageBar.CRITICAL)
        else:
            self.export()

    def search(self, top, bottom, left, right):
        self.search_thread_pool.waitForDone(0)

        # validate credentials if they changed
        errors = []
        username, password, api_key, max_items_to_return = SettingsOps.get_settings()
        credentials = [username, password, api_key]
        if not self.previous_credentials or self.previous_credentials != credentials:
            SettingsOps.validate_stored_info(username, password, api_key, max_items_to_return, errors)
        self.previous_credentials = credentials

        # validate filters
        if not errors:
            self.filters.validate(errors)

        if errors:
            self.iface.messageBar().pushMessage("Error", "The following errors occurred: " + "<br />".join(errors), level=QgsMessageBar.CRITICAL)
        else:
            self.init_layers()

            self.dialog_ui.tab_widget.setCurrentIndex(RESULTS_TAB_INDEX)
            
            next_x_list = self.drange_list(float(left) + INCREMENTAL_INTERVAL, float(right), INCREMENTAL_INTERVAL)
            next_y_list = self.drange_list(float(bottom) + INCREMENTAL_INTERVAL, float(top), INCREMENTAL_INTERVAL)
            self.init_progress_bar(len(next_x_list) * len(next_y_list))

            self.model = CatalogTableModel(self.dialog_ui.table_view)
            self.dialog_ui.table_view.setModel(self.model)
            self.dialog_ui.table_view.selectionModel().selectionChanged.connect(self.selection_changed)

            if not self.query:
                self.query = GBDQuery(username=username, password=password, api_key=api_key)

            filters = self.filters.get_query_filters()
            time_begin = self.filters.get_datetime_begin()
            time_end = self.filters.get_datetime_end()

            current_x = float(left)
            current_y = float(bottom)
            for next_x in next_x_list:
                for next_y in next_y_list:
                    search_runnable = CatalogSearchRunnable(self.query, self.model, self, top=next_y, left=current_x, right=next_x, bottom=current_y, 
                                                            time_begin=time_begin, time_end=time_end, filters=filters)
                    search_runnable.task_object.task_complete.connect(self.on_search_complete)
                    self.search_thread_pool.start(search_runnable)
                    current_y = next_y
                current_y = bottom
                current_x = next_x

    def reset(self):
        self.filters.remove_all()

    def export(self):
        self.export_thread_pool.waitForDone(0)
        acquisitions = None
        if self.model is not None:
            acquisitions = self.model.data

        if not acquisitions:
            self.iface.messageBar().pushMessage("Error", "No data to export.", level=QgsMessageBar.CRITICAL)
        else:
            # open file ui
            select_file_ui = QFileDialog()
            starting_file = self.export_file or os.path.expanduser("~")
            export_file = select_file_ui.getSaveFileName(None, "Choose output file", starting_file, SELECT_FILTER)

            if export_file:
                self.export_file = export_file
                self.init_progress_bar(0)
                export_runnable = CatalogExportRunnable(acquisitions, self.export_file)
                export_runnable.task_object.task_complete.connect(self.on_export_complete)
                self.export_thread_pool.start(export_runnable)

    @pyqtSlot()
    def on_search_complete(self):
        thread_count = self.get_search_active_thread_count()
        if self.progress_message_bar:
            self.progress_bar.setValue(self.progress_bar.value() + 1)
        if thread_count == 0:
            self.clear_widgets()
            self.dialog_ui.table_view.resizeColumnsToContents()

    @pyqtSlot()
    def on_export_complete(self):
        thread_count = self.get_export_active_thread_count()
        if self.progress_message_bar:
            self.progress_bar.setValue(self.progress_bar.value() + 1)
        if thread_count == 0:
            self.clear_widgets()
            self.iface.messageBar().pushMessage("Info", 'File export has completed to "%s".' % self.export_file)

    def selection_changed(self, selected, deselected):
        self.footprint_layer.startEditing()

        # draw footprints for selected rows
        selected_rows = set()
        for index in selected.indexes():
            selected_rows.add(index.row())
        for row in selected_rows:
            acquisition = self.model.get(row)
            feature_id = self.model.generate_feature_id()
            self.model.set_feature_id(acquisition, feature_id)
            feature = CatalogAcquisitionFeature(feature_id, acquisition)
            self.footprint_layer.dataProvider().addFeatures([feature])

        # remove footprints for deselected rows
        deselected_rows = set()
        for index in deselected.indexes():
            deselected_rows.add(index.row())
        feature_ids_to_remove = []
        for row in deselected_rows:
            acquisition = self.model.get(row)
            feature_id = self.model.get_feature_id(acquisition)
            feature_ids_to_remove.append(feature_id)
            self.model.remove_feature_id(acquisition)
        if feature_ids_to_remove:
            self.footprint_layer.dataProvider().deleteFeatures(feature_ids_to_remove)

        self.footprint_layer.commitChanges()
        self.footprint_layer.updateExtents()
        self.footprint_layer.triggerRepaint()

    def drange_list(self, start, stop, step):
        drange_list = []
        r = start
        while r < stop:
            drange_list.append(r)
            r += step
        if not drange_list:
            drange_list.append(stop)
        return drange_list
Example #58
0
 def deffer_to_thread(self, cb, *args, **kwargs):
     args = (self, ) + args
     thread = Thread(self, cb, *args, **kwargs)
     QThreadPool.globalInstance().start(thread)
Example #59
0
 def __init__(self):
     super(Tasks, self).__init__()
     self.pool = QThreadPool.globalInstance()
     self.pool.setMaxThreadCount(2)