Skip to content

Commit

Permalink
Infrastructure: remove unneeded control from trigger editor (Mudlet#7505
Browse files Browse the repository at this point in the history
)

#### Brief overview of PR changes/additions
Simplify and improve the "trigger" editor part of the "Editor" by
combining/removing some group boxes and making a third one not have a
check-box. Also move them all from the bottom of the left to the right
side of that area and improve the behaviour as the splitter for the
right side is adjusted.

#### Motivation for adding to Mudlet
Improving the UI - helping to make the controls a bit more intuitive.

#### Other info (issues closed, discussion etc)
Having a QSpinBox to control the "condition line" delta for
AND/Multi-line triggers as well as placing it in a checkable group-box
to enable it is more complex than it needs to be. This PR removes the
checkbox and instead uses the ability of a `QComboBox` to have a
"special value" which it presents different - in this case to report the
trigger as an OR/Multi-item type trigger. Having done this I realised
that it and the other two items at the bottom of the left side of the
"triggers" editor could be simplified further and all moved to the right
side. Then I had to tweak the code that showed/hid the right and bottom
widgets (since the bottom ones had gone). Finally I put in code to
disable the "OR multi-item"/"AND multi-line" control when there is less
than two "items" in the trigger.

Also:
* rename `(QWidget*) dlgTriggerEditor::HpatternList` ==>
`dlgTriggerEditor::mpWidget_triggerItems`
* convert the - not used as a button - `pushButton_prompt` from a
`QPushButton` to a `QLabel` - and make it be enabled when GA/EoRs are
detected - as well as changing the text as before.

This was prompted by working on the fix for Mudlet#7480.
  • Loading branch information
SlySven authored Nov 4, 2024
1 parent c84c920 commit 5f99503
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 314 deletions.
8 changes: 7 additions & 1 deletion src/TTrigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,8 @@ bool TTrigger::setupTmpColorTrigger(int ansiFg, int ansiBg)
return false;
}

// createColorPatternText(...) now returns an empty string if BOTH color
// codes are the scmIgnored ones:
mPatterns << createColorPatternText(ansiFg, ansiBg);
mPatternKinds << REGEX_COLOR_PATTERN;
mColorPatternList.push_back(pCT);
Expand Down Expand Up @@ -1461,7 +1463,11 @@ QString TTrigger::createColorPatternText(const int fgColorCode, const int bgColo
bgText = qsl("%1").arg(bgColorCode, 3, 10, QLatin1Char('0'));
}

return qsl("ANSI_COLORS_F{%1}_B{%2}").arg(fgText, bgText);
// QString::compare(...) returns zero (boolean false) on a match, or a
// non-zero (boolean true) on no match - and we want to detect when BOTH
// texts are IGNORE so we return an empty string in that case only - so that
// it is equivalent to an empty other trigger type:
return (fgText.compare(QLatin1String("IGNORE")) || bgText.compare(QLatin1String("IGNORE"))) ? qsl("ANSI_COLORS_F{%1}_B{%2}").arg(fgText, bgText) : QString();
}

void TTrigger::decodeColorPatternText(const QString& patternText, int& fgColorCode, int& bgColorCode)
Expand Down
127 changes: 81 additions & 46 deletions src/dlgTriggerEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,11 +789,11 @@ dlgTriggerEditor::dlgTriggerEditor(Host* pH)
slot_showSearchAreaResults(false);

mpScrollArea = mpTriggersMainArea->scrollArea;
HpatternList = new QWidget;
auto lay1 = new QVBoxLayout(HpatternList);
mpWidget_triggerItems = new QWidget;
auto lay1 = new QVBoxLayout(mpWidget_triggerItems);
lay1->setContentsMargins(0, 0, 0, 0);
lay1->setSpacing(0);
mpScrollArea->setWidget(HpatternList);
mpScrollArea->setWidget(mpWidget_triggerItems);

QPixmap pixMap_subString(256, 256);
pixMap_subString.fill(Qt::black);
Expand Down Expand Up @@ -838,7 +838,7 @@ dlgTriggerEditor::dlgTriggerEditor(Host* pH)
<< tr("prompt");

for (int i = 0; i < 50; i++) {
auto pItem = new dlgTriggerPatternEdit(HpatternList);
auto pItem = new dlgTriggerPatternEdit(mpWidget_triggerItems);
QComboBox* pBox = pItem->comboBox_patternType;
pBox->addItems(patternList);
pBox->setItemData(0, QVariant(i));
Expand All @@ -854,12 +854,12 @@ dlgTriggerEditor::dlgTriggerEditor(Host* pH)
connect(pItem->pushButton_fgColor, &QAbstractButton::clicked, this, &dlgTriggerEditor::slot_colorTriggerFg);
connect(pItem->pushButton_bgColor, &QAbstractButton::clicked, this, &dlgTriggerEditor::slot_colorTriggerBg);
connect(pItem->lineEdit_pattern, &QLineEdit::textChanged, this, &dlgTriggerEditor::slot_changedPattern);
HpatternList->layout()->addWidget(pItem);
mpWidget_triggerItems->layout()->addWidget(pItem);
mTriggerPatternEdit.push_back(pItem);
pItem->mRow = i;
pItem->pushButton_fgColor->hide();
pItem->pushButton_bgColor->hide();
pItem->pushButton_prompt->hide();
pItem->label_prompt->hide();
pItem->spinBox_lineSpacer->hide();
pItem->label_patternNumber->setText(QString::number(i+1));
pItem->label_patternNumber->show();
Expand Down Expand Up @@ -3788,15 +3788,14 @@ void dlgTriggerEditor::addTrigger(bool isFolder)
}
mpTriggersMainArea->lineEdit_trigger_name->clear();
mpTriggersMainArea->label_idNumber->clear();
mpTriggersMainArea->groupBox_perlSlashGOption->setChecked(false);
mpTriggersMainArea->checkBox_perlSlashGOption->setChecked(false);

clearDocument(mpSourceEditorEdbee); // New Trigger

mpTriggersMainArea->lineEdit_trigger_command->clear();
mpTriggersMainArea->groupBox_filterTrigger->setChecked(false);
mpTriggersMainArea->checkBox_filterTrigger->setChecked(false);
mpTriggersMainArea->spinBox_stayOpen->setValue(0);
mpTriggersMainArea->spinBox_lineMargin->setValue(0);
mpTriggersMainArea->groupBox_multiLineTrigger->setChecked(false);
mpTriggersMainArea->spinBox_lineMargin->setValue(-1);

mpTriggersMainArea->pushButtonFgColor->setChecked(false);
mpTriggersMainArea->pushButtonBgColor->setChecked(false);
Expand Down Expand Up @@ -4440,7 +4439,7 @@ void dlgTriggerEditor::saveTrigger()
mpTriggersMainArea->trimName();
const QString name = mpTriggersMainArea->lineEdit_trigger_name->text();
const QString command = mpTriggersMainArea->lineEdit_trigger_command->text();
const bool isMultiline = mpTriggersMainArea->groupBox_multiLineTrigger->isChecked();
const bool isMultiline = (mpTriggersMainArea->spinBox_lineMargin->value() > -1);
QStringList patterns;
QList<int> patternKinds;
for (int i = 0; i < 50; i++) {
Expand Down Expand Up @@ -4496,9 +4495,11 @@ void dlgTriggerEditor::saveTrigger()

pT->setScript(script);
pT->setIsMultiline(isMultiline);
pT->mPerlSlashGOption = mpTriggersMainArea->groupBox_perlSlashGOption->isChecked();
pT->mFilterTrigger = mpTriggersMainArea->groupBox_filterTrigger->isChecked();
pT->setConditionLineDelta(mpTriggersMainArea->spinBox_lineMargin->value());
pT->mPerlSlashGOption = mpTriggersMainArea->checkBox_perlSlashGOption->isChecked();
pT->mFilterTrigger = mpTriggersMainArea->checkBox_filterTrigger->isChecked();
if (mpTriggersMainArea->spinBox_lineMargin->value() >= 0) {
pT->setConditionLineDelta(mpTriggersMainArea->spinBox_lineMargin->value());
}
pT->mStayOpen = mpTriggersMainArea->spinBox_stayOpen->value();
pT->mSoundTrigger = mpTriggersMainArea->groupBox_soundTrigger->isChecked();
pT->setSound(mpTriggersMainArea->lineEdit_soundFile->text());
Expand Down Expand Up @@ -5535,39 +5536,43 @@ void dlgTriggerEditor::setupPatternControls(const int type, dlgTriggerPatternEdi
pItem->lineEdit_pattern->show();
pItem->pushButton_fgColor->hide();
pItem->pushButton_bgColor->hide();
pItem->pushButton_prompt->hide();
pItem->label_prompt->hide();
pItem->spinBox_lineSpacer->hide();
break;
case REGEX_LINE_SPACER:
pItem->lineEdit_pattern->hide();
pItem->pushButton_fgColor->hide();
pItem->pushButton_bgColor->hide();
pItem->pushButton_prompt->hide();
pItem->label_prompt->hide();
pItem->spinBox_lineSpacer->show();
break;
case REGEX_COLOR_PATTERN:
// CHECKME: Do we need to regenerate (hidden patter text) and button texts/colors?
pItem->lineEdit_pattern->hide();
pItem->pushButton_fgColor->show();
pItem->pushButton_bgColor->show();
pItem->pushButton_prompt->hide();
pItem->label_prompt->hide();
pItem->spinBox_lineSpacer->hide();
break;
case REGEX_PROMPT:
pItem->lineEdit_pattern->hide();
pItem->pushButton_fgColor->hide();
pItem->pushButton_bgColor->hide();
if (mpHost->mTelnet.mGA_Driver) {
pItem->pushButton_prompt->setText(tr("match on the prompt line"));
pItem->pushButton_prompt->setToolTip(QString());
pItem->label_prompt->setText(tr("match on the prompt line"));
pItem->label_prompt->setToolTip(QString());
pItem->label_prompt->setEnabled(true);
} else {
pItem->pushButton_prompt->setText(tr("match on the prompt line (disabled)"));
pItem->pushButton_prompt->setToolTip(utils::richText(tr("A Go-Ahead (GA) signal from the game is required to make this feature work")));
pItem->label_prompt->setText(tr("match on the prompt line (disabled)"));
pItem->label_prompt->setToolTip(utils::richText(tr("A Go-Ahead (GA) signal from the game is required to make this feature work")));
pItem->label_prompt->setEnabled(false);
}
pItem->pushButton_prompt->show();
pItem->label_prompt->show();
pItem->spinBox_lineSpacer->hide();
break;
}

checkForMoreThanOneTriggerItem();
}

void dlgTriggerEditor::slot_changedPattern()
Expand All @@ -5576,6 +5581,8 @@ void dlgTriggerEditor::slot_changedPattern()
if (lineEditShouldMarkSpaces[lineEdit]) {
markQLineEdit(lineEdit);
}

checkForMoreThanOneTriggerItem();
}

// This can get called after the lineEdit contents has changed and it is now a
Expand Down Expand Up @@ -5682,15 +5689,14 @@ void dlgTriggerEditor::slot_triggerSelected(QTreeWidgetItem* pItem)
mpTriggersMainArea->lineEdit_trigger_name->clear();
mpTriggersMainArea->label_idNumber->clear();
clearDocument(mpSourceEditorEdbee); // Trigger Select
mpTriggersMainArea->groupBox_multiLineTrigger->setChecked(false);
mpTriggersMainArea->groupBox_perlSlashGOption->setChecked(false);
mpTriggersMainArea->groupBox_filterTrigger->setChecked(false);
mpTriggersMainArea->checkBox_perlSlashGOption->setChecked(false);
mpTriggersMainArea->checkBox_filterTrigger->setChecked(false);
mpTriggersMainArea->groupBox_triggerColorizer->setChecked(false);
mpTriggersMainArea->pushButtonFgColor->setStyleSheet(QString());
mpTriggersMainArea->pushButtonFgColor->setProperty(cButtonBaseColor, QVariant());
mpTriggersMainArea->pushButtonBgColor->setStyleSheet(QString());
mpTriggersMainArea->pushButtonBgColor->setProperty(cButtonBaseColor, QVariant());
mpTriggersMainArea->spinBox_lineMargin->setValue(1);
mpTriggersMainArea->spinBox_lineMargin->setValue(-1);

const int ID = pItem->data(0, Qt::UserRole).toInt();
TTrigger* pT = mpHost->getTriggerUnit()->getTrigger(ID);
Expand Down Expand Up @@ -5785,7 +5791,7 @@ void dlgTriggerEditor::slot_triggerSelected(QTreeWidgetItem* pItem)
}
mTriggerPatternEdit[i]->pushButton_fgColor->hide();
mTriggerPatternEdit[i]->pushButton_bgColor->hide();
mTriggerPatternEdit[i]->pushButton_prompt->hide();
mTriggerPatternEdit[i]->label_prompt->hide();
mTriggerPatternEdit[i]->spinBox_lineSpacer->hide();
// Nudge the type up and down so that the appropriate (coloured) icon is copied across to the QLineEdit:
mTriggerPatternEdit[i]->comboBox_patternType->setCurrentIndex(1);
Expand All @@ -5797,10 +5803,13 @@ void dlgTriggerEditor::slot_triggerSelected(QTreeWidgetItem* pItem)
mpTriggersMainArea->lineEdit_trigger_name->setText(pItem->text(0));
mpTriggersMainArea->label_idNumber->setText(QString::number(ID));
mpTriggersMainArea->lineEdit_trigger_command->setText(command);
mpTriggersMainArea->groupBox_multiLineTrigger->setChecked(pT->isMultiline());
mpTriggersMainArea->groupBox_perlSlashGOption->setChecked(pT->mPerlSlashGOption);
mpTriggersMainArea->groupBox_filterTrigger->setChecked(pT->mFilterTrigger);
mpTriggersMainArea->spinBox_lineMargin->setValue(pT->getConditionLineDelta());
mpTriggersMainArea->checkBox_perlSlashGOption->setChecked(pT->mPerlSlashGOption);
mpTriggersMainArea->checkBox_filterTrigger->setChecked(pT->mFilterTrigger);
if (pT->isMultiline()) {
mpTriggersMainArea->spinBox_lineMargin->setValue(pT->getConditionLineDelta());
} else {
mpTriggersMainArea->spinBox_lineMargin->setValue(-1);
}
mpTriggersMainArea->spinBox_stayOpen->setValue(pT->mStayOpen);
mpTriggersMainArea->groupBox_soundTrigger->setChecked(pT->mSoundTrigger);
if (!pT->mSoundFile.isEmpty()) {
Expand All @@ -5824,6 +5833,8 @@ void dlgTriggerEditor::slot_triggerSelected(QTreeWidgetItem* pItem)
//: Keep the existing colour on matches to highlight. Use shortest word possible so it fits on the button
mpTriggersMainArea->pushButtonBgColor->setText(transparentBg ? tr("keep") : QString());

checkForMoreThanOneTriggerItem();

clearDocument(mpSourceEditorEdbee, pT->getScript());

if (!pT->state()) {
Expand Down Expand Up @@ -9988,10 +9999,6 @@ void dlgTriggerEditor::slot_showAllTriggerControls(const bool isShown)
mpTriggersMainArea->toolButton_toggleExtraControls->setChecked(isShown);
}

if (mpTriggersMainArea->widget_bottom->isVisible() != isShown) {
mpTriggersMainArea->widget_bottom->setVisible(isShown);
}

if (mpTriggersMainArea->widget_right->isVisible() != isShown) {
mpTriggersMainArea->widget_right->setVisible(isShown);
}
Expand All @@ -10008,34 +10015,35 @@ void dlgTriggerEditor::slot_rightSplitterMoved(const int, const int)
*w_|| || | || ||
*il|| scroll area || widget | || scroll area ||
*de|| || _right | || ||
*gf|+--------------------+| | |+------------------------------+|
*et|+--------------------+| | --+--------------------------------+
*t || widget_bottom || |
*gf|| || | |+------------------------------+|
*et|| || | --+--------------------------------+
*t || || |
*=>|+--------------------+| |
*--+----------------------+---------+
*/
const int hysteresis = 10;
static int bottomWidgetHeight = 0;
if (mpTriggersMainArea->isVisible()) {
mTriggerEditorSplitterState = splitter_right->saveState();
// The triggersMainArea is visible
if (mpTriggersMainArea->toolButton_toggleExtraControls->isChecked()) {
// The extra controls are visible in the triggersMainArea
bottomWidgetHeight = mpTriggersMainArea->widget_bottom->height();
if ((mpTriggersMainArea->widget_left->height()) <= (mpTriggersMainArea->widget_right->minimumSizeHint().height() + hysteresis)
|| mpTriggersMainArea->widget_verticalSpacer_right->height() == 0) {
if (mpTriggersMainArea->widget_verticalSpacer_right->height() <= hysteresis) {
// And it is not tall enough to show the right hand side - so
// hide them:
// hide them - we are using the spacer to detect if there is any
// space:
slot_showAllTriggerControls(false);
// And the first time note down the required height:
if (mTriggerMainAreaMinimumHeightToShowAll < 1) {
mTriggerMainAreaMinimumHeightToShowAll = mpTriggersMainArea->widget_left->height();
}
}

} else {
// And the extra controls are NOT visible
if ((mpTriggersMainArea->widget_left->height() - bottomWidgetHeight) > mpTriggersMainArea->widget_right->minimumSizeHint().height() - hysteresis) {
// And it is tall enough to show the right hand side completely
// so show them:
if (mTriggerMainAreaMinimumHeightToShowAll > 0 && mpTriggersMainArea->widget_left->height() > mTriggerMainAreaMinimumHeightToShowAll) {
slot_showAllTriggerControls(true);
}

}
} else if (mpActionsMainArea->isVisible()) {
mActionEditorSplitterState = splitter_right->saveState();
Expand Down Expand Up @@ -10247,3 +10255,30 @@ void dlgTriggerEditor::showIDLabels(const bool visible)
mpTimersMainArea->frameId->setVisible(visible);
mpTriggersMainArea->frameId->setVisible(visible);
}

void dlgTriggerEditor::checkForMoreThanOneTriggerItem()
{
int activeItems = 0;
if (!mpWidget_triggerItems || !mpWidget_triggerItems->layout()) {
return;
}
auto pLayout = mpWidget_triggerItems->layout();
for (qsizetype i = 0, total = pLayout->count(); i < total; ++i) {
auto pLayoutItem = pLayout->itemAt(i)->widget();
if (pLayoutItem) {
auto* pLineEdit_pattern = pLayoutItem->findChild<QLineEdit*>(qsl("lineEdit_pattern"));
auto* pComboBox_type = pLayoutItem->findChild<QComboBox*>(qsl("comboBox_patternType"));
if (pComboBox_type && (pComboBox_type->currentIndex() == REGEX_PROMPT || pComboBox_type->currentIndex() == REGEX_LINE_SPACER)) {
// These automatically counts as an active item - though if there
// isn't any GA signals the first won't work...
++activeItems;
} else {
if (pLineEdit_pattern && !pLineEdit_pattern->text().isEmpty()) {
++activeItems;
}
}
}
}

mpTriggersMainArea->groupBox_multiLineTrigger->setEnabled(activeItems > 1);
}
8 changes: 7 additions & 1 deletion src/dlgTriggerEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ private slots:

void showOrHideRestoreEditorActionsToolbarAction();
void showOrHideRestoreEditorItemsToolbarAction();
void checkForMoreThanOneTriggerItem();

// PLACEMARKER 3/3 save button texts need to be kept in sync
std::unordered_map<QString, QString> mButtonShortcuts = {
Expand Down Expand Up @@ -495,7 +496,7 @@ private slots:
EditorViewType mCurrentView = EditorViewType::cmUnknownView;

QScrollArea* mpScrollArea = nullptr;
QWidget* HpatternList = nullptr;
QWidget* mpWidget_triggerItems = nullptr;
// this widget holds the errors, trigger patterns, and all other widgets that aren't edbee
// in it, as a workaround for an extra splitter getting created by Qt below the error msg otherwise
QWidget *mpNonCodeWidgets = nullptr;
Expand Down Expand Up @@ -563,6 +564,11 @@ private slots:
// profile autosave interval in minutes
int mAutosaveInterval = 2;

// The space recorded for the left side for "items" in the trigger area
// so as to be able to fit the right side with the extra controls,
// determined the first time the area is shrunk down by the user:
int mTriggerMainAreaMinimumHeightToShowAll = 0;

// tracks location of the splitter in the trigger editor for each tab
QByteArray mTriggerEditorSplitterState;
QByteArray mAliasEditorSplitterState;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/trigger_pattern_edit.ui
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_prompt">
<widget class="QLabel" name="label_prompt">
<property name="enabled">
<bool>false</bool>
</property>
Expand Down
Loading

0 comments on commit 5f99503

Please sign in to comment.