/***************************************************************************
 *   Copyright (C) 2003-2005 by liuspider                                  *
 *   liuspider@users.sourceforge.net                                       *
 *                                                                         *
 *   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.                                   *
 **************************************************************************/
#ifndef SCIMACTIONS_H
#define SCIMACTIONS_H

#include <qtoolbutton.h>

class SkimToolButton : public QToolButton
{
Q_OBJECT
public:
    SkimToolButton ( QWidget * parent, const char * name = 0 );
    virtual QSize sizeHint () const;
    virtual QSize minimumSizeHint () const;
};

#include <kaction.h>
namespace scim_kde{
  enum ScimActionPlugOption{
    ToolBarPreferIcon = 1,/*when no icon, just text label, otherwise the same as ToolBarIconOnly*/
    ToolBarIconOnly = 1 << 1,
    ToolBarIconUseText = 1 << 2,
    ToolBarTextOnly = 1 << 3,
  };
}

class ScimAction : public KAction
{
Q_OBJECT
public:
    ScimAction(const QString &text, const QIconSet &pix, const KShortcut &cut, const QObject *receiver, const char *slot, KActionCollection *parent, const char *name);
    ScimAction (const QString &text, const KShortcut &cut, const QObject *receiver, const char *slot, KActionCollection *parent, const char *name);
    ScimAction (const QString &text, const QPixmap &pix, 
      KActionCollection *parent, const QString & _uuid, const char *name);
    ScimAction (const QString &text, const QPixmap &pix, 
      KActionCollection *parent, const int & _id, const char *name);
    ScimAction (const QString &text, KActionCollection *parent, const int & _id, const char *name);
    virtual ~ScimAction();
    void init();
    
    void unplugAll();
    void setVisible(bool b) {_visible = b;};
    void setCurrentShown(bool b) {_currentShown = b;};
    bool visible() {return _visible;};
    bool currentShown() {return _currentShown;};
    virtual int plug (QWidget *widget, int index=-1);
    virtual void setOption(scim_kde::ScimActionPlugOption op){option = op;};
    virtual void setDisplayedText(const QString &);
    virtual void setCurrentIconSet(QIconSet &);
    virtual void setCurrentIconSet(const char * q);

signals:
    void activated(QString & uuid);
    void activated(int id);

protected slots:
    //These functions are used to honor QToolButton's individual configurable TextIcon Mode.
    virtual void updateDText (int i);
    virtual void updateCIconSet( int id );
    virtual void slotActivated ();

protected:
    virtual void updateShortcut (QPopupMenu* menu, int id);
    
    scim_kde::ScimActionPlugOption option;
    QString displayText;
    QIconSet curIconset;
    QString uuid;
    bool _visible, _currentShown;
    int id;
    class QPixmap *curIconPix;
//     std::vector<QWidget *> adjust_widgets;
};
// plug into toolbar QComboBox, while pluging into popupmenu QMenu
#include <qpopupmenu.h>

struct SubMenuInfo {
    QString path;
    QMenuItem* mitem;
    int parent;  //parent item id
    class QPixmap *iconPixCache;
};

typedef QMap<int, SubMenuInfo> SubMenuInfoRepository;
typedef QMap<QString, int> Path2ID;
typedef QMap<int, QPopupMenu*> PopupRepository;

class ScimComboAction : public ScimAction
{
Q_OBJECT
public:
    ScimComboAction (const QString &text, const QIconSet &pix, const KShortcut &cut, const QObject *receiver, const char *slot, KActionCollection *parent, const char *name);
    ScimComboAction (const QString &text, const KShortcut &cut, const QObject *receiver, const char *slot, KActionCollection *parent, const char *name);
    void init();
    virtual ~ScimComboAction();
    virtual QPopupMenu * popup () {  return &data;  };
    virtual int plug (QWidget *widget, int index=-1);
    int current(){return currentItemIndex;};
    int count(){ return data.count(); };
    void setItemVisible(int id, bool);
    void setItemEnabled(int id, bool);
signals:
    void itemActivated(int);
    void itemActivated(const QString & path);
public slots:
    void menuItemActivated(int id);
    int insertItem ( const QString & path, const char * icon,
      const QString & text, bool isMenu = false, int id = -1);
    int insertItem ( const QString & path, const QPixmap & existpix,
      const QString & text, bool isMenu = false, int id = -1);
    void insertItem ( const QIconSet & icon, const QString & text, int id = -1);
    void insertItem ( const QIconSet & icon, const QString & text, 
      const QObject * receiver, const char * member, int id = -1);
    void insertItem ( const QIconSet & icon, const QString & text, QPopupMenu * popup, int id = -1);

    void changeItem ( const QPixmap & im, const QString & t, int index, bool internal = false );
    void changeItem ( const char * icon, const QString & t, int index );
    QString text ( int index ) const;
    const QIconSet* iconSet(int) const;
    void clear();
    void slotPopup();

// protected slots: this should be a protected slot
    virtual void slotActivated ();
protected:
    int insertItem ( const QString & path, const char * icon, const QPixmap * existpix,
      const QString & text, bool isMenu, int id);
private:
    QPopupMenu data;
    int currentItemIndex;
    SubMenuInfoRepository subInfoRep;
    Path2ID path2ID;
    PopupRepository popups;
};
#endif
