[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-panel-devel
Subject:    Unable to load images for pushbuttons from relative path
From:       Eitzenberger Thomas <eth () gmx ! at>
Date:       2009-03-31 8:10:43
Message-ID: 200903311010.43331.eth () gmx ! at
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hi,

someone on #plasma told me i could ask for help via this emailaddr  too....

I need a pushbutton in the parley applet to trigger playing the sound file

The button shall show up green if the url is valid and orange if not, grey if 
checking/ongoing

Up to now I was only able to get the pushbutton showing up with the images 
using absolute URLS

I tried with "widgets/play_green.png" "widgets/play_green" and also tried to 
embedd teh images in to the svg file but there I am stranded as the PushButton 
doesnt want a QPixmap but a path QString.....

Any help would be appreciated...

For discussions you can find my also on skype (user EItzenberger Thomas) and 
on MSN (user eth@gmx.at)

Thanx for any feedback

[Attachment #5 (text/html)]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" \
"http://www.w3.org/TR/REC-html40/strict.dtd"><html><head><meta name="qrichtext" \
content="1" /><style type="text/css">p, li { white-space: pre-wrap; \
}</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; \
font-weight:400; font-style:normal;">Hi,<br> <p style="-qt-paragraph-type:empty; \
margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; \
-qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br></p>someone on #plasma \
told me i could ask for help via this emailaddr  too....<br> <p \
style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; \
margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br></p>I \
need a pushbutton in the parley applet to trigger playing the sound file<br> <p \
style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; \
margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br></p>The \
button shall show up green if the url is valid and orange if not, grey if \
checking/ongoing<br> <p style="-qt-paragraph-type:empty; margin-top:0px; \
margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; \
text-indent:0px; -qt-user-state:0;"><br></p>Up to now I was only able to get the \
pushbutton showing up with the images using absolute URLS<br> <p \
style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; \
margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br></p>I \
tried with "widgets/play_green.png" "widgets/play_green" and also tried to embedd teh \
images in to the svg file but there I am stranded as the PushButton doesnt want a \
QPixmap but a path QString.....<br> <p style="-qt-paragraph-type:empty; \
margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; \
-qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br></p>Any help would be \
appreciated...<br> <p style="-qt-paragraph-type:empty; margin-top:0px; \
margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; \
text-indent:0px; -qt-user-state:0;"><br></p>For discussions you can find my also on \
skype (user EItzenberger Thomas) and on MSN (user eth@gmx.at)<br> <p \
style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; \
margin-right:0px; -qt-block-indent:0; text-indent:0px; \
-qt-user-state:0;"><br></p>Thanx for any feedback</p></body></html>


["parley_plasma_cardet.svg" (image/svg+xml)]
["parley_plasma.cpp" (text/x-c++src)]

/***************************************************************************

    Copyright 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#include "parley_plasma.h"

#include <KDialog>
#include <KConfigGroup>
#include <KConfigDialog>
#include <KFontDialog>
#include <KColorDialog>
#include <KDebug>

#include <QUrl>
#include <QGraphicsSceneMouseEvent>
#include <QPixmap>
#include "phonon/mediaobject.h"
#include "phonon/audiooutput.h"
#include <QCryptographicHash>

ParleyPlasma::ParleyPlasma(QObject *parent, const QVariantList &args)
    : Plasma::Applet(parent, args),
    m_label1(0),
    m_label2(0),
    m_font(QFont("Helvetica", 5)),
    m_soundurl("")
{
    KGlobal::locale()->insertCatalog("parley");
    resize(256,160);
    setHasConfigurationInterface(true);
    setAcceptDrops(false);
    setAcceptsHoverEvents(true);
    setAspectRatioMode(Plasma::KeepAspectRatio);
    setBackgroundHints(NoBackground);

    m_theme = new Plasma::Svg(this);
    m_theme->setImagePath("widgets/parley_plasma_cardet");
    //TODO do we need true here if we have the play button images in it too ?
    m_theme->setContainsMultipleImages(false);
    m_theme->resize(size());
}

void ParleyPlasma::init()
{
    //TODO what is this for ?
    m_theme->size().height();

    KConfigGroup cg = config();
    m_updateInterval = cg.readEntry("updateInterval", 10000);
    m_solutionType = cg.readEntry("Solution", 0);

    m_lang1 = cg.readEntry("Top Language", 0);
    m_lang2 = cg.readEntry("Bottom Language", 1);

    m_engine = dataEngine("parley");

    m_label1 = new QGraphicsTextItem("Welcome to parley", this);
    m_label2 = new QGraphicsTextItem("--------------------", this);
    m_label1->setPos( m_theme->elementRect( "translation1" ).topLeft() );
    m_label2->setPos( m_theme->elementRect( "translation2" ).topLeft() );
    m_label1->setFont(cg.readEntry("font",m_font));
    m_label2->setFont(cg.readEntry("font",m_font));
    
    // the push button to lookup a sound file and play it via phonon
    //TODO include pngs into svgz and load it from there
    m_play =  new Plasma::PushButton(this);
    kDebug() << "IMAGE:" << m_theme->pixmap("play_green").size();
    m_play->setImage("/home/kde/kde/src/KDE/kdeedu/parley/plasmoid/play_red.png");
    m_play->setPos(m_theme->elementRect( "playbtn" ).topLeft() );
    connect(m_play, SIGNAL(clicked()), this, SLOT(sayWord()));
    
    m_sourceFile.setUrl(cg.readEntry("File Name"));
    if (m_sourceFile.isEmpty()) {
        kDebug() << "open file from parleyrc";
        KConfig parleyConfig("parleyrc");
        kDebug() << parleyConfig.groupList();
        KConfigGroup recentFilesGroup( &parleyConfig, "Recent Files" );
        // take the last file, but there are File1..n and Name1..n entries..
        m_sourceFile = recentFilesGroup.readEntry( \
recentFilesGroup.keyList().value(recentFilesGroup.keyList().count()/2-1), QString() \
);  kDebug() << "open file: " << m_sourceFile;
    }
    m_engine->connectSource(m_sourceFile.url(), this, m_updateInterval);
    
    //TODO why the heck is the first vocabulary shown in such a large font - resize \
should adapt the sizes but doesnt ???? fixed AFTER first dataupdate call :O(  \
resizeItems(); }


void ParleyPlasma::constraintsEvent(Plasma::Constraints constraints)
{
    if (constraints & Plasma::FormFactorConstraint) {
        setBackgroundHints(NoBackground);
    }

    if (constraints & Plasma::SizeConstraint) {
        double aspect = 256.0/160.0; // original aspect ratio
        if (formFactor() == Plasma::Horizontal) {
            // We have a fixed height, set some sensible width
            setMinimumWidth(contentsRect().height() * aspect);
        } else if (formFactor() == Plasma::Vertical) {
            // We have a fixed width, set some sensible height
            setMinimumHeight((int)contentsRect().width() / aspect);
        }
        resizeItems();
    }
}

ParleyPlasma::~ParleyPlasma()
{
}

void ParleyPlasma::dataUpdated(const QString& source, const Plasma::DataEngine::Data \
&data) {
    kDebug() << "data updated" << source << data;

    m_languages = data.keys();

    if ( m_label1) {
        m_label1->setPlainText(data[m_languages.value(m_lang1)].toString());
        //check for availability of sound files on wiktionary
        //TODO include pngs into svgz and load it from there
        m_play->setImage("/home/kde/kde/src/KDE/kdeedu/parley/plasmoid/play_grey.png");
                
        QString word = data[m_languages.value(m_lang1)].toString().toLower();
        m_soundurl.clear();
        lookupVoiceFileFor(word, "En-us");
        m_label2->setPlainText(data[m_languages.value(m_lang2)].toString());
        resizeItems();
    }
}

void ParleyPlasma::resizeItems() {
        m_label1->setPos(QPointF(m_theme->elementSize( "play_grey" ).width(), 0.0) + \
m_theme->elementRect( "translation1" ).topLeft() );  m_label1->setFont( m_font );
        double scale = qMin(
            m_theme->elementRect( "translation1" \
                ).width()/m_label1->boundingRect().width(),
            m_theme->elementRect( "translation1" \
).height()/m_label1->boundingRect().height());  \
m_label1->setTransform(QTransform().scale(scale, scale));

        m_label2->setPos( m_theme->elementRect( "translation2" ).topLeft() );
        m_label2->setFont( m_font );
        scale = qMin(
            m_theme->elementRect( "translation2" \
                ).width()/m_label2->boundingRect().width(),
            m_theme->elementRect( "translation2" \
).height()/m_label2->boundingRect().height());  \
m_label2->setTransform(QTransform().scale(scale, scale));  if (m_solutionType == \
Hover) {  m_label2->hide();
        }

        m_play->setPos(m_theme->elementRect( "play_grey" ).topLeft() );
        scale = qMin(
            m_theme->elementSize( "play_grey" \
                ).width()/m_play->boundingRect().width(),
            m_theme->elementSize( "play_grey" \
).height()/m_play->boundingRect().height());  \
m_play->setTransform(QTransform().scale(scale, scale)); }


void ParleyPlasma::setContentSize(const QSizeF& size)
{
    m_size = size;
}

QSizeF ParleyPlasma::contentSizeHint() const
{
    return m_size;
}

void ParleyPlasma::paintInterface(QPainter *p,
                           const QStyleOptionGraphicsItem *option,
                           const QRect &contentsRect)
{
    Q_UNUSED(option);

    QSizeF boundSize = geometry().size();
    m_theme->resize(boundSize);
    m_theme->paint(p, contentsRect, "Card");
}

void ParleyPlasma::createConfigurationInterface(KConfigDialog * parent)
{
    QWidget *widget = new QWidget();
    ui.setupUi(widget);
    parent->addPage(widget, parent->windowTitle(), icon());
    parent->setButtons( KDialog::Ok | KDialog::Cancel | KDialog::Apply );
    connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
    connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));

    connect( ui.fontSelectButton, SIGNAL(clicked()), this, SLOT(showFontSelectDlg()) \
);  connect( ui.updateIntervalSpinBox, SIGNAL(valueChanged(int)), this, \
SLOT(updateSpinBoxSuffix()) );

    ui.updateIntervalSpinBox->setValue(m_updateInterval/1000);
    emit updateSpinBoxSuffix();
    KConfigGroup cg = config();

    ui.filechooser->setUrl(m_sourceFile);
    ui.filechooser->setFilter(i18n("*.kvtml|Vocabulary Collections"));
    connect(ui.filechooser, SIGNAL(urlSelected (const KUrl &)), this, \
SLOT(urlSelected (const KUrl &)));  ui.language1->addItems(m_languages);
    ui.language2->addItems(m_languages);
    ui.language1->setCurrentIndex(m_lang1);
    ui.language2->setCurrentIndex(m_lang2);

    switch (m_solutionType) {
    case Hover:
        ui.solutionOnHover->setChecked(true);
        break;
    case Always:
        ui.solutionAlways->setChecked(true);
        break;
    }
}

void ParleyPlasma::updateSpinBoxSuffix()
{
    ui.updateIntervalSpinBox->setSuffix(QString(" ") + i18np("second", "seconds", \
ui.updateIntervalSpinBox->value())); }

//TODO really use lang variable and not assume its always En-us
//TODO also check for -noun, -verb and for other dialects too En-uk, or De-at, \
En-ca..... void ParleyPlasma::lookupVoiceFileFor(QString word, QString lang) {
    if (word.startsWith("to ")) {
      word = word.mid(3);
      kDebug() << "Searching for verb " << word << "with 'to '";
    }

    kDebug() << "LANG:" << lang;

    // mediawiki stores the files under commons in separate sub folders due to the \
enormous number of files  // the filesys is not able to cope with millions of files \
in one folder, so the rule is take the md5 hex hash  // of the filename (all \
lowercase, space replaced by "_") and sore it under /md5[0]/md5[0:1]/filename  \
QString filename = "En-us-" + word + ".ogg";  QCryptographicHash md5( \
QCryptographicHash::Md5 );  QString md5str = QString(filename);
    QByteArray ba = QByteArray(md5str.toUtf8());
    md5.addData (ba.data(), ba.size());
    md5str = md5.result().toHex();
    m_soundurl.setUrl("http://upload.wikimedia.org/wikipedia/commons/" + md5str[0] + \
"/" + md5str[0] + md5str[1] + "/" + filename);

    // start the transferjob. As mediawiki does not respond with 404 but sends an \
error page instead   // its no way to do a stat job and check for error but i have to \
do the copy job and check for is error page  kDebug() << "checking voice file: '" << \
m_soundurl <<"'";  KIO::TransferJob *job = KIO::get(KUrl(m_soundurl)); 
    connect(job,  SIGNAL(result(KJob*)),
          this, SLOT(slotResult(KJob*)));
    connect(job, SIGNAL(infoMessage(KJob*, const QString&, const QString&)), this, \
SLOT(infomsg(KJob*, const QString&, const QString&))); }

// just for debug reasons
void ParleyPlasma::infomsg(KJob*, const QString& msg, const QString&) {
  kDebug() << "HTTP STAT Info:" << msg;
}

// checks if transferred page is an error page and colorizes the button accordingly
void ParleyPlasma::slotResult(KJob *job) {
    if (((KIO::TransferJob*)job)->isErrorPage()) {
        kDebug() << "check NOT ok";
        m_soundurl.clear();
        //TODO include pngs into svgz and load it from there
        m_play->setImage("/home/kde/kde/src/KDE/kdeedu/parley/plasmoid/play_red.png");
  } else {
        kDebug() << "CHECK OK";
        //TODO include pngs into svgz and load it from there
        m_play->setImage("/home/kde/kde/src/KDE/kdeedu/parley/plasmoid/play_green.png");
  }
}

// play the off file via phonon if m_soundurl is not empty
void ParleyPlasma::sayWord()
{
    if (!m_soundurl.isEmpty()) {
        Phonon::MediaObject *soundfile = new Phonon::MediaObject(this);
        Phonon::AudioOutput* audioOutput = new \
Phonon::AudioOutput(Phonon::MusicCategory);  Phonon::createPath(soundfile, \
audioOutput);  soundfile->setCurrentSource(m_soundurl);
        kDebug() << "PHONON playing: '" << m_soundurl <<"'";
        soundfile->play();
    } else {
        kDebug() << "Sound URL is EMPTY: " << m_soundurl;
    }
}

void ParleyPlasma::urlSelected (const KUrl &file)
{
    // turn off old engine
    m_engine->disconnectSource(m_sourceFile.url(), this);
    // connect to new file
    m_sourceFile = ui.filechooser->url();
    m_engine->connectSource(m_sourceFile.url(), this, m_updateInterval);
    // get language data
    Plasma::DataEngine::Data data = m_engine->query(m_sourceFile.url());
    m_languages = data.keys();
    // update language selection
    ui.language1->clear();
    ui.language2->clear();
    ui.language1->addItems(m_languages);
    ui.language2->addItems(m_languages);
    // just select the first languages - better than nothing ;)
    ui.language1->setCurrentIndex(0);
    ui.language2->setCurrentIndex(1);
}

void ParleyPlasma::showFontSelectDlg()
{
    KFontDialog::getFont(m_font);
}

void ParleyPlasma::configAccepted()
{
    prepareGeometryChange();

    KConfigGroup cg = config();
    cg.writeEntry("font", m_font);
    m_label1->setFont(m_font);
    m_label2->setFont(m_font);
    m_updateInterval = ui.updateIntervalSpinBox->value()*1000;
    cg.writeEntry("updateInterval", m_updateInterval);

    m_engine->disconnectSource(m_sourceFile.url(), this);
    m_sourceFile = ui.filechooser->url();
    cg.writeEntry("File Name", m_sourceFile);

    m_solutionType = Hover;
    if (ui.solutionAlways->isChecked()) {
        m_solutionType = Always;
        m_label2->show();
    }
    cg.writeEntry("Solution", m_solutionType);

    m_lang1 = ui.language1->currentIndex();
    m_lang2 = ui.language2->currentIndex();

    cg.writeEntry("Top Language", m_lang1);
    cg.writeEntry("Bottom Language", m_lang2);


    m_engine->connectSource(m_sourceFile.url(), this, m_updateInterval);

    kDebug() << "open:" << m_sourceFile;

    emit configNeedsSaving();
}

void ParleyPlasma::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
{
    Plasma::Applet::hoverEnterEvent(event);
    m_label2->show();
}

void ParleyPlasma::hoverLeaveEvent(QGraphicsSceneHoverEvent  * event)
{
    Plasma::Applet::hoverLeaveEvent(event);
    m_label2->hide();
}

#include "parley_plasma.moc"


["parley_plasma.h" (text/x-chdr)]

/***************************************************************************

    Copyright 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#ifndef PARLEYPLASMA_H
#define PARLEYPLASMA_H

#include <Plasma/Applet>
#include <Plasma/Svg>
#include <plasma/dataengine.h>
#include <plasma/plasma.h>

#include <QGraphicsTextItem>
#include "plasma/widgets/pushbutton.h"
#include <kio/job.h>
#include <QGridLayout>

#include "ui_config.h"

class KDialog;
class QFont;
class QSizeF;
class KConfigDialog;

class ParleyPlasma : public Plasma::Applet
{
    Q_OBJECT

    public:
        ParleyPlasma(QObject *parent, const QVariantList &args);
        ~ParleyPlasma();
        void init();
        void setContentSize(const QSizeF& size);
        QSizeF contentSizeHint() const;
        void paintInterface(QPainter *painter,
                            const QStyleOptionGraphicsItem *option,
                            const QRect& contentsRect);
        void constraintsEvent(Plasma::Constraints constraints);

    public slots:
        void createConfigurationInterface(KConfigDialog *parent);
        void configAccepted();
        void showFontSelectDlg();
        void dataUpdated(const QString &name, const Plasma::DataEngine::Data &data);
        // in the config dialog a file was selected
        void urlSelected (const KUrl &file);
        void sayWord();
        void infomsg(KJob*, const QString&, const QString&);
	void slotResult(KJob *job);

    protected:
        virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event );
        virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event );
	void lookupVoiceFileFor(QString word, QString lang);
        void resizeItems();

    private:
        enum ShowSolution {
            Hover = 0,
            Always
        };
        Plasma::Svg *m_theme;
        QGraphicsTextItem *m_label1;
        QGraphicsTextItem *m_label2;

        Ui::config ui;

        QFont m_font;
        QSizeF m_size;

        Plasma::PushButton *m_play;
        QUrl m_soundurl;

        Plasma::DataEngine* m_engine;
        KUrl m_sourceFile;
        QStringList m_languages;
        int m_solutionType;
        int m_lang1;
        int m_lang2;
        int m_updateInterval;
    private slots:
        void updateSpinBoxSuffix();
};

K_EXPORT_PLASMA_APPLET(parley, ParleyPlasma)

#endif

["play_grey.png" (image/png)]
["CMakeLists.txt" (text/x-cmake)]

project(plasma-parley)
add_subdirectory( engine )

include_directories( ${CMAKE_SOURCE_DIR}/libkdeedu/keduvocdocument/  plasma/widget )
set(parley_plasma_SRCS
    parley_plasma.cpp
    )

kde4_add_ui_files(parley_plasma_SRCS config.ui)

kde4_add_plugin(plasma_applet_parley ${parley_plasma_SRCS})
target_link_libraries(plasma_applet_parley
    plasma ${KDE4_KIO_LIBS}
    keduvocdocument
    phonon
    )

install(
    TARGETS plasma_applet_parley
    DESTINATION ${PLUGIN_INSTALL_DIR}
    )

install(
    FILES plasma_parley.desktop
    DESTINATION ${SERVICES_INSTALL_DIR}
    )

install(
    FILES parley_plasma_card.svg parley_plasma_cardet.svg play_grey.png play_red.png play_green.png 
    DESTINATION ${DATA_INSTALL_DIR}/desktoptheme/default/widgets/
    )

["play_green.png" (image/png)]
["play_red.png" (image/png)]

_______________________________________________
Plasma-devel mailing list
Plasma-devel@kde.org
https://mail.kde.org/mailman/listinfo/plasma-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic