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