Created
June 18, 2020 07:17
-
-
Save JosephCurvin/22f080803d842ca3a055ec68ea3300b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Testing Template for throw away experiment | |
""" | |
import sys | |
import os | |
import re | |
from PyQt5 import QtWidgets as qtw | |
from PyQt5 import QtCore as qtc | |
from PyQt5 import QtGui as qtg | |
class InputWorker(qtc.QObject): | |
work_signal = qtc.pyqtSignal(str) | |
finishedworking = qtc.pyqtSignal() # emmits that its finished | |
def __init__(self, parent=None): | |
super().__init__(parent=parent) | |
self.continue_run = True | |
@qtc.pyqtSlot() | |
def receave_caliperdata(self): | |
while self.continue_run: | |
data = input("inser something: ") | |
self.work_signal.emit(data) | |
def stop_me(self): | |
self.continue_run = False | |
class DataModel(qtc.QAbstractTableModel): | |
def __init__(self, input_data=None): | |
super().__init__() | |
self.input_data = input_data or [[None], [None], [None], [None], [None]] | |
def data(self, index, role): | |
if role == qtc.Qt.DisplayRole: | |
try: | |
text = self.input_data[index.row()][index.column()] | |
# self.scaledatachanged_signal.emit() dont need it | |
except IndexError: | |
text = None | |
return text | |
def rowCount(self, index=qtc.QModelIndex()): | |
return 0 if index.isValid() else len(self.input_data) | |
def columnCount(self, index): | |
return len(self.input_data[0]) | |
def headerData(self, section, orientation, role): | |
if role == qtc.Qt.DisplayRole: | |
if orientation == qtc.Qt.Vertical: | |
return "row " + str(section + 1) | |
def flags(self, index): | |
return qtc.Qt.ItemIsEditable | qtc.Qt.ItemIsSelectable | qtc.Qt.ItemIsEnabled | |
def setData(self, index, value, role=qtc.Qt.EditRole): | |
if role == qtc.Qt.EditRole: # The data in a form suitable for editing in an editor. returns string | |
try: | |
row = index.row() | |
column = index.column() | |
# filter floats and digits and comma stuff | |
pattern = '^[\d]+(?:[,.][\d]+)?$' # execepts "," and "." | |
if re.fullmatch(pattern, value, flags=0): | |
pattern = '^.*[,].*$' | |
if re.fullmatch(pattern, value, flags=0): | |
value = value.replace(',', '.') | |
self.input_data[row][column] = float(value) | |
print(type(value)) | |
else: | |
self.input_data[row][column] = float(value) # float | |
print(type(value)) | |
else: | |
pass | |
return True | |
except ValueError: | |
return False | |
def insertRows(self, position, rows, parent=qtc.QModelIndex()): | |
position = (position + self.rowCount()) if position < 0 else position | |
start = position | |
end = position + rows - 1 | |
if end <= 8: | |
self.beginInsertRows(parent, start, end) | |
self.input_data.append([None]) | |
self.endInsertRows() | |
return True | |
else: | |
return False | |
def removeRows(self, position, rows, parent=qtc.QModelIndex()): | |
position = (position + self.rowCount()) if position < 0 else position | |
start = position | |
end = position + rows - 1 | |
if end >= 1: | |
self.beginRemoveRows(parent, start, end) | |
del self.input_data[start:end + 1] | |
self.endRemoveRows() | |
return True | |
else: | |
return False | |
class MainWindow(qtw.QWidget): | |
def __init__(self): | |
super().__init__() | |
self.table_view = qtw.QTableView() | |
self.table_view.setSelectionMode(qtw.QAbstractItemView.SingleSelection) | |
self.datamodel = DataModel() | |
self.table_view.setModel(self.datamodel) | |
# widget | |
self.addrow_button = qtw.QPushButton("add row") | |
self.deleaterow_button = qtw.QPushButton("deleate row") | |
# set the layout | |
layout = qtw.QVBoxLayout() | |
layout.addWidget(self.table_view) | |
layout.addWidget(self.addrow_button) | |
layout.addWidget(self.deleaterow_button) | |
self.setLayout(layout) | |
# -------------------------------- # | |
self.addrow_button.clicked.connect( | |
lambda: self.datamodel.insertRows(-1, 1)) | |
self.deleaterow_button.clicked.connect(self.delete_row) | |
self.table_view.selectionModel().selectionChanged.connect(self.change_inputdata) | |
# ---------thread-and-Worker------------------ # | |
# ------------caliper------------------------------# | |
# ----scale_thread-and-worker-object---------------- # | |
self.inputworker = InputWorker() # 1--create a worker instance | |
self.thread = qtc.QThread() # 1--create a scale_thread instance | |
# ----------stop--worker-and-scale_thread----------# | |
# -mein-stopsignal-mit-worker-stop-method-- # | |
# self.emit_stop_signal.connect(self.inputworker.stop_me) | |
# --worker stop signal and scale_thread to quit | |
self.inputworker.finishedworking.connect(self.thread.quit) | |
# ----cleaning--prozess------- # | |
self.inputworker.finishedworking.connect(self.inputworker.deleteLater) | |
self.thread.finished.connect(self.thread.deleteLater) | |
# -----move-worker-to-scale_thread---------------- # | |
self.inputworker.moveToThread(self.thread) | |
# ----connect----scale_thread-with-worker----- # | |
self.thread.started.connect(self.inputworker.receave_caliperdata) | |
# --auslaufprozess-------- # | |
self.thread.finished.connect(self.inputworker.stop_me) | |
# -------start-scale_thread--------- # | |
# caliper thread needs to be started after scale thread with time delay otherwise scale can not connect !!!! two devices at same time not possible | |
qtc.QTimer.singleShot(1000, lambda: self.thread.start(qtc.QThread.NormalPriority)) # 3000 works / 2000 works # scaale thread muss 2 secunken apart liegen | |
def change_inputdata(self): | |
self.inputworker.work_signal.connect(self.insertinputdata_onclick) | |
def insertinputdata_onclick(self, data): | |
x = self.table_view.selectionModel().currentIndex().row() | |
y = self.table_view.selectionModel().currentIndex().column() | |
if type(data) is str: | |
self.datamodel.input_data[x][y] = data | |
self.datamodel.layoutChanged.emit() | |
self.table_view.selectionModel().clearSelection() | |
def delete_row(self): | |
self.table_view.setCurrentIndex(qtc.QModelIndex()) | |
self.datamodel.removeRows(-1, 1) | |
if __name__ == '__main__': | |
app = qtw.QApplication(sys.argv) | |
main = MainWindow() | |
main.show() | |
sys.exit(app.exec_()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment