Skip to content

Commit

Permalink
Adding functionality to replace/append selected text w/ Wingman answer
Browse files Browse the repository at this point in the history
  • Loading branch information
dvorka committed Jan 28, 2024
1 parent c9221f4 commit 8297468
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 13 deletions.
12 changes: 12 additions & 0 deletions app/src/qt/dialogs/wingman_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ WingmanDialog::WingmanDialog(QWidget* parent)
askButton = new QPushButton(tr("&Run Prompt"), this);
copyButton = new QPushButton(tr("C&opy"), this);
copyButton->setToolTip(tr("Copy last answer to the clipboard"));
appendButton = new QPushButton(tr("&Append"), this);
appendButton->setToolTip(tr("Append the last Wingman answer to the Note text after the cursor"));
replaceButton = new QPushButton(tr("&Replace"), this);
replaceButton->setToolTip(tr("Replace (selected) Note text with the last Wingman answer"));
togglePromptSourceButton = new QPushButton(tr("Show Predefined &Prompts"), this);
toggleContextButton = new QPushButton(tr("Show &Context"), this);
closeButton = new QPushButton(tr("Close"), this);
Expand Down Expand Up @@ -124,6 +128,8 @@ WingmanDialog::WingmanDialog(QWidget* parent)
buttonLayout->addWidget(togglePromptSourceButton);
buttonLayout->addStretch(1);
buttonLayout->addWidget(copyButton);
buttonLayout->addWidget(appendButton);
buttonLayout->addWidget(replaceButton);
buttonLayout->addWidget(askButton);
buttonLayout->addStretch();

Expand Down Expand Up @@ -185,16 +191,22 @@ void WingmanDialog::initForMode(WingmanDialogModes mode)
prompts = &outlinePrompts;
contextTypeEdit->setText(tr("notebook"));
contextEdit->setText(tr("<Notebook text>"));
appendButton->hide();
replaceButton->hide();
break;
case WingmanDialogModes::WINGMAN_DIALOG_MODE_NOTE:
prompts = &notePrompts;
contextTypeEdit->setText(tr("note"));
contextEdit->setText(tr("<Note text>"));
appendButton->hide();
replaceButton->hide();
break;
case WingmanDialogModes::WINGMAN_DIALOG_MODE_TEXT:
prompts = &textPrompts;
contextNameEdit->clear();
contextEdit->clear();
appendButton->show();
replaceButton->show();
break;
}

Expand Down
6 changes: 6 additions & 0 deletions app/src/qt/dialogs/wingman_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class WingmanDialog : public QDialog // TODO rename to WingmanDialog

QPushButton* askButton;
QPushButton* copyButton;
QPushButton* appendButton;
QPushButton* replaceButton;

QPushButton* togglePromptSourceButton;
QPushButton* toggleContextButton;
Expand Down Expand Up @@ -124,6 +126,10 @@ class WingmanDialog : public QDialog // TODO rename to WingmanDialog
bool error=false);

std::string getPrompt();
std::string getLastAnswer() { return this->lastAnswer; }

QPushButton* getAppendButton() const { return appendButton; }
QPushButton* getReplaceButton() const { return replaceButton; }

WingmanDialogModes getContextType() const {
return this->mode;
Expand Down
88 changes: 88 additions & 0 deletions app/src/qt/main_window_presenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ MainWindowPresenter::MainWindowPresenter(MainWindowView& view)
QObject::connect(
wingmanDialog, SIGNAL(signalRunWingman()),
this, SLOT(slotRunWingmanFromDialog()));
QObject::connect(
wingmanDialog->getAppendButton(), SIGNAL(clicked()),
this, SLOT(slotWingmanAppendFromDialog()));
QObject::connect(
wingmanDialog->getReplaceButton(), SIGNAL(clicked()),
this, SLOT(slotWingmanReplaceFromDialog()));
QObject::connect(
// TODO remove / comment
runToolDialog->getRunButton(), SIGNAL(clicked()),
Expand Down Expand Up @@ -2261,6 +2267,88 @@ void MainWindowPresenter::slotRunWingmanFromDialog()
}
}

void MainWindowPresenter::slotWingmanAppendFromDialog()
{
if(this->wingmanDialog->getLastAnswer().length()) {
if(orloj->isFacetActive(OrlojPresenterFacets::FACET_EDIT_OUTLINE_HEADER)) {
if(orloj->getOutlineHeaderEdit()->getView()->getHeaderEditor()->getSelectedText().size()) {
orloj->getOutlineHeaderEdit()->getView()->getHeaderEditor()->appendAfterSelectedText(
this->wingmanDialog->getLastAnswer());
statusBar->showInfo(QString(tr("Wingman's answer appended after selected text in Notebook header.")));
this->wingmanDialog->hide();
} else{
QMessageBox::critical(
&view,
tr("Wingman Action Error"),
tr("Unable to append text in Notebook header editor - no text selected.")
);
}
return;
} else if(orloj->isFacetActive(OrlojPresenterFacets::FACET_EDIT_NOTE)) {
if(orloj->getNoteEdit()->getView()->getNoteEditor()->getSelectedText().size()) {
orloj->getNoteEdit()->getView()->getNoteEditor()->appendAfterSelectedText(
this->wingmanDialog->getLastAnswer());
statusBar->showInfo(QString(tr("Wingman's answer appended after selected text in the Note editor.")));
this->wingmanDialog->hide();
} else{
QMessageBox::critical(
&view,
tr("Wingman Action Error"),
tr("Unable to append text in Note editor - no text selected.")
);
}
return;
} else {
statusBar->showInfo(
QString(
tr("Unable to append after selected text with Wingman's answer in non-edit perspective.")));
return;
}
}
statusBar->showInfo(QString(tr("No answer from Wingman to append after selected text - run a prompt.")));
}

void MainWindowPresenter::slotWingmanReplaceFromDialog()
{
if(this->wingmanDialog->getLastAnswer().length()) {
if(orloj->isFacetActive(OrlojPresenterFacets::FACET_EDIT_OUTLINE_HEADER)) {
if(orloj->getOutlineHeaderEdit()->getView()->getHeaderEditor()->getSelectedText().size()) {
orloj->getOutlineHeaderEdit()->getView()->getHeaderEditor()->replaceSelectedText(
this->wingmanDialog->getLastAnswer());
statusBar->showInfo(QString(tr("Wingman's answer replaced selected text in Notebook header.")));
this->wingmanDialog->hide();
} else{
QMessageBox::critical(
&view,
tr("Wingman Action Error"),
tr("Unable to replace Notebook header text - no text selected.")
);
}
return;
} else if(orloj->isFacetActive(OrlojPresenterFacets::FACET_EDIT_NOTE)) {
if(orloj->getNoteEdit()->getView()->getNoteEditor()->getSelectedText().size()) {
orloj->getNoteEdit()->getView()->getNoteEditor()->replaceSelectedText(
this->wingmanDialog->getLastAnswer());
statusBar->showInfo(QString(tr("Wingman's answer replaced selected text in Note text.")));
this->wingmanDialog->hide();
} else{
QMessageBox::critical(
&view,
tr("Wingman Action Error"),
tr("Unable to replace Note text - no text selected.")
);
}
return;
} else {
statusBar->showInfo(
QString(
tr("Unable to replace selected text with Wingman's answer in non-edit perspective.")));
return;
}
}
statusBar->showInfo(QString(tr("No answer from Wingman to replace selected text - run a prompt.")));
}

void MainWindowPresenter::doActionOutlineOrNoteNew()
{
if(orloj->isFacetActive(OrlojPresenterFacets::FACET_VIEW_OUTLINE_HEADER)
Expand Down
2 changes: 2 additions & 0 deletions app/src/qt/main_window_presenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ public slots:
// wingman
void handleActionWingman();
void slotRunWingmanFromDialog();
void slotWingmanAppendFromDialog();
void slotWingmanReplaceFromDialog();
// TODO remake to CLI or REMOVE tools toolbar
void handleLeftToolbarAction(std::string selectedTool);
void doActionArxivToolbar();
Expand Down
35 changes: 22 additions & 13 deletions app/src/qt/note_editor_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ void NoteEditorView::dragMoveEvent(QDragMoveEvent* event)
event->acceptProposedAction();
}

void NoteEditorView::replaceSelectedText(const std::string& text)
{
QTextCursor cursor = textCursor();
cursor.removeSelectedText();
cursor.insertText(QString::fromStdString(text));
setTextCursor(cursor);
}

void NoteEditorView::appendAfterSelectedText(const std::string& phrase) {
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::EndOfBlock);
cursor.insertText(" " + QString::fromStdString(phrase));
}

/*
* Formatting
*/
Expand Down Expand Up @@ -575,23 +589,18 @@ QString NoteEditorView::getToolPhrase()
{
QString phrase{};

// get PHRASE: selection > the word under the cursor
QString selection{textCursor().selectedText()};
if(selection.size()) {
phrase = selection;
} else {
MF_DEBUG(
"Run tool: getting the phrase under the cursor..." << endl);
phrase = getCompletionPrefix();
MF_DEBUG(
"Run tool: phrase under the cursor '" << phrase.toStdString()
<< "'" << endl);
// if there is no selection, attempt to select the word under the cursor
if(textCursor().selectedText().size() == 0) {
// select the word under cursor
QTextCursor cursor = textCursor();
cursor.select(QTextCursor::WordUnderCursor);
setTextCursor(cursor);
}

return phrase;
// get PHRASE from selection
return QString{textCursor().selectedText()};
}


void NoteEditorView::slotStartRunTool()
{
QString phrase = getToolPhrase();
Expand Down
2 changes: 2 additions & 0 deletions app/src/qt/note_editor_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class NoteEditorView : public QPlainTextEdit

// formatting
QString getSelectedText() const { return textCursor().selectedText(); }
void replaceSelectedText(const std::string &text);
void appendAfterSelectedText(const std::string& phrase);
void wrapSelectedText(const QString &tag) { wrapSelectedText(tag,""); }
void wrapSelectedText(const QString &tag, const QString &endTag);
void removeSelectedText() { textCursor().removeSelectedText(); }
Expand Down
1 change: 1 addition & 0 deletions lib/src/config/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Configuration::Configuration()
uiFullOPreview{DEFAULT_FULL_O_PREVIEW},
uiShowToolbar{DEFAULT_UI_SHOW_TOOLBAR},
uiExpertMode{DEFAULT_UI_EXPERT_MODE},
uiAppFontSize{DEFAULT_UI_APP_FONT_SIZE},
uiDistractionFreeMode{},
uiHoistedMode{},
uiLiveNotePreview{DEFAULT_UI_LIVE_NOTE_PREVIEW},
Expand Down

0 comments on commit 8297468

Please sign in to comment.