File indexing completed on 2024-05-12 16:40:06

0001 /* This file is part of the KDE project
0002    Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
0003    Copyright (C) 2011-2014 Jarosław Staniek <staniek@kde.org>
0004 
0005    Based on qmenu.h from Qt 4.7
0006 
0007    This library is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU Library General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 
0012    This library is distributed in the hope that it will be useful,
0013    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015    Library General Public License for more details.
0016 
0017    You should have received a copy of the GNU Library General Public License
0018    along with this library; see the file COPYING.LIB.  If not, write to
0019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  * Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #ifndef KEXIMENUWIDGET_H
0024 #define KEXIMENUWIDGET_H
0025 
0026 #include <KStandardAction>
0027 
0028 #include <QWidget>
0029 #include <QString>
0030 #include <QIcon>
0031 #include <QAction>
0032 
0033 class KexiMenuWidgetPrivate;
0034 class KexiMenuWidgetActionPrivate;
0035 class QStyleOptionMenuItem;
0036 class QEventLoop;
0037 struct KexiMenuWidgetCaused;
0038 
0039 class KexiMenuWidgetAction : public QAction
0040 {
0041     Q_OBJECT
0042 public:
0043     explicit KexiMenuWidgetAction(QObject *parent);
0044     KexiMenuWidgetAction(const QString &text, QObject *parent);
0045     KexiMenuWidgetAction(const QIcon &icon, const QString &text, QObject *parent);
0046     KexiMenuWidgetAction(KStandardAction::StandardAction id, QObject *parent);
0047     ~KexiMenuWidgetAction();
0048 
0049     void setPersistentlySelected(bool set);
0050     bool persistentlySelected() const;
0051 
0052 protected:
0053     const QScopedPointer<KexiMenuWidgetActionPrivate> d;
0054 };
0055 
0056 class KexiMenuWidget : public QWidget
0057 {
0058 private:
0059     Q_OBJECT
0060 
0061     //Q_PROPERTY(bool tearOffEnabled READ isTearOffEnabled WRITE setTearOffEnabled)
0062     Q_PROPERTY(QString title READ title WRITE setTitle)
0063     Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
0064     Q_PROPERTY(bool separatorsCollapsible READ separatorsCollapsible WRITE setSeparatorsCollapsible)
0065     Q_PROPERTY(bool frame READ hasFrame WRITE setFrame)
0066 
0067 public:
0068     explicit KexiMenuWidget(QWidget *parent = 0);
0069     explicit KexiMenuWidget(const QString &title, QWidget *parent = 0);
0070     ~KexiMenuWidget();
0071 
0072     KexiMenuWidgetAction* persistentlySelectedAction() const;
0073     void setPersistentlySelectedAction(KexiMenuWidgetAction* action, bool set);
0074 
0075 #ifdef Q_NO_USING_KEYWORD
0076     inline void addAction(QAction *action) { QWidget::addAction(action); }
0077 #else
0078     using QWidget::addAction;
0079 #endif
0080     QAction *addAction(const QString &text);
0081     QAction *addAction(const QIcon &icon, const QString &text);
0082     QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
0083     QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
0084 
0085 //    QAction *addMenu(QMenu *menu);
0086 //    QMenu *addMenu(const QString &title);
0087 //    QMenu *addMenu(const QIcon &icon, const QString &title);
0088 
0089     QAction *addSeparator();
0090 
0091 //    QAction *insertMenu(QAction *before, QMenu *menu);
0092     QAction *insertSeparator(QAction *before);
0093 
0094     //! @return true if frame is visible. By default the frame is visible.
0095     //! It may be still visually no change, e.g. on Oxygen style.
0096     bool hasFrame() const;
0097 
0098     //! Sets frame visibility.
0099     void setFrame(bool set);
0100 
0101     //! @return true if persistent selections are enabled. False by default.
0102     //! @see setPersistentSelectionsEnabled()
0103 //    bool persistentSelectionsEnabled() const;
0104 
0105     //! Sets flag of for persistent selections in action items.
0106     //! In KexiMenuWidget, persistent selection flag is used in place of submenus.
0107 //    void setPersistentSelectionsEnabled(bool set);
0108 
0109     bool isEmpty() const;
0110     void clear();
0111 
0112     void setTearOffEnabled(bool);
0113     bool isTearOffEnabled() const;
0114 
0115     bool isTearOffMenuVisible() const;
0116     void hideTearOffMenu();
0117 
0118     void setDefaultAction(QAction *);
0119     QAction *defaultAction() const;
0120 
0121     void setActiveAction(QAction *act);
0122     QAction *activeAction() const;
0123 
0124     void popup(const QPoint &pos, QAction *at=0);
0125     // QAction *exec();
0126     // QAction *exec(const QPoint &pos, QAction *at=0);
0127 
0128     // ### Qt 5: merge
0129     // static QAction *exec(QList<QAction*> actions, const QPoint &pos, QAction *at=0);
0130     // static QAction *exec(QList<QAction*> actions, const QPoint &pos, QAction *at, QWidget *parent);
0131 
0132     QSize sizeHint() const override;
0133 
0134     QRect actionGeometry(QAction *) const;
0135     QAction *actionAt(const QPoint &) const;
0136 
0137     QAction *menuAction() const;
0138 
0139     QString title() const;
0140     void setTitle(const QString &title);
0141 
0142     QIcon icon() const;
0143     void setIcon(const QIcon &icon);
0144 
0145     void setNoReplayFor(QWidget *widget);
0146 
0147     bool separatorsCollapsible() const;
0148     void setSeparatorsCollapsible(bool collapse);
0149 
0150 Q_SIGNALS:
0151     void aboutToShow();
0152     void aboutToHide();
0153     void triggered(QAction *action);
0154     void hovered(QAction *action);
0155 
0156 protected:
0157     int columnCount() const;
0158 
0159     void changeEvent(QEvent *) override;
0160     void keyPressEvent(QKeyEvent *) override;
0161     void mouseReleaseEvent(QMouseEvent *) override;
0162     void mousePressEvent(QMouseEvent *) override;
0163     void mouseMoveEvent(QMouseEvent *) override;
0164 #ifndef QT_NO_WHEELEVENT
0165     void wheelEvent(QWheelEvent *) override;
0166 #endif
0167     void enterEvent(QEvent *) override;
0168     void leaveEvent(QEvent *) override;
0169     void showEvent(QShowEvent* event) override;
0170     void hideEvent(QHideEvent *) override;
0171     void paintEvent(QPaintEvent *) override;
0172     void actionEvent(QActionEvent *) override;
0173     void timerEvent(QTimerEvent *) override;
0174     bool event(QEvent *) override;
0175     bool focusNextPrevChild(bool next) override;
0176     void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
0177 
0178 private Q_SLOTS:
0179     void internalSetSloppyAction();
0180     //void internalDelayedPopup();
0181     void actionTriggered();
0182     void actionHovered();
0183     void overrideMenuActionDestroyed();
0184 
0185 protected:
0186     const KexiMenuWidgetCaused& causedPopup() const;
0187     enum SelectionReason {
0188         SelectedFromKeyboard,
0189         SelectedFromElsewhere
0190     };
0191     void setCurrentAction(QAction *, int popup = -1, SelectionReason reason = SelectedFromElsewhere, bool activateFirst = false);
0192     QEventLoop *eventLoop() const;
0193     void setSyncAction(QAction *a);
0194 
0195 private:
0196     Q_DISABLE_COPY(KexiMenuWidget)
0197 
0198     KexiMenuWidgetPrivate * const d;
0199     friend class KexiMenuWidgetPrivate;
0200 };
0201 
0202 #endif