1 /***************************************************************************
2  *   This file is part of the Lime Report project                          *
3  *   Copyright (C) 2015 by Alexander Arin                                  *
4  *   arin_a@bk.ru                                                          *
5  *                                                                         *
6  **                   GNU General Public License Usage                    **
7  *                                                                         *
8  *   This library is free software: you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation, either version 3 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *   You should have received a copy of the GNU General Public License     *
13  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
14  *                                                                         *
15  **                  GNU Lesser General Public License                    **
16  *                                                                         *
17  *   This library is free software: you can redistribute it and/or modify  *
18  *   it under the terms of the GNU Lesser General Public License as        *
19  *   published by the Free Software Foundation, either version 3 of the    *
20  *   License, or (at your option) any later version.                       *
21  *   You should have received a copy of the GNU Lesser General Public      *
22  *   License along with this library.                                      *
23  *   If not, see <http://www.gnu.org/licenses/>.                           *
24  *                                                                         *
25  *   This library is distributed in the hope that it will be useful,       *
26  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
27  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
28  *   GNU General Public License for more details.                          *
29  ****************************************************************************/
30 #ifndef LRREPORTDESIGNINTF_P_H
31 #define LRREPORTDESIGNINTF_P_H
32 
33 #include <QObject>
34 #include <QSharedPointer>
35 #include <QMainWindow>
36 #include <QLocale>
37 #include "lrreportengine.h"
38 #include "lrcollection.h"
39 #include "lrglobal.h"
40 #include "lrdatasourcemanager.h"
41 #include "lrbanddesignintf.h"
42 #include "lrreportrender.h"
43 #include "serializators/lrstorageintf.h"
44 #include "lrscriptenginemanager.h"
45 #include "lrreporttranslation.h"
46 #include "lrdesignerplugininterface.h"
47 #include "lrreportdesignwindowintrerface.h"
48 
49 class QFileSystemWatcher;
50 
51 
52 namespace LimeReport{
53 
54 class PageDesignIntf;
55 class PrintRange;
56 class ReportDesignWindow;
57 class ReportExporterInterface;
58 
59 
60 class WatermarkHelper{
61 public:
WatermarkHelper(const WatermarkSetting & watermark)62     WatermarkHelper(const WatermarkSetting& watermark)
63         : m_watermark(watermark){}
64     qreal sceneX();
65     qreal sceneY();
66     qreal sceneWidth();
67     qreal sceneHeight();
68     QPointF scenePos();
69     QSizeF  sceneSize();
70     QPointF mapToPage(const PageItemDesignIntf &page);
71 private:
72     qreal valueToPixels(qreal value);
73 private:
74     const WatermarkSetting& m_watermark;
75 };
76 
77 
78 class ReportEnginePrivateInterface {
79 public:
80     virtual PageDesignIntf*         appendPage(const QString& pageName="") = 0;
81     virtual bool                    deletePage(PageDesignIntf *page) = 0;
82     virtual void                    reorderPages(const QList<PageDesignIntf *> &reorderedPages) = 0;
83     virtual int                     pageCount() = 0;
84     virtual PageDesignIntf*         pageAt(int index) = 0;
85     virtual void                    clearReport() = 0;
86     virtual ScriptEngineContext*    scriptContext() = 0;
87     virtual ScriptEngineManager*    scriptManager() = 0;
88     virtual DataSourceManager*      dataManager() = 0;
89     virtual QString                 reportFileName() = 0;
90     virtual void                    setReportFileName(const QString& reportFileName) = 0;
91     virtual bool                    isNeedToSave() = 0;
92     virtual bool                    emitSaveReport() = 0;
93     virtual bool                    emitSaveReportAs() = 0;
94     virtual void                    emitSaveFinished() = 0;
95     virtual bool                    saveToFile(const QString& fileName = "") = 0;
96     virtual bool                    isSaved() = 0;
97     virtual QString                 reportName() = 0;
98     virtual bool                    loadFromFile(const QString& fileName, bool autoLoadPreviewOnChange) = 0;
99     virtual bool                    emitLoadReport() = 0;
100     virtual void                    emitLoadFinished() = 0;
101     virtual void                    clearSelection() = 0;
102     virtual bool                    printReport(QPrinter *printer=0) = 0;
103     virtual void                    previewReport(PreviewHints hints = PreviewBarsUserSetting) = 0;
104     virtual void                    setCurrentReportsDir(const QString& dirName) = 0;
105     virtual QString                 currentReportsDir() = 0;
106     virtual bool                    suppressFieldAndVarError() const = 0;
107     virtual void                    setSuppressFieldAndVarError(bool suppressFieldAndVarError) = 0;
108     virtual void                    setStyleSheet(const QString& styleSheet) = 0;
109     virtual QString                 styleSheet() const = 0;
110     virtual QList<QLocale::Language> designerLanguages() = 0;
111     virtual QLocale::Language       currentDesignerLanguage() = 0;
112     virtual void                    setCurrentDesignerLanguage(QLocale::Language language) = 0;
113     virtual void                    cancelRender() = 0;
114     virtual void                    setShowProgressDialog(bool value) = 0;
115     virtual bool                    isShowProgressDialog() const = 0;
116     virtual bool                    isBusy() = 0;
117 };
118 
119 class PrintProcessor{
120 public:
121     explicit PrintProcessor(QPrinter* printer);
~PrintProcessor()122     ~PrintProcessor(){ if (m_painter) delete m_painter;}
123     bool printPage(LimeReport::PageItemDesignIntf::Ptr page);
124 private:
125     void initPrinter(PageItemDesignIntf* page);
126 private:
127     QPrinter* m_printer;
128     QPainter* m_painter;
129     LimeReport::PageDesignIntf m_renderPage;
130     bool m_firstPage;
131 };
132 
133 class ReportEnginePrivate : public QObject,
134         public ICollectionContainer,
135         public ITranslationContainer,
136         public IExternalPainter,
137         public ReportEnginePrivateInterface
138 {
139     Q_OBJECT
140     Q_DECLARE_PUBLIC(ReportEngine)
141     Q_PROPERTY(ACollectionProperty pages READ fakeCollectionReader())
142     Q_PROPERTY(QObject* datasourcesManager READ dataManager)
143     Q_PROPERTY(QObject* scriptContext READ scriptContext)
144     Q_PROPERTY(bool suppressFieldAndVarError READ suppressFieldAndVarError WRITE setSuppressFieldAndVarError)
145     Q_PROPERTY(ATranslationProperty translation READ fakeTranslationReader)
146 
147     friend class PreviewReportWidget;
148 public:
149     bool printPages(ReportPages pages, QPrinter *printer);
150     void printPages(ReportPages pages, QMap<QString,QPrinter*>printers, bool printToAllPrinters = false);
151     Q_INVOKABLE QStringList aviableReportTranslations();
152     Q_INVOKABLE void setReportTranslation(const QString& languageName);
153 public:
154     explicit ReportEnginePrivate(QObject *parent = 0);
155     virtual ~ReportEnginePrivate();
156 
157     PageDesignIntf*      appendPage(const QString& pageName="");
158     bool deletePage(PageDesignIntf *page);
159     PageDesignIntf*      createPreviewPage();
pageAt(int index)160     PageDesignIntf*      pageAt(int index){return (index<=(m_pages.count()-1)) ? m_pages.at(index):0;}
pageCount()161     int                  pageCount() {return m_pages.count();}
dataManager()162     DataSourceManager*   dataManager(){return m_datasources;}
scriptContext()163     ScriptEngineContext* scriptContext(){return m_scriptEngineContext;}
164     ScriptEngineManager* scriptManager();
dataManagerIntf()165     IDataSourceManager*  dataManagerIntf(){return m_datasources;}
166 
scriptManagerIntf()167     IScriptEngineManager* scriptManagerIntf(){
168         ScriptEngineManager::instance().setDataManager(dataManager());
169         return &ScriptEngineManager::instance();
170     }
171 
172     void    clearReport();
173     bool    printReport(QPrinter* printer=0);
174     bool    printReport(QMap<QString, QPrinter*>printers, bool printToAllPrinters);
175 
176     void    printToFile(const QString& fileName);
177     bool    printToPDF(const QString& fileName);
178     bool    exportReport(QString exporterName, const QString &fileName = "", const QMap<QString, QVariant>& params = QMap<QString, QVariant>());
179     void    previewReport(PreviewHints hints = PreviewBarsUserSetting);
180     void    previewReport(QPrinter* printer, PreviewHints hints = PreviewBarsUserSetting);
181 
182     ReportDesignWindowInterface* getDesignerWindow();
183     void    designReport(bool showModal);
184     void    setSettings(QSettings* value);
setShowProgressDialog(bool value)185     void    setShowProgressDialog(bool value){m_showProgressDialog = value;}
isShowProgressDialog()186     bool    isShowProgressDialog() const {return m_showProgressDialog;}
187     QSettings*  settings();
188     bool    loadFromFile(const QString& fileName, bool autoLoadPreviewOnChange);
189     bool    loadFromByteArray(QByteArray *data, const QString& name = "");
190     bool    loadFromString(const QString& report, const QString& name = "");
reportFileName()191     QString reportFileName(){return m_fileName;}
setReportFileName(const QString & reportFileName)192     void    setReportFileName(const QString& reportFileName){ m_fileName = reportFileName;}
193     bool    saveToFile(const QString& fileName = "");
194     QByteArray  saveToByteArray();
195     QString saveToString();
196     bool    isNeedToSave();
197     QString lastError();
198     ReportEngine * q_ptr;
199     bool emitSaveReport();
200     bool emitSaveReportAs();
201     bool emitLoadReport();
202     void emitSaveFinished();
203     void emitLoadFinished();
204     void emitPrintedToPDF(QString fileName);
205     bool isSaved();
206     void setCurrentReportsDir(const QString& dirName);
currentReportsDir()207     QString currentReportsDir(){ return m_reportsDir;}
setReportName(const QString & reportName)208     void setReportName(const QString& reportName){ m_reportName=reportName;}
reportName()209     QString reportName(){ return m_reportName;}
hasActivePreview()210     bool hasActivePreview(){return m_activePreview;}
211     PageDesignIntf *createPreviewScene(QObject *parent);
212     PreviewReportWidget *createPreviewWidget(QWidget *parent);
213     QIcon previewWindowIcon() const;
214     void setPreviewWindowIcon(const QIcon &previewWindowIcon);
215     QString previewWindowTitle() const;
216     void setPreviewWindowTitle(const QString &previewWindowTitle);
217     QColor previewWindowPageBackground();
218     void setPreviewWindowPageBackground(QColor color);
219 
220     bool suppressFieldAndVarError() const;
221     void setSuppressFieldAndVarError(bool suppressFieldAndVarError);
222     bool isBusy();
223     bool resultIsEditable() const;
224     void setResultEditable(bool value);
225     bool saveToFileIsVisible() const;
226     void setSaveToFileVisible(bool value);
227     bool printToPdfIsVisible() const;
228     void setPrintToPdfVisible(bool value);
229     bool printIsVisible() const;
230     void setPrintVisible(bool value);
231 
232     void setPassPhrase(const QString &passPhrase);
233     bool addTranslationLanguage(QLocale::Language language);
234     bool removeTranslationLanguage(QLocale::Language language);
235     bool setReportLanguage(QLocale::Language language);
236     QList<QLocale::Language> aviableLanguages();
237     ReportTranslation* reportTranslation(QLocale::Language language);
238     void reorderPages(const QList<PageDesignIntf *> &reorderedPages);
239     void clearSelection();
240     Qt::LayoutDirection previewLayoutDirection();
241     void setPreviewLayoutDirection(const Qt::LayoutDirection& previewLayoutDirection);
242     QString styleSheet() const;
243     void setStyleSheet(const QString &styleSheet);
244     QList<QLocale::Language> designerLanguages();
245     QLocale::Language currentDesignerLanguage();
246     void setCurrentDesignerLanguage(QLocale::Language language);
247     ScaleType previewScaleType();
248     int       previewScalePercent();
249     void      setPreviewScaleType(const ScaleType &previewScaleType, int percent = 0);
250     void      addWatermark(const WatermarkSetting& watermarkSetting);
251     void      clearWatermarks();
252     IPreparedPages* preparedPages();
253     bool showPreparedPages(PreviewHints hints);
254     bool showPreparedPages(QPrinter *defaultPrinter, PreviewHints hints);
255     bool prepareReportPages();
256     bool printPreparedPages();
257 signals:
258     void    pagesLoadFinished();
259     void    datasourceCollectionLoadFinished(const QString& collectionName);
260     void    cleared();
261     void    renderStarted();
262     void    renderFinished();
263     void    renderPageFinished(int renderedPageCount);
264     void    onSave(bool& saved);
265     void    onSaveAs(bool& saved);
266     void    onSavePreview(bool& saved, LimeReport::IPreparedPages* pages);
267     void    onLoad(bool& loaded);
268     void    saveFinished();
269     void    loadFinished();
270     void    printedToPDF(QString fileName);
271 
272     void    getAvailableDesignerLanguages(QList<QLocale::Language>* languages);
273     void    currentDefaultDesignerLanguageChanged(QLocale::Language);
274     QLocale::Language  getCurrentDefaultDesignerLanguage();
275     void    externalPaint(const QString& objectName, QPainter* painter, const QStyleOptionGraphicsItem*);
276     void    printingStarted(int pageCount);
277     void    printingFinished();
278     void    pagePrintingFinished(int index);
279 
280 public slots:
281     bool    slotLoadFromFile(const QString& fileName);
282     void    cancelRender();
283     void    cancelPrinting();
284 protected:
285     PageDesignIntf* createPage(const QString& pageName="", bool preview = false);
286     bool showPreviewWindow(ReportPages pages, PreviewHints hints, QPrinter *printer);
287     void internalPrintPages(ReportPages pages, QPrinter &printer);
288 protected slots:
289     void    slotDataSourceCollectionLoaded(const QString& collectionName);
290 private slots:
291     void slotPreviewWindowDestroyed(QObject* window);
292     void slotDesignerWindowDestroyed(QObject* window);
293 private:
294     //ICollectionContainer
295     virtual QObject*    createElement(const QString&,const QString&);
296     virtual int         elementsCount(const QString&);
297     virtual QObject*    elementAt(const QString&, int index);
298     virtual void        collectionLoadFinished(const QString&);
299     void    saveError(QString message);
300     void    showError(QString message);
301     //ICollectionContainer
302     //ITranslationContainer
translations()303     Translations* translations(){ return &m_translations;}
304     void updateTranslations();
305     //ITranslationContainer
306     ReportPages renderToPages();
307     QString renderToString();
308     PageItemDesignIntf *getPageByName(const QString& pageName);
fakeTranslationReader()309     ATranslationProperty fakeTranslationReader(){ return ATranslationProperty();}
310     PageItemDesignIntf *createRenderingPage(PageItemDesignIntf *page);
311     void initReport();
312     void paintByExternalPainter(const QString& objectName, QPainter* painter, const QStyleOptionGraphicsItem* options);
dropChanges()313     void dropChanges(){ m_datasources->dropChanges(); m_scriptEngineContext->dropChanges();}
314     void clearRenderingPages();
315 private:
316     QList<PageDesignIntf*> m_pages;
317     QList<PageItemDesignIntf*> m_renderingPages;
318     ReportPages m_preparedPages;
319     PreparedPages* m_preparedPagesManager;
320     DataSourceManager* m_datasources;
321     ScriptEngineContext* m_scriptEngineContext;
322     ReportRender::Ptr m_reportRender;
323     QString m_fileName;
324     QString m_lastError;
325     QSettings* m_settings;
326     bool m_ownedSettings;
327     QScopedPointer<QPrinter> m_printer;
328     bool m_printerSelected;
329     bool m_showProgressDialog;
330     QString m_reportsDir;
331     QString m_reportName;
332     QMainWindow* m_activePreview;
333     QIcon m_previewWindowIcon;
334     QString m_previewWindowTitle;
335     QPointer<ReportDesignWindowInterface> m_designerWindow;
336     ReportSettings m_reportSettings;
337     bool m_reportRendering;
338     bool m_resultIsEditable;
339     QString m_passPhrase;
340     QFileSystemWatcher  *m_fileWatcher;
341     Translations m_translations;
342     QLocale::Language m_reportLanguage;
343     void activateLanguage(QLocale::Language language);
344     Qt::LayoutDirection m_previewLayoutDirection;
345     LimeReportDesignerPluginInterface* m_designerFactory;
346     QString m_styleSheet;
347     QLocale::Language m_currentDesignerLanguage;
348     QMap<QString, ReportExporterInterface*> exporters;
349     ScaleType m_previewScaleType;
350     int m_previewScalePercent;
351     int m_startTOCPage;
352     QColor m_previewPageBackgroundColor;
353     QVector<WatermarkSetting> m_watermarks;
354     BaseDesignIntf *createWatermark(PageDesignIntf *page, WatermarkSetting watermarkSetting);
355     bool m_saveToFileVisible;
356     bool m_printToPdfVisible;
357     bool m_printVisible;
358     bool m_cancelPrinting;
359 };
360 
361 }
362 #endif // LRREPORTDESIGNINTF_P_H
363