From 67b990c2b53c66095c6ca096fae201efb17ab0a6 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 5 Oct 2017 10:38:42 +0200 Subject: [PATCH] fixes #0002323: Workbench Start recent list of 3 files is - after a crash - not sync with recent list in File Menu Recent list of 4 files --- src/Base/ParameterPy.cpp | 6 +-- src/Gui/Action.cpp | 3 +- src/Mod/Start/Gui/Workbench.cpp | 37 ++++++++++++++--- src/Mod/Web/Gui/AppWebGui.cpp | 20 ++++++++- src/Mod/Web/Gui/BrowserView.cpp | 73 ++++++++++++++++++++++++++++++++- src/Mod/Web/Gui/BrowserView.h | 22 ++-------- 6 files changed, 130 insertions(+), 31 deletions(-) diff --git a/src/Base/ParameterPy.cpp b/src/Base/ParameterPy.cpp index 7ce79e3e2a..3cce49e3ee 100644 --- a/src/Base/ParameterPy.cpp +++ b/src/Base/ParameterPy.cpp @@ -69,8 +69,6 @@ public: } virtual void OnChange(ParameterGrp::SubjectType &rCaller,ParameterGrp::MessageType Reason) { - if (!Reason || Reason[0] == '\0') - return; Base::PyGILStateLocker lock; try { ParameterGrp& rGrp = static_cast(rCaller); @@ -78,7 +76,9 @@ public: Py::Callable method(this->inst.getAttr(std::string("onChange"))); Py::Tuple args(2); args.setItem(0, Py::asObject(GetPyObject(hGrp))); - args.setItem(1, Py::String(Reason)); + // A Reason of null indicates to clear the parameter group + if (Reason && Reason[0] != '\0') + args.setItem(1, Py::String(Reason)); method.apply(args); } catch (Py::Exception&) { diff --git a/src/Gui/Action.cpp b/src/Gui/Action.cpp index 0f9df4d35a..c2034b0fa2 100644 --- a/src/Gui/Action.cpp +++ b/src/Gui/Action.cpp @@ -773,7 +773,6 @@ void RecentFilesAction::save() ->GetGroup("Preferences")->GetGroup("RecentFiles"); int count = hGrp->GetInt("RecentFiles", this->visibleItems); // save number of files hGrp->Clear(); - hGrp->SetInt("RecentFiles", count); // restore // count all set items QList recentFiles = _group->actions(); @@ -785,6 +784,8 @@ void RecentFilesAction::save() break; hGrp->SetASCII(key.toLatin1(), value.toUtf8()); } + + hGrp->SetInt("RecentFiles", count); // restore } // -------------------------------------------------------------------- diff --git a/src/Mod/Start/Gui/Workbench.cpp b/src/Mod/Start/Gui/Workbench.cpp index 1b902e39eb..14c09e0981 100644 --- a/src/Mod/Start/Gui/Workbench.cpp +++ b/src/Mod/Start/Gui/Workbench.cpp @@ -25,6 +25,7 @@ #ifndef _PreComp_ # include +# include #endif #include "Workbench.h" @@ -69,15 +70,39 @@ void StartGui::Workbench::activated() try { QByteArray utf8Title = title.toUtf8(); - Gui::Command::doCommand(Gui::Command::Gui,"import WebGui"); - Gui::Command::doCommand(Gui::Command::Gui,"from StartPage import StartPage"); + QByteArray cmd; + QTextStream str(&cmd); + str << "import WebGui" << endl; + str << "from StartPage import StartPage" << endl; + str << endl; + str << "class WebPage(object):" << endl; + str << " def __init__(self):" << endl; + str << " self.browser=WebGui.openBrowserWindow('" << utf8Title << "')" << endl; #if defined(FC_OS_WIN32) - Gui::Command::doCommand(Gui::Command::Gui,"WebGui.openBrowserHTML" - "(StartPage.handle(),App.getResourceDir() + 'Mod/Start/StartPage/','%s')", utf8Title.data()); + str << " self.browser.setHtml(StartPage.handle(), App.getResourceDir() + 'Mod/Start/StartPage/')" << endl; #else - Gui::Command::doCommand(Gui::Command::Gui,"WebGui.openBrowserHTML" - "(StartPage.handle(),'file://' + App.getResourceDir() + 'Mod/Start/StartPage/','%s')", utf8Title.data()); + str << " self.browser.setHtml(StartPage.handle(), 'file://' + App.getResourceDir() + 'Mod/Start/StartPage/')" << endl; #endif + str << " def onChange(self, par, reason):" << endl; + str << " if reason == 'RecentFiles':" << endl; +#if defined(FC_OS_WIN32) + str << " self.browser.setHtml(StartPage.handle(), App.getResourceDir() + 'Mod/Start/StartPage/')" << endl; +#else + str << " self.browser.setHtml(StartPage.handle(), 'file://' + App.getResourceDir() + 'Mod/Start/StartPage/')" << endl; +#endif + str << endl; + str << "class WebView(object):" << endl; + str << " def __init__(self):" << endl; + str << " self.pargrp = FreeCAD.ParamGet('User parameter:BaseApp/Preferences/RecentFiles')" << endl; + str << " self.webPage = WebPage()" << endl; + str << " self.pargrp.Attach(self.webPage)" << endl; + str << " def __del__(self):" << endl; + str << " self.pargrp.Detach(self.webPage)" << endl; + str << endl; + str << "webView=WebView()" << endl; + + + Gui::Command::runCommand(Gui::Command::Gui, cmd); } catch (const Base::Exception& e) { Base::Console().Error("%s\n", e.what()); diff --git a/src/Mod/Web/Gui/AppWebGui.cpp b/src/Mod/Web/Gui/AppWebGui.cpp index fc695303c3..6ead5f6240 100644 --- a/src/Mod/Web/Gui/AppWebGui.cpp +++ b/src/Mod/Web/Gui/AppWebGui.cpp @@ -60,6 +60,8 @@ public: ); add_varargs_method("openBrowserHTML",&Module::openBrowserHTML ); + add_varargs_method("openBrowserWindow",&Module::openBrowserWindow + ); initialize("This module is the WebGui module."); // register with Python } @@ -94,11 +96,27 @@ private: WebGui::BrowserView* pcBrowserView = 0; pcBrowserView = new WebGui::BrowserView(Gui::getMainWindow()); pcBrowserView->resize(400, 300); - pcBrowserView->setHtml(QString::fromUtf8(HtmlCode),QUrl(QString::fromLatin1(BaseUrl)),QString::fromUtf8(TabName)); + pcBrowserView->setHtml(QString::fromUtf8(HtmlCode),QUrl(QString::fromLatin1(BaseUrl))); + pcBrowserView->setWindowTitle(QString::fromUtf8(TabName)); Gui::getMainWindow()->addWindow(pcBrowserView); return Py::None(); } + + Py::Object openBrowserWindow(const Py::Tuple& args) + { + const char* TabName = "Browser"; + if (! PyArg_ParseTuple(args.ptr(), "|s",&TabName)) + throw Py::Exception(); + + WebGui::BrowserView* pcBrowserView = 0; + pcBrowserView = new WebGui::BrowserView(Gui::getMainWindow()); + pcBrowserView->resize(400, 300); + pcBrowserView->setWindowTitle(QString::fromUtf8(TabName)); + Gui::getMainWindow()->addWindow(pcBrowserView); + + return Py::asObject(pcBrowserView->getPyObject()); + } }; PyObject* initModule() diff --git a/src/Mod/Web/Gui/BrowserView.cpp b/src/Mod/Web/Gui/BrowserView.cpp index bbf22af330..2480c325c9 100644 --- a/src/Mod/Web/Gui/BrowserView.cpp +++ b/src/Mod/Web/Gui/BrowserView.cpp @@ -51,6 +51,7 @@ # include # include # include +# include #endif #include "BrowserView.h" @@ -64,10 +65,68 @@ #include #include +#include using namespace WebGui; using namespace Gui; +namespace WebGui { +class BrowserViewPy : public Py::PythonExtension +{ +public: + static void init_type(void); // announce properties and methods + + BrowserViewPy(BrowserView* view); + ~BrowserViewPy(); + + Py::Object repr(); + + Py::Object setHtml(const Py::Tuple&); + +private: + QPointer myWebView; +}; + +void BrowserViewPy::init_type() +{ + behaviors().name("BrowserView"); + behaviors().doc("Python interface class to BrowserView"); + // you must have overwritten the virtual functions + behaviors().supportRepr(); + behaviors().supportGetattr(); + behaviors().supportSetattr(); + behaviors().readyType(); + + add_varargs_method("setHtml",&BrowserViewPy::setHtml,"setHtml(str)"); +} + +BrowserViewPy::BrowserViewPy(BrowserView* view) : myWebView(view) +{ +} + +BrowserViewPy::~BrowserViewPy() +{ +} + +Py::Object BrowserViewPy::repr() +{ + std::stringstream s; + s << ""; + return Py::String(s.str()); +} + +Py::Object BrowserViewPy::setHtml(const Py::Tuple& args) +{ + char* HtmlCode; + char* BaseUrl; + if (! PyArg_ParseTuple(args.ptr(), "ss",&HtmlCode,&BaseUrl)) + throw Py::Exception(); + if (myWebView) + myWebView->setHtml(QString::fromUtf8(HtmlCode),QUrl(QString::fromLatin1(BaseUrl))); + return Py::None(); +} +} + /** * Constructs a WebView widget which can be zoomed with Ctrl+Mousewheel * @@ -302,13 +361,12 @@ void BrowserView::load(const QUrl & url) setWindowIcon(QWebSettings::iconForUrl(url)); } -void BrowserView::setHtml(const QString& HtmlCode,const QUrl & BaseUrl,const QString& TabName) +void BrowserView::setHtml(const QString& HtmlCode,const QUrl & BaseUrl) { if (isLoading) stop(); view->setHtml(HtmlCode,BaseUrl); - setWindowTitle(TabName); setWindowIcon(QWebSettings::iconForUrl(BaseUrl)); } @@ -416,5 +474,16 @@ bool BrowserView::canClose(void) return true; } +PyObject* BrowserView::getPyObject(void) +{ + static bool init = false; + if (!init) { + init = true; + BrowserViewPy::init_type(); + } + + return new BrowserViewPy(this); +} + #include "moc_BrowserView.cpp" diff --git a/src/Mod/Web/Gui/BrowserView.h b/src/Mod/Web/Gui/BrowserView.h index f745c8a210..4409888668 100644 --- a/src/Mod/Web/Gui/BrowserView.h +++ b/src/Mod/Web/Gui/BrowserView.h @@ -63,7 +63,8 @@ Q_SIGNALS: * A special view class which sends the messages from the application to * the editor and embeds it in a window. */ -class WebGuiExport BrowserView : public Gui::MDIView, public Gui::WindowParameter +class WebGuiExport BrowserView : public Gui::MDIView, + public Gui::WindowParameter { Q_OBJECT @@ -73,34 +74,19 @@ public: void load(const char* URL); void load(const QUrl & url); - void setHtml(const QString& HtmlCode,const QUrl & BaseUrl,const QString& TabName=QString::fromLatin1("Browser")); + void setHtml(const QString& HtmlCode,const QUrl & BaseUrl); void stop(void); void OnChange(Base::Subject &rCaller,const char* rcReason); const char *getName(void) const {return "BrowserView";} - void onUpdate(void){}; + virtual PyObject *getPyObject(void); bool onMsg(const char* pMsg,const char** ppReturn); bool onHasMsg(const char* pMsg) const; bool canClose(void); - /** @name Standard actions of the editor */ - //@{ - //bool open (const QString &f); - //bool saveAs (); - //void cut (); - //void copy (); - //void paste (); - //void undo (); - //void redo (); - //void run (); - //void print (); - //void printPdf(); - //@} - - protected Q_SLOTS: void onLoadStarted(); void onLoadProgress(int);