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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
|
/* This file is part of the KDE project
Copyright (C) 2004 Cedric Pasteur <[email protected]>
Copyright (C) 2004-2007 Jaroslaw Staniek <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kexiformview.h"
#include <tqobjectlist.h>
#include <tqfileinfo.h>
#include <formeditor/form.h>
#include <formeditor/formIO.h>
#include <formeditor/formmanager.h>
#include <formeditor/objecttree.h>
#include <formeditor/container.h>
#include <formeditor/widgetpropertyset.h>
#include <formeditor/commands.h>
#include <formeditor/widgetwithsubpropertiesinterface.h>
#include <formeditor/objecttree.h>
#include <kexi.h>
#include <kexidialogbase.h>
#include <kexidragobjects.h>
#include <kexidb/field.h>
#include <kexidb/fieldlist.h>
#include <kexidb/connection.h>
#include <kexidb/cursor.h>
#include <kexidb/utils.h>
#include <kexidb/preparedstatement.h>
#include <tableview/kexitableitem.h>
#include <tableview/kexitableviewdata.h>
#include <widget/kexipropertyeditorview.h>
#include <widget/kexiqueryparameters.h>
#include <kexiutils/utils.h>
#include <koproperty/set.h>
#include <koproperty/property.h>
#include "widgets/kexidbform.h"
#include "kexiformscrollview.h"
#include "kexidatasourcepage.h"
#include "widgets/kexidbautofield.h"
#define NO_DSWIZARD
//! @todo #define KEXI_SHOW_SPLITTER_WIDGET
KexiFormView::KexiFormView(KexiMainWindow *mainWin, TQWidget *tqparent,
const char *name, bool /*dbAware*/)
: KexiDataAwareView( mainWin, tqparent, name )
, m_propertySet(0)
, m_resizeMode(KexiFormView::ResizeDefault)
, m_query(0)
, m_queryIsOwned(false)
, m_cursor(0)
// , m_firstFocusWidget(0)
{
m_delayedFormContentsResizeOnShow = 0;
TQHBoxLayout *l = new TQHBoxLayout(this);
l->setAutoAdd(true);
m_scrollView = new KexiFormScrollView(this, viewMode()==Kexi::DataViewMode);
//moved setViewWidget(m_scrollView);
// m_scrollView->show();
m_dbform = new KexiDBForm(m_scrollView->viewport(), m_scrollView, name/*, conn*/);
// m_dbform->resize( m_scrollView->viewport()->size() - TQSize(20, 20) );
// m_dbform->resize(TQSize(400, 300));
m_scrollView->setWidget(m_dbform);
m_scrollView->setResizingEnabled(viewMode()!=Kexi::DataViewMode);
// initForm();
if (viewMode()==Kexi::DataViewMode) {
m_scrollView->recordNavigator()->setRecordHandler( m_scrollView );
m_scrollView->viewport()->setPaletteBackgroundColor(m_dbform->tqpalette().active().background());
//moved to formmanager connect(formPart()->manager(), TQT_SIGNAL(noFormSelected()), TQT_SLOT(slotNoFormSelected()));
}
else
{
connect(KFormDesigner::FormManager::self(), TQT_SIGNAL(propertySetSwitched(KoProperty::Set*, bool, const TQCString&)),
this, TQT_SLOT(slotPropertySetSwitched(KoProperty::Set*, bool, const TQCString&)));
connect(KFormDesigner::FormManager::self(), TQT_SIGNAL(dirty(KFormDesigner::Form *, bool)),
this, TQT_SLOT(slotDirty(KFormDesigner::Form *, bool)));
connect(m_dbform, TQT_SIGNAL(handleDragMoveEvent(TQDragMoveEvent*)),
this, TQT_SLOT(slotHandleDragMoveEvent(TQDragMoveEvent*)));
connect(m_dbform, TQT_SIGNAL(handleDropEvent(TQDropEvent*)),
this, TQT_SLOT(slotHandleDropEvent(TQDropEvent*)));
// action stuff
plugSharedAction("formpart_taborder", KFormDesigner::FormManager::self(), TQT_SLOT(editTabOrder()));
plugSharedAction("formpart_adjust_size", KFormDesigner::FormManager::self(), TQT_SLOT(adjustWidgetSize()));
//TODO plugSharedAction("formpart_pixmap_collection", formPart()->manager(), TQT_SLOT(editFormPixmapCollection()));
//TODO plugSharedAction("formpart_connections", formPart()->manager(), TQT_SLOT(editConnections()));
plugSharedAction("edit_copy", KFormDesigner::FormManager::self(), TQT_SLOT(copyWidget()));
plugSharedAction("edit_cut", KFormDesigner::FormManager::self(), TQT_SLOT(cutWidget()));
plugSharedAction("edit_paste", KFormDesigner::FormManager::self(), TQT_SLOT(pasteWidget()));
plugSharedAction("edit_delete", KFormDesigner::FormManager::self(), TQT_SLOT(deleteWidget()));
plugSharedAction("edit_select_all", KFormDesigner::FormManager::self(), TQT_SLOT(selectAll()));
plugSharedAction("formpart_clear_contents", KFormDesigner::FormManager::self(), TQT_SLOT(clearWidgetContent()));
plugSharedAction("edit_undo", KFormDesigner::FormManager::self(), TQT_SLOT(undo()));
plugSharedAction("edit_redo", KFormDesigner::FormManager::self(), TQT_SLOT(redo()));
plugSharedAction("formpart_layout_menu", KFormDesigner::FormManager::self(), 0 );
plugSharedAction("formpart_layout_hbox", KFormDesigner::FormManager::self(), TQT_SLOT(tqlayoutHBox()) );
plugSharedAction("formpart_layout_vbox", KFormDesigner::FormManager::self(), TQT_SLOT(tqlayoutVBox()) );
plugSharedAction("formpart_layout_grid", KFormDesigner::FormManager::self(), TQT_SLOT(tqlayoutGrid()) );
#ifdef KEXI_SHOW_SPLITTER_WIDGET
plugSharedAction("formpart_layout_hsplitter", KFormDesigner::FormManager::self(), TQT_SLOT(tqlayoutHSplitter()) );
plugSharedAction("formpart_layout_vsplitter", KFormDesigner::FormManager::self(), TQT_SLOT(tqlayoutVSplitter()) );
#endif
plugSharedAction("formpart_break_layout", KFormDesigner::FormManager::self(), TQT_SLOT(breakLayout()) );
plugSharedAction("formpart_format_raise", KFormDesigner::FormManager::self(), TQT_SLOT(bringWidgetToFront()) );
plugSharedAction("formpart_format_lower", KFormDesigner::FormManager::self(), TQT_SLOT(sendWidgetToBack()) );
plugSharedAction("other_widgets_menu", KFormDesigner::FormManager::self(), 0 );
setAvailable("other_widgets_menu", true);
plugSharedAction("formpart_align_menu", KFormDesigner::FormManager::self(), 0 );
plugSharedAction("formpart_align_to_left", KFormDesigner::FormManager::self(),TQT_SLOT(alignWidgetsToLeft()) );
plugSharedAction("formpart_align_to_right", KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToRight()) );
plugSharedAction("formpart_align_to_top", KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToTop()) );
plugSharedAction("formpart_align_to_bottom", KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToBottom()) );
plugSharedAction("formpart_align_to_grid", KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToGrid()) );
plugSharedAction("formpart_adjust_size_menu", KFormDesigner::FormManager::self(), 0 );
plugSharedAction("formpart_adjust_to_fit", KFormDesigner::FormManager::self(), TQT_SLOT(adjustWidgetSize()) );
plugSharedAction("formpart_adjust_size_grid", KFormDesigner::FormManager::self(), TQT_SLOT(adjustSizeToGrid()) );
plugSharedAction("formpart_adjust_height_small", KFormDesigner::FormManager::self(), TQT_SLOT(adjustHeightToSmall()) );
plugSharedAction("formpart_adjust_height_big", KFormDesigner::FormManager::self(), TQT_SLOT(adjustHeightToBig()) );
plugSharedAction("formpart_adjust_width_small", KFormDesigner::FormManager::self(), TQT_SLOT(adjustWidthToSmall()) );
plugSharedAction("formpart_adjust_width_big", KFormDesigner::FormManager::self(), TQT_SLOT(adjustWidthToBig()) );
plugSharedAction("format_font", KFormDesigner::FormManager::self(), TQT_SLOT(changeFont()) );
}
initForm();
KexiDataAwareView::init( m_scrollView, m_scrollView, m_scrollView,
/* skip data-awarness if design mode */ viewMode()==Kexi::DesignViewMode );
connect(this, TQT_SIGNAL(focus(bool)), this, TQT_SLOT(slotFocus(bool)));
/// @todo skip this if ther're no borders
// m_dbform->resize( m_dbform->size()+TQSize(m_scrollView->verticalScrollBar()->width(), m_scrollView->horizontalScrollBar()->height()) );
}
KexiFormView::~KexiFormView()
{
if (m_cursor) {
KexiDB::Connection *conn = tqparentDialog()->mainWin()->project()->dbConnection();
conn->deleteCursor(m_cursor);
m_cursor = 0;
}
deleteQuery();
// Important: form window is closed.
// Set property set to 0 because there is *only one* instance of a property set class
// in Kexi, so the main window wouldn't know the set in fact has been changed.
m_propertySet = 0;
propertySetSwitched();
}
void
KexiFormView::deleteQuery()
{
if (m_cursor) {
KexiDB::Connection *conn = tqparentDialog()->mainWin()->project()->dbConnection();
conn->deleteCursor(m_cursor);
m_cursor = 0;
}
if (m_queryIsOwned) {
delete m_query;
} else {
//! @todo remove this shared query from listened queries list
}
m_query = 0;
}
KFormDesigner::Form*
KexiFormView::form() const
{
if(viewMode()==Kexi::DataViewMode)
return tempData()->previewForm;
else
return tempData()->form;
}
void
KexiFormView::setForm(KFormDesigner::Form *f)
{
if(viewMode()==Kexi::DataViewMode)
tempData()->previewForm = f;
else
tempData()->form = f;
}
void
KexiFormView::initForm()
{
setForm( new KFormDesigner::Form(KexiFormPart::library(), 0, viewMode()==Kexi::DesignViewMode) );
// if (viewMode()==Kexi::DataViewMode)
//form()->setDesignMode(false);
form()->createToplevel(m_dbform, m_dbform);
if (viewMode()==Kexi::DesignViewMode) {
//we want to be informed about executed commands
connect(form()->commandHistory(), TQT_SIGNAL(commandExecuted()),
KFormDesigner::FormManager::self(), TQT_SLOT(slotHistoryCommandExecuted()));
}
const bool newForm = tqparentDialog()->id() < 0;
KexiDB::FieldList *fields = 0;
if (newForm) {
// Show the form wizard if this is a new Form
#ifndef NO_DSWIZARD
KexiDataSourceWizard *w = new KexiDataSourceWizard(mainWin(), (TQWidget*)mainWin(), "datasource_wizard");
if(!w->exec())
fields = 0;
else
fields = w->fields();
delete w;
#endif
}
if(fields)
{
TQDomDocument dom;
formPart()->generateForm(fields, dom);
KFormDesigner::FormIO::loadFormFromDom(form(), m_dbform, dom);
//! @todo handle errors
}
else
loadForm();
if(form()->autoTabStops())
form()->autoAssignTabStops();
//collect tab order information
m_dbform->updateTabStopsOrder(form());
// if (m_dbform->orderedFocusWidgets()->first())
// m_scrollView->setFocusProxy( m_dbform->orderedFocusWidgets()->first() );
KFormDesigner::FormManager::self()->importForm(form(), viewMode()==Kexi::DataViewMode);
m_scrollView->setForm(form());
// m_dbform->updateTabStopsOrder(form());
// TQSize s = m_dbform->size();
// TQApplication::sendPostedEvents();
// m_scrollView->resize( s );
// m_dbform->resize(s);
m_scrollView->refreshContentsSize();
// m_scrollView->refreshContentsSizeLater(true,true);
if (newForm && !fields) {
/* Our form's area will be resized more than once.
Let's resize form widget itself later. */
m_delayedFormContentsResizeOnShow = 3;
}
updateDataSourcePage();
if (!newForm && viewMode()==Kexi::DesignViewMode) {
form()->clearCommandHistory();
}
}
void KexiFormView::updateAutoFieldsDataSource()
{
//! @todo call this when form's data source is changed
//update autofields:
//-inherit captions
//-inherit data types
//(this data has not been stored in the form)
TQString dataSourceString( m_dbform->dataSource() );
TQCString dataSourceMimeTypeString( m_dbform->dataSourceMimeType() );
KexiDB::Connection *conn = tqparentDialog()->mainWin()->project()->dbConnection();
KexiDB::TableOrQuerySchema tableOrQuery(
conn, dataSourceString.latin1(), dataSourceMimeTypeString=="kexi/table");
if (!tableOrQuery.table() && !tableOrQuery.query())
return;
for (KFormDesigner::ObjectTreeDictIterator it(*form()->objectTree()->dict());
it.current(); ++it)
{
KexiDBAutoField *afWidget = dynamic_cast<KexiDBAutoField*>( it.current()->widget() );
if (afWidget) {
KexiDB::QueryColumnInfo *colInfo = tableOrQuery.columnInfo( afWidget->dataSource() );
if (colInfo) {
afWidget->setColumnInfo(colInfo);
//setFieldTypeInternal((int)colInfo->field->type());
//afWidget->setFieldCaptionInternal(colInfo->captionOrAliasOrName());
}
}
}
}
void KexiFormView::updateValuesForSubproperties()
{
//! @todo call this when form's data source is changed
//update autofields:
//-inherit captions
//-inherit data types
//(this data has not been stored in the form)
TQString dataSourceString( m_dbform->dataSource() );
TQCString dataSourceMimeTypeString( m_dbform->dataSourceMimeType() );
KexiDB::Connection *conn = tqparentDialog()->mainWin()->project()->dbConnection();
KexiDB::TableOrQuerySchema tableOrQuery(
conn, dataSourceString.latin1(), dataSourceMimeTypeString=="kexi/table");
if (!tableOrQuery.table() && !tableOrQuery.query())
return;
for (KFormDesigner::ObjectTreeDictIterator it(*form()->objectTree()->dict());
it.current(); ++it)
{
// (delayed) set values for subproperties
//! @todo this could be at the KFD level, but KFD is going to be merged anyway with kexiforms, right?
KFormDesigner::WidgetWithSubpropertiesInterface* subpropIface
= dynamic_cast<KFormDesigner::WidgetWithSubpropertiesInterface*>( it.current()->widget() );
if (subpropIface && subpropIface->subwidget() && it.current()->subproperties() ) {
TQWidget *subwidget = subpropIface->subwidget();
TQMap<TQString, TQVariant>* subprops = it.current()->subproperties();
for (TQMapConstIterator<TQString, TQVariant> subpropIt = subprops->constBegin(); subpropIt!=subprops->constEnd(); ++subpropIt) {
kexipluginsdbg << "KexiFormView::loadForm(): delayed setting of the subproperty: widget="
<< it.current()->widget()->name() << " prop=" << subpropIt.key() << " val=" << subpropIt.data() << endl;
const int count = subwidget->tqmetaObject()->tqfindProperty(subpropIt.key().latin1(), true);
const TQMetaProperty *meta = count!=-1 ? subwidget->tqmetaObject()->property(count, true) : 0;
if (meta) {
// Special case: the property value of type enum (set) but is saved as a string list,
// not as int, so we need to translate it to int. It's been created as such
// by FormIO::readPropertyValue(). Example: "tqalignment" property.
if (meta->isSetType() && subpropIt.data().type()==TQVariant::StringList) {
TQStrList keys;
const TQStringList list( subpropIt.data().toStringList() );
for (TQStringList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it)
keys.append((*it).latin1());
subwidget->setProperty( subpropIt.key().latin1(), meta->keysToValue(keys) );
}
else {
subwidget->setProperty( subpropIt.key().latin1(), subpropIt.data() );
}
}
}//for
}
}
}
//! Used in KexiFormView::loadForm()
static void setUnsavedBLOBIdsForDataViewMode(
TQWidget* widget, const TQMap<TQCString, KexiBLOBBuffer::Id_t>& unsavedLocalBLOBsByName)
{
if (-1 != widget->tqmetaObject()->tqfindProperty("pixmapId")) {
const KexiBLOBBuffer::Id_t blobID = unsavedLocalBLOBsByName[ widget->name() ];
if (blobID > 0)
widget->setProperty("pixmapId", (uint /* KexiBLOBBuffer::Id_t is unsafe and unsupported by TQVariant - will be fixed in TQt4*/)blobID);
}
const TQObjectList list = widget->childrenListObject();
if (list.isEmpty())
return;
for (TQObjectListIterator it(list); it.current(); ++it) {
if (dynamic_cast<TQWidget*>(it.current()))
setUnsavedBLOBIdsForDataViewMode(dynamic_cast<TQWidget*>(it.current()), unsavedLocalBLOBsByName);
}
}
void
KexiFormView::loadForm()
{
//@todo also load m_resizeMode !
kexipluginsdbg << "KexiFormView::loadForm() Loading the form with id : " << tqparentDialog()->id() << endl;
// If we are previewing the Form, use the tempData instead of the form stored in the db
if(viewMode()==Kexi::DataViewMode && !tempData()->tempForm.isNull() )
{
KFormDesigner::FormIO::loadFormFromString(form(), m_dbform, tempData()->tempForm);
setUnsavedBLOBIdsForDataViewMode( m_dbform, tempData()->unsavedLocalBLOBsByName );
updateAutoFieldsDataSource();
updateValuesForSubproperties();
return;
}
// normal load
TQString data;
loadDataBlock(data);
KFormDesigner::FormIO::loadFormFromString(form(), m_dbform, data);
//"autoTabStops" property is loaded -set it within the form tree as well
form()->setAutoTabStops( m_dbform->autoTabStops() );
updateAutoFieldsDataSource();
updateValuesForSubproperties();
}
void
KexiFormView::slotPropertySetSwitched(KoProperty::Set *set, bool forceReload, const TQCString& propertyToSelect)
{
// if (set && tqparentDialog()!=tqparentDialog()->mainWin()->currentDialog())
if (form() != KFormDesigner::FormManager::self()->activeForm())
return; //this is not the current form view
m_propertySet = set;
if (forceReload)
propertySetReloaded(true/*preservePrevSelection*/, propertyToSelect);
else
propertySetSwitched();
formPart()->dataSourcePage()->assignPropertySet(m_propertySet);
}
tristate
KexiFormView::beforeSwitchTo(int mode, bool &dontStore)
{
if (mode!=viewMode()) {
if (viewMode()==Kexi::DataViewMode) {
if (!m_scrollView->acceptRowEdit())
return cancelled;
m_scrollView->beforeSwitchView();
}
else {
//remember our pos
tempData()->scrollViewContentsPos
= TQPoint(m_scrollView->contentsX(), m_scrollView->contentsY());
}
}
// we don't store on db, but in our TempData
dontStore = true;
if(dirty() && (mode == Kexi::DataViewMode) && form()->objectTree()) {
KexiFormPart::TempData* temp = tempData();
if (!KFormDesigner::FormIO::saveFormToString(form(), temp->tempForm))
return false;
//collect blobs from design mode by name for use in data view mode
temp->unsavedLocalBLOBsByName.clear();
for (TQMapConstIterator<TQWidget*, KexiBLOBBuffer::Id_t> it = temp->unsavedLocalBLOBs.constBegin();
it!=temp->unsavedLocalBLOBs.constEnd(); ++it)
{
if (!it.key())
continue;
temp->unsavedLocalBLOBsByName.insert( it.key()->name(), it.data() );
}
}
return true;
}
tristate
KexiFormView::afterSwitchFrom(int mode)
{
if (mode == 0 || mode == Kexi::DesignViewMode) {
if (tqparentDialog()->neverSaved()) {
m_dbform->resize(TQSize(400, 300));
m_scrollView->refreshContentsSizeLater(true,true);
//m_delayedFormContentsResizeOnShow = false;
}
}
if (mode != 0 && mode != Kexi::DesignViewMode) {
//preserve contents pos after switching to other view
m_scrollView->setContentsPos(tempData()->scrollViewContentsPos.x(),
tempData()->scrollViewContentsPos.y());
}
// if (mode == Kexi::DesignViewMode) {
//m_scrollView->move(0,0);
//m_scrollView->setContentsPos(0,0);
//m_scrollView->moveChild(m_dbform, 0, 0);
// }
if((mode == Kexi::DesignViewMode) && viewMode()==Kexi::DataViewMode) {
// The form may have been modified, so we must recreate the preview
delete m_dbform; // also deletes form()
m_dbform = new KexiDBForm(m_scrollView->viewport(), m_scrollView, "KexiDBForm");
m_scrollView->setWidget(m_dbform);
initForm();
//moved to formmanager slotNoFormSelected();
//reset position
m_scrollView->setContentsPos(0,0);
m_dbform->move(0,0);
}
//update tab stops if needed
if (viewMode()==Kexi::DataViewMode) {
// //propagate current "autoTabStops" property value to the form tree
// form()->setAutoTabStops( m_dbform->autoTabStops() );
// if(form()->autoTabStops())
// form()->autoAssignTabStops();
}
else {
//set "autoTabStops" property
m_dbform->setAutoTabStops( form()->autoTabStops() );
}
if (viewMode() == Kexi::DataViewMode) {
//TMP!!
initDataSource();
//handle events for this form
m_scrollView->setMainWidgetForEventHandling(tqparentDialog()->mainWin(), m_dbform);
//set focus on 1st focusable widget which has valid dataSource property set
if (!m_dbform->orderedFocusWidgets()->isEmpty()) {
// TQWidget *www = tqfocusWidget();
//if (Kexi::hasParent(this, tqApp->tqfocusWidget())) {
KexiUtils::unsetFocusWithReason(tqApp->tqfocusWidget(), TQFocusEvent::Tab);
//}
TQPtrListIterator<TQWidget> it(*m_dbform->orderedFocusWidgets());
for (;it.current(); ++it) {
KexiFormDataItemInterface *iface = dynamic_cast<KexiFormDataItemInterface*>(it.current());
if (iface)
kexipluginsdbg << iface->dataSource() << endl;
if (iface && iface->columnInfo() && !iface->isReadOnly()
/*! @todo add option for skipping autoincremented fields */
/* also skip autoincremented fields:*/
&& !iface->columnInfo()->field->isAutoIncrement()) //!iface->dataSource().isEmpty()
break;
}
if (!it.current()) //eventually, focus first available widget if nothing other is available
it.toFirst();
it.current()->setFocus();
KexiUtils::setFocusWithReason(it.current(), TQFocusEvent::Tab);
m_setFocusInternalOnce = it.current();
}
if (m_query)
m_scrollView->selectFirstRow();
}
//dirty only if it's a new object
if (mode == 0)
setDirty( tqparentDialog()->partItem()->neverSaved() );
if (mode==Kexi::DataViewMode && viewMode()==Kexi::DesignViewMode) {
// slotPropertySetSwitched
// emit KFormDesigner::FormManager::self()->propertySetSwitched( KFormDesigner::FormManager::self()->propertySet()->set(), true );
}
return true;
}
void KexiFormView::initDataSource()
{
deleteQuery();
TQString dataSourceString( m_dbform->dataSource() );
TQCString dataSourceMimeTypeString( m_dbform->dataSourceMimeType() );
//! @todo also handle anonymous (not stored) queries provided as statements here
bool ok = !dataSourceString.isEmpty();
/* if (m_previousDataSourceString.lower()==dataSourceString.lower() && !m_cursor) {
//data source changed: delete previous cursor
m_conn->deleteCursor(m_cursor);
m_cursor = 0;
}*/
KexiDB::TableSchema *tableSchema = 0;
KexiDB::Connection *conn = 0;
TQStringList sources;
bool forceReadOnlyDataSource = false;
if (ok) {
// m_previousDataSourceString = dataSourceString;
//collect all data-aware widgets and create query schema
m_scrollView->setMainDataSourceWidget(m_dbform);
sources = m_scrollView->usedDataSources();
conn = tqparentDialog()->mainWin()->project()->dbConnection();
if (dataSourceMimeTypeString.isEmpty() /*table type is the default*/
|| dataSourceMimeTypeString=="kexi/table")
{
tableSchema = conn->tableSchema( dataSourceString );
if (tableSchema) {
/* We will build a _minimum_ query schema from selected table fields. */
m_query = new KexiDB::QuerySchema();
m_queryIsOwned = true;
if (dataSourceMimeTypeString.isEmpty())
m_dbform->setDataSourceMimeType("kexi/table"); //update for compatibility
}
}
if (!tableSchema) {
if (dataSourceMimeTypeString.isEmpty() /*also try to find a query (for compatibility with Kexi<=0.9)*/
|| dataSourceMimeTypeString=="kexi/query")
{
//try to find predefined query schema.
//Note: In general, we could not skip unused fields within this query because
// it can have GROUP BY clause.
//! @todo check if the query could have skipped unused fields (no GROUP BY, no joins, etc.)
m_query = conn->querySchema( dataSourceString );
m_queryIsOwned = false;
ok = m_query != 0;
if (ok && dataSourceMimeTypeString.isEmpty())
m_dbform->setDataSourceMimeType("kexi/query"); //update for compatibility
// query results are read-only
//! @todo There can be read-write queries, e.g. simple "SELECT * FROM...". Add a checking function to KexiDB.
forceReadOnlyDataSource = true;
}
else //no other mime types supported
ok = false;
}
}
TQDict<char> invalidSources(997);
if (ok) {
KexiDB::IndexSchema *pkey = tableSchema ? tableSchema->primaryKey() : 0;
if (pkey) {
//always add all fields from table's primary key
// (don't worry about duplicates, unique list will be computed later)
sources += pkey->names();
kexipluginsdbg << "KexiFormView::initDataSource(): pkey added to data sources: " << pkey->names() << endl;
}
kexipluginsdbg << "KexiFormView::initDataSource(): sources=" << sources << endl;
uint index = 0;
for (TQStringList::ConstIterator it = sources.constBegin();
it!=sources.constEnd(); ++it, index++) {
/*! @todo add expression support */
TQString fieldName( (*it).lower() );
//remove "tablename." if it was prepended
if (tableSchema && fieldName.startsWith( tableSchema->name().lower()+"." ))
fieldName = fieldName.mid(tableSchema->name().length()+1);
//remove "queryname." if it was prepended
if (!tableSchema && fieldName.startsWith( m_query->name().lower()+"." ))
fieldName = fieldName.mid(m_query->name().length()+1);
KexiDB::Field *f = tableSchema ? tableSchema->field(fieldName) : m_query->field(fieldName);
if (!f) {
/*! @todo show error */
//remove this widget from the set of data widgets in the provider
/*! @todo fieldName is ok, but what about expressions? */
invalidSources.insert( fieldName, (const char*)1 ); // += index;
kexipluginsdbg << "KexiFormView::initDataSource(): invalidSources+=" << index << " ("
<< (*it) << ")" << endl;
continue;
}
if (tableSchema) {
if (!m_query->hasField( f )) {
//we're building a new query: add this field
m_query->addField( f );
}
}
}
if (invalidSources.count()==sources.count()) {
//all data sources are invalid! don't execute the query
deleteQuery();
}
else {
KexiDB::debug( m_query->parameters() );
// like in KexiQueryView::executeQuery()
TQValueList<TQVariant> params;
{
KexiUtils::WaitCursorRemover remover;
params = KexiQueryParameters::getParameters(this, *conn->driver(), *m_query, ok);
}
if (ok) //input cancelled
m_cursor = conn->executeQuery( *m_query, params );
}
m_scrollView->tqinvalidateDataSources( invalidSources, m_query );
ok = m_cursor!=0;
}
if (!invalidSources.isEmpty())
m_dbform->updateTabStopsOrder();
if (ok) {
//! @todo PRIMITIVE!! data setting:
//! @todo KexiTableViewData is not great name for data class here... rename/move?
KexiTableViewData* data = new KexiTableViewData(m_cursor);
if (forceReadOnlyDataSource)
data->setReadOnly(true);
data->preloadAllRows();
///*! @todo few backends return result count for free! - no need to reopen() */
// int resultCount = -1;
// if (ok) {
// resultCount = m_conn->resultCount(m_conn->selectStatement(*m_query));
// ok = m_cursor->reopen();
// }
// if (ok)
// ok = ! (!m_cursor->moveFirst() && m_cursor->error());
m_scrollView->setData( data, true /*owner*/ );
}
else
m_scrollView->setData( 0, false );
}
void
KexiFormView::slotDirty(KFormDesigner::Form *dirtyForm, bool isDirty)
{
if(dirtyForm == form())
KexiViewBase::setDirty(isDirty);
}
KexiDB::SchemaData*
KexiFormView::storeNewData(const KexiDB::SchemaData& sdata, bool &cancel)
{
KexiDB::SchemaData *s = KexiViewBase::storeNewData(sdata, cancel);
kexipluginsdbg << "KexiDBForm::storeNewData(): new id:" << s->id() << endl;
if (!s || cancel) {
delete s;
return 0;
}
if (!storeData()) {
//failure: remove object's schema data to avoid garbage
KexiDB::Connection *conn = tqparentDialog()->mainWin()->project()->dbConnection();
conn->removeObject( s->id() );
delete s;
return 0;
}
return s;
}
tristate
KexiFormView::storeData(bool dontAsk)
{
Q_UNUSED(dontAsk);
kexipluginsdbg << "KexiDBForm::storeData(): " << tqparentDialog()->partItem()->name()
<< " [" << tqparentDialog()->id() << "]" << endl;
//-- first, store local BLOBs, so identifiers can be updated
//! @todo remove unused data stored previously
KexiDB::Connection *conn = tqparentDialog()->mainWin()->project()->dbConnection();
KexiDB::TableSchema *blobsTable = conn->tableSchema("kexi__blobs");
if (!blobsTable) { //compatibility check for older Kexi project versions
//! @todo show message about missing kexi__blobs?
return false;
}
// Not all engines accept passing NULL to PKEY o_id, so we're omitting it.
TQStringList blobsFieldNamesWithoutID(blobsTable->names());
blobsFieldNamesWithoutID.pop_front();
KexiDB::FieldList *blobsFieldsWithoutID = blobsTable->subList(blobsFieldNamesWithoutID);
KexiDB::PreparedStatement::Ptr st = conn->prepareStatement(
KexiDB::PreparedStatement::InsertStatement, *blobsFieldsWithoutID);
if (!st) {
delete blobsFieldsWithoutID;
//! @todo show message
return false;
}
KexiBLOBBuffer *blobBuf = KexiBLOBBuffer::self();
KexiFormView *designFormView
= dynamic_cast<KexiFormView*>( tqparentDialog()->viewForMode(Kexi::DesignViewMode) );
if (designFormView) {
for (TQMapConstIterator<TQWidget*, KexiBLOBBuffer::Id_t> it = tempData()->unsavedLocalBLOBs.constBegin();
it!=tempData()->unsavedLocalBLOBs.constEnd(); ++it)
{
if (!it.key()) {
kexipluginswarn << "KexiFormView::storeData(): it.key()==0 !" << endl;
continue;
}
kexipluginsdbg << "name=" << it.key()->name() << " dataID=" << it.data() << endl;
KexiBLOBBuffer::Handle h( blobBuf->objectForId(it.data(), /*!stored*/false) );
if (!h)
continue; //no BLOB assigned
TQString originalFileName(h.originalFileName());
TQFileInfo fi(originalFileName);
TQString caption(fi.baseName().tqreplace('_', " ").simplifyWhiteSpace());
if (st) {
*st /* << NO, (pgsql doesn't support this):TQVariant()*/ /*id*/
<< h.data() << originalFileName << caption
<< h.mimeType() << (uint)/*! @todo unsafe */h.folderId();
if (!st->execute()) {
delete blobsFieldsWithoutID;
kexipluginsdbg << " execute error" << endl;
return false;
}
}
delete blobsFieldsWithoutID;
blobsFieldsWithoutID=0;
const TQ_ULLONG storedBLOBID = conn->lastInsertedAutoIncValue("o_id", "kexi__blobs");
if ((TQ_ULLONG)-1 == storedBLOBID) {
//! @todo show message?
return false;
}
kexipluginsdbg << " storedDataID=" << storedBLOBID << endl;
h.setStoredWidthID((KexiBLOBBuffer::Id_t /*unsafe - will be fixed in TQt4*/)storedBLOBID);
//set widget's internal property so it can be saved...
const TQVariant oldStoredPixmapId( it.key()->property("storedPixmapId") );
it.key()->setProperty("storedPixmapId",
TQVariant((uint /* KexiBLOBBuffer::Id_t is unsafe and unsupported by TQVariant - will be fixed in TQt4*/)storedBLOBID));
KFormDesigner::ObjectTreeItem *widgetItem = designFormView->form()->objectTree()->lookup(it.key()->name()); //form()->objectTree()->lookup(it.key()->name());
if (widgetItem)
widgetItem->addModifiedProperty( "storedPixmapId", oldStoredPixmapId );
else
kexipluginswarn << "KexiFormView::storeData(): no '" << widgetItem->name() << "' widget found within a form" << endl;
}
}
//-- now, save form's XML
TQString data;
if (!KFormDesigner::FormIO::saveFormToString(tempData()->form, data))
return false;
if (!storeDataBlock(data))
return false;
//all blobs are now saved
tempData()->unsavedLocalBLOBs.clear();
tempData()->tempForm = TQString();
return true;
}
#if 0
/// Action stuff /////////////////
void
KexiFormView::slotWidgetSelected(KFormDesigner::Form *f, bool multiple)
{
if(f != form())
return;
enableFormActions();
// Enable edit actions
setAvailable("edit_copy", true);
setAvailable("edit_cut", true);
setAvailable("edit_clear", true);
// 'Align Widgets' menu
setAvailable("formpart_align_menu", multiple);
setAvailable("formpart_align_to_left", multiple);
setAvailable("formpart_align_to_right", multiple);
setAvailable("formpart_align_to_top", multiple);
setAvailable("formpart_align_to_bottom", multiple);
setAvailable("formpart_adjust_size_menu", true);
setAvailable("formpart_adjust_width_small", multiple);
setAvailable("formpart_adjust_width_big", multiple);
setAvailable("formpart_adjust_height_small", multiple);
setAvailable("formpart_adjust_height_big", multiple);
setAvailable("formpart_format_raise", true);
setAvailable("formpart_format_lower", true);
// If the widgets selected is a container, we enable tqlayout actions
if(!multiple)
{
KFormDesigner::ObjectTreeItem *item = f->objectTree()->lookup( f->selectedWidgets()->first()->name() );
if(item && item->container())
multiple = true;
}
// Layout actions
setAvailable("formpart_layout_hbox", multiple);
setAvailable("formpart_layout_vbox", multiple);
setAvailable("formpart_layout_grid", multiple);
KFormDesigner::Container *container = f->activeContainer();
setAvailable("formpart_break_layout", container ?
(container->tqlayoutType() != KFormDesigner::Container::NoLayout) : false );
}
void
KexiFormView::slotFormWidgetSelected(KFormDesigner::Form *f)
{
if(f != form())
return;
disableWidgetActions();
enableFormActions();
// Layout actions
setAvailable("formpart_layout_hbox", true);
setAvailable("formpart_layout_vbox", true);
setAvailable("formpart_layout_grid", true);
setAvailable("formpart_break_layout", (f->toplevelContainer()->tqlayoutType() != KFormDesigner::Container::NoLayout));
}
void
KexiFormView::slotNoFormSelected() // == form in preview mode
{
disableWidgetActions();
// Disable paste action
setAvailable("edit_paste", false);
setAvailable("edit_undo", false);
setAvailable("edit_redo", false);
// Disable 'Tools' actions
setAvailable("formpart_pixmap_collection", false);
setAvailable("formpart_connections", false);
setAvailable("formpart_taborder", false);
setAvailable("formpart_change_style", false);
}
void
KexiFormView::enableFormActions()
{
// Enable 'Tools' actions
setAvailable("formpart_pixmap_collection", true);
setAvailable("formpart_connections", true);
setAvailable("formpart_taborder", true);
setAvailable("edit_paste", KFormDesigner::FormManager::self()->isPasteEnabled());
}
void
KexiFormView::disableWidgetActions()
{
// Disable edit actions
setAvailable("edit_copy", false);
setAvailable("edit_cut", false);
setAvailable("edit_clear", false);
// Disable format functions
setAvailable("formpart_align_menu", false);
setAvailable("formpart_align_to_left", false);
setAvailable("formpart_align_to_right", false);
setAvailable("formpart_align_to_top", false);
setAvailable("formpart_align_to_bottom", false);
setAvailable("formpart_adjust_size_menu", false);
setAvailable("formpart_adjust_width_small", false);
setAvailable("formpart_adjust_width_big", false);
setAvailable("formpart_adjust_height_small", false);
setAvailable("formpart_adjust_height_big", false);
setAvailable("formpart_format_raise", false);
setAvailable("formpart_format_lower", false);
setAvailable("formpart_layout_hbox", false);
setAvailable("formpart_layout_vbox", false);
setAvailable("formpart_layout_grid", false);
setAvailable("formpart_break_layout", false);
}
void
KexiFormView::setUndoEnabled(bool enabled)
{
setAvailable("edit_undo", enabled);
}
void
KexiFormView::setRedoEnabled(bool enabled)
{
setAvailable("edit_redo", enabled);
}
#endif //0
TQSize
KexiFormView::preferredSizeHint(const TQSize& otherSize)
{
if (tqparentDialog()->neverSaved()) {
//ignore otherSize if possible
// return KexiViewBase::preferredSizeHint( (tqparentDialog() && tqparentDialog()->mdiParent()) ? TQSize(10000,10000) : otherSize);
}
return (m_dbform->size()
+TQSize(m_scrollView->verticalScrollBar()->isVisible() ? m_scrollView->verticalScrollBar()->width()*3/2 : 10,
m_scrollView->horizontalScrollBar()->isVisible() ? m_scrollView->horizontalScrollBar()->height()*3/2 : 10))
.expandedTo( KexiViewBase::preferredSizeHint(otherSize) );
}
void
KexiFormView::resizeEvent( TQResizeEvent *e )
{
if (viewMode()==Kexi::DataViewMode) {
m_scrollView->refreshContentsSizeLater(
e->size().width()!=e->oldSize().width(),
e->size().height()!=e->oldSize().height()
);
}
KexiViewBase::resizeEvent(e);
m_scrollView->updateNavPanelGeometry();
if (m_delayedFormContentsResizeOnShow>0) { // && isVisible()) {
m_delayedFormContentsResizeOnShow--;
m_dbform->resize( e->size() - TQSize(30, 30) );
}
}
void
KexiFormView::setFocusInternal()
{
if (viewMode() == Kexi::DataViewMode) {
if (m_dbform->tqfocusWidget()) {
//better-looking focus
if (m_setFocusInternalOnce) {
KexiUtils::setFocusWithReason(m_setFocusInternalOnce, TQFocusEvent::Other);//Tab);
m_setFocusInternalOnce = 0;
}
else {
//ok? SET_FOCUS_USING_REASON(m_dbform->tqfocusWidget(), TQFocusEvent::Other);//Tab);
}
return;
}
}
TQWidget::setFocus();
}
void
KexiFormView::show()
{
KexiDataAwareView::show();
//moved from KexiFormScrollView::show():
//now get resize mode settings for entire form
// if (resizeMode() == KexiFormView::ResizeAuto)
if (viewMode()==Kexi::DataViewMode) {
if (resizeMode() == KexiFormView::ResizeAuto)
m_scrollView->setResizePolicy(TQScrollView::AutoOneFit);
}
}
void
KexiFormView::slotFocus(bool in)
{
if(in && form() && KFormDesigner::FormManager::self() && KFormDesigner::FormManager::self()->activeForm() != form()) {
KFormDesigner::FormManager::self()->windowChanged(m_dbform);
updateDataSourcePage();
}
}
void
KexiFormView::updateDataSourcePage()
{
if (viewMode()==Kexi::DesignViewMode) {
TQCString dataSourceMimeType, dataSource;
KFormDesigner::WidgetPropertySet *set = KFormDesigner::FormManager::self()->propertySet();
if (set->tqcontains("dataSourceMimeType"))
dataSourceMimeType = (*set)["dataSourceMimeType"].value().toCString();
if (set->tqcontains("dataSource"))
dataSource = (*set)["dataSource"].value().toCString();
formPart()->dataSourcePage()->setDataSource(dataSourceMimeType, dataSource);
}
}
void
KexiFormView::slotHandleDragMoveEvent(TQDragMoveEvent* e)
{
if (KexiFieldDrag::canDecodeMultiple( e )) {
e->accept(true);
//dirty: drawRect(TQRect( e->pos(), TQSize(50, 20)), 2);
}
}
void
KexiFormView::slotHandleDropEvent(TQDropEvent* e)
{
const TQWidget *targetContainerWidget = dynamic_cast<const TQWidget*>(sender());
KFormDesigner::ObjectTreeItem *targetContainerWidgetItem = targetContainerWidget
? form()->objectTree()->lookup( targetContainerWidget->name() ) : 0;
if (targetContainerWidgetItem && targetContainerWidgetItem->container()
&& KexiFieldDrag::canDecodeMultiple( e ))
{
TQString sourceMimeType, sourceName;
TQStringList fields;
if (!KexiFieldDrag::decodeMultiple( e, sourceMimeType, sourceName, fields ))
return;
insertAutoFields(sourceMimeType, sourceName, fields,
targetContainerWidgetItem->container(), e->pos());
}
}
void
KexiFormView::insertAutoFields(const TQString& sourceMimeType, const TQString& sourceName,
const TQStringList& fields, KFormDesigner::Container* targetContainer, const TQPoint& _pos)
{
if (fields.isEmpty())
return;
KexiDB::Connection *conn = tqparentDialog()->mainWin()->project()->dbConnection();
KexiDB::TableOrQuerySchema tableOrQuery(conn, sourceName.latin1(), sourceMimeType=="kexi/table");
if (!tableOrQuery.table() && !tableOrQuery.query()) {
kexipluginswarn << "KexiFormView::insertAutoFields(): no such table/query \""
<< sourceName << "\"" << endl;
return;
}
TQPoint pos(_pos);
//if pos is not specified, compute a new position:
if (pos==TQPoint(-1,-1)) {
if (m_widgetGeometryForRecentInsertAutoFields.isValid()) {
pos = m_widgetGeometryForRecentInsertAutoFields.bottomLeft()
+ TQPoint(0,form()->gridSize());
}
else {
pos = TQPoint(40, 40); //start here
}
}
// there will be many actions performed, do not update property pane until all that's finished
KFormDesigner::FormManager::self()->blockPropertyEditorUpdating(this);
//! todo unnamed query colums are not supported
// KFormDesigner::WidgetList* prevSelection = form()->selectedWidgets();
KFormDesigner::WidgetList widgetsToSelect;
KFormDesigner::CommandGroup *group = new KFormDesigner::CommandGroup(
fields.count()==1 ? i18n("Insert AutoField widget") : i18n("Insert %1 AutoField widgets").tqarg(fields.count()),
KFormDesigner::FormManager::self()->propertySet()
);
foreach( TQStringList::ConstIterator, it, fields ) {
KexiDB::QueryColumnInfo* column = tableOrQuery.columnInfo(*it);
if (!column) {
kexipluginswarn << "KexiFormView::insertAutoFields(): no such field \""
<< *it << "\" in table/query \"" << sourceName << "\"" << endl;
continue;
}
//! todo add autolabel using field's caption or name
//KFormDesigner::Container *targetContainer;
/* TQWidget* targetContainerWidget = TQApplication::widgetAt(pos, true);
while (targetContainerWidget
&& !dynamic_cast<KFormDesigner::Container*>(targetContainerWidget))
{
targetContainerWidget = targetContainerWidget->tqparentWidget();
}
if (dynamic_cast<KFormDesigner::Container*>(targetContainerWidget))
targetContainer = dynamic_cast<KFormDesigner::Container*>(targetContainerWidget);
else
targetContainer = form()->toplevelContainer();*/
KFormDesigner::InsertWidgetCommand *insertCmd
= new KFormDesigner::InsertWidgetCommand(targetContainer,
//! todo this is hardcoded!
"KexiDBAutoField",
//! todo this name can be invalid for expressions: if so, fall back to a default class' prefix!
pos, column->aliasOrName()
);
insertCmd->execute();
group->addCommand(insertCmd, false/*don't exec twice*/);
KFormDesigner::ObjectTreeItem *newWidgetItem
= form()->objectTree()->dict()->tqfind(insertCmd->widgetName());
KexiDBAutoField* newWidget
= newWidgetItem ? dynamic_cast<KexiDBAutoField*>(newWidgetItem->widget()) : 0;
widgetsToSelect.append(newWidget);
//#if 0
KFormDesigner::CommandGroup *subGroup
= new KFormDesigner::CommandGroup("", KFormDesigner::FormManager::self()->propertySet());
TQMap<TQCString, TQVariant> propValues;
propValues.insert("dataSource", column->aliasOrName());
propValues.insert("fieldTypeInternal", (int)column->field->type());
propValues.insert("fieldCaptionInternal", column->captionOrAliasOrName());
KFormDesigner::FormManager::self()->propertySet()->createPropertyCommandsInDesignMode(
newWidget, propValues, subGroup, false/*!addToActiveForm*/,
true /*!execFlagForSubCommands*/);
subGroup->execute();
group->addCommand( subGroup, false/*will not be executed on CommandGroup::execute()*/ );
//#endif
//set data source and caption
//-we don't need to use PropertyCommand here beacause we don't need UNDO
// for these single commands
// newWidget->setDataSource(column->aliasOrName());
// newWidget->setFieldTypeInternal((int)column->field->type());
// newWidget->setFieldCaptionInternal(column->captionOrAliasOrName());
//resize again because autofield's type changed what can lead to changed tqsizeHint()
// newWidget->resize(newWidget->tqsizeHint());
KFormDesigner::WidgetList list;
list.append(newWidget);
KFormDesigner::AdjustSizeCommand *adjustCommand
= new KFormDesigner::AdjustSizeCommand(KFormDesigner::AdjustSizeCommand::SizeToFit,
list, form());
adjustCommand->execute();
group->addCommand( adjustCommand,
false/*will not be executed on CommandGroup::execute()*/
);
if (newWidget) {//move position down for next widget
pos.setY( pos.y() + newWidget->height() + form()->gridSize());
}
}
if (widgetsToSelect.last()) {
//resize form if needed
TQRect oldFormRect( m_dbform->tqgeometry() );
TQRect newFormRect( oldFormRect );
newFormRect.setWidth(TQMAX(m_dbform->width(), widgetsToSelect.last()->tqgeometry().right()+1));
newFormRect.setHeight(TQMAX(m_dbform->height(), widgetsToSelect.last()->tqgeometry().bottom()+1));
if (newFormRect != oldFormRect) {
//1. resize by hand
m_dbform->setGeometry( newFormRect );
//2. store information about resize
KFormDesigner::PropertyCommand *resizeFormCommand = new KFormDesigner::PropertyCommand(
KFormDesigner::FormManager::self()->propertySet(), m_dbform->name(),
oldFormRect, newFormRect, "geometry");
group->addCommand(resizeFormCommand, true/*will be executed on CommandGroup::execute()*/);
}
//remember tqgeometry of the last inserted widget
m_widgetGeometryForRecentInsertAutoFields = widgetsToSelect.last()->tqgeometry();
}
//eventually, add entire command group to active form
form()->addCommand( group, true/*exec*/ );
// group->debug();
//enable proper REDO usage
group->resetAllowExecuteFlags();
m_scrollView->tqrepaint();
m_scrollView->viewport()->tqrepaint();
m_scrollView->repaintContents();
m_scrollView->updateContents();
m_scrollView->clipper()->tqrepaint();
m_scrollView->refreshContentsSize();
//select all inserted widgets, if multiple
if (widgetsToSelect.count()>1) {
form()->setSelectedWidget(0);
foreach_list (KFormDesigner::WidgetListIterator, it, widgetsToSelect)
form()->setSelectedWidget(it.current(), true/*add*/, true/*dontRaise*/);
}
// eventually, update property pane
KFormDesigner::FormManager::self()->unblockPropertyEditorUpdating(this, KFormDesigner::FormManager::self()->propertySet());
}
void
KexiFormView::setUnsavedLocalBLOB(TQWidget *widget, KexiBLOBBuffer::Id_t id)
{
//! @todo if there already was data assigned, remember it should be dereferenced
if (id==0)
tempData()->unsavedLocalBLOBs.remove(widget);
else
tempData()->unsavedLocalBLOBs.insert(widget, id);
}
/*
todo
void KexiFormView::updateActions(bool activated)
{
if (viewMode()==Kexi::DesignViewMode) {
if (form()->selectedWidget()) {
if (form()->widget() == form()->selectedWidget())
KFormDesigner::FormManager::self()->emitFormWidgetSelected( form() );
else
KFormDesigner::FormManager::self()->emitWidgetSelected( form(), false );
}
else if (form()->selectedWidgets()) {
KFormDesigner::FormManager::self()->emitWidgetSelected( form(), true );
}
}
KexiDataAwareView::updateActions(activated);
}*/
/*
void KexiFormView::tqparentDialogDetached()
{
m_dbform->updateTabStopsOrder(form());
}
void KexiFormView::tqparentDialogAttached(KMdiChildFrm *)
{
m_dbform->updateTabStopsOrder(form());
}*/
#include "kexiformview.moc"
|