-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskaner_main.py
718 lines (614 loc) · 27.2 KB
/
skaner_main.py
1
2
3
4
5
6
7
8
9
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
import random
import math
import matplotlib
from matplotlib.backends.backend_qt5agg import FigureCanvas
import matplotlib as mpl
import matplotlib.figure as mpl_fig
import matplotlib.animation as anim
import PyQt5.QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import \
QLabel, \
QGridLayout, \
QLineEdit, \
QPushButton, \
QHBoxLayout, \
QMessageBox, \
QApplication, \
QWidget, \
QComboBox, \
QFileDialog, \
QCheckBox, \
QButtonGroup, \
QProgressBar, \
QGroupBox
import json
import csv
import const
matplotlib.use('Qt5Agg')
'''
Klasa widżetu wykresu
'''
lineCount = 5
lineColors = ['red', 'green', 'blue', 'skyblue', 'olive']
class MplCanvas(FigureCanvas, anim.FuncAnimation):
def __init__(self, parent=None, width=5, height=4, dpi=100):
FigureCanvas.__init__(self, mpl_fig.Figure())
self.axes = self.figure.subplots()
self.annMax = None
self.annMin = None
self.maxY = 0
self.minY = 0
self.plotref = []
self.axLegends = ["a", "b", "c", "d", "e"]
def plotData(self, xdata, ydata, lines):
for i in range(lines):
if len(self.axLegends) >= i + 1:
axlabel = self.axLegends[i]
else:
axlabel = None
plot_refs = self.axes.plot(
xdata, ydata[0], lineColors[i], label=axlabel)
legend = self.axes.legend(loc='upper center', bbox_to_anchor=(
0.5, 1.05), ncol=3, fancybox=True, shadow=True)
self.plotref.append(plot_refs[0])
self.plotref[i].set_ydata(ydata[i])
self.draw()
return
def clearPlot(self):
self.axes = self.figure.clear()
self.plotref = []
self.draw()
self.axes = self.figure.subplots()
def setAxisLabels(self, xlegend, ylegend):
self.axes.set_xlabel(xlegend)
self.axes.set_ylabel(ylegend)
def setAxesLegend(self, legends):
self.axLegends = []
self.axLegends = legends
def setXscale(self, scale):
self.axes.set_xscale(scale)
def setYscale(self, scale):
self.axes.set_yscale(scale)
def setYlim(self, min, max):
if min != max:
# if min == max:
# self.axes.set_ylim(min)
# else:
if min > 0:
minn = min * 0.9
else:
minn = min * 1.1
if max > 0:
maxx = max * 1.1
else:
maxx = max * 0.9
self.axes.set_ylim(minn, maxx)
self.axes.plot()
self.maxY = maxx
self.minY = minn
def setPlotGrid(self, gridType, gridAxis):
self.axes.grid(False)
self.axes.grid(gridType, which='both', axis=gridAxis)
def setAutoscale(self, state, ax):
self.axes.autoscale(state, ax)
def saveToPNG(self, filename):
self.figureCpy = self.figure
self.figure.savefig(filename, dpi=100, format='png')
'''
Klasa głównego okna aplikacji.
'''
class MainWindow(QWidget):
def __init__(self, *args, **kwargs):
# Inicjlizacja zmiennych
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("Skaner")
self.__index = 0
self.__samples = const.defSamples
self.__samplingPeriod = const.defSamplingPeriod
self.__initValue = const.definitValue
self.__endValue = const.defendValue
self.maxval = 0.0
self.maxval_x = 0.0
self.minval = 0.0
self.minval_x = 0.0
self.XreadFromFile = False
# Definicje etkiet w programie
# Etykiety, umieszczone obok pół wpisywania danych
samplesNumberLabel = QLabel("Ilość punktów:", self)
samplingIntervalLabel = QLabel("Okres próbkowania [ms]:", self)
initValueLabel = QLabel("Wartość początkowa:", self)
endValueLabel = QLabel("Wartość końcowa:", self)
xGenScaleLabel = QLabel("Generowanie X:")
self.xGenLabel = QLabel("Brak X")
# Etykiety wyświetlania współrzędnych ostatniego punktu
self.currentXlabel = QLabel("x = 0", self)
self.currentYlabel = QLabel('y = 0', self)
self.maxLabel = QLabel("MAX: x = 0; y = 0", self)
self.minLabel = QLabel("MIN: x = 0; y = 0", self)
# Definicje pól wprowadzania tekstu w programie
self.samplesNumberEdit = QLineEdit()
self.samplingIntervalEdit = QLineEdit()
self.initValueEdit = QLineEdit()
self.endValueEdit = QLineEdit()
self.samplesNumberEdit.setText(str(const.defSamples))
self.samplingIntervalEdit.setText(str(const.defSamplingPeriod))
self.initValueEdit.setText(str(const.definitValue))
self.endValueEdit.setText(str(const.defendValue))
self.xLegendEdit = QLineEdit()
self.yLegendEdit = QLineEdit()
self.lineLegendsEdit = []
for i in range(lineCount):
self.lineLegendsEdit.append(QLineEdit())
self.lineLegendsEdit[i].setToolTip('Przebieg ' + str(i))
# Definicje przycisków w programie
self.startBtn = QPushButton("&Start", self)
self.startBtn.setStyleSheet("background-color:lightgreen")
self.startBtn.clicked.connect(self.start_plot)
self.stopBtn = QPushButton("Stop", self)
self.stopBtn.setStyleSheet("background-color:red")
self.stopBtn.clicked.connect(self.stop_plot)
# Przyciski do operacji na plikach
self.saveConfBtn = QPushButton("Zapisz", self)
self.openConfBtn = QPushButton("Wczytaj", self)
self.saveConfBtn.clicked.connect(self.saveConfigFile)
self.openConfBtn.clicked.connect(self.openConfigFile)
self.genXbtn = QPushButton("Generuj X")
self.genXbtn.clicked.connect(self.genXmethod)
self.saveXBtn = QPushButton("Zapisz X", self)
self.saveXBtn.clicked.connect(self.savexToFile)
self.readXBtn = QPushButton("Wczytaj X", self)
self.readXBtn.clicked.connect(self.readxFromFile)
self.saveToPicBtn = QPushButton("Zapisz PNG", self)
self.saveToPicBtn.clicked.connect(self.savePlotToPic)
# Combo Boxy
self.plotXscale = QComboBox(self)
self.plotXscale.addItem('X: Liniowa')
self.plotXscale.addItem('X: Logarytmiczna')
self.plotYscale = QComboBox(self)
self.plotYscale.addItem('Y: Liniowa')
self.plotYscale.addItem('Y: Logarytmiczna')
self.plotGrid = QComboBox(self)
self.plotGrid.addItem('Brak')
self.plotGrid.addItem('Tylko X')
self.plotGrid.addItem('Tylko Y')
self.plotGrid.addItem('X i Y')
self.plotGrid.currentIndexChanged.connect(self.changePlotGrid)
self.sampXGenCombo = QComboBox(self)
self.sampXGenCombo.addItem('Liniowo')
self.sampXGenCombo.addItem('Logarytmicznie')
self.plotCntCombo = QComboBox(self)
for i in range(5):
if i == 0:
suffix = " przebieg"
else:
suffix = " przebiegów"
self.plotCntCombo.addItem(str(i + 1) + suffix)
# Progres bar
self.progressBar = QProgressBar(self)
# Boxy na widgety
self.plotSaveButtonsGroup = QGroupBox("Zapis przebiegu")
grdLay = QGridLayout()
grdLay.addWidget(self.saveToPicBtn, 0, 0)
self.plotSaveButtonsGroup.setLayout(grdLay)
self.dataParametersGroup = QGroupBox("Nastawy")
grdLay = QGridLayout()
grdLay.addWidget(samplesNumberLabel,
const.sampNumLabRow, const.sampNumLabCol)
grdLay.addWidget(samplingIntervalLabel,
const.sampIntLabRow, const.sampIntLabCol)
grdLay.addWidget(initValueLabel, const.iniValLabRow,
const.iniValLabCol)
grdLay.addWidget(endValueLabel, const.endValLabRow, const.endValLabCol)
grdLay.addWidget(xGenScaleLabel, const.xGenScaleLabelRow,
const.xGenScaleLabelCol)
grdLay.addWidget(self.samplesNumberEdit,
const.sampNumEditRow, const.sampNumEditCol)
grdLay.addWidget(self.samplingIntervalEdit,
const.sampIntEditRow, const.sampIntEditCol)
grdLay.addWidget(self.initValueEdit,
const.iniValEditRow, const.iniValEditCol)
grdLay.addWidget(self.endValueEdit,
const.endValEditRow, const.endValEditCol)
grdLay.addWidget(self.sampXGenCombo,
const.sampXScaleRow, const.sampXScaleCol)
grdLay.addWidget(self.saveXBtn,
const.saveXBtnRow, const.saveXBtnCol)
grdLay.addWidget(self.readXBtn,
const.readXBtnRow, const.readXBtnCol)
grdLay.addWidget(self.genXbtn,
const.genXBtnRow, const.genXBtnCol)
grdLay.addWidget(self.xGenLabel, const.xGenLabelRow,
const.xGenLabelCol)
self.dataParametersGroup.setLayout(grdLay)
self.currentDataGroup = QGroupBox("Wartości")
grdLay = QGridLayout()
grdLay.addWidget(self.maxLabel, const.maxLabelRow, const.maxLabelCol)
grdLay.addWidget(self.minLabel, const.minLabelRow, const.minLabelCol)
grdLay.addWidget(self.currentXlabel,
const.currXLabRow, const.currXLabCol)
grdLay.addWidget(self.currentYlabel,
const.currYLabRow, const.currYLabCol)
grdLay.addWidget(self.progressBar, const.prgBarRow, const.prgBarCol)
self.currentDataGroup.setLayout(grdLay)
self.plotSettingsGroup = QGroupBox("Ustawienia wykresu")
grdLay = QGridLayout()
grdLay.addWidget(QLabel("Skala"), 0, 0)
grdLay.addWidget(QLabel("Legenda X"), 0, 1)
grdLay.addWidget(QLabel("Legenda Y"), 2, 1)
grdLay.addWidget(self.xLegendEdit, 1, 1)
grdLay.addWidget(self.yLegendEdit, 3, 1)
grdLay.addWidget(self.plotXscale, const.pltXsclRow, const.pltXsclCol)
grdLay.addWidget(self.plotYscale, const.pltYsclRow, const.pltYsclCol)
grdLay.addWidget(QLabel("Siatka"), 3, 0)
grdLay.addWidget(self.plotGrid, const.pltGridRow, const.pltGridCol)
grdLay.addWidget(self.plotCntCombo, 4, 1)
for i in range(lineCount):
grdLay.addWidget(self.lineLegendsEdit[i], i, 3)
self.plotSettingsGroup.setLayout(grdLay)
self.ctrlButtonsGroup = QGroupBox("Kontrola")
grdLay = QGridLayout()
grdLay.addWidget(self.startBtn, const.startBtnRow, const.startBtnCol)
grdLay.addWidget(self.stopBtn, const.stopBtnRow, const.stopBtnCol)
grdLay.addWidget(QLabel("Plik ustawień"), 2, 0)
grdLay.addWidget(self.saveConfBtn,
const.saveConfBtnRow, const.saveConfBtnCol)
grdLay.addWidget(self.openConfBtn,
const.openConfBtnRow, const.openConfBtnCol)
self.ctrlButtonsGroup.setLayout(grdLay)
self.confFileGroup = QGroupBox("Plik ustawień")
grdLay = QGridLayout()
self.confFileGroup.setLayout(grdLay)
# Layout Tabelaryczny
Tlayout = QGridLayout()
Tlayout.addWidget(self.ctrlButtonsGroup, 0, 0)
Tlayout.addWidget(self.plotSaveButtonsGroup, 0, 1)
Tlayout.addWidget(self.dataParametersGroup, 0, 2)
Tlayout.addWidget(self.plotSettingsGroup, 0, 3)
Tlayout.addWidget(self.currentDataGroup, 0, 4)
# Okno wykresu
self.canvas = MplCanvas(self, width=const.canvWid,
height=const.canvHght, dpi=const.canvDpi)
Tlayout.addWidget(self.canvas, const.canvRow,
const.canvCol, const.canvRowDim, const.canvColDim)
self.setLayout(Tlayout)
self.xdata = []
self.ydata = []
# We need to store a reference to the plotted line
# somewhere, so we can apply the new data to it.
self._plot_ref = None
# Setup a timer to trigger the redraw by calling update_plot.
self.timer = PyQt5.QtCore.QTimer()
self.timer.timeout.connect(self.update_plot)
self.showMaximized()
def increment_index(self):
self.__index += 1
def get_index(self):
return self.__index
def update_plot(self):
# Drop off the first y element, append a new one.
index = self.get_index()
data = self.getData()
currx = "X = 0"
curry = "Y = 0"
self.progressBar.setValue(index)
if index > 0:
self.ydata[0][index - 1] = data[0]
self.ydata[1][index - 1] = data[1]
self.ydata[2][index - 1] = data[2]
self.ydata[3][index - 1] = data[3]
self.ydata[4][index - 1] = data[4]
extrData = self.findMinMax(data)
if extrData[0] > self.maxval:
self.maxval = extrData[0]
self.maxval_x = self.xdata[index - 1]
maxxupdt = True
else:
maxxupdt = False
if extrData[1] < self.minval:
self.minval = extrData[1]
self.minval_x = self.xdata[index - 1]
minupdt = True
else:
minupdt = False
curridx = "idx = " + str(self.get_index())
currx = "X = " + \
"{:.5f}".format(self.xdata[self.get_index() - 1])
curry = "Y = " + \
"{:.5f}".format(self.ydata[0][self.get_index() - 1])
else:
self.maxval = data[0]
self.minval = data[0]
maxxupdt = True
minupdt = True
self.canvas.setAxisLabels(
self.xLegendEdit.text(), self.yLegendEdit.text())
self.increment_index()
self.currentYlabel.setText(curry)
self.currentXlabel.setText(currx)
if maxxupdt == True:
maxx = "MAX: X=" + \
"{:.5f}".format(self.maxval_x) + " Y=" + \
"{:.5f}".format(self.maxval)
self.maxLabel.setText(maxx)
if minupdt == True:
minx = "MIN: X=" + \
"{:.5f}".format(self.minval_x) + " Y=" + \
"{:.5f}".format(self.minval)
self.minLabel.setText(minx)
self.canvas.setYlim(self.minval, self.maxval)
self.canvas.plotData(self.xdata, self.ydata, lineCount)
if index == self.__samples:
self.stop_plot()
QMessageBox.information(
self, "Info", "Zakończono.", QMessageBox.Ok)
def start_plot(self):
self.clear_plot()
try:
self.__samples = int(self.samplesNumberEdit.text())
self.__samplingPeriod = int(self.samplingIntervalEdit.text())
self.__initValue = float(self.initValueEdit.text())
self.__endValue = float(self.endValueEdit.text())
self.__step = (self.__endValue - self.__initValue) / self.__samples
self.__samples = self.__samples + 1
legends = []
for i in range(lineCount):
legends.append(self.lineLegendsEdit[i].text())
self.canvas.setAxesLegend(legends)
dataok = True
except ValueError:
QMessageBox.warning(self, "Błąd", "Błędne dane.", QMessageBox.Ok)
dataok = False
if dataok:
if self.__samplingPeriod > 0:
if self.__samples > 0 and self.__step != 0.0:
if self.XreadFromFile == False:
self.genXmethod()
else:
self.__samples = len(self.xdata)
self.samplesNumberEdit.setText(str(self.__samples))
dataok = True
else:
dataok = False
if dataok:
if self.__samplingPeriod < 10:
QMessageBox.warning(
self, "Uwaga!", "Uwaga, okres próbkowania niżzy niż 10 ms, może powodować niepoprawną pracę programu.")
self.samplingIntervalEdit.setReadOnly(True)
self.samplesNumberEdit.setReadOnly(True)
self.initValueEdit.setReadOnly(True)
self.endValueEdit.setReadOnly(True)
self.progressBar.setMaximum(self.__samples)
self.progressBar.setValue(0)
self.plotXscale.setEnabled(False)
self.plotYscale.setEnabled(False)
self.startBtn.setEnabled(False)
self.openConfBtn.setEnabled(False)
self.sampXGenCombo.setEnabled(False)
self.clear_plot()
self.changePlotGrid()
if self.plotXscale.currentIndex() == 0:
self.canvas.setXscale('linear')
elif self.plotXscale.currentIndex() == 1:
self.canvas.setXscale('log')
if self.plotYscale.currentIndex() == 0:
self.canvas.setYscale('linear')
elif self.plotYscale.currentIndex() == 1:
self.canvas.setYscale('log')
self.canvas.setAutoscale(True, 'both')
self.canvas.setAxisLabels(
self.xLegendEdit.text(), self.yLegendEdit.text())
# Tablica na wartości Y
self.ydata = []
for j in range(10):
self.ydata.append([0.0 for i in range(self.__samples)])
self.timer.setInterval(self.__samplingPeriod)
try:
self.update_plot()
except Exception as e:
QMessageBox.warning(
self, "Błąd", "Nieoczekiwany błąd.", QMessageBox.Ok)
print(e)
self.stop_plot()
try:
self.timer.start()
except:
QMessageBox.warning(
self, "Błąd", "Wprowadź poprawny okres próbkowania", QMessageBox.Ok)
self.stop_plot()
else:
QMessageBox.warning(
self, "Błąd", "Błędne dane.", QMessageBox.Ok)
def stop_plot(self):
self.canvas.axes.autoscale(enable=True, axis='y')
self.timer.stop()
self.plotXscale.setEnabled(True)
self.plotYscale.setEnabled(True)
self.startBtn.setEnabled(True)
self.openConfBtn.setEnabled(True)
self.samplingIntervalEdit.setReadOnly(False)
self.samplesNumberEdit.setReadOnly(False)
self.initValueEdit.setReadOnly(False)
self.endValueEdit.setReadOnly(False)
self.sampXGenCombo.setEnabled(True)
def clear_plot(self):
self.__index = 0
self.minval = 0.0
self.maxval = 0.0
self.canvas.clearPlot()
self._plot_ref = None
def changePlotGrid(self):
gridOn = False
gridAx = 'both'
if self.plotGrid.currentIndex() == 1:
gridOn = True
gridAx = 'x'
elif self.plotGrid.currentIndex() == 2:
gridOn = True
gridAx = 'y'
elif self.plotGrid.currentIndex() == 3:
gridOn = True
gridAx = 'both'
self.canvas.setPlotGrid(gridOn, gridAx)
self.canvas.draw()
self.canvas.setAutoscale(True, 'both')
def saveConfigFile(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
dialogBox = QFileDialog()
dialogBox.setWindowTitle("Zapisz plik konfiguracyjny")
dialogBox.setNameFilters(
["Plik JSON (*.json)", "Plik tekstowy (*.txt)"])
dialogBox.setDefaultSuffix('json')
dialogBox.setAcceptMode(QFileDialog.AcceptSave)
if dialogBox.exec_() == QFileDialog.Accepted:
filename = dialogBox.selectedFiles()[0]
config = {}
config['sampling'] = []
config['sampling'].append({
'samplingPeriod': self.samplingIntervalEdit.text(),
'firstValue': self.initValueEdit.text(),
'lastValue': self.endValueEdit.text(),
'sampleCount': self.samplesNumberEdit.text(),
'xGen': self.sampXGenCombo.currentIndex()
})
config['plot'] = []
config['plot'].append({
'grids': self.plotGrid.currentIndex(),
'xScale': self.plotXscale.currentIndex(),
'yScale': self.plotYscale.currentIndex()
})
with open(filename, 'w') as outfile:
json.dump(config, outfile, indent=4, sort_keys=True)
def openConfigFile(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
dialogBox = QFileDialog()
dialogBox.setWindowTitle("Wybierz plik konfiguracyjny")
dialogBox.setNameFilters(
["Plik JSON (*.json)", "Plik tekstowy (*.txt)"])
dialogBox.setDefaultSuffix('json')
dialogBox.setAcceptMode(QFileDialog.AcceptOpen)
if dialogBox.exec_() == QFileDialog.Accepted:
filename = dialogBox.selectedFiles()[0]
with open(filename) as jsonFile:
data = json.load(jsonFile)
try:
samplConf = data['sampling'][0]
self.samplingIntervalEdit.setText(
samplConf["samplingPeriod"])
self.initValueEdit.setText(samplConf["firstValue"])
self.endValueEdit.setText(samplConf["lastValue"])
self.samplesNumberEdit.setText(samplConf["sampleCount"])
self.sampXGenCombo.setCurrentIndex(samplConf["xGen"])
except:
QMessageBox.warning(
self, "Błąd", "Błędne ustawienia próbkowania.", QMessageBox.Ok)
try:
samplConf = data['plot'][0]
self.plotGrid.setCurrentIndex(samplConf['grids'])
self.plotXscale.setCurrentIndex(samplConf['xScale'])
self.plotYscale.setCurrentIndex(samplConf['yScale'])
except Exception as e:
print(e)
QMessageBox.warning(
self, "Błąd", "Błędne ustawienia wykresu.", QMessageBox.Ok)
def savexToFile(self):
if len(self.xdata) != 0:
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
dialogBox = QFileDialog()
dialogBox.setWindowTitle("Zapisz X do pliku")
dialogBox.setNameFilters(
["Plik CSV (*.csv)"])
dialogBox.setDefaultSuffix('csv')
dialogBox.setAcceptMode(QFileDialog.AcceptSave)
if dialogBox.exec_() == QFileDialog.Accepted:
filename = dialogBox.selectedFiles()[0]
with open(filename, 'w', newline='') as xfile:
wr = csv.writer(xfile)
for i in self.xdata:
wr.writerow([i])
else:
QMessageBox.warning(
self, "Błąd", "X nie został jeszcze wygenerowany.", QMessageBox.Ok)
def readxFromFile(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
dialogBox = QFileDialog()
dialogBox.setWindowTitle("Zapisz X do pliku")
dialogBox.setNameFilters(["Plik CSV (*.csv)"])
dialogBox.setDefaultSuffix('csv')
dialogBox.setAcceptMode(QFileDialog.AcceptOpen)
if dialogBox.exec_() == QFileDialog.Accepted:
filename = dialogBox.selectedFiles()[0]
with open(filename, newline='') as xfile:
reader = csv.reader(xfile)
self.xdata = [float(row[0]) for row in reader]
self.XreadFromFile = True
self.xGenLabel.setText("X wczytany")
def genXmethod(self):
self.XreadFromFile = False
try:
self.__samples = int(self.samplesNumberEdit.text())
self.__initValue = float(self.initValueEdit.text())
self.__endValue = float(self.endValueEdit.text())
self.__step = (self.__endValue - self.__initValue) / self.__samples
self.__samples = self.__samples + 1
if self.sampXGenCombo.currentIndex() == 0:
self.xdata = list(self.__initValue + i *
self.__step for i in range(self.__samples))
elif self.sampXGenCombo.currentIndex() == 1:
x1 = math.log10(self.__initValue)
x2 = math.log10(self.__endValue)
i = []
for j in range(self.__samples):
i.append(x1 + j * ((x2 - x1)/(self.__samples - 1)))
self.xdata = list(math.pow(10.0, j) for j in i)
self.xGenLabel.setText("X wygenerowany")
except ValueError:
QMessageBox.warning(self, "Błąd", "Błędne dane.", QMessageBox.Ok)
def getData(self):
index = self.get_index()
if index == 0:
'''
Pierwszy obieg, nie ma jeszcze żadnych danych pomiarowych.
Wyśli dane do urządzenia i nie odbieraj nic.
'''
pomiar = [0.0, 0.0, 0.0, 0.0, 0.0]
elif index > 0:
pomiar = []
'''
Pobierz dane z urządzenia z poprzedniego pomiaru i wyślij kolejną nastawę
'''
pomiar.append(
math.cos(self.xdata[index - 1] * 10 * random.random()))
pomiar.append(
math.cos(self.xdata[index - 1] * 0.5 * random.random()))
pomiar.append(
math.cos(self.xdata[index - 1] * random.random()))
pomiar.append(
math.cos(self.xdata[index - 1] * 2 * random.random()))
pomiar.append(
math.cos(self.xdata[index - 1] * 0.1 * random.random()))
return pomiar
def findMinMax(self, data):
minimum = min(data)
maximum = max(data)
return [maximum, minimum]
def savePlotToPic(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
dialogBox = QFileDialog()
dialogBox.setWindowTitle("Zapisz przebiegi do pliku")
dialogBox.setNameFilters(["Obraz PNG (*.png)"])
dialogBox.setDefaultSuffix('png')
dialogBox.setAcceptMode(QFileDialog.AcceptSave)
if dialogBox.exec_() == QFileDialog.Accepted:
filename = dialogBox.selectedFiles()[0]
self.canvas.saveToPNG(filename)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
okno = MainWindow()
sys.exit(app.exec_())