0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-24 00:33:02 +03:00
LimeReport/limereport/databrowser/lrsqleditdialog.cpp

399 lines
14 KiB
C++
Raw Permalink Normal View History

2016-02-17 10:11:00 +03:00
/***************************************************************************
* This file is part of the Lime Report project *
2021-08-18 20:21:36 +03:00
* Copyright (C) 2021 by Alexander Arin *
2016-02-17 10:11:00 +03:00
* arin_a@bk.ru *
* *
** GNU General Public License Usage **
* *
* This library is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
** GNU Lesser General Public License **
* *
* This library is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library. *
* If not, see <http://www.gnu.org/licenses/>. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
****************************************************************************/
#include "lrsqleditdialog.h"
#include "ui_lrsqleditdialog.h"
2016-02-17 10:11:00 +03:00
#include "lrreportengine_p.h"
2016-02-17 10:11:00 +03:00
#include <QDebug>
#include <QMessageBox>
#include <QSqlDatabase>
2016-02-17 10:11:00 +03:00
namespace LimeReport {
SQLEditDialog::SQLEditDialog(QWidget* parent, LimeReport::DataSourceManager* dataSources,
SQLDialogMode dialogMode):
QDialog(parent),
ui(new Ui::SQLEditDialog),
m_datasources(dataSources),
m_dialogMode(dialogMode),
m_oldDatasourceName(""),
m_settings(0),
m_ownedSettings(false)
{
2016-02-17 10:11:00 +03:00
ui->setupUi(this);
m_masterDatasources = new QCompleter(this);
ui->leMaster->setCompleter(m_masterDatasources);
ui->leChild->setCompleter(m_masterDatasources);
ui->gbFieldsMap->setVisible(false);
ui->gbDataPreview->setVisible(false);
ui->pbHidePreview->setVisible(false);
ui->rbSubQuery->setChecked(true);
ui->rbProxy->setVisible(false);
ui->rbSubQuery->setVisible(false);
ui->leMaster->setVisible(false);
ui->lbMaster->setVisible(false);
ui->fieldsMap->setHorizontalHeaderItem(0, new QTableWidgetItem("master"));
ui->fieldsMap->setHorizontalHeaderItem(1, new QTableWidgetItem("detail"));
2016-02-17 10:11:00 +03:00
ui->fieldsMap->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
2016-02-17 10:11:00 +03:00
ui->pnlChildDatasource->setVisible(false);
connect(ui->pbPreview, SIGNAL(pressed()), this, SLOT(slotPreviewData()));
connect(ui->pbHidePreview, SIGNAL(pressed()), this, SLOT(slotHidePreview()));
}
SQLEditDialog::~SQLEditDialog()
{
2016-02-17 10:11:00 +03:00
delete ui;
if (m_settings && m_ownedSettings)
delete m_settings;
}
QSettings* SQLEditDialog::settings()
{
if (m_settings) {
2016-02-17 10:11:00 +03:00
return m_settings;
} else {
m_settings = new QSettings("LimeReport", QCoreApplication::applicationName());
2016-02-17 10:11:00 +03:00
m_ownedSettings = true;
return m_settings;
}
}
void SQLEditDialog::setSettings(QSettings* value, bool owned)
{
if (m_settings && m_ownedSettings)
delete m_settings;
m_settings = value;
2016-02-17 10:11:00 +03:00
m_ownedSettings = owned;
}
void SQLEditDialog::accept()
{
2016-02-17 10:11:00 +03:00
SQLEditResult result;
if (ui->tabWidget->currentIndex() == 1) {
result.resultMode = SQLEditResult::CSVText;
} else if (!ui->cbSubdetail->isChecked()) {
result.resultMode = SQLEditResult::Query;
2016-02-17 10:11:00 +03:00
} else {
if (ui->rbSubQuery->isChecked())
result.resultMode = SQLEditResult::SubQuery;
else
result.resultMode = SQLEditResult::SubProxy;
2016-02-17 10:11:00 +03:00
}
result.connectionName
= ConnectionDesc::connectionNameForReport(ui->cbbConnection->currentText());
result.datasourceName = ui->leDatasourceName->text();
result.sql = ui->sqlText->toPlainText();
result.csv = ui->csvText->toPlainText();
result.dialogMode = m_dialogMode;
result.oldDatasourceName = m_oldDatasourceName;
2016-02-17 10:11:00 +03:00
result.subdetail = ui->cbSubdetail->isChecked();
result.masterDatasource = ui->leMaster->text();
result.childDataSource = ui->leChild->text();
result.separator = ui->leSeparator->text();
result.firstRowIsHeader = ui->cbUseFirstRowAsHeader->isChecked();
2016-02-17 10:11:00 +03:00
if (ui->fieldsMap->rowCount() > 0) {
for (int i = 0; i < ui->fieldsMap->rowCount(); ++i) {
2016-02-17 10:11:00 +03:00
LimeReport::FieldsCorrelation fieldsCorrelation;
fieldsCorrelation.master = ui->fieldsMap->item(i, 0)
? ui->fieldsMap->item(i, 0)->data(Qt::DisplayRole).toString()
: "";
fieldsCorrelation.detail = ui->fieldsMap->item(i, 1)
? ui->fieldsMap->item(i, 1)->data(Qt::DisplayRole).toString()
: "";
2016-02-17 10:11:00 +03:00
result.fieldMap.append(fieldsCorrelation);
}
}
try {
check();
emit signalSqlEditingFinished(result);
QDialog::accept();
} catch (LimeReport::ReportError& exception) {
QMessageBox::critical(this, tr("Error"), exception.what());
2016-02-17 10:11:00 +03:00
}
}
void SQLEditDialog::showEvent(QShowEvent*)
{
2016-02-17 10:11:00 +03:00
ui->lblInfo->setVisible(false);
initConnections();
readSettings();
}
void SQLEditDialog::closeEvent(QCloseEvent*) { writeSetting(); }
2016-02-17 10:11:00 +03:00
void SQLEditDialog::hideEvent(QHideEvent*) { writeSetting(); }
2016-02-17 10:11:00 +03:00
void SQLEditDialog::check()
{
if (ui->leDatasourceName->text().isEmpty())
throw LimeReport::ReportError(tr("Datasource Name is empty!"));
if (ui->sqlText->toPlainText().isEmpty() && (!ui->rbProxy))
throw LimeReport::ReportError(tr("SQL is empty!"));
if (m_dialogMode == AddMode) {
if (m_datasources->containsDatasource(ui->leDatasourceName->text())) {
throw LimeReport::ReportError(
QString(tr("Datasource with name: \"%1\" already exists!"))
.arg(ui->leDatasourceName->text()));
2016-02-17 10:11:00 +03:00
}
}
}
void SQLEditDialog::initConnections()
{
foreach (QString connectionName, QSqlDatabase::connectionNames()) {
ui->cbbConnection->addItem(QIcon(":/databrowser/images/plug-connect.png"),
ConnectionDesc::connectionNameForUser(connectionName));
2016-02-17 10:11:00 +03:00
}
foreach (QString connectionName, m_datasources->connectionNames()) {
connectionName = (connectionName.compare(QSqlDatabase::defaultConnection) == 0)
? tr("defaultConnection")
: connectionName;
if (ui->cbbConnection->findText(connectionName, Qt::MatchExactly) == -1)
ui->cbbConnection->addItem(QIcon(":/databrowser/images/plug-disconnect.png"),
ConnectionDesc::connectionNameForUser(connectionName));
2016-02-17 10:11:00 +03:00
}
2016-02-17 10:11:00 +03:00
ui->cbbConnection->setCurrentIndex(ui->cbbConnection->findText(m_defaultConnection));
if (!m_oldDatasourceName.isEmpty()) {
ui->cbbConnection->setCurrentIndex(
ui->cbbConnection->findText(ConnectionDesc::connectionNameForUser(
m_datasources->connectionName(m_oldDatasourceName))));
2016-02-17 10:11:00 +03:00
}
}
void SQLEditDialog::setDataSources(LimeReport::DataSourceManager* dataSources,
QString datasourceName)
{
m_datasources = dataSources;
if (!datasourceName.isEmpty()) {
ui->cbSubdetail->setEnabled(true);
m_oldDatasourceName = datasourceName;
2016-02-17 10:11:00 +03:00
ui->leDatasourceName->setText(datasourceName);
ui->sqlText->setText(dataSources->queryText(datasourceName));
if (dataSources->isQuery(datasourceName)) {
initQueryMode();
}
if (dataSources->isSubQuery(datasourceName)) {
2016-02-17 10:11:00 +03:00
initSubQueryMode();
ui->leMaster->setText(dataSources->subQueryByName(datasourceName)->master());
}
if (dataSources->isProxy(datasourceName)) {
2016-02-17 10:11:00 +03:00
initProxyMode();
LimeReport::ProxyDesc* proxyDesc = dataSources->proxyByName(datasourceName);
2016-02-17 10:11:00 +03:00
ui->leChild->setText(proxyDesc->child());
ui->leMaster->setText(proxyDesc->master());
int curIndex = 0;
foreach (LimeReport::FieldMapDesc* fields, *proxyDesc->fieldsMap()) {
ui->fieldsMap->setRowCount(curIndex + 1);
ui->fieldsMap->setItem(curIndex, 0, new QTableWidgetItem(fields->master()));
ui->fieldsMap->setItem(curIndex, 1, new QTableWidgetItem(fields->detail()));
2016-02-17 10:11:00 +03:00
curIndex++;
}
}
if (dataSources->isCSV(datasourceName)) {
ui->csvText->setPlainText(dataSources->csvByName(datasourceName)->csvText());
ui->leSeparator->setText(dataSources->csvByName(datasourceName)->separator());
ui->cbUseFirstRowAsHeader->setChecked(
dataSources->csvByName(datasourceName)->firstRowIsHeader());
initCSVMode();
}
2016-02-17 10:11:00 +03:00
}
}
void SQLEditDialog::setDefaultConnection(QString defaultConnection)
{
m_defaultConnection = ConnectionDesc::connectionNameForUser(defaultConnection);
2016-02-17 10:11:00 +03:00
}
void SQLEditDialog::slotDataSourceNameEditing()
{
if (m_dialogMode == AddMode) {
QPalette palette = ui->leDatasourceName->palette();
if (m_datasources->containsDatasource(ui->leDatasourceName->text())) {
palette.setColor(QPalette::Text, Qt::red);
2016-02-17 10:11:00 +03:00
ui->leDatasourceName->setPalette(palette);
ui->lblInfo->setText(QString(tr("Datasource with name %1 already exist"))
.arg(ui->leDatasourceName->text()));
2016-02-17 10:11:00 +03:00
ui->lblInfo->setVisible(true);
} else {
palette.setColor(QPalette::Text, QApplication::palette().text().color());
2016-02-17 10:11:00 +03:00
ui->leDatasourceName->setPalette(palette);
ui->lblInfo->setVisible(false);
}
}
}
void SQLEditDialog::on_cbSubdetail_clicked(bool checked)
{
if (checked) {
m_masterDatasources->setModel(
new QStringListModel(m_datasources->dataSourceNames(), m_datasources));
2016-02-17 10:11:00 +03:00
}
ui->leMaster->setEnabled(checked);
ui->rbProxy->setEnabled(checked);
ui->rbSubQuery->setEnabled(checked);
if ((checked) && (ui->rbProxy->isChecked()))
initProxyMode();
if ((checked) && (ui->rbSubQuery->isChecked()))
initSubQueryMode();
if (!checked)
initQueryMode();
2016-02-17 10:11:00 +03:00
}
void SQLEditDialog::on_rbProxy_clicked(bool checked)
{
if (checked)
initProxyMode();
2016-02-17 10:11:00 +03:00
}
void SQLEditDialog::on_rbSubQuery_clicked(bool checked)
{
if (checked)
initSubQueryMode();
2016-02-17 10:11:00 +03:00
}
void SQLEditDialog::on_pbAddField_clicked()
{
ui->fieldsMap->setRowCount(ui->fieldsMap->rowCount() + 1);
}
2016-02-17 10:11:00 +03:00
void SQLEditDialog::initQueryMode()
{
2016-02-17 10:11:00 +03:00
ui->gbSQL->setVisible(true);
ui->gbFieldsMap->setVisible(false);
ui->pnlChildDatasource->setVisible(false);
ui->rbSubQuery->setVisible(false);
ui->rbProxy->setVisible(false);
ui->cbSubdetail->setChecked(false);
ui->leMaster->setVisible(false);
ui->lbMaster->setVisible(false);
// ui->tabWidget->removeTab(1);
2019-07-23 18:15:32 +03:00
ui->tabWidget->addTab(ui->csvTab, tr("CSV"));
2016-02-17 10:11:00 +03:00
}
void SQLEditDialog::initSubQueryMode()
{
2016-02-17 10:11:00 +03:00
ui->gbSQL->setVisible(true);
ui->gbFieldsMap->setVisible(false);
ui->pnlChildDatasource->setVisible(false);
ui->rbSubQuery->setChecked(true);
ui->cbSubdetail->setChecked(true);
ui->rbSubQuery->setVisible(true);
ui->rbProxy->setVisible(true);
ui->leMaster->setVisible(true);
ui->leMaster->setEnabled(true);
ui->lbMaster->setVisible(true);
ui->tabWidget->removeTab(1);
2016-02-17 10:11:00 +03:00
}
void SQLEditDialog::initProxyMode()
{
2016-02-17 10:11:00 +03:00
ui->gbSQL->setVisible(false);
ui->gbFieldsMap->setVisible(true);
ui->pnlChildDatasource->setVisible(true);
ui->rbProxy->setChecked(true);
ui->cbSubdetail->setChecked(true);
ui->rbSubQuery->setVisible(true);
ui->rbProxy->setVisible(true);
ui->leMaster->setVisible(true);
ui->leMaster->setEnabled(true);
ui->lbMaster->setVisible(true);
ui->cbSubdetail->setEnabled(false);
ui->tabWidget->removeTab(1);
}
void SQLEditDialog::initCSVMode() { ui->tabWidget->setCurrentWidget(ui->csvTab); }
2016-02-17 10:11:00 +03:00
void SQLEditDialog::slotPreviewData()
{
if (ui->cbbConnection->currentText().isEmpty()) {
QMessageBox::critical(this, tr("Attention"), tr("Connection is not specified"));
2016-02-17 10:11:00 +03:00
return;
}
m_previewModel = m_datasources->previewSQL(
ConnectionDesc::connectionNameForReport(ui->cbbConnection->currentText()),
ui->sqlText->toPlainText(), ui->leMaster->text());
if (m_previewModel) {
2016-02-17 10:11:00 +03:00
ui->tvPreview->setModel(m_previewModel.data());
ui->gbDataPreview->setVisible(true);
ui->pbPreview->setText(tr("Refresh"));
ui->pbHidePreview->setVisible(true);
} else {
if (ui->gbDataPreview->isVisible())
hidePreview();
QMessageBox::critical(this, tr("Attention"), m_datasources->lastError());
2016-02-17 10:11:00 +03:00
}
}
void SQLEditDialog::slotHidePreview() { hidePreview(); }
2016-02-17 10:11:00 +03:00
void SQLEditDialog::writeSetting()
{
if (settings() != 0) {
2016-02-17 10:11:00 +03:00
settings()->beginGroup("SQLEditor");
settings()->setValue("Geometry", saveGeometry());
2016-02-17 10:11:00 +03:00
settings()->endGroup();
}
}
void SQLEditDialog::readSettings()
{
if (settings() == 0)
return;
2016-02-17 10:11:00 +03:00
settings()->beginGroup("SQLEditor");
QVariant v = settings()->value("Geometry");
if (v.isValid()) {
2016-02-17 10:11:00 +03:00
restoreGeometry(v.toByteArray());
}
settings()->endGroup();
}
void SQLEditDialog::hidePreview()
{
2016-02-17 10:11:00 +03:00
ui->gbDataPreview->setVisible(false);
ui->pbPreview->setText(tr("Preview"));
ui->pbHidePreview->setVisible(false);
}
void SQLEditDialog::on_pbDelField_clicked()
{
ui->fieldsMap->removeRow(ui->fieldsMap->currentRow());
}
2018-07-12 21:09:18 +03:00
} // namespace LimeReport