kopete/kopete
emoticonselector.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "emoticonselector.h"
00023 #include "kopeteemoticons.h"
00024
00025 #include <math.h>
00026
00027 #include <QPixmap>
00028 #include <QMouseEvent>
00029 #include <QHBoxLayout>
00030 #include <QObject>
00031 #include <QHideEvent>
00032 #include <QShowEvent>
00033
00034 #include <kdebug.h>
00035 #include <kemoticons.h>
00036
00037 EmoticonItem::EmoticonItem(const QString &emoticonText, const QString &pixmapPath, QListWidget *parent)
00038 : QListWidgetItem(parent)
00039 {
00040 m_text = emoticonText;
00041 m_pixmapPath = pixmapPath;
00042 QPixmap p(m_pixmapPath);
00043
00044
00045
00046
00047 if (p.width() > 32 || p.height() > 32)
00048 p = p.scaled(QSize(32,32), Qt::KeepAspectRatio);
00049
00050 setIcon(p);
00051 }
00052
00053 QString EmoticonItem::text() const
00054 {
00055 return m_text;
00056 }
00057
00058 QString EmoticonItem::pixmapPath() const
00059 {
00060 return m_pixmapPath;
00061 }
00062
00063 EmoticonSelector::EmoticonSelector(QWidget *parent)
00064 : QWidget(parent)
00065 {
00066 QHBoxLayout *lay = new QHBoxLayout(this);
00067 lay->setSpacing( 0 );
00068 lay->setContentsMargins( 0, 0, 0, 0 );
00069 m_emoticonList = new QListWidget(this);
00070 lay->addWidget(m_emoticonList);
00071 m_emoticonList->setViewMode(QListView::IconMode);
00072 m_emoticonList->setSelectionMode(QAbstractItemView::SingleSelection);
00073 m_emoticonList->setMouseTracking(true);
00074 m_emoticonList->setDragEnabled(false);
00075
00076 m_currentEmoticon = new QLabel( this );
00077 m_currentEmoticon->setFrameShape( QFrame::Box );
00078 m_currentEmoticon->setMinimumSize(QSize(128,128));
00079 m_currentEmoticon->setAlignment( Qt::AlignCenter );
00080 lay->addWidget(m_currentEmoticon);
00081
00082 m_currentMovie = new QMovie(this);
00083 m_currentEmoticon->setMovie(m_currentMovie);
00084
00085 connect(m_emoticonList, SIGNAL(itemEntered(QListWidgetItem*)),
00086 this, SLOT(mouseOverItem(QListWidgetItem*)));
00087 connect(m_emoticonList, SIGNAL(itemSelectionChanged()),
00088 this, SLOT(currentChanged()));
00089 connect(m_emoticonList, SIGNAL(itemActivated(QListWidgetItem*)),
00090 this, SLOT(emoticonClicked(QListWidgetItem*)));
00091
00092 }
00093
00094 void EmoticonSelector::prepareList(void)
00095 {
00096 m_emoticonList->clear();
00097
00098 QHash<QString, QStringList> list = Kopete::Emoticons::self()->theme().emoticonsMap();
00099
00100 for (QHash<QString, QStringList>::const_iterator it = list.constBegin(); it != list.constEnd(); ++it )
00101 (void) new EmoticonItem(it.value().first(), it.key(), m_emoticonList);
00102
00103 m_emoticonList->setIconSize(QSize(32,32));
00104 }
00105
00106 void EmoticonSelector::emoticonClicked(QListWidgetItem *i)
00107 {
00108 EmoticonItem *item = dynamic_cast<EmoticonItem*>(i);
00109 if (!item)
00110 return;
00111
00112
00113 emit itemSelected ( item->text() );
00114 if ( isVisible() && parentWidget() &&
00115 parentWidget()->inherits("QMenu") )
00116 {
00117 parentWidget()->close();
00118 }
00119 }
00120
00121 void EmoticonSelector::mouseOverItem(QListWidgetItem *item)
00122 {
00123 item->setSelected(true);
00124 if (!m_emoticonList->hasFocus())
00125 m_emoticonList->setFocus();
00126 }
00127
00128 void EmoticonSelector::currentChanged()
00129 {
00130
00131 if (!m_emoticonList->selectedItems().count())
00132 return;
00133
00134 EmoticonItem *item = dynamic_cast<EmoticonItem*>(m_emoticonList->selectedItems().first());
00135 if (!item)
00136 return;
00137
00138 m_currentMovie->stop();
00139 m_currentMovie->setFileName(item->pixmapPath());
00140 m_currentMovie->start();
00141
00142
00143 m_currentEmoticon->update();
00144 }
00145
00146 void EmoticonSelector::hideEvent( QHideEvent* )
00147 {
00148 m_currentMovie->stop();
00149 }
00150
00151 void EmoticonSelector::showEvent( QShowEvent* )
00152 {
00153 m_currentMovie->start();
00154 }
00155
00156 #include "emoticonselector.moc"
00157
00158
00159