[resultsscreen] copy and paste to excel works
This commit is contained in:
parent
519463e690
commit
9524d8f8f8
@ -1,3 +1,6 @@
|
|||||||
|
import io
|
||||||
|
import csv
|
||||||
|
from PyQt5 import QtWidgets
|
||||||
from PyQt5.QtGui import QFont, QKeySequence
|
from PyQt5.QtGui import QFont, QKeySequence
|
||||||
from PyQt5.QtWidgets import QAction, QComboBox, QFrame, QGroupBox, QHBoxLayout, QLabel, QSplitter, QStackedLayout, QTableWidget, QTableWidgetItem, QTextEdit, QVBoxLayout, QWidget
|
from PyQt5.QtWidgets import QAction, QComboBox, QFrame, QGroupBox, QHBoxLayout, QLabel, QSplitter, QStackedLayout, QTableWidget, QTableWidgetItem, QTextEdit, QVBoxLayout, QWidget
|
||||||
from PyQt5.QtCore import QItemSelectionModel, Qt, reset, pyqtSignal
|
from PyQt5.QtCore import QItemSelectionModel, Qt, reset, pyqtSignal
|
||||||
@ -21,14 +24,20 @@ class ResultsScreen(QWidget):
|
|||||||
font.setPointSize(25)
|
font.setPointSize(25)
|
||||||
label.setStyleSheet("color: #2D4B9A")
|
label.setStyleSheet("color: #2D4B9A")
|
||||||
label.setFont(font)
|
label.setFont(font)
|
||||||
self._stack.addWidget(label) # 0
|
self._stack.addWidget(label) # 0
|
||||||
self._stack.addWidget(self.table) # 1
|
self._stack.addWidget(self.table) # 1
|
||||||
self.setLayout(self._stack)
|
self.setLayout(self._stack)
|
||||||
|
|
||||||
self._back_action = QAction("back")
|
self._back_action = QAction("back")
|
||||||
self._back_action.setShortcut(QKeySequence("escape"))
|
self._back_action.setShortcut(QKeySequence("escape"))
|
||||||
self._back_action.triggered.connect(self.on_back)
|
self._back_action.triggered.connect(self.on_back)
|
||||||
|
|
||||||
|
self._copy_action = QAction("copy")
|
||||||
|
self._copy_action.setShortcut(QKeySequence("ctrl+c"))
|
||||||
|
self._copy_action.triggered.connect(self.copy_selection)
|
||||||
|
|
||||||
self.addAction(self._back_action)
|
self.addAction(self._back_action)
|
||||||
|
self.addAction(self._copy_action)
|
||||||
|
|
||||||
def set_results(self, measurement_results):
|
def set_results(self, measurement_results):
|
||||||
if len(measurement_results) == 0:
|
if len(measurement_results) == 0:
|
||||||
@ -54,6 +63,26 @@ class ResultsScreen(QWidget):
|
|||||||
|
|
||||||
self._stack.setCurrentIndex(1)
|
self._stack.setCurrentIndex(1)
|
||||||
|
|
||||||
|
def copy_selection(self):
|
||||||
|
"""
|
||||||
|
solution stolen from StackOverflow https://stackoverflow.com/a/55204654
|
||||||
|
"""
|
||||||
|
selection = self.table.selectedIndexes()
|
||||||
|
if selection:
|
||||||
|
rows = sorted(index.row() for index in selection)
|
||||||
|
columns = sorted(index.column() for index in selection)
|
||||||
|
rowcount = rows[-1] - rows[0] + 1
|
||||||
|
colcount = columns[-1] - columns[0] + 1
|
||||||
|
table = [[''] * colcount for _ in range(rowcount)]
|
||||||
|
for index in selection:
|
||||||
|
row = index.row() - rows[0]
|
||||||
|
column = index.column() - columns[0]
|
||||||
|
table[row][column] = index.data()
|
||||||
|
stream = io.StringIO()
|
||||||
|
csv.writer(stream, delimiter='\t').writerows(table)
|
||||||
|
QtWidgets.qApp.clipboard().setText(stream.getvalue())
|
||||||
|
return
|
||||||
|
|
||||||
def on_back(self):
|
def on_back(self):
|
||||||
self.back_signal.emit()
|
self.back_signal.emit()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user