Logo Search packages:      
Sourcecode: kmess version File versions

emoticonsidebar.cpp

/***************************************************************************
                          emoticonsidebar.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 <qlayout.h>
#include <qobjectlist.h>

#include "emoticonsidebar.h"
#include "emoticonbutton.h"

#include "../emoticoncollection.h"
#include "../kmessdebug.h"




// The constructor
EmoticonSidebar::EmoticonSidebar(QWidget *parent, const char *name)
 : QWidget(parent, name)
 , col_(0)
 , row_(0)
{
  Emoticon           *emoticon;
  EmoticonCollection *emoticons;

  // Add all emoticons to the list
  emoticons = EmoticonCollection::instance();

  for(emoticon = emoticons->first(); emoticon != 0; emoticon = emoticons->next() )
  {
    if( emoticon->isValid() )
    {
      createEmoticonButton( emoticon );
    }
  }

  // Postion them.
  updateEmoticonLayout();
}



// The destructor
EmoticonSidebar::~EmoticonSidebar()
{
}



// Create an emoticon button on this dialog.
void EmoticonSidebar::createEmoticonButton(const Emoticon *emoticon)
{
  EmoticonButton *button;

  // Create the button
  // Use a separate EmoticonButton, so we can also include the
  // emoticon code in the button signal.
  button = new EmoticonButton(emoticon, this, "EmoticonButton");

  // Connect the button's insertEmoticon signal
  connect( button, SIGNAL(     insertEmoticon(QString) ) ,
           this,   SLOT  ( slotInsertEmoticon(QString) ) );
}


// Overwritten method to detect resizes
void EmoticonSidebar::resizeEvent(QResizeEvent *event)
{
  QWidget::resizeEvent(event); // call parent
  updateEmoticonLayout();      // update emoticon layout
}


// Layout the emoticon buttons
void EmoticonSidebar::updateEmoticonLayout()
{
  // Insert the button to the layout
  const QObjectList *childWidgets = children();
  if(childWidgets == 0) return;  // Is null if there aren't any children.
  QObjectListIt it( *childWidgets );
  QObject *obj;
  QWidget *widget;

  int parentWidth = width();

  int left   = 0;
  int top    = 0;
  int width  = 0;
  int height = 0;
  int rowHeight = 0;

  while( it.current() != 0 )
  {
    obj = it.current();
    if( ! obj->isA("QWidget") )
    {
      widget = static_cast<QWidget*>( obj );

      // If the widget doesn't fit at the current row, move to the next row.
      width  = widget->width();
      height = widget->height();
      if( left + width > parentWidth )
      {
        left      = 0;
        top      += height;
        rowHeight = height;
      }
      else
      {
        rowHeight = QMAX(rowHeight, height);
      }

      // Set position
      widget->setGeometry(left, top, width, height);

      // Move to next col
      left += width;
    }
    ++it;
  }
}



// A button was clicked
void EmoticonSidebar::slotInsertEmoticon(QString html)
{
  emit insertEmoticon(html);
}


#include "emoticonsidebar.moc"

Generated by  Doxygen 1.6.0   Back to index