Commit 798896d2 authored by Tobias Koenig's avatar Tobias Koenig

add widget for editing pronunciation of a contact

svn path=/trunk/KDE/kdepim/kcontactmanager/editor/; revision=912631
parent 6d295216
......@@ -23,6 +23,7 @@
#include "addresseditwidget.h"
#include "emaileditwidget.h"
#include "imagewidget.h"
#include "soundeditwidget.h"
#include <klineedit.h>
#include <klocale.h>
......@@ -59,7 +60,7 @@ class ContactEditor::Private
ImageWidget *mPhotoWidget;
KLineEdit *mDisplayNameWidget;
KLineEdit *mNickNameWidget;
QLabel *mSoundWidget;
SoundEditWidget *mPronunciationWidget;
// widgets from internet group
EmailEditWidget *mEmailWidget;
......@@ -163,13 +164,13 @@ void ContactEditor::Private::initGuiContactTab()
label->setBuddy( mNickNameWidget );
nameLayout->addWidget( mNickNameWidget, 2, 1 );
label = new QLabel( i18n( "Sound:" ) );
label = new QLabel( i18n( "Pronunciation:" ) );
label->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
nameLayout->addWidget( label, 3, 0 );
mSoundWidget = new QLabel;
label->setBuddy( mSoundWidget );
nameLayout->addWidget( mSoundWidget, 3, 1 );
mPronunciationWidget = new SoundEditWidget;
label->setBuddy( mPronunciationWidget );
nameLayout->addWidget( mPronunciationWidget, 3, 1 );
nameLayout->setRowStretch( 4, 1 );
......@@ -411,6 +412,7 @@ void ContactEditor::loadContact( const KABC::Addressee &contact )
// name group
d->mPhotoWidget->loadContact( contact );
d->mNickNameWidget->setText( contact.nickName() );
d->mPronunciationWidget->loadContact( contact );
// internet group
d->mEmailWidget->loadContact( contact );
......@@ -449,6 +451,7 @@ void ContactEditor::storeContact( KABC::Addressee &contact ) const
// name group
d->mPhotoWidget->storeContact( contact );
contact.setNickName( d->mNickNameWidget->text().trimmed() );
d->mPronunciationWidget->storeContact( contact );
// internet group
d->mEmailWidget->storeContact( contact );
......
......@@ -35,7 +35,6 @@
#include <QtGui/QDragEnterEvent>
#include <QtGui/QDropEvent>
#include <QtGui/QMenu>
#include <QtGui/QPainter>
/**
* @short Small helper class to load image from network
......@@ -266,6 +265,7 @@ void ImageWidget::saveImage()
void ImageWidget::deleteImage()
{
mHasImage = false;
mImage = QImage();
updateView();
}
......
/*
This file is part of KContactManager.
Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
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.
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.
*/
#include "soundeditwidget.h"
#include <kabc/addressee.h>
#include <kfiledialog.h>
#include <kicon.h>
#include <kio/netaccess.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <phonon/mediaobject.h>
#include <QtCore/QBuffer>
#include <QtGui/QContextMenuEvent>
#include <QtGui/QMenu>
/**
* @short Small helper class to load sound from network
*/
class SoundLoader
{
public:
SoundLoader( QWidget *parent = 0 );
QByteArray loadSound( const KUrl &url, bool *ok );
private:
QByteArray mSound;
QWidget *mParent;
};
SoundLoader::SoundLoader( QWidget *parent )
: mParent( parent )
{
}
QByteArray SoundLoader::loadSound( const KUrl &url, bool *ok )
{
QByteArray sound;
QString tempFile;
if ( url.isEmpty() )
return sound;
(*ok) = false;
if ( url.isLocalFile() ) {
QFile file( url.path() );
if ( file.open( QIODevice::ReadOnly ) ) {
sound = file.readAll();
file.close();
(*ok) = true;
}
} else if ( KIO::NetAccess::download( url, tempFile, mParent ) ) {
QFile file( tempFile );
if ( file.open( QIODevice::ReadOnly ) ) {
sound = file.readAll();
file.close();
(*ok) = true;
}
KIO::NetAccess::removeTempFile( tempFile );
}
if ( !(*ok) ) {
KMessageBox::sorry( mParent, i18n( "This contact's sound cannot be found." ) );
return sound;
}
(*ok) = true;
return sound;
}
SoundEditWidget::SoundEditWidget( QWidget *parent )
: QToolButton( parent ),
mHasSound( false ),
mReadOnly( false ),
mSoundLoader( 0 )
{
connect( this, SIGNAL( clicked() ), SLOT( playSound() ) );
updateView();
}
SoundEditWidget::~SoundEditWidget()
{
delete mSoundLoader;
}
void SoundEditWidget::loadContact( const KABC::Addressee &contact )
{
const KABC::Sound sound = contact.sound();
if ( sound.isIntern() && !sound.data().isEmpty() ) {
mHasSound = true;
mSound = sound.data();
}
updateView();
}
void SoundEditWidget::storeContact( KABC::Addressee &contact ) const
{
KABC::Sound sound( contact.sound() );
sound.setData( mSound );
contact.setSound( sound );
}
void SoundEditWidget::setReadOnly( bool readOnly )
{
mReadOnly = readOnly;
}
void SoundEditWidget::updateView()
{
if ( mHasSound ) {
setIcon( KIcon( "audio-volume-medium" ) );
setToolTip( i18n( "Click to play pronunciation" ) );
} else {
setIcon( KIcon( "audio-volume-muted" ) );
setToolTip( i18n( "No pronunciation available" ) );
}
}
void SoundEditWidget::contextMenuEvent( QContextMenuEvent *event )
{
QMenu menu;
if ( mHasSound )
menu.addAction( i18n( "Play" ), this, SLOT( playSound() ) );
if ( !mReadOnly )
menu.addAction( i18n( "Change..." ), this, SLOT( changeSound() ) );
if ( mHasSound ) {
menu.addAction( i18n( "Save..." ), this, SLOT( saveSound() ) );
if ( !mReadOnly )
menu.addAction( i18n( "Remove" ), this, SLOT( deleteSound() ) );
}
menu.exec( event->globalPos() );
}
void SoundEditWidget::playSound()
{
if ( !mHasSound )
return;
Phonon::MediaObject* player = Phonon::createPlayer( Phonon::NotificationCategory );
QBuffer* soundData = new QBuffer( player );
soundData->setData( mSound );
player->setCurrentSource( soundData );
player->setParent( this );
connect( player, SIGNAL( finished() ), player, SLOT( deleteLater() ) );
player->play();
}
void SoundEditWidget::changeSound()
{
const KUrl url = KFileDialog::getOpenUrl( QString(), "*.wav", this );
if ( url.isValid() ) {
bool ok = false;
const QByteArray sound = soundLoader()->loadSound( url, &ok );
if ( ok ) {
mSound = sound;
mHasSound = true;
updateView();
}
}
}
void SoundEditWidget::saveSound()
{
const QString fileName = KFileDialog::getSaveFileName( KUrl(), "*.wav", this );
if ( !fileName.isEmpty() ) {
QFile file( fileName );
if ( file.open( QIODevice::WriteOnly ) ) {
file.write( mSound );
file.close();
}
}
}
void SoundEditWidget::deleteSound()
{
mHasSound = false;
mSound = QByteArray();
updateView();
}
SoundLoader* SoundEditWidget::soundLoader()
{
if ( !mSoundLoader )
mSoundLoader = new SoundLoader;
return mSoundLoader;
}
/*
This file is part of KContactManager.
Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
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.
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.
*/
#ifndef SOUNDEDITWIDGET_H
#define SOUNDEDITWIDGET_H
#include <QtCore/QPoint>
#include <QtGui/QToolButton>
namespace KABC
{
class Addressee;
}
class SoundLoader;
class SoundEditWidget : public QToolButton
{
Q_OBJECT
public:
SoundEditWidget( QWidget *parent = 0 );
~SoundEditWidget();
void loadContact( const KABC::Addressee &contact );
void storeContact( KABC::Addressee &contact ) const;
void setReadOnly( bool readOnly );
protected:
// context menu handling
virtual void contextMenuEvent( QContextMenuEvent* );
private Q_SLOTS:
void playSound();
void updateView();
void changeSound();
void saveSound();
void deleteSound();
private:
SoundLoader *soundLoader();
QByteArray mSound;
bool mHasSound;
bool mReadOnly;
SoundLoader *mSoundLoader;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment