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, const QCString& propertyToSelect)
00111 {
00112     if (parentDialog())
00113         m_mainWin->propertySetSwitched( parentDialog(), true, preservePrevSelection, propertyToSelect );
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(this);
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, bool canBeEmpty )
00179 {
00180     if (!m_dialog)
00181         return false;
00182     const tristate res = m_mainWin->project()->dbConnection()->loadDataBlock(m_dialog->id(), dataString, dataID);
00183     if (canBeEmpty && ~res) {
00184         dataString = QString::null;
00185         return true;
00186     }
00187     return res == true;
00188 }
00189 
00190 bool KexiViewBase::storeDataBlock( const QString &dataString, const QString &dataID )
00191 {
00192     if (!m_dialog)
00193         return false;
00194     int effectiveID;
00195     if (m_newlyAssignedID>0) {//ID not yet stored within dialog, but we've got ID here
00196         effectiveID = m_newlyAssignedID;
00197         m_newlyAssignedID = -1;
00198     }
00199     else
00200         effectiveID = m_dialog->id();
00201 
00202     return effectiveID>0
00203         && m_mainWin->project()->dbConnection()->storeDataBlock(effectiveID, dataString, dataID);
00204 }
00205 
00206 bool KexiViewBase::removeDataBlock( const QString& dataID )
00207 {
00208     if (!m_dialog)
00209         return false;
00210     return m_mainWin->project()->dbConnection()->removeDataBlock(m_dialog->id(), dataID);
00211 }
00212 
00213 bool KexiViewBase::eventFilter( QObject *o, QEvent *e )
00214 {
00215     if (e->type()==QEvent::FocusIn || e->type()==QEvent::FocusOut) {// && o->inherits("QWidget")) {
00216 //      //hp==true if currently focused widget is a child of this table view
00217 //      const bool hp = Kexi::hasParent( static_cast<QWidget*>(o), focusWidget());
00218 //      kexidbg << "KexiViewBase::eventFilter(): " << o->name() << " " << e->type() << endl;
00219         if (KexiUtils::hasParent( this, static_cast<QWidget*>(o))) {
00220             if (e->type()==QEvent::FocusOut && focusWidget() && !KexiUtils::hasParent( this, focusWidget())) {
00221                 //focus out: when currently focused widget is not a parent of this view
00222                 emit focus(false);
00223             } else if (e->type()==QEvent::FocusIn) {
00224                 emit focus(true);
00225             }
00226             if (e->type()==QEvent::FocusOut) { // && focusWidget() && Kexi::hasParent( this, focusWidget())) { // && focusWidget()->inherits("KexiViewBase")) {
00227 //              kdDebug() << focusWidget()->className() << " " << focusWidget()->name()<< endl;
00228 //              kdDebug() << o->className() << " " << o->name()<< endl;
00229                 KexiViewBase *v = KexiUtils::findParent<KexiViewBase>(o, "KexiViewBase") ;
00230 //              QWidget *www=v->focusWidget();
00231                 if (v) {
00232                     while (v->m_parentView)
00233                         v = v->m_parentView;
00234                     if (KexiUtils::hasParent( this, static_cast<QWidget*>(v->focusWidget()) ))
00235                         v->m_lastFocusedChildBeforeFocusOut = static_cast<QWidget*>(v->focusWidget());
00236 //                  v->m_lastFocusedChildBeforeFocusOut = static_cast<QWidget*>(o); //focusWidget();
00237                 }
00238             }
00239 
00240             if (e->type()==QEvent::FocusIn && m_actionProxyParent) {
00241                 m_actionProxyParent->m_focusedChild = this;
00242             }
00243 //          m_mainWin->invalidateSharedActions(this);
00244         }
00245     }
00246     return false;
00247 }
00248 
00249 void KexiViewBase::setViewWidget(QWidget* w, bool focusProxy)
00250 {
00251     if (m_viewWidget == w)
00252         return;
00253     if (m_viewWidget) {
00254         m_viewWidget->removeEventFilter(this);
00255     }
00256     m_viewWidget = w;
00257     if (m_viewWidget) {
00258         m_viewWidget->installEventFilter(this);
00259         if (focusProxy)
00260             setFocusProxy(m_viewWidget); //js: ok?
00261     }
00262 }
00263 
00264 void KexiViewBase::addChildView( KexiViewBase* childView )
00265 {
00266     m_children.append( childView );
00267     addActionProxyChild( childView );
00268     childView->m_parentView = this;
00269 //  if (m_parentView)
00270 //      childView->installEventFilter(m_parentView);
00271     childView->installEventFilter(this);
00272 
00273 }
00274 
00275 void KexiViewBase::setFocus()
00276 {
00277     if (!m_lastFocusedChildBeforeFocusOut.isNull()) {
00278 //      kdDebug() << "FOCUS: " << m_lastFocusedChildBeforeFocusOut->className() << " " << m_lastFocusedChildBeforeFocusOut->name()<< endl;
00279         QWidget *w = m_lastFocusedChildBeforeFocusOut;
00280         m_lastFocusedChildBeforeFocusOut = 0;
00281         w->setFocus();
00282     }
00283     else {
00284         if (hasFocus())
00285             setFocusInternal();
00286         else
00287             setFocusInternal();
00288     }
00289     m_mainWin->invalidateSharedActions(this);
00290 }
00291 
00292 KAction* KexiViewBase::sharedAction( const char *action_name )
00293 {
00294     if (part()) {
00295         KActionCollection *ac;
00296         if ( (ac = part()->actionCollectionForMode( viewMode() )) ) {
00297             KAction* a = ac->action( action_name );
00298             if (a)
00299                 return a;
00300         }
00301     }
00302     return KexiActionProxy::sharedAction(action_name);
00303 }
00304 
00305 void KexiViewBase::setAvailable(const char* action_name, bool set)
00306 {
00307     if (part()) {
00308         KActionCollection *ac;
00309         KAction* a;
00310         if ( (ac = part()->actionCollectionForMode( viewMode() )) && (a = ac->action( action_name )) ) {
00311             a->setEnabled(set);
00312 //why?          return;
00313         }
00314     }
00315     KexiActionProxy::setAvailable(action_name, set);
00316 }
00317 
00318 void KexiViewBase::updateActions(bool activated)
00319 {
00320     //do nothing here
00321     //do the same for children :)
00322     for (QPtrListIterator<KexiViewBase> it(m_children); it.current(); ++it) {
00323         it.current()->updateActions(activated);
00324     }
00325 }
00326 
00327 #include "kexiviewbase.moc"
00328 
KDE Home | KDE Accessibility Home | Description of Access Keys