kexi

kexiformmanager.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "kexiformmanager.h"
21#include "widgets/kexidbform.h"
22#include "widgets/kexidbautofield.h"
23#include "kexiformscrollview.h"
24#include "kexiformview.h"
25#include "kexidatasourcepage.h"
26
27#include <formeditor/formmanager.h>
28#include <formeditor/widgetpropertyset.h>
29#include <formeditor/form.h>
30#include <formeditor/widgetlibrary.h>
31#include <formeditor/commands.h>
32#include <formeditor/objecttree.h>
33
34#include <koproperty/set.h>
35#include <koproperty/property.h>
36#include <widget/kexicustompropertyfactory.h>
37
38KexiFormManager::KexiFormManager(KexiPart::Part *parent, const char* name)
39 : KFormDesigner::FormManager(parent,
40 KFormDesigner::FormManager::HideEventsInPopupMenu |
41 KFormDesigner::FormManager::SkipFileActions |
42 KFormDesigner::FormManager::HideSignalSlotConnections
43 , name)
44 , m_part(parent)
45{
46 m_emitSelectionSignalsUpdatesPropertySet = true;
48}
49
50KexiFormManager::~KexiFormManager()
51{
52}
53
54TDEAction* KexiFormManager::action( const char* name )
55{
56 TDEActionCollection *col = m_part->actionCollectionForMode(Kexi::DesignViewMode);
57 if (!col)
58 return 0;
59 TQCString n( translateName( name ).latin1() );
60 TDEAction *a = col->action(n.data());
61 if (a)
62 return a;
63 KexiDBForm *dbform;
64 if (!activeForm() || !activeForm()->designMode()
65 || !(dbform = dynamic_cast<KexiDBForm*>(activeForm()->formWidget())))
66 return 0;
67 KexiFormScrollView *scrollViewWidget = dynamic_cast<KexiFormScrollView*>(dbform->dataAwareObject());
68 if (!scrollViewWidget)
69 return 0;
70 KexiFormView* formViewWidget = dynamic_cast<KexiFormView*>(scrollViewWidget->parent());
71 if (!formViewWidget)
72 return 0;
73 return formViewWidget->parentDialog()->mainWin()->actionCollection()->action(n.data());
74}
75
76KexiFormView* KexiFormManager::activeFormViewWidget() const
77{
78 KexiDBForm *dbform;
79 if (!activeForm() || !activeForm()->designMode()
80 || !(dbform = dynamic_cast<KexiDBForm*>(activeForm()->formWidget())))
81 return 0;
82 KexiFormScrollView *scrollViewWidget = dynamic_cast<KexiFormScrollView*>(dbform->dataAwareObject());
83 if (!scrollViewWidget)
84 return 0;
85 return dynamic_cast<KexiFormView*>(scrollViewWidget->parent());
86}
87
88void KexiFormManager::enableAction( const char* name, bool enable )
89{
90 KexiFormView* formViewWidget = activeFormViewWidget();
91 if (!formViewWidget)
92 return;
93// if (TQString(name)=="layout_menu")
94// kdDebug() << "!!!!!!!!!!! " << enable << endl;
95 formViewWidget->setAvailable(translateName( name ).latin1(), enable);
96}
97
98void KexiFormManager::setFormDataSource(const TQCString& mime, const TQCString& name)
99{
100 if (!activeForm())
101 return;
102 KexiDBForm* formWidget = dynamic_cast<KexiDBForm*>(activeForm()->widget());
103 if (!formWidget)
104 return;
105
106// setPropertyValueInDesignMode(formWidget, "dataSource", name);
107
108 TQCString oldDataSourceMimeType( formWidget->dataSourceMimeType() );
109 TQCString oldDataSource( formWidget->dataSource().latin1() );
110 if (mime!=oldDataSourceMimeType || name!=oldDataSource) {
111 TQMap<TQCString, TQVariant> propValues;
112 propValues.insert("dataSource", name);
113 propValues.insert("dataSourceMimeType", mime);
114 KFormDesigner::CommandGroup *group
115 = new KFormDesigner::CommandGroup(i18n("Set Form's Data Source to \"%1\"").arg(name.data()), propertySet());
116 propertySet()->createPropertyCommandsInDesignMode(formWidget, propValues, group, true /*addToActiveForm*/);
117 }
118
119/*
120 if (activeForm()->selectedWidget() == formWidget) {
121 //active form is selected: just use properties system
122 KFormDesigner::WidgetPropertySet *set = propertySet();
123 if (!set || !set->contains("dataSource"))
124 return;
125 (*set)["dataSource"].setValue(name);
126 if (set->contains("dataSourceMimeType"))
127 (*set)["dataSourceMimeType"].setValue(mime);
128 return;
129 }
130
131 //active form isn't selected: change it's data source and mime type by hand
132 TQCString oldDataSourceMimeType( formWidget->dataSourceMimeType() );
133 TQCString oldDataSource( formWidget->dataSource().latin1() );
134
135 if (mime!=oldDataSourceMimeType || name!=oldDataSource) {
136 formWidget->setDataSourceMimeType(mime);
137 formWidget->setDataSource(name);
138 emit dirty(activeForm(), true);
139
140 activeForm()->addCommand(
141 new KFormDesigner::PropertyCommand(propertySet(), TQString(formWidget->name()),
142 oldDataSource, name, "dataSource"),
143 false );
144
145 // If the property is changed, we add it in ObjectTreeItem modifProp
146 KFormDesigner::ObjectTreeItem *fromTreeItem = activeForm()->objectTree()->lookup(formWidget->name());
147 fromTreeItem->addModifiedProperty("dataSourceMimeType", mime);
148 fromTreeItem->addModifiedProperty("dataSource", name);
149 }*/
150}
151
152void KexiFormManager::setDataSourceFieldOrExpression(const TQString& string, const TQString& caption,
154{
155 if (!activeForm())
156 return;
157// KexiFormDataItemInterface* dataWidget = dynamic_cast<KexiFormDataItemInterface*>(activeForm()->selectedWidget());
158// if (!dataWidget)
159// return;
160
161 KFormDesigner::WidgetPropertySet *set = propertySet();
162 if (!set || !set->contains("dataSource"))
163 return;
164
165 (*set)["dataSource"].setValue(string);
166
167 if (set->contains("autoCaption") && (*set)["autoCaption"].value().toBool()) {
168 if (set->contains("fieldCaptionInternal"))
169 (*set)["fieldCaptionInternal"].setValue(caption);
170 }
171 if (//type!=KexiDB::Field::InvalidType &&
172 set->contains("widgetType") && (*set)["widgetType"].value().toString()=="Auto")
173 {
174 if (set->contains("fieldTypeInternal"))
175 (*set)["fieldTypeInternal"].setValue(type);
176 }
177
178/* TQString oldDataSource( dataWidget->dataSource() );
179 if (string!=oldDataSource) {
180 dataWidget->setDataSource(string);
181 emit dirty(activeForm(), true);
182
183 buffer
184 }*/
185}
186
187void KexiFormManager::insertAutoFields(const TQString& sourceMimeType, const TQString& sourceName,
188 const TQStringList& fields)
189{
190 KexiFormView* formViewWidget = activeFormViewWidget();
191 if (!formViewWidget || !formViewWidget->form() || !formViewWidget->form()->activeContainer())
192 return;
193 formViewWidget->insertAutoFields(sourceMimeType, sourceName, fields,
194 formViewWidget->form()->activeContainer());
195}
196
197void KexiFormManager::slotHistoryCommandExecuted()
198{
199 const KFormDesigner::CommandGroup *group = dynamic_cast<const KFormDesigner::CommandGroup*>(sender());
200 if (group) {
201 if (group->commands().count()==2) {
202 KexiDBForm* formWidget = dynamic_cast<KexiDBForm*>(activeForm()->widget());
203 if (!formWidget)
204 return;
205 TQPtrListIterator<KCommand> it(group->commands());
206 const KFormDesigner::PropertyCommand* pc1 = dynamic_cast<const KFormDesigner::PropertyCommand*>(it.current());
207 ++it;
208 const KFormDesigner::PropertyCommand* pc2 = dynamic_cast<const KFormDesigner::PropertyCommand*>(it.current());
209 if (pc1 && pc2 && pc1->property()=="dataSource" && pc2->property()=="dataSourceMimeType") {
210 const TQMap<TQCString, TQVariant>::const_iterator it1( pc1->oldValues().constBegin() );
211 const TQMap<TQCString, TQVariant>::const_iterator it2( pc2->oldValues().constBegin() );
212 if (it1.key()==formWidget->name() && it2.key()==formWidget->name())
213 static_cast<KexiFormPart*>(m_part)->dataSourcePage()->setDataSource(
214 formWidget->dataSourceMimeType(), formWidget->dataSource().latin1());
215 }
216 }
217 }
218}
219
220/*
221bool KexiFormManager::loadFormFromDomInternal(Form *form, TQWidget *container, TQDomDocument &inBuf)
222{
223 TQMap<TQCString,TQString> customProperties;
224 FormIO::loadFormFromDom(myform, container, domDoc, &customProperties);
225}
226
227bool KexiFormManager::saveFormToStringInternal(Form *form, TQString &dest, int indent)
228{
229 TQMap<TQCString,TQString> customProperties;
230 return KFormDesigner::FormIO::saveFormToString(form, dest, indent, &customProperties);
231}
232
233*/
234
235#include "kexiformmanager.moc"
const TQPtrList< KCommand > & commands() const
WidgetPropertySet * propertySet() const
Container * activeContainer()
Definition form.cpp:151
TQWidget * widget() const
Definition form.cpp:114
static void init()
Called once to register all propert and editor types provided by this factory.
The KexiFormView lass provides a data-driven (record-based) form view .
void insertAutoFields(const TQString &sourceMimeType, const TQString &sourceName, const TQStringList &fields, KFormDesigner::Container *targetContainerWidget, const TQPoint &pos=TQPoint(-1,-1))
The main class for kexi frontend parts like tables, queries, forms and reports.
Definition kexipart.h:66
virtual void setAvailable(const char *action_name, bool set)
KexiDialogBase * parentDialog() const