Add new menu item to import from CSV data in the system clipboard

This adds a new menu item to the File -> Import menu for importing a
table from the CSV data in the system clipboard instead of a file on
disk.

See issue #2462.
This commit is contained in:
Martin Kleusberg
2020-11-08 18:47:24 +01:00
parent b07621195d
commit 9d87b963a4
2 changed files with 67 additions and 22 deletions

View File

@@ -50,6 +50,7 @@
#include <QPrinter>
#include <QPrintPreviewDialog>
#include <QPushButton>
#include <QTemporaryFile>
#include <QToolButton>
#ifdef Q_OS_MACX //Needed only on macOS
@@ -1313,29 +1314,47 @@ void MainWindow::mainTabSelected(int /*tabindex*/)
void MainWindow::importTableFromCSV()
{
QStringList file_filter;
file_filter << FILE_FILTER_CSV
<< FILE_FILTER_TSV
<< FILE_FILTER_DSV
<< FILE_FILTER_TXT
<< FILE_FILTER_DAT
<< FILE_FILTER_ALL;
QStringList wFiles = FileDialog::getOpenFileNames(
OpenCSVFile,
this,
tr("Choose text files"),
file_filter.join(";;"));
std::vector<QString> validFiles;
for(const auto& file : wFiles) {
if (QFile::exists(file))
validFiles.push_back(file);
}
if (!validFiles.empty())
// Are we importing from a file or from the clipboard?
if(sender() == ui->fileImportCSVAction)
{
ImportCsvDialog dialog(validFiles, &db, this);
// Ask user to specify file(s)
QStringList file_filter;
file_filter << FILE_FILTER_CSV
<< FILE_FILTER_TSV
<< FILE_FILTER_DSV
<< FILE_FILTER_TXT
<< FILE_FILTER_DAT
<< FILE_FILTER_ALL;
QStringList wFiles = FileDialog::getOpenFileNames(
OpenCSVFile,
this,
tr("Choose text files"),
file_filter.join(";;"));
std::vector<QString> validFiles;
for(const auto& file : wFiles) {
if (QFile::exists(file))
validFiles.push_back(file);
}
if (!validFiles.empty())
{
ImportCsvDialog dialog(validFiles, &db, this);
if (dialog.exec())
refreshTableBrowsers();
}
} else if(sender() == ui->actionFileImportCsvClipboard) {
// Save clipboard content to temporary file
QTemporaryFile temp("csv_clipboard");
temp.open();
QClipboard* clipboard = QGuiApplication::clipboard();
temp.write(clipboard->text().toUtf8());
temp.close();
ImportCsvDialog dialog({temp.fileName()}, &db, this);
if (dialog.exec())
refreshTableBrowsers();
}
@@ -1791,6 +1810,7 @@ void MainWindow::activateFields(bool enable)
ui->fileExportCSVAction->setEnabled(enable);
ui->fileExportSQLAction->setEnabled(enable);
ui->fileImportCSVAction->setEnabled(enable && write);
ui->actionFileImportCsvClipboard->setEnabled(enable && write);
ui->editCreateTableAction->setEnabled(enable && write);
ui->editCreateIndexAction->setEnabled(enable && write);
ui->actionDbPrint->setEnabled(enable);

View File

@@ -764,6 +764,7 @@ You can drag SQL statements from an object row and drop them into other applicat
</property>
<addaction name="fileImportSQLAction"/>
<addaction name="fileImportCSVAction"/>
<addaction name="actionFileImportCsvClipboard"/>
</widget>
<widget class="QMenu" name="menuExport">
<property name="title">
@@ -2219,6 +2220,14 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
<string>Detach database file attached to the current database connection</string>
</property>
</action>
<action name="actionFileImportCsvClipboard">
<property name="text">
<string>Table from CSV data in Clipboard...</string>
</property>
<property name="toolTip">
<string>This treats the current clipboard contents as a CSV file and opens the same import wizard that is used for importing CSV data from a file.</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
@@ -3641,6 +3650,22 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
</hint>
</hints>
</connection>
<connection>
<sender>actionFileImportCsvClipboard</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>importTableFromCSV()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>518</x>
<y>314</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>fileOpen()</slot>