kexi

kexiviewbase.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2004 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 "kexiviewbase.h"
00021 
00022 #include "keximainwindow.h"
00023 #include "kexidialogbase.h"
00024 #include "kexiproject.h"
00025 #include <koproperty/set.h>
00026 
00027 #include <kexidb/connection.h>
00028 #include <kexidb/utils.h>
00029 #include <kexiutils/utils.h>
00030 
00031 #include <kdebug.h>
00032 
00033 KexiViewBase::KexiViewBase(KexiMainWindow *mainWin, QWidget *parent, const char *name)
00034  : QWidget(parent, name)
00035  , KexiActionProxy(this, mainWin)
00036  , m_mainWin(mainWin)
00037  , m_viewWidget(0)
00038  , m_parentView(0)
00039  , m_newlyAssignedID(-1)
00040  , m_viewMode(0) //unknown!
00041  , m_dirty(false)
00042 {
00043     QWidget *wi=this;
00044     while ((wi = wi->parentWidget()) && !wi->inherits("KexiDialogBase"))
00045         ;
00046     m_dialog = (wi && wi->inherits("KexiDialogBase")) ? static_cast<KexiDialogBase*>(wi) : 0;
00047     if (m_dialog) {
00048         //init view mode number for this view (obtained from dialog where this view is created)
00049         if (m_dialog->supportsViewMode(m_dialog->m_creatingViewsMode))
00050             m_viewMode = m_dialog->m_creatingViewsMode;
00051     }
00052 
00053     installEventFilter(this);
00054 }
00055 
00056 KexiViewBase::~KexiViewBase()
00057 {
00058 }
00059 
00060 KexiPart::Part* KexiViewBase::part() const
00061 {
00062     return m_dialog ? m_dialog->part() : 0;
00063 }
00064 
00065 tristate KexiViewBase::beforeSwitchTo(int /* mode */, bool & /*dontStore*/)
00066 {
00067     return true;
00068 }
00069 
00070 tristate KexiViewBase::afterSwitchFrom(int /* mode */)
00071 {
00072     return true;
00073 }
00074 
00075 QSize KexiViewBase::preferredSizeHint(const QSize& otherSize)
00076 {
00077     KexiDialogBase* dlg = parentDialog();
00078     if (dlg && dlg->mdiParent()) {
00079         QRect r = dlg->mdiParent()->mdiAreaContentsRect();
00080         return otherSize.boundedTo( QSize(
00081             r.width() - 10,
00082             r.height() - dlg->mdiParent()->captionHeight() - dlg->pos().y() - 10
00083         ) );
00084     }
00085     return otherSize;
00086 }
00087 
00088 void KexiViewBase::closeEvent( QCloseEvent * e )
00089 {
00090     bool cancel = false;
00091     emit closing(cancel);
00092     if (cancel) {
00093         e->ignore();
00094         return;
00095     }
00096     QWidget::closeEvent(e);
00097 }
00098 
00099 KoProperty::Set *KexiViewBase::propertySet()
00100 {
00101     return 0;
00102 }
00103 
00104 void KexiViewBase::propertySetSwitched()
00105 {
00106     if (parentDialog())
00107         m_mainWin->propertySetSwitched( parentDialog(), false );
00108 }
00109 
00110 void KexiViewBase::propertySetReloaded(bool preservePrevSelection)
00111 {
00112     if (parentDialog())
00113         m_mainWin->propertySetSwitched( parentDialog(), true, preservePrevSelection );
00114 }
00115 
00116 void KexiViewBase::setDirty(bool set)
00117 {
00118 /*  if (m_dirty == set) {//no change here
00119         if (m_dialog) {
00120             // however, it's a change from dialog perspective
00121             if (m_dialog->dirty()!=set)
00122                 m_dialog->dirtyChanged();
00123         }
00124         return;
00125     }*/
00126     const bool changed = (m_dirty != set);
00127     m_dirty = set;
00128     m_dirty = dirty();
00129 //  if (m_dirty!=set)//eventually didn't change
00130 //      return;
00131     if (m_parentView) {
00132         m_parentView->setDirty(m_dirty);
00133     }
00134     else {
00135         if (changed && m_dialog)
00136             m_dialog->dirtyChanged();
00137     }
00138 }
00139 
00140 /*bool KexiViewBase::saveData()
00141 {
00142     //TODO....
00143 
00144     //finally:
00145     setDirty(false);
00146     return true;
00147 }*/
00148 
00149 KexiDB::SchemaData* KexiViewBase::storeNewData(const KexiDB::SchemaData& sdata, bool & /*cancel*/)
00150 {
00151     KexiDB::SchemaData *new_schema = new KexiDB::SchemaData();
00152     *new_schema = sdata;
00153 
00154     if (!m_mainWin->project()->dbConnection()
00155             ->storeObjectSchemaData( *new_schema, true ))
00156     {
00157         delete new_schema;
00158         new_schema=0;
00159     }
00160     m_newlyAssignedID = new_schema->id();
00161     return new_schema;
00162 }
00163 
00164 tristate KexiViewBase::storeData(bool dontAsk)
00165 {
00166     Q_UNUSED(dontAsk);
00167     if (!m_dialog || !m_dialog->schemaData())
00168         return false;
00169     if (!m_mainWin->project()->dbConnection()
00170             ->storeObjectSchemaData( *m_dialog->schemaData(), false /*existing object*/ ))
00171     {
00172         return false;
00173     }
00174     setDirty(false);
00175     return true;
00176 }
00177 
00178 bool KexiViewBase::loadDataBlock( QString &dataString, const QString& dataID )
00179 {
00180     if (!m_dialog)
00181         return false;
00182     return m_mainWin->project()->dbConnection()->loadDataBlock(m_dialog->id(), dataString, dataID);
00183 }
00184 
00185 bool KexiViewBase::storeDataBlock( const QString &dataString, const QString &dataID )
00186 {
00187     if (!m_dialog)
00188         return false;
00189     int effectiveID;
00190     if (m_newlyAssignedID>0) {//ID not yet stored within dialog, but we've got ID here
00191         effectiveID = m_newlyAssignedID;
00192         m_newlyAssignedID = -1;
00193     }
00194     else
00195         effectiveID = m_dialog->id();
00196 
00197     return effectiveID>0
00198         && m_mainWin->project()->dbConnection()->storeDataBlock(effectiveID, dataString, dataID);
00199 }
00200 
00201 bool KexiViewBase::removeDataBlock( const QString& dataID )
00202 {
00203     if (!m_dialog)
00204         return false;
00205     return m_mainWin->project()->dbConnection()->removeDataBlock(m_dialog->id(), dataID);
00206 }
00207 
00208 bool KexiViewBase::eventFilter( QObject *o, QEvent *e )
00209 {
00210     if (e->type()==QEvent::FocusIn || e->type()==QEvent::FocusOut) {// && o->inherits("QWidget")) {
00211 //      //hp==true if currently focused widget is a child of this table view
00212 //      const bool hp = Kexi::hasParent( static_cast<QWidget*>(o), focusWidget());
00213 //      kexidbg << "KexiViewBase::eventFilter(): " << o->name() << " " << e->type() << endl;
00214         if (KexiUtils::hasParent( this, static_cast<QWidget*>(o))) {
00215             if (e->type()==QEvent::FocusOut && focusWidget() && !KexiUtils::hasParent( this, focusWidget())) {
00216                 //focus out: when currently focused widget is not a parent of this view
00217                 emit focus(false);
00218             } else if (e->type()==QEvent::FocusIn) {
00219                 emit focus(true);
00220             }
00221             if (e->type()==QEvent::FocusOut) { // && focusWidget() && Kexi::hasParent( this, focusWidget())) { // && focusWidget()->inherits("KexiViewBase")) {
00222 //              kdDebug() << focusWidget()->className() << " " << focusWidget()->name()<< endl;
00223 //              kdDebug() << o->className() << " " << o->name()<< endl;
00224                 KexiViewBase *v = KexiUtils::findParent<KexiViewBase>(o, "KexiViewBase") ;
00225 //              QWidget *www=v->focusWidget();
00226                 if (v) {
00227                     while (v->m_parentView)
00228                         v = v->m_parentView;
00229                     v->m_lastFocusedChildBeforeFocusOut = static_cast<QWidget*>(v->focusWidget());
00230 //                  v->m_lastFocusedChildBeforeFocusOut = static_cast<QWidget*>(o); //focusWidget();
00231                 }
00232             }
00233 
00234             if (e->type()==QEvent::FocusIn && m_actionProxyParent) {
00235                 m_actionProxyParent->m_focusedChild = this;
00236             }
00237 //          m_mainWin->invalidateSharedActions(this);
00238         }
00239     }
00240     return false;
00241 }
00242 
00243 void KexiViewBase::setViewWidget(QWidget* w, bool focusProxy)
00244 {
00245     if (m_viewWidget == w)
00246         return;
00247     if (m_viewWidget) {
00248         m_viewWidget->removeEventFilter(this);
00249     }
00250     m_viewWidget = w;
00251     if (m_viewWidget) {
00252         m_viewWidget->installEventFilter(this);
00253         if (focusProxy)
00254             setFocusProxy(m_viewWidget); //js: ok?
00255     }
00256 }
00257 
00258 void KexiViewBase::addChildView( KexiViewBase* childView )
00259 {
00260     m_children.append( childView );
00261     addActionProxyChild( childView );
00262     childView->m_parentView = this;
00263 //  if (m_parentView)
00264 //      childView->installEventFilter(m_parentView);
00265     childView->installEventFilter(this);
00266 
00267 }
00268 
00269 void KexiViewBase::setFocus()
00270 {
00271     if (!m_lastFocusedChildBeforeFocusOut.isNull()) {
00272 //      kdDebug() << "FOCUS: " << m_lastFocusedChildBeforeFocusOut->className() << " " << m_lastFocusedChildBeforeFocusOut->name()<< endl;
00273         QWidget *w = m_lastFocusedChildBeforeFocusOut;
00274         m_lastFocusedChildBeforeFocusOut = 0;
00275         w->setFocus();
00276     }
00277     else {
00278         if (hasFocus())
00279             setFocusInternal();
00280         else
00281             setFocusInternal();
00282     }
00283     m_mainWin->invalidateSharedActions(this);
00284 }
00285 
00286 KAction* KexiViewBase::sharedAction( const char *action_name )
00287 {
00288     if (part()) {
00289         KActionCollection *ac;
00290         if ( (ac = part()->actionCollectionForMode( viewMode() )) ) {
00291             KAction* a = ac->action( action_name );
00292             if (a)
00293                 return a;
00294         }
00295     }
00296     return KexiActionProxy::sharedAction(action_name);
00297 }
00298 
00299 void KexiViewBase::setAvailable(const char* action_name, bool set)
00300 {
00301     if (part()) {
00302         KActionCollection *ac;
00303         KAction* a;
00304         if ( (ac = part()->actionCollectionForMode( viewMode() )) && (a = ac->action( action_name )) ) {
00305             a->setEnabled(set);
00306 //why?          return;
00307         }
00308     }
00309     KexiActionProxy::setAvailable(action_name, set);
00310 }
00311 
00312 void KexiViewBase::updateActions(bool activated)
00313 {
00314     //do nothing here
00315     //do the same for children :)
00316     for (QPtrListIterator<KexiViewBase> it(m_children); it.current(); ++it) {
00317         it.current()->updateActions(activated);
00318     }
00319 }
00320 
00321 #include "kexiviewbase.moc"
00322 
KDE Home | KDE Accessibility Home | Description of Access Keys