-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgethelpers.cpp
More file actions
333 lines (276 loc) · 9.57 KB
/
widgethelpers.cpp
File metadata and controls
333 lines (276 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "widgethelpers.h"
#include <QVariant>
#include <QWidget>
#include <QSqlQuery>
#include <QSqlError>
#include <QApplication>
#include <QStyle>
#include <QSqlTableModel>
#include <QSortFilterProxyModel>
#include <QTableView>
#include <QPushButton>
#include <QMetaObject>
#include <QMetaProperty>
#include <QRadioButton>
#include <QButtonGroup>
#include <QComboBox>
#include <QCheckBox>
#include <QDateEdit>
#include <QLabel>
#include <QLineEdit>
#ifndef Q_NULLPTR
#define Q_NULLPTR NULL
#endif
namespace
{
const char* VALUE_PROP = "v";
QByteArray userPropertyName( const QObject* const inputBox )
{
return inputBox->metaObject()->userProperty().name();
}
}
const char* WidgetHelpers::COLUMN_NAME_PROP = "c";
const char* WidgetHelpers::TABLE_NAME_PROP = "t";
QSqlRecord WidgetHelpers::addRecord( const QString& tableName, const QVariant& recordId )
{
QSqlQuery query;
query.prepare( QString( "SELECT * FROM %1 WHERE id = :id " ).arg( tableName ) );
query.bindValue( ":id", recordId );
EXEC_SQL_QUERY( query );
query.first();
QSqlRecord record = query.record();
m_tablesRecords.insert( tableName, record );
return record;
}
void WidgetHelpers::setupForm( QWidget* formPointer, const QString& defaultTableName )
{
if( m_defaultTableName.isEmpty() )
m_defaultTableName = defaultTableName;
const QMetaMethod saveDataSl = metaObject()->method(
metaObject()->indexOfSlot( "saveDataSlot()" ) );
foreach( QWidget* w, formPointer->findChildren< QWidget * >() )
{
const QVariant columnName( w->property( COLUMN_NAME_PROP ) );
if( columnName.isNull() )
continue; // Not need to set value
m_inputsList << w;
QString tablePropStr( w->property( TABLE_NAME_PROP ).toString() );
if( tablePropStr.isEmpty() && columnName.isValid() ) // In case of defailt table
{
tablePropStr = defaultTableName;
w->setProperty( TABLE_NAME_PROP, defaultTableName ); // Assign default
}
const QMetaMethod signal = w->metaObject()->userProperty().notifySignal();
if( QRadioButton* const button = qobject_cast< QRadioButton* >( w ) )
{
QButtonGroup* grp = button->group();
Q_ASSERT( !"No group for that button" || grp );
grp->setProperty( COLUMN_NAME_PROP, button->property( COLUMN_NAME_PROP ) ); // For saveDataSlot()
grp->setProperty( TABLE_NAME_PROP, button->property( TABLE_NAME_PROP ) );
connect( grp, SIGNAL( buttonClicked( QAbstractButton* ) ), SLOT( saveDataSlot() ) );
}
#if( QT_VERSION < QT_VERSION_CHECK(5, 0, 0) ) // Just different NOTIFY attribute in different Qt versions. Not sure with version crireria exactly
else if( QComboBox* const box = qobject_cast< QComboBox* >( w ) )
{
connect( box, SIGNAL( currentIndexChanged( int ) ), SLOT( saveDataSlot() ) );
}
#endif
else if( QLineEdit* edit = qobject_cast< QLineEdit* >( w ) )
{
connect( edit, SIGNAL( editingFinished() ), this, SLOT( saveDataSlot() ) ); // notifySignal() made UI slow
}
else if( signal.enclosingMetaObject() != Q_NULLPTR )
{
connect( w, signal, this, saveDataSl );
}
else if( QLabel* const box = qobject_cast< QLabel* >( w ) )
{
Q_UNUSED( box ); // No need save data from QLabel
}
else
Q_ASSERT( !"Please implement that widjet type!" );
}
}
void WidgetHelpers::fillForm(const QVariant& tableRecordId, const QString &tableName /*= QString::null */)
{
const bool setFieldsEmpty = !tableRecordId.isValid();
const QString currentTable( tableName.isEmpty() ? m_defaultTableName : tableName );
if( !setFieldsEmpty )
addRecord( currentTable, tableRecordId );
foreach( QWidget* w, m_inputsList )
{
const QString tablePropStr( w->property( TABLE_NAME_PROP ).toString() );
const QString columnName( w->property( COLUMN_NAME_PROP ).toString() );
const QSqlRecord& record = m_tablesRecords.value( tablePropStr );
if( QString::compare( currentTable, tablePropStr, Qt::CaseInsensitive ) == 0 )
{
#ifdef QT_DEBUG
const char* ORIGINAL_TOOLTIP_PROP = "original_tooltip"; // No difference what name
const char* DEBUG_TOOLTIP_PREFIX = "t = "; // Prefix visible on debug tooltip
QString debugToolTip
, widgetToolTip( w->toolTip() )
, originalToolTip( w->property( ORIGINAL_TOOLTIP_PROP ).toString() );
if( !setFieldsEmpty )
{
debugToolTip = "%1, c = %2, id = %3";
debugToolTip = debugToolTip.arg( DEBUG_TOOLTIP_PREFIX + tablePropStr ).arg( columnName ).arg( record.value( record.indexOf( "id" ) ).toString() );
}
if( !widgetToolTip.startsWith( DEBUG_TOOLTIP_PREFIX ) && !widgetToolTip.isEmpty() && originalToolTip.isEmpty() )
{
w->setProperty( ORIGINAL_TOOLTIP_PROP, widgetToolTip );
originalToolTip = widgetToolTip;
}
if( !originalToolTip.isEmpty() && !debugToolTip.isEmpty() )
originalToolTip += "<br/> -------------------- <br/>";
w->setToolTip( originalToolTip + debugToolTip );
#endif
QVariant value;
if( setFieldsEmpty )
value = QVariant( QString::null );
else
value = record.value( record.indexOf( columnName ) );
setWidgetValue( value, w ); // In some cases can change focus at setVidgetValue()
if( QAbstractButton* btn = qobject_cast< QAbstractButton* >( w ) )
{
QButtonGroup* grp = btn->group();
if( grp )
{
foreach( QAbstractButton* b, grp->buttons() )
{
b->setDisabled( setFieldsEmpty );
}
}
}
w->setDisabled( setFieldsEmpty );
}
}
if( currentTable == m_defaultTableName ) // Need that only one time. When handle default table
{
foreach( QWidget* w, m_additionalWidgetsToDisable )
{
w->setDisabled( setFieldsEmpty );
}
}
}
void WidgetHelpers::setAdditionalDisableWidgets( const QWidgetList& widgets )
{
m_additionalWidgetsToDisable = widgets;
}
void WidgetHelpers::saveDataSlot()
{
const QObject* const sendr = sender();
if( sendr )
{
const QString tableName( sendr->property( TABLE_NAME_PROP ).toString() )
, columnName( sendr->property( COLUMN_NAME_PROP ).toString() );
const QVariant newValue( getWidgetValue( sendr ) ),
id( getTableId( tableName ) ),
oldValue( getFieldValue( tableName, columnName ) );
if( !tableName.isEmpty() && !columnName.isEmpty()
&& newValue != oldValue ) // Prevent old data saving
{
QSqlQuery updateQuery;
updateQuery.prepare( QString( "UPDATE %1 SET %2 = :value WHERE id = :id" ).arg( tableName ).arg( columnName ) );
updateQuery.bindValue( ":value", newValue );
updateQuery.bindValue( ":id", id );
EXEC_SQL_QUERY( updateQuery );
emit dataChanged( tableName, columnName, id, newValue );
}
}
else
Q_ASSERT( !"Please use as SLOT only!" );
}
void WidgetHelpers::setWidgetValue( const QVariant &value, QObject* const inputBox )
{
if( inputBox == Q_NULLPTR )
return;
inputBox->blockSignals( true ); // Prevent unwanted data saving
{
const QByteArray uPropName = userPropertyName( inputBox );
QDateEdit* dateEdit = qobject_cast< QDateEdit* >( inputBox );
if( dateEdit && value.isNull() )
{
dateEdit->setSpecialValueText( " " );
dateEdit->setDate( QDate::fromString( "01/01/0001", "dd/MM/yyyy" ) ); // Little hack for set ampty value
}
else if( QComboBox *box = qobject_cast< QComboBox* >( inputBox ) )
{
box->setDisabled( true ); // Prevent chaning selection at personsTable
const int index = box->findData( value, Qt::UserRole, Qt::MatchExactly );
if( index > -1 )
box->setCurrentIndex( index );
box->setDisabled( false );
}
else if( QRadioButton *box = qobject_cast< QRadioButton* >( inputBox ) )
setWidgetValue( value, box->group() );
else if( !uPropName.isEmpty() )
inputBox->setProperty( uPropName, value );
else if( QButtonGroup* grp = qobject_cast< QButtonGroup* >( inputBox ) )
{
foreach( QAbstractButton* abtn, grp->buttons() ) // QRadioButton in common case
{
if( abtn )
{
if( abtn->property( VALUE_PROP ) == value )
{
abtn->setChecked( true );
break;
}
else
abtn->setChecked( false );
}
}
}
else if( QLabel* box = qobject_cast< QLabel* >( inputBox ) )
box->setText( value.toString() );
else
{
qDebug() << inputBox->metaObject()->className();
Q_ASSERT( !"Please implement that widget type!" );
}
}
inputBox->blockSignals( false );
}
QVariant WidgetHelpers::getWidgetValue( const QObject* const sourceBox )
{
if( sourceBox == Q_NULLPTR )
return QVariant();
QVariant result;
const QByteArray uPropName = userPropertyName( sourceBox );
if( const QComboBox* box = qobject_cast< const QComboBox* >( sourceBox ) )
result = box->itemData( box->currentIndex(), Qt::UserRole );
else if( const QAbstractButton* box = qobject_cast< const QAbstractButton* >( sourceBox ) ) // Another buttons. QCheckBox handling before
result = box->property( VALUE_PROP );
else if( !uPropName.isEmpty() )
result = sourceBox->property( uPropName );
else if( const QButtonGroup* buttonGroup = qobject_cast< const QButtonGroup* >( sourceBox ) )
result = getWidgetValue( buttonGroup->checkedButton() ); // to QAbstractButton
else
{
Q_ASSERT( !"Please implement that widget type!" );
}
return result;
}
QVariant WidgetHelpers::getTableId( const QString& tableName ) const
{
return getFieldValue( tableName, "id" );
}
QVariant WidgetHelpers::getFieldValue( const QString& tableName, const QString& columnName ) const
{
const QSqlRecord& rec = m_tablesRecords.value( tableName );
QVariant value( rec.value( columnName ) );
return value;
}
bool WidgetHelpers::execSqlQuery( QSqlQuery *query, const QString funcInfo )
{
bool queryOk = query->exec();
if( !queryOk )
{
qDebug() << "------- SQL query completed with errors-------";
qDebug() << funcInfo;
qDebug() << query->lastQuery();
qDebug() << query->lastError();
qDebug() << "----------------------------------------------";
}
return queryOk;
}