diff --git a/core/core.pro b/core/core.pro index ee89f4a..7d40d7c 100644 --- a/core/core.pro +++ b/core/core.pro @@ -65,7 +65,8 @@ SOURCES += \ reporting/reportdialog.cpp \ csvimporter.cpp \ importdialog.cpp \ - importprogress.cpp + importprogress.cpp \ + reporting/variablefiller.cpp HEADERS += core.h\ core_global.h \ @@ -130,7 +131,8 @@ HEADERS += core.h\ csvimporter.h \ iimportprogress.h \ importdialog.h \ - importprogress.h + importprogress.h \ + reporting/variablefiller.h unix { target.path = /usr/lib diff --git a/core/gridform.h b/core/gridform.h index e39fa19..fd72813 100644 --- a/core/gridform.h +++ b/core/gridform.h @@ -244,6 +244,16 @@ protected: } } + virtual int currentRecordId() + { + if (tableView()->currentIndex().isValid()) + { + return m_tableModel->itemFromIndex(tableView()->currentIndex())->id(); + } + + return 0; + } + void addRow(QSharedPointer entity) { m_tableModel->addRow(entity); diff --git a/core/igridform.cpp b/core/igridform.cpp index 12cf933..319f195 100644 --- a/core/igridform.cpp +++ b/core/igridform.cpp @@ -22,10 +22,17 @@ IGridForm::IGridForm(QWidget *parent) : m_columnDialog = new ColumnDialog(this); connect(m_columnDialog, SIGNAL(accepted()), this, SLOT(columnsAccepted())); + + m_varFiller = new VariableFiller(); } IGridForm::~IGridForm() { + if (m_varFiller != NULL) + { + delete m_varFiller; + } + delete ui; } @@ -59,6 +66,16 @@ QWidget *IGridForm::toolbar() return ui->widget; } +void IGridForm::setReportVarFiller(VariableFiller *filler) +{ + if (m_varFiller != NULL) + { + delete m_varFiller; + } + + m_varFiller = filler; +} + void IGridForm::hideColumns(const QList &cols) { foreach (int col, cols) { @@ -157,6 +174,12 @@ void IGridForm::on_btnPrint_clicked() { ReportDialog *dialog = new ReportDialog(this); dialog->setAttribute(Qt::WA_DeleteOnClose); + + if (m_varFiller != NULL) + { + m_varFiller->fillList(Context::instance().plugin(pluginId())->reports(), currentRecordId()); + } + dialog->setReports(Context::instance().plugin(pluginId())->reports()); dialog->show(); } diff --git a/core/igridform.h b/core/igridform.h index 9c0970a..b733387 100644 --- a/core/igridform.h +++ b/core/igridform.h @@ -12,6 +12,7 @@ #include "filterui.h" #include "defaultformhandler.h" #include "core_global.h" +#include "reporting/variablefiller.h" namespace Ui { class GridForm; @@ -30,6 +31,7 @@ public: QTableView *tableView(); QWidget *toolbar(); virtual void setTranslations(const QMap &translations) = 0; + void setReportVarFiller(VariableFiller *filler); signals: void dataChanged(); @@ -44,6 +46,7 @@ protected: virtual bool canAddRecord() { return true; } virtual bool canEditRecord() { return true; } virtual bool canDeleteRecord() { return true; } + virtual int currentRecordId() = 0; void hideColumns(const QList &cols); QWidget *filterWidget(); void enableButtons(); @@ -69,6 +72,7 @@ private: Ui::GridForm *ui; QMenu *m_contextMenu; ColumnDialog *m_columnDialog; + VariableFiller *m_varFiller; protected: FilterUi *m_filterUi; diff --git a/core/reporting/report.cpp b/core/reporting/report.cpp index 5b3fb45..d204046 100644 --- a/core/reporting/report.cpp +++ b/core/reporting/report.cpp @@ -55,7 +55,7 @@ void Report::setVariables(const QMap &variables) m_variables = variables; } -void Report::addVariable(const QString &varName, const QString &value) +void Report::setVariable(const QString &varName, const QString &value) { m_variables[varName] = value; } diff --git a/core/reporting/report.h b/core/reporting/report.h index 4a84170..fc048e3 100644 --- a/core/reporting/report.h +++ b/core/reporting/report.h @@ -27,7 +27,7 @@ public: QMap variables() const; void setVariables(const QMap &variables); - void addVariable(const QString &varName, const QString &value); + void setVariable(const QString &varName, const QString &value); private: QString m_name; diff --git a/core/reporting/reportviewer.cpp b/core/reporting/reportviewer.cpp index 49a2fb1..2ce3a49 100644 --- a/core/reporting/reportviewer.cpp +++ b/core/reporting/reportviewer.cpp @@ -32,6 +32,10 @@ void ReportViewer::setReport(ReportPtr report) m_report->loadFromByteArray(&data); m_report->setReportFileName(reportPath); m_report->dataManager()->setReportVariable("dbPath", Context::instance().settings()->value("db/path", "").toString()); + + foreach (QString key, report->variables().keys()) { + m_report->dataManager()->setReportVariable(key, report->variables()[key]); + } } void ReportViewer::openPreview() diff --git a/core/reporting/variablefiller.cpp b/core/reporting/variablefiller.cpp new file mode 100644 index 0000000..5ae0a8f --- /dev/null +++ b/core/reporting/variablefiller.cpp @@ -0,0 +1,52 @@ +#include "variablefiller.h" +#include "../settingsservice.h" + +VariableFiller::VariableFiller() +{ +} + +VariableFiller::~VariableFiller() +{ +} + +void VariableFiller::fill(ReportPtr report, int recordId) +{ + if (m_settings.isNull()) + { + loadSettings(); + } + + QMap vars; + vars[COMPANY] = m_settings->firmName(); + vars[STREET] = m_settings->street(); + vars[HOUSE_NUMBER] = m_settings->houseNumber(); + vars[CITY] = m_settings->city(); + vars[ZIP_CODE] = m_settings->zipCode(); + vars[IC] = QString::number(m_settings->ic()); + vars[DIC] = m_settings->dic(); + vars[LOGO_PATH] = m_settings->logoPath(); + + if (recordId > 0) + { + vars[RECORD_ID] = QString::number(recordId); + } + else + { + vars[RECORD_ID] = ""; + } + + report->setVariables(vars); +} + +void VariableFiller::fillList(QList reports, int recordId) +{ + foreach (ReportPtr report, reports) { + fill(report, recordId); + } +} + +void VariableFiller::loadSettings() +{ + SettingsService srv("CORE"); + m_settings = srv.loadSettings(); +} diff --git a/core/reporting/variablefiller.h b/core/reporting/variablefiller.h new file mode 100644 index 0000000..e410747 --- /dev/null +++ b/core/reporting/variablefiller.h @@ -0,0 +1,32 @@ +#ifndef VARIABLEFILLER_H +#define VARIABLEFILLER_H + +#include "report.h" +#include "../settings/globalsettings.h" +#include "../core_global.h" + +#define COMPANY "COMPANY" +#define STREET "STREET" +#define HOUSE_NUMBER "HOUSE_NUMBER" +#define CITY "CITY" +#define ZIP_CODE "ZIP_CODE" +#define IC "IC" +#define DIC "DIC" +#define LOGO_PATH "LOGO_PATH" +#define RECORD_ID "RECORD_ID" + +class CORESHARED_EXPORT VariableFiller +{ +public: + VariableFiller(); + virtual ~VariableFiller(); + + virtual void fill(ReportPtr report, int recordId = 0); + void fillList(QList reports, int recordId = 0); + void loadSettings(); + +private: + GlobalSettingsPtr m_settings; +}; + +#endif // VARIABLEFILLER_H