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 "lrconnectiondialog.h"
#include "lrreportengine_p.h" #include "lrreportengine_p.h"
#include "lrvariabledialog.h" #include "lrvariabledialog.h"
#include "lrdatabrowsertree.h"
namespace LimeReport{ namespace LimeReport{
@ -596,7 +595,7 @@ void DataBrowser::changeQuery(SQLEditResult result)
{ {
try { try {
m_report->dataManager()->removeDatasource(result.oldDatasourceName); m_report->dataManager()->removeDatasource(result.oldDatasourceName);
m_report->dataManager()->addQuery(result.datasourceName, result.sql, result.connectionName); addQuery(result);
} catch(ReportError &exception){ } catch(ReportError &exception){
qDebug()<<exception.what(); qDebug()<<exception.what();
} }
@ -615,7 +614,7 @@ void DataBrowser::changeSubQuery(SQLEditResult result)
{ {
try { try {
m_report->dataManager()->removeDatasource(result.oldDatasourceName); m_report->dataManager()->removeDatasource(result.oldDatasourceName);
m_report->dataManager()->addSubQuery(result.datasourceName, result.sql, result.connectionName, result.masterDatasource); addSubQuery(result);
} catch(ReportError &exception){ } catch(ReportError &exception){
qDebug()<<exception.what(); qDebug()<<exception.what();
} }
@ -634,7 +633,31 @@ void DataBrowser::changeProxy(SQLEditResult result)
{ {
try { try {
m_report->dataManager()->removeDatasource(result.oldDatasourceName); 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){ } catch(ReportError &exception){
qDebug()<<exception.what(); 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()->isQuery(datasourceName)) return SQLEditResult::Query;
if (m_report->dataManager()->isSubQuery(datasourceName)) return SQLEditResult::SubQuery; if (m_report->dataManager()->isSubQuery(datasourceName)) return SQLEditResult::SubQuery;
if (m_report->dataManager()->isProxy(datasourceName)) return SQLEditResult::SubProxy; if (m_report->dataManager()->isProxy(datasourceName)) return SQLEditResult::SubProxy;
if (m_report->dataManager()->isCSV(datasourceName)) return SQLEditResult::CSVText;
return SQLEditResult::Undefined; return SQLEditResult::Undefined;
} }
@ -662,12 +686,16 @@ void DataBrowser::applyChanges(SQLEditResult result)
case SQLEditResult::SubProxy: case SQLEditResult::SubProxy:
changeProxy(result); changeProxy(result);
break; break;
case SQLEditResult::CSVText:
changeCSV(result);
break;
default: break; default: break;
} }
} else { } else {
removeDatasource(result.datasourceName); removeDatasource(result.datasourceName);
addDatasource(result); addDatasource(result);
} }
activateItem(result.datasourceName, DataBrowserTree::Table);
} }
void DataBrowser::addDatasource(SQLEditResult result) void DataBrowser::addDatasource(SQLEditResult result)
@ -682,21 +710,32 @@ void DataBrowser::addDatasource(SQLEditResult result)
case SQLEditResult::SubProxy: case SQLEditResult::SubProxy:
addProxy(result); addProxy(result);
break; break;
case SQLEditResult::CSVText:
addCSV(result);
break;
default: default:
break; 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) void DataBrowser::addConnectionDesc(ConnectionDesc *connection)
{ {
m_report->dataManager()->addConnectionDesc(connection); m_report->dataManager()->addConnectionDesc(connection);
updateDataTree(); updateDataTree();
activateItem(connection->name(), DataBrowserTree::Connection);
} }
void DataBrowser::changeConnectionDesc(ConnectionDesc *connection) void DataBrowser::changeConnectionDesc(ConnectionDesc *connection)
{ {
if (connection->autoconnect()) m_report->dataManager()->connectConnection(connection->name()); if (connection->autoconnect()) m_report->dataManager()->connectConnection(connection->name());
updateDataTree(); updateDataTree();
activateItem(connection->name(), DataBrowserTree::Connection);
} }
bool DataBrowser::checkConnectionDesc(ConnectionDesc *connection) bool DataBrowser::checkConnectionDesc(ConnectionDesc *connection)

View File

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

View File

@ -94,7 +94,9 @@ void SQLEditDialog::accept()
{ {
SQLEditResult result; SQLEditResult result;
if (!ui->cbSubdetail->isChecked()){ if (ui->tabWidget->currentIndex() == 1){
result.resultMode = SQLEditResult::CSVText;
} else if (!ui->cbSubdetail->isChecked()){
result.resultMode=SQLEditResult::Query; result.resultMode=SQLEditResult::Query;
} else { } else {
if (ui->rbSubQuery->isChecked()) result.resultMode = SQLEditResult::SubQuery; if (ui->rbSubQuery->isChecked()) result.resultMode = SQLEditResult::SubQuery;
@ -103,12 +105,15 @@ void SQLEditDialog::accept()
result.connectionName = ConnectionDesc::connectionNameForReport(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->sqlText->toPlainText();
result.csv = ui->csvText->toPlainText();
result.dialogMode = m_dialogMode; result.dialogMode = m_dialogMode;
result.oldDatasourceName = m_oldDatasourceName; result.oldDatasourceName = m_oldDatasourceName;
result.subdetail = ui->cbSubdetail->isChecked(); result.subdetail = ui->cbSubdetail->isChecked();
result.masterDatasource=ui->leMaster->text(); result.masterDatasource=ui->leMaster->text();
result.childDataSource=ui->leChild->text(); result.childDataSource=ui->leChild->text();
result.separator = ui->leSeparator->text();
result.firstRowIsHeader = ui->cbUseFirstRowAsHeader->isChecked();
if (ui->fieldsMap->rowCount() > 0){ if (ui->fieldsMap->rowCount() > 0){
for(int i=0; i< ui->fieldsMap->rowCount(); ++i){ for(int i=0; i< ui->fieldsMap->rowCount(); ++i){
@ -148,7 +153,7 @@ void SQLEditDialog::hideEvent(QHideEvent *)
void SQLEditDialog::check() void SQLEditDialog::check()
{ {
if (ui->leDatasourceName->text().isEmpty()) throw LimeReport::ReportError(tr("Datasource Name is empty!")); 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_dialogMode==AddMode){
if (m_datasources->containsDatasource(ui->leDatasourceName->text())){ if (m_datasources->containsDatasource(ui->leDatasourceName->text())){
throw LimeReport::ReportError(QString(tr("Datasource with name: \"%1\" already exists!")).arg(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; m_datasources=dataSources;
if (!datasourceName.isEmpty()){ if (!datasourceName.isEmpty()){
ui->cbSubdetail->setEnabled(true); ui->cbSubdetail->setEnabled(true);
initQueryMode();
m_oldDatasourceName=datasourceName; m_oldDatasourceName=datasourceName;
ui->leDatasourceName->setText(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)){ if (dataSources->isSubQuery(datasourceName)){
initSubQueryMode(); initSubQueryMode();
ui->leMaster->setText(dataSources->subQueryByName(datasourceName)->master()); ui->leMaster->setText(dataSources->subQueryByName(datasourceName)->master());
@ -201,6 +208,12 @@ void SQLEditDialog::setDataSources(LimeReport::DataSourceManager *dataSources, Q
curIndex++; 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); ui->fieldsMap->setRowCount(ui->fieldsMap->rowCount()+1);
} }
void SQLEditDialog::initQueryMode() void SQLEditDialog::initQueryMode()
{ {
ui->gbSQL->setVisible(true); ui->gbSQL->setVisible(true);
@ -264,6 +278,7 @@ void SQLEditDialog::initQueryMode()
ui->cbSubdetail->setChecked(false); ui->cbSubdetail->setChecked(false);
ui->leMaster->setVisible(false); ui->leMaster->setVisible(false);
ui->lbMaster->setVisible(false); ui->lbMaster->setVisible(false);
ui->tabWidget->removeTab(1);
} }
void SQLEditDialog::initSubQueryMode() void SQLEditDialog::initSubQueryMode()
@ -278,6 +293,7 @@ void SQLEditDialog::initSubQueryMode()
ui->leMaster->setVisible(true); ui->leMaster->setVisible(true);
ui->leMaster->setEnabled(true); ui->leMaster->setEnabled(true);
ui->lbMaster->setVisible(true); ui->lbMaster->setVisible(true);
ui->tabWidget->removeTab(1);
} }
void SQLEditDialog::initProxyMode() void SQLEditDialog::initProxyMode()
@ -293,6 +309,12 @@ void SQLEditDialog::initProxyMode()
ui->leMaster->setEnabled(true); ui->leMaster->setEnabled(true);
ui->lbMaster->setVisible(true); ui->lbMaster->setVisible(true);
ui->cbSubdetail->setEnabled(false); ui->cbSubdetail->setEnabled(false);
ui->tabWidget->removeTab(1);
}
void SQLEditDialog::initCSVMode()
{
ui->tabWidget->removeTab(0);
} }
void SQLEditDialog::slotPreviewData() void SQLEditDialog::slotPreviewData()
@ -303,7 +325,7 @@ void SQLEditDialog::slotPreviewData()
} }
m_previewModel = m_datasources->previewSQL( m_previewModel = m_datasources->previewSQL(
ConnectionDesc::connectionNameForReport(ui->cbbConnection->currentText()), ConnectionDesc::connectionNameForReport(ui->cbbConnection->currentText()),
ui->textEditSQL->toPlainText(), ui->sqlText->toPlainText(),
ui->leMaster->text() ui->leMaster->text()
); );
if (m_previewModel){ if (m_previewModel){

View File

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

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>411</width> <width>422</width>
<height>617</height> <height>622</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -21,30 +21,6 @@
<string/> <string/>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_6"> <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> <item>
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
<item> <item>
@ -207,6 +183,15 @@
</color> </color>
</brush> </brush>
</colorrole> </colorrole>
<colorrole role="PlaceholderText">
<brush brushstyle="NoBrush">
<color alpha="128">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</active> </active>
<inactive> <inactive>
<colorrole role="WindowText"> <colorrole role="WindowText">
@ -344,6 +329,15 @@
</color> </color>
</brush> </brush>
</colorrole> </colorrole>
<colorrole role="PlaceholderText">
<brush brushstyle="NoBrush">
<color alpha="128">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</inactive> </inactive>
<disabled> <disabled>
<colorrole role="WindowText"> <colorrole role="WindowText">
@ -481,6 +475,15 @@
</color> </color>
</brush> </brush>
</colorrole> </colorrole>
<colorrole role="PlaceholderText">
<brush brushstyle="NoBrush">
<color alpha="128">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</colorrole>
</disabled> </disabled>
</palette> </palette>
</property> </property>
@ -497,6 +500,40 @@
</item> </item>
</layout> </layout>
</item> </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> <item>
<layout class="QVBoxLayout" name="verticalLayout_4"> <layout class="QVBoxLayout" name="verticalLayout_4">
<item> <item>
@ -568,7 +605,7 @@
<number>2</number> <number>2</number>
</property> </property>
<item> <item>
<widget class="QTextEdit" name="textEditSQL"/> <widget class="QTextEdit" name="sqlText"/>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_6"> <layout class="QHBoxLayout" name="horizontalLayout_6">
@ -747,6 +784,70 @@
</layout> </layout>
</widget> </widget>
</item> </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> <item>
<layout class="QHBoxLayout" name="horizontalLayout_5"> <layout class="QHBoxLayout" name="horizontalLayout_5">
<item> <item>
@ -787,39 +888,14 @@
</layout> </layout>
</widget> </widget>
<tabstops> <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_2</tabstop>
<tabstop>pushButton</tabstop> <tabstop>pushButton</tabstop>
</tabstops> </tabstops>
<resources> <resources>
<include location="lrdatabrowser.qrc"/> <include location="lrdatabrowser.qrc"/>
<include location="lrdatabrowser.qrc"/>
</resources> </resources>
<connections> <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> <connection>
<sender>pushButton</sender> <sender>pushButton</sender>
<signal>clicked()</signal> <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 } //namespace LimeReport

View File

@ -33,6 +33,7 @@
#include <QObject> #include <QObject>
#include <QMap> #include <QMap>
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QStandardItemModel>
#include <QtSql/QSqlQuery> #include <QtSql/QSqlQuery>
#include <QDebug> #include <QDebug>
#include <QSharedPointer> #include <QSharedPointer>
@ -131,6 +132,64 @@ public:
virtual QString lastError() const = 0; 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{ class QueryDesc : public QObject{
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString queryName READ queryName WRITE setQueryName) Q_PROPERTY(QString queryName READ queryName WRITE setQueryName)
@ -140,11 +199,11 @@ public:
QueryDesc(QString queryName, QString queryText, QString connection); QueryDesc(QString queryName, QString queryText, QString connection);
explicit QueryDesc(QObject* parent=0):QObject(parent){} explicit QueryDesc(QObject* parent=0):QObject(parent){}
void setQueryName(QString value){m_queryName=value;} 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);} 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;} void setConnectionName(QString value){m_connectionName=value;}
QString connectionName(){return m_connectionName;} QString connectionName() const {return m_connectionName;}
signals: signals:
void queryTextChanged(const QString& queryName, const QString& queryText); void queryTextChanged(const QString& queryName, const QString& queryText);
private: private:

View File

@ -537,7 +537,7 @@ void DataSourceManager::addSubQuery(const QString &name, const QString &sqlText,
emit datasourcesChanged(); 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 *proxyDesc = new ProxyDesc();
proxyDesc->setName(name); proxyDesc->setName(name);
@ -552,6 +552,15 @@ void DataSourceManager::addProxy(const QString &name, QString master, QString de
emit datasourcesChanged(); 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) QString DataSourceManager::queryText(const QString &dataSourceName)
{ {
if (isQuery(dataSourceName)) return queryByName(dataSourceName)->queryText(); if (isQuery(dataSourceName)) return queryByName(dataSourceName)->queryText();
@ -559,16 +568,16 @@ QString DataSourceManager::queryText(const QString &dataSourceName)
else return QString(); 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); if (queryIndex!=-1) return m_queries.at(queryIndex);
return 0; 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); if (queryIndex!=-1) return m_subqueries.at(queryIndex);
return 0; return 0;
} }
@ -607,6 +616,15 @@ int DataSourceManager::proxyIndexByName(const QString &dataSourceName)
return -1; 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) int DataSourceManager::connectionIndexByName(const QString &connectionName)
{ {
for(int i=0;i<m_connections.count();++i){ for(int i=0;i<m_connections.count();++i){
@ -639,13 +657,20 @@ QString DataSourceManager::connectionName(const QString &dataSourceName)
return QString(); return QString();
} }
ProxyDesc *DataSourceManager::proxyByName(QString datasourceName) ProxyDesc *DataSourceManager::proxyByName(const QString &datasourceName)
{ {
int proxyIndex = proxyIndexByName(datasourceName); int proxyIndex = proxyIndexByName(datasourceName);
if (proxyIndex > -1) return m_proxies.at(proxyIndex); if (proxyIndex > -1) return m_proxies.at(proxyIndex);
else return 0; else return 0;
} }
CSVDesc *DataSourceManager::csvByName(const QString &datasourceName)
{
int csvIndex = csvIndexByName(datasourceName);
if (csvIndex > -1) return m_csvs.at(csvIndex);
else return 0;
}
void DataSourceManager::removeDatasource(const QString &name) void DataSourceManager::removeDatasource(const QString &name)
{ {
invalidateLinkedDatasources(name); invalidateLinkedDatasources(name);
@ -671,6 +696,11 @@ void DataSourceManager::removeDatasource(const QString &name)
delete m_proxies.at(proxyIndex); delete m_proxies.at(proxyIndex);
m_proxies.removeAt(proxyIndex); m_proxies.removeAt(proxyIndex);
} }
if (isCSV(name)){
int csvIndex=csvIndexByName(name);
delete m_csvs.at(csvIndex);
m_csvs.removeAt(csvIndex);
}
m_hasChanges = true; m_hasChanges = true;
emit datasourcesChanged(); emit datasourcesChanged();
} }
@ -763,6 +793,15 @@ void DataSourceManager::putProxyDesc(ProxyDesc *proxyDesc)
} else throw ReportError(tr("Datasource with name \"%1\" already exists!").arg(proxyDesc->name())); } 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 DataSourceManager::initAndOpenDB(QSqlDatabase& db, ConnectionDesc& connectionDesc){
bool connected = false; bool connected = false;
@ -933,7 +972,7 @@ bool DataSourceManager::containsDatasource(const QString &dataSourceName)
bool DataSourceManager::isSubQuery(const QString &dataSourceName) bool DataSourceManager::isSubQuery(const QString &dataSourceName)
{ {
return subQueryIndexByName(dataSourceName.toLower())!=-1; return subQueryIndexByName(dataSourceName) != -1;
} }
bool DataSourceManager::isProxy(const QString &dataSourceName) bool DataSourceManager::isProxy(const QString &dataSourceName)
@ -941,6 +980,11 @@ 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) bool DataSourceManager::isConnection(const QString &connectionName)
{ {
return connectionIndexByName(connectionName) != -1; return connectionIndexByName(connectionName) != -1;
@ -1089,6 +1133,12 @@ QObject *DataSourceManager::createElement(const QString& collectionName, const Q
return var; return var;
} }
if (collectionName=="csvs"){
CSVDesc* csvDesc = new CSVDesc;
m_csvs.append(csvDesc);
return csvDesc;
}
return 0; return 0;
} }
@ -1109,6 +1159,9 @@ int DataSourceManager::elementsCount(const QString &collectionName)
if (collectionName=="variables"){ if (collectionName=="variables"){
return m_reportVariables.variablesCount(); return m_reportVariables.variablesCount();
} }
if (collectionName=="csvs"){
return m_csvs.count();
}
return 0; return 0;
} }
@ -1129,6 +1182,9 @@ QObject* DataSourceManager::elementAt(const QString &collectionName, int index)
if (collectionName=="variables"){ if (collectionName=="variables"){
return m_reportVariables.variableAt(index); return m_reportVariables.variableAt(index);
} }
if (collectionName=="csvs"){
return m_csvs.at(index);
}
return 0; return 0;
} }
@ -1206,6 +1262,25 @@ void DataSourceManager::collectionLoadFinished(const QString &collectionName)
m_tempVars.clear(); m_tempVars.clear();
} }
EASY_END_BLOCK; 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()){ if (designTime()){
EASY_BLOCK("emit datasourcesChanged()"); EASY_BLOCK("emit datasourcesChanged()");
emit datasourcesChanged(); emit datasourcesChanged();
@ -1328,6 +1403,14 @@ void DataSourceManager::slotVariableHasBeenChanged(const QString& variableName)
m_hasChanges = true; 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) void DataSourceManager::clear(ClearMethod method)
{ {
DataSourcesMap::iterator dit; 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 subqueries READ fakeCollectionReader)
Q_PROPERTY(ACollectionProperty subproxies READ fakeCollectionReader) Q_PROPERTY(ACollectionProperty subproxies READ fakeCollectionReader)
Q_PROPERTY(ACollectionProperty variables READ fakeCollectionReader) Q_PROPERTY(ACollectionProperty variables READ fakeCollectionReader)
Q_PROPERTY(ACollectionProperty csvs READ fakeCollectionReader)
friend class ReportEnginePrivate; friend class ReportEnginePrivate;
friend class ReportRender; friend class ReportRender;
public: public:
@ -112,7 +113,8 @@ public:
bool checkConnectionDesc(ConnectionDesc *connection); bool checkConnectionDesc(ConnectionDesc *connection);
void addQuery(const QString& name, const QString& sqlText, const QString& connectionName=""); 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 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); bool addModel(const QString& name, QAbstractItemModel *model, bool owned);
void removeModel(const QString& name); void removeModel(const QString& name);
ICallbackDatasource* createCallbackDatasource(const QString &name); ICallbackDatasource* createCallbackDatasource(const QString &name);
@ -143,18 +145,21 @@ public:
bool containsDatasource(const QString& dataSourceName); bool containsDatasource(const QString& dataSourceName);
bool isSubQuery(const QString& dataSourceName); bool isSubQuery(const QString& dataSourceName);
bool isProxy(const QString& dataSourceName); bool isProxy(const QString& dataSourceName);
bool isCSV(const QString& datasourceName);
bool isConnection(const QString& connectionName); bool isConnection(const QString& connectionName);
bool isConnectionConnected(const QString& connectionName); bool isConnectionConnected(const QString& connectionName);
bool connectConnection(const QString &connectionName); bool connectConnection(const QString &connectionName);
void connectAutoConnections(); void connectAutoConnections();
void disconnectConnection(const QString &connectionName); void disconnectConnection(const QString &connectionName);
QueryDesc* queryByName(const QString& dataSourceName); QueryDesc* queryByName(const QString& datasourceName);
SubQueryDesc* subQueryByName(const QString& dataSourceName); SubQueryDesc* subQueryByName(const QString& datasourceName);
ProxyDesc* proxyByName(QString datasourceName); ProxyDesc* proxyByName(const QString& datasourceName);
CSVDesc* csvByName(const QString& datasourceName);
ConnectionDesc *connectionByName(const QString& connectionName); ConnectionDesc *connectionByName(const QString& connectionName);
int queryIndexByName(const QString& dataSourceName); int queryIndexByName(const QString& dataSourceName);
int subQueryIndexByName(const QString& dataSourceName); int subQueryIndexByName(const QString& dataSourceName);
int proxyIndexByName(const QString& dataSourceName); int proxyIndexByName(const QString& dataSourceName);
int csvIndexByName(const QString& dataSourceName);
int connectionIndexByName(const QString& connectionName); int connectionIndexByName(const QString& connectionName);
QList<ConnectionDesc *> &conections(); QList<ConnectionDesc *> &conections();
@ -226,6 +231,7 @@ protected:
void putQueryDesc(QueryDesc *queryDesc); void putQueryDesc(QueryDesc *queryDesc);
void putSubQueryDesc(SubQueryDesc *subQueryDesc); void putSubQueryDesc(SubQueryDesc *subQueryDesc);
void putProxyDesc(ProxyDesc *proxyDesc); void putProxyDesc(ProxyDesc *proxyDesc);
void putCSVDesc(CSVDesc* csvDesc);
bool connectConnection(ConnectionDesc* connectionDesc); bool connectConnection(ConnectionDesc* connectionDesc);
void clearReportVariables(); void clearReportVariables();
QList<QString> childDatasources(const QString& datasourceName); QList<QString> childDatasources(const QString& datasourceName);
@ -246,6 +252,7 @@ private slots:
void slotQueryTextChanged(const QString& queryName, const QString& queryText); void slotQueryTextChanged(const QString& queryName, const QString& queryText);
void slotVariableHasBeenAdded(const QString& variableName); void slotVariableHasBeenAdded(const QString& variableName);
void slotVariableHasBeenChanged(const QString& variableName); void slotVariableHasBeenChanged(const QString& variableName);
void slotCSVTextChanged(const QString& csvName, const QString& csvText);
private: private:
explicit DataSourceManager(QObject *parent = 0); explicit DataSourceManager(QObject *parent = 0);
bool initAndOpenDB(QSqlDatabase &db, ConnectionDesc &connectionDesc); bool initAndOpenDB(QSqlDatabase &db, ConnectionDesc &connectionDesc);
@ -256,6 +263,7 @@ private:
QList<SubQueryDesc*> m_subqueries; QList<SubQueryDesc*> m_subqueries;
QList<ProxyDesc*> m_proxies; QList<ProxyDesc*> m_proxies;
QList<VarDesc*> m_tempVars; QList<VarDesc*> m_tempVars;
QList<CSVDesc*> m_csvs;
QMultiMap<QString,GroupFunction*> m_groupFunctions; QMultiMap<QString,GroupFunction*> m_groupFunctions;
GroupFunctionFactory m_groupFunctionFactory; GroupFunctionFactory m_groupFunctionFactory;