| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 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 <QActionEvent> |
| 24 |
#include <QActionGroup> |
| 25 |
#include <QApplication> |
| 26 |
#include <QEvent> |
| 27 |
#include <QFileInfo> |
| 28 |
#include <QMenu> |
| 29 |
#include <QRegularExpression> |
| 30 |
#include <QScreen> |
| 31 |
#include <QTimer> |
| 32 |
#include <QToolBar> |
| 33 |
#include <QToolButton> |
| 34 |
#include <QToolTip> |
| 35 |
|
| 36 |
#include <Base/Exception.h> |
| 37 |
#include <Base/Interpreter.h> |
| 38 |
#include <Base/Tools.h> |
| 39 |
#include <App/Document.h> |
| 40 |
|
| 41 |
#include "Action.h" |
| 42 |
#include "BitmapFactory.h" |
| 43 |
#include "Command.h" |
| 44 |
#include "Dialogs/DlgUndoRedo.h" |
| 45 |
#include "PreferencePages/DlgSettingsWorkbenchesImp.h" |
| 46 |
#include "Document.h" |
| 47 |
#include "EditorView.h" |
| 48 |
#include "Macro.h" |
| 49 |
#include "ModuleIO.h" |
| 50 |
#include "MainWindow.h" |
| 51 |
#include "PythonEditor.h" |
| 52 |
#include "WhatsThis.h" |
| 53 |
#include "Widgets.h" |
| 54 |
#include "Workbench.h" |
| 55 |
#include "WorkbenchManager.h" |
| 56 |
#include "WorkbenchSelector.h" |
| 57 |
#include "ShortcutManager.h" |
| 58 |
#include "Tools.h" |
| 59 |
|
| 60 |
|
| 61 |
using namespace Gui; |
| 62 |
using namespace Gui::Dialog; |
| 63 |
namespace sp = std::placeholders; |
| 64 |
|
| 65 |
/** |
| 66 |
* Constructs an action called \a name with parent \a parent. It also stores a pointer |
| 67 |
* to the command object. |
| 68 |
*/ |
| 69 |
Action::Action(Command* pcCmd, QObject* parent) |
| 70 |
: QObject(parent) |
| 71 |
, _action(new QAction(this)) |
| 72 |
, _pcCmd(pcCmd) |
| 73 |
{ |
| 74 |
_action->setObjectName(QString::fromLatin1(_pcCmd->getName())); |
| 75 |
_connection = connect(_action, &QAction::triggered, this, &Action::onActivated); |
| 76 |
} |
| 77 |
|
| 78 |
Action::Action(Command* pcCmd, QAction* action, QObject* parent) |
| 79 |
: QObject(parent) |
| 80 |
, _action(action) |
| 81 |
, _pcCmd(pcCmd) |
| 82 |
{ |
| 83 |
_action->setParent(this); |
| 84 |
_action->setObjectName(QString::fromLatin1(_pcCmd->getName())); |
| 85 |
_connection = connect(_action, &QAction::triggered, this, &Action::onActivated); |
| 86 |
} |
| 87 |
|
| 88 |
Action::~Action() |
| 89 |
{ |
| 90 |
delete _action; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Adds this action to widget \a widget. |
| 95 |
*/ |
| 96 |
void Action::addTo(QWidget* widget) |
| 97 |
{ |
| 98 |
widget->addAction(_action); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Activates the command. |
| 103 |
*/ |
| 104 |
void Action::onActivated() |
| 105 |
{ |
| 106 |
command()->invoke(0, Command::TriggerAction); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Sets whether the command is toggled. |
| 111 |
*/ |
| 112 |
void Action::onToggled(bool toggle) |
| 113 |
{ |
| 114 |
command()->invoke(toggle ? 1 : 0, Command::TriggerAction); |
| 115 |
} |
| 116 |
|
| 117 |
void Action::setCheckable(bool check) |
| 118 |
{ |
| 119 |
if (check == _action->isCheckable()) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
_action->setCheckable(check); |
| 124 |
|
| 125 |
if (check) { |
| 126 |
disconnect(_connection); |
| 127 |
_connection = connect(_action, &QAction::toggled, this, &Action::onToggled); |
| 128 |
} |
| 129 |
else { |
| 130 |
disconnect(_connection); |
| 131 |
_connection = connect(_action, &QAction::triggered, this, &Action::onActivated); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
void Action::setChecked(bool check) |
| 136 |
{ |
| 137 |
_action->setChecked(check); |
| 138 |
} |
| 139 |
|
| 140 |
/*! |
| 141 |
* \brief Action::setBlockedChecked |
| 142 |
* \param check |
| 143 |
* Does the same as \ref setChecked but additionally blocks |
| 144 |
* any signals. |
| 145 |
*/ |
| 146 |
void Action::setBlockedChecked(bool check) |
| 147 |
{ |
| 148 |
QSignalBlocker block(_action); |
| 149 |
_action->setChecked(check); |
| 150 |
} |
| 151 |
|
| 152 |
bool Action::isChecked() const |
| 153 |
{ |
| 154 |
return _action->isChecked(); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Sets whether the action is enabled. |
| 159 |
*/ |
| 160 |
void Action::setEnabled(bool enable) |
| 161 |
{ |
| 162 |
_action->setEnabled(enable); |
| 163 |
} |
| 164 |
|
| 165 |
bool Action::isEnabled() const |
| 166 |
{ |
| 167 |
return _action->isEnabled(); |
| 168 |
} |
| 169 |
|
| 170 |
void Action::setVisible(bool visible) |
| 171 |
{ |
| 172 |
_action->setVisible(visible); |
| 173 |
} |
| 174 |
|
| 175 |
void Action::setShortcut(const QString& key) |
| 176 |
{ |
| 177 |
_action->setShortcut(key); |
| 178 |
setToolTip(_tooltip, _title); |
| 179 |
} |
| 180 |
|
| 181 |
QKeySequence Action::shortcut() const |
| 182 |
{ |
| 183 |
return _action->shortcut(); |
| 184 |
} |
| 185 |
|
| 186 |
void Action::setIcon(const QIcon& icon) |
| 187 |
{ |
| 188 |
_action->setIcon(icon); |
| 189 |
} |
| 190 |
|
| 191 |
QIcon Action::icon() const |
| 192 |
{ |
| 193 |
return _action->icon(); |
| 194 |
} |
| 195 |
|
| 196 |
void Action::setStatusTip(const QString& text) |
| 197 |
{ |
| 198 |
_action->setStatusTip(text); |
| 199 |
} |
| 200 |
|
| 201 |
QString Action::statusTip() const |
| 202 |
{ |
| 203 |
return _action->statusTip(); |
| 204 |
} |
| 205 |
|
| 206 |
void Action::setText(const QString& text) |
| 207 |
{ |
| 208 |
_action->setText(text); |
| 209 |
if (_title.isEmpty()) { |
| 210 |
setToolTip(_tooltip); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
QString Action::text() const |
| 215 |
{ |
| 216 |
return _action->text(); |
| 217 |
} |
| 218 |
|
| 219 |
void Action::setToolTip(const QString& text, const QString& title) |
| 220 |
{ |
| 221 |
_tooltip = text; |
| 222 |
_title = title; |
| 223 |
_action->setToolTip(createToolTip( |
| 224 |
text, |
| 225 |
title.isEmpty() ? _action->text() : title, |
| 226 |
_action->font(), |
| 227 |
_action->shortcut().toString(QKeySequence::NativeText), |
| 228 |
command() |
| 229 |
)); |
| 230 |
} |
| 231 |
|
| 232 |
QString Action::cleanTitle(const QString& title) |
| 233 |
{ |
| 234 |
QString text(title); |
| 235 |
// Deal with QAction title mnemonic |
| 236 |
static QRegularExpression re(QStringLiteral("&(.)")); |
| 237 |
text.replace(re, QStringLiteral("\\1")); |
| 238 |
|
| 239 |
// Probably not a good idea to trim ending punctuation |
| 240 |
#if 0 |
| 241 |
// Trim line ending punctuation |
| 242 |
static QRegularExpression rePunct(QStringLiteral("[[:punct:]]+$")); |
| 243 |
text.replace(rePunct, QString()); |
| 244 |
#endif |
| 245 |
return text; |
| 246 |
} |
| 247 |
|
| 248 |
QString Action::commandToolTip(const Command* cmd, bool richFormat) |
| 249 |
{ |
| 250 |
if (!cmd) { |
| 251 |
return {}; |
| 252 |
} |
| 253 |
|
| 254 |
if (richFormat) { |
| 255 |
if (auto action = cmd->getAction()) { |
| 256 |
return action->_action->toolTip(); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
QString title, tooltip; |
| 261 |
if (dynamic_cast<const MacroCommand*>(cmd)) { |
| 262 |
if (auto txt = cmd->getMenuText()) { |
| 263 |
title = QString::fromUtf8(txt); |
| 264 |
} |
| 265 |
if (auto txt = cmd->getToolTipText()) { |
| 266 |
tooltip = QString::fromUtf8(txt); |
| 267 |
} |
| 268 |
} |
| 269 |
else { |
| 270 |
if (auto txt = cmd->getMenuText()) { |
| 271 |
title = qApp->translate(cmd->className(), txt); |
| 272 |
} |
| 273 |
if (auto txt = cmd->getToolTipText()) { |
| 274 |
tooltip = qApp->translate(cmd->className(), txt); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if (!richFormat) { |
| 279 |
return tooltip; |
| 280 |
} |
| 281 |
return createToolTip(tooltip, title, QFont(), cmd->getShortcut(), cmd); |
| 282 |
} |
| 283 |
|
| 284 |
QString Action::commandMenuText(const Command* cmd) |
| 285 |
{ |
| 286 |
if (!cmd) { |
| 287 |
return {}; |
| 288 |
} |
| 289 |
|
| 290 |
QString title; |
| 291 |
if (auto action = cmd->getAction()) { |
| 292 |
title = action->text(); |
| 293 |
} |
| 294 |
else if (dynamic_cast<const MacroCommand*>(cmd)) { |
| 295 |
if (auto txt = cmd->getMenuText()) { |
| 296 |
title = QString::fromUtf8(txt); |
| 297 |
} |
| 298 |
} |
| 299 |
else { |
| 300 |
if (auto txt = cmd->getMenuText()) { |
| 301 |
title = qApp->translate(cmd->className(), txt); |
| 302 |
} |
| 303 |
} |
| 304 |
if (title.isEmpty()) { |
| 305 |
title = QString::fromUtf8(cmd->getName()); |
| 306 |
} |
| 307 |
else { |
| 308 |
title = cleanTitle(title); |
| 309 |
} |
| 310 |
return title; |
| 311 |
} |
| 312 |
|
| 313 |
QString Action::createToolTip( |
| 314 |
QString helpText, |
| 315 |
const QString& title, |
| 316 |
const QFont& font, |
| 317 |
const QString& shortCut, |
| 318 |
const Command* command |
| 319 |
) |
| 320 |
{ |
| 321 |
QString text = cleanTitle(title); |
| 322 |
|
| 323 |
if (text.isEmpty()) { |
| 324 |
return helpText; |
| 325 |
} |
| 326 |
|
| 327 |
// The following code tries to make a more useful tooltip by inserting at |
| 328 |
// the beginning of the tooltip the action title in bold followed by the |
| 329 |
// shortcut. |
| 330 |
// |
| 331 |
// The long winding code is to deal with the fact that Qt will auto wrap |
| 332 |
// a rich text tooltip but the width is too short. We can escape the auto |
| 333 |
// wrapping using <p style='white-space:pre'>. |
| 334 |
|
| 335 |
QString shortcut = shortCut; |
| 336 |
if (!shortcut.isEmpty() && helpText.endsWith(shortcut)) { |
| 337 |
helpText.resize(helpText.size() - shortcut.size()); |
| 338 |
} |
| 339 |
if (!shortcut.isEmpty()) { |
| 340 |
shortcut = QStringLiteral(" (%1)").arg(shortcut); |
| 341 |
} |
| 342 |
|
| 343 |
QString tooltip = QStringLiteral("<p style='white-space:pre; margin-bottom:0.5em;'><b>%1</b>%2</p>") |
| 344 |
.arg(text.toHtmlEscaped(), shortcut.toHtmlEscaped()); |
| 345 |
|
| 346 |
QString cmdName; |
| 347 |
if (command && command->getName()) { |
| 348 |
cmdName = QString::fromLatin1(command->getName()); |
| 349 |
if (auto groupcmd = dynamic_cast<const GroupCommand*>(command)) { |
| 350 |
if (auto act = command->getAction()) { |
| 351 |
int idx = act->property("defaultAction").toInt(); |
| 352 |
auto cmd = groupcmd->getCommand(idx); |
| 353 |
if (cmd && cmd->getName()) { |
| 354 |
cmdName = QStringLiteral("%1 (%2:%3)") |
| 355 |
.arg(QString::fromLatin1(cmd->getName()), cmdName) |
| 356 |
.arg(idx); |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
cmdName = QStringLiteral("<p style='white-space:pre; margin-top:0.5em;'><i>%1</i></p>") |
| 361 |
.arg(cmdName.toHtmlEscaped()); |
| 362 |
} |
| 363 |
|
| 364 |
if (!shortcut.isEmpty() && helpText.endsWith(shortcut)) { |
| 365 |
helpText.resize(helpText.size() - shortcut.size()); |
| 366 |
} |
| 367 |
|
| 368 |
if (helpText.isEmpty() || helpText == text || helpText == title) { |
| 369 |
return tooltip + cmdName; |
| 370 |
} |
| 371 |
if (Qt::mightBeRichText(helpText)) { |
| 372 |
// already rich text, so let it be to avoid duplicated unwrapping |
| 373 |
return tooltip + helpText + cmdName; |
| 374 |
} |
| 375 |
|
| 376 |
tooltip += QStringLiteral("<p style='white-space:pre; margin:0;'>"); |
| 377 |
|
| 378 |
// If the user supplied tooltip contains line break, we shall honour it. |
| 379 |
if (helpText.indexOf(QLatin1Char('\n')) >= 0) { |
| 380 |
tooltip += helpText.toHtmlEscaped() + QStringLiteral("</p>"); |
| 381 |
} |
| 382 |
else { |
| 383 |
// If not, try to end the non wrapping paragraph at some pre defined |
| 384 |
// width, so that the following text can wrap at that width. |
| 385 |
float tipWidth = 400; |
| 386 |
QFontMetrics fm(font); |
| 387 |
int width = QtTools::horizontalAdvance(fm, helpText); |
| 388 |
if (width <= tipWidth) { |
| 389 |
tooltip += helpText.toHtmlEscaped() + QStringLiteral("</p>"); |
| 390 |
} |
| 391 |
else { |
| 392 |
int index = tipWidth / width * helpText.size(); |
| 393 |
// Try to only break at white space |
| 394 |
for (int i = 0; i < 50 && index < helpText.size(); ++i, ++index) { |
| 395 |
if (helpText[index] == QLatin1Char(' ')) { |
| 396 |
break; |
| 397 |
} |
| 398 |
} |
| 399 |
tooltip += helpText.left(index).toHtmlEscaped() + QStringLiteral("</p>") |
| 400 |
+ helpText.right(helpText.size() - index).trimmed().toHtmlEscaped(); |
| 401 |
} |
| 402 |
} |
| 403 |
return tooltip + cmdName; |
| 404 |
} |
| 405 |
|
| 406 |
QString Action::toolTip() const |
| 407 |
{ |
| 408 |
return _tooltip; |
| 409 |
} |
| 410 |
|
| 411 |
void Action::setWhatsThis(const QString& text) |
| 412 |
{ |
| 413 |
_action->setWhatsThis(text); |
| 414 |
} |
| 415 |
|
| 416 |
QString Action::whatsThis() const |
| 417 |
{ |
| 418 |
return _action->whatsThis(); |
| 419 |
} |
| 420 |
|
| 421 |
void Action::setMenuRole(QAction::MenuRole menuRole) |
| 422 |
{ |
| 423 |
_action->setMenuRole(menuRole); |
| 424 |
} |
| 425 |
|
| 426 |
// -------------------------------------------------------------------- |
| 427 |
|
| 428 |
/** |
| 429 |
* Constructs an action called \a name with parent \a parent. It also stores a pointer |
| 430 |
* to the command object. |
| 431 |
*/ |
| 432 |
ActionGroup::ActionGroup(Command* pcCmd, QObject* parent) |
| 433 |
: Action(pcCmd, parent) |
| 434 |
, _group(nullptr) |
| 435 |
, _dropDown(false) |
| 436 |
, _isMode(false) |
| 437 |
, _rememberLast(true) |
| 438 |
{ |
| 439 |
_group = new QActionGroup(this); |
| 440 |
connect(_group, &QActionGroup::triggered, this, qOverload<QAction*>(&ActionGroup::onActivated)); |
| 441 |
connect(_group, &QActionGroup::hovered, this, &ActionGroup::onHovered); |
| 442 |
} |
| 443 |
|
| 444 |
ActionGroup::~ActionGroup() |
| 445 |
{ |
| 446 |
delete _group; |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* Adds this action to widget \a w. |
| 451 |
*/ |
| 452 |
void ActionGroup::addTo(QWidget* widget) |
| 453 |
{ |
| 454 |
// When adding an action that has defined a menu then shortcuts |
| 455 |
// of the menu actions don't work. To make this working we must |
| 456 |
// set the menu explicitly. This means calling QAction::setMenu() |
| 457 |
// and adding this action to the widget doesn't work. |
| 458 |
if (_dropDown) { |
| 459 |
if (widget->inherits("QMenu")) { |
| 460 |
auto menu = new QMenu(widget); |
| 461 |
QAction* item = qobject_cast<QMenu*>(widget)->addMenu(menu); |
| 462 |
item->setMenuRole(action()->menuRole()); |
| 463 |
menu->setTitle(action()->text()); |
| 464 |
menu->addActions(groupAction()->actions()); |
| 465 |
|
| 466 |
QObject::connect(menu, &QMenu::aboutToShow, [this, menu]() { Q_EMIT aboutToShow(menu); }); |
| 467 |
|
| 468 |
QObject::connect(menu, &QMenu::aboutToHide, [this, menu]() { Q_EMIT aboutToHide(menu); }); |
| 469 |
} |
| 470 |
else if (widget->inherits("QToolBar")) { |
| 471 |
widget->addAction(action()); |
| 472 |
QToolButton* tb = widget->findChildren<QToolButton*>().constLast(); |
| 473 |
tb->setPopupMode(QToolButton::MenuButtonPopup); |
| 474 |
tb->setObjectName(QStringLiteral("qt_toolbutton_menubutton")); |
| 475 |
QList<QAction*> acts = groupAction()->actions(); |
| 476 |
auto menu = new QMenu(tb); |
| 477 |
menu->addActions(acts); |
| 478 |
tb->setMenu(menu); |
| 479 |
|
| 480 |
QObject::connect(menu, &QMenu::aboutToShow, [this, menu]() { Q_EMIT aboutToShow(menu); }); |
| 481 |
|
| 482 |
QObject::connect(menu, &QMenu::aboutToHide, [this, menu]() { Q_EMIT aboutToHide(menu); }); |
| 483 |
} |
| 484 |
else { |
| 485 |
widget->addActions(groupAction()->actions()); // no drop-down |
| 486 |
} |
| 487 |
} |
| 488 |
else { |
| 489 |
widget->addActions(groupAction()->actions()); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
void ActionGroup::setEnabled(bool check) |
| 494 |
{ |
| 495 |
Action::setEnabled(check); |
| 496 |
groupAction()->setEnabled(check); |
| 497 |
} |
| 498 |
|
| 499 |
void ActionGroup::setDisabled(bool check) |
| 500 |
{ |
| 501 |
Action::setEnabled(!check); |
| 502 |
groupAction()->setDisabled(check); |
| 503 |
} |
| 504 |
|
| 505 |
void ActionGroup::setExclusive(bool check) |
| 506 |
{ |
| 507 |
groupAction()->setExclusive(check); |
| 508 |
} |
| 509 |
|
| 510 |
bool ActionGroup::isExclusive() const |
| 511 |
{ |
| 512 |
return groupAction()->isExclusive(); |
| 513 |
} |
| 514 |
|
| 515 |
void ActionGroup::setVisible(bool check) |
| 516 |
{ |
| 517 |
Action::setVisible(check); |
| 518 |
groupAction()->setVisible(check); |
| 519 |
} |
| 520 |
|
| 521 |
void ActionGroup::setRememberLast(bool remember) |
| 522 |
{ |
| 523 |
_rememberLast = remember; |
| 524 |
} |
| 525 |
|
| 526 |
bool ActionGroup::doesRememberLast() const |
| 527 |
{ |
| 528 |
return _rememberLast; |
| 529 |
} |
| 530 |
|
| 531 |
QAction* ActionGroup::addAction(QAction* action) |
| 532 |
{ |
| 533 |
return groupAction()->addAction(action); |
| 534 |
} |
| 535 |
|
| 536 |
QAction* ActionGroup::addAction(const QString& text) |
| 537 |
{ |
| 538 |
return groupAction()->addAction(text); |
| 539 |
} |
| 540 |
|
| 541 |
QList<QAction*> ActionGroup::actions() const |
| 542 |
{ |
| 543 |
return groupAction()->actions(); |
| 544 |
} |
| 545 |
|
| 546 |
int ActionGroup::checkedAction() const |
| 547 |
{ |
| 548 |
auto checked = groupAction()->checkedAction(); |
| 549 |
|
| 550 |
return actions().indexOf(checked); |
| 551 |
} |
| 552 |
|
| 553 |
void ActionGroup::setCheckedAction(int index) |
| 554 |
{ |
| 555 |
auto acts = groupAction()->actions(); |
| 556 |
QAction* act = acts.at(index); |
| 557 |
act->setChecked(true); |
| 558 |
this->setIcon(act->icon()); |
| 559 |
|
| 560 |
if (!this->_isMode) { |
| 561 |
this->action()->setToolTip(act->toolTip()); |
| 562 |
} |
| 563 |
this->setProperty("defaultAction", QVariant(index)); |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* Activates the command. |
| 568 |
*/ |
| 569 |
void ActionGroup::onActivated() |
| 570 |
{ |
| 571 |
command()->invoke(this->property("defaultAction").toInt(), Command::TriggerAction); |
| 572 |
} |
| 573 |
|
| 574 |
void ActionGroup::onToggled(bool check) |
| 575 |
{ |
| 576 |
Q_UNUSED(check) |
| 577 |
onActivated(); |
| 578 |
} |
| 579 |
|
| 580 |
/** |
| 581 |
* Activates the command. |
| 582 |
*/ |
| 583 |
void ActionGroup::onActivated(QAction* act) |
| 584 |
{ |
| 585 |
if (_rememberLast) { |
| 586 |
int index = groupAction()->actions().indexOf(act); |
| 587 |
|
| 588 |
this->setIcon(act->icon()); |
| 589 |
if (!this->_isMode) { |
| 590 |
this->action()->setToolTip(act->toolTip()); |
| 591 |
} |
| 592 |
this->setProperty("defaultAction", QVariant(index)); |
| 593 |
command()->invoke(index, Command::TriggerChildAction); |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
* Shows tooltip at the right side when hovered. |
| 599 |
*/ |
| 600 |
void ActionGroup::onHovered(QAction* act) |
| 601 |
{ |
| 602 |
const auto topLevelWidgets = QApplication::topLevelWidgets(); |
| 603 |
QMenu* foundMenu = nullptr; |
| 604 |
|
| 605 |
for (QWidget* widget : topLevelWidgets) { |
| 606 |
QList<QMenu*> menus = widget->findChildren<QMenu*>(); |
| 607 |
|
| 608 |
for (QMenu* menu : menus) { |
| 609 |
if (menu->isVisible() && menu->actions().contains(act)) { |
| 610 |
foundMenu = menu; |
| 611 |
break; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
if (foundMenu) { |
| 616 |
break; |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
if (foundMenu) { |
| 621 |
QRect actionRect = foundMenu->actionGeometry(act); |
| 622 |
QPoint globalPos = foundMenu->mapToGlobal(actionRect.topRight()); |
| 623 |
QToolTip::showText(globalPos, act->toolTip(), foundMenu, actionRect); |
| 624 |
} |
| 625 |
else { |
| 626 |
QToolTip::showText(QCursor::pos(), act->toolTip()); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
/* TRANSLATOR Gui::WorkbenchGroup */ |
| 632 |
WorkbenchGroup::WorkbenchGroup(Command* pcCmd, QObject* parent) |
| 633 |
: ActionGroup(pcCmd, parent) |
| 634 |
{ |
| 635 |
refreshWorkbenchList(); |
| 636 |
|
| 637 |
// NOLINTBEGIN |
| 638 |
Application::Instance->signalRefreshWorkbenches.connect( |
| 639 |
std::bind(&WorkbenchGroup::refreshWorkbenchList, this) |
| 640 |
); |
| 641 |
// NOLINTEND |
| 642 |
|
| 643 |
connect(getMainWindow(), &MainWindow::workbenchActivated, this, &WorkbenchGroup::onWorkbenchActivated); |
| 644 |
} |
| 645 |
|
| 646 |
QAction* WorkbenchGroup::getOrCreateAction(const QString& wbName) |
| 647 |
{ |
| 648 |
if (!actionByWorkbenchName.contains(wbName)) { |
| 649 |
actionByWorkbenchName[wbName] = new QAction(QApplication::instance()); |
| 650 |
} |
| 651 |
|
| 652 |
return actionByWorkbenchName[wbName]; |
| 653 |
} |
| 654 |
|
| 655 |
void WorkbenchGroup::addTo(QWidget* widget) |
| 656 |
{ |
| 657 |
if (widget->inherits("QToolBar")) { |
| 658 |
ParameterGrp::handle hGrp; |
| 659 |
hGrp = App::GetApplication().GetParameterGroupByPath( |
| 660 |
"User parameter:BaseApp/Preferences/Workbenches" |
| 661 |
); |
| 662 |
|
| 663 |
QWidget* workbenchSelectorWidget; |
| 664 |
if (hGrp->GetInt("WorkbenchSelectorType", 0) == 0) { |
| 665 |
workbenchSelectorWidget = new WorkbenchComboBox(this, widget); |
| 666 |
} |
| 667 |
else { |
| 668 |
workbenchSelectorWidget = new WorkbenchTabWidget(this, widget); |
| 669 |
} |
| 670 |
|
| 671 |
static_cast<QToolBar*>(widget)->addWidget(workbenchSelectorWidget); |
| 672 |
} |
| 673 |
else if (widget->inherits("QMenu")) { |
| 674 |
auto menu = qobject_cast<QMenu*>(widget); |
| 675 |
menu = menu->addMenu(action()->text()); |
| 676 |
menu->addActions(getEnabledWbActions()); |
| 677 |
|
| 678 |
connect(this, &WorkbenchGroup::workbenchListRefreshed, this, [menu](QList<QAction*> actions) { |
| 679 |
menu->clear(); |
| 680 |
menu->addActions(actions); |
| 681 |
}); |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
void WorkbenchGroup::refreshWorkbenchList() |
| 686 |
{ |
| 687 |
QStringList enabledWbNames = DlgSettingsWorkbenchesImp::getEnabledWorkbenches(); |
| 688 |
|
| 689 |
// Clear the actions. |
| 690 |
for (QAction* action : actions()) { |
| 691 |
groupAction()->removeAction(action); |
| 692 |
} |
| 693 |
|
| 694 |
enabledWbsActions.clear(); |
| 695 |
disabledWbsActions.clear(); |
| 696 |
|
| 697 |
std::string activeWbName = WorkbenchManager::instance()->activeName(); |
| 698 |
|
| 699 |
// Create action list of enabled wb |
| 700 |
int index = 0; |
| 701 |
for (const auto& wbName : enabledWbNames) { |
| 702 |
QString name = Application::Instance->workbenchMenuText(wbName); |
| 703 |
QPixmap px = Application::Instance->workbenchIcon(wbName); |
| 704 |
QString tip = Application::Instance->workbenchToolTip(wbName); |
| 705 |
|
| 706 |
QAction* action = getOrCreateAction(wbName); |
| 707 |
|
| 708 |
groupAction()->addAction(action); |
| 709 |
|
| 710 |
action->setText(name); |
| 711 |
action->setCheckable(true); |
| 712 |
action->setData(QVariant(index)); // set the index |
| 713 |
action->setObjectName(wbName); |
| 714 |
action->setIcon(px); |
| 715 |
action->setToolTip(tip); |
| 716 |
action->setStatusTip(tr("Selects the '%1' workbench").arg(name)); |
| 717 |
if (index < 9) { |
| 718 |
action->setShortcut(QKeySequence(QStringLiteral("W,%1").arg(index + 1))); |
| 719 |
} |
| 720 |
if (wbName.toStdString() == activeWbName) { |
| 721 |
action->setChecked(true); |
| 722 |
} |
| 723 |
enabledWbsActions.push_back(action); |
| 724 |
index++; |
| 725 |
} |
| 726 |
|
| 727 |
// Also create action list of disabled wbs |
| 728 |
QStringList disabledWbNames = DlgSettingsWorkbenchesImp::getDisabledWorkbenches(); |
| 729 |
for (const auto& wbName : disabledWbNames) { |
| 730 |
QString name = Application::Instance->workbenchMenuText(wbName); |
| 731 |
QPixmap px = Application::Instance->workbenchIcon(wbName); |
| 732 |
QString tip = Application::Instance->workbenchToolTip(wbName); |
| 733 |
|
| 734 |
QAction* action = getOrCreateAction(wbName); |
| 735 |
|
| 736 |
groupAction()->addAction(action); |
| 737 |
|
| 738 |
action->setText(name); |
| 739 |
action->setCheckable(true); |
| 740 |
action->setData(QVariant(index)); // set the index |
| 741 |
action->setObjectName(wbName); |
| 742 |
action->setIcon(px); |
| 743 |
action->setToolTip(tip); |
| 744 |
action->setStatusTip(tr("Select the '%1' workbench").arg(name)); |
| 745 |
if (wbName.toStdString() == activeWbName) { |
| 746 |
action->setChecked(true); |
| 747 |
} |
| 748 |
disabledWbsActions.push_back(action); |
| 749 |
index++; |
| 750 |
} |
| 751 |
|
| 752 |
// Signal to the widgets (WorkbenchComboBox & menu) to update the wb list |
| 753 |
workbenchListRefreshed(enabledWbsActions); |
| 754 |
} |
| 755 |
|
| 756 |
void WorkbenchGroup::onWorkbenchActivated(const QString& name) |
| 757 |
{ |
| 758 |
// There might be more than only one instance of WorkbenchComboBox there. |
| 759 |
// However, all of them share the same QAction objects. Thus, if the user |
| 760 |
// has selected one it also gets checked. Then Application::activateWorkbench |
| 761 |
// also invokes this slot method by calling the signal workbenchActivated in |
| 762 |
// MainWindow. If calling activateWorkbench() from within the Python console |
| 763 |
// the matching action must be set by calling this function. |
| 764 |
// To avoid to recursively (but only one recursion level) call Application:: |
| 765 |
// activateWorkbench the method refreshWorkbenchList() shouldn't set the |
| 766 |
// checked item. |
| 767 |
// QVariant item = itemData(currentIndex()); |
| 768 |
|
| 769 |
for (QAction* action : actions()) { |
| 770 |
if (action->objectName() == name) { |
| 771 |
if (!action->isChecked()) { |
| 772 |
action->trigger(); |
| 773 |
} |
| 774 |
break; |
| 775 |
} |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
QList<QAction*> WorkbenchGroup::getEnabledWbActions() const |
| 780 |
{ |
| 781 |
return enabledWbsActions; |
| 782 |
} |
| 783 |
|
| 784 |
QList<QAction*> WorkbenchGroup::getDisabledWbActions() const |
| 785 |
{ |
| 786 |
return disabledWbsActions; |
| 787 |
} |
| 788 |
|
| 789 |
// -------------------------------------------------------------------- |
| 790 |
|
| 791 |
class RecentFilesAction::Private: public ParameterGrp::ObserverType |
| 792 |
{ |
| 793 |
public: |
| 794 |
Private(const Private&) = delete; |
| 795 |
Private(Private&&) = delete; |
| 796 |
void operator=(const Private&) = delete; |
| 797 |
void operator=(Private&&) = delete; |
| 798 |
|
| 799 |
Private(RecentFilesAction* master, const char* path) |
| 800 |
: master(master) |
| 801 |
{ |
| 802 |
handle = App::GetApplication().GetParameterGroupByPath(path); |
| 803 |
handle->Attach(this); |
| 804 |
} |
| 805 |
|
| 806 |
~Private() override |
| 807 |
{ |
| 808 |
handle->Detach(this); |
| 809 |
} |
| 810 |
|
| 811 |
void OnChange(Base::Subject<const char*>& sub, const char* reason) override |
| 812 |
{ |
| 813 |
Q_UNUSED(sub) |
| 814 |
if (!updating && reason && strcmp(reason, "RecentFiles") == 0) { |
| 815 |
Base::StateLocker guard(updating); |
| 816 |
master->restore(); |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
void trySaveUserParameter() |
| 821 |
{ |
| 822 |
// update the XML structure and save the user parameter to disk (#0001989) |
| 823 |
bool saveParameter = App::GetApplication() |
| 824 |
.GetParameterGroupByPath("User parameter:BaseApp/Preferences/General") |
| 825 |
->GetBool("SaveUserParameter", true); |
| 826 |
if (saveParameter) { |
| 827 |
saveUserParameter(); |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
void saveUserParameter() |
| 832 |
{ |
| 833 |
ParameterManager* parmgr = App::GetApplication().GetParameterSet("User parameter"); |
| 834 |
parmgr->SaveDocument(App::Application::Config()["UserParameter"].c_str()); |
| 835 |
} |
| 836 |
|
| 837 |
public: |
| 838 |
RecentFilesAction* master; |
| 839 |
ParameterGrp::handle handle; |
| 840 |
bool updating = false; |
| 841 |
}; |
| 842 |
|
| 843 |
// -------------------------------------------------------------------- |
| 844 |
|
| 845 |
/* TRANSLATOR Gui::RecentFilesAction */ |
| 846 |
|
| 847 |
RecentFilesAction::RecentFilesAction(Command* pcCmd, QObject* parent) |
| 848 |
: ActionGroup(pcCmd, parent) |
| 849 |
, visibleItems(4) |
| 850 |
, maximumItems(20) |
| 851 |
{ |
| 852 |
_pimpl = std::make_unique<Private>(this, "User parameter:BaseApp/Preferences/RecentFiles"); |
| 853 |
restore(); |
| 854 |
|
| 855 |
sep.setSeparator(true); |
| 856 |
sep.setToolTip({}); |
| 857 |
this->groupAction()->addAction(&sep); |
| 858 |
|
| 859 |
//: Empties the list of recent files |
| 860 |
clearRecentFilesListAction.setText(tr("Clear Recent Files")); |
| 861 |
clearRecentFilesListAction.setToolTip({}); |
| 862 |
this->groupAction()->addAction(&clearRecentFilesListAction); |
| 863 |
|
| 864 |
auto clearFun = [this, hGrp = _pimpl->handle]() { |
| 865 |
const size_t recentFilesListSize = hGrp->GetASCIIs("MRU").size(); |
| 866 |
for (size_t i = 0; i < recentFilesListSize; i++) { |
| 867 |
const QByteArray key = QStringLiteral("MRU%1").arg(i).toLocal8Bit(); |
| 868 |
hGrp->SetASCII(key.data(), ""); |
| 869 |
} |
| 870 |
restore(); |
| 871 |
clearRecentFilesListAction.setEnabled(false); |
| 872 |
}; |
| 873 |
|
| 874 |
connect(&clearRecentFilesListAction, &QAction::triggered, this, clearFun); |
| 875 |
|
| 876 |
connect( |
| 877 |
&clearRecentFilesListAction, |
| 878 |
&QAction::triggered, |
| 879 |
this, |
| 880 |
&RecentFilesAction::recentFilesListModified |
| 881 |
); |
| 882 |
} |
| 883 |
|
| 884 |
RecentFilesAction::~RecentFilesAction() |
| 885 |
{ |
| 886 |
_pimpl.reset(nullptr); |
| 887 |
} |
| 888 |
|
| 889 |
/** Adds the new item to the recent files. */ |
| 890 |
void RecentFilesAction::appendFile(const QString& filename) |
| 891 |
{ |
| 892 |
// restore the list of recent files |
| 893 |
QStringList files = this->files(); |
| 894 |
|
| 895 |
// if already inside remove and prepend it |
| 896 |
files.removeAll(filename); |
| 897 |
files.prepend(filename); |
| 898 |
setFiles(files); |
| 899 |
save(); |
| 900 |
|
| 901 |
_pimpl->trySaveUserParameter(); |
| 902 |
|
| 903 |
clearRecentFilesListAction.setEnabled(true); |
| 904 |
|
| 905 |
Q_EMIT recentFilesListModified(); |
| 906 |
} |
| 907 |
|
| 908 |
static QString numberToLabel(int number) |
| 909 |
{ |
| 910 |
if (number > 0 && number < 10) { // NOLINT: *-magic-numbers |
| 911 |
return QStringLiteral("&%1").arg(number); |
| 912 |
} |
| 913 |
if (number == 10) { // NOLINT: *-magic-numbers |
| 914 |
return QStringLiteral("1&0"); |
| 915 |
} |
| 916 |
// If we have a number greater than 10, we start using the alphabet. |
| 917 |
// So 11 becomes 'A' and so on. |
| 918 |
constexpr char lettersStart = 11; |
| 919 |
constexpr char lettersEnd = lettersStart + ('Z' - 'A'); |
| 920 |
if (number >= lettersStart && number < lettersEnd) { |
| 921 |
QChar letter = QChar::fromLatin1('A' + (number - lettersStart)); |
| 922 |
return QStringLiteral("%1 (&%2)").arg(number).arg(letter); |
| 923 |
} |
| 924 |
// Not enough accelerators to cover this number. |
| 925 |
return QStringLiteral("%1").arg(number); |
| 926 |
} |
| 927 |
|
| 928 |
/** |
| 929 |
* Set the list of recent files. For each item an action object is |
| 930 |
* created and added to this action group. |
| 931 |
*/ |
| 932 |
void RecentFilesAction::setFiles(const QStringList& files) |
| 933 |
{ |
| 934 |
QList<QAction*> recentFiles = groupAction()->actions(); |
| 935 |
|
| 936 |
int numRecentFiles = std::min<int>(recentFiles.count(), files.count()); |
| 937 |
for (int index = 0; index < numRecentFiles; index++) { |
| 938 |
QString numberLabel = numberToLabel(index + 1); |
| 939 |
QFileInfo fi(files[index]); |
| 940 |
QString fileName {fi.fileName()}; |
| 941 |
fileName.replace(QLatin1Char('&'), QStringLiteral("&&")); |
| 942 |
recentFiles[index]->setText(QStringLiteral("%1 %2").arg(numberLabel, fileName)); |
| 943 |
recentFiles[index]->setStatusTip(tr("Open file %1").arg(files[index])); |
| 944 |
recentFiles[index]->setToolTip(files[index]); // set the full name that we need later for saving |
| 945 |
recentFiles[index]->setData(QVariant(index)); |
| 946 |
recentFiles[index]->setVisible(true); |
| 947 |
} |
| 948 |
|
| 949 |
// if less file names than actions |
| 950 |
numRecentFiles = std::min<int>(numRecentFiles, this->visibleItems); |
| 951 |
for (int index = numRecentFiles; index < recentFiles.count(); index++) { |
| 952 |
if (recentFiles[index] == &sep || recentFiles[index] == &clearRecentFilesListAction) { |
| 953 |
continue; |
| 954 |
} |
| 955 |
recentFiles[index]->setVisible(false); |
| 956 |
recentFiles[index]->setText(QString()); |
| 957 |
recentFiles[index]->setToolTip(QString()); |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
/** |
| 962 |
* Returns the list of defined recent files. |
| 963 |
*/ |
| 964 |
QStringList RecentFilesAction::files() const |
| 965 |
{ |
| 966 |
QStringList files; |
| 967 |
QList<QAction*> recentFiles = groupAction()->actions(); |
| 968 |
for (int index = 0; index < recentFiles.count(); index++) { |
| 969 |
QString file = recentFiles[index]->toolTip(); |
| 970 |
if (file.isEmpty()) { |
| 971 |
break; |
| 972 |
} |
| 973 |
files.append(file); |
| 974 |
} |
| 975 |
|
| 976 |
return files; |
| 977 |
} |
| 978 |
|
| 979 |
void RecentFilesAction::activateFile(int id) |
| 980 |
{ |
| 981 |
// restore the list of recent files |
| 982 |
QStringList files = this->files(); |
| 983 |
if (id < 0 || id >= files.count()) { |
| 984 |
return; // no valid item |
| 985 |
} |
| 986 |
|
| 987 |
QString filename = files[id]; |
| 988 |
if (!ModuleIO::verifyFile(filename)) { |
| 989 |
files.removeAll(filename); |
| 990 |
setFiles(files); |
| 991 |
save(); |
| 992 |
} |
| 993 |
else { |
| 994 |
ModuleIO::openFile(filename); |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
void RecentFilesAction::resizeList(int size) |
| 999 |
{ |
| 1000 |
this->visibleItems = size; |
| 1001 |
int diff = this->visibleItems - this->maximumItems; |
| 1002 |
// create new items if needed |
| 1003 |
for (int i = 0; i < diff; i++) { |
| 1004 |
groupAction()->addAction(QLatin1String(""))->setVisible(false); |
| 1005 |
} |
| 1006 |
setFiles(files()); |
| 1007 |
} |
| 1008 |
|
| 1009 |
/** Loads all recent files from the preferences. */ |
| 1010 |
void RecentFilesAction::restore() |
| 1011 |
{ |
| 1012 |
ParameterGrp::handle hGrp = _pimpl->handle; |
| 1013 |
// we want at least 20 items but we do only show the number of files |
| 1014 |
// that is defined in user parameters |
| 1015 |
this->visibleItems = hGrp->GetInt("RecentFiles", this->visibleItems); |
| 1016 |
|
| 1017 |
int count = std::max<int>(this->maximumItems, this->visibleItems); |
| 1018 |
for (int i = 0; i < count; i++) { |
| 1019 |
groupAction()->addAction(QLatin1String(""))->setVisible(false); |
| 1020 |
} |
| 1021 |
std::vector<std::string> MRU = hGrp->GetASCIIs("MRU"); |
| 1022 |
QStringList files; |
| 1023 |
for (const auto& it : MRU) { |
| 1024 |
auto filePath = QString::fromUtf8(it.c_str()); |
| 1025 |
if (QFileInfo::exists(filePath)) { |
| 1026 |
files.append(filePath); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
setFiles(files); |
| 1030 |
} |
| 1031 |
|
| 1032 |
/** Saves all recent files to the preferences. */ |
| 1033 |
void RecentFilesAction::save() |
| 1034 |
{ |
| 1035 |
ParameterGrp::handle hGrp = _pimpl->handle; |
| 1036 |
int count = hGrp->GetInt("RecentFiles", this->visibleItems); // save number of files |
| 1037 |
hGrp->Clear(); |
| 1038 |
|
| 1039 |
// count all set items |
| 1040 |
QList<QAction*> recentFiles = groupAction()->actions(); |
| 1041 |
int num = std::min<int>(count, recentFiles.count()); |
| 1042 |
for (int index = 0; index < num; index++) { |
| 1043 |
QString key = QStringLiteral("MRU%1").arg(index); |
| 1044 |
QString value = recentFiles[index]->toolTip(); |
| 1045 |
if (value.isEmpty()) { |
| 1046 |
break; |
| 1047 |
} |
| 1048 |
hGrp->SetASCII(key.toLatin1(), value.toUtf8()); |
| 1049 |
} |
| 1050 |
|
| 1051 |
Base::StateLocker guard(_pimpl->updating); |
| 1052 |
hGrp->SetInt("RecentFiles", count); // restore |
| 1053 |
} |
| 1054 |
|
| 1055 |
// -------------------------------------------------------------------- |
| 1056 |
|
| 1057 |
/* TRANSLATOR Gui::RecentMacrosAction */ |
| 1058 |
|
| 1059 |
RecentMacrosAction::RecentMacrosAction(Command* pcCmd, QObject* parent) |
| 1060 |
: ActionGroup(pcCmd, parent) |
| 1061 |
, visibleItems(4) |
| 1062 |
, maximumItems(20) |
| 1063 |
{ |
| 1064 |
restore(); |
| 1065 |
} |
| 1066 |
|
| 1067 |
/** Adds the new item to the recent files. */ |
| 1068 |
void RecentMacrosAction::appendFile(const QString& filename) |
| 1069 |
{ |
| 1070 |
// restore the list of recent files |
| 1071 |
QStringList files = this->files(); |
| 1072 |
|
| 1073 |
// if already inside remove and prepend it |
| 1074 |
files.removeAll(filename); |
| 1075 |
files.prepend(filename); |
| 1076 |
setFiles(files); |
| 1077 |
save(); |
| 1078 |
|
| 1079 |
// update the XML structure and save the user parameter to disk (#0001989) |
| 1080 |
bool saveParameter = App::GetApplication() |
| 1081 |
.GetParameterGroupByPath("User parameter:BaseApp/Preferences/General") |
| 1082 |
->GetBool("SaveUserParameter", true); |
| 1083 |
if (saveParameter) { |
| 1084 |
ParameterManager* parmgr = App::GetApplication().GetParameterSet("User parameter"); |
| 1085 |
parmgr->SaveDocument(App::Application::Config()["UserParameter"].c_str()); |
| 1086 |
} |
| 1087 |
} |
| 1088 |
|
| 1089 |
/** |
| 1090 |
* Set the list of recent macro files. For each item an action object is |
| 1091 |
* created and added to this action group. |
| 1092 |
*/ |
| 1093 |
void RecentMacrosAction::setFiles(const QStringList& files) |
| 1094 |
{ |
| 1095 |
ParameterGrp::handle hGrp = App::GetApplication() |
| 1096 |
.GetUserParameter() |
| 1097 |
.GetGroup("BaseApp") |
| 1098 |
->GetGroup("Preferences") |
| 1099 |
->GetGroup("RecentMacros"); |
| 1100 |
this->shortcut_modifiers = hGrp->GetASCII("ShortcutModifiers", "Ctrl+Shift+"); |
| 1101 |
this->shortcut_count = std::min<int>(hGrp->GetInt("ShortcutCount", 3), 9); // max = 9, e.g. |
| 1102 |
// Ctrl+Shift+9 |
| 1103 |
this->visibleItems = hGrp->GetInt("RecentMacros", 12); |
| 1104 |
QList<QAction*> recentFiles = groupAction()->actions(); |
| 1105 |
|
| 1106 |
int numRecentFiles = std::min<int>(recentFiles.count(), files.count()); |
| 1107 |
QStringList existingCommands; |
| 1108 |
auto accel_col = QString::fromStdString(shortcut_modifiers); |
| 1109 |
for (int index = 0; index < numRecentFiles; index++) { |
| 1110 |
QFileInfo fi(files[index]); |
| 1111 |
QString numberLabel = numberToLabel(index + 1); |
| 1112 |
recentFiles[index]->setText( |
| 1113 |
QStringLiteral("%1 %2").arg(numberLabel).arg(fi.completeBaseName()) |
| 1114 |
); |
| 1115 |
recentFiles[index]->setToolTip(files[index]); // set the full name that we need later for saving |
| 1116 |
recentFiles[index]->setData(QVariant(index)); |
| 1117 |
QString accel(tr("none")); |
| 1118 |
if (index < shortcut_count) { |
| 1119 |
auto accel_tmp = QString::fromStdString(shortcut_modifiers); |
| 1120 |
accel_tmp.append(QString::number(index + 1, 10)).toStdString(); |
| 1121 |
auto check = Application::Instance->commandManager().checkAcceleratorForConflicts( |
| 1122 |
qPrintable(accel_tmp) |
| 1123 |
); |
| 1124 |
if (check) { |
| 1125 |
recentFiles[index]->setShortcut(QKeySequence()); |
| 1126 |
accel_col.append(accel_tmp); |
| 1127 |
existingCommands.append(QLatin1String(check->getName())); |
| 1128 |
} |
| 1129 |
else { |
| 1130 |
accel = accel_tmp; |
| 1131 |
recentFiles[index]->setShortcut(accel); |
| 1132 |
} |
| 1133 |
} |
| 1134 |
recentFiles[index]->setStatusTip( |
| 1135 |
tr("Run macro %1 (Shift+click to edit) keyboard shortcut: %2").arg(files[index], accel) |
| 1136 |
); |
| 1137 |
recentFiles[index]->setVisible(true); |
| 1138 |
} |
| 1139 |
|
| 1140 |
// if less file names than actions |
| 1141 |
numRecentFiles = std::min<int>(numRecentFiles, this->visibleItems); |
| 1142 |
for (int index = numRecentFiles; index < recentFiles.count(); index++) { |
| 1143 |
recentFiles[index]->setVisible(false); |
| 1144 |
recentFiles[index]->setText(QString()); |
| 1145 |
recentFiles[index]->setToolTip(QString()); |
| 1146 |
} |
| 1147 |
// Raise a single warning no matter how many conflicts |
| 1148 |
if (!existingCommands.isEmpty()) { |
| 1149 |
auto msgMain = QStringLiteral("Recent macros : keyboard shortcuts"); |
| 1150 |
for (int index = 0; index < accel_col.size(); index++) { |
| 1151 |
msgMain += QStringLiteral(" %1").arg(accel_col[index]); |
| 1152 |
} |
| 1153 |
msgMain += QStringLiteral(" disabled because of conflicts with"); |
| 1154 |
for (int index = 0; index < existingCommands.count(); index++) { |
| 1155 |
msgMain += QStringLiteral(" %1").arg(existingCommands[index]); |
| 1156 |
} |
| 1157 |
msgMain += QStringLiteral( |
| 1158 |
" respectively.\nHint: In Preferences → Python → Macro →" |
| 1159 |
" Recent Macros menu → Keyboard Modifiers this should be Ctrl+Shift+" |
| 1160 |
" by default, if this is now blank then you should revert it back to" |
| 1161 |
" Ctrl+Shift+ by pressing both keys at the same time." |
| 1162 |
); |
| 1163 |
Base::Console().warning("%s\n", qPrintable(msgMain)); |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
/** |
| 1168 |
* Returns the list of defined recent files. |
| 1169 |
*/ |
| 1170 |
QStringList RecentMacrosAction::files() const |
| 1171 |
{ |
| 1172 |
QStringList files; |
| 1173 |
QList<QAction*> recentFiles = groupAction()->actions(); |
| 1174 |
for (int index = 0; index < recentFiles.count(); index++) { |
| 1175 |
QString file = recentFiles[index]->toolTip(); |
| 1176 |
if (file.isEmpty()) { |
| 1177 |
break; |
| 1178 |
} |
| 1179 |
files.append(file); |
| 1180 |
} |
| 1181 |
|
| 1182 |
return files; |
| 1183 |
} |
| 1184 |
|
| 1185 |
void RecentMacrosAction::activateFile(int id) |
| 1186 |
{ |
| 1187 |
// restore the list of recent files |
| 1188 |
QStringList files = this->files(); |
| 1189 |
if (id < 0 || id >= files.count()) { |
| 1190 |
return; // no valid item |
| 1191 |
} |
| 1192 |
|
| 1193 |
QString filename = files[id]; |
| 1194 |
QFileInfo fi(filename); |
| 1195 |
if (!ModuleIO::verifyFile(filename)) { |
| 1196 |
files.removeAll(filename); |
| 1197 |
setFiles(files); |
| 1198 |
} |
| 1199 |
else { |
| 1200 |
if (QApplication::keyboardModifiers() |
| 1201 |
== Qt::ShiftModifier) { // open for editing on Shift+click |
| 1202 |
auto editor = new PythonEditor(); |
| 1203 |
editor->setWindowIcon(Gui::BitmapFactory().iconFromTheme("applications-python")); |
| 1204 |
auto edit = new PythonEditorView(editor, getMainWindow()); |
| 1205 |
edit->setDisplayName(PythonEditorView::FileName); |
| 1206 |
edit->open(filename); |
| 1207 |
edit->resize(400, 300); |
| 1208 |
getMainWindow()->addWindow(edit); |
| 1209 |
getMainWindow()->appendRecentMacro(filename); |
| 1210 |
edit->setWindowTitle(fi.fileName()); |
| 1211 |
} |
| 1212 |
else { // execute macro on normal (non-shifted) click |
| 1213 |
try { |
| 1214 |
getMainWindow()->appendRecentMacro(fi.filePath()); |
| 1215 |
Application::Instance->macroManager()->run( |
| 1216 |
Gui::MacroManager::File, |
| 1217 |
fi.filePath().toUtf8() |
| 1218 |
); |
| 1219 |
// after macro run recalculate the document |
| 1220 |
if (Application::Instance->activeDocument()) { |
| 1221 |
Application::Instance->activeDocument()->getDocument()->recompute(); |
| 1222 |
} |
| 1223 |
} |
| 1224 |
catch (const Base::SystemExitException&) { |
| 1225 |
// handle SystemExit exceptions |
| 1226 |
Base::PyGILStateLocker locker; |
| 1227 |
Base::PyException exc; |
| 1228 |
exc.reportException(); |
| 1229 |
} |
| 1230 |
} |
| 1231 |
} |
| 1232 |
} |
| 1233 |
|
| 1234 |
void RecentMacrosAction::resizeList(int size) |
| 1235 |
{ |
| 1236 |
this->visibleItems = size; |
| 1237 |
int diff = this->visibleItems - this->maximumItems; |
| 1238 |
// create new items if needed |
| 1239 |
for (int i = 0; i < diff; i++) { |
| 1240 |
groupAction()->addAction(QLatin1String(""))->setVisible(false); |
| 1241 |
} |
| 1242 |
setFiles(files()); |
| 1243 |
} |
| 1244 |
|
| 1245 |
/** Loads all recent files from the preferences. */ |
| 1246 |
void RecentMacrosAction::restore() |
| 1247 |
{ |
| 1248 |
ParameterGrp::handle hGrp = App::GetApplication() |
| 1249 |
.GetUserParameter() |
| 1250 |
.GetGroup("BaseApp") |
| 1251 |
->GetGroup("Preferences") |
| 1252 |
->GetGroup("RecentMacros"); |
| 1253 |
|
| 1254 |
for (int i = groupAction()->actions().size(); i < this->maximumItems; i++) { |
| 1255 |
groupAction()->addAction(QLatin1String(""))->setVisible(false); |
| 1256 |
} |
| 1257 |
resizeList(hGrp->GetInt("RecentMacros")); |
| 1258 |
|
| 1259 |
std::vector<std::string> MRU = hGrp->GetASCIIs("MRU"); |
| 1260 |
QStringList files; |
| 1261 |
for (auto& filename : MRU) { |
| 1262 |
files.append(QString::fromUtf8(filename.c_str())); |
| 1263 |
} |
| 1264 |
setFiles(files); |
| 1265 |
} |
| 1266 |
|
| 1267 |
/** Saves all recent files to the preferences. */ |
| 1268 |
void RecentMacrosAction::save() |
| 1269 |
{ |
| 1270 |
ParameterGrp::handle hGrp = App::GetApplication() |
| 1271 |
.GetUserParameter() |
| 1272 |
.GetGroup("BaseApp") |
| 1273 |
->GetGroup("Preferences") |
| 1274 |
->GetGroup("RecentMacros"); |
| 1275 |
int count = hGrp->GetInt("RecentMacros", this->visibleItems); // save number of files |
| 1276 |
hGrp->Clear(); |
| 1277 |
|
| 1278 |
// count all set items |
| 1279 |
QList<QAction*> recentFiles = groupAction()->actions(); |
| 1280 |
int num = std::min<int>(count, recentFiles.count()); |
| 1281 |
for (int index = 0; index < num; index++) { |
| 1282 |
QString key = QStringLiteral("MRU%1").arg(index); |
| 1283 |
QString value = recentFiles[index]->toolTip(); |
| 1284 |
if (value.isEmpty()) { |
| 1285 |
break; |
| 1286 |
} |
| 1287 |
hGrp->SetASCII(key.toLatin1(), value.toUtf8()); |
| 1288 |
} |
| 1289 |
|
| 1290 |
hGrp->SetInt("RecentMacros", count); // restore |
| 1291 |
hGrp->SetInt("ShortcutCount", this->shortcut_count); |
| 1292 |
hGrp->SetASCII("ShortcutModifiers", this->shortcut_modifiers.c_str()); |
| 1293 |
} |
| 1294 |
|
| 1295 |
// -------------------------------------------------------------------- |
| 1296 |
|
| 1297 |
UndoAction::UndoAction(Command* pcCmd, QObject* parent) |
| 1298 |
: Action(pcCmd, parent) |
| 1299 |
{ |
| 1300 |
_toolAction = new QAction(this); |
| 1301 |
_toolAction->setMenu(new UndoDialog()); |
| 1302 |
connect(_toolAction, &QAction::triggered, this, &UndoAction::onActivated); |
| 1303 |
} |
| 1304 |
|
| 1305 |
UndoAction::~UndoAction() |
| 1306 |
{ |
| 1307 |
QMenu* menu = _toolAction->menu(); |
| 1308 |
delete menu; |
| 1309 |
delete _toolAction; |
| 1310 |
} |
| 1311 |
|
| 1312 |
void UndoAction::addTo(QWidget* widget) |
| 1313 |
{ |
| 1314 |
if (widget->inherits("QToolBar")) { |
| 1315 |
actionChanged(); |
| 1316 |
connect(action(), &QAction::changed, this, &UndoAction::actionChanged); |
| 1317 |
widget->addAction(_toolAction); |
| 1318 |
} |
| 1319 |
else { |
| 1320 |
widget->addAction(action()); |
| 1321 |
} |
| 1322 |
} |
| 1323 |
|
| 1324 |
void UndoAction::actionChanged() |
| 1325 |
{ |
| 1326 |
// Do NOT set the shortcut again for _toolAction since this is already |
| 1327 |
// reserved for _action. Otherwise we get an ambiguity of it with the |
| 1328 |
// result that it doesn't work anymore. |
| 1329 |
_toolAction->setText(action()->text()); |
| 1330 |
_toolAction->setToolTip(action()->toolTip()); |
| 1331 |
_toolAction->setStatusTip(action()->statusTip()); |
| 1332 |
_toolAction->setWhatsThis(action()->whatsThis()); |
| 1333 |
_toolAction->setIcon(action()->icon()); |
| 1334 |
} |
| 1335 |
|
| 1336 |
void UndoAction::setEnabled(bool check) |
| 1337 |
{ |
| 1338 |
Action::setEnabled(check); |
| 1339 |
_toolAction->setEnabled(check); |
| 1340 |
} |
| 1341 |
|
| 1342 |
void UndoAction::setVisible(bool check) |
| 1343 |
{ |
| 1344 |
Action::setVisible(check); |
| 1345 |
_toolAction->setVisible(check); |
| 1346 |
} |
| 1347 |
|
| 1348 |
// -------------------------------------------------------------------- |
| 1349 |
|
| 1350 |
RedoAction::RedoAction(Command* pcCmd, QObject* parent) |
| 1351 |
: Action(pcCmd, parent) |
| 1352 |
{ |
| 1353 |
_toolAction = new QAction(this); |
| 1354 |
_toolAction->setMenu(new RedoDialog()); |
| 1355 |
connect(_toolAction, &QAction::triggered, this, &RedoAction::onActivated); |
| 1356 |
} |
| 1357 |
|
| 1358 |
RedoAction::~RedoAction() |
| 1359 |
{ |
| 1360 |
QMenu* menu = _toolAction->menu(); |
| 1361 |
delete menu; |
| 1362 |
delete _toolAction; |
| 1363 |
} |
| 1364 |
|
| 1365 |
void RedoAction::addTo(QWidget* widget) |
| 1366 |
{ |
| 1367 |
if (widget->inherits("QToolBar")) { |
| 1368 |
actionChanged(); |
| 1369 |
connect(action(), &QAction::changed, this, &RedoAction::actionChanged); |
| 1370 |
widget->addAction(_toolAction); |
| 1371 |
} |
| 1372 |
else { |
| 1373 |
widget->addAction(action()); |
| 1374 |
} |
| 1375 |
} |
| 1376 |
|
| 1377 |
void RedoAction::actionChanged() |
| 1378 |
{ |
| 1379 |
// Do NOT set the shortcut again for _toolAction since this is already |
| 1380 |
// reserved for _action. Otherwise we get an ambiguity of it with the |
| 1381 |
// result that it doesn't work anymore. |
| 1382 |
_toolAction->setText(action()->text()); |
| 1383 |
_toolAction->setToolTip(action()->toolTip()); |
| 1384 |
_toolAction->setStatusTip(action()->statusTip()); |
| 1385 |
_toolAction->setWhatsThis(action()->whatsThis()); |
| 1386 |
_toolAction->setIcon(action()->icon()); |
| 1387 |
} |
| 1388 |
|
| 1389 |
void RedoAction::setEnabled(bool check) |
| 1390 |
{ |
| 1391 |
Action::setEnabled(check); |
| 1392 |
_toolAction->setEnabled(check); |
| 1393 |
} |
| 1394 |
|
| 1395 |
void RedoAction::setVisible(bool check) |
| 1396 |
{ |
| 1397 |
Action::setVisible(check); |
| 1398 |
_toolAction->setVisible(check); |
| 1399 |
} |
| 1400 |
|
| 1401 |
// -------------------------------------------------------------------- |
| 1402 |
|
| 1403 |
DockWidgetAction::DockWidgetAction(Command* pcCmd, QObject* parent) |
| 1404 |
: Action(pcCmd, parent) |
| 1405 |
, _menu(nullptr) |
| 1406 |
{} |
| 1407 |
|
| 1408 |
DockWidgetAction::~DockWidgetAction() |
| 1409 |
{ |
| 1410 |
delete _menu; |
| 1411 |
} |
| 1412 |
|
| 1413 |
void DockWidgetAction::addTo(QWidget* widget) |
| 1414 |
{ |
| 1415 |
if (!_menu) { |
| 1416 |
_menu = new QMenu(); |
| 1417 |
action()->setMenu(_menu); |
| 1418 |
getMainWindow()->setDockWindowMenu(_menu); |
| 1419 |
} |
| 1420 |
|
| 1421 |
widget->addAction(action()); |
| 1422 |
} |
| 1423 |
|
| 1424 |
// -------------------------------------------------------------------- |
| 1425 |
|
| 1426 |
ToolBarAction::ToolBarAction(Command* pcCmd, QObject* parent) |
| 1427 |
: Action(pcCmd, parent) |
| 1428 |
, _menu(nullptr) |
| 1429 |
{} |
| 1430 |
|
| 1431 |
ToolBarAction::~ToolBarAction() |
| 1432 |
{ |
| 1433 |
delete _menu; |
| 1434 |
} |
| 1435 |
|
| 1436 |
void ToolBarAction::addTo(QWidget* widget) |
| 1437 |
{ |
| 1438 |
if (!_menu) { |
| 1439 |
_menu = new QMenu(); |
| 1440 |
action()->setMenu(_menu); |
| 1441 |
getMainWindow()->setToolBarMenu(_menu); |
| 1442 |
} |
| 1443 |
|
| 1444 |
widget->addAction(action()); |
| 1445 |
} |
| 1446 |
|
| 1447 |
// -------------------------------------------------------------------- |
| 1448 |
|
| 1449 |
WindowAction::WindowAction(Command* pcCmd, QObject* parent) |
| 1450 |
: ActionGroup(pcCmd, parent) |
| 1451 |
, _menu(nullptr) |
| 1452 |
{} |
| 1453 |
|
| 1454 |
void WindowAction::addTo(QWidget* widget) |
| 1455 |
{ |
| 1456 |
auto menu = qobject_cast<QMenu*>(widget); |
| 1457 |
if (!menu) { |
| 1458 |
if (!_menu) { |
| 1459 |
_menu = new QMenu(); |
| 1460 |
action()->setMenu(_menu); |
| 1461 |
_menu->addActions(groupAction()->actions()); |
| 1462 |
getMainWindow()->setWindowsMenu(_menu); |
| 1463 |
} |
| 1464 |
|
| 1465 |
widget->addAction(action()); |
| 1466 |
} |
| 1467 |
else { |
| 1468 |
menu->addActions(groupAction()->actions()); |
| 1469 |
getMainWindow()->setWindowsMenu(menu); |
| 1470 |
} |
| 1471 |
} |
| 1472 |
|
| 1473 |
#include "moc_Action.cpp" |