Line data Source code
1 : // DUNE DAQ modification notice:
2 : // This file has been modified from the original ATLAS dbe source for the DUNE DAQ project.
3 : // Fork baseline commit: dbe-02-12-17 (2022-05-12).
4 : // Renamed since fork: no.
5 :
6 : /// Include Qt Headers
7 : #include <QApplication>
8 : #include <QContextMenuEvent>
9 : #include <QMenu>
10 : #include <QValidator>
11 : /// Include DBE
12 : #include "dbe/CustomLineEdit.hpp"
13 : #include "dbe/StyleUtility.hpp"
14 :
15 0 : dbe::CustomLineEdit::CustomLineEdit(QWidget* parent, bool IsNull, bool IsMulti)
16 : : QLineEdit(parent)
17 0 : , Valid(false)
18 0 : , NullCheck(IsNull)
19 0 : , CheckDefault(true)
20 0 : , PopupMenu(false)
21 0 : , IsMultiValue(IsMulti)
22 0 : , ContextMenu(nullptr)
23 0 : , Dec(nullptr)
24 0 : , Oct(nullptr)
25 0 : , Hex(nullptr)
26 0 : , Validator(nullptr)
27 : {
28 0 : connect(this, SIGNAL(textChanged(QString)), this, SLOT(TryValidate(QString)));
29 0 : this->setFixedHeight(30);
30 : // this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
31 0 : }
32 :
33 0 : dbe::CustomLineEdit::~CustomLineEdit()
34 : {
35 0 : if (Validator != nullptr) {
36 0 : delete Validator;
37 : }
38 0 : }
39 :
40 : void
41 0 : dbe::CustomLineEdit::SetNullCheck(bool IsNull)
42 : {
43 0 : NullCheck = IsNull;
44 :
45 0 : if (NullCheck == false) {
46 0 : setPalette(QApplication::palette(this));
47 : }
48 0 : }
49 :
50 : void
51 0 : dbe::CustomLineEdit::SetMultiCheck(bool IsMulti)
52 : {
53 0 : IsMultiValue = IsMulti;
54 0 : }
55 :
56 : bool
57 0 : dbe::CustomLineEdit::IsNullCheck()
58 : {
59 0 : return NullCheck;
60 : }
61 :
62 : bool
63 0 : dbe::CustomLineEdit::IsValid()
64 : {
65 0 : return Valid;
66 : }
67 :
68 : void
69 0 : dbe::CustomLineEdit::SetCheckDefault(bool Default)
70 : {
71 0 : CheckDefault = Default;
72 0 : }
73 :
74 : void
75 0 : dbe::CustomLineEdit::SetDefaultValue(QString ValueDefault)
76 : {
77 0 : DefaultValue = ValueDefault;
78 0 : }
79 :
80 : void
81 0 : dbe::CustomLineEdit::ValidateText()
82 : {
83 0 : QString Dummy("Dummy");
84 0 : TryValidate(Dummy);
85 0 : }
86 :
87 : void
88 0 : dbe::CustomLineEdit::SetLoadedDefaultFlag(bool Loaded)
89 : {
90 0 : setProperty("loadedDefaults", Loaded);
91 0 : }
92 :
93 : void
94 0 : dbe::CustomLineEdit::SetValidator(QValidator* ValidatorSet)
95 : {
96 0 : Validator = ValidatorSet;
97 0 : }
98 :
99 : void
100 0 : dbe::CustomLineEdit::SetPopupMenu()
101 : {
102 0 : PopupMenu = true;
103 0 : }
104 :
105 : void
106 0 : dbe::CustomLineEdit::CreateActions()
107 : {
108 0 : Dec = new QAction(tr("Dec"), this);
109 0 : Dec->setShortcutContext(Qt::WidgetShortcut);
110 0 : connect(Dec, SIGNAL(triggered()), this, SLOT(EmitDecSlot()));
111 0 : Dec->setCheckable(true);
112 0 : Dec->setChecked(false);
113 0 : ContextMenu->addAction(Dec);
114 :
115 0 : Oct = new QAction(tr("Oct"), this);
116 0 : Oct->setShortcutContext(Qt::WidgetShortcut);
117 0 : connect(Oct, SIGNAL(triggered()), this, SLOT(EmitOctSlot()));
118 0 : Oct->setCheckable(true);
119 0 : Oct->setChecked(false);
120 0 : ContextMenu->addAction(Oct);
121 :
122 0 : Hex = new QAction(tr("Hex"), this);
123 0 : Hex->setShortcutContext(Qt::WidgetShortcut);
124 0 : Hex->setCheckable(true);
125 0 : Hex->setChecked(false);
126 0 : connect(Hex, SIGNAL(triggered()), this, SLOT(EmitHexSlot()));
127 0 : ContextMenu->addAction(Hex);
128 0 : }
129 :
130 : void
131 0 : dbe::CustomLineEdit::TryValidate(QString Dummy)
132 : {
133 0 : Q_UNUSED(Dummy)
134 :
135 0 : QString tmp_st = this->text();
136 0 : int i = 0;
137 :
138 0 : if (tmp_st.isEmpty() && NullCheck) {
139 0 : Valid = false;
140 :
141 0 : if (!IsMultiValue) {
142 0 : this->setPalette(StyleUtility::WarningStatusBarPallete);
143 0 : setToolTip(QString("Field cannot be empty!"));
144 : }
145 :
146 0 : emit StringValidated(); // it is caught by EditStringAttrWidget
147 0 : } else if (Validator != 0) {
148 0 : if ((Validator->validate(tmp_st, i)) == QValidator::Acceptable) {
149 0 : Valid = true;
150 0 : this->setPalette(QApplication::palette(this));
151 0 : setToolTip(QString("This new object ID is unique"));
152 0 : } else if ((Validator->validate(tmp_st, i)) == QValidator::Intermediate) {
153 0 : Valid = false;
154 0 : this->setPalette(StyleUtility::AlertStatusBarPallete);
155 0 : setToolTip(QString("This UID is already used"));
156 : }
157 0 : } else if (Validator == 0) {
158 0 : Valid = true;
159 0 : setPalette(QApplication::palette(this));
160 :
161 0 : if (tmp_st == DefaultValue && CheckDefault) {
162 0 : this->setPalette(StyleUtility::LoadedDefault);
163 : } else {
164 0 : this->setPalette(QApplication::palette(this));
165 : }
166 :
167 0 : emit StringValidated();
168 : }
169 0 : }
170 :
171 : void
172 0 : dbe::CustomLineEdit::EmitDecSlot()
173 : {
174 0 : Dec->setChecked(true);
175 0 : Oct->setChecked(false);
176 0 : Hex->setChecked(false);
177 0 : emit DecChange();
178 0 : }
179 :
180 : void
181 0 : dbe::CustomLineEdit::EmitOctSlot()
182 : {
183 0 : Dec->setChecked(false);
184 0 : Oct->setChecked(true);
185 0 : Hex->setChecked(false);
186 0 : emit OctChange();
187 0 : }
188 :
189 : void
190 0 : dbe::CustomLineEdit::EmitHexSlot()
191 : {
192 0 : Dec->setChecked(false);
193 0 : Oct->setChecked(false);
194 0 : Hex->setChecked(true);
195 0 : emit HexChange();
196 0 : }
197 :
198 : void
199 0 : dbe::CustomLineEdit::contextMenuEvent(QContextMenuEvent* Event)
200 : {
201 0 : if (!PopupMenu) {
202 : return;
203 : }
204 :
205 0 : if (ContextMenu == nullptr) {
206 0 : ContextMenu = new QMenu(this);
207 0 : CreateActions();
208 : }
209 :
210 0 : ContextMenu->exec(Event->globalPos());
211 : }
|