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

defaultConnection support has been added to LimeReport designer

This commit is contained in:
Arin Alexander 2017-02-15 02:52:11 +03:00
parent 8fb3fb5aba
commit fa985d7fb7
9 changed files with 108 additions and 42 deletions

View File

@ -41,7 +41,7 @@ namespace LimeReport{
ConnectionDialog::ConnectionDialog(LimeReport::IConnectionController *conControl, LimeReport::ConnectionDesc* connectionDesc, QWidget *parent) :
QDialog(parent),
ui(new Ui::ConnectionDialog),m_connection(connectionDesc),m_controller(conControl)
ui(new Ui::ConnectionDialog), m_connection(connectionDesc), m_controller(conControl), m_savedConnectionName("")
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose,true);
@ -56,6 +56,7 @@ ConnectionDialog::~ConnectionDialog()
void ConnectionDialog::init()
{
ui->cbbDrivers->addItems(QSqlDatabase::drivers());
ui->cbbUseDefaultConnection->setEnabled(!m_controller->containsDefaultConnection());
}
void ConnectionDialog::showEvent(QShowEvent *)
@ -101,27 +102,20 @@ void ConnectionDialog::checkFieldsFill()
bool ConnectionDialog::checkConnection()
{
QScopedPointer<LimeReport::ConnectionDesc> con(uiToConnection());
// LimeReport::ConnectionDesc con;
// con.setName(ui->leConnectionName->text()+"_check");
// con.setHost(ui->leServerName->text());
// con.setUserName(ui->leUserName->text());
// con.setPassword(ui->lePassword->text());
// con.setDatabaseName(ui->leDataBase->text());
// con.setDriver(ui->cbbDrivers->currentText());
if (!m_controller->checkConnectionDesc(con.data())){
throw LimeReport::ReportError(m_controller->lastError());
}
return true;
}
LimeReport::ConnectionDesc *ConnectionDialog::uiToConnection(LimeReport::ConnectionDesc* conDesc)
ConnectionDesc *ConnectionDialog::uiToConnection(LimeReport::ConnectionDesc* conDesc)
{
LimeReport::ConnectionDesc* result;
if (conDesc)
result = conDesc;
else
result = new LimeReport::ConnectionDesc();
result ->setName(ui->leConnectionName->text());
result ->setName(ConnectionDesc::connectionNameForReport(ui->leConnectionName->text()));
result ->setHost(ui->leServerName->text());
result ->setDriver(ui->cbbDrivers->currentText());
result ->setUserName(ui->leUserName->text());
@ -135,7 +129,8 @@ void ConnectionDialog::connectionToUI()
{
init();
if (!m_connection) return;
ui->leConnectionName->setText(m_connection->name());
ui->leConnectionName->setText(ConnectionDesc::connectionNameForUser(m_connection->name()));
ui->cbbUseDefaultConnection->setChecked(m_connection->name().compare(QSqlDatabase::defaultConnection) == 0);
ui->leDataBase->setText(m_connection->databaseName());
ui->leServerName->setText(m_connection->host());
ui->leUserName->setText(m_connection->userName());
@ -144,10 +139,26 @@ void ConnectionDialog::connectionToUI()
ui->cbAutoConnect->setChecked(m_connection->autoconnect());
}
void ConnectionDialog::on_toolButton_clicked()
{
ui->leDataBase->setText(QFileDialog::getOpenFileName());
}
void ConnectionDialog::on_cbbUseDefaultConnection_toggled(bool checked)
{
if (checked){
m_savedConnectionName = ui->leConnectionName->text();
ui->leConnectionName->setText(tr("defaultConnection"));
ui->leConnectionName->setEnabled(false);
} else {
ui->leConnectionName->setText(m_savedConnectionName);
ui->leConnectionName->setEnabled(true);
}
}
} // namespace LimeReport

View File

@ -50,7 +50,7 @@ protected:
void init();
void checkFieldsFill();
bool checkConnection();
LimeReport::ConnectionDesc* uiToConnection(LimeReport::ConnectionDesc *conDesc = 0);
ConnectionDesc* uiToConnection(LimeReport::ConnectionDesc *conDesc = 0);
void connectionToUI();
signals:
void conectionRegistred(LimeReport::ConnectionDesc* connectionDesc);
@ -58,11 +58,14 @@ private slots:
void slotAccept();
void slotCheckConnection();
void on_toolButton_clicked();
void on_cbbUseDefaultConnection_toggled(bool checked);
private:
Ui::ConnectionDialog *ui;
LimeReport::ConnectionDesc* m_connection;
ConnectionDesc* m_connection;
bool m_changeMode;
LimeReport::IConnectionController* m_controller;
IConnectionController* m_controller;
QString m_savedConnectionName;
};
} // namespace LimeReport

View File

@ -36,6 +36,13 @@
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="cbbUseDefaultConnection">
<property name="text">
<string>Use default application connection</string>
</property>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">

View File

@ -126,17 +126,17 @@ void DataBrowser::slotSQLEditingFinished(SQLEditResult result)
void DataBrowser::slotDeleteConnection()
{
if (!getConnectionName().isEmpty()){
if (!getConnectionName(NameForUser).isEmpty()){
if (
QMessageBox::critical(
this,
tr("Attention"),
tr("Do you really want to delete \"%1\" connection ?").arg(getConnectionName()),
tr("Do you really want to delete \"%1\" connection ?").arg(getConnectionName(NameForUser)),
QMessageBox::Ok|QMessageBox::No,
QMessageBox::No
)==QMessageBox::Ok
) == QMessageBox::Ok
){
m_report->dataManager()->removeConnection(getConnectionName());
m_report->dataManager()->removeConnection(getConnectionName(NameForReport));
updateDataTree();
}
}
@ -153,7 +153,7 @@ void DataBrowser::slotAddDataSource()
#endif
sqlEdit->setSettings(settings());
sqlEdit->setDataSources(m_report->dataManager());
sqlEdit->setDefaultConnection(getConnectionName());
sqlEdit->setDefaultConnection(getConnectionName(NameForReport));
connect(sqlEdit,SIGNAL(signalSqlEditingFinished(SQLEditResult)),this,SLOT(slotSQLEditingFinished(SQLEditResult)));
sqlEdit->exec();
}
@ -173,7 +173,10 @@ void DataBrowser::updateDataTree()
foreach(QString dataSourceName, m_report->datasourcesNames()){
QTreeWidgetItem *item=new QTreeWidgetItem(QStringList(dataSourceName),DataBrowserTree::Table);
QTreeWidgetItem *parentItem = findByNameAndType(m_report->dataManager()->connectionName(dataSourceName),DataBrowserTree::Connection);
QTreeWidgetItem *parentItem = findByNameAndType(
ConnectionDesc::connectionNameForUser(m_report->dataManager()->connectionName(dataSourceName)),
DataBrowserTree::Connection
);
if (parentItem){
parentItem->addChild(item);
if (!parentItem->isExpanded()) ui->dataTree->expandItem(parentItem);
@ -336,11 +339,18 @@ QTreeWidgetItem* findConnectionItem(QTreeWidgetItem* item){
}
}
QString DataBrowser::getConnectionName()
QString DataBrowser::getConnectionName(NameType nameType)
{
if (ui->dataTree->currentItem()){
QTreeWidgetItem * ci = findConnectionItem(ui->dataTree->currentItem());
if (ci) return ci->text(0);
if (ci) {
switch (nameType) {
case NameForUser:
return ConnectionDesc::connectionNameForUser(ci->text(0));
case NameForReport:
return ConnectionDesc::connectionNameForReport(ci->text(0));
}
}
else return QString();
};
return QString();
@ -417,7 +427,11 @@ void DataBrowser::initConnections()
ui->dataTree->clear();
QList<QTreeWidgetItem *>items;
foreach(QString connectionName,m_report->dataManager()->connectionNames()){
QTreeWidgetItem *item=new QTreeWidgetItem(ui->dataTree,QStringList(connectionName), DataBrowserTree::Connection);
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
@ -482,10 +496,10 @@ void DataBrowser::slotDataWindowClosed()
void DataBrowser::slotChangeConnection()
{
if (!getConnectionName().isEmpty()){
if (!getConnectionName(NameForUser).isEmpty()){
ConnectionDialog *connectionEdit = new ConnectionDialog(
this,
m_report->dataManager()->connectionByName(getConnectionName()),
m_report->dataManager()->connectionByName(getConnectionName(NameForReport)),
this
);
connectionEdit->setAttribute(Qt::WA_DeleteOnClose,true);
@ -501,7 +515,7 @@ void DataBrowser::slotChangeConnection()
void DataBrowser::slotChangeConnectionState()
{
QString connectionName = getConnectionName();
QString connectionName = getConnectionName(NameForReport);
if (!connectionName.isEmpty()){
if (!m_report->dataManager()->isConnectionConnected(connectionName)){
setCursor(Qt::WaitCursor);
@ -626,6 +640,12 @@ bool DataBrowser::checkConnectionDesc(ConnectionDesc *connection)
return result;
}
bool DataBrowser::containsDefaultConnection()
{
bool result = m_report->dataManager()->connectionByName(QSqlDatabase::defaultConnection);
return result;
}
QString DataBrowser::lastError() const
{
return m_lastError;
@ -778,4 +798,4 @@ void DataBrowser::on_variablesTree_itemDoubleClicked(QTreeWidgetItem *item, int
}
}
} // namespace LimeReport
} // namespace LimeReport

View File

@ -87,8 +87,9 @@ private slots:
void on_variablesTree_itemDoubleClicked(QTreeWidgetItem *item, int);
private:
enum NameType{NameForUser, NameForReport};
QString getDatasourceName();
QString getConnectionName();
QString getConnectionName(NameType nameType);
QString getVariable();
bool isClosingWindows() const {return m_closingWindows;}
QTreeWidgetItem * findByNameAndType(QString name, int itemType);
@ -108,6 +109,7 @@ private:
void addConnectionDesc(ConnectionDesc *connection);
void changeConnectionDesc(ConnectionDesc *connection);
bool checkConnectionDesc(ConnectionDesc *connection);
bool containsDefaultConnection();
private:
Ui::DataBrowser* ui;

View File

@ -101,7 +101,7 @@ void SQLEditDialog::accept()
else result.resultMode=SQLEditResult::SubProxy;
}
result.connectionName=ui->cbbConnection->currentText();
result.connectionName = ConnectionDesc::connectionNameForReport(ui->cbbConnection->currentText());
result.datasourceName=ui->leDatasourceName->text();
result.sql=ui->textEditSQL->toPlainText();
result.dialogMode=m_dialogMode;
@ -159,15 +159,19 @@ void SQLEditDialog::check()
void SQLEditDialog::initConnections()
{
foreach(QString connectionName, QSqlDatabase::connectionNames()){
ui->cbbConnection->addItem(QIcon(":/databrowser/images/plug-connect.png"),connectionName);
ui->cbbConnection->addItem(QIcon(":/databrowser/images/plug-connect.png"),ConnectionDesc::connectionNameForUser(connectionName));
}
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"),connectionName);
ui->cbbConnection->addItem(QIcon(":/databrowser/images/plug-disconnect.png"),ConnectionDesc::connectionNameForUser(connectionName));
}
ui->cbbConnection->setCurrentIndex(ui->cbbConnection->findText(m_defaultConnection));
if (!m_oldDatasourceName.isEmpty()){
ui->cbbConnection->setCurrentIndex(ui->cbbConnection->findText(m_datasources->connectionName(m_oldDatasourceName)));
ui->cbbConnection->setCurrentIndex(ui->cbbConnection->findText(ConnectionDesc::connectionNameForUser(m_datasources->connectionName(m_oldDatasourceName))));
}
}
@ -202,7 +206,7 @@ void SQLEditDialog::setDataSources(LimeReport::DataSourceManager *dataSources, Q
void SQLEditDialog::setDefaultConnection(QString defaultConnection)
{
m_defaultConnection=defaultConnection;
m_defaultConnection = ConnectionDesc::connectionNameForUser(defaultConnection);
}
void SQLEditDialog::slotDataSourceNameEditing()
@ -298,7 +302,11 @@ void SQLEditDialog::slotPreviewData()
QMessageBox::critical(this,tr("Attention"),tr("Connection is not specified"));
return;
}
m_previewModel = m_datasources->previewSQL(ui->cbbConnection->currentText(),ui->textEditSQL->toPlainText(),ui->leMaster->text());
m_previewModel = m_datasources->previewSQL(
ConnectionDesc::connectionNameForReport(ui->cbbConnection->currentText()),
ui->textEditSQL->toPlainText(),
ui->leMaster->text()
);
if (m_previewModel){
ui->tvPreview->setModel(m_previewModel.data());
ui->gbDataPreview->setVisible(true);

View File

@ -378,7 +378,17 @@ bool ConnectionDesc::isEqual(const QSqlDatabase &db)
(db.hostName() == m_connectionHost) &&
(db.connectionName() == m_connectionName) &&
(db.userName() == m_user) &&
(db.password() == m_password);
(db.password() == m_password);
}
QString ConnectionDesc::connectionNameForUser(const QString &connectionName)
{
return connectionName.compare(QSqlDatabase::defaultConnection) == 0 ? tr("defaultConnection") : connectionName;
}
QString ConnectionDesc::connectionNameForReport(const QString &connectionName)
{
return connectionName.compare(tr("defaultConnection")) == 0 ? QSqlDatabase::defaultConnection : connectionName;
}
QueryDesc::QueryDesc(QString queryName, QString queryText, QString connection)

View File

@ -132,6 +132,9 @@ public:
bool isEqual(const QSqlDatabase& db);
bool isInternal(){ return m_internal; }
void setInternal(bool value) {m_internal = value;}
public:
static QString connectionNameForUser(const QString& connectionName);
static QString connectionNameForReport(const QString& connectionName);
signals:
void nameChanged(const QString& oldName,const QString& newName);
private:
@ -150,6 +153,7 @@ public:
virtual void addConnectionDesc(ConnectionDesc* connection) = 0;
virtual void changeConnectionDesc(ConnectionDesc* connection) = 0;
virtual bool checkConnectionDesc(ConnectionDesc* connection) = 0;
virtual bool containsDefaultConnection() = 0;
virtual QString lastError() const = 0;
};

View File

@ -622,16 +622,17 @@ void DataSourceManager::removeDatasource(const QString &name)
void DataSourceManager::removeConnection(const QString &connectionName)
{
for(int i=0;i<m_connections.count();++i){
if (m_connections.at(i)->name()==connectionName){
if (m_connections.at(i)->isInternal()){
QList<ConnectionDesc*>::iterator cit = m_connections.begin();
while( cit != m_connections.end() ){
if ( ((*cit)->name().compare(connectionName) == 0) && (*cit)->isInternal() ){
{
QSqlDatabase db = QSqlDatabase::database(connectionName);
db.close();
QSqlDatabase::removeDatabase(connectionName);
}
delete m_connections.at(i);
m_connections.removeAt(i);
QSqlDatabase::removeDatabase(connectionName);
}
delete (*cit);
cit = m_connections.erase(cit);
}
emit datasourcesChanged();
}