X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fpromptlineedit.cpp;fp=src%2Fpromptlineedit.cpp;h=f79878d5b34806531eb64cd5fcfb155c8b5fea23;hb=5d8c9e52606315fbfe857f2715b8f051b4f97491;hp=0000000000000000000000000000000000000000;hpb=742d2aa9bb46bce4f690474fa22f5980e175e55e;p=architektonas diff --git a/src/promptlineedit.cpp b/src/promptlineedit.cpp new file mode 100644 index 0000000..f79878d --- /dev/null +++ b/src/promptlineedit.cpp @@ -0,0 +1,80 @@ +// +// promptlineedit.cpp: QLineEdit widget with uneditable (by user) prompt +// +// Part of the Architektonas Project +// (C) 2021 Underground Software +// See the README and GPLv3 files for licensing and warranty information +// +// JLH = James Hammons +// +// WHO WHEN WHAT +// --- ---------- ------------------------------------------------------------ +// JLH 11/27/2021 Created this file +// + +#include "promptlineedit.h" + +PromptLineEdit::PromptLineEdit(QWidget * parent/*= NULL*/): QLineEdit(parent) +{ + connect(this, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(HandleCursorMoved(int, int))); + connect(this, SIGNAL(textChanged(const QString &)), this, SLOT(HandleTextChanged(const QString &))); + connect(this, SIGNAL(selectionChanged(void)), this, SLOT(HandleSelectionChanged(void))); + + SetPrompt("ATNS"); +} + +PromptLineEdit::~PromptLineEdit() +{ +} + +void PromptLineEdit::HandleCursorMoved(int /*old*/, int neu) +{ + if (neu < prompt.length()) + setCursorPosition(prompt.length()); +} + +void PromptLineEdit::HandleTextChanged(const QString & s) +{ + if (s.length() == 0) + setText(prompt); +} + +void PromptLineEdit::HandleSelectionChanged(void) +{ + int selStart = selectionStart(); + int selEnd = selectionEnd(); + int promptLen = prompt.length(); + + if (selStart == -1) + return; + + if (selStart < promptLen) + setSelection(selEnd, -(selEnd - promptLen)); +} + +void PromptLineEdit::keyPressEvent(QKeyEvent * event) +{ + if (event->key() == Qt::Key_Backspace) + { + if (cursorPosition() <= prompt.length()) + { + event->accept(); + return; + } + } + + QLineEdit::keyPressEvent(event); +} + +QString PromptLineEdit::text(void) const +{ + QString s = QLineEdit::text(); + + return s.right(s.length() - prompt.length()); +} + +void PromptLineEdit::SetPrompt(QString s) +{ + prompt = s + ": "; + setText(prompt); +}