Improve editing of hotkeys (#4628)

Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
Mm2PL
2023-05-27 14:04:30 +00:00
committed by GitHub
parent fb02d59b48
commit c7b22939d5
7 changed files with 412 additions and 125 deletions
+165 -61
View File
@@ -17,6 +17,14 @@ EditHotkeyDialog::EditHotkeyDialog(const std::shared_ptr<Hotkey> hotkey,
, data_(hotkey)
{
this->ui_->setupUi(this);
this->setStyleSheet(R"(QToolTip {
padding: 2px;
background-color: #333333;
border: 1px solid #545454;
color: white;
})");
this->ui_->easyArgsPicker->setVisible(false);
this->ui_->easyArgsLabel->setVisible(false);
// dynamically add category names to the category picker
for (const auto &[_, hotkeyCategory] : getApp()->hotkeys->categories())
{
@@ -28,34 +36,7 @@ EditHotkeyDialog::EditHotkeyDialog(const std::shared_ptr<Hotkey> hotkey,
if (hotkey)
{
if (!hotkey->validAction())
{
this->showEditError("Invalid action, make sure you select the "
"correct action before saving.");
}
// editing a hotkey
// update pickers/input boxes to values from Hotkey object
this->ui_->categoryPicker->setCurrentIndex(size_t(hotkey->category()));
this->ui_->keyComboEdit->setKeySequence(
QKeySequence::fromString(hotkey->keySequence().toString()));
this->ui_->nameEdit->setText(hotkey->name());
// update arguments
QString argsText;
bool first = true;
for (const auto &arg : hotkey->arguments())
{
if (!first)
{
argsText += '\n';
}
argsText += arg;
first = false;
}
this->ui_->argumentsEdit->setPlainText(argsText);
this->setFromHotkey(hotkey);
}
else
{
@@ -66,6 +47,96 @@ EditHotkeyDialog::EditHotkeyDialog(const std::shared_ptr<Hotkey> hotkey,
this->ui_->argumentsEdit->setPlainText("");
}
}
void EditHotkeyDialog::setFromHotkey(std::shared_ptr<Hotkey> hotkey)
{
if (!hotkey->validAction())
{
this->showEditError("Invalid action, make sure you select the "
"correct action before saving.");
}
// editing a hotkey
// update pickers/input boxes to values from Hotkey object
this->ui_->categoryPicker->setCurrentIndex(size_t(hotkey->category()));
this->ui_->keyComboEdit->setKeySequence(
QKeySequence::fromString(hotkey->keySequence().toString()));
this->ui_->nameEdit->setText(hotkey->name());
auto def = findHotkeyActionDefinition(hotkey->category(), hotkey->action());
if (def.has_value() && !def->possibleArguments.empty())
{
qCDebug(chatterinoHotkeys) << "Enabled easy picker and arg edit "
"because we have arguments from hotkey";
this->ui_->easyArgsLabel->setVisible(true);
this->ui_->easyArgsPicker->setVisible(true);
this->ui_->argumentsEdit->setVisible(false);
this->ui_->argumentsLabel->setVisible(false);
this->ui_->argumentsDescription->setVisible(false);
this->ui_->easyArgsPicker->clear();
this->ui_->easyArgsLabel->setText(def->argumentsPrompt);
this->ui_->easyArgsLabel->setToolTip(def->argumentsPromptHover);
int matchIdx = -1;
for (int i = 0; i < def->possibleArguments.size(); i++)
{
const auto &[displayText, argData] = def->possibleArguments.at(i);
this->ui_->easyArgsPicker->addItem(displayText);
// check if matches
if (argData.size() != hotkey->arguments().size())
{
continue;
}
bool matches = true;
for (int j = 0; j < argData.size(); j++)
{
if (argData.at(j) != hotkey->arguments().at(j))
{
matches = false;
break;
}
}
if (matches)
{
matchIdx = i;
}
}
if (matchIdx != -1)
{
this->ui_->easyArgsPicker->setCurrentIndex(matchIdx);
return;
}
qCDebug(chatterinoHotkeys)
<< "Did not match hotkey arguments for " << hotkey->toString()
<< "using text edit instead of easy picker";
this->showEditError("Arguments do not match what's expected. The "
"argument picker is not available.");
this->ui_->easyArgsLabel->setVisible(false);
this->ui_->easyArgsPicker->setVisible(false);
this->ui_->argumentsEdit->setVisible(true);
this->ui_->argumentsLabel->setVisible(true);
this->ui_->argumentsDescription->setVisible(true);
}
// update arguments
QString argsText;
bool first = true;
for (const auto &arg : hotkey->arguments())
{
if (!first)
{
argsText += '\n';
}
argsText += arg;
first = false;
}
this->ui_->argumentsEdit->setPlainText(argsText);
}
EditHotkeyDialog::~EditHotkeyDialog()
{
@@ -151,6 +222,14 @@ void EditHotkeyDialog::afterEdit()
action = actionTemp.toString();
}
auto def = findHotkeyActionDefinition(*category, action);
if (def.has_value() && this->ui_->easyArgsPicker->isVisible())
{
arguments =
def->possibleArguments.at(this->ui_->easyArgsPicker->currentIndex())
.second;
}
auto hotkey = std::make_shared<Hotkey>(
*category, this->ui_->keyComboEdit->keySequence(), action, arguments,
nameText);
@@ -263,44 +342,69 @@ void EditHotkeyDialog::updateArgumentsInput()
}
const ActionDefinition &def = definition->second;
if (def.maxCountArguments != 0)
{
QString text =
"Arguments wrapped in <> are required.\nArguments wrapped in "
"[] "
"are optional.\nArguments are separated by a newline.";
if (!def.argumentDescription.isEmpty())
{
this->ui_->argumentsDescription->setVisible(true);
this->ui_->argumentsDescription->setText(
def.argumentDescription);
}
else
{
this->ui_->argumentsDescription->setVisible(false);
}
text = QString("Arguments wrapped in <> are required.");
if (def.maxCountArguments != def.minCountArguments)
{
text += QString("\nArguments wrapped in [] are optional.");
}
text += "\nArguments are separated by a newline.";
this->ui_->argumentsEdit->setEnabled(true);
this->ui_->argumentsEdit->setPlaceholderText(text);
this->ui_->argumentsLabel->setVisible(true);
this->ui_->argumentsDescription->setVisible(true);
this->ui_->argumentsEdit->setVisible(true);
}
else
if (def.maxCountArguments == 0)
{
qCDebug(chatterinoHotkeys) << "Disabled easy picker and arg edit "
"because we don't have any arguments";
this->ui_->argumentsLabel->setVisible(false);
this->ui_->argumentsDescription->setVisible(false);
this->ui_->argumentsEdit->setVisible(false);
this->ui_->easyArgsLabel->setVisible(false);
this->ui_->easyArgsPicker->setVisible(false);
return;
}
if (!def.argumentDescription.isEmpty())
{
this->ui_->argumentsDescription->setVisible(true);
this->ui_->argumentsDescription->setText(def.argumentDescription);
}
else
{
this->ui_->argumentsDescription->setVisible(false);
}
QString text = "Arguments wrapped in <> are required.";
if (def.maxCountArguments != def.minCountArguments)
{
text += QString("\nArguments wrapped in [] are optional.");
}
text += "\nArguments are separated by a newline.";
this->ui_->argumentsEdit->setEnabled(true);
this->ui_->argumentsEdit->setPlaceholderText(text);
this->ui_->argumentsLabel->setVisible(true);
this->ui_->argumentsDescription->setVisible(true);
this->ui_->argumentsEdit->setVisible(true);
// update easy picker
if (def.possibleArguments.empty())
{
qCDebug(chatterinoHotkeys)
<< "Disabled easy picker because we have possible arguments";
this->ui_->easyArgsPicker->setVisible(false);
this->ui_->easyArgsLabel->setVisible(false);
return;
}
qCDebug(chatterinoHotkeys)
<< "Enabled easy picker because we have possible arguments";
this->ui_->easyArgsPicker->setVisible(true);
this->ui_->easyArgsLabel->setVisible(true);
this->ui_->argumentsLabel->setVisible(false);
this->ui_->argumentsEdit->setVisible(false);
this->ui_->argumentsDescription->setVisible(false);
this->ui_->easyArgsPicker->clear();
for (const auto &[displayText, _] : def.possibleArguments)
{
this->ui_->easyArgsPicker->addItem(displayText);
}
this->ui_->easyArgsPicker->setCurrentIndex(0);
this->ui_->easyArgsLabel->setText(def.argumentsPrompt);
this->ui_->easyArgsLabel->setToolTip(def.argumentsPromptHover);
}
}
+1
View File
@@ -49,6 +49,7 @@ protected slots:
private:
void showEditError(QString errorText);
void setFromHotkey(std::shared_ptr<Hotkey> hotkey);
Ui::EditHotkeyDialog *ui_;
std::shared_ptr<Hotkey> data_;
+42 -13
View File
@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<height>400</height>
</rect>
</property>
<property name="windowTitle">
@@ -42,6 +42,9 @@ see this message :)</string>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="nameLabel">
<property name="toolTip">
<string>Set a name for the hotkey so you will be able to identify it later</string>
</property>
<property name="text">
<string>Name:</string>
</property>
@@ -76,6 +79,9 @@ see this message :)</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="categoryPicker"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="actionLabel">
<property name="text">
@@ -95,6 +101,9 @@ see this message :)</string>
</item>
<item row="3" column="0">
<widget class="QLabel" name="keyComboLabel">
<property name="toolTip">
<string>Pressing this keybinding will invoke the hotkey</string>
</property>
<property name="text">
<string>Keybinding:</string>
</property>
@@ -107,6 +116,16 @@ see this message :)</string>
<widget class="QKeySequenceEdit" name="keyComboEdit"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="easyArgsLabel">
<property name="toolTip">
<string>You are not supposed to see this, please report this!</string>
</property>
<property name="text">
<string>Argument:</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="argumentsLabel">
<property name="text">
<string>Arguments:</string>
@@ -116,7 +135,7 @@ see this message :)</string>
</property>
</widget>
</item>
<item row="4" column="1">
<item row="5" column="1">
<widget class="QLabel" name="argumentsDescription">
<property name="text">
<string>You should never see this message :)</string>
@@ -126,7 +145,7 @@ see this message :)</string>
</property>
</widget>
</item>
<item row="5" column="1">
<item row="6" column="1">
<widget class="QPlainTextEdit" name="argumentsEdit">
<property name="plainText">
<string/>
@@ -136,8 +155,18 @@ see this message :)</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="categoryPicker"/>
<item row="4" column="1">
<widget class="QComboBox" name="easyArgsPicker">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="currentText">
<string/>
</property>
</widget>
</item>
</layout>
</item>
@@ -169,8 +198,8 @@ see this message :)</string>
<slot>afterEdit()</slot>
<hints>
<hint type="sourcelabel">
<x>257</x>
<y>290</y>
<x>263</x>
<y>352</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
@@ -185,8 +214,8 @@ see this message :)</string>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>325</x>
<y>290</y>
<x>331</x>
<y>352</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
@@ -201,8 +230,8 @@ see this message :)</string>
<slot>updatePossibleActions()</slot>
<hints>
<hint type="sourcelabel">
<x>246</x>
<y>85</y>
<x>172</x>
<y>118</y>
</hint>
<hint type="destinationlabel">
<x>75</x>
@@ -217,8 +246,8 @@ see this message :)</string>
<slot>updateArgumentsInput()</slot>
<hints>
<hint type="sourcelabel">
<x>148</x>
<y>119</y>
<x>172</x>
<y>156</y>
</hint>
<hint type="destinationlabel">
<x>74</x>