32 #include "../berryDisplay.h" 33 #include "../berryAsyncRunnable.h" 35 #include "../handlers/berryIHandlerService.h" 36 #include "../services/berryIServiceLocator.h" 38 #include "../berryWorkbenchPlugin.h" 43 #include <QApplication> 45 #include <QMetaMethod> 74 this->icon = contributionParameters->icon;
75 this->label = contributionParameters->label;
76 this->mnemonic = contributionParameters->mnemonic;
77 this->shortcut = contributionParameters->shortcut;
78 this->tooltip = contributionParameters->tooltip;
79 this->style = contributionParameters->style;
80 this->helpContextId = contributionParameters->helpContextId;
81 this->visibleEnabled = contributionParameters->visibleEnabled;
82 this->mode = contributionParameters->mode;
84 menuService = contributionParameters->serviceLocator->GetService<
IMenuService>();
85 commandService = contributionParameters->serviceLocator->GetService<
ICommandService>();
86 handlerService = contributionParameters->serviceLocator->GetService<
IHandlerService>();
90 this->CreateCommand(contributionParameters->commandId,
91 contributionParameters->parameters);
106 :
UIElement(serviceLocator), item(item) {}
108 void SetText(
const QString& text)
override 113 void SetToolTip(
const QString& text)
override 115 item->SetToolTip(text);
118 void SetIcon(
const QIcon& icon)
override 123 void SetChecked(
bool checked)
override 125 item->SetChecked(checked);
128 void SetDropDownId(
const QString&
id)
override 130 item->dropDownMenuOverride = id;
136 contributionParameters->serviceLocator));
138 elementRef = commandService->RegisterElementForCommand(command, callback);
139 command->GetCommand()->AddCommandListener(this->GetCommandListener());
140 this->SetImages(contributionParameters->serviceLocator,
141 contributionParameters->iconStyle);
143 if (contributionParameters->helpContextId.isEmpty())
147 this->helpContextId = commandService->GetHelpContextId(
148 contributionParameters->commandId);
150 catch (
const NotDefinedException& )
162 catch (
const NotDefinedException& )
165 +
"\", command \"" + contributionParameters->commandId
173 if (!command || action || parent ==
nullptr)
179 Style tmpStyle = style;
183 QAction* item =
nullptr;
186 item =
new QAction(icon, label, parent);
187 parent->insertAction(before, item);
191 item = parent->addAction(icon, label);
195 if (!shortcut.isEmpty())
197 item->setShortcut(shortcut);
200 item->setProperty(
"contributionItem", QVariant::fromValue(
Object::Pointer(
this)));
207 connect(item, SIGNAL(triggered()), SLOT(HandleWidgetSelection()));
208 connect(item, SIGNAL(destroyed()), SLOT(HandleActionDestroyed()));
219 if (!command || action || parent ==
nullptr)
224 QAction* item =
nullptr;
227 item = parent->addAction(icon, label);
231 item =
new QAction(icon, label, parent);
232 parent->insertAction(before, item);
235 item->setProperty(
"contributionItem", QVariant::fromValue(
Object::Pointer(
this)));
237 connect(item, SIGNAL(triggered()), SLOT(HandleWidgetSelection()));
238 connect(item, SIGNAL(destroyed()), SLOT(HandleActionDestroyed()));
249 this->
Update(QString::null);
256 QWidget* parent = action->parentWidget();
257 if(qobject_cast<QMenu*>(parent))
259 this->UpdateMenuItem();
261 else if (qobject_cast<QMenuBar*>(parent))
263 this->UpdateMenuItem();
265 else if (qobject_cast<QToolBar*>(parent))
267 this->UpdateToolItem();
273 void CommandContributionItem::UpdateMenuItem()
275 QString text = label;
278 if (command.IsNotNull())
282 text = command->GetCommand()->GetName();
284 catch (
const NotDefinedException& e)
294 text = UpdateMnemonic(text);
318 if (action->isChecked() != checkedState)
320 action->setChecked(checkedState);
325 if (action->isEnabled() != shouldBeEnabled)
327 action->setEnabled(shouldBeEnabled);
331 void CommandContributionItem::UpdateToolItem()
333 QString text = label;
334 QString tooltip = label;
337 if (command.IsNotNull())
341 text = command->GetCommand()->GetName();
342 tooltip = command->GetCommand()->GetDescription();
343 if (tooltip.trimmed().isEmpty())
348 catch (
const NotDefinedException& e)
362 action->setText(text);
365 QString toolTipText = GetToolTipText(tooltip);
366 action->setToolTip(toolTipText);
368 if (action->isChecked() != checkedState)
370 action->setChecked(checkedState);
375 if (action->isEnabled() != shouldBeEnabled)
377 action->setEnabled(shouldBeEnabled);
385 commandService->UnregisterElement(elementRef);
389 command->GetCommand()->RemoveCommandListener(commandListener.data());
397 command->GetCommand()->SetEnabled(menuService->GetCurrentState());
398 return command->GetCommand()->IsEnabled();
413 const QString& iconStyle)
420 icon = service->
GetImage(command->GetId(), iconStyle);
427 if (!commandListener)
443 if (commandEvent->IsHandledChanged() || commandEvent->IsEnabledChanged()
444 || commandEvent->IsDefinedChanged())
446 item->UpdateCommandProperties(commandEvent);
452 commandListener.reset(
new MyCommandListener(
this));
454 return commandListener.data();
457 void CommandContributionItem::UpdateCommandProperties(
const SmartPointer<
460 if (commandEvent->IsHandledChanged())
462 dropDownMenuOverride =
"";
484 if (commandEvent->GetCommand()->IsDefined())
488 if (commandEvent->IsEnabledChanged()
489 || commandEvent->IsHandledChanged())
502 void CommandContributionItem::HandleActionDestroyed()
504 this->action =
nullptr;
531 void CommandContributionItem::CreateCommand(
const QString &commandId,
532 const QHash<QString,Object::Pointer> ¶meters)
534 if (commandId.isEmpty())
539 BERRY_ERROR <<
"Unable to create menu item \"" << this->
GetId().toStdString()
540 <<
"\", no command id";
544 if (!cmd->IsDefined())
549 BERRY_ERROR <<
"Unable to create menu item \"" << this->
GetId().toStdString()
550 <<
"\", command \"" << commandId.toStdString() <<
"\" not defined";
557 QString CommandContributionItem::GetToolTipText(
const QString& text)
const 559 QString tooltipText = tooltip;
560 if (tooltip.isNull())
588 QString CommandContributionItem::UpdateMnemonic(
const QString &s)
590 if (mnemonic.isNull() || s.isEmpty())
595 int idx = s.indexOf(mnemonic);
601 return s.left(idx) +
'&' + s.mid(idx);
613 void CommandContributionItem::HandleWidgetSelection()
621 checkedState = action->isChecked();
628 catch (
const ExecutionException& e)
632 catch (
const NotDefinedException& e)
636 catch (
const NotEnabledException& e)
640 catch (
const NotHandledException& e)
693 void CommandContributionItem::SetIcon(
const QIcon &icon)
699 void CommandContributionItem::UpdateIcons()
701 action->setIcon(icon);
704 void CommandContributionItem::SetText(
const QString &text)
710 void CommandContributionItem::SetChecked(
bool checked)
712 if (checkedState == checked)
716 checkedState = checked;
717 action->setChecked(checkedState);
720 void CommandContributionItem::SetToolTip(
const QString &text)
723 action->setToolTip(text);
static MsgHandler handler
virtual void Update(bool force)=0
IContributionManager * GetParent() const
bool IsVisible() const override
static void Log(const QString &message)
virtual bool InDisplayThread()=0
void UpdateCommandPropertiesInUI(const SmartPointer< const CommandEvent > &commandEvent)
virtual QIcon GetImage(const QString &commandId)=0
SmartPointer< Other > Cast() const
bool IsVisible() const override
void Fill(QMenu *parent, QAction *before) override
static Display * GetDefault()
Implements transparent reference counting.
virtual void AsyncExec(Poco::Runnable *)=0
static ParameterizedCommand::Pointer GenerateCommand(const SmartPointer< Command > command, const QHash< QString, Object::Pointer > ¶meters)
bool IsEnabled() const override
CommandContributionItem(const SmartPointer< CommandContributionItemParameter > &contributionParameters)
~CommandContributionItem() override
berry::Display * display
[MinimalApplicationClass_StartMethod]
QString GetId() const override