kexi

kexiformmanager.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kexiformmanager.h"
00021 #include "widgets/kexidbform.h"
00022 #include "widgets/kexidbautofield.h"
00023 #include "kexiformscrollview.h"
00024 #include "kexiformview.h"
00025 #include "kexidatasourcepage.h"
00026 
00027 #include <formeditor/formmanager.h>
00028 #include <formeditor/widgetpropertyset.h>
00029 #include <formeditor/form.h>
00030 #include <formeditor/widgetlibrary.h>
00031 #include <formeditor/commands.h>
00032 #include <formeditor/objecttree.h>
00033 
00034 #include <koproperty/set.h>
00035 #include <koproperty/property.h>
00036 #include <widget/kexicustompropertyfactory.h>
00037 
00038 KexiFormManager::KexiFormManager(KexiPart::Part *parent, const char* name)
00039  : KFormDesigner::FormManager(parent, 
00040         KFormDesigner::FormManager::HideEventsInPopupMenu |
00041         KFormDesigner::FormManager::SkipFileActions |
00042         KFormDesigner::FormManager::HideSignalSlotConnections
00043     , name)
00044  , m_part(parent)
00045 {
00046     m_emitSelectionSignalsUpdatesPropertySet = true;
00047     KexiCustomPropertyFactory::init();
00048 }
00049 
00050 KexiFormManager::~KexiFormManager()
00051 {
00052 }
00053 
00054 KAction* KexiFormManager::action( const char* name )
00055 {
00056     KActionCollection *col = m_part->actionCollectionForMode(Kexi::DesignViewMode);
00057     if (!col)
00058         return 0;
00059     QCString n( translateName( name ).latin1() );
00060     KAction *a = col->action(n);
00061     if (a)
00062         return a;
00063     KexiDBForm *dbform;
00064     if (!activeForm() || !activeForm()->designMode()
00065         || !(dbform = dynamic_cast<KexiDBForm*>(activeForm()->formWidget())))
00066         return 0;
00067     KexiFormScrollView *scrollViewWidget = dynamic_cast<KexiFormScrollView*>(dbform->dataAwareObject());
00068     if (!scrollViewWidget)
00069         return 0;
00070     KexiFormView* formViewWidget = dynamic_cast<KexiFormView*>(scrollViewWidget->parent());
00071     if (!formViewWidget)
00072         return 0;
00073     return formViewWidget->parentDialog()->mainWin()->actionCollection()->action(n);
00074 }
00075 
00076 KexiFormView* KexiFormManager::activeFormViewWidget() const
00077 {
00078     KexiDBForm *dbform;
00079     if (!activeForm() || !activeForm()->designMode()
00080         || !(dbform = dynamic_cast<KexiDBForm*>(activeForm()->formWidget())))
00081         return 0;
00082     KexiFormScrollView *scrollViewWidget = dynamic_cast<KexiFormScrollView*>(dbform->dataAwareObject());
00083     if (!scrollViewWidget)
00084         return 0;
00085     return dynamic_cast<KexiFormView*>(scrollViewWidget->parent());
00086 }
00087 
00088 void KexiFormManager::enableAction( const char* name, bool enable )
00089 {
00090     KexiFormView* formViewWidget = activeFormViewWidget();
00091     if (!formViewWidget)
00092         return;
00093 //  if (QString(name)=="layout_menu")
00094 //      kdDebug() << "!!!!!!!!!!! " << enable << endl;
00095     formViewWidget->setAvailable(translateName( name ).latin1(), enable);
00096 }
00097 
00098 void KexiFormManager::setFormDataSource(const QCString& mime, const QCString& name)
00099 {
00100     if (!activeForm())
00101         return;
00102     KexiDBForm* formWidget = dynamic_cast<KexiDBForm*>(activeForm()->widget());
00103     if (!formWidget)
00104         return;
00105 
00106 //  setPropertyValueInDesignMode(formWidget, "dataSource", name);
00107 
00108     QCString oldDataSourceMimeType( formWidget->dataSourceMimeType() );
00109     QCString oldDataSource( formWidget->dataSource().latin1() );
00110     if (mime!=oldDataSourceMimeType || name!=oldDataSource) {
00111         QMap<QCString, QVariant> propValues;
00112         propValues.insert("dataSource", name);
00113         propValues.insert("dataSourceMimeType", mime);
00114         KFormDesigner::CommandGroup *group 
00115             = new KFormDesigner::CommandGroup(i18n("Set Form's Data Source to \"%1\"").arg(name), propertySet());
00116         propertySet()->createPropertyCommandsInDesignMode(formWidget, propValues, group, true /*addToActiveForm*/);
00117     }
00118 
00119 /*
00120     if (activeForm()->selectedWidget() == formWidget) {
00121         //active form is selected: just use properties system
00122         KFormDesigner::WidgetPropertySet *set = propertySet();
00123         if (!set || !set->contains("dataSource"))
00124             return;
00125         (*set)["dataSource"].setValue(name);
00126         if (set->contains("dataSourceMimeType"))
00127             (*set)["dataSourceMimeType"].setValue(mime);
00128         return;
00129     }
00130 
00131     //active form isn't selected: change it's data source and mime type by hand
00132     QCString oldDataSourceMimeType( formWidget->dataSourceMimeType() );
00133     QCString oldDataSource( formWidget->dataSource().latin1() );
00134 
00135     if (mime!=oldDataSourceMimeType || name!=oldDataSource) {
00136         formWidget->setDataSourceMimeType(mime);
00137         formWidget->setDataSource(name);
00138         emit dirty(activeForm(), true);
00139 
00140         activeForm()->addCommand( 
00141             new KFormDesigner::PropertyCommand(propertySet(), QString(formWidget->name()),
00142                 oldDataSource, name, "dataSource"), 
00143             false );
00144 
00145         // If the property is changed, we add it in ObjectTreeItem modifProp
00146         KFormDesigner::ObjectTreeItem *fromTreeItem = activeForm()->objectTree()->lookup(formWidget->name());
00147         fromTreeItem->addModifiedProperty("dataSourceMimeType", mime);
00148         fromTreeItem->addModifiedProperty("dataSource", name);
00149     }*/
00150 }
00151 
00152 void KexiFormManager::setDataSourceFieldOrExpression(const QString& string, const QString& caption, 
00153     KexiDB::Field::Type type)
00154 {
00155     if (!activeForm())
00156         return;
00157 //  KexiFormDataItemInterface* dataWidget = dynamic_cast<KexiFormDataItemInterface*>(activeForm()->selectedWidget());
00158 //  if (!dataWidget)
00159 //      return;
00160     
00161     KFormDesigner::WidgetPropertySet *set = propertySet();
00162     if (!set || !set->contains("dataSource"))
00163         return;
00164 
00165     (*set)["dataSource"].setValue(string);
00166 
00167     if (set->contains("autoCaption") && (*set)["autoCaption"].value().toBool()) {
00168         if (set->contains("fieldCaptionInternal"))
00169             (*set)["fieldCaptionInternal"].setValue(caption);
00170     }
00171     if (//type!=KexiDB::Field::InvalidType && 
00172         set->contains("widgetType") && (*set)["widgetType"].value().toString()=="Auto")
00173     {
00174         if (set->contains("fieldTypeInternal"))
00175             (*set)["fieldTypeInternal"].setValue(type);
00176     }
00177 
00178 /*  QString oldDataSource( dataWidget->dataSource() );
00179     if (string!=oldDataSource) {
00180         dataWidget->setDataSource(string);
00181         emit dirty(activeForm(), true);
00182 
00183         buffer
00184     }*/
00185 }
00186 
00187 void KexiFormManager::insertAutoFields(const QString& sourceMimeType, const QString& sourceName,
00188     const QStringList& fields)
00189 {
00190     KexiFormView* formViewWidget = activeFormViewWidget();
00191     if (!formViewWidget || !formViewWidget->form() || !formViewWidget->form()->activeContainer())
00192         return;
00193     formViewWidget->insertAutoFields(sourceMimeType, sourceName, fields, 
00194         formViewWidget->form()->activeContainer());
00195 }
00196 
00197 void KexiFormManager::slotHistoryCommandExecuted()
00198 {
00199     const KFormDesigner::CommandGroup *group = dynamic_cast<const KFormDesigner::CommandGroup*>(sender());
00200     if (group) {
00201         if (group->commands().count()==2) {
00202             KexiDBForm* formWidget = dynamic_cast<KexiDBForm*>(activeForm()->widget());
00203             if (!formWidget)
00204                 return;
00205             QPtrListIterator<KCommand> it(group->commands());
00206             const KFormDesigner::PropertyCommand* pc1 = dynamic_cast<const KFormDesigner::PropertyCommand*>(it.current());
00207             ++it;
00208             const KFormDesigner::PropertyCommand* pc2 = dynamic_cast<const KFormDesigner::PropertyCommand*>(it.current());
00209             if (pc1 && pc2 && pc1->property()=="dataSource" && pc2->property()=="dataSourceMimeType") {
00210                 const QMap<QCString, QVariant>::const_iterator it1( pc1->oldValues().constBegin() );
00211                 const QMap<QCString, QVariant>::const_iterator it2( pc2->oldValues().constBegin() );
00212                 if (it1.key()==formWidget->name() && it2.key()==formWidget->name())
00213                     static_cast<KexiFormPart*>(m_part)->dataSourcePage()->setDataSource(
00214                         formWidget->dataSourceMimeType(), formWidget->dataSource().latin1());
00215             }
00216         }
00217     }
00218 }
00219 
00220 /*
00221 bool KexiFormManager::loadFormFromDomInternal(Form *form, QWidget *container, QDomDocument &inBuf)
00222 {
00223     QMap<QCString,QString> customProperties;
00224     FormIO::loadFormFromDom(myform, container, domDoc, &customProperties);
00225 }
00226 
00227 bool KexiFormManager::saveFormToStringInternal(Form *form, QString &dest, int indent)
00228 {
00229     QMap<QCString,QString> customProperties;
00230     return KFormDesigner::FormIO::saveFormToString(form, dest, indent, &customProperties);
00231 }
00232 
00233 */
00234 
00235 #include "kexiformmanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys