RFC: An action class to ease implementation of show/hide-like actions

Aurélien Gâteau agateau at kde.org
Fri Sep 17 22:31:54 BST 2010


On 10/09/2010 23:20, Aurélien Gâteau wrote:
> 2. I wanted the class to have a boolean state and decided to call it
> "on". I am not so sure it is a good idea now because I realized QAction
> already has an "on" property in qt3support. It was renamed to "checked"
> in Qt4, but it still appears in the DBus API when we expose our actions.
> 
> 3. The bool parameter in the methods to get/set the icon/text,tooltip is
> there to make it possible to define those for "off" and "on" values. I
> dislike however having to use the "using" keyword. Would it be better to
> use different names?

Found some time today to work a bit more on this class. I made some
changes to address the two issues quoted above, now the API looks like this:

---
class KDEUI_EXPORT KDualAction : public KAction
{
    Q_OBJECT
public:
    KDualAction(const QString &offActionText, QObject *parent);
    explicit KDualAction(QObject *parent);

    void setGuiItemForState(bool, const KGuiItem &item);
    KGuiItem guiItemForState(bool) const;

    void setTextForState(bool, const QString &text);
    QString textForState(bool) const;

    void setIconForState(bool, const QIcon &icon);
    QIcon iconForState(bool) const;

    void setToolTipForState(bool, const QString &tip);
    QString toolTipForState(bool) const;

    bool isActive() const;
    void setActive(bool);
    void silentSetActive(bool);

    /**
     * Whether the current action should automatically be changed when
     * the user triggers this action. The default value is true.
     */
    void setAutoToggle(bool);
    bool autoToggle() const;

Q_SIGNALS:
    void activeChanged(bool);

private:
    Q_PRIVATE_SLOT(d, void slotTriggered())
    KDualActionPrivate *const d;
    friend class KDualActionPrivate;
};
---

I renamed the "on" property to "active" (and statusChanged() signal to
activeChanged()) and also suffixed methods to get/set icon/text/tooltip
with "ForState" to avoid the ambiguity of the previous version.

I am also considering making the set{Icon,Text,IconText,ToolTip} methods
protected to make sure users do not mis-use the class. What do you think?

Aurélien




More information about the kde-core-devel mailing list