Gui: Application/Document/MainWindow changes following App namespace

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.
This commit is contained in:
Zheng, Lei
2019-07-11 10:00:57 +08:00
committed by wmayer
parent 00bcef0619
commit bd2f5191c9
27 changed files with 2557 additions and 563 deletions

View File

@@ -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
@@ -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);
@@ -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
}
@@ -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)
@@ -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);
@@ -753,6 +769,12 @@ 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;
@@ -760,6 +782,7 @@ void Application::slotActiveDocument(const App::Document& Doc)
}
}
signalActiveDocument(*doc->second);
getMainWindow()->updateActions();
}
}
@@ -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)
@@ -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)
@@ -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();
@@ -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())
@@ -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) {
@@ -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();
}
}
@@ -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());
@@ -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;
}