From df866d7b6747ff7b4b287d6e9d0ae6f254553961 Mon Sep 17 00:00:00 2001 From: Eelke Klein Date: Sun, 5 Feb 2017 08:23:06 +0100 Subject: [PATCH] Copy as C string added. --- icons/token_shortland_character.png | Bin 0 -> 2274 bytes mainwindow.cpp | 55 ++++++++++++---------------- mainwindow.h | 1 + mainwindow.ui | 15 ++++++++ querytab.cpp | 15 ++++++++ querytab.h | 2 + resources.qrc | 1 + util.cpp | 31 ++++++++++++++++ util.h | 1 + 9 files changed, 89 insertions(+), 32 deletions(-) create mode 100644 icons/token_shortland_character.png diff --git a/icons/token_shortland_character.png b/icons/token_shortland_character.png new file mode 100644 index 0000000000000000000000000000000000000000..83314acd394120158481b2d9883fc239d7860eca GIT binary patch literal 2274 zcmaJ@c~nyQ9=|}jlnZU*@TOrK67cT)b!O%k=bbJ zSTD25%$BFQG){FgLq$>#m>diR`rf6MuNKHu&0$ITB9U1L4R zaSi|g)*)+|5yll|dM(V1UwL}^m~nA{STRtfBmq)#WFSD}N#a3Nh=`L2Mt~e%T1q<@ z000QIASwomVQ-*tB_a&Rgu$ppQX?Ax0s>W14mTNuQ1M`*KukrCUTH<61UxEw6N!yu zOBo4SiWQ$3y){{(0oWFCJj%( z<7g~8nMj-AG9_Gv2oyszT;AW@;IDE`RuD;zk(r=OkP7mGWfBqUQ^^#;*K=WfRqr#G z_w`(uU*%$r$zV-`{jWjKTp8VC+J3RE@$kj?pxEeknbFpQ#oSi_FgrSgNsCh5yRd0H z-}&2xA72|rPUZ``c}IP%SLCzr-4*t)=vF7Kv)EWw6jgP+;x}^_7VGU0BPhIU4|?4- zj}t4`ZXK)0n-kxSo+U&__yw}NtMJt)h}C@t;?Gm}Zx`$Ozp4MGzV8C|Ximq2jLruc zE&8%!4Q`JDUU}3MmS-C3^rh!sa=7xPuw}w7#dzv~B87iiCr1Cb1$6~mbm{IFxSy@{j4~}?r5u^`%PXvr&P2j zF5(s`$W2S~J$u69W-4nL1a_oF0QQTV5|I?m?qA!s(B0V++58=5+W>mS&|RMoyNi3n z-dAjNC{L=H9qo+lv&fM;ATiF9bq95_VgK;08`1>a{0IXIJ*suadS5Qny#ros#r7&g5qHd-1I@>piV!& zM%KG3qisR1f_(KxxBdPd@WBt@P0OUcHXCY^-nhGEu%Ono$}N{}OSD#zHSAMIdTU#w ztJ{XgcbrcKhY{!^*yZIt1C96Qdto=*zmu=4JDw3!+4}0#ipuaqsFsAm!im-=&J_w4 zJ6{%Uk>yZeU4v>?=Lvto`l@?)KV8mB&kRE*Y4g>N&wEA;1P%QxINf{gz1-(nwsbnX z+|KNUB{+MFL$dc*R$IOv?xOy7S@-zn0qWa8-A?BX?x$<>+wY!P-*#c;+EL4exs8nx zw{LrK20Ibg>7hWYA)uh4ffW^<_h#GUAAsenv`q=*mtfYLJaie%@@A(qJj@=gJ$S2m zNf{S9IyWxqA2wxU_bc@S{Cpr~D@fIt;2wh5)9FT=cOLxCshPgSu1&k?rgUOk1FQVT zAKy+dy5{L|G`9Li@AlcDSsUZhj5E`>@DM$Q5dJ$%QEA;D*LPiq@Z|CFlz*k%dpF#{ zEm+cNP|-8P{K0lFB5{=QYu!1DkJvMdq)}>pz9*mZL7W`jUN(;z0E@lbZ)>|*Uz&xD z)p`As@jZ;uH@_NrcT#<)vM#xvaQ5x3Gfqx>-~+Yplb0ukKL|^-?oPr&(Rooq+==m# zab;^ezR^B)_<5UGEIslLZD-iE-Oun78CJp{MD-b(9i%P^@r8(B;e258dZOCD eZ1bB|00E2@3|@44Y=JlZb%d}&nP=&7x&HxL_^yNi literal 0 HcmV?d00001 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