This forum has been archived. All content is frozen. Please use KDE Discuss instead.

PyKDE Plasma.TreeView example

Tags: None
(comma "," separated)
bjneuman
Registered Member
Posts
1
Karma
0

PyKDE Plasma.TreeView example

Fri Mar 13, 2009 5:39 pm
Could someone provide, or point me to an example of the use of the PyKDE Plasma.TreeView? I would like to use the TreeView in my python plasmoid but am having absolutely no luck. A very simple python plasmoid that renders a TreeView would be great! Really, just a simple init() method that creates a TreeView with a static model, adds it to its layout so it is visible would be perfect.

Thanks a million!
Ben
User avatar
astromme
KDE Developer
Posts
6
Karma
0
OS

RE: PyKDE Plasma.TreeView example

Sat Mar 28, 2009 8:56 pm
bjneuman wrote:Could someone provide, or point me to an example of the use of the PyKDE Plasma.TreeView? I would like to use the TreeView in my python plasmoid but am having absolutely no luck. A very simple python plasmoid that renders a TreeView would be great! Really, just a simple init() method that creates a TreeView with a static model, adds it to its layout so it is visible would be perfect.

Thanks a million!
Ben



Try this:

TreeView/contents/code/main.py
Code: Select all
# -*- coding: utf-8 -*-
#
#   Copyright (C) 2009 Andrew Stromme
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License version 2,
#   or (at your option) any later version, as published by the Free
#   Software Foundation
#
#   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, write to the
#   Free Software Foundation, Inc.,
#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.kdecore import i18n
from PyKDE4.kdeui import *
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
import dbus

class TreeViewExample(plasmascript.Applet):
    def __init__(self,parent,args=None):
        plasmascript.Applet.__init__(self,parent)

    def init(self):
        self.setHasConfigurationInterface(False)
        self.setAspectRatioMode(Plasma.IgnoreAspectRatio)

        self.theme = Plasma.Svg(self)
        self.theme.setImagePath("widgets/background")
        self.setBackgroundHints(Plasma.Applet.DefaultBackground)

        self.layout = QGraphicsLinearLayout(Qt.Vertical, self.applet)

        self.label = Plasma.Label(self.applet)
        self.label.setText(i18n("TreeView Example:"))
        self.layout.addItem(self.label)

        self.stringlist = QStringList()
        for i in range(1,10):
          self.stringlist.append("Item " + str(i))

        self.model = QStringListModel(self.applet)
        self.model.setStringList(self.stringlist)
 
        treeview = Plasma.TreeView(self.applet)
        treeview.setModel(self.model)
        self.layout.addItem(treeview)

        self.lineedit = Plasma.LineEdit(self.applet)
        self.lineedit.nativeWidget().setClearButtonShown(True)
        self.lineedit.nativeWidget().setClickMessage(i18n("Add a new string..."))
        QObject.connect(self.lineedit, SIGNAL("returnPressed()"), self.addString)
        self.layout.addItem(self.lineedit)

        self.setLayout(self.layout)
        self.resize(250,400)

    def addString(self):
        self.stringlist.append(self.lineedit.text())
        self.lineedit.nativeWidget().clear()
        self.model.setStringList(self.stringlist)

def CreateApplet(parent):
    return TreeViewExample(parent)


TreeView/metadata.desktop
Code: Select all
[Desktop Entry]
Encoding=UTF-8
Name=Example TreeView Applet
Type=Service
ServiceTypes=Plasma/Applet
Icon=applications-system
X-Plasma-API=python
X-Plasma-MainScript=code/main.py
X-KDE-PluginInfo-Author=Andrew Stromme
X-KDE-PluginInfo-Email=astromme@chatonka.com
X-KDE-PluginInfo-Name=plasma_applet_treeview
X-KDE-PluginInfo-Version=1.0
X-KDE-PluginInfo-Website=http://plasma.kde.org/
X-KDE-PluginInfo-Category=Example
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true
screamnAbdab
Registered Member
Posts
5
Karma
0

RE: PyKDE Plasma.TreeView example

Tue Mar 31, 2009 2:24 am
Thanks. I'll give this a try and let you know how it goes.
Ben

astromme wrote:
bjneuman wrote:Could someone provide, or point me to an example of the use of the PyKDE Plasma.TreeView? I would like to use the TreeView in my python plasmoid but am having absolutely no luck. A very simple python plasmoid that renders a TreeView would be great! Really, just a simple init() method that creates a TreeView with a static model, adds it to its layout so it is visible would be perfect.

Thanks a million!
Ben



Try this:

TreeView/contents/code/main.py
Code: Select all
# -*- coding: utf-8 -*-
#
#   Copyright (C) 2009 Andrew Stromme
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License version 2,
#   or (at your option) any later version, as published by the Free
#   Software Foundation
#
#   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, write to the
#   Free Software Foundation, Inc.,
#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.kdecore import i18n
from PyKDE4.kdeui import *
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
import dbus

class TreeViewExample(plasmascript.Applet):
    def __init__(self,parent,args=None):
        plasmascript.Applet.__init__(self,parent)

    def init(self):
        self.setHasConfigurationInterface(False)
        self.setAspectRatioMode(Plasma.IgnoreAspectRatio)

        self.theme = Plasma.Svg(self)
        self.theme.setImagePath("widgets/background")
        self.setBackgroundHints(Plasma.Applet.DefaultBackground)

        self.layout = QGraphicsLinearLayout(Qt.Vertical, self.applet)

        self.label = Plasma.Label(self.applet)
        self.label.setText(i18n("TreeView Example:"))
        self.layout.addItem(self.label)

        self.stringlist = QStringList()
        for i in range(1,10):
          self.stringlist.append("Item " + str(i))

        self.model = QStringListModel(self.applet)
        self.model.setStringList(self.stringlist)
 
        treeview = Plasma.TreeView(self.applet)
        treeview.setModel(self.model)
        self.layout.addItem(treeview)

        self.lineedit = Plasma.LineEdit(self.applet)
        self.lineedit.nativeWidget().setClearButtonShown(True)
        self.lineedit.nativeWidget().setClickMessage(i18n("Add a new string..."))
        QObject.connect(self.lineedit, SIGNAL("returnPressed()"), self.addString)
        self.layout.addItem(self.lineedit)

        self.setLayout(self.layout)
        self.resize(250,400)

    def addString(self):
        self.stringlist.append(self.lineedit.text())
        self.lineedit.nativeWidget().clear()
        self.model.setStringList(self.stringlist)

def CreateApplet(parent):
    return TreeViewExample(parent)


TreeView/metadata.desktop
Code: Select all
[Desktop Entry]
Encoding=UTF-8
Name=Example TreeView Applet
Type=Service
ServiceTypes=Plasma/Applet
Icon=applications-system
X-Plasma-API=python
X-Plasma-MainScript=code/main.py
X-KDE-PluginInfo-Author=Andrew Stromme
X-KDE-PluginInfo-Email=astromme@chatonka.com
X-KDE-PluginInfo-Name=plasma_applet_treeview
X-KDE-PluginInfo-Version=1.0
X-KDE-PluginInfo-Website=http://plasma.kde.org/
X-KDE-PluginInfo-Category=Example
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true

umaga
Registered Member
Posts
1
Karma
0

RE: PyKDE Plasma.TreeView example

Fri Apr 10, 2009 1:46 pm
Hi, i tried your script about treeview and i found it very usefull for me.
I have a question now:
How can I make treeview sending signal itemclicked or signal itemdoubleclicked?
I found that treeview doesn't send any signal about click event. How can i do that?
it's possible or do i need to use another widget.

Thank a lot

Last edited by umaga on Fri Apr 10, 2009 1:54 pm, edited 1 time in total.


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell