kexi

kexidbsubform.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3 Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "kexidbsubform.h"
22
23#include "kexidbform.h"
24#include "../kexiformview.h"
25#include <kexidb/utils.h>
26#include <formeditor/formIO.h>
27#include <formeditor/objecttree.h>
28#include <formeditor/utils.h>
29#include <formeditor/container.h>
30#include <formeditor/formmanager.h>
31
32KexiDBSubForm::KexiDBSubForm(KFormDesigner::Form *parentForm, TQWidget *parent, const char *name)
33: TQScrollView(parent, name), m_parentForm(parentForm), m_form(0), m_widget(0)
34{
35 setFrameStyle(TQFrame::WinPanel | TQFrame::Sunken);
36 viewport()->setPaletteBackgroundColor(colorGroup().mid());
37}
38/*
39void
40KexiDBSubForm::paintEvent(TQPaintEvent *ev)
41{
42 TQScrollView::paintEvent(ev);
43 TQPainter p;
44
45 setWFlags(WPaintUnclipped);
46
47 TQString txt("Subform");
48 TQFont f = font();
49 f.setPointSize(f.pointSize() * 3);
50 TQFontMetrics fm(f);
51 const int txtw = fm.width(txt), txth = fm.height();
52
53 p.begin(this, true);
54 p.setPen(black);
55 p.setFont(f);
56 p.drawText(width()/2, height()/2, txt, TQt::AlignCenter|TQt::AlignVCenter);
57 p.end();
58
59 clearWFlags( WPaintUnclipped );
60}
61*/
62void
63KexiDBSubForm::setFormName(const TQString &name)
64{
65 if(m_formName==name)
66 return;
67
68 m_formName = name; //assign, even if the name points to nowhere
69
70 if(name.isEmpty()) {
71 delete m_widget;
72 m_widget = 0;
73 updateScrollBars();
74 return;
75 }
76
77 TQWidget *pw = parentWidget();
78 KexiFormView *view = 0;
79 TQStringList list;
80 while(pw) {
81 if(pw->isA("KexiDBSubForm")) {
82 if(list.contains(pw->name())) {
84 return; // Be sure to don't run into a endless-loop cause of recursive subforms.
85 }
86 list.append(pw->name());
87 }
88 else if(! view && pw->isA("KexiFormView"))
89 view = static_cast<KexiFormView*>(pw); // we need a KexiFormView*
90 pw = pw->parentWidget();
91 }
92
93 if (!view || !view->parentDialog() || !view->parentDialog()->mainWin()
94 || !view->parentDialog()->mainWin()->project()->dbConnection())
95 return;
96
97 KexiDB::Connection *conn = view->parentDialog()->mainWin()->project()->dbConnection();
98
99 // we check if there is a form with this name
100 int id = KexiDB::idForObjectName(*conn, name, KexiPart::FormObjectType);
101 if((id == 0) || (id == view->parentDialog()->id())) // == our form
102 return; // because of recursion when loading
103
104 // we create the container widget
105 delete m_widget;
106 m_widget = new KexiDBFormBase(viewport(), "KexiDBSubForm_widget");
107 m_widget->show();
108 addChild(m_widget);
109 m_form = new KFormDesigner::Form(KexiFormPart::library(), this->name());
110 m_form->createToplevel(m_widget);
111
112 // and load the sub form
113 TQString data;
114 tristate res = conn->loadDataBlock(id, data, TQString());
115 if (res == true)
116 res = KFormDesigner::FormIO::loadFormFromString(m_form, m_widget, data);
117 if(res != true) {
118 delete m_widget;
119 m_widget = 0;
120 updateScrollBars();
121 m_formName = TQString();
122 return;
123 }
124 m_form->setDesignMode(false);
125
126 // Install event filters on the whole newly created form
127 KFormDesigner::ObjectTreeItem *tree = m_parentForm->objectTree()->lookup(this->name());
128 KFormDesigner::installRecursiveEventFilter(this, tree->eventEater());
129}
130
131#include "kexidbsubform.moc"
A simple class representing a form.
Definition form.h:136
An item representing a widget Holds the properties of a widget (classname, name, parent,...
Definition objecttree.h:59
void setFormName(const TQString &name)
Sets the name of the subform to display inside this widget.
Provides database connection, allowing queries and data modification.
Definition connection.h:62
tristate loadDataBlock(int objectID, TQString &dataString, const TQString &dataID)
static KFormDesigner::WidgetLibrary * library()
The KexiFormView lass provides a data-driven (record-based) form view .
virtual KexiProject * project()=0
Project data of currently opened project or NULL if no project here yet.
KexiDB::Connection * dbConnection() const
KexiDialogBase * parentDialog() const
3-state logical type with three values: true, false and cancelled and convenient operators.
Definition tristate.h:100
KFORMEDITOR_EXPORT void installRecursiveEventFilter(TQObject *object, TQObject *container)
KEXI_DB_EXPORT int idForObjectName(Connection &conn, const TQString &objName, int objType)