Reply to topic

Plasma::PopupApplet Class how to display the icon in the panel?

User avatar darkman
Registered Member
Posts
13
Karma
0
OS
Hello everyone,
i'm trying to make a menu for kde 4.2. I read the api for the kdelibs 4.x and i found the PopupApplet class. I think this is the correct class (also raptor-menu use this!)
In the description of the method setPopupIcon(const QString &iconName) there's written:

* icon the icon that has to be displayed when the applet is in a panel.

so i set-up an icon for my pop-up applet but i cannot see any icon in the panel.
how can i do this?

i also took a look to the raptor-menu's code but i haven't understood yet how to do it.

can anyone help me?
thanks in advance


My belly is full of theory but when I play I forget it all - Al di Meola
--------------
darkman, proud to be a member of KDE forums since 2008-Nov.
User avatar marcin.baszczewski
Registered Member
Posts
8
Karma
0
Try something like this (my example - painting icon):

Code: Select all
help::help(QObject *parent, const QVariantList &args)  :
  Plasma::PopupApplet(parent, args)
{
    //...
    setPopupIcon(QIcon());
    int iconSize = IconSize(KIconLoader::Desktop);
    resize(iconSize*2 , iconSize*2 );
    setAspectRatioMode(Plasma::ConstrainedSquare );
    setPopupIcon(QIcon());
    //we load some icon
    m_icon= new KIcon("kmix");
    //create extender
    m_extender = new Plasma::ExtenderItem(extender());
    m_extender->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
    initExtender(m_extender);
}

void help::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
{
    Q_UNUSED( option );
    //draw our icon!!
    p->setRenderHint(QPainter::SmoothPixmapTransform);
    p->setRenderHint(QPainter::Antialiasing);
    int iconSize = IconSize(KIconLoader::Desktop);
    QPixmap pixmap = m_icon->pixmap(QSize(iconSize, iconSize));
    p->drawPixmap(contentsRect, pixmap);
}
void help::initExtender(Plasma::ExtenderItem *item)
{
    QGraphicsWidget       *controls    = new QGraphicsWidget(item);
    QGraphicsLinearLayout   *layout    = new QGraphicsLinearLayout(controls);
    layout->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    layout->setOrientation(Qt::Vertical);

    Plasma::Label *label1 = new Plasma::Label(controls);
    label1->setText(i18n("help"));
    label1->nativeWidget()->setWordWrap(false);
    layout->addItem(label1);
   
    controls->setLayout(layout);
    item->setWidget(controls);
    item->setTitle(i18n("..."));
}


By the way: you can learn many things with source code of existing plasmoids.

Last edited by marcin.baszczewski on Wed Jan 07, 2009 9:23 pm, edited 1 time in total.


Image
User avatar bcooksley
Administrator
Posts
19765
Karma
87
OS
The easiest way to have an icon is to enable KIcon to load it, by drawing it in an application such as Krita or Inkscape, and installing it alongside your Plasmoid.

Once this is done, you can simply call setPopupIcon()
Note that icon names cannot contain spaces.

@marcin.baszczewski: This method will only work if your Plasmoid is designed to be used as an extender. If you just want your Plasmoid to behave in the Panel, then that method will not work, unless you draw it in a QPixmap, convert to QIcon, and then set it.


KDE Sysadmin
[img]http://forum.kde.org/content/bcooksley_sig.png[/img]
ezeaguerre
Registered Member
Posts
3
Karma
0
OS
Hi, I'm having a similar problem (I think it's not necessary to open another thread right?):

I have made a really simple plasmoid... I want to display a button in the desktop and an icon in the panel... but I can't make it work..

Code: Select all
#include
#include
#include "miapplet.h"

class MiPopup : public Plasma::PopupApplet {
  Q_OBJECT
  public:
    MiPopup ( QObject *, const QVariantList & );
    QWidget *widget () { return m_widget; }
  private:
    QWidget *m_widget;
};

MiPopup::MiPopup ( QObject *parent, const QVariantList &args )
  : Plasma::PopupApplet ( parent, args )
{
  m_widget = new QPushButton ( "Hello :-)" );
  setPopupIcon ( "plasma" );
}


The plasmoid shows the pushbutton in the desktop and in the panel... but I want an icon in the panel :-(
What I'm missing?

Thanks!
User avatar bcooksley
Administrator
Posts
19765
Karma
87
OS
You need to implement graphicsWidget() to return the QGraphicsWidget you want the Popup to display. This will also be used when your Applet is on the desktop.


KDE Sysadmin
[img]http://forum.kde.org/content/bcooksley_sig.png[/img]
ezeaguerre
Registered Member
Posts
3
Karma
0
OS
Hi!

I was sure the documentation said I could reimplement widget() or graphicsWiget(), that's why I chose widget()... anyway, it doesn't work with graphicsWidget() either...

Code: Select all
#include
#include

class MiPopup : public Plasma::PopupApplet {
  Q_OBJECT
  public:
    MiPopup ( QObject *, const QVariantList & );
    QGraphicsWidget *graphicsWidget () { return m_widget; }
  private:
    Plasma::Label *m_widget;
};

MiPopup::MiPopup ( QObject *parent, const QVariantList &args )
  : Plasma::PopupApplet ( parent, args )
{
  m_widget = new Plasma::Label ( this );
  m_widget->setText ( i18n ( "Hello" ) );
  setPopupIcon ( "plasma" );
}


The label is shown in the desktop and in the panel... why it doesn't get replaced by an icon? It's driving me crazy :'(

Thanks!
User avatar bcooksley
Administrator
Posts
19765
Karma
87
OS
You might also be missing the init() function Plasma uses, but this should work.
Try changing this:
Code: Select all
MiPopup::MiPopup ( QObject *parent, const QVariantList &args )
  : Plasma::PopupApplet ( parent, args )


To this as well:
Code: Select all
MiPopup::MiPopup ( QObject *parent, const QVariantList &args )
  : Plasma::PopupApplet ( parent, args )
  , m_widget(0)

Last edited by bcooksley on Thu Jan 22, 2009 10:05 pm, edited 1 time in total.


KDE Sysadmin
[img]http://forum.kde.org/content/bcooksley_sig.png[/img]
ezeaguerre
Registered Member
Posts
3
Karma
0
OS
No, it didn't work :-(
I've being reading the code and found this: http://websvn.kde.org/trunk/KDE/kdelibs ... iew=markup

Code: Select all
/Applet on desktop
        if (icon && !icon->icon().isNull() && ((f != Plasma::Vertical && f != Plasma::Horizontal) ||
            ((f == Plasma::Vertical && containmentSize.width() >= minimum.width()) ||
             (f == Plasma::Horizontal && containmentSize.height() >= minimum.height())))) {

these 2 checks: containmentSize.width() >= minimum.width() and containmentSize.height() >= minimum.height() made me believe I had to set a minimum width... and here: http://websvn.kde.org/trunk/KDE/kdeplas ... iew=markup

Code: Select all
// So we don't show in panel
m_lcd->setMinimumSize(m_lcd->preferredSize() / 2);


I've confirmed it... it seems I need to set a minimum size to get it work... so I did it and now it works, but a new problem arose... the label would still be visible on the desktop, and the popup showed me that part of the desktop instead of the label... like this: http://img509.imageshack.us/img509/8755 ... ot1ll4.jpg

Finally I realized all I needed was a proper layout, and now it looks like this: http://img232.imageshack.us/img232/7200 ... ot2ay1.jpg

And this is the code:

Code: Select all
#include
#include
#include
#include

class MiPopup : public Plasma::PopupApplet {
  Q_OBJECT
  public:
    MiPopup ( QObject *, const QVariantList & );
    QGraphicsWidget *graphicsWidget () { return m_widget; }
  private:
    QGraphicsWidget *m_widget;
};

MiPopup::MiPopup ( QObject *parent, const QVariantList &args )
  : Plasma::PopupApplet ( parent, args )
{
  m_widget = new QGraphicsWidget;

  Plasma::Label *lbl = new Plasma::Label;
  lbl->setText ( i18n ( "Hello" ) );

  QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
  layout->addItem ( lbl );
  m_widget->setLayout ( layout );

  m_widget->setMinimumSize ( 100, 100 );
  setPopupIcon ( "plasma" );
}


Cheers!! :-)

 
Reply to topic

Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], funkyskywalker, Google [Bot], irvingarrietavazquez, jeffgila, jmacleod, koffeinfriedhof, richmaxw, Sogou [Bot], Stephen Leibowitz, Yahoo [Bot]