]> Shamusworld >> Repos - architektonas/blob - src/promptlineedit.cpp
Miscellaneous fixes/updates:
[architektonas] / src / promptlineedit.cpp
1 //
2 // promptlineedit.cpp: QLineEdit widget with uneditable (by user) prompt
3 //
4 // Part of the Architektonas Project
5 // (C) 2021 Underground Software
6 // See the README and GPLv3 files for licensing and warranty information
7 //
8 // JLH = James Hammons <jlhamm@acm.org>
9 //
10 // WHO  WHEN        WHAT
11 // ---  ----------  ------------------------------------------------------------
12 // JLH  11/27/2021  Created this file
13 //
14
15 #include "promptlineedit.h"
16
17 PromptLineEdit::PromptLineEdit(QWidget * parent/*= NULL*/): QLineEdit(parent)
18 {
19         connect(this, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(HandleCursorMoved(int, int)));
20         connect(this, SIGNAL(textChanged(const QString &)), this, SLOT(HandleTextChanged(const QString &)));
21         connect(this, SIGNAL(selectionChanged(void)), this, SLOT(HandleSelectionChanged(void)));
22
23         SetPrompt("ATNS");
24 }
25
26 PromptLineEdit::~PromptLineEdit()
27 {
28 }
29
30 void PromptLineEdit::HandleCursorMoved(int /*old*/, int neu)
31 {
32         if (neu < prompt.length())
33                 setCursorPosition(prompt.length());
34 }
35
36 void PromptLineEdit::HandleTextChanged(const QString & s)
37 {
38         if (s.length() == 0)
39                 setText(prompt);
40 }
41
42 void PromptLineEdit::HandleSelectionChanged(void)
43 {
44         int selStart = selectionStart();
45         int selEnd = selectionEnd();
46         int promptLen = prompt.length();
47
48         if (selStart == -1)
49                 return;
50
51         if (selStart < promptLen)
52                 setSelection(selEnd, -(selEnd - promptLen));
53 }
54
55 void PromptLineEdit::keyPressEvent(QKeyEvent * event)
56 {
57         if (event->key() == Qt::Key_Backspace)
58         {
59                 if (cursorPosition() <= prompt.length())
60                 {
61                         event->accept();
62                         return;
63                 }
64         }
65
66         QLineEdit::keyPressEvent(event);
67 }
68
69 QString PromptLineEdit::text(void) const
70 {
71         QString s = QLineEdit::text();
72
73         return s.right(s.length() - prompt.length());
74 }
75
76 void PromptLineEdit::SetPrompt(QString s)
77 {
78         prompt = s + ": ";
79         setText(prompt);
80 }