15 #include <QAbstractItemModel>
17 #include <QActionGroup>
18 #include <QCoreApplication>
21 #include <QModelIndex>
22 #include <QPersistentModelIndex>
25 #include "../util/btassert.h"
26 #include "../util/btconnect.h"
41 :
QMenu(title, parent)
60 auto const triggerRebuild =
62 QCoreApplication::postEvent(
this,
64 Qt::HighEventPriority);
82 return QMenu::event(e);
94 QVariant displayData(
m_model->data(itemIndex, Qt::DisplayRole));
95 QVariant iconData(
m_model->data(itemIndex, Qt::DecorationRole));
96 QVariant toolTipData(
m_model->data(itemIndex, Qt::ToolTipRole));
97 QVariant statusTipData(
m_model->data(itemIndex, Qt::StatusTipRole));
98 QVariant whatsThisData(
m_model->data(itemIndex, Qt::WhatsThisRole));
103 if (displayData.canConvert<QString>()) {
104 childAction->setText(displayData.toString());
108 if (iconData.canConvert<QIcon>()) {
109 childAction->setIcon(iconData.value<QIcon>());
113 if (toolTipData.canConvert<QString>()) {
114 childAction->setToolTip(toolTipData.toString());
118 if (statusTipData.canConvert<QString>()) {
119 childAction->setStatusTip(statusTipData.toString());
123 if (whatsThisData.canConvert<QString>()) {
124 childAction->setWhatsThis(whatsThisData.toString());
128 if (
m_model->flags(itemIndex).testFlag(Qt::ItemIsUserCheckable)) {
129 childAction->setCheckable(
true);
133 QVariant checkData(
m_model->data(itemIndex, Qt::CheckStateRole));
135 Qt::CheckState
const state =
136 static_cast<Qt::CheckState
>(checkData.toInt(&ok));
138 childAction->setChecked(state == Qt::Checked);
144 QVariant displayData(
m_model->data(itemIndex, Qt::DisplayRole));
145 QVariant iconData(
m_model->data(itemIndex, Qt::DecorationRole));
146 QVariant toolTipData(
m_model->data(itemIndex, Qt::ToolTipRole));
147 QVariant statusTipData(
m_model->data(itemIndex, Qt::StatusTipRole));
148 QVariant whatsThisData(
m_model->data(itemIndex, Qt::WhatsThisRole));
153 if (displayData.canConvert<QString>()) {
154 childMenu->setTitle(displayData.toString());
158 if (iconData.canConvert<QIcon>()) {
159 childMenu->setIcon(iconData.value<QIcon>());
163 if (toolTipData.canConvert<QString>()) {
164 childMenu->setToolTip(toolTipData.toString());
168 if (statusTipData.canConvert<QString>()) {
169 childMenu->setStatusTip(statusTipData.toString());
173 if (whatsThisData.canConvert<QString>()) {
174 childMenu->setWhatsThis(whatsThisData.toString());
193 [
this](
QAction *
const action){
194 auto const indexProperty =
196 if (!indexProperty.isValid())
199 indexProperty.toPersistentModelIndex());
207 int children =
m_model->rowCount(parentIndex);
208 for (
int i = 0; i < children; i++) {
209 QModelIndex childIndex(
m_model->index(i, 0, parentIndex));
211 if (
m_model->rowCount(childIndex) > 0) {
214 if (childMenu !=
nullptr) {
216 parentMenu->addMenu(childMenu);
222 if (childAction !=
nullptr) {
228 parentMenu->addAction(childAction);
232 QPersistentModelIndex(childIndex));
242 for (
auto *
const childMenu
243 : findChildren<QMenu *>(QString(), Qt::FindDirectChildrenOnly))
244 childMenu->deleteLater();