mirror of
https://github.com/bec-project/bec_widgets.git
synced 2026-06-08 06:18:39 +02:00
feat: table can be exported to csv
This commit is contained in:
@@ -541,6 +541,20 @@
|
|||||||
</column>
|
</column>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_exportCSV">
|
||||||
|
<property name="text">
|
||||||
|
<string>Export CSV</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButton_2">
|
||||||
|
<property name="text">
|
||||||
|
<string>Import CSV</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="QWidget" name="tab_settings">
|
<widget class="QWidget" name="tab_settings">
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import csv
|
||||||
import os
|
import os
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from functools import partial
|
from functools import partial
|
||||||
@@ -9,7 +10,7 @@ from PyQt5.QtCore import QThread, pyqtSlot, QPoint
|
|||||||
from PyQt5.QtCore import pyqtSignal, Qt
|
from PyQt5.QtCore import pyqtSignal, Qt
|
||||||
from PyQt5.QtGui import QDoubleValidator
|
from PyQt5.QtGui import QDoubleValidator
|
||||||
from PyQt5.QtGui import QKeySequence
|
from PyQt5.QtGui import QKeySequence
|
||||||
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget
|
from PyQt5.QtWidgets import QApplication, QWidget, QTableWidget, QFileDialog
|
||||||
from PyQt5.QtWidgets import QShortcut
|
from PyQt5.QtWidgets import QShortcut
|
||||||
from pyqtgraph.Qt import QtWidgets, uic, QtCore
|
from pyqtgraph.Qt import QtWidgets, uic, QtCore
|
||||||
|
|
||||||
@@ -405,6 +406,11 @@ class MotorApp(QWidget):
|
|||||||
# Signals
|
# Signals
|
||||||
self.tableWidget_coordinates.itemChanged.connect(self.update_saved_coordinates)
|
self.tableWidget_coordinates.itemChanged.connect(self.update_saved_coordinates)
|
||||||
|
|
||||||
|
# Buttons
|
||||||
|
self.pushButton_exportCSV.clicked.connect(
|
||||||
|
lambda: self.export_table_to_csv(self.tableWidget_coordinates)
|
||||||
|
)
|
||||||
|
|
||||||
def init_ui(self) -> None:
|
def init_ui(self) -> None:
|
||||||
"""Setup all ui elements"""
|
"""Setup all ui elements"""
|
||||||
|
|
||||||
@@ -686,6 +692,34 @@ class MotorApp(QWidget):
|
|||||||
partial(self.move_to_row_coordinates, self.tableWidget_coordinates, row)
|
partial(self.move_to_row_coordinates, self.tableWidget_coordinates, row)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def export_table_to_csv(self, table: QtWidgets.QTableWidget):
|
||||||
|
options = QFileDialog.Options()
|
||||||
|
filePath, _ = QFileDialog.getSaveFileName(
|
||||||
|
self, "Save File", "", "CSV Files (*.csv);;All Files (*)", options=options
|
||||||
|
)
|
||||||
|
|
||||||
|
if filePath:
|
||||||
|
if not filePath.endswith(".csv"):
|
||||||
|
filePath += ".csv"
|
||||||
|
|
||||||
|
with open(filePath, mode="w", newline="") as file:
|
||||||
|
writer = csv.writer(file)
|
||||||
|
|
||||||
|
# Write the header
|
||||||
|
header = []
|
||||||
|
for col in range(2, table.columnCount()):
|
||||||
|
header_item = table.horizontalHeaderItem(col)
|
||||||
|
header.append(header_item.text() if header_item else "")
|
||||||
|
writer.writerow(header)
|
||||||
|
|
||||||
|
# Write the content
|
||||||
|
for row in range(table.rowCount()):
|
||||||
|
row_data = []
|
||||||
|
for col in range(2, table.columnCount()):
|
||||||
|
item = table.item(row, col)
|
||||||
|
row_data.append(item.text() if item else "")
|
||||||
|
writer.writerow(row_data)
|
||||||
|
|
||||||
def save_absolute_coordinates(self):
|
def save_absolute_coordinates(self):
|
||||||
self.generate_table_coordinate(
|
self.generate_table_coordinate(
|
||||||
self.tableWidget_coordinates,
|
self.tableWidget_coordinates,
|
||||||
|
|||||||
Reference in New Issue
Block a user