~ubuntu-branches/ubuntu/lucid/kmess/lucid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/***************************************************************************
                          emoticonswidget.cpp -  description
                             -------------------
    begin                : Mon 12 09 2005
    copyright            : (C) 2005 by Diederik van der Boor
    email                : "vdboor" --at-- "codingdomain.com"
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "emoticonswidget.h"

#include "../emoticonmanager.h"
#include "../emoticontheme.h"
#include "../kmessdebug.h"
#include "../utils/kmessshared.h"
#include "../dialogs/addemoticondialog.h"
#include "../accountsmanager.h"
#include "../currentaccount.h"

#include <QAction>
#include <QDockWidget>
#include <QListWidgetItem>
#include <QMenu>
#include <QMouseEvent>
#include <QPoint>



// The constructor
EmoticonsWidget::EmoticonsWidget( QWidget *parent )
 : QWidget(parent)
 , emoticonDialog_(0)
 , initialized_(false)
 , noEmoticonsWarning_(0)
{
  // Get and connect to the emoticon manager. We'll need to update ourselves when the emoticon
  // theme changes
  manager_ = EmoticonManager::instance();
  connect( manager_, SIGNAL(          updated() ),
           this,     SLOT  ( slotThemeUpdated() ) );

  // Create and set the properties for list widget
  list_ = new EmoticonListWidget( this );

  // React to mouse button clicks
  connect( list_ , SIGNAL(       leftClicked(const QListWidgetItem*)               ),
           this,   SLOT  (   slotItemClicked(const QListWidgetItem*)               ) );
  // Enable the right-click context menu
  connect( list_, SIGNAL(       rightClicked(const QListWidgetItem*,const QPoint&) ),
           this,  SLOT  (    showContextMenu(const QListWidgetItem*,const QPoint&) ) );
  connect( list_, SIGNAL(     requestPreview(const QListWidgetItem*)               ),
           this,    SLOT( slotRequestPreview(const QListWidgetItem*)               ) );

  // Set the label for preview
  previewLabel_ = new QLabel( this );
  previewLabel_->setFrameShape( QFrame::StyledPanel );
  previewLabel_->setFrameShadow( QFrame::Sunken );
  previewLabel_->setFixedSize( 48, 48 );
  previewLabel_->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );

  // Set the movie preview
  previewMovie_ = new QMovie( this );
  previewMovie_->stop();
  previewLabel_->setMovie( previewMovie_ );

  // Create an initial layout for the widget
  layout_ = new QBoxLayout( QBoxLayout::LeftToRight, this );
  layout_->setContentsMargins( 0,0,0,0 );
  layout_->setSpacing( 0 );
  layout_->addWidget( list_ );
  layout_->addWidget( previewLabel_ );

  // Install the event filter for the preview label
  installEventFilter( this );
}



// Constructor for emoticon list widget class
EmoticonListWidget::EmoticonListWidget( QWidget *parent )
  : QListWidget(parent)
  , previousItem_(0)
{
  // Set the list properties
  setViewMode( QListView::IconMode );
  setResizeMode( QListView::Adjust );
  setIconSize( QSize( 16,16 ) );
  setGridSize( QSize( 20,20 ) );
  setUniformItemSizes( true );
  setMovement( QListView::Static );
  setSelectionMode( QAbstractItemView::SingleSelection );
  setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
  setFrameShape( QFrame::NoFrame );
  setStyleSheet( "QListWidget { background-color: transparent; }" );

  // Install event filter
  viewport()->installEventFilter( this );
}



// Preview requested
void EmoticonsWidget::slotRequestPreview( const QListWidgetItem *item )
{
  if( ! initialized_ )
  {
    kWarning() << "Uninitialized usage of EmoticonsWidget!";
    return;
  }

  // Stop the movie and set the new emoticon
  previewMovie_->stop();

  // If the new item is not null set the emoticon to preview
  if( item != 0 )
  {
    previewLabel_->setMovie( previewMovie_ );
    previewMovie_->setFileName( item->data( PictureRole ).toString() );
  }
  else
  {
    // if the item is null, set empty pixmap
    previewLabel_->setPixmap( QPixmap() );
    return;
  }

  // Check if the file format is valid
  if( ! previewMovie_->isValid() )
  {
    return;
  }

  previewMovie_->start();

  // Scale the emoticon if it is larger than its container
  if( previewMovie_->currentPixmap().size().width()  > previewLabel_->size().width()
  ||  previewMovie_->currentPixmap().size().height() > previewLabel_->size().height() )
  {
    previewLabel_->setScaledContents( true );
  }
  else
  {
    previewLabel_->setScaledContents( false );
  }
}



// Reimplement the mouse press event to grep left and right click
void EmoticonListWidget::mousePressEvent( QMouseEvent *e )
{
  // Buttons press event
  QListWidgetItem *item = itemAt( e->pos() );

  if( item == 0 )
  {
    return;
  }

  if( e->button() == Qt::LeftButton )
  {
    emit leftClicked( item );
  }
  else if( e->button() == Qt::RightButton )
  {
    emit rightClicked( item, e->pos() );
  }
}



// Reimplement the event filter
bool EmoticonListWidget::eventFilter( QObject */*obj*/, QEvent *e )
{
  if( e->type() == QEvent::HoverMove )
  {
    // The cursor mouse is moving over the widget
    QListWidgetItem *item = itemAt( static_cast<QHoverEvent*>(e)->pos() );

    // Check if the mouse is over the same emoticon or the selection is changed
    if( item == previousItem_ )
    {
      return false;
    }

    previousItem_ = item;
    emit requestPreview( item );
  }

  return false;
}



// The destructor
EmoticonsWidget::~EmoticonsWidget()
{
  delete emoticonDialog_;
  delete noEmoticonsWarning_;
}



// Event filter for the preview label
bool EmoticonsWidget::eventFilter( QObject *obj, QEvent *event )
{
  Q_UNUSED( obj );

  int type = event->type();

  if( type == QEvent::Hide || type == QEvent::Leave )
  {
    // Stop the preview while the emoticon widget is hidden
    previewLabel_->setPixmap( QPixmap() );
    previewMovie_->stop();
  }

  return false;
}



// One item was clicked
void EmoticonsWidget::slotItemClicked( const QListWidgetItem * item )
{
  if( ! initialized_ )
  {
    kWarning() << "Uninitialized usage of EmoticonsWidget!";
    return;
  }

  // Deselect current item so it has the click effect
  list_->setCurrentItem( 0 );

  emit insertEmoticon( item->data( Qt::UserRole ).toString() );
}



// The "Click to open the custom emoticons settings" link has been clicked
void EmoticonsWidget::slotNoEmoticonsLinkClicked()
{
  AccountsManager::instance()->showAccountSettings( CurrentAccount::instance(), window(), AccountSettingsDialog::PageEmoticonsTabCustom );
}



// The theme has changed, we need to reload it
void EmoticonsWidget::slotThemeUpdated()
{
  if( ! initialized_ )
  {
    kWarning() << "Uninitialized usage of EmoticonsWidget!";
    return;
  }

#ifdef KMESSDEBUG_EMOTICONSWIDGET
  kDebug() << "Removing old theme.";
#endif

  // Clear list
  list_->clear();

#ifdef KMESSDEBUG_EMOTICONSWIDGET
  kDebug() << "Updating with current emoticons.";
#endif

  // Add all theme emoticons to the list
  EmoticonTheme *theme = manager_->getTheme( isCustom_ );
  const QList<Emoticon*> &emoticons = theme->getEmoticons();

  // If the custom emoticon theme is empty, show the label
  if( isCustom_ && emoticons.isEmpty() )
  {
    if( noEmoticonsWarning_ == 0 )
    {
      // Create and set the properties for the warning message
      noEmoticonsWarning_ = new QLabel( this );
      noEmoticonsWarning_->setScaledContents( true );
      noEmoticonsWarning_->setWordWrap( true );
      noEmoticonsWarning_->setText( i18nc( "Informative label on the chat's emoticons bar",
                                    "<html><p><b>You have not added any custom emoticons yet.</b></p>"
                                    "<p>To add new emoticons, <a href='#'>click here</a>!</p></html>" ) );

      // Enable the link click action
      connect( noEmoticonsWarning_, SIGNAL(              linkActivated(const QString&) ),
               this,                SLOT  (               slotNoEmoticonsLinkClicked() ) );

      layout_->addWidget( noEmoticonsWarning_ );
    }

    // Show the label and hide the listwidget
    noEmoticonsWarning_->show();
    list_->hide();

    // Set the preview movie and label
    previewMovie_->stop();
    previewLabel_->hide();

#ifdef KMESSDEBUG_EMOTICONSWIDGET
    kDebug() << "Custom emoticon theme is empty";
#endif
    return;
  }

  // If the label was created, hide it!
  if( noEmoticonsWarning_ != 0 )
  {
    previewLabel_->show();
    noEmoticonsWarning_->hide();
  }

  list_->show();

  foreach( Emoticon *emoticon, emoticons )
  {
    if( ! emoticon->isValid() )
    {
      continue;
    }

    QListWidgetItem *item = new QListWidgetItem( QIcon( emoticon->getPicturePath() ), QString(), list_ );

    // Set the shortcut into data of current item widget
    item->setData( ShortcutRole, emoticon->getShortcut()    );
    item->setData( PictureRole,  emoticon->getPicturePath() );

    // Add the emoticon tooltip
    item->setToolTip( emoticon->getTooltip() );
  }

#ifdef KMESSDEBUG_EMOTICONSWIDGET
  kDebug() << "Updated total" << emoticons.count() << "emoticons";
#endif
}



// Initialize the widget
void EmoticonsWidget::initialize( bool useCustomTheme )
{
  if( initialized_ )
  {
    kWarning() << "EmoticonsWidget is already initialized!";
    return;
  }

  initialized_ = true;

  // Set the theme and load it
  isCustom_ = useCustomTheme;
  slotThemeUpdated();
}



// Connect the widget to a dock
void EmoticonsWidget::setDockWidget( QDockWidget *dockWidget, Qt::DockWidgetArea initialArea )
{
#ifdef KMESSTEST
  KMESS_ASSERT( dockWidget );
#endif

  // We need to change layout when the dock moves
  connect( dockWidget, SIGNAL( dockLocationChanged(Qt::DockWidgetArea) ),
           this,       SLOT  ( slotLocationChanged(Qt::DockWidgetArea) ) );
  connect( dockWidget, SIGNAL(     topLevelChanged(bool)               ),
           this,       SLOT  ( slotTopLevelChanged(bool)               ) );

  slotLocationChanged( initialArea );
}



// The location of the parent dock widget has changed
void EmoticonsWidget::slotLocationChanged( Qt::DockWidgetArea area )
{
#ifdef KMESSDEBUG_EMOTICONSWIDGET
  kDebug() << "Changing location to" << area;
#endif

  switch( area )
  {
    case Qt::TopDockWidgetArea:
    case Qt::BottomDockWidgetArea:
      layout_->setDirection( QBoxLayout::LeftToRight );
      break;

    case Qt::LeftDockWidgetArea:
    case Qt::RightDockWidgetArea:
    default:
      layout_->setDirection( QBoxLayout::TopToBottom );
      break;
  }

  // Update our size constraints
  setMinimumSize( minimumSizeHint() );
}



// The parent dock widget floating status has changed
void EmoticonsWidget::slotTopLevelChanged( bool isTopLevel )
{
  setVisible( ! isTopLevel );
  adjustSize();
}



// Right mouse button was clicked
void EmoticonsWidget::showContextMenu( const QListWidgetItem *item, const QPoint &point )
{
  if( ! initialized_ )
  {
    kWarning() << "Uninitialized usage of EmoticonsWidget!";
    return;
  }

  // Deselect current item so it has the click effect
  list_->setCurrentItem( 0 );

  // The popup menu is only for custom emoticon
  if( isCustom_ == false )
  {
    return;
  }

  // Create the popup menu for custom emoticon
  QMenu menu;
  QAction addAction(    KIcon( "insert-image" ),  i18n( "Add to Chat" ), this );
  QAction insertAction( KIcon( "list-add" ),      i18n( "Add New" ),     this );
  QAction editAction(   KIcon( "draw-freehand" ), i18n( "Edit"   ),      this );
  QAction removeAction( KIcon( "list-remove" ),   i18n( "Remove" ),      this );

  menu.addAction( &addAction    );
  menu.addAction( &insertAction );
  menu.addAction( &editAction   );
  menu.addAction( &removeAction );

  // Exec the menu popup and grep the action selected
  QAction *clickedAction = menu.exec( mapToGlobal( point ) );

  if( clickedAction == 0 )
  {
    // No action clicked
    return;
  }

  // Check which action was clicked
  if( clickedAction == &addAction )
  {
    slotItemClicked( item );
    return;
  }
  else if( clickedAction == &removeAction )
  {
    manager_->getTheme( isCustom_ )->removeEmoticon( item->data( ShortcutRole ).toString() );
    return;
  }

  if( emoticonDialog_ == 0 )
  {
    emoticonDialog_ = new AddEmoticonDialog( manager_->getTheme( true ), this );
  }

  if( clickedAction == &insertAction )
  {
    emoticonDialog_->preSelect( "", "" );
  }
  else if( clickedAction == &editAction )
  {
    emoticonDialog_->editEmoticon( item->data( PictureRole ).toString(), item->data( ShortcutRole ).toString() );
  }

  emoticonDialog_->show();
}


#include "emoticonswidget.moc"