-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueryWidget.cpp
More file actions
97 lines (75 loc) · 2.28 KB
/
QueryWidget.cpp
File metadata and controls
97 lines (75 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "QueryWidget.h"
#include "ui_QueryWidget.h"
#include <QMessageBox>
#include <QSqlQueryModel>
#include <QSqlError>
#include "ExportSelectionDialog.h"
QueryWidget::QueryWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::QueryWidget)
{
// Setup UI
ui->setupUi(this);
// Attach SQL Highlighter
m_highlighter = new SQLHighlighter(ui->queryEdit->document());
// Connect history and saved queries
connect(ui->queryHistoryWidget, SIGNAL(querySelected(QString)), this, SLOT(setQuery(QString)));
connect(ui->savedQueryWidget, SIGNAL(querySelected(QString)), this, SLOT(setQuery(QString)));
connect(ui->savedQueryWidget, SIGNAL(addButtonClicked()), this, SLOT(saveQuery()));
}
QueryWidget::~QueryWidget()
{
// Delete highlighter
delete m_highlighter;
// Delete UI
delete ui;
}
void QueryWidget::setDatabase(QSqlDatabase *database)
{
// Set the database
m_database = database;
// Setup the model
m_model = new QSqlQueryModel(this);
// Setup saved query widget
ui->savedQueryWidget->setDatabase(m_database);
}
void QueryWidget::on_runButton_clicked()
{
// Set the query from the query input box
m_model->setQuery(ui->queryEdit->toPlainText(), *m_database);
// Check for errors
if (m_model->lastError().type() != QSqlError::NoError) {
QMessageBox::critical(this, "Could not execute query", m_model->lastError().text());
return;
}
// Add to history
ui->queryHistoryWidget->addQuery(ui->queryEdit->toPlainText());
// Attach the result table to the model
ui->resultTableView->setModel(m_model);
// Enable button
ui->exportButton->setEnabled(true);
// Signal that a refresh is needed
emit refreshNeeded();
}
void QueryWidget::on_clearButton_clicked()
{
// Clear the query text
ui->queryEdit->clear();
}
void QueryWidget::on_exportButton_clicked()
{
// Create export selection dialog
ExportSelectionDialog exportSelectionDialog;
// Initialise the dialog
exportSelectionDialog.init(ui->resultTableView);
// Execute the dialog
exportSelectionDialog.exec();
}
void QueryWidget::saveQuery()
{
ui->savedQueryWidget->addQuery(ui->queryEdit->toPlainText());
}
void QueryWidget::setQuery(QString query)
{
ui->queryEdit->setText(query);
}