From 94db98d5966045dc0eb084fc361ff09810fa7b66 Mon Sep 17 00:00:00 2001 From: Andy Maloney Date: Wed, 19 Apr 2017 22:43:48 -0400 Subject: [PATCH] Optionally monitor changes to files loaded with ReportEngine::loadFromFile() When using the designer as a stand alone application and the renderer/preview in the user application, the preview window would have to be closed and opened again every time a change was made to the report. This change adds an optional QFileSystemWatcher to monitor changes to the report file and automatically refreshes the preview in the application when it is saved. It may be turned on using the new parameter to ReportEngine::loadFromFile(). If the preview window is open in the application and the file name changes or the file is removed, inform the user and close the preview window. --- include/lrreportengine.h | 2 +- limereport/lrpreviewreportwindow.cpp | 5 ++ limereport/lrpreviewreportwindow.h | 1 + limereport/lrreportdesignwidget.cpp | 2 +- limereport/lrreportengine.cpp | 107 ++++++++++++++++++--------- limereport/lrreportengine.h | 2 +- limereport/lrreportengine_p.h | 7 +- 7 files changed, 89 insertions(+), 37 deletions(-) diff --git a/include/lrreportengine.h b/include/lrreportengine.h index dbbb2a8..99728e1 100644 --- a/include/lrreportengine.h +++ b/include/lrreportengine.h @@ -82,7 +82,7 @@ public: void setShowProgressDialog(bool value); IDataSourceManager* dataManager(); IScriptEngineManager* scriptManager(); - bool loadFromFile(const QString& fileName); + bool loadFromFile(const QString& fileName, bool autoLoadPreviewOnChange = false); bool loadFromByteArray(QByteArray *data); bool loadFromString(const QString& data); QString reportFileName(); diff --git a/limereport/lrpreviewreportwindow.cpp b/limereport/lrpreviewreportwindow.cpp index b000547..47c4ec0 100644 --- a/limereport/lrpreviewreportwindow.cpp +++ b/limereport/lrpreviewreportwindow.cpp @@ -136,6 +136,11 @@ void PreviewReportWindow::initPreview(int pagesCount) m_pagesNavigator->setValue(1); } +void PreviewReportWindow::reloadPreview() +{ + m_previewReportWidget->refreshPages(); +} + void PreviewReportWindow::setSettings(QSettings* value) { if (m_ownedSettings) diff --git a/limereport/lrpreviewreportwindow.h b/limereport/lrpreviewreportwindow.h index 92b2829..78e4c07 100644 --- a/limereport/lrpreviewreportwindow.h +++ b/limereport/lrpreviewreportwindow.h @@ -60,6 +60,7 @@ public: void setPages(ReportPages pages); void exec(); void initPreview(int pagesCount); + void reloadPreview(); void setSettings(QSettings* value); void setErrorMessages(const QStringList& value); void setToolBarVisible(bool value); diff --git a/limereport/lrreportdesignwidget.cpp b/limereport/lrreportdesignwidget.cpp index 4852b97..9e22238 100644 --- a/limereport/lrreportdesignwidget.cpp +++ b/limereport/lrreportdesignwidget.cpp @@ -288,7 +288,7 @@ bool ReportDesignWidget::save() bool ReportDesignWidget::loadFromFile(const QString &fileName) { - if (m_report->loadFromFile(fileName)){ + if (m_report->loadFromFile(fileName,false)){ createTabs(); //connectPage(m_report->pageAt(0)); m_scriptEditor->setPlainText(m_report->scriptContext()->initScript()); diff --git a/limereport/lrreportengine.cpp b/limereport/lrreportengine.cpp index d41c194..dbd1882 100644 --- a/limereport/lrreportengine.cpp +++ b/limereport/lrreportengine.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include "time.h" @@ -59,13 +60,15 @@ ReportEnginePrivate::ReportEnginePrivate(QObject *parent) : m_printer(new QPrinter(QPrinter::HighResolution)), m_printerSelected(false), m_showProgressDialog(true), m_reportName(""), m_activePreview(0), m_previewWindowIcon(":/report/images/logo32"), m_previewWindowTitle(tr("Preview")), - m_reportRendering(false), m_resultIsEditable(true), m_passPhrase("HjccbzHjlbyfCkjy") + m_reportRendering(false), m_resultIsEditable(true), m_passPhrase("HjccbzHjlbyfCkjy"), + m_fileWatcher( new QFileSystemWatcher( this ) ) { m_datasources = new DataSourceManager(this); m_datasources->setReportSettings(&m_reportSettings); m_scriptEngineContext = new ScriptEngineContext(this); m_datasources->setObjectName("datasources"); connect(m_datasources,SIGNAL(loadCollectionFinished(QString)),this,SLOT(slotDataSourceCollectionLoaded(QString))); + connect(m_fileWatcher,SIGNAL(fileChanged(const QString &)),this,SLOT(slotLoadFromFile(const QString &))); } ReportEnginePrivate::~ReportEnginePrivate() @@ -457,6 +460,63 @@ void ReportEnginePrivate::setCurrentReportsDir(const QString &dirName) m_reportsDir = dirName; } +bool ReportEnginePrivate::slotLoadFromFile(const QString &fileName) +{ + PreviewReportWindow *currentPreview = qobject_cast(m_activePreview); + + if (!QFile::exists(fileName)) + { + if ( hasActivePreview() ) + { + QMessageBox::information( NULL, + tr( "Report File Change" ), + tr( "The report file \"%1\" has changed names or been deleted.\n\nThis preview is no longer valid." ).arg( fileName ) + ); + + clearReport(); + + currentPreview->close(); + } + + return false; + } + + clearReport(); + + ItemsReaderIntf::Ptr reader = FileXMLReader::create(fileName); + reader->setPassPhrase(m_passPhrase); + if (reader->first()){ + if (reader->readItem(this)){ + m_fileName=fileName; + QFileInfo fi(fileName); + m_reportName = fi.fileName(); + + QString dbSettingFileName = fi.absolutePath()+"/"+fi.baseName()+".db"; + if (QFile::exists(dbSettingFileName)){ + QSettings dbcredentals(dbSettingFileName, QSettings::IniFormat); + foreach (ConnectionDesc* connection, dataManager()->conections()) { + if (!connection->keepDBCredentials()){ + dbcredentals.beginGroup(connection->name()); + connection->setUserName(dbcredentals.value("user").toString()); + connection->setPassword(dbcredentals.value("password").toString()); + dbcredentals.endGroup(); + } + } + } + + dataManager()->connectAutoConnections(); + + if ( hasActivePreview() ) + { + currentPreview->reloadPreview(); + } + return true; + }; + } + m_lastError = reader->lastError(); + return false; +} + void ReportEnginePrivate::cancelRender() { if (m_reportRender) @@ -509,39 +569,20 @@ QSettings*ReportEnginePrivate::settings() } } -bool ReportEnginePrivate::loadFromFile(const QString &fileName) +bool ReportEnginePrivate::loadFromFile(const QString &fileName, bool autoLoadPreviewOnChange) { - if (!QFile::exists(fileName)) return false; + // only watch one file at a time + if ( !m_fileWatcher->files().isEmpty() ) + { + m_fileWatcher->removePaths( m_fileWatcher->files() ); + } - clearReport(); + if ( autoLoadPreviewOnChange ) + { + m_fileWatcher->addPath( fileName ); + } - ItemsReaderIntf::Ptr reader = FileXMLReader::create(fileName); - reader->setPassPhrase(m_passPhrase); - if (reader->first()){ - if (reader->readItem(this)){ - m_fileName=fileName; - QFileInfo fi(fileName); - m_reportName = fi.fileName(); - - QString dbSettingFileName = fi.absolutePath()+"/"+fi.baseName()+".db"; - if (QFile::exists(dbSettingFileName)){ - QSettings dbcredentals(dbSettingFileName, QSettings::IniFormat); - foreach (ConnectionDesc* connection, dataManager()->conections()) { - if (!connection->keepDBCredentials()){ - dbcredentals.beginGroup(connection->name()); - connection->setUserName(dbcredentals.value("user").toString()); - connection->setPassword(dbcredentals.value("password").toString()); - dbcredentals.endGroup(); - } - } - } - - dataManager()->connectAutoConnections(); - return true; - }; - } - m_lastError = reader->lastError(); - return false; + return slotLoadFromFile( fileName ); } bool ReportEnginePrivate::loadFromByteArray(QByteArray* data, const QString &name){ @@ -880,10 +921,10 @@ IScriptEngineManager* ReportEngine::scriptManager() return d->scriptManagerIntf(); } -bool ReportEngine::loadFromFile(const QString &fileName) +bool ReportEngine::loadFromFile(const QString &fileName, bool autoLoadPreviewOnChange) { Q_D(ReportEngine); - return d->loadFromFile(fileName); + return d->loadFromFile(fileName, autoLoadPreviewOnChange); } bool ReportEngine::loadFromByteArray(QByteArray* data){ diff --git a/limereport/lrreportengine.h b/limereport/lrreportengine.h index dbbb2a8..99728e1 100644 --- a/limereport/lrreportengine.h +++ b/limereport/lrreportengine.h @@ -82,7 +82,7 @@ public: void setShowProgressDialog(bool value); IDataSourceManager* dataManager(); IScriptEngineManager* scriptManager(); - bool loadFromFile(const QString& fileName); + bool loadFromFile(const QString& fileName, bool autoLoadPreviewOnChange = false); bool loadFromByteArray(QByteArray *data); bool loadFromString(const QString& data); QString reportFileName(); diff --git a/limereport/lrreportengine_p.h b/limereport/lrreportengine_p.h index ca080f2..f900217 100644 --- a/limereport/lrreportengine_p.h +++ b/limereport/lrreportengine_p.h @@ -42,6 +42,8 @@ #include "serializators/lrstorageintf.h" #include "lrscriptenginemanager.h" +class QFileSystemWatcher; + namespace LimeReport{ class PageDesignIntf; @@ -91,7 +93,7 @@ public: void setSettings(QSettings* value); void setShowProgressDialog(bool value){m_showProgressDialog = value;} QSettings* settings(); - bool loadFromFile(const QString& fileName); + bool loadFromFile(const QString& fileName, bool autoLoadPreviewOnChange); bool loadFromByteArray(QByteArray *data, const QString& name = ""); bool loadFromString(const QString& report, const QString& name = ""); QString reportFileName(){return m_fileName;} @@ -138,6 +140,7 @@ signals: void onSave(); void saveFinished(); public slots: + bool slotLoadFromFile(const QString& fileName); void cancelRender(); protected: PageDesignIntf* createPage(const QString& pageName=""); @@ -178,6 +181,8 @@ private: bool m_reportRendering; bool m_resultIsEditable; QString m_passPhrase; + + QFileSystemWatcher *m_fileWatcher; }; }