Development/Tutorials/Icons: Difference between revisions

    From KDE TechBase
    No edit summary
    No edit summary
    Line 3: Line 3:
    When trying to add an icon to your program, you will realize there is a lot of wording against common sense. You will see the classes, KIconLoader, KIconEngine, QIconEngine, QIcon and KIcon that all seem to mean the same thing, an icon.
    When trying to add an icon to your program, you will realize there is a lot of wording against common sense. You will see the classes, KIconLoader, KIconEngine, QIconEngine, QIcon and KIcon that all seem to mean the same thing, an icon.


    = Analyzing the KApplications =
    Maybe you have already searched for yourself how a KDE application finds its icons. You will not be successful like this:
    Maybe you have already searched for yourself how a KDE application finds its icons. You will not be successful like this:
      # [http://www.linuxintro.org/wiki/Strace#Search_for_a_syscall strace -e open] ktimetracker 2>&1 | grep icon
      # [http://www.linuxintro.org/wiki/Strace#Search_for_a_syscall strace -e open] ktimetracker 2>&1 | grep icon
    Line 19: Line 20:
      [...]
      [...]


    = Your KDE project =
    If you have a KDE project and want to use icons within it, and want that these icons install seamlessly, you will have to name the icons after the project. In the following we look at a project named ''krep''. The important lines in CMakeLists.txt are the ones printed bold below:
    If you have a KDE project and want to use icons within it, and want that these icons install seamlessly, you will have to name the icons after the project. In the following we look at a project named ''krep''. The important lines in CMakeLists.txt are the ones printed bold below:
      PROJECT( krep )
      PROJECT( krep )
    Line 46: Line 48:


    = How to test it =
    = How to test it =
    * delete a file
      rm /usr/local/share/icons/hicolor/22x22/apps/krep.png  
      rm /usr/local/share/icons/hicolor/22x22/apps/krep.png  
    * delete your cache
      rm -rf /var/tmp/kdecache-''user''/
      rm -rf /var/tmp/kdecache-''user''/
    * relogin
    using this test case I could remove one icon after the other, 22x22, 64x64, 48x48, but when I removed 32x32, the icon disappeared from my application. This was the last icon that existed.

    Revision as of 12:31, 28 August 2011

    Template:Stub

    When trying to add an icon to your program, you will realize there is a lot of wording against common sense. You will see the classes, KIconLoader, KIconEngine, QIconEngine, QIcon and KIcon that all seem to mean the same thing, an icon.

    Analyzing the KApplications

    Maybe you have already searched for yourself how a KDE application finds its icons. You will not be successful like this:

    # strace -e open ktimetracker 2>&1 | grep icon
    open("/usr/lib64/libkemoticons.so.4", O_RDONLY) = 3
    #
    

    You see, a KDE application (in this case ktimetracker) does not open icon files or even look for them. However, it spawns a process to look for these icons:

    linux-qgla:~/repos/kdepim/ktimetracker # strace -ffe open ktimetracker 2>&1 | grep icon
    open("/usr/lib64/libkemoticons.so.4", O_RDONLY) = 3
    [pid  3457] open("/root/.icons/DMZ/cursors/left_ptr", O_RDONLY) = -1 ENOENT (No such file or directory)
    [pid  3457] open("/root/.icons/DMZ/index.theme", O_RDONLY) = -1 ENOENT (No such file or directory)
    [pid  3457] open("/usr/share/icons/DMZ/cursors/left_ptr", O_RDONLY) = 10
    [pid  3457] open("/var/tmp/kdecache-root/icon-cache.kcache", O_RDWR|O_CREAT|O_CLOEXEC, 0666) = 10
    [pid  3457] open("/var/tmp/kdecache-root/kpc/kde-icon-cache.data", O_RDONLY|O_CLOEXEC) = 10
    [pid  3457] open("/var/tmp/kdecache-root/kpc/kde-icon-cache.index", O_RDONLY|O_CLOEXEC) = 10
    [...]
    

    Your KDE project

    If you have a KDE project and want to use icons within it, and want that these icons install seamlessly, you will have to name the icons after the project. In the following we look at a project named krep. The important lines in CMakeLists.txt are the ones printed bold below:

    PROJECT( krep )
    FIND_PACKAGE( KDE4 REQUIRED )
    INCLUDE_DIRECTORIES( ${KDE4_INCLUDES} . )
    
    SET(krepSources main.cpp krep.cpp krep.h)
    
    qt4_add_dbus_adaptor( krepSources 
                          org.kde.krep.xml
                          krep.h
                          krep
                          mainadaptor
                          MainAdaptor )
    
    kde4_add_ui_files( krepSources krepui.ui )
    
    kde4_add_app_icon( krepSources "hi*-app-krep.png" )
    
    KDE4_ADD_EXECUTABLE( krep ${krepSources} )
    
    TARGET_LINK_LIBRARIES( krep ${KDE4_KPARTS_LIBS} )
    
    install( TARGETS krep ${INSTALL_TARGETS_DEFAULT_ARGS} )
    
    kde4_install_icons( ${ICON_INSTALL_DIR} )
    

    How to test it

    • delete a file
    rm /usr/local/share/icons/hicolor/22x22/apps/krep.png 
    
    • delete your cache
    rm -rf /var/tmp/kdecache-user/
    
    • relogin

    using this test case I could remove one icon after the other, 22x22, 64x64, 48x48, but when I removed 32x32, the icon disappeared from my application. This was the last icon that existed.