Merge pull request #4995 from chennes/fix4664HardcodedTooltips

[GUI] Refactor tooltip code to rebuild shortcut
This commit is contained in:
Yorik van Havre
2021-08-27 12:19:45 +02:00
committed by GitHub
4 changed files with 43 additions and 54 deletions

View File

@@ -852,34 +852,24 @@ const char * Command::endCmdHelp(void)
return "</body></html>\n\n";
}
void Command::applyCommandData(const char* context, Action* action)
void Command::recreateTooltip(const char* context, Action* action)
{
action->setText(QCoreApplication::translate(
QString tooltip;
tooltip.append(QString::fromLatin1("<h3>"));
tooltip.append(QCoreApplication::translate(
context, getMenuText()));
// build the tooltip
QString tooltip;
tooltip.append(QString::fromLatin1("<h3>"));
tooltip.append(QCoreApplication::translate(
context, getMenuText()));
tooltip.append(QString::fromLatin1("</h3>"));
QRegularExpression re(QString::fromLatin1("([^&])&([^&])"));
tooltip.replace(re, QString::fromLatin1("\\1\\2"));
tooltip.replace(QString::fromLatin1("&&"), QString::fromLatin1("&"));
tooltip.append(QCoreApplication::translate(
context, getToolTipText()));
tooltip.append(QString::fromLatin1("<br><i>("));
tooltip.append(QCoreApplication::translate(
context, getWhatsThis()));
tooltip.append(QString::fromLatin1(")</i> "));
action->setToolTip(tooltip);
action->setWhatsThis(QCoreApplication::translate(
tooltip.append(QString::fromLatin1("</h3>"));
QRegularExpression re(QString::fromLatin1("([^&])&([^&])"));
tooltip.replace(re, QString::fromLatin1("\\1\\2"));
tooltip.replace(QString::fromLatin1("&&"), QString::fromLatin1("&"));
tooltip.append(QCoreApplication::translate(
context, getToolTipText()));
tooltip.append(QString::fromLatin1("<br><i>("));
tooltip.append(QCoreApplication::translate(
context, getWhatsThis()));
if (sStatusTip)
action->setStatusTip(QCoreApplication::translate(
context, getStatusTip()));
else
action->setStatusTip(QCoreApplication::translate(
context, getToolTipText()));
tooltip.append(QString::fromLatin1(")</i> "));
action->setToolTip(tooltip);
QString accel = action->shortcut().toString(QKeySequence::NativeText);
if (!accel.isEmpty()) {
// show shortcut inside tooltip
@@ -892,6 +882,22 @@ void Command::applyCommandData(const char* context, Action* action)
.arg(accel, action->statusTip());
action->setStatusTip(stip);
}
if (sStatusTip)
action->setStatusTip(QCoreApplication::translate(
context, getStatusTip()));
else
action->setStatusTip(QCoreApplication::translate(
context, getToolTipText()));
}
void Command::applyCommandData(const char* context, Action* action)
{
action->setText(QCoreApplication::translate(
context, getMenuText()));
recreateTooltip(context, action);
action->setWhatsThis(QCoreApplication::translate(
context, getWhatsThis()));
}
const char* Command::keySequenceToAccel(int sk) const

View File

@@ -343,6 +343,8 @@ public:
void testActive(void);
/// Enables or disables the command
void setEnabled(bool);
/// (Re)Create the text for the tooltip (for example, when the shortcut is changed)
void recreateTooltip(const char* context, Action*);
/// Command trigger source
enum TriggerSource {
/// No external trigger, e.g. invoked through Python

View File

@@ -234,35 +234,8 @@ void DlgCustomKeyboardImp::setShortcutOfCurrentAction(const QString& accelText)
ui->editShortcut->clear();
}
// update the tool tip
QString toolTip = QCoreApplication::translate(cmd->className(),
cmd->getToolTipText());
if (!nativeText.isEmpty()) {
if (!toolTip.isEmpty()) {
QString tip = QString::fromLatin1("%1 (%2)")
.arg(toolTip, nativeText);
action->setToolTip(tip);
}
}
else {
action->setToolTip(toolTip);
}
// update the status tip
QString statusTip = QCoreApplication::translate(cmd->className(),
cmd->getStatusTip());
if (statusTip.isEmpty())
statusTip = toolTip;
if (!nativeText.isEmpty()) {
if (!statusTip.isEmpty()) {
QString tip = QString::fromLatin1("(%1)\t%2")
.arg(nativeText, statusTip);
action->setStatusTip(tip);
}
}
else {
action->setStatusTip(statusTip);
}
// update the tool tip (and status tip)
cmd->recreateTooltip(cmd->className(), action);
// The shortcuts for macros are store in a different location,
// also override the command's shortcut directly
@@ -313,6 +286,9 @@ void DlgCustomKeyboardImp::on_buttonReset_clicked()
ui->accelLineEditShortcut->setText((txt.isEmpty() ? tr("none") : txt));
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("Shortcut");
hGrp->RemoveASCII(name.constData());
// update the tool tip (and status tip)
cmd->recreateTooltip(cmd->className(), cmd->getAction());
}
ui->buttonReset->setEnabled( false );
@@ -327,6 +303,10 @@ void DlgCustomKeyboardImp::on_buttonResetAll_clicked()
if ((*it)->getAction()) {
(*it)->getAction()->setShortcut(QKeySequence(QString::fromLatin1((*it)->getAccel()))
.toString(QKeySequence::NativeText));
// update the tool tip (and status tip)
(*it)->recreateTooltip((*it)->className(), (*it)->getAction());
}
}

View File

@@ -325,6 +325,7 @@ void Workbench::setupCustomShortcuts() const
QString str = QString::fromUtf8(it->second.c_str());
QKeySequence shortcut = str;
cmd->getAction()->setShortcut(shortcut.toString(QKeySequence::NativeText));
cmd->recreateTooltip(it->first.c_str(), cmd->getAction()); // The tooltip has the shortcut in it...
}
}
}