Skip to content

Commit

Permalink
Gui: Application/Document/MainWindow changes following App namespace
Browse files Browse the repository at this point in the history
Application:

* signalNewDocument, check the extra argument, isMainDoc, the decide
  whether to create view of the new document. This is so that external
  linked document can be opened in background without crowding the tab
  list.

* slotDeleteDocument, calls Document::beforeDelete()

* slotActiveDocument, creates view if none, because external document
  is now opened without view.

* onLastWindowClosed(), switch to next active document, and creates view
  if none.

* send(Has)MsgToFocusView(), new API to send message to the active view
  in focus. This is to solve the ambiguity of things like pressing
  delete key, copy, paste handling when the active new is not in focus.
  For example, when spread sheet view is active, delete/copy/paste
  handling should be different when the focus on the spread sheet view
  or focus on tree view.

* tryClose(), delegate to MainWindow for close confirmation

* reopen(), new API to reload a partial document in full

Document/DocumentP:

* _CoinMap, new internal map for quick access view provider from its
  root node.

* slotNewObject, modified to support view provider override from
  App::FeaturePython, through new API
  DocumentObject::getViewProviderNameOverride().

* slotDeletedObject/slotTransactionRemove, improve handling of geo group
  children rebuild

* slotSkipRecompute, add special handling of document with skip
  recompute. Some command cannot work when skip recompute is active.
  For example, sketcher command will check if recompute is successful
  on many commands, and will undo if not. New 'PartialCompute' flag is
  added to allow recompute only the editing object and all its
  dependencies if 'SkipRecompute' is active.

* slotTouchedObject, new signal handling of manually touched object.

* setModified(), do nothing if already modified. This is a critical
  performance improvement, because marking tab window as modified turns
  out to be a relatively expensive operation, and will cause massive
  slow down if calling it on every property change.

* getViewProviderByPathFromHead/getViewProvidersByPath(), new APIs to
  obtain view provider(s) from coin SoPath.

* save/saveAll/saveCopy, modified to support external document saving.

* Save/RestoreDocFile(), save and restore tree item recursive expansion
  status.

* slotFinishRestoreObject(), handle new signal
  signalFinishRestoreObject(), unifies postprocessing in restore and
  import operation.

* createView/setActiveView(), add support of delayed view creations

* canClose(), delegate to MainWindows to ask for confirmation

* undo/redo(), support grouped transaction undo/redo. Transactions may
  be grouped by the same transaction ID if they are triggered by a
  single operation but involves objects from multiple documents.

* toggleInSceneGraph(), new API to add or remove root node from or to
  scenegraph without deleting the view object.

MainWindow:

* Update command status using a single shot timer instead of periodical
  one.

* Improve message display is status bar. Give error and warning message
  higher priority (using QStatusBar::showMessage()) than normal status
  message (using actionLabel), reversed from original implementation.

* confirmSave(), new API to check for modification, and ask user to save
  the document before closing. The confirmation dialog allows user to
  apply the answer to all document for convenience.

* saveAll(), new API to save all document with correct ordering in case
  of external linking.

* createMimeDataFromSelection/insertFromMimeData(), support copy paste
  object with external linking. A new dialog DlgObjectSelection is used
  to let user select exactly which object to export.

CommandDoc/CommandWindow:

* Related changes to object delete, document import, export, and save.
  • Loading branch information
realthunder authored and wwmayer committed Aug 17, 2019
1 parent 00bcef0 commit bd2f519
Show file tree
Hide file tree
Showing 27 changed files with 2,557 additions and 563 deletions.
173 changes: 140 additions & 33 deletions src/Gui/Application.cpp
Expand Up @@ -288,11 +288,12 @@ Application::Application(bool GUIenabled)
{
//App::GetApplication().Attach(this);
if (GUIenabled) {
App::GetApplication().signalNewDocument.connect(boost::bind(&Gui::Application::slotNewDocument, this, _1));
App::GetApplication().signalNewDocument.connect(boost::bind(&Gui::Application::slotNewDocument, this, _1, _2));
App::GetApplication().signalDeleteDocument.connect(boost::bind(&Gui::Application::slotDeleteDocument, this, _1));
App::GetApplication().signalRenameDocument.connect(boost::bind(&Gui::Application::slotRenameDocument, this, _1));
App::GetApplication().signalActiveDocument.connect(boost::bind(&Gui::Application::slotActiveDocument, this, _1));
App::GetApplication().signalRelabelDocument.connect(boost::bind(&Gui::Application::slotRelabelDocument, this, _1));
App::GetApplication().signalShowHidden.connect(boost::bind(&Gui::Application::slotShowHidden, this, _1));


// install the last active language
Expand Down Expand Up @@ -669,7 +670,7 @@ void Application::createStandardOperations()
Gui::CreateTestCommands();
}

void Application::slotNewDocument(const App::Document& Doc)
void Application::slotNewDocument(const App::Document& Doc, bool isMainDoc)
{
#ifdef FC_DEBUG
std::map<const App::Document*, Gui::Document*>::const_iterator it = d->documents.find(&Doc);
Expand All @@ -687,12 +688,13 @@ void Application::slotNewDocument(const App::Document& Doc)
pDoc->signalInEdit.connect(boost::bind(&Gui::Application::slotInEdit, this, _1));
pDoc->signalResetEdit.connect(boost::bind(&Gui::Application::slotResetEdit, this, _1));

signalNewDocument(*pDoc);
pDoc->createView(View3DInventor::getClassTypeId());
signalNewDocument(*pDoc, isMainDoc);
if(isMainDoc)
pDoc->createView(View3DInventor::getClassTypeId());
// FIXME: Do we really need this further? Calling processEvents() mixes up order of execution in an
// unpredicatable way. At least it seems that with Qt5 we don't need this any more.
#if QT_VERSION < 0x050000
qApp->processEvents(); // make sure to show the window stuff on the right place
// qApp->processEvents(); // make sure to show the window stuff on the right place
#endif
}

Expand All @@ -704,11 +706,15 @@ void Application::slotDeleteDocument(const App::Document& Doc)
return;
}

// We must clear the selection here to notify all observers
Gui::Selection().clearSelection(doc->second->getDocument()->getName());
// We must clear the selection here to notify all observers.
// And because of possible cross document link, better clear all selection
// to be safe
Gui::Selection().clearCompleteSelection();
doc->second->signalDeleteDocument(*doc->second);
signalDeleteDocument(*doc->second);

doc->second->beforeDelete();

// If the active document gets destructed we must set it to 0. If there are further existing documents then the
// view that becomes active sets the active document again. So, we needn't worry about this.
if (d->activeDocument == doc->second)
Expand Down Expand Up @@ -740,6 +746,16 @@ void Application::slotRenameDocument(const App::Document& Doc)
signalRenameDocument(*doc->second);
}

void Application::slotShowHidden(const App::Document& Doc)
{
std::map<const App::Document*, Gui::Document*>::iterator doc = d->documents.find(&Doc);
#ifdef FC_DEBUG
assert(doc!=d->documents.end());
#endif

signalShowHidden(*doc->second);
}

void Application::slotActiveDocument(const App::Document& Doc)
{
std::map<const App::Document*, Gui::Document*>::iterator doc = d->documents.find(&Doc);
Expand All @@ -753,13 +769,20 @@ void Application::slotActiveDocument(const App::Document& Doc)
Base::PyGILStateLocker lock;
Py::Object active(d->activeDocument->getPyObject(), true);
Py::Module("FreeCADGui").setAttr(std::string("ActiveDocument"),active);

auto view = getMainWindow()->activeWindow();
if(!view || view->getAppDocument()!=&Doc) {
Gui::MDIView* view = d->activeDocument->getActiveView();
getMainWindow()->setActiveWindow(view);
}
}
else {
Base::PyGILStateLocker lock;
Py::Module("FreeCADGui").setAttr(std::string("ActiveDocument"),Py::None());
}
}
signalActiveDocument(*doc->second);
getMainWindow()->updateActions();
}
}

Expand All @@ -776,6 +799,7 @@ void Application::slotDeletedObject(const ViewProvider& vp)
void Application::slotChangedObject(const ViewProvider& vp, const App::Property& prop)
{
this->signalChangedObject(vp,prop);
getMainWindow()->updateActions(true);
}

void Application::slotRelabelObject(const ViewProvider& vp)
Expand All @@ -786,6 +810,7 @@ void Application::slotRelabelObject(const ViewProvider& vp)
void Application::slotActivatedObject(const ViewProvider& vp)
{
this->signalActivatedObject(vp);
getMainWindow()->updateActions();
}

void Application::slotInEdit(const Gui::ViewProviderDocumentObject& vp)
Expand All @@ -804,6 +829,19 @@ void Application::onLastWindowClosed(Gui::Document* pcDoc)
try {
// Call the closing mechanism from Python. This also checks whether pcDoc is the last open document.
Command::doCommand(Command::Doc, "App.closeDocument(\"%s\")", pcDoc->getDocument()->getName());
if (!d->activeDocument && d->documents.size()) {
for(auto &v : d->documents) {
Gui::MDIView* view = v.second->getActiveView();
if(view) {
setActiveDocument(v.second);
getMainWindow()->setActiveWindow(view);
return;
}
}
auto gdoc = d->documents.begin()->second;
setActiveDocument(gdoc);
activateView(View3DInventor::getClassTypeId(),true);
}
}
catch (const Base::Exception& e) {
e.ReportException();
Expand All @@ -828,6 +866,31 @@ bool Application::sendHasMsgToActiveView(const char* pMsg)
return pView ? pView->onHasMsg(pMsg) : false;
}

/// send Messages to the active view
bool Application::sendMsgToFocusView(const char* pMsg, const char** ppReturn)
{
MDIView* pView = getMainWindow()->activeWindow();
if(!pView)
return false;
for(auto focus=qApp->focusWidget();focus;focus=focus->parentWidget()) {
if(focus == pView)
return pView->onMsg(pMsg,ppReturn);
}
return false;
}

bool Application::sendHasMsgToFocusView(const char* pMsg)
{
MDIView* pView = getMainWindow()->activeWindow();
if(!pView)
return false;
for(auto focus=qApp->focusWidget();focus;focus=focus->parentWidget()) {
if(focus == pView)
return pView->onHasMsg(pMsg);
}
return false;
}

Gui::MDIView* Application::activeView(void) const
{
if (activeDocument())
Expand Down Expand Up @@ -1040,24 +1103,9 @@ void Application::updateActive(void)

void Application::tryClose(QCloseEvent * e)
{
if (d->documents.size() == 0) {
e->accept();
}
else {
// ask all documents if closable
std::map<const App::Document*, Gui::Document*>::iterator It;
for (It = d->documents.begin();It!=d->documents.end();++It) {
// a document may have several views attached, so ask it directly
#if 0
MDIView* active = It->second->getActiveView();
e->setAccepted(active->canClose());
#else
e->setAccepted(It->second->canClose());
#endif
if (!e->isAccepted())
return;
}
}
e->setAccepted(getMainWindow()->closeAllDocuments(false));
if(!e->isAccepted())
return;

// ask all passive views if closable
for (std::list<Gui::BaseView*>::iterator It = d->passive.begin();It!=d->passive.end();++It) {
Expand All @@ -1079,14 +1127,7 @@ void Application::tryClose(QCloseEvent * e)
itp = d->passive.begin();
}

// remove all documents
size_t cnt = d->documents.size();
while (d->documents.size() > 0 && cnt > 0) {
// destroys also the Gui document
It = d->documents.begin();
App::GetApplication().closeDocument(It->second->getDocument()->getName());
--cnt; // avoid infinite loop
}
App::GetApplication().closeAllDocuments();
}
}

Expand Down Expand Up @@ -1680,6 +1721,14 @@ void Application::runApplication(void)

// A new QApplication
Base::Console().Log("Init: Creating Gui::Application and QApplication\n");

#if defined(QTWEBENGINE) && defined(Q_OS_LINUX)
// Avoid warning of 'Qt WebEngine seems to be initialized from a plugin...'
// QTWEBENGINE is defined in src/Gui/CMakeLists.txt, currently only enabled
// when build with Conda.
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
#endif

// if application not yet created by the splasher
int argc = App::Application::GetARGC();
GUISingleApplication mainApp(argc, App::Application::GetARGV());
Expand Down Expand Up @@ -2144,3 +2193,61 @@ void Application::checkForPreviousCrashes()
dlg.exec();
}
}

App::Document *Application::reopen(App::Document *doc) {
if(!doc) return 0;
std::string name = doc->FileName.getValue();
std::set<const Gui::Document*> untouchedDocs;
for(auto &v : d->documents) {
if(!v.second->isModified() && !v.second->getDocument()->isTouched())
untouchedDocs.insert(v.second);
}

WaitCursor wc;
wc.setIgnoreEvents(WaitCursor::NoEvents);

if(doc->testStatus(App::Document::PartialDoc)
|| doc->testStatus(App::Document::PartialRestore))
{
App::GetApplication().openDocument(name.c_str());
} else {
std::vector<std::string> docs;
for(auto d : doc->getDependentDocuments(true)) {
if(d->testStatus(App::Document::PartialDoc)
|| d->testStatus(App::Document::PartialRestore) )
docs.push_back(d->FileName.getValue());
}
for(auto &file : docs)
App::GetApplication().openDocument(file.c_str(),false);
}

doc = 0;
for(auto &v : d->documents) {
if(name == v.first->FileName.getValue())
doc = const_cast<App::Document*>(v.first);
if(untouchedDocs.count(v.second)) {
if(!v.second->isModified()) continue;
bool reset = true;
for(auto obj : v.second->getDocument()->getObjects()) {
if(!obj->isTouched())
continue;
std::vector<App::Property*> props;
obj->getPropertyList(props);
for(auto prop : props){
auto link = dynamic_cast<App::PropertyLinkBase*>(prop);
if(link && link->checkRestore()) {
reset = false;
break;
}
}
if(!reset)
break;
}
if(reset) {
v.second->getDocument()->purgeTouched();
v.second->setModified(false);
}
}
}
return doc;
}
18 changes: 16 additions & 2 deletions src/Gui/Application.h
Expand Up @@ -33,6 +33,7 @@
#include <App/Application.h>

class QCloseEvent;
class SoNode;

namespace Gui{
class BaseView;
Expand Down Expand Up @@ -65,6 +66,8 @@ class GuiExport Application
void importFrom(const char* FileName, const char* DocName, const char* Module);
/// Export objects from the document DocName to a single file
void exportTo(const char* FileName, const char* DocName, const char* Module);
/// Reload a partial opened document
App::Document *reopen(App::Document *doc);
//@}


Expand All @@ -74,6 +77,10 @@ class GuiExport Application
bool sendMsgToActiveView(const char* pMsg, const char** ppReturn=0);
/// send Messages test to the active view
bool sendHasMsgToActiveView(const char* pMsg);
/// send Messages to the focused view
bool sendMsgToFocusView(const char* pMsg, const char** ppReturn=0);
/// send Messages test to the focused view
bool sendHasMsgToFocusView(const char* pMsg);
/// Attach a view (get called by the FCView constructor)
void attachView(Gui::BaseView* pcView);
/// Detach a view (get called by the FCView destructor)
Expand All @@ -89,7 +96,7 @@ class GuiExport Application
/** @name Signals of the Application */
//@{
/// signal on new Document
boost::signals2::signal<void (const Gui::Document&)> signalNewDocument;
boost::signals2::signal<void (const Gui::Document&, bool)> signalNewDocument;
/// signal on deleted Document
boost::signals2::signal<void (const Gui::Document&)> signalDeleteDocument;
/// signal on relabeling Document
Expand All @@ -114,6 +121,8 @@ class GuiExport Application
boost::signals2::signal<void (const char*)> signalAddWorkbench;
/// signal on removed workbench
boost::signals2::signal<void (const char*)> signalRemoveWorkbench;
/// signal on show hidden items
boost::signals2::signal<void (const Gui::Document&)> signalShowHidden;
/// signal on activating view
boost::signals2::signal<void (const Gui::MDIView*)> signalActivateView;
/// signal on entering in edit mode
Expand All @@ -126,11 +135,12 @@ class GuiExport Application
//@{
protected:
/// Observer message from the Application
void slotNewDocument(const App::Document&);
void slotNewDocument(const App::Document&,bool);
void slotDeleteDocument(const App::Document&);
void slotRelabelDocument(const App::Document&);
void slotRenameDocument(const App::Document&);
void slotActiveDocument(const App::Document&);
void slotShowHidden(const App::Document&);
void slotNewObject(const ViewProvider&);
void slotDeletedObject(const ViewProvider&);
void slotChangedObject(const ViewProvider&, const App::Property& Prop);
Expand Down Expand Up @@ -223,8 +233,10 @@ class GuiExport Application
static PyObject* sAddLangPath (PyObject *self,PyObject *args); // adds a path to a qm file
static PyObject* sAddIconPath (PyObject *self,PyObject *args); // adds a path to an icon file
static PyObject* sAddIcon (PyObject *self,PyObject *args); // adds an icon to the cache
static PyObject* sGetIcon (PyObject *self,PyObject *args); // get an icon from the cache

static PyObject* sSendActiveView (PyObject *self,PyObject *args);
static PyObject* sSendFocusView (PyObject *self,PyObject *args);

static PyObject* sGetMainWindow (PyObject *self,PyObject *args);
static PyObject* sUpdateGui (PyObject *self,PyObject *args);
Expand All @@ -238,6 +250,7 @@ class GuiExport Application
static PyObject* sRunCommand (PyObject *self,PyObject *args);
static PyObject* sAddCommand (PyObject *self,PyObject *args);
static PyObject* sListCommands (PyObject *self,PyObject *args);
static PyObject* sIsCommandActive (PyObject *self,PyObject *args);

static PyObject* sHide (PyObject *self,PyObject *args); // deprecated
static PyObject* sShow (PyObject *self,PyObject *args); // deprecated
Expand All @@ -247,6 +260,7 @@ class GuiExport Application
static PyObject* sOpen (PyObject *self,PyObject *args); // open Python scripts
static PyObject* sInsert (PyObject *self,PyObject *args); // open Python scripts
static PyObject* sExport (PyObject *self,PyObject *args);
static PyObject* sReload (PyObject *self,PyObject *args);

static PyObject* sCoinRemoveAllChildren (PyObject *self,PyObject *args);

Expand Down

0 comments on commit bd2f519

Please sign in to comment.