kexi

kexiviewbase.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 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 "kexiviewbase.h"
21
22#include "keximainwindow.h"
23#include "kexidialogbase.h"
24#include "kexiproject.h"
25#include <koproperty/set.h>
26
27#include <kexidb/connection.h>
28#include <kexidb/utils.h>
29#include <kexiutils/utils.h>
30
31#include <kdebug.h>
32
33KexiViewBase::KexiViewBase(KexiMainWindow *mainWin, TQWidget *parent, const char *name)
34 : TQWidget(parent, name)
35 , KexiActionProxy(this, mainWin)
36 , m_mainWin(mainWin)
37 , m_viewWidget(0)
38 , m_parentView(0)
39 , m_newlyAssignedID(-1)
40 , m_viewMode(0) //unknown!
41 , m_dirty(false)
42{
43 TQWidget *wi=this;
44 while ((wi = wi->parentWidget()) && !wi->inherits("KexiDialogBase"))
45 ;
46 m_dialog = (wi && wi->inherits("KexiDialogBase")) ? static_cast<KexiDialogBase*>(wi) : 0;
47 if (m_dialog) {
48 //init view mode number for this view (obtained from dialog where this view is created)
49 if (m_dialog->supportsViewMode(m_dialog->m_creatingViewsMode))
50 m_viewMode = m_dialog->m_creatingViewsMode;
51 }
52
53 installEventFilter(this);
54}
55
56KexiViewBase::~KexiViewBase()
57{
58}
59
61{
62 return m_dialog ? m_dialog->part() : 0;
63}
64
65tristate KexiViewBase::beforeSwitchTo(int /* mode */, bool & /*dontStore*/)
66{
67 return true;
68}
69
71{
72 return true;
73}
74
75TQSize KexiViewBase::preferredSizeHint(const TQSize& otherSize)
76{
77 KexiDialogBase* dlg = parentDialog();
78 if (dlg && dlg->mdiParent()) {
79 TQRect r = dlg->mdiParent()->mdiAreaContentsRect();
80 return otherSize.boundedTo( TQSize(
81 r.width() - 10,
82 r.height() - dlg->mdiParent()->captionHeight() - dlg->pos().y() - 10
83 ) );
84 }
85 return otherSize;
86}
87
88void KexiViewBase::closeEvent( TQCloseEvent * e )
89{
90 bool cancel = false;
91 emit closing(cancel);
92 if (cancel) {
93 e->ignore();
94 return;
95 }
96 TQWidget::closeEvent(e);
97}
98
99KoProperty::Set *KexiViewBase::propertySet()
100{
101 return 0;
102}
103
105{
106 if (parentDialog())
107 m_mainWin->propertySetSwitched( parentDialog(), false );
108}
109
110void KexiViewBase::propertySetReloaded(bool preservePrevSelection, const TQCString& propertyToSelect)
111{
112 if (parentDialog())
113 m_mainWin->propertySetSwitched( parentDialog(), true, preservePrevSelection, propertyToSelect );
114}
115
117{
118/* if (m_dirty == set) {//no change here
119 if (m_dialog) {
120 // however, it's a change from dialog perspective
121 if (m_dialog->dirty()!=set)
122 m_dialog->dirtyChanged();
123 }
124 return;
125 }*/
126 const bool changed = (m_dirty != set);
127 m_dirty = set;
128 m_dirty = dirty();
129// if (m_dirty!=set)//eventually didn't change
130// return;
131 if (m_parentView) {
132 m_parentView->setDirty(m_dirty);
133 }
134 else {
135 if (changed && m_dialog)
136 m_dialog->dirtyChanged(this);
137 }
138}
139
140/*bool KexiViewBase::saveData()
141{
142 //TODO....
143
144 //finally:
145 setDirty(false);
146 return true;
147}*/
148
150{
151 KexiDB::SchemaData *new_schema = new KexiDB::SchemaData();
152 *new_schema = sdata;
153
154 if (!m_mainWin->project()->dbConnection()
155 ->storeObjectSchemaData( *new_schema, true ))
156 {
157 delete new_schema;
158 new_schema=0;
159 }
160 m_newlyAssignedID = new_schema->id();
161 return new_schema;
162}
163
165{
166 Q_UNUSED(dontAsk);
167 if (!m_dialog || !m_dialog->schemaData())
168 return false;
169 if (!m_mainWin->project()->dbConnection()
170 ->storeObjectSchemaData( *m_dialog->schemaData(), false /*existing object*/ ))
171 {
172 return false;
173 }
174 setDirty(false);
175 return true;
176}
177
178bool KexiViewBase::loadDataBlock( TQString &dataString, const TQString& dataID, bool canBeEmpty )
179{
180 if (!m_dialog)
181 return false;
182 const tristate res = m_mainWin->project()->dbConnection()->loadDataBlock(m_dialog->id(), dataString, dataID);
183 if (canBeEmpty && ~res) {
184 dataString = TQString();
185 return true;
186 }
187 return res == true;
188}
189
190bool KexiViewBase::storeDataBlock( const TQString &dataString, const TQString &dataID )
191{
192 if (!m_dialog)
193 return false;
194 int effectiveID;
195 if (m_newlyAssignedID>0) {//ID not yet stored within dialog, but we've got ID here
196 effectiveID = m_newlyAssignedID;
197 m_newlyAssignedID = -1;
198 }
199 else
200 effectiveID = m_dialog->id();
201
202 return effectiveID>0
203 && m_mainWin->project()->dbConnection()->storeDataBlock(effectiveID, dataString, dataID);
204}
205
206bool KexiViewBase::removeDataBlock( const TQString& dataID )
207{
208 if (!m_dialog)
209 return false;
210 return m_mainWin->project()->dbConnection()->removeDataBlock(m_dialog->id(), dataID);
211}
212
213bool KexiViewBase::eventFilter( TQObject *o, TQEvent *e )
214{
215 if (e->type()==TQEvent::FocusIn || e->type()==TQEvent::FocusOut) {// && o->inherits("TQWidget")) {
216// //hp==true if currently focused widget is a child of this table view
217// const bool hp = Kexi::hasParent( o, focusWidget());
218// kexidbg << "KexiViewBase::eventFilter(): " << o->name() << " " << e->type() << endl;
219 if (KexiUtils::hasParent( this, o)) {
220 if (e->type()==TQEvent::FocusOut && focusWidget() && !KexiUtils::hasParent( this, focusWidget())) {
221 //focus out: when currently focused widget is not a parent of this view
222 emit focus(false);
223 } else if (e->type()==TQEvent::FocusIn) {
224 emit focus(true);
225 }
226 if (e->type()==TQEvent::FocusOut) { // && focusWidget() && Kexi::hasParent( this, focusWidget())) { // && focusWidget()->inherits("KexiViewBase")) {
227// kdDebug() << focusWidget()->className() << " " << focusWidget()->name()<< endl;
228// kdDebug() << o->className() << " " << o->name()<< endl;
229 KexiViewBase *v = KexiUtils::findParent<KexiViewBase>(o, "KexiViewBase") ;
230// TQWidget *www=v->focusWidget();
231 if (v) {
232 while (v->m_parentView)
233 v = v->m_parentView;
234 if (KexiUtils::hasParent( this, v->focusWidget() ))
235 v->m_lastFocusedChildBeforeFocusOut = v->focusWidget();
236// v->m_lastFocusedChildBeforeFocusOut = o; //focusWidget();
237 }
238 }
239
240 if (e->type()==TQEvent::FocusIn && m_actionProxyParent) {
241 m_actionProxyParent->m_focusedChild = this;
242 }
243// m_mainWin->invalidateSharedActions(this);
244 }
245 }
246 return false;
247}
248
249void KexiViewBase::setViewWidget(TQWidget* w, bool focusProxy)
250{
251 if (m_viewWidget == w)
252 return;
253 if (m_viewWidget) {
254 m_viewWidget->removeEventFilter(this);
255 }
256 m_viewWidget = w;
257 if (m_viewWidget) {
258 m_viewWidget->installEventFilter(this);
259 if (focusProxy)
260 setFocusProxy(m_viewWidget); //js: ok?
261 }
262}
263
264void KexiViewBase::addChildView( KexiViewBase* childView )
265{
266 m_children.append( childView );
267 addActionProxyChild( childView );
268 childView->m_parentView = this;
269// if (m_parentView)
270// childView->installEventFilter(m_parentView);
271 childView->installEventFilter(this);
272
273}
274
275void KexiViewBase::setFocus()
276{
277 if (!m_lastFocusedChildBeforeFocusOut.isNull()) {
278// kdDebug() << "FOCUS: " << m_lastFocusedChildBeforeFocusOut->className() << " " << m_lastFocusedChildBeforeFocusOut->name()<< endl;
279 TQWidget *w = m_lastFocusedChildBeforeFocusOut;
280 m_lastFocusedChildBeforeFocusOut = 0;
281 w->setFocus();
282 }
283 else {
284 if (hasFocus())
285 setFocusInternal();
286 else
287 setFocusInternal();
288 }
289 m_mainWin->invalidateSharedActions(this);
290}
291
292TDEAction* KexiViewBase::sharedAction( const char *action_name )
293{
294 if (part()) {
295 TDEActionCollection *ac;
296 if ( (ac = part()->actionCollectionForMode( viewMode() )) ) {
297 TDEAction* a = ac->action( action_name );
298 if (a)
299 return a;
300 }
301 }
302 return KexiActionProxy::sharedAction(action_name);
303}
304
305void KexiViewBase::setAvailable(const char* action_name, bool set)
306{
307 if (part()) {
308 TDEActionCollection *ac;
309 TDEAction* a;
310 if ( (ac = part()->actionCollectionForMode( viewMode() )) && (a = ac->action( action_name )) ) {
311 a->setEnabled(set);
312//why? return;
313 }
314 }
315 KexiActionProxy::setAvailable(action_name, set);
316}
317
318void KexiViewBase::updateActions(bool activated)
319{
320 //do nothing here
321 //do the same for children :)
322 for (TQPtrListIterator<KexiViewBase> it(m_children); it.current(); ++it) {
323 it.current()->updateActions(activated);
324 }
325}
326
327#include "kexiviewbase.moc"
328
An interface that acts as proxy for shared actions within the application.
void addActionProxyChild(KexiActionProxy *child)
virtual TDEAction * sharedAction(const char *action_name)
Base class for child window of Kexi's main application window.
Kexi's main window interface This interface is implemented by KexiMainWindowImpl class.
The main class for kexi frontend parts like tables, queries, forms and reports.
Definition kexipart.h:66
Base class for single view embeddable of in KexiDialogBase.
virtual TDEAction * sharedAction(const char *action_name)
virtual tristate beforeSwitchTo(int mode, bool &dontStore)
virtual tristate afterSwitchFrom(int mode)
virtual bool dirty() const
KexiPart::Part * part() const
virtual KexiDB::SchemaData * storeNewData(const KexiDB::SchemaData &sdata, bool &cancel)
virtual tristate storeData(bool dontAsk=false)
virtual void setAvailable(const char *action_name, bool set)
virtual void propertySetSwitched()
void propertySetReloaded(bool preservePrevSelection=false, const TQCString &propertyToSelect=TQCString())
int viewMode() const
virtual KoProperty::Set * propertySet()
bool removeDataBlock(const TQString &dataID=TQString())
bool storeDataBlock(const TQString &dataString, const TQString &dataID=TQString())
void closing(bool &cancel)
emitted when the view is about to close
bool loadDataBlock(TQString &dataString, const TQString &dataID=TQString(), bool canBeEmpty=false)
KexiDialogBase * parentDialog() const
virtual TQSize preferredSizeHint(const TQSize &otherSize)
virtual void updateActions(bool activated)
3-state logical type with three values: true, false and cancelled and convenient operators.
Definition tristate.h:100
type * findParent(TQObject *o, const char *className)
bool hasParent(TQObject *par, TQObject *o)