36 #include "../berryDisplay.h"
37 #include "../berryAsyncRunnable.h"
39 #include "../handlers/berryIHandlerService.h"
40 #include "../services/berryIServiceLocator.h"
42 #include "../internal/berryWorkbenchPlugin.h"
47 #include <QApplication>
49 #include <QMetaMethod>
78 this->icon = contributionParameters->icon;
79 this->label = contributionParameters->label;
80 this->mnemonic = contributionParameters->mnemonic;
81 this->shortcut = contributionParameters->shortcut;
82 this->tooltip = contributionParameters->tooltip;
83 this->style = contributionParameters->style;
84 this->helpContextId = contributionParameters->helpContextId;
85 this->visibleEnabled = contributionParameters->visibleEnabled;
86 this->mode = contributionParameters->mode;
88 menuService = contributionParameters->serviceLocator->GetService<
IMenuService>();
89 commandService = contributionParameters->serviceLocator->GetService<
ICommandService>();
90 handlerService = contributionParameters->serviceLocator->GetService<
IHandlerService>();
94 this->CreateCommand(contributionParameters->commandId,
95 contributionParameters->parameters);
101 class CommandUIElement :
public UIElement
110 :
UIElement(serviceLocator), item(item) {}
112 void SetText(
const QString& text)
override
117 void SetToolTip(
const QString& text)
override
119 item->SetToolTip(text);
122 void SetIcon(
const QIcon& icon)
override
127 void SetChecked(
bool checked)
override
129 item->SetChecked(checked);
132 void SetDropDownId(
const QString&
id)
override
134 item->dropDownMenuOverride = id;
140 contributionParameters->serviceLocator));
142 elementRef = commandService->RegisterElementForCommand(command, callback);
143 command->GetCommand()->AddCommandListener(this->GetCommandListener());
144 this->SetImages(contributionParameters->serviceLocator,
145 contributionParameters->iconStyle);
147 if (contributionParameters->helpContextId.isEmpty())
151 this->helpContextId = commandService->GetHelpContextId(
152 contributionParameters->commandId);
154 catch (
const NotDefinedException& )
166 catch (
const NotDefinedException& )
168 WorkbenchPlugin::Log(QString(
"Unable to register menu item \"") + this->
GetId()
169 +
"\", command \"" + contributionParameters->commandId
177 if (!command || action || parent ==
nullptr)
183 Style tmpStyle = style;
187 QAction* item =
nullptr;
190 item =
new QAction(icon, label, parent);
191 parent->insertAction(before, item);
195 item = parent->addAction(icon, label);
199 if (!shortcut.isEmpty())
201 item->setShortcut(shortcut);
204 item->setProperty(
"contributionItem", QVariant::fromValue(
Object::Pointer(
this)));
211 connect(item, SIGNAL(triggered()), SLOT(HandleWidgetSelection()));
212 connect(item, SIGNAL(destroyed()), SLOT(HandleActionDestroyed()));
223 if (!command || action || parent ==
nullptr)
228 QAction* item =
nullptr;
231 item = parent->addAction(icon, label);
235 item =
new QAction(icon, label, parent);
236 parent->insertAction(before, item);
239 item->setProperty(
"contributionItem", QVariant::fromValue(
Object::Pointer(
this)));
241 connect(item, SIGNAL(triggered()), SLOT(HandleWidgetSelection()));
242 connect(item, SIGNAL(destroyed()), SLOT(HandleActionDestroyed()));
253 this->
Update(QString::null);
260 QWidget* parent = action->parentWidget();
261 if(qobject_cast<QMenu*>(parent))
263 this->UpdateMenuItem();
265 else if (qobject_cast<QMenuBar*>(parent))
267 this->UpdateMenuItem();
269 else if (qobject_cast<QToolBar*>(parent))
271 this->UpdateToolItem();
277 void CommandContributionItem::UpdateMenuItem()
279 QString text = label;
282 if (command.IsNotNull())
286 text = command->GetCommand()->GetName();
288 catch (
const NotDefinedException& e)
298 text = UpdateMnemonic(text);
322 if (action->isChecked() != checkedState)
324 action->setChecked(checkedState);
329 if (action->isEnabled() != shouldBeEnabled)
331 action->setEnabled(shouldBeEnabled);
335 void CommandContributionItem::UpdateToolItem()
337 QString text = label;
338 QString tooltip = label;
341 if (command.IsNotNull())
345 text = command->GetCommand()->GetName();
346 tooltip = command->GetCommand()->GetDescription();
347 if (tooltip.trimmed().isEmpty())
352 catch (
const NotDefinedException& e)
363 if ((icon.isNull() || (mode & MODE_FORCE_TEXT) == MODE_FORCE_TEXT)
366 action->setText(text);
369 QString toolTipText = GetToolTipText(tooltip);
370 action->setToolTip(toolTipText);
372 if (action->isChecked() != checkedState)
374 action->setChecked(checkedState);
379 if (action->isEnabled() != shouldBeEnabled)
381 action->setEnabled(shouldBeEnabled);
393 command->GetCommand()->RemoveCommandListener(commandListener.data());
402 return command->GetCommand()->IsEnabled();
417 const QString& iconStyle)
424 icon = service->
GetImage(command->GetId(), iconStyle);
429 ICommandListener* CommandContributionItem::GetCommandListener()
431 if (!commandListener)
433 class MyCommandListener :
public ICommandListener
445 void CommandChanged(
const SmartPointer<const CommandEvent>& commandEvent)
override
447 if (commandEvent->IsHandledChanged() || commandEvent->IsEnabledChanged()
448 || commandEvent->IsDefinedChanged())
450 item->UpdateCommandProperties(commandEvent);
456 commandListener.reset(
new MyCommandListener(
this));
458 return commandListener.data();
461 void CommandContributionItem::UpdateCommandProperties(
const SmartPointer<
462 const CommandEvent> commandEvent)
464 if (commandEvent->IsHandledChanged())
466 dropDownMenuOverride =
"";
475 if (display->InDisplayThread())
481 display->AsyncExec(update);
488 if (commandEvent->GetCommand()->IsDefined())
492 if (commandEvent->IsEnabledChanged()
493 || commandEvent->IsHandledChanged())
506 void CommandContributionItem::HandleActionDestroyed()
508 this->action =
nullptr;
511 bool CommandContributionItem::ShouldRestoreAppearance(
const SmartPointer<IHandler>&
handler)
515 if (handler.IsNull())
518 if (!(handler.Cast<IElementUpdater>()))
530 SmartPointer<ParameterizedCommand> CommandContributionItem::GetCommand()
const
535 void CommandContributionItem::CreateCommand(
const QString &commandId,
536 const QHash<QString,Object::Pointer> ¶meters)
538 if (commandId.isEmpty())
543 BERRY_ERROR <<
"Unable to create menu item \"" << this->
GetId().toStdString()
544 <<
"\", no command id";
548 if (!cmd->IsDefined())
553 BERRY_ERROR <<
"Unable to create menu item \"" << this->
GetId().toStdString()
554 <<
"\", command \"" << commandId.toStdString() <<
"\" not defined";
561 QString CommandContributionItem::GetToolTipText(
const QString& text)
const
563 QString tooltipText = tooltip;
564 if (tooltip.isNull())
592 QString CommandContributionItem::UpdateMnemonic(
const QString &s)
594 if (mnemonic.isNull() || s.isEmpty())
599 int idx = s.indexOf(mnemonic);
605 return s.left(idx) +
'&' + s.mid(idx);
617 void CommandContributionItem::HandleWidgetSelection()
625 checkedState = action->isChecked();
632 catch (
const ExecutionException& e)
634 WorkbenchPlugin::Log(
"Failed to execute item " +
GetId(), e);
636 catch (
const NotDefinedException& e)
638 WorkbenchPlugin::Log(
"Failed to execute item " +
GetId(), e);
640 catch (
const NotEnabledException& e)
642 WorkbenchPlugin::Log(
"Failed to execute item " +
GetId(), e);
644 catch (
const NotHandledException& e)
646 WorkbenchPlugin::Log(
"Failed to execute item " +
GetId(), e);
697 void CommandContributionItem::SetIcon(
const QIcon &icon)
703 void CommandContributionItem::UpdateIcons()
705 action->setIcon(icon);
708 void CommandContributionItem::SetText(
const QString &text)
714 void CommandContributionItem::SetChecked(
bool checked)
716 if (checkedState == checked)
720 checkedState = checked;
721 action->setChecked(checkedState);
724 void CommandContributionItem::SetToolTip(
const QString &text)
727 action->setToolTip(text);
static MsgHandler handler
virtual void Update(bool force)=0
bool IsVisible() const override
virtual void UnregisterElement(const SmartPointer< IElementReference > &elementReference)=0
void UpdateCommandPropertiesInUI(const SmartPointer< const CommandEvent > &commandEvent)
virtual QIcon GetImage(const QString &commandId)=0
bool IsVisible() const override
void Fill(QMenu *parent, QAction *before) override
static Display * GetDefault()
Implements transparent reference counting.
virtual Object::Pointer ExecuteCommand(const QString &commandId, const SmartPointer< const UIElement > &uielement)=0
static ParameterizedCommand::Pointer GenerateCommand(const SmartPointer< Command > command, const QHash< QString, Object::Pointer > ¶meters)
IContributionManager * GetParent() const
bool IsEnabled() const override
berry::SmartPointer< Self > Pointer
virtual SmartPointer< Command > GetCommand(const QString &commandId) const =0
~CommandContributionItem()
CommandContributionItem(const SmartPointer< CommandContributionItemParameter > &contributionParameters)
QString GetId() const override