1/*
2 Copyright (c) 2012 Montel Laurent <montel@kde.org>
3 based on code from kopete
4
5 This library is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version.
9
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA.
19*/
20
21#include "emoticontexteditselector.h"
22
23#include <KEmoticons>
24#include <kemoticonstheme.h>
25
26#include <QListWidget>
27#include <QPixmap>
28#include <QHBoxLayout>
29
30// Use a static for this as calls to the KEmoticons constructor are expensive.
31K_GLOBAL_STATIC( KEmoticons, sEmoticons )
32
33using namespace KPIMTextEdit;
34
35EmoticonTextEditItem::EmoticonTextEditItem( const QString &emoticonText,
36 const QString &pixmapPath,
37 QListWidget *parent )
38 : QListWidgetItem( parent )
39{
40 mText = emoticonText;
41 mPixmapPath = pixmapPath;
42 QPixmap p( mPixmapPath );
43 // Some of the custom icons are rather large
44 // so lets limit them to a maximum size for this display panel
45 //
46 if ( p.width() > 32 || p.height() > 32 ) {
47 p = p.scaled( QSize( 32, 32 ), Qt::KeepAspectRatio );
48 }
49
50 setIcon( p );
51 setToolTip( mText );
52}
53
54QString EmoticonTextEditItem::text() const
55{
56 return mText;
57}
58
59QString EmoticonTextEditItem::pixmapPath() const
60{
61 return mPixmapPath;
62}
63
64class EmoticonTextEditSelector::EmoticonTextEditSelectorPrivate
65{
66 public:
67 EmoticonTextEditSelectorPrivate()
68 {
69 }
70 QListWidget *listEmoticon;
71};
72
73EmoticonTextEditSelector::EmoticonTextEditSelector( QWidget *parent )
74 : QWidget( parent ), d( new EmoticonTextEditSelectorPrivate() )
75{
76 QHBoxLayout *lay = new QHBoxLayout( this );
77 lay->setSpacing( 0 );
78 lay->setContentsMargins( 0, 0, 0, 0 );
79 d->listEmoticon = new QListWidget( this );
80 lay->addWidget( d->listEmoticon );
81 d->listEmoticon->setViewMode( QListView::IconMode );
82 d->listEmoticon->setSelectionMode( QAbstractItemView::SingleSelection );
83 d->listEmoticon->setMouseTracking( true );
84 d->listEmoticon->setDragEnabled( false );
85 connect( d->listEmoticon, SIGNAL(itemEntered(QListWidgetItem*)),
86 this, SLOT(slotMouseOverItem(QListWidgetItem*)) );
87 connect( d->listEmoticon, SIGNAL(itemClicked(QListWidgetItem*)),
88 this, SLOT(slotEmoticonClicked(QListWidgetItem*)) );
89}
90
91EmoticonTextEditSelector::~EmoticonTextEditSelector()
92{
93 delete d;
94}
95
96void EmoticonTextEditSelector::slotCreateEmoticonList()
97{
98 d->listEmoticon->clear();
99 static QString cachedEmoticonsThemeName;
100 if ( cachedEmoticonsThemeName.isEmpty() ) {
101 cachedEmoticonsThemeName = KEmoticons::currentThemeName();
102 }
103 const QHash<QString, QStringList> list =
104 sEmoticons->theme( cachedEmoticonsThemeName ).emoticonsMap();
105
106 //Keep in sync with linklocator.cpp
107 QStringList exclude;
108 exclude << QLatin1String("(c)") << QLatin1String("(C)") << QLatin1String("&gt;:-(") << QLatin1String("&gt;:(") << QLatin1String("(B)") << QLatin1String("(b)") << QLatin1String("(P)") << QLatin1String("(p)");
109 exclude << QLatin1String("(O)") << QLatin1String("(o)") << QLatin1String("(D)") << QLatin1String("(d)") << QLatin1String("(E)") << QLatin1String("(e)") << QLatin1String("(K)") << QLatin1String("(k)");
110 exclude << QLatin1String("(I)") << QLatin1String("(i)") << QLatin1String("(L)") << QLatin1String("(l)") << QLatin1String("(8)") << QLatin1String("(T)") << QLatin1String("(t)") << QLatin1String("(G)");
111 exclude << QLatin1String("(g)") << QLatin1String("(F)") << QLatin1String("(f)") << QLatin1String("(H)");
112 exclude << QLatin1String("8)") << QLatin1String("(N)") << QLatin1String("(n)") << QLatin1String("(Y)") << QLatin1String("(y)") << QLatin1String("(U)") << QLatin1String("(u)") << QLatin1String("(W)") << QLatin1String("(w)");
113
114 QHash<QString, QStringList>::const_iterator end = list.constEnd();
115 for ( QHash<QString, QStringList>::const_iterator it = list.constBegin(); it != end; ++it ) {
116 if (!exclude.contains(it.value().first()))
117 new EmoticonTextEditItem( it.value().first(), it.key(), d->listEmoticon );
118 }
119
120 d->listEmoticon->setIconSize( QSize( 32, 32 ) );
121}
122
123void EmoticonTextEditSelector::slotMouseOverItem( QListWidgetItem *item )
124{
125 item->setSelected( true );
126 if ( !d->listEmoticon->hasFocus() ) {
127 d->listEmoticon->setFocus();
128 }
129}
130
131void EmoticonTextEditSelector::slotEmoticonClicked( QListWidgetItem *item )
132{
133 if ( !item ) {
134 return;
135 }
136 EmoticonTextEditItem *itemEmoticon = static_cast<EmoticonTextEditItem*>( item );
137
138 emit itemSelected ( itemEmoticon->text() );
139 if ( isVisible() && parentWidget() &&
140 parentWidget()->inherits( "QMenu" ) ) {
141 parentWidget()->close();
142 }
143}
144
145