OpenLexocad  28.0
CoreApplication.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Base/Observer.h>
4 #include <Base/String.h>
6 
7 #include <functional> // for function, _Func_class
8 namespace Core { class Command; }
9 
10 
11 typedef std::vector<CA_CommandObserver*> ca_CommandObserver_Vector;
12 typedef std::vector<CA_TransactionObserver*> CA_TransactionObserver_Vector;
13 
14 
15 namespace Core
16 {
17 class CoreApplicationP;
18 class CoreDocument;
19 class DocObject;
20 class Property;
21 class CommandFactory;
22 
23 
27 class LX_CORE_EXPORT AppChanges
28 {
29 public:
30  enum why
31  {
35  } Why;
36 
37  bool operator==(const AppChanges& m) const
38  {
39  return ((this->Why == m.Why) && (this->Doc == m.Doc) && (this->CreateGui == m.CreateGui) &&
40  (this->SetAsActiveDocument == m.SetAsActiveDocument));
41  }
42 
43  Core::CoreDocument* Doc = nullptr;
44  bool CreateGui; // If true a corresponding GuiDocument gets created
45  bool SetAsActiveDocument = true; // If true when NewDocument is notified, the document will become active
46 };
47 
48 
49 class LX_CORE_EXPORT LoadedDll
50 {
51 public:
52  LoadedDll(void){};
53  virtual ~LoadedDll(void){};
54  virtual Base::String getInfo() = 0;
55  virtual void init() = 0;
56  virtual void release() = 0;
57 };
58 
59 class LX_CORE_EXPORT PartAcis : public Core::LoadedDll
60 {
61 public:
62  PartAcis(void){};
63  virtual ~PartAcis(void){};
64  virtual Base::String getInfo() = 0;
65  virtual void init() = 0;
66  virtual void release() = 0;
67  virtual Core::DocObject* create_PartGeometry(Core::CoreDocument* doc, const std::string& type) = 0;
68 };
69 
70 class LX_CORE_EXPORT CoreApplication : public Base::Subject<Core::AppChanges>
71 {
72 public:
74  // //
75  // --------------------- BEGIN API --------------------- //
76  // //
77  // ATTENTION: DO NOT CHANGE ANY SIGNATURES IN THE API ! //
78  // //
80 
81  CoreApplication(int argc, char** argv);
83 
85  static CoreApplication* instance(void);
87  static void destroy();
89  static void reset();
90 
94  void setApplicationName(const Base::String& name);
95 
97  Core::CoreDocument* newCoreDocument(const std::string& typeName, const Base::String& name = Base::String());
100  Core::CoreDocument* newDocument(const std::string& typeName,
101  const Base::String& name = Base::String(),
102  bool createGui = true,
103  bool setAsActiveDocument = true,
104  bool createDefaultObjects = true);
106  Core::CoreDocument* openCoreDocument(const std::string& typeName, const Base::String& path);
108  Core::CoreDocument* openDocument(const std::string& typeName,
109  const Base::String& path = Base::String(),
110  bool createGui = true,
111  bool savepath = true,
112  bool setAsActiveDocument = true);
122  std::vector<Core::CoreDocument*> getDocuments() const;
129 
131  void setModulePreferenceValue(const std::string& moduleName, const std::string& key, const std::string& value);
133  std::string getModulePreferenceValue(const std::string& moduleName, const std::string& key);
134 
136  // //
137  // ---------------------- END API ---------------------- //
138  // //
140 
141 #ifndef LXAPI // INTERFACES BELOW ARE -NOT- PART OF THE LEXOCAD API
142 
149 
153 
154  // Notifications
165 
171 
175 
178 
179  void notifyApp(Core::CoreDocument* doc, AppChanges::why why, bool createGui, bool setAsActiveDocument = true);
180  void notifyCmdObservers(Core::CoreDocument* doc, bool createGui);
181 
183  bool closeDocument(Core::CoreDocument* doc, bool forceClose /*=false*/, bool dontNotify = false);
184 
185  // Look through the opened documents map for the tested document;
186  // this could happen when the document was closed and we just have still the pointer to removed document
187  // which is invalid
188  bool isDocumentValid(Core::CoreDocument* testedDocument) const;
189 
191  bool onClose(bool forceClose, bool dontNotify, bool& hardClose);
192  bool closeApplication(bool forceClose, bool dontNotify, bool& hardClose);
194  int getOpenDocuments(std::vector<Core::CoreDocument*>& vec);
195 
197  void initPython() const;
200  void finalizePython() const;
201 
202  bool runPythonString(const Base::String& str) const;
203  bool runPythonString(const Base::String& str, Base::String& err) const;
204  bool runPythonScript(const Base::String& scr, Base::String& err) const;
205  bool runPythonScript(const Base::String& scr) const;
206 
207  bool isClosing() const;
208  bool hasGui() const;
209  void sethasGui(bool on);
210 
211  static unsigned int getVersionYear();
212  static std::string getBuildDateTime();
213  static std::string getDocumentVersion();
216  static long getRefCount();
218  void ref(void);
220  void unref(void);
221 
223 
224  void setCommandFactory(Core::CommandFactory* commandFactory) { _commandFactory = commandFactory; }
225  Core::CommandFactory* getCommandFactory() const { return _commandFactory; }
226 
227 
228  Core::Command* createCommand(const std::string& commandName) { return _createCommand(commandName); };
229  void setCreateCommandFunction(std::function < Core::Command * (const std::string&)> f) { _createCommand = f; }
230 
231  virtual const char* subject_name(void) { return "CoreApplication"; };
232 
233 
252  int setActiveTransaction(const char* name, bool persist = false);
254  const char* getActiveTransaction(int* tid = nullptr) const;
264  void closeActiveTransaction(bool abort = false, int id = 0);
266 
267 #endif
268 
269 protected:
270  Core::CoreDocument* _newDocument(const std::string& typeName, const Base::String& name, bool createGui);
271 
274 
276  std::map<Base::String, Core::CoreDocument*> _documentMap;
277 
278 private:
279  static void cleanOldTempDirectories();
280  //CoreApplication() {}
281  bool maybeSave(Core::CoreDocument* doc);
282  bool closeDocumentP(Core::CoreDocument* doc, bool forceClose, bool dontNotify);
283 
284  CoreApplicationP* _pimpl = nullptr;
285  static CoreApplication* _instance;
286  Base::String _applicationName;
287  std::set<CA_CommandObserver*> _commandObserverRegistry;
288  std::set<CA_TransactionObserver*> _transactionObserverRegistry;
289  static long _refcnt;
290  Core::PartAcis* _partTool = nullptr;
291  Core::CommandFactory* _commandFactory = nullptr;
292  std::function<Core::Command*(const std::string&)> _createCommand;
293 
294  friend class AutoTransaction;
295 
296  std::string _activeTransactionName = "";
297  int _activeTransactionID = 0;
298  int _activeTransactionGuard = 0;
299  bool _activeTransactionTmpName = false;
300 
301 };
302 
303 } // namespace Core
CA_TransactionObserver
Definition: CA_CommandObserver.h:56
Core::CoreApplication::getTmpDirectory
Base::String getTmpDirectory(Core::CoreDocument *doc)
Gets the TempDir of the specified document. If doc = 0 takes the active document.
Core::CoreApplication::runPythonScript
bool runPythonScript(const Base::String &scr, Base::String &err) const
Core::CoreApplication::openDocument
Core::CoreDocument * openDocument(const std::string &typeName, const Base::String &path=Base::String(), bool createGui=true, bool savepath=true, bool setAsActiveDocument=true)
Opens a document. If no path is provided and the application has a Gui the user is prompted with a fi...
Core::CoreApplication::notifyDocumentOpened
void notifyDocumentOpened(Core::CoreDocument *doc)
Core::CoreApplication::initPython
void initPython() const
Initialize the Python interpreter (if not already initialized). Call it before using any other Python...
Core::AppChanges::operator==
bool operator==(const AppChanges &m) const
Definition: CoreApplication.h:37
Core::CoreApplication::closeDocument
bool closeDocument(Core::CoreDocument *doc)
Closes the document. Returns 'true' if successful, 'false' if canceled.
Core::CoreDocument
Definition: CoreDocument.h:269
Core::CoreApplication::notifyDocumentClosed
void notifyDocumentClosed(Core::CoreDocument *doc)
Core::CoreApplication::setCreateCommandFunction
void setCreateCommandFunction(std::function< Core::Command *(const std::string &)> f)
Definition: CoreApplication.h:229
Core::CoreApplication::hasGui
bool hasGui() const
Core::CoreApplication::notifyPropertyCreate
void notifyPropertyCreate(Core::CoreDocument *doc, Core::DocObject *obj, Core::Property *pro)
Core::CoreApplication::notifyDocumentSaved
void notifyDocumentSaved(Core::CoreDocument *doc)
Core::CoreApplication::unref
void unref(void)
Decrements the reference count by one.
Core::CoreApplication::notifyApp
void notifyApp(Core::CoreDocument *doc, AppChanges::why why, bool createGui, bool setAsActiveDocument=true)
Core::CoreApplication::notifyDocumentSetActive
void notifyDocumentSetActive(Core::CoreDocument *doc)
Core::CoreApplication::getBuildDateTime
static std::string getBuildDateTime()
Core::CoreApplication::getOpenDocuments
int getOpenDocuments(std::vector< Core::CoreDocument * > &vec)
Core::PartAcis::~PartAcis
virtual ~PartAcis(void)
Definition: CoreApplication.h:63
entt::observer
basic_observer< entity > observer
Alias declaration for the most common use case.
Definition: entt.hpp:3793
Core::CoreApplication::ref
void ref(void)
Increments the reference count by one.
Core::CoreApplication::closeApplication
bool closeApplication(bool forceClose, bool dontNotify, bool &hardClose)
Observer.h
Core::CoreApplication::_documentMap
std::map< Base::String, Core::CoreDocument * > _documentMap
Map of all documents.
Definition: CoreApplication.h:276
Core::PartAcis
Definition: CoreApplication.h:60
Core::PartAcis::create_PartGeometry
virtual Core::DocObject * create_PartGeometry(Core::CoreDocument *doc, const std::string &type)=0
Core::CoreApplication::isDocumentValid
bool isDocumentValid(Core::CoreDocument *testedDocument) const
CA_CommandObserver.h
Core::CoreApplication::destroy
static void destroy()
Deletes the Singleton.
Core::LoadedDll::~LoadedDll
virtual ~LoadedDll(void)
Definition: CoreApplication.h:53
Core::CoreApplication::notifyDocumentRenamed
void notifyDocumentRenamed(Core::CoreDocument *doc)
Core::AppChanges::Why
enum Core::AppChanges::why Why
Core::CoreApplication::setCommandFactory
void setCommandFactory(Core::CommandFactory *commandFactory)
Definition: CoreApplication.h:224
Core::CoreApplication::notifyObject
void notifyObject(Core::CoreDocument *doc, Core::DocObject *obj, Core::Property *pro, CA_Transaction::why transaction)
Core::CoreApplication::getActiveDocument
Core::CoreDocument * getActiveDocument() const
Returns the active document. Returns Null if there is no active document.
Core::CoreApplication::registerCommandObserver
bool registerCommandObserver(CA_CommandObserver *observer)
Register an Observer for Commands.
Core::CoreApplication::getDocumentVersionAsInteger
static int getDocumentVersionAsInteger()
Core::CoreApplication::getUniqueDocumentName
Base::String getUniqueDocumentName(const Base::String &s) const
Core::CoreApplication::getApplicationName
Base::String getApplicationName() const
Returns the name of the application.
Core::AppChanges::why
why
Definition: CoreApplication.h:31
Core::CoreApplication::notifyObjectHasError
void notifyObjectHasError(Core::CoreDocument *doc, Core::DocObject *obj)
Core::CoreApplication::getRefCount
static long getRefCount()
Returns the reference count of the Application.
Core::CoreApplication::getVersionYear
static unsigned int getVersionYear()
Core::CoreApplication::closeActiveTransaction
void closeActiveTransaction(bool abort=false, int id=0)
Core::CoreApplication::runPythonString
bool runPythonString(const Base::String &str) const
Core::AppChanges::Doc
Core::CoreDocument * Doc
Definition: CoreApplication.h:43
Core::DocObject
Definition: DocObject.h:54
Core::AppChanges
Definition: CoreApplication.h:28
Core::CoreApplication::getActiveTransaction
const char * getActiveTransaction(int *tid=nullptr) const
Return the current active transaction name and ID.
Core::CoreApplication::setApplicationName
void setApplicationName(const Base::String &name)
Sets the application name.
Core::CoreApplication::getTransactionObservers
CA_TransactionObserver_Vector getTransactionObservers()
Core::CoreApplication::~CoreApplication
~CoreApplication()
Core::PartAcis::getInfo
virtual Base::String getInfo()=0
Core::CoreApplication::unregisterCommandObserver
bool unregisterCommandObserver(CA_CommandObserver *observer)
Unregister an Observer for Commands.
Core::CommandFactory
Definition: Command.h:79
Core
Definition: Base.h:5
Core::CoreApplication::notifyDocumentChanged
void notifyDocumentChanged(Core::CoreDocument *doc, Core::Property *pro)
Core::CoreApplication::CoreApplication
CoreApplication(int argc, char **argv)
Core::CoreApplication::runPythonString
bool runPythonString(const Base::String &str, Base::String &err) const
Core::LoadedDll::LoadedDll
LoadedDll(void)
Definition: CoreApplication.h:52
CA_Transaction::why
why
Definition: CA_CommandObserver.h:22
Core::AppChanges::SetAsActiveDocument
bool SetAsActiveDocument
Definition: CoreApplication.h:45
Core::CoreApplication::notifyDocumentDeleted
void notifyDocumentDeleted(Core::CoreDocument *doc)
Core::LoadedDll::getInfo
virtual Base::String getInfo()=0
Core::CoreApplication::instance
static CoreApplication * instance(void)
Returns the Singleton.
Core::CoreApplication::notifyPropertyChanged
void notifyPropertyChanged(Core::CoreDocument *doc, Core::DocObject *obj, Core::Property *pro)
Core::CoreApplication::subject_name
virtual const char * subject_name(void)
Definition: CoreApplication.h:231
Core::CoreApplication::deleteDocument
void deleteDocument(Core::CoreDocument *doc)
Physically deletes a document. The maps are not effected.
Core::LoadedDll
Definition: CoreApplication.h:50
Core::CoreApplication
Definition: CoreApplication.h:71
Core::CoreApplication::notifyPropertyDeleted
void notifyPropertyDeleted(Core::CoreDocument *doc, Core::DocObject *obj, Core::Property *pro)
Core::CoreApplication::getCommandObserver
ca_CommandObserver_Vector getCommandObserver()
Returns a List of CA_CommandObserver.
Core::CoreApplication::setModulePreferenceValue
void setModulePreferenceValue(const std::string &moduleName, const std::string &key, const std::string &value)
Sets a persistent preference key and value for a module. On Windows the key and value are stored in t...
Core::CoreApplication::getDocumentVersion
static std::string getDocumentVersion()
CA_CommandObserver
Definition: CA_CommandObserver.h:68
Core::CoreApplication::loadDllByName
LoadedDll * loadDllByName(const Base::String &name)
Core::Command
Definition: Command.h:38
ca_CommandObserver_Vector
std::vector< CA_CommandObserver * > ca_CommandObserver_Vector
Definition: CoreApplication.h:11
Core::CoreApplication::finalizePython
void finalizePython() const
Core::LoadedDll::release
virtual void release()=0
Core::CoreApplication::notifyRecomputeFailed
void notifyRecomputeFailed(Core::CoreDocument *doc)
Core::CoreApplication::notifyCmdObservers
void notifyCmdObservers(Core::CoreDocument *doc, bool createGui)
String.h
Core::CoreApplication::setActiveTransaction
int setActiveTransaction(const char *name, bool persist=false)
Core::CoreApplication::notifyObjectDeleted
void notifyObjectDeleted(Core::CoreDocument *doc, Core::DocObject *obj)
Core::CoreApplication::notifyDocumentCreated
void notifyDocumentCreated(Core::CoreDocument *doc)
Core::CoreApplication::closeDocument
bool closeDocument(Core::CoreDocument *doc, bool forceClose, bool dontNotify=false)
Closes a document.
Core::CoreApplication::getCommandFactory
Core::CommandFactory * getCommandFactory() const
Definition: CoreApplication.h:225
Core::CoreApplication::createCommand
Core::Command * createCommand(const std::string &commandName)
Definition: CoreApplication.h:228
Core::CoreApplication::isClosing
bool isClosing() const
Core::CoreApplication::getDocumentByName
Core::CoreDocument * getDocumentByName(const Base::String &name)
Returns the document by name.
Core::CoreApplication::_newDocument
Core::CoreDocument * _newDocument(const std::string &typeName, const Base::String &name, bool createGui)
Core::CoreApplication::closeApplication
void closeApplication()
Closes the application.
CA_TransactionObserver_Vector
std::vector< CA_TransactionObserver * > CA_TransactionObserver_Vector
Definition: CoreApplication.h:12
Core::Property
Definition: Property.h:72
Core::PartAcis::init
virtual void init()=0
Core::CoreApplication::newCoreDocument
Core::CoreDocument * newCoreDocument(const std::string &typeName, const Base::String &name=Base::String())
Creates a new document without Gui. The new document becomes NOT the active document....
Core::PartAcis::PartAcis
PartAcis(void)
Definition: CoreApplication.h:62
Core::AutoTransaction
Helper class to manager transaction (i.e. undo/redo)
Definition: AutoTransaction.h:50
Base::Subject
Definition: Observer.h:9
Core::CoreApplication::notifyRecomputeSuccess
void notifyRecomputeSuccess(Core::CoreDocument *doc)
Core::CoreApplication::sethasGui
void sethasGui(bool on)
Core::AppChanges::NewDocument
@ NewDocument
Definition: CoreApplication.h:32
Core::CoreApplication::onClose
bool onClose(bool forceClose, bool dontNotify, bool &hardClose)
Gets the version number of the application.
Base::String
A Utf-16 (windows) or ucs4 (unix) encoded string class.
Definition: String.h:18
Core::CoreApplication::notifyDocumentFinishedRead
void notifyDocumentFinishedRead(Core::CoreDocument *doc)
Core::CoreApplication::reset
static void reset()
Resets the Singleton.
Core::CoreApplication::getDocuments
std::vector< Core::CoreDocument * > getDocuments() const
get a list of all documents in the application
CA_TransactionObserver_Vector
std::vector< CA_TransactionObserver * > CA_TransactionObserver_Vector
Definition: CA_CommandObserver.h:102
Core::CoreApplication::notifyObjectAdded
void notifyObjectAdded(Core::CoreDocument *doc, Core::DocObject *obj)
Core::PartAcis::release
virtual void release()=0
SetActiveDocument
SetActiveDocument
Definition: Globals.h:43
Core::CoreApplication::runPythonScript
bool runPythonScript(const Base::String &scr) const
Core::CoreApplication::openCoreDocument
Core::CoreDocument * openCoreDocument(const std::string &typeName, const Base::String &path)
Opens a document without Gui. The document path is not saved. The new document becomes NOT the active...
Core::CoreApplication::getApplicationPath
Base::String getApplicationPath() const
Get the Application-Path.
Core::CoreApplication::notifyObjectCreated
void notifyObjectCreated(Core::CoreDocument *doc, Core::DocObject *obj)
Core::CoreApplication::remove_TransactionObserver
bool remove_TransactionObserver(CA_TransactionObserver *observer)
Core::CoreApplication::newDocument
Core::CoreDocument * newDocument(const std::string &typeName, const Base::String &name=Base::String(), bool createGui=true, bool setAsActiveDocument=true, bool createDefaultObjects=true)
Core::CoreApplication::setActiveDocument
bool setActiveDocument(Core::CoreDocument *doc)
Sets the active document.
ca_CommandObserver_Vector
std::vector< CA_CommandObserver * > ca_CommandObserver_Vector
Definition: CA_CommandObserver.h:101
Core::AppChanges::CreateGui
bool CreateGui
Definition: CoreApplication.h:44
Core::AppChanges::CloseDocument
@ CloseDocument
Definition: CoreApplication.h:33
Core::CoreApplication::add_TransactionObserver
bool add_TransactionObserver(CA_TransactionObserver *observer)
Core::CoreApplication::notifyObjectChanged
void notifyObjectChanged(Core::CoreDocument *doc, Core::DocObject *obj, Core::Property *pro)
Core::CoreApplication::getModulePreferenceValue
std::string getModulePreferenceValue(const std::string &moduleName, const std::string &key)
Returns the persistent value of a preference key for a module. Returns empty string if key or module ...
Core::LoadedDll::init
virtual void init()=0