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 "ui_lrdatabrowser.h"
|
|
|
|
|
|
|
|
#include "lrconnectiondialog.h"
|
2024-09-04 17:31:16 +03:00
|
|
|
#include "lrdatabrowser.h"
|
2016-02-17 10:11:00 +03:00
|
|
|
#include "lrreportengine_p.h"
|
2024-09-04 17:31:16 +03:00
|
|
|
#include "lrsqleditdialog.h"
|
2016-02-17 10:11:00 +03:00
|
|
|
#include "lrvariabledialog.h"
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QDockWidget>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QMainWindow>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QTableView>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include <QtSql/QSqlDatabase>
|
|
|
|
#include <QtSql/QSqlError>
|
|
|
|
#include <QtSql/QSqlQuery>
|
|
|
|
#include <QtSql/QSqlQueryModel>
|
|
|
|
|
|
|
|
namespace LimeReport {
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
DataBrowser::DataBrowser(QWidget* parent):
|
2016-02-17 10:11:00 +03:00
|
|
|
QWidget(parent),
|
2024-09-04 17:31:16 +03:00
|
|
|
ui(new Ui::DataBrowser),
|
|
|
|
m_report(0),
|
|
|
|
m_closingWindows(false),
|
|
|
|
m_settings(0),
|
|
|
|
m_ownedSettings(false)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2024-09-04 17:31:16 +03:00
|
|
|
connect(ui->addConnection, SIGNAL(clicked()), this, SLOT(slotAddConnection()));
|
|
|
|
connect(ui->deleteConection, SIGNAL(clicked()), this, SLOT(slotDeleteConnection()));
|
|
|
|
connect(ui->addDataSource, SIGNAL(clicked()), this, SLOT(slotAddDataSource()));
|
|
|
|
connect(ui->viewDataSource, SIGNAL(clicked()), this, SLOT(slotViewDatasource()));
|
|
|
|
connect(ui->editDataSource, SIGNAL(clicked()), this, SLOT(slotEditDatasource()));
|
|
|
|
connect(ui->deleteDataSource, SIGNAL(clicked()), this, SLOT(slotDeleteDatasource()));
|
|
|
|
connect(ui->changeConnection, SIGNAL(clicked()), this, SLOT(slotChangeConnection()));
|
|
|
|
connect(ui->pbConnect, SIGNAL(clicked()), this, SLOT(slotChangeConnectionState()));
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2021-08-23 08:07:08 +03:00
|
|
|
int margin = Const::DOCKWIDGET_MARGINS;
|
|
|
|
ui->verticalLayout_2->setContentsMargins(margin, margin, margin, margin);
|
2019-05-26 15:15:06 +03:00
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
ui->dataTree->setHeaderLabel(tr("Datasources"));
|
|
|
|
ui->pbConnect->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
DataBrowser::~DataBrowser()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
if (m_settings && m_ownedSettings)
|
|
|
|
delete m_settings;
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
QSize DataBrowser::sizeHint() const { return QSize(100, 200); }
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
void DataBrowser::slotAddConnection()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
ConnectionDialog* connectionEdit = new ConnectionDialog(this, 0, this);
|
|
|
|
connectionEdit->setAttribute(Qt::WA_DeleteOnClose, true);
|
2016-02-17 10:18:19 +03:00
|
|
|
#ifdef Q_OS_MAC
|
2016-02-17 10:11:00 +03:00
|
|
|
connectionEdit->setWindowModality(Qt::WindowModal);
|
2016-02-17 10:18:19 +03:00
|
|
|
#else
|
|
|
|
connectionEdit->setWindowModality(Qt::ApplicationModal);
|
2016-02-17 10:11:00 +03:00
|
|
|
#endif
|
2024-09-04 17:31:16 +03:00
|
|
|
// connect(connectionEdit,SIGNAL(finished(int)),this,SLOT(slotConnectionEditFinished(int)));
|
|
|
|
// connect(connectionEdit,SIGNAL(conectionRegistred(QString)),this,SLOT(slotConnectionRegistred(QString)));
|
2016-02-17 10:11:00 +03:00
|
|
|
connectionEdit->exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::slotSQLEditingFinished(SQLEditResult result)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (result.dialogMode == SQLEditDialog::AddMode) {
|
2018-05-21 21:10:16 +03:00
|
|
|
addDatasource(result);
|
2016-02-17 10:11:00 +03:00
|
|
|
} else {
|
2018-05-21 21:10:16 +03:00
|
|
|
applyChanges(result);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
updateDataTree();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::slotDeleteConnection()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!getConnectionName(NameForUser).isEmpty()) {
|
|
|
|
if (QMessageBox::critical(this, tr("Attention"),
|
|
|
|
tr("Do you really want to delete \"%1\" connection?")
|
|
|
|
.arg(getConnectionName(NameForUser)),
|
|
|
|
QMessageBox::Ok | QMessageBox::No, QMessageBox::No)
|
|
|
|
== QMessageBox::Ok) {
|
2017-02-15 02:52:11 +03:00
|
|
|
m_report->dataManager()->removeConnection(getConnectionName(NameForReport));
|
2016-02-17 10:11:00 +03:00
|
|
|
updateDataTree();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::slotAddDataSource()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
SQLEditDialog* sqlEdit
|
|
|
|
= new SQLEditDialog(this, m_report->dataManager(), SQLEditDialog::AddMode);
|
|
|
|
sqlEdit->setAttribute(Qt::WA_DeleteOnClose, true);
|
2016-02-17 10:18:19 +03:00
|
|
|
#ifdef Q_OS_MAC
|
2016-02-17 10:11:00 +03:00
|
|
|
sqlEdit->setWindowModality(Qt::WindowModal);
|
2016-02-17 10:18:19 +03:00
|
|
|
#else
|
|
|
|
sqlEdit->setWindowModality(Qt::ApplicationModal);
|
2016-02-17 10:11:00 +03:00
|
|
|
#endif
|
|
|
|
sqlEdit->setSettings(settings());
|
|
|
|
sqlEdit->setDataSources(m_report->dataManager());
|
2017-02-15 02:52:11 +03:00
|
|
|
sqlEdit->setDefaultConnection(getConnectionName(NameForReport));
|
2024-09-04 17:31:16 +03:00
|
|
|
connect(sqlEdit, SIGNAL(signalSqlEditingFinished(SQLEditResult)), this,
|
|
|
|
SLOT(slotSQLEditingFinished(SQLEditResult)));
|
2016-02-17 10:11:00 +03:00
|
|
|
sqlEdit->exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::updateDataTree()
|
|
|
|
{
|
|
|
|
QString selectedText = "";
|
|
|
|
int selectedType = 0;
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (ui->dataTree->selectedItems().count() == 1) {
|
2016-02-17 10:11:00 +03:00
|
|
|
selectedType = ui->dataTree->selectedItems().at(0)->type();
|
|
|
|
selectedText = ui->dataTree->selectedItems().at(0)->text(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
initConnections();
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString dataSourceName, m_report->datasourcesNames()) {
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QTreeWidgetItem* item
|
|
|
|
= new QTreeWidgetItem(QStringList(dataSourceName), DataBrowserTree::Table);
|
|
|
|
QTreeWidgetItem* parentItem
|
|
|
|
= findByNameAndType(ConnectionDesc::connectionNameForUser(
|
|
|
|
m_report->dataManager()->connectionName(dataSourceName)),
|
|
|
|
DataBrowserTree::Connection);
|
|
|
|
if (parentItem) {
|
2016-02-17 10:11:00 +03:00
|
|
|
parentItem->addChild(item);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!parentItem->isExpanded())
|
|
|
|
ui->dataTree->expandItem(parentItem);
|
2016-02-17 10:11:00 +03:00
|
|
|
} else {
|
|
|
|
ui->dataTree->addTopLevelItem(item);
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
try {
|
2016-02-17 10:11:00 +03:00
|
|
|
IDataSource* datasource = m_report->dataManager()->dataSource(dataSourceName);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (datasource) {
|
|
|
|
fillFields(item, datasource);
|
2016-02-17 10:11:00 +03:00
|
|
|
if (!datasource->isInvalid())
|
2024-09-04 17:31:16 +03:00
|
|
|
item->setIcon(0, QIcon(":/databrowser/images/table_good"));
|
2016-02-17 10:11:00 +03:00
|
|
|
else
|
2024-09-04 17:31:16 +03:00
|
|
|
item->setIcon(0, QIcon(":/databrowser/images/table_error"));
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
} else
|
|
|
|
item->setIcon(0, QIcon(":/databrowser/images/table_error"));
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
} catch (ReportError& /*exception*/) {
|
|
|
|
item->setIcon(0, QIcon(":/databrowser/images/table_error"));
|
|
|
|
// qDebug()<<exception.what();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QTreeWidgetItem* selectedItem = findByNameAndType(selectedText, selectedType);
|
|
|
|
if (selectedItem) {
|
2016-02-17 10:11:00 +03:00
|
|
|
selectedItem->setSelected(true);
|
|
|
|
ui->dataTree->setCurrentItem(selectedItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::updateVariablesTree()
|
|
|
|
{
|
|
|
|
ui->variablesTree->clear();
|
2024-09-04 17:31:16 +03:00
|
|
|
QTreeWidgetItem* reportVariables
|
|
|
|
= new QTreeWidgetItem(QStringList(tr("Report variables")), DataBrowserTree::Category);
|
|
|
|
reportVariables->setIcon(0, QIcon(":/report/images/folder"));
|
|
|
|
QTreeWidgetItem* systemVariables
|
|
|
|
= new QTreeWidgetItem(QStringList(tr("System variables")), DataBrowserTree::Category);
|
|
|
|
systemVariables->setIcon(0, QIcon(":/report/images/folder"));
|
|
|
|
QTreeWidgetItem* externalVariables
|
|
|
|
= new QTreeWidgetItem(QStringList(tr("External variables")), DataBrowserTree::Category);
|
|
|
|
externalVariables->setIcon(0, QIcon(":/report/images/folder"));
|
2016-06-07 00:44:21 +03:00
|
|
|
ui->variablesTree->addTopLevelItem(reportVariables);
|
2016-02-17 10:11:00 +03:00
|
|
|
ui->variablesTree->addTopLevelItem(systemVariables);
|
2016-06-07 00:44:21 +03:00
|
|
|
ui->variablesTree->addTopLevelItem(externalVariables);
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString variableName, m_report->dataManager()->variableNames()) {
|
2016-02-17 10:11:00 +03:00
|
|
|
QStringList values;
|
2024-09-04 17:31:16 +03:00
|
|
|
values << variableName
|
|
|
|
+ ((m_report->dataManager()->variableIsSystem(variableName))
|
|
|
|
? ""
|
|
|
|
: " [" + m_report->dataManager()->variable(variableName).toString() + "]")
|
|
|
|
<< variableName;
|
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem(values, DataBrowserTree::Variable);
|
|
|
|
item->setIcon(0, QIcon(":/databrowser/images/value"));
|
|
|
|
if (m_report->dataManager()->variableIsSystem(variableName)) {
|
|
|
|
systemVariables->addChild(item);
|
2016-02-17 10:11:00 +03:00
|
|
|
} else {
|
2016-06-07 00:44:21 +03:00
|
|
|
reportVariables->addChild(item);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString variableName, m_report->dataManager()->userVariableNames()) {
|
|
|
|
if (!m_report->dataManager()->variableNames().contains(variableName)) {
|
2016-06-07 00:44:21 +03:00
|
|
|
QStringList values;
|
2024-09-04 17:31:16 +03:00
|
|
|
values << variableName + " ["
|
|
|
|
+ m_report->dataManager()->variable(variableName).toString() + "]"
|
|
|
|
<< variableName;
|
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem(values, DataBrowserTree::ExternalVariable);
|
|
|
|
item->setIcon(0, QIcon(":/databrowser/images/value"));
|
2016-06-07 00:44:21 +03:00
|
|
|
externalVariables->addChild(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->variablesTree->expandItem(reportVariables);
|
2016-02-17 10:11:00 +03:00
|
|
|
ui->variablesTree->expandItem(systemVariables);
|
2016-06-07 00:44:21 +03:00
|
|
|
ui->variablesTree->expandItem(externalVariables);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::closeAllDataWindows()
|
|
|
|
{
|
|
|
|
m_closingWindows = true;
|
2024-09-04 17:31:16 +03:00
|
|
|
QMap<QString, QDockWidget*>::iterator it = m_dataWindows.begin();
|
|
|
|
for (; it != m_dataWindows.end();) {
|
2016-02-17 10:11:00 +03:00
|
|
|
(*it)->close();
|
|
|
|
delete (*it);
|
|
|
|
it = m_dataWindows.erase(it);
|
|
|
|
}
|
|
|
|
m_closingWindows = false;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataBrowser::setSettings(QSettings* value, bool owned)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_settings && m_ownedSettings)
|
|
|
|
delete m_settings;
|
|
|
|
m_settings = value;
|
2016-02-17 10:11:00 +03:00
|
|
|
m_ownedSettings = owned;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QSettings* DataBrowser::settings()
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_settings) {
|
2016-02-17 10:11:00 +03:00
|
|
|
return m_settings;
|
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
m_settings = new QSettings("LimeReport", QCoreApplication::applicationName());
|
2016-02-17 10:11:00 +03:00
|
|
|
m_ownedSettings = true;
|
|
|
|
return m_settings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataBrowser::slotDatasourcesChanged() { updateDataTree(); }
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataBrowser::fillFields(QTreeWidgetItem* parentItem, LimeReport::IDataSource* dataSource)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!dataSource)
|
|
|
|
return;
|
|
|
|
for (int i = 0; i < dataSource->columnCount(); i++) {
|
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem(QStringList(dataSource->columnNameByIndex(i)),
|
|
|
|
DataBrowserTree::Row);
|
|
|
|
item->setIcon(0, QIcon(":/databrowser/images/field"));
|
2016-02-17 10:11:00 +03:00
|
|
|
parentItem->addChild(item);
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
parentItem->sortChildren(0, Qt::AscendingOrder);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QTreeWidgetItem* DataBrowser::findByNameAndType(QString name, int itemType)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (name.isEmpty())
|
|
|
|
return 0;
|
|
|
|
QList<QTreeWidgetItem*> items
|
|
|
|
= ui->dataTree->findItems(name, Qt::MatchContains | Qt::MatchRecursive);
|
|
|
|
if (!items.isEmpty()) {
|
|
|
|
for (int i = 0; i < items.count(); i++) {
|
|
|
|
if ((items.at(i)->type() == itemType) /* && (items.at(0)->text(0)==name)*/) {
|
|
|
|
return items.at(i);
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::slotViewDatasource()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
QString datasourceName = getDatasourceName();
|
|
|
|
if (!datasourceName.isEmpty()) {
|
2016-02-17 10:11:00 +03:00
|
|
|
showDataWindow(datasourceName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DataBrowser::getDatasourceName()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (ui->dataTree->currentItem()) {
|
|
|
|
if (ui->dataTree->currentItem()->type() == DataBrowserTree::Table)
|
|
|
|
return ui->dataTree->currentItem()->text(0);
|
|
|
|
if (ui->dataTree->currentItem()->type() == DataBrowserTree::Row)
|
|
|
|
return ui->dataTree->currentItem()->parent()->text(0);
|
2016-02-17 10:11:00 +03:00
|
|
|
};
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QTreeWidgetItem* findConnectionItem(QTreeWidgetItem* item)
|
|
|
|
{
|
|
|
|
if (item->type() == DataBrowserTree::Connection) {
|
2016-02-17 10:11:00 +03:00
|
|
|
return item;
|
|
|
|
} else {
|
|
|
|
if (item->parent())
|
|
|
|
return findConnectionItem(item->parent());
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 02:52:11 +03:00
|
|
|
QString DataBrowser::getConnectionName(NameType nameType)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (ui->dataTree->currentItem()) {
|
|
|
|
QTreeWidgetItem* ci = findConnectionItem(ui->dataTree->currentItem());
|
2017-02-15 02:52:11 +03:00
|
|
|
if (ci) {
|
|
|
|
switch (nameType) {
|
|
|
|
case NameForUser:
|
|
|
|
return ConnectionDesc::connectionNameForUser(ci->text(0));
|
|
|
|
case NameForReport:
|
|
|
|
return ConnectionDesc::connectionNameForReport(ci->text(0));
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
} else
|
|
|
|
return QString();
|
2016-02-17 10:11:00 +03:00
|
|
|
};
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DataBrowser::getVariable()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (ui->variablesTree->currentItem()
|
|
|
|
&& (ui->variablesTree->currentItem()->type() == DataBrowserTree::Variable
|
|
|
|
|| ui->variablesTree->currentItem()->type() == DataBrowserTree::ExternalVariable)) {
|
2016-02-17 10:11:00 +03:00
|
|
|
return ui->variablesTree->currentItem()->text(1);
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::slotEditDatasource()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!getDatasourceName().isEmpty()) {
|
|
|
|
closeDataWindow(getDatasourceName());
|
|
|
|
SQLEditDialog* sqlEdit
|
|
|
|
= new SQLEditDialog(this, m_report->dataManager(), SQLEditDialog::EditMode);
|
|
|
|
sqlEdit->setAttribute(Qt::WA_DeleteOnClose);
|
2016-02-17 10:18:19 +03:00
|
|
|
#ifdef Q_OS_MAC
|
2024-09-04 17:31:16 +03:00
|
|
|
sqlEdit->setWindowModality(Qt::WindowModal);
|
2016-02-17 10:18:19 +03:00
|
|
|
#else
|
2024-09-04 17:31:16 +03:00
|
|
|
sqlEdit->setWindowModality(Qt::ApplicationModal);
|
2016-02-17 10:11:00 +03:00
|
|
|
#endif
|
2024-09-04 17:31:16 +03:00
|
|
|
sqlEdit->setSettings(settings());
|
|
|
|
sqlEdit->setDataSources(m_report->dataManager(), getDatasourceName());
|
|
|
|
connect(sqlEdit, SIGNAL(signalSqlEditingFinished(SQLEditResult)), this,
|
|
|
|
SLOT(slotSQLEditingFinished(SQLEditResult)));
|
|
|
|
sqlEdit->exec();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::slotDeleteDatasource()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
QString datasourceName = getDatasourceName();
|
|
|
|
QTreeWidgetItem* item = findByNameAndType(datasourceName, DataBrowserTree::Table);
|
|
|
|
if (item) {
|
|
|
|
if (QMessageBox::critical(
|
|
|
|
this, tr("Attention"),
|
|
|
|
tr("Do you really want to delete \"%1\" datasource?").arg(datasourceName),
|
|
|
|
QMessageBox::Ok | QMessageBox::No, QMessageBox::No)
|
|
|
|
== QMessageBox::Ok) {
|
2016-02-17 10:11:00 +03:00
|
|
|
removeDatasource(datasourceName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataBrowser::setReportEditor(LimeReport::ReportDesignWidget* report)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
m_report = report;
|
|
|
|
connect(m_report, SIGNAL(cleared()), this, SLOT(slotClear()));
|
|
|
|
connect(m_report->dataManager(), SIGNAL(datasourcesChanged()), this,
|
|
|
|
SLOT(slotDatasourcesChanged()));
|
2016-02-17 10:11:00 +03:00
|
|
|
updateDataTree();
|
|
|
|
updateVariablesTree();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::slotClear()
|
|
|
|
{
|
|
|
|
ui->dataTree->clear();
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QDockWidget* window, m_dataWindows.values())
|
|
|
|
window->close();
|
2016-02-17 10:11:00 +03:00
|
|
|
updateDataTree();
|
2016-06-07 00:44:21 +03:00
|
|
|
updateVariablesTree();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::initConnections()
|
|
|
|
{
|
|
|
|
ui->dataTree->clear();
|
2024-09-04 17:31:16 +03:00
|
|
|
QList<QTreeWidgetItem*> items;
|
2017-02-18 17:22:04 +03:00
|
|
|
|
|
|
|
QStringList connections = QSqlDatabase::connectionNames();
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString connectionName, m_report->dataManager()->connectionNames()) {
|
|
|
|
if (!connections.contains(connectionName, Qt::CaseInsensitive)) {
|
2017-02-18 17:22:04 +03:00
|
|
|
connections.append(connectionName);
|
|
|
|
}
|
|
|
|
}
|
2021-08-23 08:07:08 +03:00
|
|
|
std::sort(connections.begin(), connections.end());
|
2017-02-18 17:22:04 +03:00
|
|
|
foreach (QString connectionName, connections) {
|
2024-09-04 17:31:16 +03:00
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem(
|
|
|
|
ui->dataTree, QStringList(ConnectionDesc::connectionNameForUser(connectionName)),
|
|
|
|
DataBrowserTree::Connection);
|
|
|
|
if (!m_report->dataManager()->connectionNames().contains(
|
|
|
|
ConnectionDesc::connectionNameForReport(connectionName), Qt::CaseInsensitive)) {
|
|
|
|
item->setIcon(0, QIcon(":/databrowser/images/database_connected"));
|
2017-02-18 17:22:04 +03:00
|
|
|
} else {
|
|
|
|
if (m_report->dataManager()->isConnectionConnected(connectionName))
|
2024-09-04 17:31:16 +03:00
|
|
|
item->setIcon(0, QIcon(":/databrowser/images/database_connected"));
|
2017-02-18 17:22:04 +03:00
|
|
|
else
|
2024-09-04 17:31:16 +03:00
|
|
|
item->setIcon(0, QIcon(":/databrowser/images/database_disconnected"));
|
2017-02-18 17:22:04 +03:00
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
items.append(item);
|
|
|
|
}
|
2017-02-18 17:22:04 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
// foreach (QString connectionName, connections) {
|
|
|
|
// QTreeWidgetItem *item=new QTreeWidgetItem(
|
|
|
|
// ui->dataTree,
|
|
|
|
// QStringList(ConnectionDesc::connectionNameForUser(connectionName)),
|
|
|
|
// DataBrowserTree::Connection
|
|
|
|
// );
|
|
|
|
// item->setIcon(0,QIcon(":/databrowser/images/database_connected"));
|
|
|
|
// }
|
|
|
|
|
|
|
|
// connections = m_report->dataManager()->connectionNames();
|
|
|
|
// std::sort(connections);
|
|
|
|
// foreach(QString connectionName,connectionName){
|
|
|
|
// if (!QSqlDatabase::contains(connectionName)){
|
|
|
|
// QTreeWidgetItem *item=new QTreeWidgetItem(
|
|
|
|
// ui->dataTree,
|
|
|
|
// QStringList(ConnectionDesc::connectionNameForUser(connectionName)),
|
|
|
|
// DataBrowserTree::Connection
|
|
|
|
// );
|
|
|
|
// if (m_report->dataManager()->isConnectionConnected(connectionName))
|
|
|
|
// item->setIcon(0,QIcon(":/databrowser/images/database_connected"));
|
|
|
|
// else
|
|
|
|
// item->setIcon(0,QIcon(":/databrowser/images/database_disconnected"));
|
|
|
|
// items.append(item);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
ui->dataTree->insertTopLevelItems(0, items);
|
|
|
|
}
|
|
|
|
|
|
|
|
QDockWidget* DataBrowser::createDataWindow(QString datasourceName)
|
|
|
|
{
|
|
|
|
QDockWidget* window = new QDockWidget("Table: " + datasourceName);
|
|
|
|
QTableView* tableView = new QTableView(window);
|
|
|
|
// TODO: exception or message ?
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
try {
|
2016-02-17 10:19:50 +03:00
|
|
|
IDataSourceHolder* holder = m_report->dataManager()->dataSourceHolder(datasourceName);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (holder)
|
|
|
|
holder->update();
|
2016-02-17 10:11:00 +03:00
|
|
|
IDataSource* datasource = m_report->dataManager()->dataSource(datasourceName);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (datasource) {
|
2016-02-17 10:11:00 +03:00
|
|
|
tableView->setModel(datasource->model());
|
|
|
|
}
|
|
|
|
} catch (ReportError& exception) {
|
2024-09-04 17:31:16 +03:00
|
|
|
qDebug() << exception.what();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
window->setWidget(tableView);
|
|
|
|
window->setAttribute(Qt::WA_DeleteOnClose);
|
2024-09-04 17:31:16 +03:00
|
|
|
connect(window, SIGNAL(destroyed()), this, SLOT(slotDataWindowClosed()));
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
if (!m_dataWindows.isEmpty())
|
2024-09-04 17:31:16 +03:00
|
|
|
m_mainWindow->tabifyDockWidget(m_dataWindows.values().at(0), window);
|
2016-02-17 10:11:00 +03:00
|
|
|
else
|
2024-09-04 17:31:16 +03:00
|
|
|
m_mainWindow->addDockWidget(Qt::BottomDockWidgetArea, window);
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
m_dataWindows.insert(datasourceName, window);
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QDockWidget* DataBrowser::dataWindow(QString datasourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_dataWindows.contains(datasourceName)) {
|
2016-02-17 10:11:00 +03:00
|
|
|
return m_dataWindows.value(datasourceName);
|
2024-09-04 17:31:16 +03:00
|
|
|
} else
|
|
|
|
return createDataWindow(datasourceName);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataBrowser::setMainWindow(QMainWindow* mainWindow) { m_mainWindow = mainWindow; }
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
void DataBrowser::slotDataWindowClosed()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (isClosingWindows())
|
|
|
|
return;
|
|
|
|
for (int i = 0; i < m_dataWindows.count(); i++) {
|
|
|
|
if (m_dataWindows.values().at(i) == sender()) {
|
2016-02-17 10:11:00 +03:00
|
|
|
m_dataWindows.remove(m_dataWindows.keys().at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::slotChangeConnection()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!getConnectionName(NameForUser).isEmpty()) {
|
|
|
|
ConnectionDialog* connectionEdit = new ConnectionDialog(
|
|
|
|
this, m_report->dataManager()->connectionByName(getConnectionName(NameForReport)),
|
|
|
|
this);
|
|
|
|
connectionEdit->setAttribute(Qt::WA_DeleteOnClose, true);
|
2016-02-17 10:18:19 +03:00
|
|
|
#ifdef Q_OS_MAC
|
2016-02-17 10:11:00 +03:00
|
|
|
connectionEdit->setWindowModality(Qt::WindowModal);
|
2016-02-17 10:18:19 +03:00
|
|
|
#else
|
|
|
|
connectionEdit->setWindowModality(Qt::ApplicationModal);
|
2016-02-17 10:11:00 +03:00
|
|
|
#endif
|
2024-09-04 17:31:16 +03:00
|
|
|
// connect(connectionEdit,SIGNAL(finished(int)),this,SLOT(slotConnectionEditFinished(int)));
|
2016-02-17 10:11:00 +03:00
|
|
|
connectionEdit->exec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::slotChangeConnectionState()
|
|
|
|
{
|
2017-02-15 02:52:11 +03:00
|
|
|
QString connectionName = getConnectionName(NameForReport);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!connectionName.isEmpty()) {
|
|
|
|
if (!m_report->dataManager()->isConnectionConnected(connectionName)) {
|
2016-02-17 10:11:00 +03:00
|
|
|
setCursor(Qt::WaitCursor);
|
2024-09-04 17:31:16 +03:00
|
|
|
// try {
|
|
|
|
if (!m_report->dataManager()->connectConnection(connectionName)) {
|
|
|
|
//} catch(std::runtime_error &exception) {
|
|
|
|
QMessageBox::critical(this, tr("Attention"), m_report->dataManager()->lastError());
|
|
|
|
//}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
updateDataTree();
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
} else {
|
|
|
|
m_report->dataManager()->disconnectConnection(connectionName);
|
|
|
|
updateDataTree();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataBrowser::slotVariableEditorAccept(const QString& variable)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
updateVariablesTree();
|
2024-09-04 17:31:16 +03:00
|
|
|
QList<QTreeWidgetItem*> items
|
|
|
|
= ui->variablesTree->findItems(variable, Qt::MatchContains | Qt::MatchRecursive);
|
|
|
|
if (!items.isEmpty()) {
|
2016-02-17 10:11:00 +03:00
|
|
|
ui->variablesTree->setCurrentItem(items.at(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::showDataWindow(QString datasourceName)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
QDockWidget* window = dataWindow(datasourceName);
|
|
|
|
if (window) {
|
2016-02-17 10:11:00 +03:00
|
|
|
window->show();
|
|
|
|
window->raise();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::closeDataWindow(QString datasourceName)
|
|
|
|
{
|
|
|
|
if (m_dataWindows.contains(datasourceName))
|
|
|
|
m_dataWindows.value(datasourceName)->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::removeDatasource(QString datasourceName)
|
|
|
|
{
|
|
|
|
closeDataWindow(datasourceName);
|
|
|
|
m_report->dataManager()->removeDatasource(datasourceName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::addQuery(SQLEditResult result)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
m_report->dataManager()->addQuery(result.datasourceName, result.sql, result.connectionName);
|
2024-09-04 17:31:16 +03:00
|
|
|
} catch (ReportError& exception) {
|
|
|
|
qDebug() << exception.what();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::changeQuery(SQLEditResult result)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
m_report->dataManager()->removeDatasource(result.oldDatasourceName);
|
2019-04-04 04:37:18 +03:00
|
|
|
addQuery(result);
|
2024-09-04 17:31:16 +03:00
|
|
|
} catch (ReportError& exception) {
|
|
|
|
qDebug() << exception.what();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::addSubQuery(SQLEditResult result)
|
|
|
|
{
|
|
|
|
try {
|
2024-09-04 17:31:16 +03:00
|
|
|
m_report->dataManager()->addSubQuery(result.datasourceName, result.sql,
|
|
|
|
result.connectionName, result.masterDatasource);
|
|
|
|
} catch (ReportError& exception) {
|
|
|
|
qDebug() << exception.what();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::changeSubQuery(SQLEditResult result)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
m_report->dataManager()->removeDatasource(result.oldDatasourceName);
|
2019-04-04 04:37:18 +03:00
|
|
|
addSubQuery(result);
|
2024-09-04 17:31:16 +03:00
|
|
|
} catch (ReportError& exception) {
|
|
|
|
qDebug() << exception.what();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::addProxy(SQLEditResult result)
|
|
|
|
{
|
|
|
|
try {
|
2024-09-04 17:31:16 +03:00
|
|
|
m_report->dataManager()->addProxy(result.datasourceName, result.masterDatasource,
|
|
|
|
result.childDataSource, result.fieldMap);
|
|
|
|
} catch (ReportError& exception) {
|
|
|
|
qDebug() << exception.what();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::changeProxy(SQLEditResult result)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
m_report->dataManager()->removeDatasource(result.oldDatasourceName);
|
2019-04-04 04:37:18 +03:00
|
|
|
addProxy(result);
|
2024-09-04 17:31:16 +03:00
|
|
|
} catch (ReportError& exception) {
|
|
|
|
qDebug() << exception.what();
|
2019-04-04 04:37:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::addCSV(SQLEditResult result)
|
|
|
|
{
|
|
|
|
try {
|
2024-09-04 17:31:16 +03:00
|
|
|
m_report->dataManager()->addCSV(result.datasourceName, result.csv, result.separator,
|
|
|
|
result.firstRowIsHeader);
|
|
|
|
} catch (ReportError& exception) {
|
|
|
|
qDebug() << exception.what();
|
2019-04-04 04:37:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::changeCSV(SQLEditResult result)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
m_report->dataManager()->removeDatasource(result.oldDatasourceName);
|
|
|
|
addCSV(result);
|
2024-09-04 17:31:16 +03:00
|
|
|
} catch (ReportError& exception) {
|
|
|
|
qDebug() << exception.what();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 21:10:16 +03:00
|
|
|
SQLEditResult::ResultMode DataBrowser::currentDatasourceType(const QString& datasourceName)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_report->dataManager()->isQuery(datasourceName))
|
|
|
|
return SQLEditResult::Query;
|
|
|
|
if (m_report->dataManager()->isSubQuery(datasourceName))
|
|
|
|
return SQLEditResult::SubQuery;
|
|
|
|
if (m_report->dataManager()->isProxy(datasourceName))
|
|
|
|
return SQLEditResult::SubProxy;
|
|
|
|
if (m_report->dataManager()->isCSV(datasourceName))
|
|
|
|
return SQLEditResult::CSVText;
|
2018-05-21 21:10:16 +03:00
|
|
|
return SQLEditResult::Undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::applyChanges(SQLEditResult result)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (result.resultMode == currentDatasourceType(result.oldDatasourceName)) {
|
|
|
|
switch (result.resultMode) {
|
|
|
|
case SQLEditResult::Query:
|
|
|
|
changeQuery(result);
|
|
|
|
break;
|
|
|
|
case SQLEditResult::SubQuery:
|
|
|
|
changeSubQuery(result);
|
|
|
|
break;
|
|
|
|
case SQLEditResult::SubProxy:
|
|
|
|
changeProxy(result);
|
|
|
|
break;
|
|
|
|
case SQLEditResult::CSVText:
|
|
|
|
changeCSV(result);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-05-21 21:10:16 +03:00
|
|
|
}
|
|
|
|
} else {
|
2020-08-16 17:54:44 +03:00
|
|
|
removeDatasource(result.oldDatasourceName);
|
2018-05-21 21:10:16 +03:00
|
|
|
addDatasource(result);
|
|
|
|
}
|
2019-04-04 04:37:18 +03:00
|
|
|
activateItem(result.datasourceName, DataBrowserTree::Table);
|
2018-05-21 21:10:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::addDatasource(SQLEditResult result)
|
|
|
|
{
|
|
|
|
switch (result.resultMode) {
|
2024-09-04 17:31:16 +03:00
|
|
|
case SQLEditResult::Query:
|
|
|
|
addQuery(result);
|
|
|
|
break;
|
|
|
|
case SQLEditResult::SubQuery:
|
|
|
|
addSubQuery(result);
|
|
|
|
break;
|
|
|
|
case SQLEditResult::SubProxy:
|
|
|
|
addProxy(result);
|
|
|
|
break;
|
|
|
|
case SQLEditResult::CSVText:
|
|
|
|
addCSV(result);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-05-21 21:10:16 +03:00
|
|
|
}
|
2019-04-04 04:37:18 +03:00
|
|
|
activateItem(result.datasourceName, DataBrowserTree::Table);
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataBrowser::activateItem(const QString& name, DataBrowserTree::NodeType type)
|
|
|
|
{
|
2019-04-04 04:37:18 +03:00
|
|
|
QTreeWidgetItem* item = findByNameAndType(name, type);
|
2019-12-19 23:40:32 +03:00
|
|
|
if (item)
|
|
|
|
item->treeWidget()->setCurrentItem(item);
|
2018-05-21 21:10:16 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataBrowser::addConnectionDesc(ConnectionDesc* connection)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
m_report->dataManager()->addConnectionDesc(connection);
|
|
|
|
updateDataTree();
|
2019-04-04 04:37:18 +03:00
|
|
|
activateItem(connection->name(), DataBrowserTree::Connection);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataBrowser::changeConnectionDesc(ConnectionDesc* connection)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (connection->autoconnect())
|
|
|
|
m_report->dataManager()->connectConnection(connection->name());
|
2016-02-17 10:11:00 +03:00
|
|
|
updateDataTree();
|
2019-04-04 04:37:18 +03:00
|
|
|
activateItem(connection->name(), DataBrowserTree::Connection);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataBrowser::checkConnectionDesc(ConnectionDesc* connection)
|
2016-02-25 00:27:11 +03:00
|
|
|
{
|
|
|
|
bool result = m_report->dataManager()->checkConnectionDesc(connection);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!result)
|
|
|
|
setLastError(m_report->dataManager()->lastError());
|
2016-02-25 00:27:11 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-02-15 02:52:11 +03:00
|
|
|
bool DataBrowser::containsDefaultConnection()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
bool result = m_report->dataManager()->connectionByName(QSqlDatabase::defaultConnection)
|
|
|
|
|| QSqlDatabase::contains(QSqlDatabase::defaultConnection);
|
2017-02-15 02:52:11 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QString DataBrowser::lastError() const { return m_lastError; }
|
2016-02-25 00:27:11 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataBrowser::setLastError(const QString& lastError) { m_lastError = lastError; }
|
2016-02-25 00:27:11 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataBrowser::on_dataTree_currentItemChanged(QTreeWidgetItem* current,
|
|
|
|
QTreeWidgetItem* previous)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
Q_UNUSED(previous)
|
2024-09-04 17:31:16 +03:00
|
|
|
if (current && (current->type() == DataBrowserTree::Connection)) {
|
|
|
|
bool internalConnection = m_report->dataManager()->connectionByName(
|
|
|
|
ConnectionDesc::connectionNameForReport(current->text(0)));
|
|
|
|
if (m_report->dataManager()->isConnectionConnected(
|
|
|
|
ConnectionDesc::connectionNameForReport(current->text(0)))) {
|
2016-02-17 10:11:00 +03:00
|
|
|
ui->pbConnect->setIcon(QIcon(":/databrowser/images/plug-connect.png"));
|
|
|
|
} else {
|
|
|
|
ui->pbConnect->setIcon(QIcon(":/databrowser/images/plug-disconnect.png"));
|
|
|
|
}
|
|
|
|
ui->editDataSource->setEnabled(false);
|
|
|
|
ui->deleteDataSource->setEnabled(false);
|
2024-09-04 17:31:16 +03:00
|
|
|
ui->viewDataSource->setEnabled(false);
|
2017-02-18 17:22:04 +03:00
|
|
|
ui->pbConnect->setEnabled(internalConnection);
|
|
|
|
ui->changeConnection->setEnabled(internalConnection);
|
|
|
|
ui->deleteConection->setEnabled(internalConnection);
|
2016-02-17 10:11:00 +03:00
|
|
|
ui->errorMessage->setDisabled(true);
|
|
|
|
} else {
|
|
|
|
ui->changeConnection->setEnabled(false);
|
|
|
|
ui->deleteConection->setEnabled(false);
|
|
|
|
ui->pbConnect->setEnabled(false);
|
|
|
|
IDataSourceHolder* holder = m_report->dataManager()->dataSourceHolder(getDatasourceName());
|
2024-09-04 17:31:16 +03:00
|
|
|
if (holder) {
|
2016-02-17 10:11:00 +03:00
|
|
|
ui->viewDataSource->setEnabled(!holder->isInvalid());
|
|
|
|
ui->editDataSource->setEnabled(holder->isEditable());
|
|
|
|
ui->deleteDataSource->setEnabled(holder->isRemovable());
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!holder->lastError().isEmpty()) {
|
2016-02-17 10:11:00 +03:00
|
|
|
ui->errorMessage->setEnabled(true);
|
2024-09-04 17:31:16 +03:00
|
|
|
} else {
|
|
|
|
ui->errorMessage->setEnabled(false);
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LimeReport::DataBrowser::on_addVariable_clicked()
|
|
|
|
{
|
|
|
|
LRVariableDialog dialog(this);
|
2016-02-17 10:18:19 +03:00
|
|
|
#ifdef Q_OS_MAC
|
2016-02-17 10:11:00 +03:00
|
|
|
dialog.setWindowModality(Qt::WindowModal);
|
2016-02-17 10:18:19 +03:00
|
|
|
#else
|
|
|
|
dialog.setWindowModality(Qt::ApplicationModal);
|
2016-02-17 10:11:00 +03:00
|
|
|
#endif
|
|
|
|
dialog.setVariableContainer(m_report->dataManager());
|
2024-09-04 17:31:16 +03:00
|
|
|
connect(&dialog, SIGNAL(signalVariableAccepted(QString)), this,
|
|
|
|
SLOT(slotVariableEditorAccept(QString)));
|
2016-02-17 10:11:00 +03:00
|
|
|
dialog.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::on_editVariable_clicked()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!getVariable().isEmpty()) {
|
2016-02-17 10:11:00 +03:00
|
|
|
LRVariableDialog dialog(this);
|
2016-02-17 10:18:19 +03:00
|
|
|
#ifdef Q_OS_MAC
|
2016-02-17 10:11:00 +03:00
|
|
|
dialog.setWindowModality(Qt::WindowModal);
|
2016-02-17 10:18:19 +03:00
|
|
|
#else
|
|
|
|
dialog.setWindowModality(Qt::ApplicationModal);
|
2016-02-17 10:11:00 +03:00
|
|
|
#endif
|
|
|
|
dialog.setVariableContainer(m_report->dataManager());
|
|
|
|
QString varName = getVariable();
|
|
|
|
dialog.setVariableName(varName);
|
2024-09-04 17:31:16 +03:00
|
|
|
connect(&dialog, SIGNAL(signalVariableAccepted(QString)), this,
|
|
|
|
SLOT(slotVariableEditorAccept(QString)));
|
2016-02-17 10:11:00 +03:00
|
|
|
dialog.exec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::on_deleteVariable_clicked()
|
|
|
|
{
|
|
|
|
QString varName = getVariable();
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!varName.isEmpty()) {
|
|
|
|
if (QMessageBox::critical(
|
|
|
|
this, tr("Attention"),
|
|
|
|
QString(tr("Do you really want to delete variable \"%1\"?")).arg(varName),
|
|
|
|
QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel)
|
|
|
|
== QMessageBox::Ok) {
|
2016-02-17 10:11:00 +03:00
|
|
|
m_report->dataManager()->deleteVariable(varName);
|
|
|
|
updateVariablesTree();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataBrowser::on_variablesTree_currentItemChanged(QTreeWidgetItem* current,
|
|
|
|
QTreeWidgetItem* previous)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
Q_UNUSED(previous)
|
2024-09-04 17:31:16 +03:00
|
|
|
if (current) {
|
|
|
|
if (m_report->dataManager()->containsVariable(current->text(1))) {
|
|
|
|
if (m_report->dataManager()->variableType(current->text(1)) == VarDesc::Report) {
|
2016-06-07 00:44:21 +03:00
|
|
|
ui->editVariable->setEnabled(true);
|
|
|
|
ui->deleteVariable->setEnabled(true);
|
|
|
|
} else {
|
|
|
|
ui->editVariable->setEnabled(false);
|
|
|
|
ui->deleteVariable->setEnabled(false);
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_report->dataManager()->variableType(current->text(1)) == VarDesc::User) {
|
|
|
|
ui->varToReport->setEnabled(true);
|
|
|
|
} else {
|
|
|
|
ui->varToReport->setEnabled(false);
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
} else {
|
|
|
|
ui->editVariable->setEnabled(false);
|
|
|
|
ui->deleteVariable->setEnabled(false);
|
2016-06-07 00:44:21 +03:00
|
|
|
ui->varToReport->setEnabled(false);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataBrowser::on_errorMessage_clicked()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!getDatasourceName().isEmpty()
|
|
|
|
&& m_report->dataManager()->dataSourceHolder(getDatasourceName())) {
|
|
|
|
QMessageBox::critical(
|
|
|
|
this, tr("Error"),
|
|
|
|
m_report->dataManager()->dataSourceHolder(getDatasourceName())->lastError());
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 00:44:21 +03:00
|
|
|
void DataBrowser::on_varToReport_clicked()
|
|
|
|
{
|
|
|
|
QString varName = getVariable();
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!varName.isEmpty()) {
|
|
|
|
m_report->dataManager()->addVariable(varName, m_report->dataManager()->variable(varName),
|
|
|
|
VarDesc::Report);
|
2016-06-07 00:44:21 +03:00
|
|
|
ui->varToReport->setEnabled(false);
|
|
|
|
updateVariablesTree();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataBrowser::on_variablesTree_itemDoubleClicked(QTreeWidgetItem* item, int)
|
2016-06-07 00:44:21 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (item) {
|
2016-06-07 00:44:21 +03:00
|
|
|
QString varName = item->text(1);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!varName.isEmpty()
|
|
|
|
&& (m_report->dataManager()->variableType(varName) == VarDesc::Report)) {
|
2016-06-07 00:44:21 +03:00
|
|
|
LRVariableDialog dialog(this);
|
2024-09-04 17:31:16 +03:00
|
|
|
#ifdef Q_OS_MAC
|
2016-06-07 00:44:21 +03:00
|
|
|
dialog.setWindowModality(Qt::WindowModal);
|
2024-09-04 17:31:16 +03:00
|
|
|
#else
|
2016-06-07 00:44:21 +03:00
|
|
|
dialog.setWindowModality(Qt::ApplicationModal);
|
2024-09-04 17:31:16 +03:00
|
|
|
#endif
|
2016-06-07 00:44:21 +03:00
|
|
|
dialog.setVariableContainer(m_report->dataManager());
|
|
|
|
dialog.setVariableName(varName);
|
2024-09-04 17:31:16 +03:00
|
|
|
connect(&dialog, SIGNAL(signalVariableAccepted(QString)), this,
|
|
|
|
SLOT(slotVariableEditorAccept(QString)));
|
2016-06-07 00:44:21 +03:00
|
|
|
dialog.exec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 02:52:11 +03:00
|
|
|
} // namespace LimeReport
|