Logo Search packages:      
Sourcecode: kmess version File versions

emoticon.cpp

/***************************************************************************
                          emoticon.cpp  -  description
                             -------------------
    begin                : Mon Apr 15 2002
    copyright            : (C) 2002 by Mike K. Bennett
    email                : mkb137b@hotmail.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 "emoticon.h"

#include <qdir.h>
#include <qfile.h>
#include <qimage.h>
#include <qstring.h>
#include <qstringlist.h>
#include <qregexp.h>

#include <kconfig.h>
#include <kstandarddirs.h>


#include "kmessdebug.h"

#ifdef KMESSDEBUG_EMOTICONS
#define KMESSDEBUG_EMOTICONS_GENERAL
#endif



Emoticon::Emoticon( const QString &fileTitle, const QString &tooltip )
  : fileTitle_(fileTitle)
  , height_(16)
  , originalFileTitle_(fileTitle)
  , tooltip_(tooltip)
  , width_(16)
{
  // Try to find the location of the current image
  updatePath();
}



Emoticon::~Emoticon()
{
}



// Add another text element into QStringList text_
void Emoticon::addText( QString text )
{
  text_.append( text );
}



// Read the full file path.
const QString& Emoticon::getFilePath() const
{
  return filePath_;
}



// Read the file title.
const QString& Emoticon::getFileTitle() const
{
  return fileTitle_;
}



// Get a suitable bit of HTML for the emoticon.
QString Emoticon::getHtml( bool small ) const
{
  int width  = ( small ? QMIN(16, width_)  : width_  );
  int height = ( small ? QMIN(16, height_) : height_ );
  return "<img src='" + filePath_ + "' alt='" + text_.first() + "' height='" + QString::number(height)
       + "' width='" + QString::number(width) + "' valign='middle' title='' class='standardEmoticon' />";
}



// Get a pattern which will match any of the variants of the emoticon
QString Emoticon::getPattern() const
{
  QString pattern;
  QStringList::const_iterator variant;

  for ( variant = text_.begin(); variant != text_.end() ; variant++ )
  {
    if( pattern.length() > 0 )
    {
      pattern += "|";
    }
    pattern += QRegExp::escape(*variant);
  }


  return pattern;
}


// Read property of QString  text_.
QString Emoticon::getText() const
{
  return text_.first();
}



// Return all possible codes.
const QStringList & Emoticon::getTextList() const
{
  return text_;
}



// Is the emoticon valid - ie, can we find an icon for it?
bool Emoticon::isValid() const
{
  return valid_;
}



// Read property of QString  tooltip_.
QString Emoticon::getTooltip() const
{
  return tooltip_;
}



// Does the emoticon have a tooltip?
bool Emoticon::hasTooltip() const
{
  return (! tooltip_.isNull());
}


// Set the file title.
void Emoticon::setFileTitle(const QString &fileTitle)
{
  fileTitle_ = fileTitle;
  updatePath();
}



// Update the file location
void Emoticon::updatePath()
{
  // Find the new location of the emoticon
  KStandardDirs *dirs  = KGlobal::dirs();
  QString emoticonDir  = CurrentAccount::instance()->getEmoticonStyle();
  QString emoticonPath = dirs->findResource( "emoticons", emoticonDir + "/" + fileTitle_ + ".png" );

  // If the emoticon can't be found, try to revert to a default value.
  if( emoticonPath.isNull() )
  {
    emoticonPath = dirs->findResource( "emoticons", "KMess-new/" + originalFileTitle_ + ".png" );
  }

  // Set the resulting whole path as the new file
  valid_    = (! emoticonPath.isNull());
  filePath_ = emoticonPath;

#ifdef KMESSDEBUG_EMOTICONS_GENERAL
  kdDebug() << "Emoticon: setting path for '" << fileTitle_ << "' to '" << filePath_ << "'" << endl;
#endif

  // Try to find the dimensions of the image, to make it render nicer.
  // Otherwise just keep the existing size, it will render incorrect anyway.
  if(valid_)
  {
    QImage emoticonData(filePath_);
    if( ! emoticonData.isNull() )
    {
      width_  = emoticonData.width();
      height_ = emoticonData.height();
    }
  }

  emit changedFilePath();
}


#include "emoticon.moc"

Generated by  Doxygen 1.6.0   Back to index