refactor: Change UI for fix behavior

This commit is contained in:
Petr Mironychev
2026-01-19 23:52:44 +01:00
parent ddd6aba091
commit 6f11260cd1
2 changed files with 82 additions and 77 deletions

View File

@ -34,6 +34,7 @@
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QDir> #include <QDir>
#include <QFontMetrics> #include <QFontMetrics>
#include <QFrame>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QIcon> #include <QIcon>
#include <QLabel> #include <QLabel>
@ -101,10 +102,11 @@ QuickRefactorDialog::QuickRefactorDialog(QWidget *parent, const QString &lastIns
setupUi(); setupUi();
QTimer::singleShot(0, this, &QuickRefactorDialog::updateDialogSize); QTimer::singleShot(0, this, &QuickRefactorDialog::updateDialogSize);
m_quickInstructionEdit->installEventFilter(this);
m_textEdit->installEventFilter(this); m_textEdit->installEventFilter(this);
updateDialogSize(); updateDialogSize();
m_commandsComboBox->setFocus(); m_quickInstructionEdit->setFocus();
} }
void QuickRefactorDialog::setupUi() void QuickRefactorDialog::setupUi()
@ -173,54 +175,56 @@ void QuickRefactorDialog::setupUi()
mainLayout->addLayout(actionsLayout); mainLayout->addLayout(actionsLayout);
QHBoxLayout *instructionsLayout = new QHBoxLayout(); QLabel *quickInstructionLabel = new QLabel(Tr::tr("Quick Instruction:"), this);
instructionsLayout->setSpacing(4); mainLayout->addWidget(quickInstructionLabel);
QLabel *instructionsLabel = new QLabel(Tr::tr("Custom Instructions:"), this); m_quickInstructionEdit = new QLineEdit(this);
instructionsLayout->addWidget(instructionsLabel); m_quickInstructionEdit->setPlaceholderText(Tr::tr("Type your instruction here..."));
mainLayout->addWidget(m_quickInstructionEdit);
QHBoxLayout *savedInstructionsLayout = new QHBoxLayout();
savedInstructionsLayout->setSpacing(4);
QLabel *savedLabel = new QLabel(Tr::tr("Or select saved:"), this);
savedInstructionsLayout->addWidget(savedLabel);
m_commandsComboBox = new QComboBox(this); m_commandsComboBox = new QComboBox(this);
m_commandsComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); m_commandsComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
m_commandsComboBox->setEditable(true); savedInstructionsLayout->addWidget(m_commandsComboBox);
m_commandsComboBox->setInsertPolicy(QComboBox::NoInsert);
m_commandsComboBox->lineEdit()->setPlaceholderText("Search or select instruction...");
QCompleter *completer = new QCompleter(this);
completer->setCompletionMode(QCompleter::PopupCompletion);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setFilterMode(Qt::MatchContains);
m_commandsComboBox->setCompleter(completer);
instructionsLayout->addWidget(m_commandsComboBox);
m_addCommandButton = new QToolButton(this); m_addCommandButton = new QToolButton(this);
m_addCommandButton->setText("+"); m_addCommandButton->setText("+");
m_addCommandButton->setToolTip(Tr::tr("Add Custom Instruction")); m_addCommandButton->setToolTip(Tr::tr("Add Custom Instruction"));
instructionsLayout->addWidget(m_addCommandButton); savedInstructionsLayout->addWidget(m_addCommandButton);
m_editCommandButton = new QToolButton(this); m_editCommandButton = new QToolButton(this);
m_editCommandButton->setText(""); m_editCommandButton->setText("");
m_editCommandButton->setToolTip(Tr::tr("Edit Custom Instruction")); m_editCommandButton->setToolTip(Tr::tr("Edit Custom Instruction"));
instructionsLayout->addWidget(m_editCommandButton); savedInstructionsLayout->addWidget(m_editCommandButton);
m_deleteCommandButton = new QToolButton(this); m_deleteCommandButton = new QToolButton(this);
m_deleteCommandButton->setText(""); m_deleteCommandButton->setText("");
m_deleteCommandButton->setToolTip(Tr::tr("Delete Custom Instruction")); m_deleteCommandButton->setToolTip(Tr::tr("Delete Custom Instruction"));
instructionsLayout->addWidget(m_deleteCommandButton); savedInstructionsLayout->addWidget(m_deleteCommandButton);
m_openFolderButton = new QToolButton(this); m_openFolderButton = new QToolButton(this);
m_openFolderButton->setText("📁"); m_openFolderButton->setText("📁");
m_openFolderButton->setToolTip(Tr::tr("Open Instructions Folder")); m_openFolderButton->setToolTip(Tr::tr("Open Instructions Folder"));
instructionsLayout->addWidget(m_openFolderButton); savedInstructionsLayout->addWidget(m_openFolderButton);
mainLayout->addLayout(instructionsLayout); mainLayout->addLayout(savedInstructionsLayout);
m_instructionsLabel = new QLabel(Tr::tr("Additional instructions (optional):"), this); QFrame *separator = new QFrame(this);
separator->setFrameShape(QFrame::HLine);
separator->setFrameShadow(QFrame::Sunken);
mainLayout->addWidget(separator);
m_instructionsLabel = new QLabel(Tr::tr("Additional Context (optional):"), this);
mainLayout->addWidget(m_instructionsLabel); mainLayout->addWidget(m_instructionsLabel);
m_textEdit = new QPlainTextEdit(this); m_textEdit = new QPlainTextEdit(this);
m_textEdit->setMinimumHeight(100); m_textEdit->setMinimumHeight(60);
m_textEdit->setPlaceholderText(Tr::tr("Add extra details or modifications to the selected instruction...")); m_textEdit->setPlaceholderText(Tr::tr("Add extra details or context..."));
connect(m_textEdit, &QPlainTextEdit::textChanged, this, &QuickRefactorDialog::updateDialogSize); connect(m_textEdit, &QPlainTextEdit::textChanged, this, &QuickRefactorDialog::updateDialogSize);
connect( connect(
@ -258,9 +262,6 @@ void QuickRefactorDialog::setupUi()
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
mainLayout->addWidget(buttonBox); mainLayout->addWidget(buttonBox);
setTabOrder(m_commandsComboBox, m_textEdit);
setTabOrder(m_textEdit, buttonBox);
} }
void QuickRefactorDialog::createActionButtons() void QuickRefactorDialog::createActionButtons()
@ -297,9 +298,9 @@ QString QuickRefactorDialog::instructions() const
{ {
QString result; QString result;
CustomInstruction instruction = findCurrentInstruction(); QString quickInstruction = m_quickInstructionEdit->text().trimmed();
if (!instruction.id.isEmpty()) { if (!quickInstruction.isEmpty()) {
result = instruction.body; result = quickInstruction;
} }
QString additionalText = m_textEdit->toPlainText().trimmed(); QString additionalText = m_textEdit->toPlainText().trimmed();
@ -315,7 +316,7 @@ QString QuickRefactorDialog::instructions() const
void QuickRefactorDialog::setInstructions(const QString &instructions) void QuickRefactorDialog::setInstructions(const QString &instructions)
{ {
m_textEdit->setPlainText(instructions); m_quickInstructionEdit->setText(instructions);
} }
QuickRefactorDialog::Action QuickRefactorDialog::selectedAction() const QuickRefactorDialog::Action QuickRefactorDialog::selectedAction() const
@ -325,17 +326,21 @@ QuickRefactorDialog::Action QuickRefactorDialog::selectedAction() const
bool QuickRefactorDialog::eventFilter(QObject *watched, QEvent *event) bool QuickRefactorDialog::eventFilter(QObject *watched, QEvent *event)
{ {
if (watched == m_textEdit && event->type() == QEvent::KeyPress) { if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) { if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
if (watched == m_quickInstructionEdit) {
accept();
return true;
} else if (watched == m_textEdit) {
if (keyEvent->modifiers() & Qt::ShiftModifier) { if (keyEvent->modifiers() & Qt::ShiftModifier) {
return false; return false;
} }
accept(); accept();
return true; return true;
} }
} }
}
return QDialog::eventFilter(watched, event); return QDialog::eventFilter(watched, event);
} }
@ -343,8 +348,8 @@ void QuickRefactorDialog::useLastInstructions()
{ {
if (!m_lastInstructions.isEmpty()) { if (!m_lastInstructions.isEmpty()) {
m_commandsComboBox->setCurrentIndex(0); m_commandsComboBox->setCurrentIndex(0);
m_commandsComboBox->clearEditText(); // Clear search text m_quickInstructionEdit->setText(m_lastInstructions);
m_textEdit->setPlainText(m_lastInstructions); m_textEdit->clear();
m_selectedAction = Action::RepeatLast; m_selectedAction = Action::RepeatLast;
} }
accept(); accept();
@ -353,10 +358,10 @@ void QuickRefactorDialog::useLastInstructions()
void QuickRefactorDialog::useImproveCodeTemplate() void QuickRefactorDialog::useImproveCodeTemplate()
{ {
m_commandsComboBox->setCurrentIndex(0); m_commandsComboBox->setCurrentIndex(0);
m_commandsComboBox->clearEditText(); // Clear search text m_quickInstructionEdit->setText(Tr::tr(
m_textEdit->setPlainText(Tr::tr(
"Improve the selected code by enhancing readability, efficiency, and maintainability. " "Improve the selected code by enhancing readability, efficiency, and maintainability. "
"Follow best practices for C++/Qt and fix any potential issues.")); "Follow best practices for C++/Qt and fix any potential issues."));
m_textEdit->clear();
m_selectedAction = Action::ImproveCode; m_selectedAction = Action::ImproveCode;
accept(); accept();
} }
@ -364,11 +369,11 @@ void QuickRefactorDialog::useImproveCodeTemplate()
void QuickRefactorDialog::useAlternativeSolutionTemplate() void QuickRefactorDialog::useAlternativeSolutionTemplate()
{ {
m_commandsComboBox->setCurrentIndex(0); m_commandsComboBox->setCurrentIndex(0);
m_commandsComboBox->clearEditText(); // Clear search text m_quickInstructionEdit->setText(
m_textEdit->setPlainText(
Tr::tr("Suggest an alternative implementation approach for the selected code. " Tr::tr("Suggest an alternative implementation approach for the selected code. "
"Provide a different solution that might be cleaner, more efficient, " "Provide a different solution that might be cleaner, more efficient, "
"or uses different Qt/C++ patterns or idioms.")); "or uses different Qt/C++ patterns or idioms."));
m_textEdit->clear();
m_selectedAction = Action::AlternativeSolution; m_selectedAction = Action::AlternativeSolution;
accept(); accept();
} }
@ -420,29 +425,22 @@ void QuickRefactorDialog::updateDialogSize()
void QuickRefactorDialog::loadCustomCommands() void QuickRefactorDialog::loadCustomCommands()
{ {
m_commandsComboBox->clear(); m_commandsComboBox->clear();
m_commandsComboBox->addItem("", QString()); // Empty item for no selection m_commandsComboBox->addItem(Tr::tr("-- Select saved instruction --"), QString());
auto &manager = CustomInstructionsManager::instance(); auto &manager = CustomInstructionsManager::instance();
const QVector<CustomInstruction> &instructions = manager.instructions(); const QVector<CustomInstruction> &instructions = manager.instructions();
QStringList instructionNames;
int defaultInstructionIndex = -1; int defaultInstructionIndex = -1;
for (int i = 0; i < instructions.size(); ++i) { for (int i = 0; i < instructions.size(); ++i) {
const CustomInstruction &instruction = instructions[i]; const CustomInstruction &instruction = instructions[i];
m_commandsComboBox->addItem(instruction.name, instruction.id); m_commandsComboBox->addItem(instruction.name, instruction.id);
instructionNames.append(instruction.name);
if (instruction.isDefault) { if (instruction.isDefault) {
defaultInstructionIndex = i + 1; defaultInstructionIndex = i + 1;
} }
} }
if (m_commandsComboBox->completer()) {
QStringListModel *model = new QStringListModel(instructionNames, this);
m_commandsComboBox->completer()->setModel(model);
}
if (defaultInstructionIndex > 0) { if (defaultInstructionIndex > 0) {
m_commandsComboBox->setCurrentIndex(defaultInstructionIndex); m_commandsComboBox->setCurrentIndex(defaultInstructionIndex);
} }
@ -454,34 +452,30 @@ void QuickRefactorDialog::loadCustomCommands()
CustomInstruction QuickRefactorDialog::findCurrentInstruction() const CustomInstruction QuickRefactorDialog::findCurrentInstruction() const
{ {
QString currentText = m_commandsComboBox->currentText().trimmed(); int currentIndex = m_commandsComboBox->currentIndex();
if (currentText.isEmpty()) { if (currentIndex <= 0) {
return CustomInstruction();
}
QString instructionId = m_commandsComboBox->itemData(currentIndex).toString();
if (instructionId.isEmpty()) {
return CustomInstruction(); return CustomInstruction();
} }
auto &manager = CustomInstructionsManager::instance(); auto &manager = CustomInstructionsManager::instance();
const QVector<CustomInstruction> &instructions = manager.instructions();
for (const CustomInstruction &instruction : instructions) {
if (instruction.name == currentText) {
return instruction;
}
}
int currentIndex = m_commandsComboBox->currentIndex();
if (currentIndex > 0) {
QString instructionId = m_commandsComboBox->itemData(currentIndex).toString();
if (!instructionId.isEmpty()) {
return manager.getInstructionById(instructionId); return manager.getInstructionById(instructionId);
}
}
return CustomInstruction();
} }
void QuickRefactorDialog::onCommandSelected(int index) void QuickRefactorDialog::onCommandSelected(int index)
{ {
Q_UNUSED(index); if (index <= 0) {
return;
}
CustomInstruction instruction = findCurrentInstruction();
if (!instruction.id.isEmpty()) {
m_quickInstructionEdit->setText(instruction.body);
}
} }
void QuickRefactorDialog::onAddCustomCommand() void QuickRefactorDialog::onAddCustomCommand()
@ -494,9 +488,12 @@ void QuickRefactorDialog::onAddCustomCommand()
if (manager.saveInstruction(instruction)) { if (manager.saveInstruction(instruction)) {
loadCustomCommands(); loadCustomCommands();
m_commandsComboBox->setCurrentText(instruction.name); for (int i = 0; i < m_commandsComboBox->count(); ++i) {
if (m_commandsComboBox->itemData(i).toString() == instruction.id) {
m_textEdit->clear(); m_commandsComboBox->setCurrentIndex(i);
break;
}
}
} else { } else {
QMessageBox::warning( QMessageBox::warning(
this, this,
@ -523,8 +520,13 @@ void QuickRefactorDialog::onEditCustomCommand()
if (manager.saveInstruction(updatedInstruction)) { if (manager.saveInstruction(updatedInstruction)) {
loadCustomCommands(); loadCustomCommands();
m_commandsComboBox->setCurrentText(updatedInstruction.name);
m_textEdit->clear(); for (int i = 0; i < m_commandsComboBox->count(); ++i) {
if (m_commandsComboBox->itemData(i).toString() == updatedInstruction.id) {
m_commandsComboBox->setCurrentIndex(i);
break;
}
}
} else { } else {
QMessageBox::warning( QMessageBox::warning(
this, this,
@ -555,7 +557,7 @@ void QuickRefactorDialog::onDeleteCustomCommand()
if (manager.deleteInstruction(instruction.id)) { if (manager.deleteInstruction(instruction.id)) {
loadCustomCommands(); loadCustomCommands();
m_commandsComboBox->setCurrentIndex(0); m_commandsComboBox->setCurrentIndex(0);
m_commandsComboBox->clearEditText(); m_quickInstructionEdit->clear();
} else { } else {
QMessageBox::warning( QMessageBox::warning(
this, this,

View File

@ -27,6 +27,8 @@ class QPlainTextEdit;
class QToolButton; class QToolButton;
class QLabel; class QLabel;
class QComboBox; class QComboBox;
class QLineEdit;
class QFrame;
namespace QodeAssist { namespace QodeAssist {
@ -70,6 +72,7 @@ private:
void createActionButtons(); void createActionButtons();
CustomInstruction findCurrentInstruction() const; CustomInstruction findCurrentInstruction() const;
QLineEdit *m_quickInstructionEdit;
QPlainTextEdit *m_textEdit; QPlainTextEdit *m_textEdit;
QToolButton *m_repeatButton; QToolButton *m_repeatButton;
QToolButton *m_improveButton; QToolButton *m_improveButton;