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/StyleUtility.hpp"
13 : #include "dbe/string_attr_text_edit.hpp"
14 :
15 0 : dbe::string_attr_text_edit::string_attr_text_edit(QWidget* parent, bool IsNull, bool IsMulti)
16 : : QTextEdit(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()), this, SLOT(TryValidate()));
29 : // this->setFixedHeight ( 30 );
30 : // this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
31 0 : }
32 :
33 0 : dbe::string_attr_text_edit::~string_attr_text_edit()
34 : {
35 0 : if (Validator != nullptr) {
36 0 : delete Validator;
37 : }
38 0 : }
39 :
40 : void
41 0 : dbe::string_attr_text_edit::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::string_attr_text_edit::SetMultiCheck(bool IsMulti)
52 : {
53 0 : IsMultiValue = IsMulti;
54 0 : }
55 :
56 : bool
57 0 : dbe::string_attr_text_edit::IsNullCheck()
58 : {
59 0 : return NullCheck;
60 : }
61 :
62 : bool
63 0 : dbe::string_attr_text_edit::IsValid()
64 : {
65 0 : return Valid;
66 : }
67 :
68 : void
69 0 : dbe::string_attr_text_edit::SetCheckDefault(bool Default)
70 : {
71 0 : CheckDefault = Default;
72 0 : }
73 :
74 : void
75 0 : dbe::string_attr_text_edit::SetDefaultValue(QString ValueDefault)
76 : {
77 0 : DefaultValue = ValueDefault;
78 0 : }
79 :
80 : void
81 0 : dbe::string_attr_text_edit::ValidateText()
82 : {
83 0 : }
84 :
85 : void
86 0 : dbe::string_attr_text_edit::SetLoadedDefaultFlag(bool Loaded)
87 : {
88 0 : setProperty("loadedDefaults", Loaded);
89 0 : }
90 :
91 : void
92 0 : dbe::string_attr_text_edit::SetValidator(QValidator* ValidatorSet)
93 : {
94 0 : Validator = ValidatorSet;
95 0 : }
96 :
97 : void
98 0 : dbe::string_attr_text_edit::SetPopupMenu()
99 : {
100 0 : PopupMenu = true;
101 0 : }
102 :
103 : void
104 0 : dbe::string_attr_text_edit::CreateActions()
105 : {
106 0 : Dec = new QAction(tr("Dec"), this);
107 0 : Dec->setShortcutContext(Qt::WidgetShortcut);
108 0 : connect(Dec, SIGNAL(triggered()), this, SLOT(EmitDecSlot()));
109 0 : Dec->setCheckable(true);
110 0 : Dec->setChecked(false);
111 0 : ContextMenu->addAction(Dec);
112 :
113 0 : Oct = new QAction(tr("Oct"), this);
114 0 : Oct->setShortcutContext(Qt::WidgetShortcut);
115 0 : connect(Oct, SIGNAL(triggered()), this, SLOT(EmitOctSlot()));
116 0 : Oct->setCheckable(true);
117 0 : Oct->setChecked(false);
118 0 : ContextMenu->addAction(Oct);
119 :
120 0 : Hex = new QAction(tr("Hex"), this);
121 0 : Hex->setShortcutContext(Qt::WidgetShortcut);
122 0 : Hex->setCheckable(true);
123 0 : Hex->setChecked(false);
124 0 : connect(Hex, SIGNAL(triggered()), this, SLOT(EmitHexSlot()));
125 0 : ContextMenu->addAction(Hex);
126 0 : }
127 :
128 : void
129 0 : dbe::string_attr_text_edit::TryValidate()
130 : {
131 0 : QString tmp_st = this->toPlainText();
132 0 : int i = 0;
133 :
134 0 : if (tmp_st.isEmpty() && NullCheck) {
135 0 : Valid = false;
136 :
137 0 : if (!IsMultiValue) {
138 0 : this->setPalette(StyleUtility::WarningStatusBarPallete);
139 0 : setToolTip(QString("Field cannot be empty!"));
140 : }
141 :
142 0 : emit StringValidated(); // it is caught by EditStringAttrWidget
143 0 : } else if (Validator != 0) {
144 0 : if ((Validator->validate(tmp_st, i)) == QValidator::Acceptable) {
145 0 : Valid = true;
146 0 : this->setPalette(QApplication::palette(this));
147 0 : setToolTip(QString("This new object ID is unique"));
148 0 : } else if ((Validator->validate(tmp_st, i)) == QValidator::Intermediate) {
149 0 : Valid = false;
150 0 : this->setPalette(StyleUtility::AlertStatusBarPallete);
151 0 : setToolTip(QString("This UID is already used"));
152 : }
153 0 : } else if (Validator == 0) {
154 0 : Valid = true;
155 0 : setPalette(QApplication::palette(this));
156 :
157 0 : if (tmp_st == DefaultValue && CheckDefault) {
158 0 : this->setPalette(StyleUtility::LoadedDefault);
159 : } else {
160 0 : this->setPalette(QApplication::palette(this));
161 : }
162 :
163 0 : emit StringValidated();
164 : }
165 0 : }
166 :
167 : void
168 0 : dbe::string_attr_text_edit::EmitDecSlot()
169 : {
170 0 : Dec->setChecked(true);
171 0 : Oct->setChecked(false);
172 0 : Hex->setChecked(false);
173 0 : emit DecChange();
174 0 : }
175 :
176 : void
177 0 : dbe::string_attr_text_edit::EmitOctSlot()
178 : {
179 0 : Dec->setChecked(false);
180 0 : Oct->setChecked(true);
181 0 : Hex->setChecked(false);
182 0 : emit OctChange();
183 0 : }
184 :
185 : void
186 0 : dbe::string_attr_text_edit::EmitHexSlot()
187 : {
188 0 : Dec->setChecked(false);
189 0 : Oct->setChecked(false);
190 0 : Hex->setChecked(true);
191 0 : emit HexChange();
192 0 : }
193 :
194 : void
195 0 : dbe::string_attr_text_edit::contextMenuEvent(QContextMenuEvent* Event)
196 : {
197 0 : if (!PopupMenu) {
198 : return;
199 : }
200 :
201 0 : if (ContextMenu == nullptr) {
202 0 : ContextMenu = new QMenu(this);
203 0 : CreateActions();
204 : }
205 :
206 0 : ContextMenu->exec(Event->globalPos());
207 : }
|