Citirea și stocarea setărilor

De multe ori, pentru un plugin, este utilă salvarea unor variabile, astfel încât utilizatorul să nu trebuiască să le reintroducă sau să le reselecteze, la fiecare rulare a plugin-ului.

Aceste variabile pot fi salvate cu ajutorul Qt și QGIS API. Pentru fiecare variabilă, ar trebui să alegeți o cheie care va fi folosită pentru a accesa variabila — pentru culoarea preferată a utilizatorului ați putea folosi o cheie de genul ‘culoare_favorită’ sau orice alt șir semnificativ. Este recomandabil să folosiți o oarecare logică în denumirea cheilor.

Putem face diferența între mai multe tipuri de setări:

  • global settings — they are bound to the user at particular machine. QGIS itself stores a lot of global settings, for example, main window size or default snapping tolerance. This functionality is provided directly by Qt framework by the means of QSettings class. By default, this class stores settings in system’s “native” way of storing settings, that is — registry (on Windows), .plist file (on Mac OS X) or .ini file (on Unix). The QSettings documentation is comprehensive, so we will provide just a simple example:

    def store():
      s = QSettings()
      s.setValue("myplugin/mytext", "hello world")
      s.setValue("myplugin/myint",  10)
      s.setValue("myplugin/myreal", 3.14)
    
    def read():
      s = QSettings()
      mytext = s.value("myplugin/mytext", "default text")
      myint  = s.value("myplugin/myint", 123)
      myreal = s.value("myplugin/myreal", 2.71)
    

Al doilea parametru al metodei value() este opțional și specifică valoarea implicită, dacă nu există nici o valoare anterioară stabilită pentru setare.

  • project settings — vary between different projects and therefore they are connected with a project file. Map canvas background color or destination coordinate reference system (CRS) are examples — white background and WGS84 might be suitable for one project, while yellow background and UTM projection are better for another one. An example of usage follows:

    proj = QgsProject.instance()
    
    # store values
    proj.writeEntry("myplugin", "mytext", "hello world")
    proj.writeEntry("myplugin", "myint", 10)
    proj.writeEntry("myplugin", "mydouble", 0.01)
    proj.writeEntry("myplugin", "mybool", True)
    
    # read values
    mytext = proj.readEntry("myplugin", "mytext", "default text")[0]
    myint = proj.readNumEntry("myplugin", "myint", 123)[0]
    

După cum puteți vedea, metoda writeEntry() este folosită pentru toate tipurile de date, dar există mai multe metode pentru a seta înapoi setarea, iar cea corespunzătoare trebuie să fie selectată pentru fiecare tip de date.

  • map layer settings — these settings are related to a particular instance of a map layer with a project. They are not connected with underlying data source of a layer, so if you create two map layer instances of one shapefile, they will not share the settings. The settings are stored in project file, so if the user opens the project again, the layer-related settings will be there again. This functionality has been added in QGIS v1.4. The API is similar to QSettings — it takes and returns QVariant instances:

    # save a value
    layer.setCustomProperty("mytext", "hello world")
    
    # read the value again
    mytext = layer.customProperty("mytext", "default text")