From 8dc67afdf38c6b27edd10ae38d5aafb945f9a16a Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Fri, 17 Feb 2023 11:24:35 +0100 Subject: [PATCH] allow full access from Python --- .../settings/qgssettingstree.sip.in | 7 +-- .../settings/qgssettingstreenode.sip.in | 7 --- python/pyplugin_installer/installer.py | 2 +- python/pyplugin_installer/installer_data.py | 16 +++--- src/core/settings/qgssettingstree.cpp | 12 ---- src/core/settings/qgssettingstree.h | 7 +-- src/core/settings/qgssettingstreenode.cpp | 56 +------------------ src/core/settings/qgssettingstreenode.h | 6 -- tests/src/python/test_qgssettingstreenode.py | 5 +- 9 files changed, 18 insertions(+), 100 deletions(-) diff --git a/python/core/auto_generated/settings/qgssettingstree.sip.in b/python/core/auto_generated/settings/qgssettingstree.sip.in index 33efa504abc..7172171710f 100644 --- a/python/core/auto_generated/settings/qgssettingstree.sip.in +++ b/python/core/auto_generated/settings/qgssettingstree.sip.in @@ -24,14 +24,13 @@ class QgsSettingsTree public: - static const QgsSettingsTreeNode *nodeCopy( const QString &key ); + static const QgsSettingsTreeNode *node( const QString &key ); %Docstring -Returns a copy of tree node at the given ``key`` +Returns the tree node at the given ``key`` .. note:: - The returned node is a copy and any modification to it won't alter the main tree. - This is intended to be used to access settings from Python. + For Plugins, use :py:func:`~QgsSettingsTree.createPluginTreeNode` to create nodes for plugin settings. %End static QgsSettingsTreeNode *createPluginTreeNode( const QString &pluginName ); diff --git a/python/core/auto_generated/settings/qgssettingstreenode.sip.in b/python/core/auto_generated/settings/qgssettingstreenode.sip.in index 735fe0fae4a..fee9b8dd24e 100644 --- a/python/core/auto_generated/settings/qgssettingstreenode.sip.in +++ b/python/core/auto_generated/settings/qgssettingstreenode.sip.in @@ -160,11 +160,6 @@ Returns the number of named nodes in the complete key sipRes = PyUnicode_FromString( str.toUtf8().constData() ); %End - virtual void copy( const QgsSettingsTreeNode *other ); -%Docstring -Copies the ``other`` node to this instance -%End - protected: void registerChildNode( QgsSettingsTreeNode *node ); %Docstring @@ -264,8 +259,6 @@ Deletes a named item from the named list node Returns the setting used to store the selected item %End - virtual void copy( const QgsSettingsTreeNode *other ); - protected: void initNamedList( const QgsSettingsTreeNode::Options &options ); %Docstring diff --git a/python/pyplugin_installer/installer.py b/python/pyplugin_installer/installer.py index beefedc0b8b..3c2b8fb3566 100644 --- a/python/pyplugin_installer/installer.py +++ b/python/pyplugin_installer/installer.py @@ -578,7 +578,7 @@ class QgsPluginInstaller(QObject): if not os.path.isfile(filePath): return - QgsSettingsTree.nodeCopy("plugin-manager").childSetting("last-zip-directory").setValue(QFileInfo(filePath).absoluteDir().absolutePath()) + QgsSettingsTree.node("plugin-manager").childSetting("last-zip-directory").setValue(QFileInfo(filePath).absoluteDir().absolutePath()) pluginName = None with zipfile.ZipFile(filePath, 'r') as zf: diff --git a/python/pyplugin_installer/installer_data.py b/python/pyplugin_installer/installer_data.py index a43212e0031..a8af38edda8 100644 --- a/python/pyplugin_installer/installer_data.py +++ b/python/pyplugin_installer/installer_data.py @@ -229,22 +229,22 @@ class Repositories(QObject): def checkingOnStart(self) -> bool: """ return true if checking for news and updates is enabled """ - return QgsSettingsTree.nodeCopy("plugin-manager").childSetting('automatically-check-for-updates').value() + return QgsSettingsTree.node("plugin-manager").childSetting('automatically-check-for-updates').value() def setCheckingOnStart(self, state: bool): """ set state of checking for news and updates """ - QgsSettingsTree.nodeCopy("plugin-manager").childSetting('automatically-check-for-updates').setValue(state) + QgsSettingsTree.node("plugin-manager").childSetting('automatically-check-for-updates').setValue(state) def saveCheckingOnStartLastDate(self): """ set today's date as the day of last checking """ - QgsSettingsTree.nodeCopy("plugin-manager").childSetting('check-on-start-last-date').setValue(QDate.currentDate()) + QgsSettingsTree.node("plugin-manager").childSetting('check-on-start-last-date').setValue(QDate.currentDate()) def timeForChecking(self) -> bool: """ determine whether it's the time for checking for news and updates now """ settings = QgsSettings() try: # QgsSettings may contain ivalid value... - interval = QgsSettingsTree.nodeCopy("plugin-manager").childSetting('check-on-start-last-date').valueAs(type=QDate).daysTo(QDate.currentDate()) + interval = QgsSettingsTree.node("plugin-manager").childSetting('check-on-start-last-date').valueAs(type=QDate).daysTo(QDate.currentDate()) except: interval = 0 if interval >= Repositories.CHECK_ON_START_INTERVAL: @@ -711,8 +711,8 @@ class Plugins(QObject): self.mPlugins = {} for i in list(self.localCache.keys()): self.mPlugins[i] = self.localCache[i].copy() - allowExperimental = QgsSettingsTree.nodeCopy("plugin-manager").childSetting("allow-experimental").value() - allowDeprecated = QgsSettingsTree.nodeCopy("plugin-manager").childSetting("allow-deprecated").value() + allowExperimental = QgsSettingsTree.node("plugin-manager").childSetting("allow-experimental").value() + allowDeprecated = QgsSettingsTree.node("plugin-manager").childSetting("allow-deprecated").value() for i in list(self.repoCache.values()): for j in i: plugin = j.copy() # do not update repoCache elements! @@ -808,7 +808,7 @@ class Plugins(QObject): # ----------------------------------------- # def markNews(self): """ mark all new plugins as new """ - seenPlugins = QgsSettingsTree.nodeCopy("plugin-manager").childSetting("seen-plugins").valueWithDefaultOverride(list(self.mPlugins.keys())) + seenPlugins = QgsSettingsTree.node("plugin-manager").childSetting("seen-plugins").valueWithDefaultOverride(list(self.mPlugins.keys())) if len(seenPlugins) > 0: for plugin in list(self.mPlugins.keys()): if seenPlugins.count(plugin) == 0 and self.mPlugins[plugin]["status"] == "not installed": @@ -817,7 +817,7 @@ class Plugins(QObject): # ----------------------------------------- # def updateSeenPluginsList(self): """ update the list of all seen plugins """ - setting = QgsSettingsTree.nodeCopy("plugin-manager").childSetting("seen-plugins") + setting = QgsSettingsTree.node("plugin-manager").childSetting("seen-plugins") seenPlugins = setting.valueWithDefaultOverride(list(self.mPlugins.keys())) for plugin in list(self.mPlugins.keys()): if seenPlugins.count(plugin) == 0: diff --git a/src/core/settings/qgssettingstree.cpp b/src/core/settings/qgssettingstree.cpp index 62137689175..0af247b18c7 100644 --- a/src/core/settings/qgssettingstree.cpp +++ b/src/core/settings/qgssettingstree.cpp @@ -22,18 +22,6 @@ QgsSettingsTreeNode *QgsSettingsTree::treeRoot() return sTreeRoot; } -const QgsSettingsTreeNode *QgsSettingsTree::nodeCopy( const QString &key ) -{ - QgsSettingsTreeNode *copyNode = nullptr; - const QgsSettingsTreeNode *node = treeRoot()->childNode( key ); - if ( node ) - { - copyNode = new QgsSettingsTreeNode(); - copyNode->copy( node ); - } - return copyNode; -} - QgsSettingsTreeNode *QgsSettingsTree::createPluginTreeNode( const QString &pluginName ) { QgsSettingsTreeNode *te = sTreePlugins->childNode( pluginName ); diff --git a/src/core/settings/qgssettingstree.h b/src/core/settings/qgssettingstree.h index ecfc949a1db..46647d06452 100644 --- a/src/core/settings/qgssettingstree.h +++ b/src/core/settings/qgssettingstree.h @@ -65,11 +65,10 @@ class CORE_EXPORT QgsSettingsTree #endif /** - * Returns a copy of tree node at the given \a key - * \note The returned node is a copy and any modification to it won't alter the main tree. - * This is intended to be used to access settings from Python. + * Returns the tree node at the given \a key + * \note For Plugins, use createPluginTreeNode() to create nodes for plugin settings. */ - static const QgsSettingsTreeNode *nodeCopy( const QString &key ); + static const QgsSettingsTreeNode *node( const QString &key ) {return treeRoot()->childNode( key );} /** * Creates a settings tree node for the given \a pluginName diff --git a/src/core/settings/qgssettingstreenode.cpp b/src/core/settings/qgssettingstreenode.cpp index 197919c5482..361b9f534c7 100644 --- a/src/core/settings/qgssettingstreenode.cpp +++ b/src/core/settings/qgssettingstreenode.cpp @@ -27,8 +27,7 @@ QgsSettingsTreeNode::~QgsSettingsTreeNode() mParent->unregisterChildNode( this ); qDeleteAll( mChildrenNodes ); - if ( !mIsCopy ) - qDeleteAll( mChildrenSettings ); + qDeleteAll( mChildrenSettings ); } QgsSettingsTreeNode *QgsSettingsTreeNode::createRootNode() @@ -116,48 +115,6 @@ void QgsSettingsTreeNode::registerChildNode( QgsSettingsTreeNode *node ) mChildrenNodes.append( node ); } -void QgsSettingsTreeNode::copy( const QgsSettingsTreeNode *other ) -{ - mIsCopy = true; - mType = other->type(); - mChildrenSettings = other->mChildrenSettings; - mParent = other->mParent; - mKey = other->mKey; - mCompleteKey = other->mCompleteKey; - mNamedNodesCount = other->mNamedNodesCount; - - const QList children = other->mChildrenNodes; - for ( const QgsSettingsTreeNode *child : children ) - { - switch ( child->type() ) - { - case Type::Root: - { - Q_ASSERT( false ); - break; - } - - case Type::Standard: - { - QgsSettingsTreeNode *newChild = new QgsSettingsTreeNode(); - newChild->copy( child ); - mChildrenNodes.append( newChild ); - break; - } - - case Type::NamedList: - { - QgsSettingsTreeNamedListNode *newChild = new QgsSettingsTreeNamedListNode(); - newChild->copy( child ); - mChildrenNodes.append( newChild ); - break; - } - } - } - - mChildrenSettings = other->childrenSettings(); -} - void QgsSettingsTreeNode::unregisterChildSetting( const QgsSettingsEntryBase *setting, bool deleteSettingValues, const QStringList &parentsNamedItems ) { if ( deleteSettingValues ) @@ -193,17 +150,6 @@ void QgsSettingsTreeNamedListNode::initNamedList( const QgsSettingsTreeNode::Opt mCompleteKey.append( QStringLiteral( "items/%%1/" ).arg( mNamedNodesCount ) ); } -void QgsSettingsTreeNamedListNode::copy( const QgsSettingsTreeNode *other ) -{ - QgsSettingsTreeNode::copy( other ); - - const QgsSettingsTreeNamedListNode *otherNamedList = dynamic_cast( other ); - - mOptions = otherNamedList->mOptions; - mSelectedItemSetting = otherNamedList->mSelectedItemSetting; - mItemsCompleteKey = otherNamedList->mItemsCompleteKey; -} - QgsSettingsTreeNamedListNode::~QgsSettingsTreeNamedListNode() { delete mSelectedItemSetting; diff --git a/src/core/settings/qgssettingstreenode.h b/src/core/settings/qgssettingstreenode.h index 5430b4491b4..419534e20ab 100644 --- a/src/core/settings/qgssettingstreenode.h +++ b/src/core/settings/qgssettingstreenode.h @@ -157,9 +157,6 @@ class CORE_EXPORT QgsSettingsTreeNode % End #endif - //! Copies the \a other node to this instance - virtual void copy( const QgsSettingsTreeNode *other ); - protected: //! Registers a child nodes void registerChildNode( QgsSettingsTreeNode *node ); @@ -189,7 +186,6 @@ class CORE_EXPORT QgsSettingsTreeNode QList mChildrenSettings; QgsSettingsTreeNode *mParent = nullptr; - bool mIsCopy = false; QString mKey; QString mCompleteKey; int mNamedNodesCount = 0; @@ -257,8 +253,6 @@ class CORE_EXPORT QgsSettingsTreeNamedListNode : public QgsSettingsTreeNode //! Returns the setting used to store the selected item const QgsSettingsEntryString *selectedItemSetting() const {return mSelectedItemSetting;} - virtual void copy( const QgsSettingsTreeNode *other ) override; - protected: //! Init the nodes with the specific \a options void initNamedList( const QgsSettingsTreeNode::Options &options ); diff --git a/tests/src/python/test_qgssettingstreenode.py b/tests/src/python/test_qgssettingstreenode.py index 0c1fa36239e..c249799a42f 100644 --- a/tests/src/python/test_qgssettingstreenode.py +++ b/tests/src/python/test_qgssettingstreenode.py @@ -148,13 +148,12 @@ class TestQgsSettingsEntry(unittest.TestCase): self.assertEqual(type(proot.childSetting("python-implemented-setting")), QgsSettingsEntryEnumFlag) def test_get_node(self): - node = QgsSettingsTree.nodeCopy("gps") + node = QgsSettingsTree.node("gps") self.assertIsNotNone(node) count = len(node.childrenNodes()) - # cannot add a child node since the getter returns a const node.createChildNode("test") self.assertEqual(len(node.childrenNodes()), count + 1) - self.assertEqual(len(QgsSettingsTree.nodeCopy("gps").childrenNodes()), count) + self.assertEqual(len(QgsSettingsTree.node("gps").childrenNodes()), count + 1) if __name__ == '__main__':