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

Added ability to create CSV data source

This commit is contained in:
Arin Alexander 2019-04-04 04:37:18 +03:00
parent de5cc0e0ca
commit 51b2effa3c
9 changed files with 737 additions and 329 deletions

View File

@ -46,7 +46,6 @@
#include "lrconnectiondialog.h"
#include "lrreportengine_p.h"
#include "lrvariabledialog.h"
#include "lrdatabrowsertree.h"
namespace LimeReport{
@ -596,8 +595,8 @@ void DataBrowser::changeQuery(SQLEditResult result)
{
try {
m_report->dataManager()->removeDatasource(result.oldDatasourceName);
m_report->dataManager()->addQuery(result.datasourceName, result.sql, result.connectionName);
}catch(ReportError &exception){
addQuery(result);
} catch(ReportError &exception){
qDebug()<<exception.what();
}
}
@ -615,8 +614,8 @@ void DataBrowser::changeSubQuery(SQLEditResult result)
{
try {
m_report->dataManager()->removeDatasource(result.oldDatasourceName);
m_report->dataManager()->addSubQuery(result.datasourceName, result.sql, result.connectionName, result.masterDatasource);
}catch(ReportError &exception){
addSubQuery(result);
} catch(ReportError &exception){
qDebug()<<exception.what();
}
}
@ -634,7 +633,31 @@ void DataBrowser::changeProxy(SQLEditResult result)
{
try {
m_report->dataManager()->removeDatasource(result.oldDatasourceName);
m_report->dataManager()->addProxy(result.datasourceName,result.masterDatasource,result.childDataSource,result.fieldMap);
addProxy(result);
} catch(ReportError &exception){
qDebug()<<exception.what();
}
}
void DataBrowser::addCSV(SQLEditResult result)
{
try {
m_report->dataManager()->addCSV(
result.datasourceName,
result.csv,
result.separator,
result.firstRowIsHeader
);
} catch(ReportError &exception){
qDebug()<<exception.what();
}
}
void DataBrowser::changeCSV(SQLEditResult result)
{
try {
m_report->dataManager()->removeDatasource(result.oldDatasourceName);
addCSV(result);
} catch(ReportError &exception){
qDebug()<<exception.what();
}
@ -645,6 +668,7 @@ SQLEditResult::ResultMode DataBrowser::currentDatasourceType(const QString& data
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;
return SQLEditResult::Undefined;
}
@ -662,12 +686,16 @@ void DataBrowser::applyChanges(SQLEditResult result)
case SQLEditResult::SubProxy:
changeProxy(result);
break;
case SQLEditResult::CSVText:
changeCSV(result);
break;
default: break;
}
} else {
removeDatasource(result.datasourceName);
addDatasource(result);
}
activateItem(result.datasourceName, DataBrowserTree::Table);
}
void DataBrowser::addDatasource(SQLEditResult result)
@ -682,21 +710,32 @@ void DataBrowser::addDatasource(SQLEditResult result)
case SQLEditResult::SubProxy:
addProxy(result);
break;
case SQLEditResult::CSVText:
addCSV(result);
break;
default:
break;
}
activateItem(result.datasourceName, DataBrowserTree::Table);
}
void DataBrowser::activateItem(const QString& name, DataBrowserTree::NodeType type){
QTreeWidgetItem* item = findByNameAndType(name, type);
item->treeWidget()->setCurrentItem(item);
}
void DataBrowser::addConnectionDesc(ConnectionDesc *connection)
{
m_report->dataManager()->addConnectionDesc(connection);
updateDataTree();
activateItem(connection->name(), DataBrowserTree::Connection);
}
void DataBrowser::changeConnectionDesc(ConnectionDesc *connection)
{
if (connection->autoconnect()) m_report->dataManager()->connectConnection(connection->name());
updateDataTree();
activateItem(connection->name(), DataBrowserTree::Connection);
}
bool DataBrowser::checkConnectionDesc(ConnectionDesc *connection)

View File

@ -37,6 +37,7 @@
#include "lrreportdesignwidget.h"
#include "lrsqleditdialog.h"
#include "lrdatabrowsertree.h"
namespace LimeReport{
@ -105,7 +106,8 @@ private:
void changeSubQuery(SQLEditResult result);
void addProxy(SQLEditResult result);
void changeProxy(SQLEditResult result);
void addCSV(SQLEditResult result);
void changeCSV(SQLEditResult result);
SQLEditResult::ResultMode currentDatasourceType(const QString& datasourceName);
void applyChanges(SQLEditResult result);
@ -115,6 +117,7 @@ private:
void changeConnectionDesc(ConnectionDesc *connection);
bool checkConnectionDesc(ConnectionDesc *connection);
bool containsDefaultConnection();
void activateItem(const QString &name, DataBrowserTree::NodeType type);
private:
Ui::DataBrowser* ui;

View File

@ -86,7 +86,7 @@ QSettings *SQLEditDialog::settings(){
void SQLEditDialog::setSettings(QSettings *value, bool owned){
if (m_settings && m_ownedSettings) delete m_settings;
m_settings=value;
m_settings = value;
m_ownedSettings = owned;
}
@ -94,24 +94,29 @@ void SQLEditDialog::accept()
{
SQLEditResult result;
if (!ui->cbSubdetail->isChecked()){
if (ui->tabWidget->currentIndex() == 1){
result.resultMode = SQLEditResult::CSVText;
} else if (!ui->cbSubdetail->isChecked()){
result.resultMode=SQLEditResult::Query;
} else {
if (ui->rbSubQuery->isChecked()) result.resultMode=SQLEditResult::SubQuery;
if (ui->rbSubQuery->isChecked()) result.resultMode = SQLEditResult::SubQuery;
else result.resultMode=SQLEditResult::SubProxy;
}
result.connectionName = ConnectionDesc::connectionNameForReport(ui->cbbConnection->currentText());
result.datasourceName=ui->leDatasourceName->text();
result.sql=ui->textEditSQL->toPlainText();
result.dialogMode=m_dialogMode;
result.oldDatasourceName=m_oldDatasourceName;
result.sql = ui->sqlText->toPlainText();
result.csv = ui->csvText->toPlainText();
result.dialogMode = m_dialogMode;
result.oldDatasourceName = m_oldDatasourceName;
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();
if (ui->fieldsMap->rowCount()>0){
for(int i=0;i<ui->fieldsMap->rowCount();++i){
if (ui->fieldsMap->rowCount() > 0){
for(int i=0; i< ui->fieldsMap->rowCount(); ++i){
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() : "";
@ -148,7 +153,7 @@ void SQLEditDialog::hideEvent(QHideEvent *)
void SQLEditDialog::check()
{
if (ui->leDatasourceName->text().isEmpty()) throw LimeReport::ReportError(tr("Datasource Name is empty!"));
if (ui->textEditSQL->toPlainText().isEmpty() && (!ui->rbProxy) ) throw LimeReport::ReportError(tr("SQL 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()));
@ -180,10 +185,12 @@ void SQLEditDialog::setDataSources(LimeReport::DataSourceManager *dataSources, Q
m_datasources=dataSources;
if (!datasourceName.isEmpty()){
ui->cbSubdetail->setEnabled(true);
initQueryMode();
m_oldDatasourceName=datasourceName;
ui->leDatasourceName->setText(datasourceName);
ui->textEditSQL->setText(dataSources->queryText(datasourceName));
ui->sqlText->setText(dataSources->queryText(datasourceName));
if (dataSources->isQuery(datasourceName)){
initQueryMode();
}
if (dataSources->isSubQuery(datasourceName)){
initSubQueryMode();
ui->leMaster->setText(dataSources->subQueryByName(datasourceName)->master());
@ -201,6 +208,12 @@ void SQLEditDialog::setDataSources(LimeReport::DataSourceManager *dataSources, Q
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();
}
}
}
@ -254,6 +267,7 @@ void SQLEditDialog::on_pbAddField_clicked()
ui->fieldsMap->setRowCount(ui->fieldsMap->rowCount()+1);
}
void SQLEditDialog::initQueryMode()
{
ui->gbSQL->setVisible(true);
@ -264,6 +278,7 @@ void SQLEditDialog::initQueryMode()
ui->cbSubdetail->setChecked(false);
ui->leMaster->setVisible(false);
ui->lbMaster->setVisible(false);
ui->tabWidget->removeTab(1);
}
void SQLEditDialog::initSubQueryMode()
@ -278,6 +293,7 @@ void SQLEditDialog::initSubQueryMode()
ui->leMaster->setVisible(true);
ui->leMaster->setEnabled(true);
ui->lbMaster->setVisible(true);
ui->tabWidget->removeTab(1);
}
void SQLEditDialog::initProxyMode()
@ -293,6 +309,12 @@ void SQLEditDialog::initProxyMode()
ui->leMaster->setEnabled(true);
ui->lbMaster->setVisible(true);
ui->cbSubdetail->setEnabled(false);
ui->tabWidget->removeTab(1);
}
void SQLEditDialog::initCSVMode()
{
ui->tabWidget->removeTab(0);
}
void SQLEditDialog::slotPreviewData()
@ -303,7 +325,7 @@ void SQLEditDialog::slotPreviewData()
}
m_previewModel = m_datasources->previewSQL(
ConnectionDesc::connectionNameForReport(ui->cbbConnection->currentText()),
ui->textEditSQL->toPlainText(),
ui->sqlText->toPlainText(),
ui->leMaster->text()
);
if (m_previewModel){

View File

@ -77,6 +77,7 @@ private slots:
void initQueryMode();
void initSubQueryMode();
void initProxyMode();
void initCSVMode();
void slotPreviewData();
void slotHidePreview();
private:
@ -96,17 +97,20 @@ private:
};
struct SQLEditResult{
enum ResultMode{Query, SubQuery, SubProxy, Undefined};
enum ResultMode{Query, SubQuery, SubProxy, CSVText, Undefined};
QString connectionName;
QString datasourceName;
QString oldDatasourceName;
QString sql;
QString csv;
bool subdetail;
ResultMode resultMode;
QString masterDatasource;
QString childDataSource;
SQLEditDialog::SQLDialogMode dialogMode;
QList<LimeReport::FieldsCorrelation> fieldMap;
QString separator;
bool firstRowIsHeader;
};
} // namespace LimeReport

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>411</width>
<height>617</height>
<width>422</width>
<height>622</height>
</rect>
</property>
<property name="windowTitle">
@ -21,30 +21,6 @@
<string/>
</property>
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Connection</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cbbConnection">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
@ -207,6 +183,15 @@
</color>
</brush>
</colorrole>
<colorrole role="PlaceholderText">
<brush brushstyle="NoBrush">
<color alpha="128">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</active>
<inactive>
<colorrole role="WindowText">
@ -344,6 +329,15 @@
</color>
</brush>
</colorrole>
<colorrole role="PlaceholderText">
<brush brushstyle="NoBrush">
<color alpha="128">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</inactive>
<disabled>
<colorrole role="WindowText">
@ -481,6 +475,15 @@
</color>
</brush>
</colorrole>
<colorrole role="PlaceholderText">
<brush brushstyle="NoBrush">
<color alpha="128">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</disabled>
</palette>
</property>
@ -497,6 +500,40 @@
</item>
</layout>
</item>
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="sqlTab">
<attribute name="title">
<string>SQL</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_8">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Connection</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="cbbConnection">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
@ -568,7 +605,7 @@
<number>2</number>
</property>
<item>
<widget class="QTextEdit" name="textEditSQL"/>
<widget class="QTextEdit" name="sqlText"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_6">
@ -747,6 +784,70 @@
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="csvTab">
<attribute name="title">
<string>CSV</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_9">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Separator</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="leSeparator">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>30</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>;</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="cbUseFirstRowAsHeader">
<property name="text">
<string>Use first row as header</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QPlainTextEdit" name="csvText"/>
</item>
</layout>
</widget>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
@ -787,39 +888,14 @@
</layout>
</widget>
<tabstops>
<tabstop>leDatasourceName</tabstop>
<tabstop>cbSubdetail</tabstop>
<tabstop>leMaster</tabstop>
<tabstop>rbSubQuery</tabstop>
<tabstop>rbProxy</tabstop>
<tabstop>textEditSQL</tabstop>
<tabstop>leChild</tabstop>
<tabstop>fieldsMap</tabstop>
<tabstop>pbAddField</tabstop>
<tabstop>pbDelField</tabstop>
<tabstop>pushButton_2</tabstop>
<tabstop>pushButton</tabstop>
</tabstops>
<resources>
<include location="lrdatabrowser.qrc"/>
<include location="lrdatabrowser.qrc"/>
</resources>
<connections>
<connection>
<sender>leDatasourceName</sender>
<signal>textChanged(QString)</signal>
<receiver>LimeReport::SQLEditDialog</receiver>
<slot>slotDataSourceNameEditing()</slot>
<hints>
<hint type="sourcelabel">
<x>259</x>
<y>54</y>
</hint>
<hint type="destinationlabel">
<x>289</x>
<y>32</y>
</hint>
</hints>
</connection>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>

View File

@ -831,4 +831,118 @@ bool CallbackDatasource::checkIfEmpty(){
}
}
QString CSVDesc::name() const
{
return m_csvName;
}
void CSVDesc::setName(const QString &csvName)
{
m_csvName = csvName;
}
QString CSVDesc::csvText() const
{
return m_csvText;
}
void CSVDesc::setCsvText(const QString &csvText)
{
m_csvText = csvText;
emit cvsTextChanged(m_csvName, m_csvText);
}
QString CSVDesc::separator() const
{
return m_separator;
}
void CSVDesc::setSeparator(const QString &separator)
{
m_separator = separator;
}
bool CSVDesc::firstRowIsHeader() const
{
return m_firstRowIsHeader;
}
void CSVDesc::setFirstRowIsHeader(bool firstRowIsHeader)
{
m_firstRowIsHeader = firstRowIsHeader;
}
void CSVHolder::updateModel()
{
m_model.clear();
QString sep = (separator().compare("\\t") == 0) ? "\t" : separator();
bool firstRow = true;
QList<QStandardItem*> columns;
QStringList headers;
foreach(QString line, m_csvText.split('\n')){
columns.clear();
foreach(QString item, line.split(sep)){
columns.append(new QStandardItem(item));
if (firstRow && m_firstRowIsHeader) headers.append(item);
}
if (firstRow){
if (!headers.isEmpty()){
m_model.setHorizontalHeaderLabels(headers);
firstRow = false;
} else {
m_model.appendRow(columns);
}
} else {
m_model.appendRow(columns);
}
}
}
bool CSVHolder::firsRowIsHeader() const
{
return m_firstRowIsHeader;
}
void CSVHolder::setFirsRowIsHeader(bool firstRowIsHeader)
{
m_firstRowIsHeader = firstRowIsHeader;
}
CSVHolder::CSVHolder(const CSVDesc &desc, DataSourceManager *dataManager)
: m_csvText(desc.csvText()),
m_separator(desc.separator()),
m_dataManager(dataManager),
m_firstRowIsHeader(desc.firstRowIsHeader())
{
m_dataSource = IDataSource::Ptr(new ModelToDataSource(&m_model, false));
updateModel();
}
void CSVHolder::setCSVText(QString csvText)
{
m_csvText = csvText;
updateModel();
}
QString CSVHolder::separator() const
{
return m_separator;
}
void CSVHolder::setSeparator(const QString &separator)
{
m_separator = separator;
updateModel();
}
IDataSource *CSVHolder::dataSource(IDataSource::DatasourceMode mode)
{
Q_UNUSED(mode);
return m_dataSource.data();
}
} //namespace LimeReport

View File

@ -33,6 +33,7 @@
#include <QObject>
#include <QMap>
#include <QAbstractItemModel>
#include <QStandardItemModel>
#include <QtSql/QSqlQuery>
#include <QDebug>
#include <QSharedPointer>
@ -131,6 +132,64 @@ public:
virtual QString lastError() const = 0;
};
class CSVDesc: public QObject{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(QString csvText READ csvText WRITE setCsvText)
Q_PROPERTY(QString separator READ separator WRITE setSeparator)
Q_PROPERTY(bool firstRowIsHeader READ firstRowIsHeader WRITE setFirstRowIsHeader)
public:
CSVDesc(const QString name, const QString csvText, QString separator, bool firstRowIsHeader)
: m_csvName(name), m_csvText(csvText), m_separator(separator), m_firstRowIsHeader(firstRowIsHeader){}
explicit CSVDesc(QObject* parent = 0):QObject(parent) {}
QString name() const;
void setName(const QString &name);
QString csvText() const;
void setCsvText(const QString &csvText);
QString separator() const;
void setSeparator(const QString &separator);
bool firstRowIsHeader() const;
void setFirstRowIsHeader(bool firstRowIsHeader);
signals:
void cvsTextChanged(const QString& cvsName, const QString& cvsText);
private:
QString m_csvName;
QString m_csvText;
QString m_separator;
bool m_firstRowIsHeader;
};
class CSVHolder: public IDataSourceHolder{
public:
CSVHolder(const CSVDesc& desc, DataSourceManager* dataManager);
void setCSVText(QString csvText);
QString csvText() { return m_csvText;}
QString separator() const;
void setSeparator(const QString &separator);
bool firsRowIsHeader() const;
void setFirsRowIsHeader(bool firstRowIsHeader);
// IDataSourceHolder interface
public:
IDataSource *dataSource(IDataSource::DatasourceMode mode = IDataSource::RENDER_MODE);
QString lastError() const {return "";}
bool isInvalid() const {return false;}
bool isOwned() const {return true;}
bool isEditable() const {return true;}
bool isRemovable() const {return true;}
void invalidate(IDataSource::DatasourceMode mode, bool dbWillBeClosed){ updateModel();}
void update(){ updateModel(); }
void clearErrors(){}
private:
void updateModel();
private:
QString m_csvText;
QStandardItemModel m_model;
QString m_separator;
IDataSource::Ptr m_dataSource;
DataSourceManager* m_dataManager;
bool m_firstRowIsHeader;
};
class QueryDesc : public QObject{
Q_OBJECT
Q_PROPERTY(QString queryName READ queryName WRITE setQueryName)
@ -140,11 +199,11 @@ public:
QueryDesc(QString queryName, QString queryText, QString connection);
explicit QueryDesc(QObject* parent=0):QObject(parent){}
void setQueryName(QString value){m_queryName=value;}
QString queryName(){return m_queryName;}
QString queryName() const {return m_queryName;}
void setQueryText(QString value){m_queryText=value; emit queryTextChanged(m_queryName, m_queryText);}
QString queryText(){return m_queryText;}
QString queryText() const {return m_queryText;}
void setConnectionName(QString value){m_connectionName=value;}
QString connectionName(){return m_connectionName;}
QString connectionName() const {return m_connectionName;}
signals:
void queryTextChanged(const QString& queryName, const QString& queryText);
private:

View File

@ -179,7 +179,7 @@ DataNode* DataSourceModel::nodeFromIndex(const QModelIndex& index) const
void DataSourceModel::fillFields(DataNode* parent)
{
foreach(QString name, m_dataManager->fieldNames(parent->name())){
parent->addChild(name,DataNode::Field,QIcon(":/report/images/field"));
parent->addChild(name, DataNode::Field,QIcon(":/report/images/field"));
}
}
@ -537,7 +537,7 @@ void DataSourceManager::addSubQuery(const QString &name, const QString &sqlText,
emit datasourcesChanged();
}
void DataSourceManager::addProxy(const QString &name, QString master, QString detail, QList<FieldsCorrelation> fields)
void DataSourceManager::addProxy(const QString &name, const QString &master, const QString &detail, QList<FieldsCorrelation> fields)
{
ProxyDesc *proxyDesc = new ProxyDesc();
proxyDesc->setName(name);
@ -552,6 +552,15 @@ void DataSourceManager::addProxy(const QString &name, QString master, QString de
emit datasourcesChanged();
}
void DataSourceManager::addCSV(const QString& name, const QString& csvText, const QString &separator, bool firstRowIsHeader)
{
CSVDesc* csvDesc = new CSVDesc(name, csvText, separator, firstRowIsHeader);
putCSVDesc(csvDesc);
putHolder(name, new CSVHolder(*csvDesc, this));
m_hasChanges = true;
emit datasourcesChanged();
}
QString DataSourceManager::queryText(const QString &dataSourceName)
{
if (isQuery(dataSourceName)) return queryByName(dataSourceName)->queryText();
@ -559,16 +568,16 @@ QString DataSourceManager::queryText(const QString &dataSourceName)
else return QString();
}
QueryDesc *DataSourceManager::queryByName(const QString &dataSourceName)
QueryDesc *DataSourceManager::queryByName(const QString &datasourceName)
{
int queryIndex = queryIndexByName(dataSourceName);
int queryIndex = queryIndexByName(datasourceName);
if (queryIndex!=-1) return m_queries.at(queryIndex);
return 0;
}
SubQueryDesc* DataSourceManager::subQueryByName(const QString &dataSourceName)
SubQueryDesc* DataSourceManager::subQueryByName(const QString &datasourceName)
{
int queryIndex = subQueryIndexByName(dataSourceName);
int queryIndex = subQueryIndexByName(datasourceName);
if (queryIndex!=-1) return m_subqueries.at(queryIndex);
return 0;
}
@ -607,6 +616,15 @@ int DataSourceManager::proxyIndexByName(const QString &dataSourceName)
return -1;
}
int DataSourceManager::csvIndexByName(const QString &dataSourceName)
{
for(int i=0; i < m_csvs.count();++i){
CSVDesc* desc=m_csvs.at(i);
if (desc->name().compare(dataSourceName,Qt::CaseInsensitive)==0) return i;
}
return -1;
}
int DataSourceManager::connectionIndexByName(const QString &connectionName)
{
for(int i=0;i<m_connections.count();++i){
@ -639,10 +657,17 @@ QString DataSourceManager::connectionName(const QString &dataSourceName)
return QString();
}
ProxyDesc *DataSourceManager::proxyByName(QString datasourceName)
ProxyDesc *DataSourceManager::proxyByName(const QString &datasourceName)
{
int proxyIndex = proxyIndexByName(datasourceName);
if (proxyIndex>-1) return m_proxies.at(proxyIndex);
if (proxyIndex > -1) return m_proxies.at(proxyIndex);
else return 0;
}
CSVDesc *DataSourceManager::csvByName(const QString &datasourceName)
{
int csvIndex = csvIndexByName(datasourceName);
if (csvIndex > -1) return m_csvs.at(csvIndex);
else return 0;
}
@ -671,6 +696,11 @@ void DataSourceManager::removeDatasource(const QString &name)
delete m_proxies.at(proxyIndex);
m_proxies.removeAt(proxyIndex);
}
if (isCSV(name)){
int csvIndex=csvIndexByName(name);
delete m_csvs.at(csvIndex);
m_csvs.removeAt(csvIndex);
}
m_hasChanges = true;
emit datasourcesChanged();
}
@ -763,6 +793,15 @@ void DataSourceManager::putProxyDesc(ProxyDesc *proxyDesc)
} else throw ReportError(tr("Datasource with name \"%1\" already exists!").arg(proxyDesc->name()));
}
void DataSourceManager::putCSVDesc(CSVDesc *csvDesc)
{
if (!containsDatasource(csvDesc->name())){
m_csvs.append(csvDesc);
connect(csvDesc, SIGNAL(cvsTextChanged(QString, QString)),
this, SLOT(slotCSVTextChanged(QString, QString)));
} else throw ReportError(tr("Datasource with name \"%1\" already exists!").arg(csvDesc->name()));
}
bool DataSourceManager::initAndOpenDB(QSqlDatabase& db, ConnectionDesc& connectionDesc){
bool connected = false;
@ -933,17 +972,22 @@ bool DataSourceManager::containsDatasource(const QString &dataSourceName)
bool DataSourceManager::isSubQuery(const QString &dataSourceName)
{
return subQueryIndexByName(dataSourceName.toLower())!=-1;
return subQueryIndexByName(dataSourceName) != -1;
}
bool DataSourceManager::isProxy(const QString &dataSourceName)
{
return proxyIndexByName(dataSourceName)!=-1;
return proxyIndexByName(dataSourceName) != -1;
}
bool DataSourceManager::isCSV(const QString &datasourceName)
{
return csvIndexByName(datasourceName) != -1;
}
bool DataSourceManager::isConnection(const QString &connectionName)
{
return connectionIndexByName(connectionName)!=-1;
return connectionIndexByName(connectionName) != -1;
}
bool DataSourceManager::isConnectionConnected(const QString &connectionName)
@ -1045,7 +1089,7 @@ QStringList DataSourceManager::fieldNames(const QString &datasourceName)
QStringList result;
IDataSource* ds = dataSource(datasourceName);
if (ds && !ds->isInvalid()){
for(int i=0;i<ds->columnCount();i++){
for(int i=0; i < ds->columnCount(); i++){
result.append(ds->columnNameByIndex(i));
}
result.sort();
@ -1089,6 +1133,12 @@ QObject *DataSourceManager::createElement(const QString& collectionName, const Q
return var;
}
if (collectionName=="csvs"){
CSVDesc* csvDesc = new CSVDesc;
m_csvs.append(csvDesc);
return csvDesc;
}
return 0;
}
@ -1109,6 +1159,9 @@ int DataSourceManager::elementsCount(const QString &collectionName)
if (collectionName=="variables"){
return m_reportVariables.variablesCount();
}
if (collectionName=="csvs"){
return m_csvs.count();
}
return 0;
}
@ -1129,6 +1182,9 @@ QObject* DataSourceManager::elementAt(const QString &collectionName, int index)
if (collectionName=="variables"){
return m_reportVariables.variableAt(index);
}
if (collectionName=="csvs"){
return m_csvs.at(index);
}
return 0;
}
@ -1206,6 +1262,25 @@ void DataSourceManager::collectionLoadFinished(const QString &collectionName)
m_tempVars.clear();
}
EASY_END_BLOCK;
if (collectionName.compare("csvs", Qt::CaseInsensitive) == 0){
QMutableListIterator<CSVDesc*> it(m_csvs);
while (it.hasNext()){
it.next();
if (!m_datasources.contains(it.value()->name().toLower())){
connect(it.value(), SIGNAL(cvsTextChanged(QString,QString)),
this, SLOT(slotCSVTextChanged(QString,QString)));
putHolder(
it.value()->name(),
new CSVHolder(*it.value(), this)
);
} else {
delete it.value();
it.remove();
}
}
}
if (designTime()){
EASY_BLOCK("emit datasourcesChanged()");
emit datasourcesChanged();
@ -1328,6 +1403,14 @@ void DataSourceManager::slotVariableHasBeenChanged(const QString& variableName)
m_hasChanges = true;
}
void DataSourceManager::slotCSVTextChanged(const QString &csvName, const QString &csvText)
{
CSVHolder* holder = dynamic_cast<CSVHolder*>(m_datasources.value(csvName));
if (holder){
holder->setCSVText(csvText);
}
}
void DataSourceManager::clear(ClearMethod method)
{
DataSourcesMap::iterator dit;

View File

@ -100,6 +100,7 @@ class DataSourceManager : public QObject, public ICollectionContainer, public IV
Q_PROPERTY(ACollectionProperty subqueries READ fakeCollectionReader)
Q_PROPERTY(ACollectionProperty subproxies READ fakeCollectionReader)
Q_PROPERTY(ACollectionProperty variables READ fakeCollectionReader)
Q_PROPERTY(ACollectionProperty csvs READ fakeCollectionReader)
friend class ReportEnginePrivate;
friend class ReportRender;
public:
@ -112,7 +113,8 @@ public:
bool checkConnectionDesc(ConnectionDesc *connection);
void addQuery(const QString& name, const QString& sqlText, const QString& connectionName="");
void addSubQuery(const QString& name, const QString& sqlText, const QString& connectionName, const QString& masterDatasource);
void addProxy(const QString& name, QString master, QString detail, QList<FieldsCorrelation> fields);
void addProxy(const QString& name, const QString& master, const QString& detail, QList<FieldsCorrelation> fields);
void addCSV(const QString& name, const QString& csvText, const QString& separator, bool firstRowIsHeader);
bool addModel(const QString& name, QAbstractItemModel *model, bool owned);
void removeModel(const QString& name);
ICallbackDatasource* createCallbackDatasource(const QString &name);
@ -143,18 +145,21 @@ public:
bool containsDatasource(const QString& dataSourceName);
bool isSubQuery(const QString& dataSourceName);
bool isProxy(const QString& dataSourceName);
bool isCSV(const QString& datasourceName);
bool isConnection(const QString& connectionName);
bool isConnectionConnected(const QString& connectionName);
bool connectConnection(const QString &connectionName);
void connectAutoConnections();
void disconnectConnection(const QString &connectionName);
QueryDesc* queryByName(const QString& dataSourceName);
SubQueryDesc* subQueryByName(const QString& dataSourceName);
ProxyDesc* proxyByName(QString datasourceName);
QueryDesc* queryByName(const QString& datasourceName);
SubQueryDesc* subQueryByName(const QString& datasourceName);
ProxyDesc* proxyByName(const QString& datasourceName);
CSVDesc* csvByName(const QString& datasourceName);
ConnectionDesc *connectionByName(const QString& connectionName);
int queryIndexByName(const QString& dataSourceName);
int subQueryIndexByName(const QString& dataSourceName);
int proxyIndexByName(const QString& dataSourceName);
int csvIndexByName(const QString& dataSourceName);
int connectionIndexByName(const QString& connectionName);
QList<ConnectionDesc *> &conections();
@ -226,6 +231,7 @@ protected:
void putQueryDesc(QueryDesc *queryDesc);
void putSubQueryDesc(SubQueryDesc *subQueryDesc);
void putProxyDesc(ProxyDesc *proxyDesc);
void putCSVDesc(CSVDesc* csvDesc);
bool connectConnection(ConnectionDesc* connectionDesc);
void clearReportVariables();
QList<QString> childDatasources(const QString& datasourceName);
@ -246,6 +252,7 @@ private slots:
void slotQueryTextChanged(const QString& queryName, const QString& queryText);
void slotVariableHasBeenAdded(const QString& variableName);
void slotVariableHasBeenChanged(const QString& variableName);
void slotCSVTextChanged(const QString& csvName, const QString& csvText);
private:
explicit DataSourceManager(QObject *parent = 0);
bool initAndOpenDB(QSqlDatabase &db, ConnectionDesc &connectionDesc);
@ -256,6 +263,7 @@ private:
QList<SubQueryDesc*> m_subqueries;
QList<ProxyDesc*> m_proxies;
QList<VarDesc*> m_tempVars;
QList<CSVDesc*> m_csvs;
QMultiMap<QString,GroupFunction*> m_groupFunctions;
GroupFunctionFactory m_groupFunctionFactory;