From be1892ac52ecf946e6beb709f794f7804a603aea Mon Sep 17 00:00:00 2001 From: Eelke Klein Date: Fri, 13 Jan 2017 19:09:58 +0100 Subject: [PATCH] Wat copy paste code en csvwriter alvast toegevoegd maar nog uitgeschakeld. --- Ivory.pro | 6 ++++-- csvwriter.cpp | 30 ++++++++++++++++++++++++++++++ csvwriter.h | 23 +++++++++++++++++++++++ mainwindow.cpp | 31 +++++++++++++++++++++++++++++++ mainwindow.ui | 2 +- ui_mainwindow.h | 2 +- 6 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 csvwriter.cpp create mode 100644 csvwriter.h diff --git a/Ivory.pro b/Ivory.pro index 9a355b0..3dd848c 100644 --- a/Ivory.pro +++ b/Ivory.pro @@ -28,7 +28,8 @@ SOURCES += main.cpp\ asyncdbconnection.cpp \ tsqueue.cpp \ win32event.cpp \ - waithandlelist.cpp + waithandlelist.cpp \ + csvwriter.cpp HEADERS += mainwindow.h \ serverproperties.h \ @@ -41,7 +42,8 @@ HEADERS += mainwindow.h \ asyncdbconnection.h \ tsqueue.h \ win32event.h \ - waithandlelist.h + waithandlelist.h \ + csvwriter.h FORMS += mainwindow.ui \ serverproperties.ui diff --git a/csvwriter.cpp b/csvwriter.cpp new file mode 100644 index 0000000..a52babe --- /dev/null +++ b/csvwriter.cpp @@ -0,0 +1,30 @@ +#include "csvwriter.h" + +CsvWriter::CsvWriter() +{ + +} + + +//int complexField = (strchr(field, csvWriter->delimiter_) || strchr(field, '\n') || strchr(field, '\"')) ? 1 : 0; + +void CsvWriter::writeField(QString field) { + // if field contains any of seperator quote or newline then it needs to be quoted + // when quoted quotes need to be doubled to escape them + bool needs_quotes = false; + int new_len = field.length(); + for (auto ch : field) { + if (ch == m_quote) { + ++new_len; + } + if (ch == '\n' || ch == m_seperator || ch == m_quote) { + needs_quotes = true; + + } + } + if (needs_quotes) { + new_len += 2; + + + } +} diff --git a/csvwriter.h b/csvwriter.h new file mode 100644 index 0000000..054284b --- /dev/null +++ b/csvwriter.h @@ -0,0 +1,23 @@ +#ifndef CSVWRITER_H +#define CSVWRITER_H + +#include +#include + +class CsvWriter { +public: + CsvWriter(); + void setDestination(QFileDevice *output); + void setDestination(QString filename); + + void setSeperator(QChar ch); + void writeField(QString field); + void nextLine(); +private: + QChar m_seperator = ','; + QChar m_quote = '\''; + + QFileDevice *m_output = nullptr; +}; + +#endif // CSVWRITER_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 60d05e3..a940e0c 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -435,3 +435,34 @@ void MainWindow::on_actionAbout_triggered() { // } + +#if false + +void Copy( ) +{ + QString selected_text; + // You need a pair of indexes to find the row changes + QModelIndex previous = indexes.first(); + indexes.removeFirst(); + foreach(current, indexes) + { + QVariant data = model->data(current); + QString text = data.toString(); + // At this point `text` contains the text in one cell + selected_text.append(text); + // If you are at the start of the row the row number of the previous index + // isn't the same. Text is followed by a row separator, which is a newline. + if (current.row() != previous.row()) + { + selected_text.append('\n'); + } + // Otherwise it's the same row, so append a column separator, which is a tab. + else + { + selected_text.append('\t'); + } + previous = current; + } + QApplication.clipboard().setText(selected_text); +} +#endif diff --git a/mainwindow.ui b/mainwindow.ui index 66aaeb1..aed18b7 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -38,7 +38,7 @@ - 2 + 1 diff --git a/ui_mainwindow.h b/ui_mainwindow.h index 50859f2..a7e0703 100644 --- a/ui_mainwindow.h +++ b/ui_mainwindow.h @@ -205,7 +205,7 @@ public: retranslateUi(MainWindow); - tabWidget->setCurrentIndex(2); + tabWidget->setCurrentIndex(1); QMetaObject::connectSlotsByName(MainWindow);