1 /***************************************************************************
2                           robjectlist  -  description
3                              -------------------
4     begin                : Wed Aug 18 2004
5     copyright            : (C) 2004-2019 by Thomas Friedrichsmeier
6     email                : thomas.friedrichsmeier@kdemail.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 #ifndef ROBJECTLIST_H
18 #define ROBJECTLIST_H
19 
20 #include <qobject.h>
21 
22 #include <qstring.h>
23 #include <qmap.h>
24 
25 #include <QUrl>
26 
27 #include "rcontainerobject.h"
28 
29 class QTimer;
30 class RCommand;
31 class RCommandChain;
32 class RKEditor;
33 class REnvironmentObject;
34 class RKProgressControl;
35 class RKOrphanNamespacesObject;
36 
37 /**
38 This class is responsible for keeping and updating a list of objects in the R-workspace.
39 Actually it kind of represents the R-workspace, including methods to save and load the workspace.
40 It acts as the "document".
41 
42 @author Thomas Friedrichsmeier
43 */
44 class RObjectList : public QObject, public RContainerObject {
45 	Q_OBJECT
46 public:
47 	RObjectList ();
48 	~RObjectList ();
49 
50 	void updateFromR (RCommandChain *chain) override;
51 	/** like updateFromR, but only adjusts to new / missing environments, but does not update the .GlobalEnv. Designed to be used from the backend, when packages were loaded/unloaded . */
52 	void updateFromR (RCommandChain *chain, const QStringList &current_searchpath, const QStringList &current_namespaces);
53 
getFullName(int)54 	QString getFullName (int) const override { return QString (); };
makeChildName(const QString & short_child_name,bool,int)55 	QString makeChildName (const QString &short_child_name, bool, int) const override { return short_child_name; };
56 	/** reimplemented from RContainerObject: do nothing. The object-list has no meta data. */
writeMetaData(RCommandChain *)57 	void writeMetaData (RCommandChain *) override {};
58 
59 	REnvironmentObject* findPackage (const QString &namespacename) const;
60 
getObjectList()61 	static RObjectList *getObjectList () { return object_list; };
getGlobalEnv()62 	static REnvironmentObject *getGlobalEnv () { return object_list->globalenv; };
63 
64 	/** detach the given list of packages (if the packages are loaded, and safe to remove)
65 	@returns a list of error messages (usually empty) */
66 	QStringList detachPackages (const QStringList &packages, RCommandChain *chain = 0, RKProgressControl *control = 0);
67 	/** A pseudo object containing as children all loaded namespaces which do not belong to a package on the search path */
orphanNamespacesObject()68 	RKOrphanNamespacesObject* orphanNamespacesObject () const { return orphan_namespaces; };
69 	QString getObjectDescription () const override;
70 public slots:
71 	void timeout ();
72 signals:
73 /// emitted when the list of objects is about to be updated	// TODO: remove me
74 	void updateStarted ();
75 /// emitted when the list of objects has been updated	// TODO: remove me
76 	void updateComplete ();
77 protected:
78 /** reimplemented from RContainerObject to search the environments in search order */
79 	RObject::ObjectList findObjects (const QStringList &path, bool partial, const QString &op) override;
80 
81 /// reimplemented from RContainerObject to call "remove (objectname)" instead of "objectname <- NULL"
82 	QString removeChildCommand (RObject *object) const override;
83 /// reimplemented from RContainerObject to call "remove (objectname)" instead of "objectname <- NULL"
84 	QString renameChildCommand (RObject *object, const QString &new_name) const override;
85 /// reimplemented from RContainerObject to emit a change signal
86 	void objectsChanged ();
87 	bool updateStructure (RData *new_data) override;
88 	void rCommandDone (RCommand *command) override;
89 	void updateEnvironments (const QStringList &env_names, bool force_globalenv_update);
90 	void updateNamespaces (const QStringList namespace_names);
91 private:
92 	friend class RKLoadAgent;
93 	friend class RKSaveAgent;
94 	QTimer *update_timer;
95 
96 	RCommandChain *update_chain;
97 	RKOrphanNamespacesObject *orphan_namespaces;
98 
99 	REnvironmentObject *createTopLevelEnvironment (const QString &name);
100 
101 	REnvironmentObject *globalenv;
102 	static RObjectList *object_list;
103 };
104 
105 /**
106 \page RepresentationOfRObjectsInRKWard Representation of R objects in RKWard
107 \brief How objects in R space are represented in RKWard
108 
109 Due to primarily two reasons, RKWard needs to keep it's own list of objects in the R workspace. The first, and most important reason is threading: R objects might be modified or even removed in the R backend, while the GUI thread is trying to access them. Since we have no control over what's going on inside R, this cannot be solved with a simple mutex. So rather, we copy a representation into memory accessed by the GUI thread only (in the future, maybe the backend thread will get access to this representation for more efficient updating, but still a representation separate from that kept in R itself is needed).
110 
111 The second reason is that R and Qt includes clash, and we cannot easily use R SEXPs directly in Qt code.
112 
113 RKWard then uses an own specialized description of R objects. This is slightly more abstracted than objects in R, but stores the most important information about each object, and of course the hierarchical organization of objects.
114 
115 TODO: write me!
116 
117 @see RObject
118 @see RKVariable
119 @see RContainerObject
120 @see RObjectList
121 @see RKModificationTracker
122 
123  */
124 
125 #endif
126