| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2002 Jürgen Riegel <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <Inventor/SbSphere.h> |
| 24 |
#include <Inventor/actions/SoGetBoundingBoxAction.h> |
| 25 |
#include <Inventor/nodes/SoOrthographicCamera.h> |
| 26 |
#include <sstream> |
| 27 |
#include <QApplication> |
| 28 |
#include <QByteArray> |
| 29 |
#include <QDir> |
| 30 |
#include <QKeySequence> |
| 31 |
#include <QMessageBox> |
| 32 |
|
| 33 |
#include <boost/algorithm/string/replace.hpp> |
| 34 |
|
| 35 |
#include <FCConfig.h> |
| 36 |
|
| 37 |
#include <App/Document.h> |
| 38 |
#include <App/DocumentObject.h> |
| 39 |
#include <App/AutoTransaction.h> |
| 40 |
#include <Base/Console.h> |
| 41 |
#include <Base/Exception.h> |
| 42 |
#include <Base/Interpreter.h> |
| 43 |
#include <Base/PyObjectBase.h> |
| 44 |
#include <Base/Tools.h> |
| 45 |
|
| 46 |
#include "Command.h" |
| 47 |
#include "Action.h" |
| 48 |
#include "Application.h" |
| 49 |
#include "BitmapFactory.h" |
| 50 |
#include "Control.h" |
| 51 |
#include "Dialogs/DlgUndoRedo.h" |
| 52 |
#include "Document.h" |
| 53 |
#include "frameobject.h" |
| 54 |
#include "Macro.h" |
| 55 |
#include "MainWindow.h" |
| 56 |
#include "Python.h" |
| 57 |
#include "Selection.h" |
| 58 |
#include "View3DInventor.h" |
| 59 |
#include "View3DInventorViewer.h" |
| 60 |
#include "ViewProviderLink.h" |
| 61 |
#include "WaitCursor.h" |
| 62 |
#include "WhatsThis.h" |
| 63 |
#include "WorkbenchManager.h" |
| 64 |
#include "Workbench.h" |
| 65 |
#include "ShortcutManager.h" |
| 66 |
|
| 67 |
|
| 68 |
FC_LOG_LEVEL_INIT("Command", true, true) |
| 69 |
|
| 70 |
using Base::Interpreter; |
| 71 |
using namespace Gui; |
| 72 |
using namespace Gui::Dialog; |
| 73 |
using namespace Gui::DockWnd; |
| 74 |
|
| 75 |
/** \defgroup commands Command Framework |
| 76 |
\ingroup GUI |
| 77 |
\brief Structure for registering commands to the FreeCAD system |
| 78 |
* \section Overview |
| 79 |
* In GUI applications many commands can be invoked via a menu item, a toolbar button or an |
| 80 |
accelerator key. The answer of Qt to master this |
| 81 |
* challenge is the class \a QAction. A QAction object can be added to a popup menu or a toolbar and |
| 82 |
keep the state of the menu item and |
| 83 |
* the toolbar button synchronized. |
| 84 |
* |
| 85 |
* For example, if the user clicks the menu item of a toggle action then the toolbar button gets |
| 86 |
also pressed |
| 87 |
* and vice versa. For more details refer to your Qt documentation. |
| 88 |
* |
| 89 |
* \section Drawbacks |
| 90 |
* Since QAction inherits QObject and emits the \a triggered() signal or \a toggled() signal for |
| 91 |
toggle actions it is very convenient to connect |
| 92 |
* these signals e.g. with slots of your MainWindow class. But this means that for every action an |
| 93 |
appropriate slot of MainWindow is necessary |
| 94 |
* and leads to an inflated MainWindow class. Furthermore, it's simply impossible to provide plugins |
| 95 |
that may also need special slots -- without |
| 96 |
* changing the MainWindow class. |
| 97 |
* |
| 98 |
* \section wayout Way out |
| 99 |
* To solve these problems we have introduced the command framework to decouple QAction and |
| 100 |
MainWindow. The base classes of the framework are |
| 101 |
* \a Gui::CommandBase and \a Gui::Action that represent the link between Qt's QAction world and the |
| 102 |
FreeCAD's command world. |
| 103 |
* |
| 104 |
* The Action class holds a pointer to QAction and CommandBase and acts as a mediator and -- to save |
| 105 |
memory -- that gets created |
| 106 |
* (@ref Gui::CommandBase::createAction()) not before it is added (@ref Gui::Command::addTo()) to a |
| 107 |
menu or toolbar. |
| 108 |
* |
| 109 |
* Now, the implementation of the slots of MainWindow can be done in the method \a activated() of |
| 110 |
subclasses of Command instead. |
| 111 |
* |
| 112 |
* For example, the implementation of the "Open file" command can be done as follows. |
| 113 |
* \code |
| 114 |
* class OpenCommand : public Command |
| 115 |
* { |
| 116 |
* public: |
| 117 |
* OpenCommand() : Command("Std_Open") |
| 118 |
* { |
| 119 |
* // set up menu text, status tip, ... |
| 120 |
* sMenuText = "&Open"; |
| 121 |
* sToolTipText = "Open a file"; |
| 122 |
* sWhatsThis = "Open a file"; |
| 123 |
* sStatusTip = "Open a file"; |
| 124 |
* sPixmap = "Open"; // name of a registered pixmap |
| 125 |
* sAccel = "Shift+P"; // or "P" or "P, L" or "Ctrl+X, Ctrl+C" for a sequence |
| 126 |
* } |
| 127 |
* protected: |
| 128 |
* void activated(int) |
| 129 |
* { |
| 130 |
* QString filter ... // make a filter of all supported file formats |
| 131 |
* QStringList FileList = QFileDialog::getOpenFileNames( filter,QString(), getMainWindow() ); |
| 132 |
* for ( QStringList::Iterator it = FileList.begin(); it != FileList.end(); ++it ) { |
| 133 |
* getGuiApplication()->open((*it).latin1()); |
| 134 |
* } |
| 135 |
* } |
| 136 |
* }; |
| 137 |
* \endcode |
| 138 |
* An instance of \a OpenCommand must be created and added to the \ref Gui::CommandManager to make |
| 139 |
the class known to FreeCAD. |
| 140 |
* To see how menus and toolbars can be built go to the @ref workbench. |
| 141 |
* |
| 142 |
* @see Gui::Command, Gui::CommandManager |
| 143 |
*/ |
| 144 |
|
| 145 |
// list of modules already loaded by a command (not issue again for macro cleanness) |
| 146 |
std::set<std::string> alreadyLoadedModule; |
| 147 |
|
| 148 |
class StringCache |
| 149 |
{ |
| 150 |
public: |
| 151 |
static const char* New(const char* str) |
| 152 |
{ |
| 153 |
using StringList = std::list<std::string>; |
| 154 |
static StringList strings; |
| 155 |
strings.emplace_back(str); |
| 156 |
return strings.back().c_str(); |
| 157 |
} |
| 158 |
}; |
| 159 |
|
| 160 |
CommandBase::CommandBase( |
| 161 |
const char* sMenu, |
| 162 |
const char* sToolTip, |
| 163 |
const char* sWhat, |
| 164 |
const char* sStatus, |
| 165 |
const char* sPixmap, |
| 166 |
const char* sAcc |
| 167 |
) |
| 168 |
: sMenuText(sMenu) |
| 169 |
, sToolTipText(sToolTip) |
| 170 |
, sWhatsThis(sWhat ? sWhat : sToolTip) |
| 171 |
, sStatusTip(sStatus ? sStatus : sToolTip) |
| 172 |
, sPixmap(sPixmap) |
| 173 |
, sAccel(sAcc) |
| 174 |
, _pcAction(nullptr) |
| 175 |
{} |
| 176 |
|
| 177 |
CommandBase::~CommandBase() |
| 178 |
{ |
| 179 |
// Note: The Action object becomes a children of MainWindow which gets destroyed _before_ the |
| 180 |
// command manager hence before any command object. So the action pointer is a dangling pointer |
| 181 |
// at this state. |
| 182 |
|
| 183 |
// Command can be destroyed before the MainWindow, for example, dynamic |
| 184 |
// command created (and later deleted) by user for a pie menu. |
| 185 |
if (getMainWindow()) { |
| 186 |
delete _pcAction; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
Action* CommandBase::getAction() const |
| 191 |
{ |
| 192 |
return _pcAction; |
| 193 |
} |
| 194 |
|
| 195 |
Action* CommandBase::createAction() |
| 196 |
{ |
| 197 |
// does nothing |
| 198 |
return nullptr; |
| 199 |
} |
| 200 |
|
| 201 |
void CommandBase::setMenuText(const char* s) |
| 202 |
{ |
| 203 |
this->sMenuText = StringCache::New(s); |
| 204 |
} |
| 205 |
|
| 206 |
void CommandBase::setToolTipText(const char* s) |
| 207 |
{ |
| 208 |
this->sToolTipText = StringCache::New(s); |
| 209 |
} |
| 210 |
|
| 211 |
void CommandBase::setStatusTip(const char* s) |
| 212 |
{ |
| 213 |
this->sStatusTip = StringCache::New(s); |
| 214 |
} |
| 215 |
|
| 216 |
void CommandBase::setWhatsThis(const char* s) |
| 217 |
{ |
| 218 |
this->sWhatsThis = StringCache::New(s); |
| 219 |
} |
| 220 |
|
| 221 |
void CommandBase::setPixmap(const char* s) |
| 222 |
{ |
| 223 |
this->sPixmap = StringCache::New(s); |
| 224 |
} |
| 225 |
|
| 226 |
void CommandBase::setAccel(const char* s) |
| 227 |
{ |
| 228 |
this->sAccel = StringCache::New(s); |
| 229 |
} |
| 230 |
|
| 231 |
//=========================================================================== |
| 232 |
// Command |
| 233 |
//=========================================================================== |
| 234 |
|
| 235 |
/* TRANSLATOR Gui::Command */ |
| 236 |
|
| 237 |
Command::Command(const char* name) |
| 238 |
: CommandBase(nullptr) |
| 239 |
, sName(name) |
| 240 |
, sHelpUrl(nullptr) |
| 241 |
{ |
| 242 |
sAppModule = "FreeCAD"; |
| 243 |
sGroup = "Standard"; |
| 244 |
eType = AlterDoc | Alter3DView | AlterSelection; |
| 245 |
bEnabled = true; |
| 246 |
bCanLog = true; |
| 247 |
} |
| 248 |
|
| 249 |
Command::~Command() = default; |
| 250 |
|
| 251 |
void Command::setShortcut(const QString& shortcut) |
| 252 |
{ |
| 253 |
if (_pcAction) { |
| 254 |
_pcAction->setShortcut(shortcut); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
QString Command::getShortcut() const |
| 259 |
{ |
| 260 |
if (_pcAction) { |
| 261 |
return _pcAction->shortcut().toString(); |
| 262 |
} |
| 263 |
return ShortcutManager::instance()->getShortcut(getName()); |
| 264 |
} |
| 265 |
|
| 266 |
bool Command::isViewOfType(Base::Type t) const |
| 267 |
{ |
| 268 |
Gui::Document* d = getGuiApplication()->activeDocument(); |
| 269 |
if (!d) { |
| 270 |
return false; |
| 271 |
} |
| 272 |
Gui::BaseView* v = d->getActiveView(); |
| 273 |
if (!v) { |
| 274 |
return false; |
| 275 |
} |
| 276 |
if (v->isDerivedFrom(t)) { |
| 277 |
return true; |
| 278 |
} |
| 279 |
else { |
| 280 |
return false; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
void Command::initAction() |
| 285 |
{ |
| 286 |
if (!_pcAction) { |
| 287 |
_pcAction = createAction(); |
| 288 |
#ifdef FC_DEBUG |
| 289 |
// Accelerator conflict can now be dynamically resolved in ShortcutManager |
| 290 |
// |
| 291 |
// printConflictingAccelerators(); |
| 292 |
#endif |
| 293 |
setShortcut(ShortcutManager::instance()->getShortcut(getName(), getAccel())); |
| 294 |
testActive(); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
void Command::addTo(QWidget* pcWidget) |
| 299 |
{ |
| 300 |
initAction(); |
| 301 |
_pcAction->addTo(pcWidget); |
| 302 |
} |
| 303 |
|
| 304 |
void Command::addToGroup(ActionGroup* group, bool checkable) |
| 305 |
{ |
| 306 |
addToGroup(group); |
| 307 |
_pcAction->setCheckable(checkable); |
| 308 |
} |
| 309 |
|
| 310 |
void Command::addToGroup(ActionGroup* group) |
| 311 |
{ |
| 312 |
initAction(); |
| 313 |
group->addAction(_pcAction->findChild<QAction*>()); |
| 314 |
} |
| 315 |
|
| 316 |
Application* Command::getGuiApplication() |
| 317 |
{ |
| 318 |
return Application::Instance; |
| 319 |
} |
| 320 |
|
| 321 |
App::Document* Command::getActiveDocument() const |
| 322 |
{ |
| 323 |
Gui::Document* doc = getActiveGuiDocument(); |
| 324 |
return doc ? doc->getDocument() : nullptr; |
| 325 |
} |
| 326 |
|
| 327 |
Gui::Document* Command::getActiveGuiDocument() const |
| 328 |
{ |
| 329 |
return getGuiApplication()->activeDocument(); |
| 330 |
} |
| 331 |
|
| 332 |
App::Document* Command::getDocument(const char* Name) const |
| 333 |
{ |
| 334 |
if (Name) { |
| 335 |
return App::GetApplication().getDocument(Name); |
| 336 |
} |
| 337 |
|
| 338 |
return getActiveDocument(); |
| 339 |
} |
| 340 |
|
| 341 |
App::DocumentObject* Command::getObject(const char* Name) const |
| 342 |
{ |
| 343 |
App::Document* pDoc = getDocument(); |
| 344 |
return pDoc ? pDoc->getObject(Name) : nullptr; |
| 345 |
} |
| 346 |
|
| 347 |
int Command::_busy; |
| 348 |
|
| 349 |
class PendingLine |
| 350 |
{ |
| 351 |
public: |
| 352 |
PendingLine(MacroManager::LineType type, const char* line) |
| 353 |
{ |
| 354 |
Application::Instance->macroManager()->addPendingLine(type, line); |
| 355 |
} |
| 356 |
~PendingLine() |
| 357 |
{ |
| 358 |
cancel(); |
| 359 |
} |
| 360 |
void cancel() |
| 361 |
{ |
| 362 |
Application::Instance->macroManager()->addPendingLine(MacroManager::Cmt, nullptr); |
| 363 |
} |
| 364 |
}; |
| 365 |
|
| 366 |
class CommandTrigger |
| 367 |
{ |
| 368 |
public: |
| 369 |
CommandTrigger(Command::TriggerSource& trigger, Command::TriggerSource source) |
| 370 |
: trigger(trigger) |
| 371 |
, saved(trigger) |
| 372 |
{ |
| 373 |
trigger = source; |
| 374 |
} |
| 375 |
|
| 376 |
~CommandTrigger() |
| 377 |
{ |
| 378 |
trigger = saved; |
| 379 |
} |
| 380 |
|
| 381 |
private: |
| 382 |
Command::TriggerSource& trigger; |
| 383 |
Command::TriggerSource saved; |
| 384 |
}; |
| 385 |
|
| 386 |
void Command::setupCheckable(int iMsg) |
| 387 |
{ |
| 388 |
QAction* action = nullptr; |
| 389 |
auto pcActionGroup = qobject_cast<Gui::ActionGroup*>(_pcAction); |
| 390 |
if (pcActionGroup) { |
| 391 |
QList<QAction*> a = pcActionGroup->actions(); |
| 392 |
assert(iMsg < a.size()); |
| 393 |
action = a[iMsg]; |
| 394 |
} |
| 395 |
else { |
| 396 |
action = _pcAction->action(); |
| 397 |
} |
| 398 |
|
| 399 |
if (!action) { |
| 400 |
return; |
| 401 |
} |
| 402 |
|
| 403 |
bool checkable = action->isCheckable(); |
| 404 |
_pcAction->setCheckable(checkable); |
| 405 |
if (checkable) { |
| 406 |
bool checked = false; |
| 407 |
switch (triggerSource()) { |
| 408 |
case TriggerNone: |
| 409 |
checked = !action->isChecked(); |
| 410 |
break; |
| 411 |
case TriggerAction: |
| 412 |
checked = _pcAction->isChecked(); |
| 413 |
break; |
| 414 |
case TriggerChildAction: |
| 415 |
checked = action->isChecked(); |
| 416 |
break; |
| 417 |
} |
| 418 |
bool blocked = action->blockSignals(true); |
| 419 |
action->setChecked(checked); |
| 420 |
action->blockSignals(blocked); |
| 421 |
if (action != _pcAction->action()) { |
| 422 |
_pcAction->setBlockedChecked(checked); |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
void Command::invoke(int i, TriggerSource trigger) |
| 428 |
{ |
| 429 |
CommandTrigger cmdTrigger(_trigger, trigger); |
| 430 |
if (displayText.empty()) { |
| 431 |
displayText = getMenuText() ? getMenuText() : ""; |
| 432 |
boost::replace_all(displayText, "&", ""); |
| 433 |
if (displayText.empty()) { |
| 434 |
displayText = getName(); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
// Do not query _pcAction since it isn't created necessarily |
| 439 |
#ifdef FC_LOGUSERACTION |
| 440 |
Base::Console().log("CmdG: %s\n", sName); |
| 441 |
#endif |
| 442 |
|
| 443 |
_invoke(i, bCanLog && !_busy); |
| 444 |
} |
| 445 |
|
| 446 |
void Command::_invoke(int id, bool disablelog) |
| 447 |
{ |
| 448 |
try { |
| 449 |
// Because Transaction now captures ViewObject changes, auto named |
| 450 |
// transaction is disabled here to avoid too many unnecessary transactions. |
| 451 |
// |
| 452 |
App::AutoTransaction committer(nullptr, true); |
| 453 |
|
| 454 |
// set the application module type for the macro |
| 455 |
getGuiApplication()->macroManager()->setModule(sAppModule); |
| 456 |
|
| 457 |
std::unique_ptr<LogDisabler> logdisabler; |
| 458 |
if (disablelog) { |
| 459 |
logdisabler = std::make_unique<LogDisabler>(); |
| 460 |
} |
| 461 |
|
| 462 |
// check if it really works NOW (could be a delay between click deactivation of the button) |
| 463 |
if (isActive()) { |
| 464 |
auto manager = getGuiApplication()->macroManager(); |
| 465 |
auto editDoc = getGuiApplication()->editDocument(); |
| 466 |
|
| 467 |
if (!logdisabler) { |
| 468 |
activated(id); |
| 469 |
} |
| 470 |
else { |
| 471 |
Gui::SelectionLogDisabler seldisabler; |
| 472 |
auto lines = manager->getLines(); |
| 473 |
std::ostringstream ss; |
| 474 |
ss << "### Begin command " << sName; |
| 475 |
// Add a pending line to mark the start of a command |
| 476 |
PendingLine pending(MacroManager::Cmt, ss.str().c_str()); |
| 477 |
ss.str(""); |
| 478 |
|
| 479 |
activated(id); |
| 480 |
|
| 481 |
if (manager->getLines() == lines) { |
| 482 |
// This command does not record any lines, lets do it for |
| 483 |
// it. The above LogDisabler is to prevent nested command |
| 484 |
// logging, i.e. we only auto log the first invoking |
| 485 |
// command. |
| 486 |
|
| 487 |
// Cancel the above pending line first |
| 488 |
pending.cancel(); |
| 489 |
ss << "Gui.runCommand('" << sName << "'," << id << ')'; |
| 490 |
manager->addLine(MacroManager::Gui, ss.str().c_str()); |
| 491 |
} |
| 492 |
else { |
| 493 |
// In case the command has any output to the console, lets |
| 494 |
// mark the end of the command here |
| 495 |
ss << "### End command " << sName; |
| 496 |
manager->addLine(MacroManager::Cmt, ss.str().c_str()); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
getMainWindow()->updateActions(); |
| 501 |
|
| 502 |
// If this command starts an editing, let the transaction persist |
| 503 |
if (!editDoc && getGuiApplication()->editDocument()) { |
| 504 |
committer.setEnable(false); |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
catch (const Base::SystemExitException&) { |
| 509 |
throw; |
| 510 |
} |
| 511 |
catch (Base::PyException& e) { |
| 512 |
e.reportException(); |
| 513 |
} |
| 514 |
catch (Py::Exception&) { |
| 515 |
Base::PyGILStateLocker lock; |
| 516 |
Base::PyException e; |
| 517 |
e.reportException(); |
| 518 |
} |
| 519 |
catch (Base::AbortException&) { |
| 520 |
} |
| 521 |
catch (Base::Exception& e) { |
| 522 |
e.reportException(); |
| 523 |
// Pop-up a dialog for FreeCAD-specific exceptions |
| 524 |
QMessageBox::critical(Gui::getMainWindow(), QObject::tr("Exception"), QLatin1String(e.what())); |
| 525 |
} |
| 526 |
catch (std::exception& e) { |
| 527 |
Base::Console().error("C++ exception thrown (%s)\n", e.what()); |
| 528 |
} |
| 529 |
catch (const char* e) { |
| 530 |
Base::Console().error("%s\n", e); |
| 531 |
} |
| 532 |
#ifndef FC_DEBUG |
| 533 |
catch (...) { |
| 534 |
Base::Console().error("Gui::Command::activated(%d): Unknown C++ exception thrown\n", id); |
| 535 |
} |
| 536 |
#endif |
| 537 |
} |
| 538 |
|
| 539 |
void Command::testActive() |
| 540 |
{ |
| 541 |
if (!_pcAction) { |
| 542 |
return; |
| 543 |
} |
| 544 |
|
| 545 |
if (_blockCmd || !bEnabled) { |
| 546 |
_pcAction->setEnabled(false); |
| 547 |
return; |
| 548 |
} |
| 549 |
|
| 550 |
if (!(eType & ForEdit)) { // special case for commands which are only in some edit modes active |
| 551 |
|
| 552 |
if ((!Gui::Control().isAllowedAlterDocument() && eType & AlterDoc) |
| 553 |
|| (!Gui::Control().isAllowedAlterView() && eType & Alter3DView) |
| 554 |
|| (!Gui::Control().isAllowedAlterSelection() && eType & AlterSelection)) { |
| 555 |
_pcAction->setEnabled(false); |
| 556 |
return; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
auto pcAction = qobject_cast<Gui::ActionGroup*>(_pcAction); |
| 561 |
if (pcAction) { |
| 562 |
Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager(); |
| 563 |
const auto actions = pcAction->actions(); |
| 564 |
for (auto action : actions) { |
| 565 |
auto name = action->property("CommandName").toByteArray(); |
| 566 |
if (!name.size()) { |
| 567 |
continue; |
| 568 |
} |
| 569 |
Command* cmd = rcCmdMgr.getCommandByName(name); |
| 570 |
if (cmd) { |
| 571 |
action->setEnabled(cmd->isActive()); |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
bool bActive = isActive(); |
| 577 |
_pcAction->setEnabled(bActive); |
| 578 |
} |
| 579 |
|
| 580 |
void Command::setEnabled(bool on) |
| 581 |
{ |
| 582 |
if (_pcAction) { |
| 583 |
bEnabled = on; |
| 584 |
_pcAction->setEnabled(on); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
//-------------------------------------------------------------------------- |
| 589 |
// Helper methods |
| 590 |
//-------------------------------------------------------------------------- |
| 591 |
|
| 592 |
bool Command::hasActiveDocument() const |
| 593 |
{ |
| 594 |
return getActiveGuiDocument() != nullptr; |
| 595 |
} |
| 596 |
/// true when there is a document and a Feature with Name |
| 597 |
bool Command::hasObject(const char* Name) |
| 598 |
{ |
| 599 |
return getDocument() != nullptr && getDocument()->getObject(Name) != nullptr; |
| 600 |
} |
| 601 |
|
| 602 |
Gui::SelectionSingleton& Command::getSelection() |
| 603 |
{ |
| 604 |
return Gui::Selection(); |
| 605 |
} |
| 606 |
|
| 607 |
std::string Command::getUniqueObjectName(const char* BaseName, const App::DocumentObject* obj) const |
| 608 |
{ |
| 609 |
auto doc = obj ? obj->getDocument() : App::GetApplication().getActiveDocument(); |
| 610 |
assert(doc); |
| 611 |
return doc->getUniqueObjectName(BaseName); |
| 612 |
} |
| 613 |
|
| 614 |
std::string Command::getObjectCmd( |
| 615 |
const char* Name, |
| 616 |
const App::Document* doc, |
| 617 |
const char* prefix, |
| 618 |
const char* postfix, |
| 619 |
bool gui |
| 620 |
) |
| 621 |
{ |
| 622 |
if (!doc) { |
| 623 |
doc = App::GetApplication().getActiveDocument(); |
| 624 |
} |
| 625 |
if (!doc || !Name) { |
| 626 |
return {"None"}; |
| 627 |
} |
| 628 |
std::ostringstream str; |
| 629 |
if (prefix) { |
| 630 |
str << prefix; |
| 631 |
} |
| 632 |
str << (gui ? "Gui" : "App") << ".getDocument('" << doc->getName() << "').getObject('" << Name |
| 633 |
<< "')"; |
| 634 |
if (postfix) { |
| 635 |
str << postfix; |
| 636 |
} |
| 637 |
return str.str(); |
| 638 |
} |
| 639 |
|
| 640 |
std::string Command::getObjectCmd( |
| 641 |
const App::DocumentObject* obj, |
| 642 |
const char* prefix, |
| 643 |
const char* postfix, |
| 644 |
bool gui |
| 645 |
) |
| 646 |
{ |
| 647 |
if (!obj || !obj->isAttachedToDocument()) { |
| 648 |
return {"None"}; |
| 649 |
} |
| 650 |
return getObjectCmd(obj->getNameInDocument(), obj->getDocument(), prefix, postfix, gui); |
| 651 |
} |
| 652 |
|
| 653 |
void Command::setAppModuleName(const char* s) |
| 654 |
{ |
| 655 |
this->sAppModule = StringCache::New(s); |
| 656 |
} |
| 657 |
|
| 658 |
void Command::setGroupName(const char* s) |
| 659 |
{ |
| 660 |
this->sGroup = StringCache::New(s); |
| 661 |
} |
| 662 |
|
| 663 |
QString Command::translatedGroupName() const |
| 664 |
{ |
| 665 |
QString text = qApp->translate(className(), getGroupName()); |
| 666 |
if (text == QString::fromLatin1(getGroupName())) { |
| 667 |
text = qApp->translate("CommandGroup", getGroupName()); |
| 668 |
} |
| 669 |
return text; |
| 670 |
} |
| 671 |
|
| 672 |
//-------------------------------------------------------------------------- |
| 673 |
// UNDO REDO transaction handling |
| 674 |
//-------------------------------------------------------------------------- |
| 675 |
/** Open a new Undo transaction on the active document |
| 676 |
* This method opens a new UNDO transaction on the active document. This transaction |
| 677 |
* will later appear in the UNDO REDO dialog with the name of the command. If the user |
| 678 |
* recall the transaction everything changed on the document between OpenCommand() and |
| 679 |
* CommitCommand will be undone (or redone). You can use an alternative name for the |
| 680 |
* operation default is the Command name. |
| 681 |
* @see CommitCommand(),AbortCommand() |
| 682 |
*/ |
| 683 |
void Command::openCommand(const char* sCmdName) |
| 684 |
{ |
| 685 |
if (!sCmdName) { |
| 686 |
sCmdName = "Command"; |
| 687 |
} |
| 688 |
App::GetApplication().setActiveTransaction(sCmdName); |
| 689 |
} |
| 690 |
|
| 691 |
void Command::commitCommand() |
| 692 |
{ |
| 693 |
App::GetApplication().closeActiveTransaction(); |
| 694 |
} |
| 695 |
|
| 696 |
void Command::abortCommand() |
| 697 |
{ |
| 698 |
App::GetApplication().closeActiveTransaction(true); |
| 699 |
} |
| 700 |
|
| 701 |
bool Command::hasPendingCommand() |
| 702 |
{ |
| 703 |
return !!App::GetApplication().getActiveTransaction(); |
| 704 |
} |
| 705 |
|
| 706 |
bool Command::_blockCmd = false; |
| 707 |
|
| 708 |
void Command::blockCommand(bool block) |
| 709 |
{ |
| 710 |
Command::_blockCmd = block; |
| 711 |
} |
| 712 |
|
| 713 |
/// Run a App level Action |
| 714 |
void Command::_doCommand(const char* file, int line, DoCmd_Type eType, const char* sCmd, ...) |
| 715 |
{ |
| 716 |
va_list ap; |
| 717 |
va_start(ap, sCmd); |
| 718 |
QString s; |
| 719 |
const QString cmd = s.vasprintf(sCmd, ap); |
| 720 |
va_end(ap); |
| 721 |
|
| 722 |
// 'vsprintf' expects a utf-8 string for '%s' |
| 723 |
QByteArray format = cmd.toUtf8(); |
| 724 |
|
| 725 |
#ifdef FC_LOGUSERACTION |
| 726 |
Base::Console().log("CmdC: %s\n", format.constData()); |
| 727 |
#endif |
| 728 |
|
| 729 |
_runCommand(file, line, eType, format.constData()); |
| 730 |
} |
| 731 |
|
| 732 |
void Command::printPyCaller() |
| 733 |
{ |
| 734 |
if (!FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 735 |
return; |
| 736 |
} |
| 737 |
PyFrameObject* frame = PyEval_GetFrame(); |
| 738 |
if (!frame) { |
| 739 |
return; |
| 740 |
} |
| 741 |
int line = PyFrame_GetLineNumber(frame); |
| 742 |
#if PY_VERSION_HEX < 0x030b0000 |
| 743 |
const char* file = PyUnicode_AsUTF8(frame->f_code->co_filename); |
| 744 |
printCaller(file ? file : "<no file>", line); |
| 745 |
#else |
| 746 |
PyCodeObject* code = PyFrame_GetCode(frame); |
| 747 |
const char* file = PyUnicode_AsUTF8(code->co_filename); |
| 748 |
printCaller(file ? file : "<no file>", line); |
| 749 |
Py_DECREF(code); |
| 750 |
#endif |
| 751 |
} |
| 752 |
|
| 753 |
void Command::printCaller(const char* file, int line) |
| 754 |
{ |
| 755 |
if (!FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 756 |
return; |
| 757 |
} |
| 758 |
std::ostringstream str; |
| 759 |
#ifdef FC_OS_WIN32 |
| 760 |
const char* _f = std::strstr(file, "\\src\\"); |
| 761 |
#else |
| 762 |
const char* _f = std::strstr(file, "/src/"); |
| 763 |
#endif |
| 764 |
str << "## " << (_f ? _f + 5 : file) << '(' << line << ')'; |
| 765 |
Gui::Application::Instance->macroManager()->addLine(MacroManager::Cmt, str.str().c_str()); |
| 766 |
} |
| 767 |
|
| 768 |
/// Run a App level Action |
| 769 |
void Command::_runCommand(const char* file, int line, DoCmd_Type eType, const char* sCmd) |
| 770 |
{ |
| 771 |
LogDisabler d1; |
| 772 |
SelectionLogDisabler d2; |
| 773 |
Base::PyGILStateLocker lock; |
| 774 |
|
| 775 |
printCaller(file, line); |
| 776 |
if (eType == Gui) { |
| 777 |
Gui::Application::Instance->macroManager()->addLine(MacroManager::Gui, sCmd); |
| 778 |
} |
| 779 |
else { |
| 780 |
Gui::Application::Instance->macroManager()->addLine(MacroManager::App, sCmd); |
| 781 |
} |
| 782 |
|
| 783 |
try { |
| 784 |
Base::Interpreter().runString(sCmd); |
| 785 |
} |
| 786 |
catch (Py::Exception&) { |
| 787 |
Base::PyException::throwException(); |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
/// Run a App level Action |
| 792 |
void Command::_runCommand(const char* file, int line, DoCmd_Type eType, const QByteArray& sCmd) |
| 793 |
{ |
| 794 |
_runCommand(file, line, eType, sCmd.constData()); |
| 795 |
} |
| 796 |
|
| 797 |
void Command::addModule(DoCmd_Type eType, const char* sModuleName) |
| 798 |
{ |
| 799 |
if (alreadyLoadedModule.find(sModuleName) == alreadyLoadedModule.end()) { |
| 800 |
LogDisabler d1; |
| 801 |
SelectionLogDisabler d2; |
| 802 |
std::string sCmd("import "); |
| 803 |
sCmd += sModuleName; |
| 804 |
if (eType == Gui) { |
| 805 |
Gui::Application::Instance->macroManager()->addLine(MacroManager::Gui, sCmd.c_str()); |
| 806 |
} |
| 807 |
else { |
| 808 |
Gui::Application::Instance->macroManager()->addLine(MacroManager::App, sCmd.c_str()); |
| 809 |
} |
| 810 |
Base::Interpreter().runString(sCmd.c_str()); |
| 811 |
alreadyLoadedModule.insert(sModuleName); |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
std::string Command::_assureWorkbench(const char* file, int line, const char* sName) |
| 816 |
{ |
| 817 |
// check if the WB is already open? |
| 818 |
std::string actName = WorkbenchManager::instance()->active()->name(); |
| 819 |
// if yes, do nothing |
| 820 |
if (actName == sName) { |
| 821 |
return actName; |
| 822 |
} |
| 823 |
|
| 824 |
// else - switch to new WB |
| 825 |
_doCommand(file, line, Gui, "Gui.activateWorkbench('%s')", sName); |
| 826 |
|
| 827 |
return actName; |
| 828 |
} |
| 829 |
|
| 830 |
void Command::_copyVisual(const char* file, int line, const char* to, const char* attr, const char* from) |
| 831 |
{ |
| 832 |
_copyVisual(file, line, to, attr, from, attr); |
| 833 |
} |
| 834 |
|
| 835 |
void Command::_copyVisual( |
| 836 |
const char* file, |
| 837 |
int line, |
| 838 |
const char* to, |
| 839 |
const char* attr_to, |
| 840 |
const char* from, |
| 841 |
const char* attr_from |
| 842 |
) |
| 843 |
{ |
| 844 |
auto doc = App::GetApplication().getActiveDocument(); |
| 845 |
if (!doc) { |
| 846 |
return; |
| 847 |
} |
| 848 |
return _copyVisual(file, line, doc->getObject(to), attr_to, doc->getObject(from), attr_from); |
| 849 |
} |
| 850 |
|
| 851 |
void Command::_copyVisual( |
| 852 |
const char* file, |
| 853 |
int line, |
| 854 |
const App::DocumentObject* to, |
| 855 |
const char* attr_to, |
| 856 |
const App::DocumentObject* from, |
| 857 |
const char* attr_from |
| 858 |
) |
| 859 |
{ |
| 860 |
if (!from || !from->isAttachedToDocument() || !to || !to->isAttachedToDocument()) { |
| 861 |
return; |
| 862 |
} |
| 863 |
static std::map<std::string, std::string> attrMap = { |
| 864 |
{"ShapeAppearance", "ShapeMaterial"}, |
| 865 |
{"Transparency", "Transparency"}, |
| 866 |
}; |
| 867 |
auto it = attrMap.find(attr_to); |
| 868 |
auto objCmd = getObjectCmd(to); |
| 869 |
if (it != attrMap.end()) { |
| 870 |
auto obj = from; |
| 871 |
for (int depth = 0;; ++depth) { |
| 872 |
auto vp = dynamic_cast<Gui::ViewProviderLink*>( |
| 873 |
Gui::Application::Instance->getViewProvider(obj) |
| 874 |
); |
| 875 |
if (vp && vp->OverrideMaterial.getValue()) { |
| 876 |
_doCommand( |
| 877 |
file, |
| 878 |
line, |
| 879 |
Gui, |
| 880 |
"%s.ViewObject.%s=%s.ViewObject.%s", |
| 881 |
objCmd.c_str(), |
| 882 |
attr_to, |
| 883 |
getObjectCmd(obj).c_str(), |
| 884 |
it->second.c_str() |
| 885 |
); |
| 886 |
return; |
| 887 |
} |
| 888 |
auto linked = obj->getLinkedObject(false, nullptr, false, depth); |
| 889 |
if (!linked || linked == obj) { |
| 890 |
break; |
| 891 |
} |
| 892 |
obj = linked; |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
try { |
| 897 |
_doCommand( |
| 898 |
file, |
| 899 |
line, |
| 900 |
Gui, |
| 901 |
"%s.ViewObject.%s=getattr(%s.getLinkedObject(True).ViewObject,'%s',%s.ViewObject.%s)", |
| 902 |
objCmd.c_str(), |
| 903 |
attr_to, |
| 904 |
getObjectCmd(from).c_str(), |
| 905 |
attr_from, |
| 906 |
objCmd.c_str(), |
| 907 |
attr_to |
| 908 |
); |
| 909 |
} |
| 910 |
catch (Base::Exception& /*e*/) { |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
void Command::_copyVisual( |
| 915 |
const char* file, |
| 916 |
int line, |
| 917 |
const App::DocumentObject* to, |
| 918 |
const char* attr, |
| 919 |
const App::DocumentObject* from |
| 920 |
) |
| 921 |
{ |
| 922 |
_copyVisual(file, line, to, attr, from, attr); |
| 923 |
} |
| 924 |
|
| 925 |
std::string Command::getPythonTuple(const std::string& name, const std::vector<std::string>& subnames) |
| 926 |
{ |
| 927 |
std::stringstream str; |
| 928 |
auto last = --subnames.end(); |
| 929 |
str << "(App.ActiveDocument." << name << ",["; |
| 930 |
for (auto it = subnames.cbegin(); it != subnames.cend(); ++it) { |
| 931 |
str << "\"" << *it << "\""; |
| 932 |
if (it != last) { |
| 933 |
str << ","; |
| 934 |
} |
| 935 |
} |
| 936 |
str << "])"; |
| 937 |
return str.str(); |
| 938 |
} |
| 939 |
|
| 940 |
const std::string Command::strToPython(const char* Str) |
| 941 |
{ |
| 942 |
return Base::InterpreterSingleton::strToPython(Str); |
| 943 |
} |
| 944 |
|
| 945 |
/// Updates the (active) document (propagate changes) |
| 946 |
void Command::updateActive() |
| 947 |
{ |
| 948 |
WaitCursor wc; |
| 949 |
doCommand(App, "App.ActiveDocument.recompute()"); |
| 950 |
} |
| 951 |
|
| 952 |
bool Command::isActiveObjectValid() |
| 953 |
{ |
| 954 |
Gui::Document* active = Gui::Application::Instance->activeDocument(); |
| 955 |
assert(active); |
| 956 |
App::Document* document = active->getDocument(); |
| 957 |
App::DocumentObject* object = document->getActiveObject(); |
| 958 |
assert(object); |
| 959 |
return object->isValid(); |
| 960 |
} |
| 961 |
|
| 962 |
//-------------------------------------------------------------------------- |
| 963 |
// Online help handling |
| 964 |
//-------------------------------------------------------------------------- |
| 965 |
|
| 966 |
/// returns the begin of a online help page |
| 967 |
const char* Command::beginCmdHelp() |
| 968 |
{ |
| 969 |
return "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n" |
| 970 |
"<html>\n" |
| 971 |
"<head>\n" |
| 972 |
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\n" |
| 973 |
"<title>FreeCAD Main Index</title>\n" |
| 974 |
"</head>\n" |
| 975 |
"<body bgcolor=\"#ffffff\">\n\n"; |
| 976 |
} |
| 977 |
|
| 978 |
/// returns the end of a online help page |
| 979 |
const char* Command::endCmdHelp() |
| 980 |
{ |
| 981 |
return "</body></html>\n\n"; |
| 982 |
} |
| 983 |
|
| 984 |
void Command::applyCommandData(const char* context, Action* action) |
| 985 |
{ |
| 986 |
action->setText(QCoreApplication::translate(context, getMenuText())); |
| 987 |
action->setToolTip(QCoreApplication::translate(context, getToolTipText())); |
| 988 |
action->setWhatsThis(QCoreApplication::translate(context, getWhatsThis())); |
| 989 |
if (sStatusTip) { |
| 990 |
action->setStatusTip(QCoreApplication::translate(context, getStatusTip())); |
| 991 |
} |
| 992 |
else { |
| 993 |
action->setStatusTip(QCoreApplication::translate(context, getToolTipText())); |
| 994 |
} |
| 995 |
|
| 996 |
// Default to QAction::NoRole instead of QAction::TextHeuristicRole to stop collisions with |
| 997 |
// e.g. "Preferences" and "Copy" |
| 998 |
if (action->action()->menuRole() == QAction::TextHeuristicRole) { |
| 999 |
action->setMenuRole(QAction::NoRole); |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
|
| 1004 |
const char* Command::keySequenceToAccel(int sk) const |
| 1005 |
{ |
| 1006 |
/* Local class to ensure free()'ing the strings allocated below */ |
| 1007 |
using StringMap = std::map<int, std::string>; |
| 1008 |
static StringMap strings; |
| 1009 |
auto i = strings.find(sk); |
| 1010 |
|
| 1011 |
if (i != strings.end()) { |
| 1012 |
return i->second.c_str(); |
| 1013 |
} |
| 1014 |
|
| 1015 |
// In case FreeCAD is loaded without GUI (issue 16407) |
| 1016 |
if (!QApplication::instance()) { |
| 1017 |
return ""; |
| 1018 |
} |
| 1019 |
|
| 1020 |
auto type = static_cast<QKeySequence::StandardKey>(sk); |
| 1021 |
QKeySequence ks(type); |
| 1022 |
QString qs = ks.toString(); |
| 1023 |
QByteArray data = qs.toLatin1(); |
| 1024 |
|
| 1025 |
return (strings[sk] = static_cast<const char*>(data)).c_str(); |
| 1026 |
} |
| 1027 |
|
| 1028 |
void Command::printConflictingAccelerators() const |
| 1029 |
{ |
| 1030 |
auto cmd = Application::Instance->commandManager().checkAcceleratorForConflicts(sAccel, this); |
| 1031 |
if (cmd) { |
| 1032 |
Base::Console().warning( |
| 1033 |
"Accelerator conflict between %s (%s) and %s (%s)\n", |
| 1034 |
sName, |
| 1035 |
sAccel, |
| 1036 |
cmd->sName, |
| 1037 |
cmd->sAccel |
| 1038 |
); |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
Action* Command::createAction() |
| 1043 |
{ |
| 1044 |
Action* pcAction; |
| 1045 |
pcAction = new Action(this, getMainWindow()); |
| 1046 |
applyCommandData(this->className(), pcAction); |
| 1047 |
if (sPixmap) { |
| 1048 |
pcAction->setIcon(Gui::BitmapFactory().iconFromTheme(sPixmap)); |
| 1049 |
} |
| 1050 |
return pcAction; |
| 1051 |
} |
| 1052 |
|
| 1053 |
void Command::languageChange() |
| 1054 |
{ |
| 1055 |
if (_pcAction) { |
| 1056 |
applyCommandData(this->className(), _pcAction); |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
void Command::updateAction(int) |
| 1061 |
{} |
| 1062 |
|
| 1063 |
//=========================================================================== |
| 1064 |
// GroupCommand |
| 1065 |
//=========================================================================== |
| 1066 |
|
| 1067 |
GroupCommand::GroupCommand(const char* name) |
| 1068 |
: Command(name) |
| 1069 |
{} |
| 1070 |
|
| 1071 |
bool GroupCommand::isCheckable() const |
| 1072 |
{ |
| 1073 |
return checkable; |
| 1074 |
} |
| 1075 |
|
| 1076 |
void GroupCommand::setCheckable(bool on) |
| 1077 |
{ |
| 1078 |
checkable = on; |
| 1079 |
} |
| 1080 |
|
| 1081 |
bool GroupCommand::isExclusive() const |
| 1082 |
{ |
| 1083 |
return exclusive; |
| 1084 |
} |
| 1085 |
|
| 1086 |
void GroupCommand::setExclusive(bool on) |
| 1087 |
{ |
| 1088 |
exclusive = on; |
| 1089 |
} |
| 1090 |
|
| 1091 |
bool GroupCommand::doesRememberLast() const |
| 1092 |
{ |
| 1093 |
return rememberLast; |
| 1094 |
} |
| 1095 |
|
| 1096 |
void GroupCommand::setRememberLast(bool on) |
| 1097 |
{ |
| 1098 |
rememberLast = on; |
| 1099 |
} |
| 1100 |
|
| 1101 |
bool GroupCommand::hasDropDownMenu() const |
| 1102 |
{ |
| 1103 |
return dropDownMenu; |
| 1104 |
} |
| 1105 |
|
| 1106 |
void GroupCommand::setDropDownMenu(bool on) |
| 1107 |
{ |
| 1108 |
dropDownMenu = on; |
| 1109 |
} |
| 1110 |
|
| 1111 |
int GroupCommand::addCommand(Command* cmd, bool reg) |
| 1112 |
{ |
| 1113 |
cmds.emplace_back(cmd, cmds.size()); |
| 1114 |
if (cmd && reg) { |
| 1115 |
Application::Instance->commandManager().addCommand(cmd); |
| 1116 |
} |
| 1117 |
return (int)cmds.size() - 1; |
| 1118 |
} |
| 1119 |
|
| 1120 |
Command* GroupCommand::addCommand(const char* name) |
| 1121 |
{ |
| 1122 |
auto cmd = Application::Instance->commandManager().getCommandByName(name); |
| 1123 |
if (cmd) { |
| 1124 |
addCommand(cmd, false); |
| 1125 |
} |
| 1126 |
return cmd; |
| 1127 |
} |
| 1128 |
|
| 1129 |
Command* GroupCommand::getCommand(int idx) const |
| 1130 |
{ |
| 1131 |
if (idx >= 0 && idx < (int)cmds.size()) { |
| 1132 |
return cmds[idx].first; |
| 1133 |
} |
| 1134 |
return nullptr; |
| 1135 |
} |
| 1136 |
|
| 1137 |
Action* GroupCommand::createAction() |
| 1138 |
{ |
| 1139 |
auto* pcAction = new ActionGroup(this, getMainWindow()); |
| 1140 |
pcAction->setMenuRole(QAction::NoRole); |
| 1141 |
pcAction->setDropDownMenu(hasDropDownMenu()); |
| 1142 |
pcAction->setExclusive(isExclusive()); |
| 1143 |
pcAction->setCheckable(isCheckable()); |
| 1144 |
pcAction->setRememberLast(doesRememberLast()); |
| 1145 |
pcAction->setWhatsThis(QString::fromLatin1(sWhatsThis)); |
| 1146 |
|
| 1147 |
for (auto& v : cmds) { |
| 1148 |
if (!v.first) { |
| 1149 |
pcAction->addAction(QStringLiteral(""))->setSeparator(true); |
| 1150 |
} |
| 1151 |
else { |
| 1152 |
v.first->addToGroup(pcAction); |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
pcAction->setProperty("defaultAction", QVariant(0)); |
| 1157 |
setup(pcAction); |
| 1158 |
return pcAction; |
| 1159 |
} |
| 1160 |
|
| 1161 |
void GroupCommand::activated(int iMsg) |
| 1162 |
{ |
| 1163 |
if (iMsg < 0 || iMsg >= (int)cmds.size()) { |
| 1164 |
return; |
| 1165 |
} |
| 1166 |
|
| 1167 |
auto& v = cmds[iMsg]; |
| 1168 |
if (!v.first) { |
| 1169 |
return; |
| 1170 |
} |
| 1171 |
|
| 1172 |
if (triggerSource() != TriggerChildAction) { |
| 1173 |
v.first->invoke(0); |
| 1174 |
} |
| 1175 |
|
| 1176 |
Action* cmdAction = v.first->getAction(); |
| 1177 |
if (_pcAction && cmdAction) { |
| 1178 |
_pcAction->setProperty("defaultAction", QVariant((int)v.second)); |
| 1179 |
setup(_pcAction); |
| 1180 |
} |
| 1181 |
} |
| 1182 |
|
| 1183 |
void GroupCommand::languageChange() |
| 1184 |
{ |
| 1185 |
Command::languageChange(); |
| 1186 |
if (_pcAction) { |
| 1187 |
setup(_pcAction); |
| 1188 |
} |
| 1189 |
} |
| 1190 |
|
| 1191 |
void GroupCommand::setup(Action* pcAction) |
| 1192 |
{ |
| 1193 |
|
| 1194 |
// The tooltip for the group is the tooltip of the active tool (that is, the tool that will |
| 1195 |
// be activated when the main portion of the button is clicked). |
| 1196 |
int idx = pcAction->property("defaultAction").toInt(); |
| 1197 |
if (idx >= 0 && idx < (int)cmds.size() && cmds[idx].first) { |
| 1198 |
auto cmd = cmds[idx].first; |
| 1199 |
QString shortcut = cmd->getShortcut(); |
| 1200 |
pcAction->setShortcut(shortcut); |
| 1201 |
pcAction->setText(QCoreApplication::translate(className(), getMenuText())); |
| 1202 |
QIcon icon; |
| 1203 |
if (auto childAction = cmd->getAction()) { |
| 1204 |
icon = childAction->icon(); |
| 1205 |
} |
| 1206 |
if (icon.isNull()) { |
| 1207 |
icon = BitmapFactory().iconFromTheme(cmd->getPixmap()); |
| 1208 |
} |
| 1209 |
pcAction->setIcon(icon); |
| 1210 |
const char* context = dynamic_cast<PythonCommand*>(cmd) ? cmd->getName() : cmd->className(); |
| 1211 |
const char* tooltip = cmd->getToolTipText(); |
| 1212 |
const char* statustip = cmd->getStatusTip(); |
| 1213 |
if (!statustip || '\0' == *statustip) { |
| 1214 |
statustip = tooltip; |
| 1215 |
} |
| 1216 |
pcAction->setToolTip( |
| 1217 |
QCoreApplication::translate(context, tooltip), |
| 1218 |
QCoreApplication::translate(cmd->className(), cmd->getMenuText()) |
| 1219 |
); |
| 1220 |
pcAction->setStatusTip(QCoreApplication::translate(context, statustip)); |
| 1221 |
} |
| 1222 |
else { |
| 1223 |
applyCommandData(this->className(), pcAction); |
| 1224 |
if (sPixmap) { |
| 1225 |
pcAction->setIcon(Gui::BitmapFactory().iconFromTheme(sPixmap)); |
| 1226 |
} |
| 1227 |
} |
| 1228 |
} |
| 1229 |
|
| 1230 |
//=========================================================================== |
| 1231 |
// MacroCommand |
| 1232 |
//=========================================================================== |
| 1233 |
|
| 1234 |
/* TRANSLATOR Gui::MacroCommand */ |
| 1235 |
|
| 1236 |
MacroCommand::MacroCommand(const char* name, bool system) |
| 1237 |
: Command(StringCache::New(name)) |
| 1238 |
, systemMacro(system) |
| 1239 |
{ |
| 1240 |
sGroup = "Macros"; |
| 1241 |
eType = 0; |
| 1242 |
sScriptName = nullptr; |
| 1243 |
} |
| 1244 |
|
| 1245 |
MacroCommand::~MacroCommand() = default; |
| 1246 |
|
| 1247 |
void MacroCommand::activated(int iMsg) |
| 1248 |
{ |
| 1249 |
Q_UNUSED(iMsg); |
| 1250 |
|
| 1251 |
QDir d; |
| 1252 |
if (!systemMacro) { |
| 1253 |
std::string cMacroPath; |
| 1254 |
|
| 1255 |
cMacroPath = App::GetApplication() |
| 1256 |
.GetParameterGroupByPath("User parameter:BaseApp/Preferences/Macro") |
| 1257 |
->GetASCII("MacroPath", App::Application::getUserMacroDir().c_str()); |
| 1258 |
|
| 1259 |
d = QDir(QString::fromUtf8(cMacroPath.c_str())); |
| 1260 |
} |
| 1261 |
else { |
| 1262 |
QString dirstr = QString::fromStdString(App::Application::getHomePath()) |
| 1263 |
+ QStringLiteral("Macro"); |
| 1264 |
d = QDir(dirstr); |
| 1265 |
} |
| 1266 |
|
| 1267 |
QFileInfo fi(d, QString::fromUtf8(sScriptName)); |
| 1268 |
if (!fi.exists()) { |
| 1269 |
QMessageBox::critical( |
| 1270 |
Gui::getMainWindow(), |
| 1271 |
qApp->translate("Gui::MacroCommand", "Macro file doesn't exist"), |
| 1272 |
qApp->translate("Gui::MacroCommand", "No such macro file: '%1'").arg(fi.absoluteFilePath()) |
| 1273 |
); |
| 1274 |
} |
| 1275 |
else { |
| 1276 |
Application::Instance->macroManager()->run(MacroManager::File, fi.filePath().toUtf8()); |
| 1277 |
// after macro run recalculate the document |
| 1278 |
if (Application::Instance->activeDocument()) { |
| 1279 |
Application::Instance->activeDocument()->getDocument()->recompute(); |
| 1280 |
} |
| 1281 |
} |
| 1282 |
} |
| 1283 |
|
| 1284 |
Action* MacroCommand::createAction() |
| 1285 |
{ |
| 1286 |
Action* pcAction; |
| 1287 |
pcAction = new Action(this, getMainWindow()); |
| 1288 |
pcAction->setText(QString::fromUtf8(sMenuText)); |
| 1289 |
pcAction->setToolTip(QString::fromUtf8(sToolTipText)); |
| 1290 |
pcAction->setStatusTip(QString::fromUtf8(sStatusTip)); |
| 1291 |
if (pcAction->statusTip().isEmpty()) { |
| 1292 |
pcAction->setStatusTip(pcAction->toolTip()); |
| 1293 |
} |
| 1294 |
pcAction->setWhatsThis(QString::fromUtf8(sWhatsThis)); |
| 1295 |
if (sPixmap) { |
| 1296 |
pcAction->setIcon(Gui::BitmapFactory().pixmap(sPixmap)); |
| 1297 |
} |
| 1298 |
|
| 1299 |
return pcAction; |
| 1300 |
} |
| 1301 |
|
| 1302 |
void MacroCommand::setScriptName(const char* s) |
| 1303 |
{ |
| 1304 |
this->sScriptName = StringCache::New(s); |
| 1305 |
} |
| 1306 |
|
| 1307 |
void MacroCommand::load() |
| 1308 |
{ |
| 1309 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 1310 |
"User parameter:BaseApp/Macro" |
| 1311 |
); |
| 1312 |
|
| 1313 |
if (hGrp->HasGroup("Macros")) { |
| 1314 |
hGrp = hGrp->GetGroup("Macros"); |
| 1315 |
std::vector<Base::Reference<ParameterGrp>> macros = hGrp->GetGroups(); |
| 1316 |
for (const auto& it : macros) { |
| 1317 |
auto macro = new MacroCommand(it->GetGroupName()); |
| 1318 |
macro->setScriptName(it->GetASCII("Script").c_str()); |
| 1319 |
macro->setMenuText(it->GetASCII("Menu").c_str()); |
| 1320 |
macro->setToolTipText(it->GetASCII("Tooltip").c_str()); |
| 1321 |
macro->setWhatsThis(it->GetASCII("WhatsThis").c_str()); |
| 1322 |
macro->setStatusTip(it->GetASCII("Statustip").c_str()); |
| 1323 |
if (it->GetASCII("Pixmap", "nix") != "nix") { |
| 1324 |
macro->setPixmap(it->GetASCII("Pixmap").c_str()); |
| 1325 |
} |
| 1326 |
macro->setAccel(it->GetASCII("Accel", nullptr).c_str()); |
| 1327 |
macro->systemMacro = it->GetBool("System", false); |
| 1328 |
Application::Instance->commandManager().addCommand(macro); |
| 1329 |
} |
| 1330 |
} |
| 1331 |
} |
| 1332 |
|
| 1333 |
void MacroCommand::save() |
| 1334 |
{ |
| 1335 |
ParameterGrp::handle hGrp = App::GetApplication() |
| 1336 |
.GetParameterGroupByPath("User parameter:BaseApp/Macro") |
| 1337 |
->GetGroup("Macros"); |
| 1338 |
hGrp->Clear(); |
| 1339 |
|
| 1340 |
std::vector<Command*> macros = Application::Instance->commandManager().getGroupCommands("Macros"); |
| 1341 |
if (!macros.empty()) { |
| 1342 |
for (const auto& it : macros) { |
| 1343 |
auto macro = (MacroCommand*)it; |
| 1344 |
ParameterGrp::handle hMacro = hGrp->GetGroup(macro->getName()); |
| 1345 |
hMacro->SetASCII("Script", macro->getScriptName()); |
| 1346 |
hMacro->SetASCII("Menu", macro->getMenuText()); |
| 1347 |
hMacro->SetASCII("Tooltip", macro->getToolTipText()); |
| 1348 |
hMacro->SetASCII("WhatsThis", macro->getWhatsThis()); |
| 1349 |
hMacro->SetASCII("Statustip", macro->getStatusTip()); |
| 1350 |
hMacro->SetASCII("Pixmap", macro->getPixmap()); |
| 1351 |
hMacro->SetASCII("Accel", macro->getAccel()); |
| 1352 |
hMacro->SetBool("System", macro->systemMacro); |
| 1353 |
} |
| 1354 |
} |
| 1355 |
} |
| 1356 |
|
| 1357 |
//=========================================================================== |
| 1358 |
// PythonCommand |
| 1359 |
//=========================================================================== |
| 1360 |
|
| 1361 |
PythonCommand::PythonCommand(const char* name, PyObject* pcPyCommand, const char* pActivationString) |
| 1362 |
: Command(StringCache::New(name)) |
| 1363 |
, _pcPyCommand(pcPyCommand) |
| 1364 |
{ |
| 1365 |
if (pActivationString) { |
| 1366 |
Activation = pActivationString; |
| 1367 |
} |
| 1368 |
|
| 1369 |
sGroup = "Python"; |
| 1370 |
|
| 1371 |
Py_INCREF(_pcPyCommand); |
| 1372 |
|
| 1373 |
// call the method "GetResources()" of the command object |
| 1374 |
_pcPyResourceDict = Interpreter().runMethodObject(_pcPyCommand, "GetResources"); |
| 1375 |
// check if the "GetResources()" method returns a Dict object |
| 1376 |
if (!PyDict_Check(_pcPyResourceDict)) { |
| 1377 |
throw Base::TypeError( |
| 1378 |
"PythonCommand::PythonCommand(): Method GetResources() of the Python " |
| 1379 |
"command object returns the wrong type (has to be dict)" |
| 1380 |
); |
| 1381 |
} |
| 1382 |
|
| 1383 |
// check for command type |
| 1384 |
std::string cmdType = getResource("CmdType"); |
| 1385 |
if (!cmdType.empty()) { |
| 1386 |
int type = 0; |
| 1387 |
if (cmdType.find("AlterDoc") != std::string::npos) { |
| 1388 |
type += int(AlterDoc); |
| 1389 |
} |
| 1390 |
if (cmdType.find("Alter3DView") != std::string::npos) { |
| 1391 |
type += int(Alter3DView); |
| 1392 |
} |
| 1393 |
if (cmdType.find("AlterSelection") != std::string::npos) { |
| 1394 |
type += int(AlterSelection); |
| 1395 |
} |
| 1396 |
if (cmdType.find("ForEdit") != std::string::npos) { |
| 1397 |
type += int(ForEdit); |
| 1398 |
} |
| 1399 |
if (cmdType.find("NoTransaction") != std::string::npos) { |
| 1400 |
type += int(NoTransaction); |
| 1401 |
} |
| 1402 |
eType = type; |
| 1403 |
} |
| 1404 |
|
| 1405 |
auto& rcCmdMgr = Gui::Application::Instance->commandManager(); |
| 1406 |
|
| 1407 |
connPyCmdInitialized = rcCmdMgr.signalPyCmdInitialized.connect([this]() { this->onActionInit(); }); |
| 1408 |
} |
| 1409 |
|
| 1410 |
PythonCommand::~PythonCommand() |
| 1411 |
{ |
| 1412 |
Base::PyGILStateLocker lock; |
| 1413 |
Py_DECREF(_pcPyCommand); |
| 1414 |
} |
| 1415 |
|
| 1416 |
const char* PythonCommand::getResource(const char* sName) const |
| 1417 |
{ |
| 1418 |
Base::PyGILStateLocker lock; |
| 1419 |
PyObject* pcTemp; |
| 1420 |
|
| 1421 |
// get the "MenuText" resource string |
| 1422 |
pcTemp = PyDict_GetItemString(_pcPyResourceDict, sName); |
| 1423 |
if (!pcTemp) { |
| 1424 |
return ""; |
| 1425 |
} |
| 1426 |
if (!PyUnicode_Check(pcTemp)) { |
| 1427 |
throw Base::TypeError( |
| 1428 |
"PythonCommand::getResource(): Method GetResources() of the Python " |
| 1429 |
"command object returns a dictionary which holds not only strings" |
| 1430 |
); |
| 1431 |
} |
| 1432 |
return PyUnicode_AsUTF8(pcTemp); |
| 1433 |
} |
| 1434 |
|
| 1435 |
void PythonCommand::activated(int iMsg) |
| 1436 |
{ |
| 1437 |
if (Activation.empty()) { |
| 1438 |
try { |
| 1439 |
if (isCheckable()) { |
| 1440 |
Interpreter().runMethod(_pcPyCommand, "Activated", "", nullptr, "(i)", iMsg); |
| 1441 |
} |
| 1442 |
else { |
| 1443 |
Interpreter().runMethodVoid(_pcPyCommand, "Activated"); |
| 1444 |
} |
| 1445 |
} |
| 1446 |
catch (const Base::PyException& e) { |
| 1447 |
Base::Console().error( |
| 1448 |
"Running the Python command '%s' failed:\n%s\n%s", |
| 1449 |
sName, |
| 1450 |
e.getStackTrace().c_str(), |
| 1451 |
e.what() |
| 1452 |
); |
| 1453 |
} |
| 1454 |
catch (const Base::Exception&) { |
| 1455 |
Base::Console().error("Running the Python command '%s' failed, try to resume", sName); |
| 1456 |
} |
| 1457 |
} |
| 1458 |
else { |
| 1459 |
runCommand(Doc, Activation.c_str()); |
| 1460 |
} |
| 1461 |
} |
| 1462 |
|
| 1463 |
bool PythonCommand::isActive() |
| 1464 |
{ |
| 1465 |
try { |
| 1466 |
Base::PyGILStateLocker lock; |
| 1467 |
Py::Object cmd(_pcPyCommand); |
| 1468 |
if (cmd.hasAttr("IsActive")) { |
| 1469 |
Py::Callable call(cmd.getAttr("IsActive")); |
| 1470 |
Py::Tuple args; |
| 1471 |
Py::Object ret = call.apply(args); |
| 1472 |
// if return type is not boolean or not true |
| 1473 |
if (!PyBool_Check(ret.ptr()) || ret.ptr() != Py_True) { |
| 1474 |
return false; |
| 1475 |
} |
| 1476 |
} |
| 1477 |
} |
| 1478 |
catch (Py::Exception& e) { |
| 1479 |
Base::PyGILStateLocker lock; |
| 1480 |
e.clear(); |
| 1481 |
return false; |
| 1482 |
} |
| 1483 |
|
| 1484 |
return true; |
| 1485 |
} |
| 1486 |
|
| 1487 |
void PythonCommand::languageChange() |
| 1488 |
{ |
| 1489 |
if (_pcAction) { |
| 1490 |
applyCommandData(getName(), _pcAction); |
| 1491 |
} |
| 1492 |
} |
| 1493 |
|
| 1494 |
const char* PythonCommand::getHelpUrl() const |
| 1495 |
{ |
| 1496 |
PyObject* pcTemp; |
| 1497 |
pcTemp = Interpreter().runMethodObject(_pcPyCommand, "CmdHelpURL"); |
| 1498 |
if (!pcTemp) { |
| 1499 |
return ""; |
| 1500 |
} |
| 1501 |
if (!PyUnicode_Check(pcTemp)) { |
| 1502 |
throw Base::TypeError("PythonCommand::CmdHelpURL(): Method CmdHelpURL() of the Python command object returns no string"); |
| 1503 |
} |
| 1504 |
return PyUnicode_AsUTF8(pcTemp); |
| 1505 |
} |
| 1506 |
|
| 1507 |
Action* PythonCommand::createAction() |
| 1508 |
{ |
| 1509 |
auto qtAction = new QAction(nullptr); |
| 1510 |
Action* pcAction; |
| 1511 |
|
| 1512 |
pcAction = new Action(this, qtAction, getMainWindow()); |
| 1513 |
applyCommandData(this->getName(), pcAction); |
| 1514 |
if (strcmp(getResource("Pixmap"), "") != 0) { |
| 1515 |
pcAction->setIcon(Gui::BitmapFactory().iconFromTheme(getResource("Pixmap"))); |
| 1516 |
} |
| 1517 |
|
| 1518 |
try { |
| 1519 |
if (isCheckable()) { |
| 1520 |
pcAction->setCheckable(true); |
| 1521 |
// Here the QAction must be tmp. blocked to avoid to call the 'activated' method |
| 1522 |
qtAction->blockSignals(true); |
| 1523 |
pcAction->setChecked(isChecked()); |
| 1524 |
qtAction->blockSignals(false); |
| 1525 |
} |
| 1526 |
} |
| 1527 |
catch (const Base::Exception& e) { |
| 1528 |
Base::Console().error("%s\n", e.what()); |
| 1529 |
} |
| 1530 |
|
| 1531 |
return pcAction; |
| 1532 |
} |
| 1533 |
|
| 1534 |
const char* PythonCommand::getWhatsThis() const |
| 1535 |
{ |
| 1536 |
const char* whatsthis = getResource("WhatsThis"); |
| 1537 |
if (Base::Tools::isNullOrEmpty(whatsthis)) { |
| 1538 |
whatsthis = this->getName(); |
| 1539 |
} |
| 1540 |
return whatsthis; |
| 1541 |
} |
| 1542 |
|
| 1543 |
const char* PythonCommand::getMenuText() const |
| 1544 |
{ |
| 1545 |
return getResource("MenuText"); |
| 1546 |
} |
| 1547 |
|
| 1548 |
const char* PythonCommand::getToolTipText() const |
| 1549 |
{ |
| 1550 |
return getResource("ToolTip"); |
| 1551 |
} |
| 1552 |
|
| 1553 |
const char* PythonCommand::getStatusTip() const |
| 1554 |
{ |
| 1555 |
return getResource("StatusTip"); |
| 1556 |
} |
| 1557 |
|
| 1558 |
const char* PythonCommand::getPixmap() const |
| 1559 |
{ |
| 1560 |
const char* ret = getResource("Pixmap"); |
| 1561 |
return !Base::Tools::isNullOrEmpty(ret) ? ret : nullptr; |
| 1562 |
} |
| 1563 |
|
| 1564 |
const char* PythonCommand::getAccel() const |
| 1565 |
{ |
| 1566 |
return getResource("Accel"); |
| 1567 |
} |
| 1568 |
|
| 1569 |
bool PythonCommand::isCheckable() const |
| 1570 |
{ |
| 1571 |
Base::PyGILStateLocker lock; |
| 1572 |
PyObject* item = PyDict_GetItemString(_pcPyResourceDict, "Checkable"); |
| 1573 |
return item ? true : false; |
| 1574 |
} |
| 1575 |
|
| 1576 |
bool PythonCommand::isChecked() const |
| 1577 |
{ |
| 1578 |
Base::PyGILStateLocker lock; |
| 1579 |
PyObject* item = PyDict_GetItemString(_pcPyResourceDict, "Checkable"); |
| 1580 |
if (!item) { |
| 1581 |
throw Base::ValueError( |
| 1582 |
"PythonCommand::isChecked(): Method GetResources() of the Python " |
| 1583 |
"command object doesn't contain the key 'Checkable'" |
| 1584 |
); |
| 1585 |
} |
| 1586 |
|
| 1587 |
if (PyBool_Check(item)) { |
| 1588 |
return Base::asBoolean(item); |
| 1589 |
} |
| 1590 |
else { |
| 1591 |
throw Base::ValueError( |
| 1592 |
"PythonCommand::isChecked(): Method GetResources() of the Python " |
| 1593 |
"command object contains the key 'Checkable' which is not a boolean" |
| 1594 |
); |
| 1595 |
} |
| 1596 |
} |
| 1597 |
|
| 1598 |
void PythonCommand::onActionInit() const |
| 1599 |
{ |
| 1600 |
try { |
| 1601 |
Base::PyGILStateLocker lock; |
| 1602 |
Py::Object cmd(_pcPyCommand); |
| 1603 |
if (cmd.hasAttr("OnActionInit")) { |
| 1604 |
Py::Callable call(cmd.getAttr("OnActionInit")); |
| 1605 |
Py::Tuple args; |
| 1606 |
Py::Object ret = call.apply(args); |
| 1607 |
} |
| 1608 |
} |
| 1609 |
catch (Py::Exception& e) { |
| 1610 |
Base::PyGILStateLocker lock; |
| 1611 |
e.clear(); |
| 1612 |
} |
| 1613 |
|
| 1614 |
connPyCmdInitialized.disconnect(); |
| 1615 |
} |
| 1616 |
|
| 1617 |
//=========================================================================== |
| 1618 |
// PythonGroupCommand |
| 1619 |
//=========================================================================== |
| 1620 |
|
| 1621 |
PythonGroupCommand::PythonGroupCommand(const char* name, PyObject* pcPyCommand) |
| 1622 |
: Command(StringCache::New(name)) |
| 1623 |
, _pcPyCommand(pcPyCommand) |
| 1624 |
{ |
| 1625 |
sGroup = "Python"; |
| 1626 |
|
| 1627 |
Py_INCREF(_pcPyCommand); |
| 1628 |
|
| 1629 |
// call the method "GetResources()" of the command object |
| 1630 |
_pcPyResource = Interpreter().runMethodObject(_pcPyCommand, "GetResources"); |
| 1631 |
// check if the "GetResources()" method returns a Dict object |
| 1632 |
if (!PyDict_Check(_pcPyResource)) { |
| 1633 |
throw Base::TypeError( |
| 1634 |
"PythonGroupCommand::PythonGroupCommand(): Method GetResources() of the Python " |
| 1635 |
"command object returns the wrong type (has to be dict)" |
| 1636 |
); |
| 1637 |
} |
| 1638 |
|
| 1639 |
// check for command type |
| 1640 |
std::string cmdType = getResource("CmdType"); |
| 1641 |
if (!cmdType.empty()) { |
| 1642 |
int type = 0; |
| 1643 |
if (cmdType.find("AlterDoc") != std::string::npos) { |
| 1644 |
type += int(AlterDoc); |
| 1645 |
} |
| 1646 |
if (cmdType.find("Alter3DView") != std::string::npos) { |
| 1647 |
type += int(Alter3DView); |
| 1648 |
} |
| 1649 |
if (cmdType.find("AlterSelection") != std::string::npos) { |
| 1650 |
type += int(AlterSelection); |
| 1651 |
} |
| 1652 |
if (cmdType.find("ForEdit") != std::string::npos) { |
| 1653 |
type += int(ForEdit); |
| 1654 |
} |
| 1655 |
eType = type; |
| 1656 |
} |
| 1657 |
|
| 1658 |
auto& rcCmdMgr = Gui::Application::Instance->commandManager(); |
| 1659 |
|
| 1660 |
connPyCmdInitialized = rcCmdMgr.signalPyCmdInitialized.connect([this]() { this->onActionInit(); }); |
| 1661 |
} |
| 1662 |
|
| 1663 |
PythonGroupCommand::~PythonGroupCommand() |
| 1664 |
{ |
| 1665 |
Base::PyGILStateLocker lock; |
| 1666 |
Py_DECREF(_pcPyCommand); |
| 1667 |
} |
| 1668 |
|
| 1669 |
void PythonGroupCommand::activated(int iMsg) |
| 1670 |
{ |
| 1671 |
try { |
| 1672 |
auto pcAction = qobject_cast<Gui::ActionGroup*>(_pcAction); |
| 1673 |
QList<QAction*> a = pcAction->actions(); |
| 1674 |
assert(iMsg < a.size()); |
| 1675 |
QAction* act = a[iMsg]; |
| 1676 |
|
| 1677 |
setupCheckable(iMsg); |
| 1678 |
|
| 1679 |
Base::PyGILStateLocker lock; |
| 1680 |
Py::Object cmd(_pcPyCommand); |
| 1681 |
if (cmd.hasAttr("Activated")) { |
| 1682 |
Py::Callable call(cmd.getAttr("Activated")); |
| 1683 |
Py::Tuple args(1); |
| 1684 |
args.setItem(0, Py::Long(iMsg)); |
| 1685 |
Py::Object ret = call.apply(args); |
| 1686 |
} |
| 1687 |
// If the command group doesn't implement the 'Activated' method then invoke the command directly |
| 1688 |
else { |
| 1689 |
Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager(); |
| 1690 |
auto cmd = rcCmdMgr.getCommandByName(act->property("CommandName").toByteArray()); |
| 1691 |
if (cmd) { |
| 1692 |
bool checked = act->isCheckable() && act->isChecked(); |
| 1693 |
cmd->invoke(checked ? 1 : 0, TriggerAction); |
| 1694 |
} |
| 1695 |
} |
| 1696 |
} |
| 1697 |
catch (Py::Exception&) { |
| 1698 |
Base::PyGILStateLocker lock; |
| 1699 |
Base::PyException e; |
| 1700 |
Base::Console().error( |
| 1701 |
"Running the Python command '%s' failed:\n%s\n%s", |
| 1702 |
sName, |
| 1703 |
e.getStackTrace().c_str(), |
| 1704 |
e.what() |
| 1705 |
); |
| 1706 |
} |
| 1707 |
} |
| 1708 |
|
| 1709 |
bool PythonGroupCommand::isActive() |
| 1710 |
{ |
| 1711 |
try { |
| 1712 |
Base::PyGILStateLocker lock; |
| 1713 |
Py::Object cmd(_pcPyCommand); |
| 1714 |
|
| 1715 |
if (cmd.hasAttr("IsActive")) { |
| 1716 |
Py::Callable call(cmd.getAttr("IsActive")); |
| 1717 |
Py::Tuple args; |
| 1718 |
Py::Object ret = call.apply(args); |
| 1719 |
// if return type is not boolean or not true |
| 1720 |
if (!PyBool_Check(ret.ptr()) || ret.ptr() != Py_True) { |
| 1721 |
return false; |
| 1722 |
} |
| 1723 |
} |
| 1724 |
} |
| 1725 |
catch (Py::Exception& e) { |
| 1726 |
Base::PyGILStateLocker lock; |
| 1727 |
e.clear(); |
| 1728 |
return false; |
| 1729 |
} |
| 1730 |
|
| 1731 |
return true; |
| 1732 |
} |
| 1733 |
|
| 1734 |
Action* PythonGroupCommand::createAction() |
| 1735 |
{ |
| 1736 |
auto pcAction = new Gui::ActionGroup(this, Gui::getMainWindow()); |
| 1737 |
pcAction->setDropDownMenu(hasDropDownMenu()); |
| 1738 |
pcAction->setExclusive(isExclusive()); |
| 1739 |
|
| 1740 |
applyCommandData(this->getName(), pcAction); |
| 1741 |
|
| 1742 |
int defaultId = 0; |
| 1743 |
|
| 1744 |
try { |
| 1745 |
Base::PyGILStateLocker lock; |
| 1746 |
Py::Object cmd(_pcPyCommand); |
| 1747 |
Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager(); |
| 1748 |
|
| 1749 |
Py::Callable call(cmd.getAttr("GetCommands")); |
| 1750 |
Py::Sequence args; |
| 1751 |
Py::Sequence ret(call.apply(args)); |
| 1752 |
for (auto it = ret.begin(); it != ret.end(); ++it) { |
| 1753 |
Py::String str(*it); |
| 1754 |
QAction* cmd = pcAction->addAction(QString()); |
| 1755 |
cmd->setProperty("CommandName", QByteArray(static_cast<std::string>(str).c_str())); |
| 1756 |
|
| 1757 |
PythonCommand* pycmd = dynamic_cast<PythonCommand*>( |
| 1758 |
rcCmdMgr.getCommandByName(cmd->property("CommandName").toByteArray()) |
| 1759 |
); |
| 1760 |
if (pycmd && pycmd->isCheckable()) { |
| 1761 |
cmd->setCheckable(true); |
| 1762 |
cmd->blockSignals(true); |
| 1763 |
cmd->setChecked(pycmd->isChecked()); |
| 1764 |
cmd->blockSignals(false); |
| 1765 |
} |
| 1766 |
cmd->setShortcut( |
| 1767 |
ShortcutManager::instance()->getShortcut(cmd->property("CommandName").toByteArray()) |
| 1768 |
); |
| 1769 |
} |
| 1770 |
|
| 1771 |
if (cmd.hasAttr("GetDefaultCommand")) { |
| 1772 |
Py::Callable call2(cmd.getAttr("GetDefaultCommand")); |
| 1773 |
Py::Long def(call2.apply(args)); |
| 1774 |
defaultId = static_cast<int>(def); |
| 1775 |
} |
| 1776 |
|
| 1777 |
QList<QAction*> a = pcAction->actions(); |
| 1778 |
if (defaultId >= 0 && defaultId < a.size()) { |
| 1779 |
QAction* qtAction = a[defaultId]; |
| 1780 |
if (qtAction->isCheckable()) { |
| 1781 |
// if the command is 'exclusive' then activate the default action |
| 1782 |
if (pcAction->isExclusive()) { |
| 1783 |
qtAction->blockSignals(true); |
| 1784 |
qtAction->setChecked(true); |
| 1785 |
qtAction->blockSignals(false); |
| 1786 |
} |
| 1787 |
else if (qtAction->isCheckable()) { |
| 1788 |
pcAction->setCheckable(true); |
| 1789 |
pcAction->setBlockedChecked(qtAction->isChecked()); |
| 1790 |
} |
| 1791 |
} |
| 1792 |
} |
| 1793 |
} |
| 1794 |
catch (Py::Exception&) { |
| 1795 |
Base::PyGILStateLocker lock; |
| 1796 |
Base::PyException e; |
| 1797 |
Base::Console().error( |
| 1798 |
"createAction() of the Python command '%s' failed:\n%s\n%s", |
| 1799 |
sName, |
| 1800 |
e.getStackTrace().c_str(), |
| 1801 |
e.what() |
| 1802 |
); |
| 1803 |
} |
| 1804 |
|
| 1805 |
_pcAction = pcAction; |
| 1806 |
languageChange(); |
| 1807 |
|
| 1808 |
if (strcmp(getResource("Pixmap"), "") != 0) { |
| 1809 |
pcAction->setIcon(Gui::BitmapFactory().iconFromTheme(getResource("Pixmap"))); |
| 1810 |
} |
| 1811 |
else { |
| 1812 |
QList<QAction*> a = pcAction->actions(); |
| 1813 |
// if out of range then set to 0 |
| 1814 |
if (defaultId < 0 || defaultId >= a.size()) { |
| 1815 |
defaultId = 0; |
| 1816 |
} |
| 1817 |
if (a.size() > defaultId) { |
| 1818 |
pcAction->setIcon(a[defaultId]->icon()); |
| 1819 |
} |
| 1820 |
} |
| 1821 |
|
| 1822 |
pcAction->setProperty("defaultAction", QVariant(defaultId)); |
| 1823 |
|
| 1824 |
return pcAction; |
| 1825 |
} |
| 1826 |
|
| 1827 |
void PythonGroupCommand::languageChange() |
| 1828 |
{ |
| 1829 |
if (!_pcAction) { |
| 1830 |
return; |
| 1831 |
} |
| 1832 |
|
| 1833 |
applyCommandData(this->getName(), _pcAction); |
| 1834 |
|
| 1835 |
Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager(); |
| 1836 |
|
| 1837 |
// Reapply setup to ensure group action tooltip includes shortcut |
| 1838 |
auto* pcActionGroup = qobject_cast<Gui::ActionGroup*>(_pcAction); |
| 1839 |
QList<QAction*> groupActions = pcActionGroup->actions(); |
| 1840 |
int idx = _pcAction->property("defaultAction").toInt(); |
| 1841 |
if (idx >= 0 && idx < groupActions.size()) { |
| 1842 |
QAction* defaultAction = groupActions[idx]; |
| 1843 |
Gui::Command* cmd = rcCmdMgr.getCommandByName( |
| 1844 |
defaultAction->property("CommandName").toByteArray() |
| 1845 |
); |
| 1846 |
if (cmd) { |
| 1847 |
const char* context = cmd->getName(); |
| 1848 |
QString tip = QApplication::translate(context, cmd->getToolTipText()); |
| 1849 |
_pcAction->setShortcut(cmd->getShortcut()); |
| 1850 |
QString newTip = Gui::Action::createToolTip( |
| 1851 |
tip, |
| 1852 |
_pcAction->text(), |
| 1853 |
_pcAction->action()->font(), |
| 1854 |
_pcAction->shortcut().toString(), |
| 1855 |
cmd |
| 1856 |
); |
| 1857 |
_pcAction->setToolTip(newTip); |
| 1858 |
} |
| 1859 |
} |
| 1860 |
auto* pcAction = qobject_cast<Gui::ActionGroup*>(_pcAction); |
| 1861 |
QList<QAction*> actions = pcAction->actions(); |
| 1862 |
for (const auto& it : actions) { |
| 1863 |
Gui::Command* cmd = rcCmdMgr.getCommandByName(it->property("CommandName").toByteArray()); |
| 1864 |
if (cmd) { |
| 1865 |
// Python command use getName as context |
| 1866 |
const char* context = dynamic_cast<PythonCommand*>(cmd) ? cmd->getName() |
| 1867 |
: cmd->className(); |
| 1868 |
const char* tooltip = cmd->getToolTipText(); |
| 1869 |
const char* statustip = cmd->getStatusTip(); |
| 1870 |
if (!statustip || '\0' == *statustip) { |
| 1871 |
statustip = tooltip; |
| 1872 |
} |
| 1873 |
|
| 1874 |
it->setIcon(Gui::BitmapFactory().iconFromTheme(cmd->getPixmap())); |
| 1875 |
QString text = QApplication::translate(context, cmd->getMenuText()); |
| 1876 |
it->setText(text); |
| 1877 |
QString helpText = QApplication::translate(context, tooltip); |
| 1878 |
QString shortCut = it->shortcut().toString(); |
| 1879 |
QFont font = it->font(); |
| 1880 |
QString newTip = Gui::Action::createToolTip(helpText, text, font, shortCut, cmd); |
| 1881 |
it->setToolTip(newTip); |
| 1882 |
it->setStatusTip(QApplication::translate(context, statustip)); |
| 1883 |
} |
| 1884 |
} |
| 1885 |
} |
| 1886 |
|
| 1887 |
const char* PythonGroupCommand::getHelpUrl() const |
| 1888 |
{ |
| 1889 |
return ""; |
| 1890 |
} |
| 1891 |
|
| 1892 |
const char* PythonGroupCommand::getResource(const char* sName) const |
| 1893 |
{ |
| 1894 |
Base::PyGILStateLocker lock; |
| 1895 |
PyObject* pcTemp; |
| 1896 |
|
| 1897 |
// get the "MenuText" resource string |
| 1898 |
pcTemp = PyDict_GetItemString(_pcPyResource, sName); |
| 1899 |
if (!pcTemp) { |
| 1900 |
return ""; |
| 1901 |
} |
| 1902 |
if (!PyUnicode_Check(pcTemp)) { |
| 1903 |
throw Base::ValueError( |
| 1904 |
"PythonGroupCommand::getResource(): Method GetResources() of the Python " |
| 1905 |
"group command object returns a dictionary which holds not only strings" |
| 1906 |
); |
| 1907 |
} |
| 1908 |
return PyUnicode_AsUTF8(pcTemp); |
| 1909 |
} |
| 1910 |
|
| 1911 |
const char* PythonGroupCommand::getWhatsThis() const |
| 1912 |
{ |
| 1913 |
const char* whatsthis = getResource("WhatsThis"); |
| 1914 |
if (Base::Tools::isNullOrEmpty(whatsthis)) { |
| 1915 |
whatsthis = this->getName(); |
| 1916 |
} |
| 1917 |
return whatsthis; |
| 1918 |
} |
| 1919 |
|
| 1920 |
const char* PythonGroupCommand::getMenuText() const |
| 1921 |
{ |
| 1922 |
return getResource("MenuText"); |
| 1923 |
} |
| 1924 |
|
| 1925 |
const char* PythonGroupCommand::getToolTipText() const |
| 1926 |
{ |
| 1927 |
return getResource("ToolTip"); |
| 1928 |
} |
| 1929 |
|
| 1930 |
const char* PythonGroupCommand::getStatusTip() const |
| 1931 |
{ |
| 1932 |
return getResource("StatusTip"); |
| 1933 |
} |
| 1934 |
|
| 1935 |
const char* PythonGroupCommand::getPixmap() const |
| 1936 |
{ |
| 1937 |
const char* ret = getResource("Pixmap"); |
| 1938 |
return !Base::Tools::isNullOrEmpty(ret) ? ret : nullptr; |
| 1939 |
} |
| 1940 |
|
| 1941 |
const char* PythonGroupCommand::getAccel() const |
| 1942 |
{ |
| 1943 |
return getResource("Accel"); |
| 1944 |
} |
| 1945 |
|
| 1946 |
bool PythonGroupCommand::isExclusive() const |
| 1947 |
{ |
| 1948 |
PyObject* item = PyDict_GetItemString(_pcPyResource, "Exclusive"); |
| 1949 |
if (!item) { |
| 1950 |
return false; |
| 1951 |
} |
| 1952 |
|
| 1953 |
if (PyBool_Check(item)) { |
| 1954 |
return Base::asBoolean(item); |
| 1955 |
} |
| 1956 |
else { |
| 1957 |
throw Base::TypeError( |
| 1958 |
"PythonGroupCommand::isExclusive(): Method GetResources() of the Python " |
| 1959 |
"command object contains the key 'Exclusive' which is not a boolean" |
| 1960 |
); |
| 1961 |
} |
| 1962 |
} |
| 1963 |
|
| 1964 |
bool PythonGroupCommand::hasDropDownMenu() const |
| 1965 |
{ |
| 1966 |
PyObject* item = PyDict_GetItemString(_pcPyResource, "DropDownMenu"); |
| 1967 |
if (!item) { |
| 1968 |
return true; |
| 1969 |
} |
| 1970 |
|
| 1971 |
if (PyBool_Check(item)) { |
| 1972 |
return Base::asBoolean(item); |
| 1973 |
} |
| 1974 |
else { |
| 1975 |
throw Base::TypeError( |
| 1976 |
"PythonGroupCommand::hasDropDownMenu(): Method GetResources() of the Python " |
| 1977 |
"command object contains the key 'DropDownMenu' which is not a boolean" |
| 1978 |
); |
| 1979 |
} |
| 1980 |
} |
| 1981 |
|
| 1982 |
void PythonGroupCommand::onActionInit() const |
| 1983 |
{ |
| 1984 |
try { |
| 1985 |
Base::PyGILStateLocker lock; |
| 1986 |
Py::Object cmd(_pcPyCommand); |
| 1987 |
if (cmd.hasAttr("OnActionInit")) { |
| 1988 |
Py::Callable call(cmd.getAttr("OnActionInit")); |
| 1989 |
Py::Tuple args; |
| 1990 |
Py::Object ret = call.apply(args); |
| 1991 |
} |
| 1992 |
} |
| 1993 |
catch (Py::Exception& e) { |
| 1994 |
Base::PyGILStateLocker lock; |
| 1995 |
e.clear(); |
| 1996 |
} |
| 1997 |
|
| 1998 |
connPyCmdInitialized.disconnect(); |
| 1999 |
} |
| 2000 |
|
| 2001 |
//=========================================================================== |
| 2002 |
// CommandManager |
| 2003 |
//=========================================================================== |
| 2004 |
|
| 2005 |
CommandManager::CommandManager() = default; |
| 2006 |
|
| 2007 |
CommandManager::~CommandManager() |
| 2008 |
{ |
| 2009 |
clearCommands(); |
| 2010 |
} |
| 2011 |
|
| 2012 |
void CommandManager::addCommand(Command* pCom) |
| 2013 |
{ |
| 2014 |
auto& cmd = _sCommands[pCom->getName()]; |
| 2015 |
if (cmd) { |
| 2016 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_LOG)) { |
| 2017 |
FC_ERR("duplicate command " << pCom->getName()); |
| 2018 |
} |
| 2019 |
return; |
| 2020 |
} |
| 2021 |
++_revision; |
| 2022 |
cmd = pCom; |
| 2023 |
signalChanged(); |
| 2024 |
} |
| 2025 |
|
| 2026 |
void CommandManager::removeCommand(Command* pCom) |
| 2027 |
{ |
| 2028 |
std::map<std::string, Command*>::iterator It = _sCommands.find(pCom->getName()); |
| 2029 |
if (It != _sCommands.end()) { |
| 2030 |
++_revision; |
| 2031 |
delete It->second; |
| 2032 |
_sCommands.erase(It); |
| 2033 |
signalChanged(); |
| 2034 |
} |
| 2035 |
} |
| 2036 |
|
| 2037 |
std::string CommandManager::newMacroName() const |
| 2038 |
{ |
| 2039 |
CommandManager& commandManager = Application::Instance->commandManager(); |
| 2040 |
std::vector<Command*> macros = commandManager.getGroupCommands("Macros"); |
| 2041 |
|
| 2042 |
bool used = true; |
| 2043 |
int id = 0; |
| 2044 |
std::string name; |
| 2045 |
while (used) { |
| 2046 |
used = false; |
| 2047 |
std::ostringstream test_name; |
| 2048 |
test_name << "Std_Macro_" << id++; |
| 2049 |
|
| 2050 |
for (const auto& macro : macros) { |
| 2051 |
if (test_name.str() == std::string(macro->getName())) { |
| 2052 |
used = true; |
| 2053 |
break; |
| 2054 |
} |
| 2055 |
} |
| 2056 |
if (!used) { |
| 2057 |
name = test_name.str(); |
| 2058 |
} |
| 2059 |
} |
| 2060 |
|
| 2061 |
return name; |
| 2062 |
} |
| 2063 |
void CommandManager::clearCommands() |
| 2064 |
{ |
| 2065 |
for (std::map<std::string, Command*>::iterator it = _sCommands.begin(); it != _sCommands.end(); |
| 2066 |
++it) { |
| 2067 |
delete it->second; |
| 2068 |
} |
| 2069 |
_sCommands.clear(); |
| 2070 |
++_revision; |
| 2071 |
signalChanged(); |
| 2072 |
} |
| 2073 |
|
| 2074 |
bool CommandManager::addTo(const char* Name, QWidget* pcWidget) |
| 2075 |
{ |
| 2076 |
if (_sCommands.find(Name) == _sCommands.end()) { |
| 2077 |
// Print in release mode only a log message instead of an error message to avoid to annoy |
| 2078 |
// the user |
| 2079 |
#ifdef FC_DEBUG |
| 2080 |
Base::Console().error( |
| 2081 |
"CommandManager::addTo() try to add an unknown command (%s) to a widget!\n", |
| 2082 |
Name |
| 2083 |
); |
| 2084 |
#else |
| 2085 |
Base::Console().warning("Unknown command '%s'\n", Name); |
| 2086 |
#endif |
| 2087 |
return false; |
| 2088 |
} |
| 2089 |
else { |
| 2090 |
Command* pCom = _sCommands[Name]; |
| 2091 |
pCom->addTo(pcWidget); |
| 2092 |
return true; |
| 2093 |
} |
| 2094 |
} |
| 2095 |
|
| 2096 |
std::vector<Command*> CommandManager::getModuleCommands(const char* sModName) const |
| 2097 |
{ |
| 2098 |
std::vector<Command*> vCmds; |
| 2099 |
|
| 2100 |
for (std::map<std::string, Command*>::const_iterator It = _sCommands.begin(); |
| 2101 |
It != _sCommands.end(); |
| 2102 |
++It) { |
| 2103 |
if (strcmp(It->second->getAppModuleName(), sModName) == 0) { |
| 2104 |
vCmds.push_back(It->second); |
| 2105 |
} |
| 2106 |
} |
| 2107 |
|
| 2108 |
return vCmds; |
| 2109 |
} |
| 2110 |
|
| 2111 |
std::vector<Command*> CommandManager::getAllCommands() const |
| 2112 |
{ |
| 2113 |
std::vector<Command*> vCmds; |
| 2114 |
|
| 2115 |
for (std::map<std::string, Command*>::const_iterator It = _sCommands.begin(); |
| 2116 |
It != _sCommands.end(); |
| 2117 |
++It) { |
| 2118 |
vCmds.push_back(It->second); |
| 2119 |
} |
| 2120 |
|
| 2121 |
return vCmds; |
| 2122 |
} |
| 2123 |
|
| 2124 |
std::vector<Command*> CommandManager::getGroupCommands(const char* sGrpName) const |
| 2125 |
{ |
| 2126 |
std::vector<Command*> vCmds; |
| 2127 |
|
| 2128 |
for (std::map<std::string, Command*>::const_iterator It = _sCommands.begin(); |
| 2129 |
It != _sCommands.end(); |
| 2130 |
++It) { |
| 2131 |
if (strcmp(It->second->getGroupName(), sGrpName) == 0) { |
| 2132 |
vCmds.push_back(It->second); |
| 2133 |
} |
| 2134 |
} |
| 2135 |
|
| 2136 |
return vCmds; |
| 2137 |
} |
| 2138 |
|
| 2139 |
Command* CommandManager::getCommandByName(const char* sName) const |
| 2140 |
{ |
| 2141 |
std::map<std::string, Command*>::const_iterator it = _sCommands.find(sName); |
| 2142 |
return (it != _sCommands.end()) ? it->second : 0; |
| 2143 |
} |
| 2144 |
|
| 2145 |
void CommandManager::runCommandByName(const char* sName) const |
| 2146 |
{ |
| 2147 |
Command* pCmd = getCommandByName(sName); |
| 2148 |
|
| 2149 |
if (pCmd) { |
| 2150 |
pCmd->invoke(0); |
| 2151 |
} |
| 2152 |
} |
| 2153 |
|
| 2154 |
void CommandManager::testActive() |
| 2155 |
{ |
| 2156 |
for (std::map<std::string, Command*>::iterator It = _sCommands.begin(); It != _sCommands.end(); |
| 2157 |
++It) { |
| 2158 |
It->second->testActive(); |
| 2159 |
} |
| 2160 |
} |
| 2161 |
|
| 2162 |
void CommandManager::addCommandMode(const char* sContext, const char* sName) |
| 2163 |
{ |
| 2164 |
_sCommandModes[sContext].push_back(sName); |
| 2165 |
} |
| 2166 |
|
| 2167 |
void CommandManager::updateCommands(const char* sContext, int mode) |
| 2168 |
{ |
| 2169 |
std::map<std::string, std::list<std::string>>::iterator it = _sCommandModes.find(sContext); |
| 2170 |
int rev = _revision; |
| 2171 |
if (it != _sCommandModes.end()) { |
| 2172 |
for (const auto& jt : it->second) { |
| 2173 |
Command* cmd = getCommandByName(jt.c_str()); |
| 2174 |
if (cmd) { |
| 2175 |
cmd->updateAction(mode); |
| 2176 |
} |
| 2177 |
} |
| 2178 |
} |
| 2179 |
if (rev != _revision) { |
| 2180 |
signalChanged(); |
| 2181 |
} |
| 2182 |
} |
| 2183 |
|
| 2184 |
const Command* Gui::CommandManager::checkAcceleratorForConflicts( |
| 2185 |
const char* accel, |
| 2186 |
const Command* ignore |
| 2187 |
) const |
| 2188 |
{ |
| 2189 |
if (Base::Tools::isNullOrEmpty(accel)) { |
| 2190 |
return nullptr; |
| 2191 |
} |
| 2192 |
|
| 2193 |
QString newCombo = QString::fromLatin1(accel); |
| 2194 |
if (newCombo.isEmpty()) { |
| 2195 |
return nullptr; |
| 2196 |
} |
| 2197 |
auto newSequence = QKeySequence::fromString(newCombo); |
| 2198 |
if (newSequence.isEmpty()) { |
| 2199 |
return nullptr; |
| 2200 |
} |
| 2201 |
|
| 2202 |
// Does this command shortcut conflict with other commands already defined? |
| 2203 |
auto commands = Application::Instance->commandManager().getAllCommands(); |
| 2204 |
for (const auto& cmd : commands) { |
| 2205 |
if (cmd == ignore) { |
| 2206 |
continue; |
| 2207 |
} |
| 2208 |
auto existingAccel = cmd->getAccel(); |
| 2209 |
if (Base::Tools::isNullOrEmpty(existingAccel)) { |
| 2210 |
continue; |
| 2211 |
} |
| 2212 |
|
| 2213 |
// Three possible conflict scenarios: |
| 2214 |
// 1) Exactly the same combo as another command |
| 2215 |
// 2) The new command is a one-char combo that overrides an existing two-char combo |
| 2216 |
// 3) The old command is a one-char combo that overrides the new command |
| 2217 |
|
| 2218 |
QString existingCombo = QString::fromLatin1(existingAccel); |
| 2219 |
if (existingCombo.isEmpty()) { |
| 2220 |
continue; |
| 2221 |
} |
| 2222 |
auto existingSequence = QKeySequence::fromString(existingCombo); |
| 2223 |
if (existingSequence.isEmpty()) { |
| 2224 |
continue; |
| 2225 |
} |
| 2226 |
|
| 2227 |
// Exact match |
| 2228 |
if (existingSequence == newSequence) { |
| 2229 |
return cmd; |
| 2230 |
} |
| 2231 |
|
| 2232 |
// If it's not exact, then see if one of the sequences is a partial match for |
| 2233 |
// the beginning of the other sequence |
| 2234 |
auto numCharsToCheck = std::min(existingSequence.count(), newSequence.count()); |
| 2235 |
bool firstNMatch = true; |
| 2236 |
for (int i = 0; i < numCharsToCheck; ++i) { |
| 2237 |
if (newSequence[i] != existingSequence[i]) { |
| 2238 |
firstNMatch = false; |
| 2239 |
break; |
| 2240 |
} |
| 2241 |
} |
| 2242 |
if (firstNMatch) { |
| 2243 |
return cmd; |
| 2244 |
} |
| 2245 |
} |
| 2246 |
|
| 2247 |
return nullptr; |
| 2248 |
} |