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

View File

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

View File

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

View File

@ -126,17 +126,17 @@ void DataBrowser::slotSQLEditingFinished(SQLEditResult result)
void DataBrowser::slotDeleteConnection() void DataBrowser::slotDeleteConnection()
{ {
if (!getConnectionName().isEmpty()){ if (!getConnectionName(NameForUser).isEmpty()){
if ( if (
QMessageBox::critical( QMessageBox::critical(
this, this,
tr("Attention"), 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::Ok|QMessageBox::No,
QMessageBox::No QMessageBox::No
)==QMessageBox::Ok ) == QMessageBox::Ok
){ ){
m_report->dataManager()->removeConnection(getConnectionName()); m_report->dataManager()->removeConnection(getConnectionName(NameForReport));
updateDataTree(); updateDataTree();
} }
} }
@ -153,7 +153,7 @@ void DataBrowser::slotAddDataSource()
#endif #endif
sqlEdit->setSettings(settings()); sqlEdit->setSettings(settings());
sqlEdit->setDataSources(m_report->dataManager()); sqlEdit->setDataSources(m_report->dataManager());
sqlEdit->setDefaultConnection(getConnectionName()); sqlEdit->setDefaultConnection(getConnectionName(NameForReport));
connect(sqlEdit,SIGNAL(signalSqlEditingFinished(SQLEditResult)),this,SLOT(slotSQLEditingFinished(SQLEditResult))); connect(sqlEdit,SIGNAL(signalSqlEditingFinished(SQLEditResult)),this,SLOT(slotSQLEditingFinished(SQLEditResult)));
sqlEdit->exec(); sqlEdit->exec();
} }
@ -173,7 +173,10 @@ void DataBrowser::updateDataTree()
foreach(QString dataSourceName, m_report->datasourcesNames()){ foreach(QString dataSourceName, m_report->datasourcesNames()){
QTreeWidgetItem *item=new QTreeWidgetItem(QStringList(dataSourceName),DataBrowserTree::Table); 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){ if (parentItem){
parentItem->addChild(item); parentItem->addChild(item);
if (!parentItem->isExpanded()) ui->dataTree->expandItem(parentItem); 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()){ if (ui->dataTree->currentItem()){
QTreeWidgetItem * ci = findConnectionItem(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(); else return QString();
}; };
return QString(); return QString();
@ -417,7 +427,11 @@ void DataBrowser::initConnections()
ui->dataTree->clear(); ui->dataTree->clear();
QList<QTreeWidgetItem *>items; QList<QTreeWidgetItem *>items;
foreach(QString connectionName,m_report->dataManager()->connectionNames()){ 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)) if (m_report->dataManager()->isConnectionConnected(connectionName))
item->setIcon(0,QIcon(":/databrowser/images/database_connected")); item->setIcon(0,QIcon(":/databrowser/images/database_connected"));
else else
@ -482,10 +496,10 @@ void DataBrowser::slotDataWindowClosed()
void DataBrowser::slotChangeConnection() void DataBrowser::slotChangeConnection()
{ {
if (!getConnectionName().isEmpty()){ if (!getConnectionName(NameForUser).isEmpty()){
ConnectionDialog *connectionEdit = new ConnectionDialog( ConnectionDialog *connectionEdit = new ConnectionDialog(
this, this,
m_report->dataManager()->connectionByName(getConnectionName()), m_report->dataManager()->connectionByName(getConnectionName(NameForReport)),
this this
); );
connectionEdit->setAttribute(Qt::WA_DeleteOnClose,true); connectionEdit->setAttribute(Qt::WA_DeleteOnClose,true);
@ -501,7 +515,7 @@ void DataBrowser::slotChangeConnection()
void DataBrowser::slotChangeConnectionState() void DataBrowser::slotChangeConnectionState()
{ {
QString connectionName = getConnectionName(); QString connectionName = getConnectionName(NameForReport);
if (!connectionName.isEmpty()){ if (!connectionName.isEmpty()){
if (!m_report->dataManager()->isConnectionConnected(connectionName)){ if (!m_report->dataManager()->isConnectionConnected(connectionName)){
setCursor(Qt::WaitCursor); setCursor(Qt::WaitCursor);
@ -626,6 +640,12 @@ bool DataBrowser::checkConnectionDesc(ConnectionDesc *connection)
return result; return result;
} }
bool DataBrowser::containsDefaultConnection()
{
bool result = m_report->dataManager()->connectionByName(QSqlDatabase::defaultConnection);
return result;
}
QString DataBrowser::lastError() const QString DataBrowser::lastError() const
{ {
return m_lastError; 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); void on_variablesTree_itemDoubleClicked(QTreeWidgetItem *item, int);
private: private:
enum NameType{NameForUser, NameForReport};
QString getDatasourceName(); QString getDatasourceName();
QString getConnectionName(); QString getConnectionName(NameType nameType);
QString getVariable(); QString getVariable();
bool isClosingWindows() const {return m_closingWindows;} bool isClosingWindows() const {return m_closingWindows;}
QTreeWidgetItem * findByNameAndType(QString name, int itemType); QTreeWidgetItem * findByNameAndType(QString name, int itemType);
@ -108,6 +109,7 @@ private:
void addConnectionDesc(ConnectionDesc *connection); void addConnectionDesc(ConnectionDesc *connection);
void changeConnectionDesc(ConnectionDesc *connection); void changeConnectionDesc(ConnectionDesc *connection);
bool checkConnectionDesc(ConnectionDesc *connection); bool checkConnectionDesc(ConnectionDesc *connection);
bool containsDefaultConnection();
private: private:
Ui::DataBrowser* ui; Ui::DataBrowser* ui;

View File

@ -101,7 +101,7 @@ void SQLEditDialog::accept()
else result.resultMode=SQLEditResult::SubProxy; else result.resultMode=SQLEditResult::SubProxy;
} }
result.connectionName=ui->cbbConnection->currentText(); result.connectionName = ConnectionDesc::connectionNameForReport(ui->cbbConnection->currentText());
result.datasourceName=ui->leDatasourceName->text(); result.datasourceName=ui->leDatasourceName->text();
result.sql=ui->textEditSQL->toPlainText(); result.sql=ui->textEditSQL->toPlainText();
result.dialogMode=m_dialogMode; result.dialogMode=m_dialogMode;
@ -159,15 +159,19 @@ void SQLEditDialog::check()
void SQLEditDialog::initConnections() void SQLEditDialog::initConnections()
{ {
foreach(QString connectionName, QSqlDatabase::connectionNames()){ 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()){ foreach(QString connectionName, m_datasources->connectionNames()){
connectionName = (connectionName.compare(QSqlDatabase::defaultConnection)==0) ?
tr("defaultConnection") : connectionName;
if (ui->cbbConnection->findText(connectionName,Qt::MatchExactly )==-1) 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)); ui->cbbConnection->setCurrentIndex(ui->cbbConnection->findText(m_defaultConnection));
if (!m_oldDatasourceName.isEmpty()){ 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) void SQLEditDialog::setDefaultConnection(QString defaultConnection)
{ {
m_defaultConnection=defaultConnection; m_defaultConnection = ConnectionDesc::connectionNameForUser(defaultConnection);
} }
void SQLEditDialog::slotDataSourceNameEditing() void SQLEditDialog::slotDataSourceNameEditing()
@ -298,7 +302,11 @@ void SQLEditDialog::slotPreviewData()
QMessageBox::critical(this,tr("Attention"),tr("Connection is not specified")); QMessageBox::critical(this,tr("Attention"),tr("Connection is not specified"));
return; 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){ if (m_previewModel){
ui->tvPreview->setModel(m_previewModel.data()); ui->tvPreview->setModel(m_previewModel.data());
ui->gbDataPreview->setVisible(true); ui->gbDataPreview->setVisible(true);

View File

@ -378,7 +378,17 @@ bool ConnectionDesc::isEqual(const QSqlDatabase &db)
(db.hostName() == m_connectionHost) && (db.hostName() == m_connectionHost) &&
(db.connectionName() == m_connectionName) && (db.connectionName() == m_connectionName) &&
(db.userName() == m_user) && (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) QueryDesc::QueryDesc(QString queryName, QString queryText, QString connection)

View File

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

View File

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