97 lines
3.2 KiB
C++
97 lines
3.2 KiB
C++
#pragma once
|
|
|
|
#include <QAction>
|
|
#include <QKeySequence>
|
|
#include <QList>
|
|
#include <QMenu>
|
|
#include <QMenuBar>
|
|
#include <QObject>
|
|
#include <QPoint>
|
|
#include <functional>
|
|
|
|
namespace Dpi {
|
|
|
|
// TMenuItem equivalent: wraps a QAction and, lazily, a QMenu used to host
|
|
// child items when the item acts as a submenu.
|
|
class MenuItem : public QObject {
|
|
Q_OBJECT
|
|
public:
|
|
explicit MenuItem(const QString &caption = QString(), QObject *parent = nullptr);
|
|
// Wraps an existing QMenu (e.g. one returned by QMenuBar::addMenu). When
|
|
// ownsMenu is true, this item deletes the menu on destruction.
|
|
explicit MenuItem(QMenu *existingMenu, bool ownsMenu = false, QObject *parent = nullptr);
|
|
// Root item for a QMenuBar: add() creates top-level bar menus.
|
|
explicit MenuItem(QMenuBar *bar, QObject *parent = nullptr);
|
|
~MenuItem() override;
|
|
|
|
QAction *action() const { return m_action; }
|
|
QMenu *submenu();
|
|
|
|
QString caption() const { return m_action ? m_action->text() : QString(); }
|
|
void setCaption(const QString &value) { if (m_action) m_action->setText(value); }
|
|
bool enabled() const { return m_action ? m_action->isEnabled() : true; }
|
|
void setEnabled(bool value) { if (m_action) m_action->setEnabled(value); }
|
|
bool visible() const { return m_action ? m_action->isVisible() : true; }
|
|
void setVisible(bool value) { if (m_action) m_action->setVisible(value); }
|
|
bool checked() const { return m_action && m_action->isChecked(); }
|
|
void setChecked(bool value) { if (m_action) m_action->setChecked(value); }
|
|
bool radioItem() const { return m_radioItem; }
|
|
void setRadioItem(bool value);
|
|
int groupIndex() const { return m_groupIndex; }
|
|
void setGroupIndex(int value) { m_groupIndex = value; }
|
|
QKeySequence shortCut() const { return m_action ? m_action->shortcut() : QKeySequence(); }
|
|
void setShortCut(const QKeySequence &value) { if (m_action) m_action->setShortcut(value); }
|
|
|
|
MenuItem *add(const QString &caption);
|
|
void insert(int index, MenuItem *item);
|
|
void remove(MenuItem *item);
|
|
void deleteItem(int index);
|
|
void clear();
|
|
int indexOf(MenuItem *item) const { return m_items.indexOf(item); }
|
|
int count() const { return m_items.count(); }
|
|
MenuItem *items(int index) const { return m_items.value(index); }
|
|
void click();
|
|
|
|
// TNotifyEvent equivalent.
|
|
std::function<void()> onClick;
|
|
|
|
private:
|
|
QAction *m_action = nullptr;
|
|
QMenu *m_submenu = nullptr;
|
|
QMenuBar *m_bar = nullptr;
|
|
bool m_ownsSubmenu = false;
|
|
QList<MenuItem *> m_items;
|
|
bool m_radioItem = false;
|
|
int m_groupIndex = 0;
|
|
};
|
|
|
|
// TMenu equivalent (a window's main menu bar).
|
|
class Menu {
|
|
public:
|
|
explicit Menu(QMenuBar *bar) : m_root(new MenuItem(bar)) {}
|
|
~Menu() { delete m_root; }
|
|
MenuItem &items() { return *m_root; }
|
|
|
|
private:
|
|
MenuItem *m_root;
|
|
};
|
|
|
|
// TPopupMenu equivalent.
|
|
class PopupMenu : public MenuItem {
|
|
Q_OBJECT
|
|
public:
|
|
explicit PopupMenu(QObject *parent = nullptr);
|
|
|
|
bool autoPopup() const { return m_autoPopup; }
|
|
void setAutoPopup(bool value) { m_autoPopup = value; }
|
|
|
|
void popup(const QPoint &pos);
|
|
|
|
private:
|
|
bool m_autoPopup = true;
|
|
};
|
|
|
|
QKeySequence shortCut(int key, Qt::KeyboardModifiers modifiers);
|
|
|
|
} // namespace Dpi
|