kexi
kexiviewbase.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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)
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
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 , bool & )
00066 {
00067 return true;
00068 }
00069
00070 tristate KexiViewBase::afterSwitchFrom(int )
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
00119
00120
00121
00122
00123
00124
00125
00126 const bool changed = (m_dirty != set);
00127 m_dirty = set;
00128 m_dirty = dirty();
00129
00130
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
00141
00142
00143
00144
00145
00146
00147
00148
00149 KexiDB::SchemaData* KexiViewBase::storeNewData(const KexiDB::SchemaData& sdata, bool & )
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 ))
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) {
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) {
00216
00217
00218
00219 if (KexiUtils::hasParent( this, static_cast<QWidget*>(o))) {
00220 if (e->type()==QEvent::FocusOut && focusWidget() && !KexiUtils::hasParent( this, focusWidget())) {
00221
00222 emit focus(false);
00223 } else if (e->type()==QEvent::FocusIn) {
00224 emit focus(true);
00225 }
00226 if (e->type()==QEvent::FocusOut) {
00227
00228
00229 KexiViewBase *v = KexiUtils::findParent<KexiViewBase>(o, "KexiViewBase") ;
00230
00231 if (v) {
00232 while (v->m_parentView)
00233 v = v->m_parentView;
00234 v->m_lastFocusedChildBeforeFocusOut = static_cast<QWidget*>(v->focusWidget());
00235
00236 }
00237 }
00238
00239 if (e->type()==QEvent::FocusIn && m_actionProxyParent) {
00240 m_actionProxyParent->m_focusedChild = this;
00241 }
00242
00243 }
00244 }
00245 return false;
00246 }
00247
00248 void KexiViewBase::setViewWidget(QWidget* w, bool focusProxy)
00249 {
00250 if (m_viewWidget == w)
00251 return;
00252 if (m_viewWidget) {
00253 m_viewWidget->removeEventFilter(this);
00254 }
00255 m_viewWidget = w;
00256 if (m_viewWidget) {
00257 m_viewWidget->installEventFilter(this);
00258 if (focusProxy)
00259 setFocusProxy(m_viewWidget);
00260 }
00261 }
00262
00263 void KexiViewBase::addChildView( KexiViewBase* childView )
00264 {
00265 m_children.append( childView );
00266 addActionProxyChild( childView );
00267 childView->m_parentView = this;
00268
00269
00270 childView->installEventFilter(this);
00271
00272 }
00273
00274 void KexiViewBase::setFocus()
00275 {
00276 if (!m_lastFocusedChildBeforeFocusOut.isNull()) {
00277
00278 QWidget *w = m_lastFocusedChildBeforeFocusOut;
00279 m_lastFocusedChildBeforeFocusOut = 0;
00280 w->setFocus();
00281 }
00282 else {
00283 if (hasFocus())
00284 setFocusInternal();
00285 else
00286 setFocusInternal();
00287 }
00288 m_mainWin->invalidateSharedActions(this);
00289 }
00290
00291 KAction* KexiViewBase::sharedAction( const char *action_name )
00292 {
00293 if (part()) {
00294 KActionCollection *ac;
00295 if ( (ac = part()->actionCollectionForMode( viewMode() )) ) {
00296 KAction* a = ac->action( action_name );
00297 if (a)
00298 return a;
00299 }
00300 }
00301 return KexiActionProxy::sharedAction(action_name);
00302 }
00303
00304 void KexiViewBase::setAvailable(const char* action_name, bool set)
00305 {
00306 if (part()) {
00307 KActionCollection *ac;
00308 KAction* a;
00309 if ( (ac = part()->actionCollectionForMode( viewMode() )) && (a = ac->action( action_name )) ) {
00310 a->setEnabled(set);
00311
00312 }
00313 }
00314 KexiActionProxy::setAvailable(action_name, set);
00315 }
00316
00317 void KexiViewBase::updateActions(bool activated)
00318 {
00319
00320
00321 for (QPtrListIterator<KexiViewBase> it(m_children); it.current(); ++it) {
00322 it.current()->updateActions(activated);
00323 }
00324 }
00325
00326 #include "kexiviewbase.moc"
00327
|