Copy as C string added.

This commit is contained in:
Eelke Klein 2017-02-05 08:23:06 +01:00
parent 4a2c6cc396
commit df866d7b67
9 changed files with 89 additions and 32 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

View file

@ -12,6 +12,8 @@
#include <windows.h>
#include <algorithm>
#include <QCloseEvent>
#include <QMetaObject>
#include <QMetaMethod>
#include <querytab.h>
#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();
}
}

View file

@ -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

View file

@ -88,6 +88,7 @@
<string>Edit</string>
</property>
<addaction name="actionCopy"/>
<addaction name="actionCopy_as_C_string"/>
</widget>
<addaction name="menuTest"/>
<addaction name="menuEdit"/>
@ -109,6 +110,7 @@
<addaction name="actionClose"/>
<addaction name="separator"/>
<addaction name="actionCopy"/>
<addaction name="actionCopy_as_C_string"/>
<addaction name="separator"/>
<addaction name="actionExecute_SQL"/>
<addaction name="actionExplain"/>
@ -272,6 +274,19 @@
<string>Ctrl+C</string>
</property>
</action>
<action name="actionCopy_as_C_string">
<property name="icon">
<iconset>
<normalon>:/icons/token_shortland_character.png</normalon>
</iconset>
</property>
<property name="text">
<string>Copy as C-string</string>
</property>
<property name="shortcut">
<string>Ctrl+Alt+C</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>

View file

@ -10,6 +10,7 @@
#include <QTextCodec>
#include <QTextDocumentFragment>
#include <QTextStream>
#include <QClipboard>
#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);
}

View file

@ -47,6 +47,8 @@ public:
void cancel();
bool canClose();
void copyQueryAsCString();
private:
// struct ResultTab {

View file

@ -18,5 +18,6 @@
<file>icons/16x16/document_yellow.png</file>
<file>icons/backups.png</file>
<file>icons/page_white_copy.png</file>
<file>icons/token_shortland_character.png</file>
</qresource>
</RCC>

View file

@ -3,6 +3,7 @@
#include <QApplication>
#include <QTextStream>
#include <QClipboard>
#include <sstream>
// 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;
}

1
util.h
View file

@ -6,5 +6,6 @@
QString msfloatToHumanReadableString(float ms);
void copySelectionToClipboard(const QTableView *view);
QString ConvertToMultiLineCString(const QString &in);
#endif // UTIL_H