From 68aca247ee540f35beec3d9c2cfacda3995ae913 Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Mon, 16 Sep 2019 16:49:09 +0800 Subject: [PATCH] Gui: remove FC_TREEPARAM, add document for class TreeParams --- src/Gui/ActiveObjectList.cpp | 2 +- src/Gui/CommandView.cpp | 10 ++-- src/Gui/Tree.cpp | 98 ++++++++++++++++-------------------- src/Gui/Tree.h | 49 ++++++++++++------ 4 files changed, 84 insertions(+), 75 deletions(-) diff --git a/src/Gui/ActiveObjectList.cpp b/src/Gui/ActiveObjectList.cpp index aaf6407fe0..f8ef85c3f6 100644 --- a/src/Gui/ActiveObjectList.cpp +++ b/src/Gui/ActiveObjectList.cpp @@ -62,7 +62,7 @@ void ActiveObjectList::setHighlight(const ObjectInfo &info, HighlightMode mode, auto vp = dynamic_cast(Application::Instance->getViewProvider(obj)); if(!vp) return; - if(FC_TREEPARAM(TreeActiveAutoExpand)) + if(TreeParams::Instance()->TreeActiveAutoExpand()) vp->getDocument()->signalExpandObject(*vp, enable?Gui::ExpandPath:Gui::CollapseItem, info.obj, info.subname.c_str()); diff --git a/src/Gui/CommandView.cpp b/src/Gui/CommandView.cpp index 0441a8ef26..0c4eed990e 100644 --- a/src/Gui/CommandView.cpp +++ b/src/Gui/CommandView.cpp @@ -3140,7 +3140,7 @@ bool StdCmdSelForward::isActive(void) #define TREEVIEW_DOC_CMD_DEF(_name,_v) \ DEF_STD_CMD_AC(StdTree##_name) \ void StdTree##_name::activated(int){ \ - FC_TREEPARAM_SET(DocumentMode,_v);\ + TreeParams::Instance()->setDocumentMode(_v);\ if(_pcAction) _pcAction->setChecked(true,true);\ }\ Action * StdTree##_name::createAction(void) {\ @@ -3152,7 +3152,7 @@ Action * StdTree##_name::createAction(void) {\ return pcAction;\ }\ bool StdTree##_name::isActive() {\ - bool checked = FC_TREEPARAM(DocumentMode)==_v;\ + bool checked = TreeParams::Instance()->DocumentMode()==_v;\ if(_pcAction && _pcAction->isChecked()!=checked)\ _pcAction->setChecked(checked,true);\ return true;\ @@ -3212,8 +3212,8 @@ StdTreeCollapseDocument::StdTreeCollapseDocument() #define TREEVIEW_CMD_DEF(_name) \ DEF_STD_CMD_AC(StdTree##_name) \ void StdTree##_name::activated(int){ \ - auto checked = !FC_TREEPARAM(_name);\ - FC_TREEPARAM_SET(_name,checked);\ + auto checked = !TreeParams::Instance()->_name();\ + TreeParams::Instance()->set##_name(checked);\ if(_pcAction) _pcAction->setChecked(checked,true);\ }\ Action * StdTree##_name::createAction(void) {\ @@ -3225,7 +3225,7 @@ Action * StdTree##_name::createAction(void) {\ return pcAction;\ }\ bool StdTree##_name::isActive() {\ - bool checked = FC_TREEPARAM(_name);\ + bool checked = TreeParams::Instance()->_name();\ if(_pcAction && _pcAction->isChecked()!=checked)\ _pcAction->setChecked(checked,true);\ return true;\ diff --git a/src/Gui/Tree.cpp b/src/Gui/Tree.cpp index af09727288..cf48f8605f 100644 --- a/src/Gui/Tree.cpp +++ b/src/Gui/Tree.cpp @@ -78,10 +78,6 @@ FC_LOG_LEVEL_INIT("Tree",false,true,true) using namespace Gui; -#define TREEVIEW_PARAM "User parameter:BaseApp/Preferences/TreeView" -#define GET_TREEVIEW_PARAM(_name) \ - ParameterGrp::handle _name = App::GetApplication().GetParameterGroupByPath(TREEVIEW_PARAM) - ///////////////////////////////////////////////////////////////////////////////// std::unique_ptr TreeWidget::documentPixmap; @@ -93,66 +89,58 @@ const int TreeWidget::ObjectType = 1001; bool _DragEventFilter; TreeParams::TreeParams() { - GET_TREEVIEW_PARAM(hGrp); - handle = hGrp; + handle = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/TreeView"); handle->Attach(this); -#define FC_TREEPARAM_INIT(_name,_type,_Type,_default) \ - _##_name = handle->Get##_Type(#_name,_default); #undef FC_TREEPARAM_DEF -#define FC_TREEPARAM_DEF FC_TREEPARAM_INIT +#define FC_TREEPARAM_DEF(_name,_type,_Type,_default) \ + _##_name = handle->Get##_Type(#_name,_default); + FC_TREEPARAM_DEFS } -#define FC_TREEPARAM_SET_FUNC(_name,_type,_Type,_default) \ + +#undef FC_TREEPARAM_DEF +#define FC_TREEPARAM_DEF(_name,_type,_Type,_default) \ void TreeParams::set##_name(_type value) {\ if(_##_name != value) {\ handle->Set##_Type(#_name,value);\ }\ } -#undef FC_TREEPARAM_DEF -#define FC_TREEPARAM_DEF FC_TREEPARAM_SET_FUNC FC_TREEPARAM_DEFS void TreeParams::OnChange(Base::Subject &, const char* sReason) { -#define FC_TREEPARAM_CHANGE(_name,_type,_Type,_default) \ + +#undef FC_TREEPARAM_DEF +#define FC_TREEPARAM_DEF(_name,_type,_Type,_default) \ + if(strcmp(sReason,#_name)==0) {\ + _##_name = handle->Get##_Type(#_name,_default);\ + return;\ + } + +#undef FC_TREEPARAM_DEF2 +#define FC_TREEPARAM_DEF2(_name,_type,_Type,_default) \ if(strcmp(sReason,#_name)==0) {\ _##_name = handle->Get##_Type(#_name,_default);\ on##_name##Changed();\ return;\ } -#undef FC_TREEPARAM_DEF -#define FC_TREEPARAM_DEF FC_TREEPARAM_CHANGE FC_TREEPARAM_DEFS } -void TreeParams::onPreSelectionChanged() {} - void TreeParams::onSyncSelectionChanged() { - if(!FC_TREEPARAM(SyncSelection) || !Gui::Selection().hasSelection()) + if(!TreeParams::Instance()->SyncSelection() || !Gui::Selection().hasSelection()) return; TreeWidget::scrollItemToTop(); } -void TreeParams::onSyncViewChanged() {} -void TreeParams::onSyncPlacementChanged() {} -void TreeParams::onRecordSelectionChanged() {} -void TreeParams::onRecomputeOnDropChanged() {} -void TreeParams::onKeepRootOrderChanged() {} -void TreeParams::onTreeActiveAutoExpandChanged() {} - void TreeParams::onDocumentModeChanged() { App::GetApplication().setActiveDocument(App::GetApplication().getActiveDocument()); } -void TreeParams::onStatusTimeoutChanged() {} -void TreeParams::onSelectionTimeoutChanged() {} -void TreeParams::onPreSelectionTimeoutChanged() {} -void TreeParams::onPreSelectionDelayChanged() {} - TreeParams *TreeParams::Instance() { static TreeParams *instance; if(!instance) @@ -775,7 +763,7 @@ void TreeWidget::_updateStatus(bool delay) { onUpdateStatus(); return; } - int timeout = FC_TREEPARAM(StatusTimeout); + int timeout = TreeParams::Instance()->StatusTimeout(); if (timeout<0) timeout = 1; FC_LOG("delay update status"); @@ -921,7 +909,7 @@ void TreeWidget::showEvent(QShowEvent *ev) { #if 0 TREE_TRACE("attaching selection observer"); this->attachSelection(); - int timeout = FC_TREEPARAM(SelectionTimeout); + int timeout = TreeParams::Instance()->SelectionTimeout(); if(timeout<=0) timeout = 1; selectTimer->start(timeout); @@ -1610,7 +1598,7 @@ void TreeWidget::dropEvent(QDropEvent *event) targetParent->getNameInDocument()); } - bool syncPlacement = FC_TREEPARAM(SyncPlacement) && targetItemObj->isGroup(); + bool syncPlacement = TreeParams::Instance()->SyncPlacement() && targetItemObj->isGroup(); bool setSelection = true; std::vector > droppedObjects; @@ -1923,7 +1911,7 @@ void TreeWidget::dropEvent(QDropEvent *event) std::vector infos; infos.reserve(items.size()); - bool syncPlacement = FC_TREEPARAM(SyncPlacement); + bool syncPlacement = TreeParams::Instance()->SyncPlacement(); // check if items can be dragged for(auto &v : items) { @@ -2104,7 +2092,7 @@ void TreeWidget::dropEvent(QDropEvent *event) } } - if(touched && FC_TREEPARAM(RecomputeOnDrop)) + if(touched && TreeParams::Instance()->RecomputeOnDrop()) thisDoc->recompute(); } @@ -2223,7 +2211,7 @@ void TreeWidget::slotActiveDocument(const Gui::Document& Doc) auto jt = DocumentMap.find(&Doc); if (jt == DocumentMap.end()) return; // signal is emitted before the item gets created - int displayMode = FC_TREEPARAM(DocumentMode); + int displayMode = TreeParams::Instance()->DocumentMode(); for (auto it = DocumentMap.begin(); it != DocumentMap.end(); ++it) { @@ -2460,33 +2448,33 @@ void TreeWidget::onItemEntered(QTreeWidgetItem * item) DocumentObjectItem* objItem = static_cast(item); objItem->displayStatusInfo(); - if(FC_TREEPARAM(PreSelection)) { - int timeout = FC_TREEPARAM(PreSelectionDelay); + if(TreeParams::Instance()->PreSelection()) { + int timeout = TreeParams::Instance()->PreSelectionDelay(); if(timeout < 0) timeout = 1; if(preselectTime.elapsed() < timeout) onPreSelectTimer(); else{ - timeout = FC_TREEPARAM(PreSelectionTimeout); + timeout = TreeParams::Instance()->PreSelectionTimeout(); if(timeout < 0) timeout = 1; preselectTimer->start(timeout); Selection().rmvPreselect(); } } - } else if(FC_TREEPARAM(PreSelection)) + } else if(TreeParams::Instance()->PreSelection()) Selection().rmvPreselect(); } void TreeWidget::leaveEvent(QEvent *) { - if(!updateBlocked && FC_TREEPARAM(PreSelection)) { + if(!updateBlocked && TreeParams::Instance()->PreSelection()) { preselectTimer->stop(); Selection().rmvPreselect(); } } void TreeWidget::onPreSelectTimer() { - if(!FC_TREEPARAM(PreSelection)) + if(!TreeParams::Instance()->PreSelection()) return; auto item = itemAt(viewport()->mapFromGlobal(QCursor::pos())); if(!item || item->type()!=TreeWidget::ObjectType) @@ -2636,7 +2624,7 @@ void TreeWidget::setupText() { } void TreeWidget::syncView(ViewProviderDocumentObject *vp) { - if(currentDocItem && FC_TREEPARAM(SyncView)) { + if(currentDocItem && TreeParams::Instance()->SyncView()) { bool focus = hasFocus(); currentDocItem->document()->setActiveView(vp); if(focus) @@ -2717,7 +2705,7 @@ void TreeWidget::onItemSelectionChanged () } if(selItems.size()<=1) { - if(FC_TREEPARAM(RecordSelection)) + if(TreeParams::Instance()->RecordSelection()) Gui::Selection().selStackPush(); // This special handling to deal with possible discrepency of @@ -2730,7 +2718,7 @@ void TreeWidget::onItemSelectionChanged () item = static_cast(selItems.front()); else if(selItems.front()->type() == DocumentType) { auto ditem = static_cast(selItems.front()); - if(FC_TREEPARAM(SyncView)) { + if(TreeParams::Instance()->SyncView()) { bool focus = hasFocus(); ditem->document()->setActiveView(); if(focus) @@ -2745,7 +2733,7 @@ void TreeWidget::onItemSelectionChanged () v.second->clearSelection(item); currentDocItem = 0; } - if(FC_TREEPARAM(RecordSelection)) + if(TreeParams::Instance()->RecordSelection()) Gui::Selection().selStackPush(); }else{ for (auto pos = DocumentMap.begin();pos!=DocumentMap.end();++pos) { @@ -2753,7 +2741,7 @@ void TreeWidget::onItemSelectionChanged () pos->second->updateSelection(pos->second); currentDocItem = 0; } - if(FC_TREEPARAM(RecordSelection)) + if(TreeParams::Instance()->RecordSelection()) Gui::Selection().selStackPush(true,true); } @@ -2764,7 +2752,7 @@ void TreeWidget::onSelectTimer() { _updateStatus(false); - bool syncSelect = FC_TREEPARAM(SyncSelection); + bool syncSelect = TreeParams::Instance()->SyncSelection(); bool locked = this->blockConnection(true); if(Selection().hasSelection()) { for(auto &v : DocumentMap) { @@ -2790,7 +2778,7 @@ void TreeWidget::onSelectionChanged(const SelectionChanges& msg) case SelectionChanges::RmvSelection: case SelectionChanges::SetSelection: case SelectionChanges::ClrSelection: { - int timeout = FC_TREEPARAM(SelectionTimeout); + int timeout = TreeParams::Instance()->SelectionTimeout(); if(timeout<=0) timeout = 1; selectTimer->start(timeout); @@ -2809,8 +2797,9 @@ TreePanel::TreePanel(const char *name, QWidget* parent) : QWidget(parent) { this->treeWidget = new TreeWidget(name, this); - ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/TreeView"); - this->treeWidget->setIndentation(hGrp->GetInt("Indentation", this->treeWidget->indentation())); + int indent = TreeParams::Instance()->Indentation(); + if(indent) + this->treeWidget->setIndentation(indent); QVBoxLayout* pLayout = new QVBoxLayout(this); pLayout->setSpacing(0); @@ -2902,8 +2891,9 @@ TreeDockWidget::TreeDockWidget(Gui::Document* pcDocument,QWidget *parent) setWindowTitle(tr("Tree view")); this->treeWidget = new TreeWidget("TreeView",this); this->treeWidget->setRootIsDecorated(false); - GET_TREEVIEW_PARAM(hGrp); - this->treeWidget->setIndentation(hGrp->GetInt("Indentation", this->treeWidget->indentation())); + int indent = TreeParams::Instance()->Indentation(); + if(indent) + this->treeWidget->setIndentation(indent); QGridLayout* pLayout = new QGridLayout(this); pLayout->setSpacing(0); @@ -3433,7 +3423,7 @@ void DocumentItem::populateItem(DocumentObjectItem *item, bool refresh, bool del } int DocumentItem::findRootIndex(App::DocumentObject *childObj) { - if(!FC_TREEPARAM(KeepRootOrder) || !childObj || !childObj->getNameInDocument()) + if(!TreeParams::Instance()->KeepRootOrder() || !childObj || !childObj->getNameInDocument()) return -1; // object id is monotonically increasing, so use this as a hint to insert diff --git a/src/Gui/Tree.h b/src/Gui/Tree.h index bcddf451b6..7066f2de86 100644 --- a/src/Gui/Tree.h +++ b/src/Gui/Tree.h @@ -488,7 +488,23 @@ public: }; -/// Helper class to read/write tree view options +/** Helper class to read/write tree view options + * + * The parameters are stored under group "User parameter:BaseApp/Preferences/TreeView". + * Call TreeParams::Instance()->ParamName/setParamName() to get/set parameter. + * To add a new parameter, add a new line under FC_TREEPARAM_DEFS using macro + * + * @code + * FC_TREEPARAM_DEF(parameter_name, c_type, parameter_type, default_value) + * @endcode + * + * If there is special handling on parameter change, use FC_TREEPARAM_DEF2() + * instead, and add a function with the following signature in Tree.cpp, + * + * @code + * void TreeParams:onChanged() + * @endcode + */ class GuiExport TreeParams : public ParameterGrp::ObserverType { public: TreeParams(); @@ -496,12 +512,12 @@ public: static TreeParams *Instance(); #define FC_TREEPARAM_DEFS \ - FC_TREEPARAM_DEF(SyncSelection,bool,Bool,true) \ + FC_TREEPARAM_DEF2(SyncSelection,bool,Bool,true) \ FC_TREEPARAM_DEF(SyncView,bool,Bool,true) \ FC_TREEPARAM_DEF(PreSelection,bool,Bool,true) \ FC_TREEPARAM_DEF(SyncPlacement,bool,Bool,false) \ FC_TREEPARAM_DEF(RecordSelection,bool,Bool,true) \ - FC_TREEPARAM_DEF(DocumentMode,int,Int,1) \ + FC_TREEPARAM_DEF2(DocumentMode,int,Int,1) \ FC_TREEPARAM_DEF(StatusTimeout,int,Int,100) \ FC_TREEPARAM_DEF(SelectionTimeout,int,Int,100) \ FC_TREEPARAM_DEF(PreSelectionTimeout,int,Int,500) \ @@ -509,31 +525,34 @@ public: FC_TREEPARAM_DEF(RecomputeOnDrop,bool,Bool,true) \ FC_TREEPARAM_DEF(KeepRootOrder,bool,Bool,true) \ FC_TREEPARAM_DEF(TreeActiveAutoExpand,bool,Bool,true) \ - -#define FC_TREEPARAM_FUNCS(_name,_type,_Type,_default) \ - _type _name() const {return _##_name;} \ - void set##_name(_type);\ - void on##_name##Changed(); + FC_TREEPARAM_DEF(Indentation,int,Int,0) \ #undef FC_TREEPARAM_DEF -#define FC_TREEPARAM_DEF FC_TREEPARAM_FUNCS +#define FC_TREEPARAM_DEF(_name,_type,_Type,_default) \ + _type _name() const {return _##_name;} \ + void set##_name(_type);\ + +#undef FC_TREEPARAM_DEF2 +#define FC_TREEPARAM_DEF2(_name,_type,_Type,_default) \ + FC_TREEPARAM_DEF(_name,_type,_Type,_default) \ + void on##_name##Changed();\ + FC_TREEPARAM_DEFS private: -#define FC_TREEPARAM_DECLARE(_name,_type,_Type,_default) \ +#undef FC_TREEPARAM_DEF +#define FC_TREEPARAM_DEF(_name,_type,_Type,_default) \ _type _##_name; -#undef FC_TREEPARAM_DEF -#define FC_TREEPARAM_DEF FC_TREEPARAM_DECLARE +#undef FC_TREEPARAM_DEF2 +#define FC_TREEPARAM_DEF2 FC_TREEPARAM_DEF + FC_TREEPARAM_DEFS ParameterGrp::handle handle; }; -#define FC_TREEPARAM(_name) (Gui::TreeParams::Instance()->_name()) -#define FC_TREEPARAM_SET(_name,_v) Gui::TreeParams::Instance()->set##_name(_v) - } #endif // GUI_TREE_H