]> Shamusworld >> Repos - architektonas/blobdiff - src/promptlineedit.cpp
Miscellaneous fixes/updates:
[architektonas] / src / promptlineedit.cpp
diff --git a/src/promptlineedit.cpp b/src/promptlineedit.cpp
new file mode 100644 (file)
index 0000000..f79878d
--- /dev/null
@@ -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 <jlhamm@acm.org>
+//
+// 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);
+}