kexi

kexidbsubform.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
00003    Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kexidbsubform.h"
00022 
00023 #include "kexidbform.h"
00024 #include "../kexiformview.h"
00025 #include <kexidb/utils.h>
00026 #include <formeditor/formIO.h>
00027 #include <formeditor/objecttree.h>
00028 #include <formeditor/utils.h>
00029 #include <formeditor/container.h>
00030 #include <formeditor/formmanager.h>
00031 
00032 KexiDBSubForm::KexiDBSubForm(KFormDesigner::Form *parentForm, QWidget *parent, const char *name)
00033 : QScrollView(parent, name), m_parentForm(parentForm), m_form(0), m_widget(0)
00034 {
00035     setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
00036     viewport()->setPaletteBackgroundColor(colorGroup().mid());
00037 }
00038 /*
00039 void
00040 KexiDBSubForm::paintEvent(QPaintEvent *ev)
00041 {
00042     QScrollView::paintEvent(ev);
00043     QPainter p;
00044 
00045     setWFlags(WPaintUnclipped);
00046 
00047     QString txt("Subform");
00048     QFont f = font();
00049     f.setPointSize(f.pointSize() * 3);
00050     QFontMetrics fm(f);
00051     const int txtw = fm.width(txt), txth = fm.height();
00052 
00053     p.begin(this, true);
00054     p.setPen(black);
00055     p.setFont(f);
00056     p.drawText(width()/2, height()/2, txt, Qt::AlignCenter|Qt::AlignVCenter);
00057     p.end();
00058 
00059     clearWFlags( WPaintUnclipped );
00060 }
00061 */
00062 void
00063 KexiDBSubForm::setFormName(const QString &name)
00064 {
00065     if(m_formName==name)
00066         return;
00067 
00068     m_formName = name; //assign, even if the name points to nowhere
00069 
00070     if(name.isEmpty()) {
00071         delete m_widget;
00072         m_widget = 0;
00073         updateScrollBars();
00074         return;
00075     }
00076 
00077     QWidget *pw = parentWidget();
00078     KexiFormView *view = 0;
00079     QStringList list;
00080     while(pw) {
00081         if(pw->isA("KexiDBSubForm")) {
00082             if(list.contains(pw->name())) {
00084                 return; // Be sure to don't run into a endless-loop cause of recursive subforms.
00085             }
00086             list.append(pw->name());
00087         }
00088         else if(! view && pw->isA("KexiFormView"))
00089             view = static_cast<KexiFormView*>(pw); // we need a KexiFormView*
00090         pw = pw->parentWidget();
00091     }
00092 
00093     if (!view || !view->parentDialog() || !view->parentDialog()->mainWin()
00094         || !view->parentDialog()->mainWin()->project()->dbConnection())
00095         return;
00096 
00097     KexiDB::Connection *conn = view->parentDialog()->mainWin()->project()->dbConnection();
00098 
00099     // we check if there is a form with this name
00100     int id = KexiDB::idForObjectName(*conn, name, KexiPart::FormObjectType);
00101     if((id == 0) || (id == view->parentDialog()->id())) // == our form
00102         return; // because of recursion when loading
00103 
00104     // we create the container widget
00105     delete m_widget;
00106     m_widget = new KexiDBFormBase(viewport(), "KexiDBSubForm_widget");
00107     m_widget->show();
00108     addChild(m_widget);
00109     m_form = new KFormDesigner::Form(KexiFormPart::library(), this->name());
00110     m_form->createToplevel(m_widget);
00111 
00112     // and load the sub form
00113     QString data;
00114     tristate res = conn->loadDataBlock(id, data, QString::null);
00115     if (res == true)
00116         res = KFormDesigner::FormIO::loadFormFromString(m_form, m_widget, data);
00117     if(res != true) {
00118         delete m_widget;
00119         m_widget = 0;
00120         updateScrollBars();
00121         m_formName = QString::null;
00122         return;
00123     }
00124     m_form->setDesignMode(false);
00125 
00126     // Install event filters on the whole newly created form
00127     KFormDesigner::ObjectTreeItem *tree = m_parentForm->objectTree()->lookup(QObject::name());
00128     KFormDesigner::installRecursiveEventFilter(this, tree->eventEater());
00129 }
00130 
00131 #include "kexidbsubform.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys