125 lines
4.7 KiB
C++
125 lines
4.7 KiB
C++
// This file is part of cevo-evo.
|
|
//
|
|
// This software is governed by the CeCILL license under French law and
|
|
// abiding by the rules of distribution of free software. You can use,
|
|
// modify and/or redistribute the software under the terms of the CeCILL
|
|
// license as circulated by CEA, CNRS and INRIA at the following URL
|
|
// "http://www.cecill.info".
|
|
//
|
|
// As a counterpart to the access to the source code and rights to copy,
|
|
// modify and redistribute granted by the license, users are provided only
|
|
// with a limited warranty and the software's author, the holder of the
|
|
// economic rights, and the successive licensors have only limited
|
|
// liability.
|
|
//
|
|
// In this respect, the user's attention is drawn to the risks associated
|
|
// with loading, using, modifying and/or developing or reproducing the
|
|
// software by the user in light of its specific status of free software,
|
|
// that may mean that it is complicated to manipulate, and that also
|
|
// therefore means that it is reserved for developers and experienced
|
|
// professionals having in-depth computer knowledge. Users are therefore
|
|
// encouraged to load and test the software's suitability as regards their
|
|
// requirements in conditions enabling the security of their systems and/or
|
|
// data to be ensured and, more generally, to use and operate it in the
|
|
// same conditions as regards security.
|
|
//
|
|
// The fact that you are presently reading this means that you have had
|
|
// knowledge of the CeCILL license and that you accept its terms.
|
|
|
|
#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
|