MCPcopy Create free account
hub / github.com/KDE/labplot / pasteIntoSelection

Method pasteIntoSelection

src/frontend/spreadsheet/SpreadsheetView.cpp:1798–2047  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1796}*/
1797
1798void SpreadsheetView::pasteIntoSelection() {
1799 if (m_spreadsheet->columnCount() < 1 || m_spreadsheet->rowCount() < 1)
1800 return;
1801
1802 const QMimeData* mime_data = QApplication::clipboard()->mimeData();
1803 if (!mime_data->hasFormat(QStringLiteral("text/plain")))
1804 return;
1805
1806 PERFTRACE(QStringLiteral("paste selected cells"));
1807 WAIT_CURSOR;
1808 m_spreadsheet->beginMacro(i18n("%1: paste from clipboard", m_spreadsheet->name()));
1809
1810 int first_col = firstSelectedColumn();
1811 int last_col = lastSelectedColumn();
1812 int first_row = firstSelectedRow();
1813 int last_row = lastSelectedRow();
1814 int input_row_count = 0;
1815 int input_col_count = 0;
1816 QVector<QStringList> cellTexts;
1817
1818 try {
1819 QString input_str = QString::fromUtf8(mime_data->data(QStringLiteral("text/plain"))).trimmed();
1820 QString separator;
1821 if (input_str.indexOf(QLatin1String("\r\n")) != -1)
1822 separator = QLatin1String("\r\n");
1823 else
1824 separator = QLatin1Char('\n');
1825
1826 QStringList input_rows(input_str.split(separator));
1827 input_str.clear(); // not needed anymore, release memory
1828 input_row_count = input_rows.count();
1829 input_col_count = 0;
1830 bool hasTabs = false;
1831 if (input_row_count > 0 && input_rows.constFirst().indexOf(QLatin1Char('\t')) != -1)
1832 hasTabs = true;
1833
1834 const auto numberLocale = QLocale();
1835 // TEST ' ' as group separator:
1836 for (int i = 0; i < input_row_count; i++) {
1837 const auto& rowText = input_rows.at(i).trimmed();
1838 if (hasTabs)
1839 cellTexts.append(rowText.split(QLatin1Char('\t')));
1840 else if (numberLocale.groupSeparator().trimmed().isEmpty()
1841 && !(numberLocale.numberOptions() & QLocale::OmitGroupSeparator)) // locale with ' ' as group separator && omit group separator not set
1842 cellTexts.append(rowText.split(QRegularExpression(QStringLiteral("\\s\\s")), (Qt::SplitBehavior)0x1)); // split with two spaces
1843 else
1844 cellTexts.append(rowText.split(QRegularExpression(QStringLiteral("\\s+"))));
1845
1846 if (cellTexts.at(i).count() > input_col_count)
1847 input_col_count = cellTexts.at(i).count();
1848 }
1849
1850 input_rows.clear(); // not needed anymore, release memory
1851
1852 // when pasting DateTime data in the format 'yyyy-MM-dd hh:mm:ss' and similar, it get's split above because of the space separator.
1853 // here we check whether we have such a situation and merge the first two columns to get the proper value,
1854 // for example "2018-03-21 10:00:00" and not "2018-03-21" and "10:00:00"
1855 if (!cellTexts.isEmpty() && cellTexts.constFirst().size() > 1) {

Callers

nothing calls this directly

Calls 15

beginMacroMethod · 0.80
splitMethod · 0.80
appendMethod · 0.80
setIntegersMethod · 0.80
setBigIntsMethod · 0.80
asStringColumnMethod · 0.80
endMacroMethod · 0.80
columnCountMethod · 0.45
rowCountMethod · 0.45
nameMethod · 0.45
dataMethod · 0.45
indexOfMethod · 0.45

Tested by

no test coverage detected