diff --git a/icons/token_shortland_character.png b/icons/token_shortland_character.png new file mode 100644 index 0000000..83314ac Binary files /dev/null and b/icons/token_shortland_character.png differ diff --git a/mainwindow.cpp b/mainwindow.cpp index 457f8eb..e0c8310 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include "util.h" #include "MasterController.h" @@ -155,37 +157,6 @@ 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 - void MainWindow::on_actionExecute_SQL_triggered() { QueryTab *tab = GetActiveQueryTab(); @@ -202,7 +173,6 @@ void MainWindow::on_actionExplain_triggered() } } - void MainWindow::on_actionExplain_Analyze_triggered() { QueryTab *tab = GetActiveQueryTab(); @@ -269,5 +239,26 @@ void MainWindow::on_actionCopy_triggered() if (tv) { copySelectionToClipboard(tv); } + else { + const QMetaObject *meta = w->metaObject(); + int i = meta->indexOfSlot("copy"); + if (i != -1) { + QMetaMethod method = meta->method(i); + method.invoke(w, Qt::AutoConnection); + } + } //this->ui-> } + + + +void MainWindow::on_actionCopy_as_C_string_triggered() +{ + // Find which edit is active, copy the selected text or all text if no selection present + // Put quote's around each line and add escapes. + + QueryTab *tab = GetActiveQueryTab(); + if (tab) { + tab->copyQueryAsCString(); + } +} diff --git a/mainwindow.h b/mainwindow.h index f091695..9f53cb2 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -81,6 +81,7 @@ private slots: void on_actionExplain_triggered(); void on_actionShow_connection_manager_triggered(); void on_actionCopy_triggered(); + void on_actionCopy_as_C_string_triggered(); }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index e0486e4..7a19461 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -88,6 +88,7 @@ Edit + @@ -109,6 +110,7 @@ + @@ -272,6 +274,19 @@ Ctrl+C + + + + :/icons/token_shortland_character.png + + + + Copy as C-string + + + Ctrl+Alt+C + + diff --git a/querytab.cpp b/querytab.cpp index 234ee94..61979a9 100644 --- a/querytab.cpp +++ b/querytab.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "explaintreemodelitem.h" #include "json/json.h" #include "mainwindow.h" @@ -550,3 +551,17 @@ void QueryTab::clearResult() resultList.clear(); // ui->lblRowCount->clear(); } + +void QueryTab::copyQueryAsCString() +{ + QString command; + QTextCursor cursor = ui->queryEdit->textCursor(); + if (cursor.hasSelection()) { + command = cursor.selection().toPlainText(); + } + else { + command = ui->queryEdit->toPlainText(); + } + QString cs = ConvertToMultiLineCString(command); + QApplication::clipboard()->setText(cs); +} diff --git a/querytab.h b/querytab.h index 273efea..7765cb7 100644 --- a/querytab.h +++ b/querytab.h @@ -47,6 +47,8 @@ public: void cancel(); bool canClose(); + + void copyQueryAsCString(); private: // struct ResultTab { diff --git a/resources.qrc b/resources.qrc index 8f234dd..86bb166 100644 --- a/resources.qrc +++ b/resources.qrc @@ -18,5 +18,6 @@ icons/16x16/document_yellow.png icons/backups.png icons/page_white_copy.png + icons/token_shortland_character.png diff --git a/util.cpp b/util.cpp index 6bab3b6..cc3cd7a 100644 --- a/util.cpp +++ b/util.cpp @@ -3,6 +3,7 @@ #include #include #include +#include // Supported range from microseconds to seconds // min:sec to hours::min::sec @@ -81,3 +82,33 @@ void copySelectionToClipboard(const QTableView *view) QApplication::clipboard()->setText(clipboard_string); } } + +QString ConvertToMultiLineCString(const QString &in) +{ + // We need to atleast escape " and \ + // also any multi byte utf8 char + + QString out; + out.append('"'); + QByteArray ba = in.toUtf8(); + for (auto c : ba) { + if (c == '\\') { + out.append("\\\\"); + } + else if (c == '"') { + out.append("\\\""); + } + else if (uchar(c) > 127) { + out.append(QString("\\x%1").arg(uchar(c), 2, 16, QChar('0'))); + } + else if (c == '\n') { + // at end of line we add a space and a new line in the string then we put in the end quote go to the next line and put the open quote + out.append(" \\n\"\n\""); + } + else { + out.append(c); + } + } + out.append('"'); + return out; +} diff --git a/util.h b/util.h index ac204ce..00d7308 100644 --- a/util.h +++ b/util.h @@ -6,5 +6,6 @@ QString msfloatToHumanReadableString(float ms); void copySelectionToClipboard(const QTableView *view); +QString ConvertToMultiLineCString(const QString &in); #endif // UTIL_H