Skip to content
Snippets Groups Projects
Select Git revision
  • 16f997ddf0b9b02016a65076dda59911bff2b961
  • master default protected
  • release/202005
  • release/202001
  • release/201912
  • release/201911
  • release/201910
  • release/201908
  • release/201906
  • release/201905
  • release/201904
  • release/201903
  • release/201902
  • release/201901
  • release/201812
  • release/201811
  • release/201808
  • releases/beta1
  • packaging
  • native
  • release-0.2.x
  • 1.0.0
  • 0.2.0
  • 0.1.1
  • 0.1.0
25 results

windowscontactbackend.h

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ViewManager.qml 3.22 KiB
    /*
     * Copyright (C) 2023 Savoir-faire Linux Inc.
     *
     * This program is free software; you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation; either version 3 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program.  If not, see <https://www.gnu.org/licenses/>.
     */
    
    import QtQuick
    
    QtObject {
        id: viewManager
    
        // A map of path strings to view objects.
        property variant views: ({})
        // A map of view names to path strings.
        property variant viewPaths: ({})
        // The number of views.
        property int nViews: 0
    
        // Destroy all views.
        function destroyAllViews() {
            for (var path in views) {
                destroyView(path)
            }
        }
    
        function createView(path, parent=null, cb=null, props={}) {
            if (views[path] !== undefined) {
                // an instance of <path> already exists
                if (cb !== null) {
                    cb(views[path])
                }
                return views[path]
            }
    
            const component = Qt.createComponent(Qt.resolvedUrl(path))
            if (component.status === Component.Ready) {
                const obj = component.createObject(parent, props)
                if (obj === null) {
                    print("error creating object")
                    return null
                }
                views[path] = obj
                // Set the view name to the object name if it has one.
                const viewName = obj.objectName.toString() !== '' ?
                            obj.objectName :
                            path.replace(/^.*[\\\/]/, '').replace(/\.[^/.]+$/, "")
                viewPaths[viewName] = path
                nViews = Object.keys(views).length
                if (cb !== null) {
                    cb(obj)
                }
                return views[path]
            }
            print("error creating component", path)
            console.error(component.errorString())
            Qt.exit(1)
            return null
        }
    
        function destroyView(path) {
            if (views[path] === undefined) {
                print(path, "instance does not exist", Object.keys(views))
                return false
            }
            views[path].destroy()
            views[path] = undefined
            // QObject::destroy is queued, and we can't connect to its completion,
            // so we queue the resulting mutation to our view storage.
            Qt.callLater(function() {
                delete views[path]
                // Remove the view name from the viewPaths map.
                for (var viewName in viewPaths) {
                    if (viewPaths[viewName] === path) {
                        delete viewPaths[viewName]
                        break
                    }
                }
                nViews = Object.keys(views).length
            })
            return true
        }
    
        function hasView(viewName) {
            return nViews && viewPaths[viewName] !== undefined
        }
    
        function getView(viewName) {
            return views[viewPaths[viewName]] || null
        }
    }