kexi

kexiformeventhandler.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 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 "kexiformeventhandler.h"
00021 
00022 #include <qwidget.h>
00023 #include <qobjectlist.h>
00024 
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 #include <kaction.h>
00028 
00029 #include <tableview/kexitableitem.h>
00030 #include <tableview/kexitableviewdata.h>
00031 #include <kexidb/queryschema.h>
00032 #include <keximainwindow.h>
00033 #include <kexidialogbase.h>
00034 #include <kexipart.h>
00035 //#include <kexipartitem.h>
00036 
00037 class KexiFormEventAction : public KAction
00038 {
00039 private:
00040     KexiMainWindow *m_mainWin;
00041     QString m_actionName, m_actionUri;
00042 public:
00043     KexiFormEventAction(KexiMainWindow *mainWin, QObject* parent, const QString& actionName, const QString& actionUri)
00044         : KAction(parent), m_mainWin(mainWin), m_actionName(actionName), m_actionUri(actionUri) {}
00045 public slots:
00046     void activate() {
00047         KexiPart::Part* part = Kexi::partManager().partForMimeType( QString("kexi/%1").arg(m_actionName) );
00048         KexiProject* project = m_mainWin->project();
00049         if( (! part) || (! project) )
00050             return;
00051 
00052         KexiPart::ItemDict* itemdict = project->items( part->info() );
00053         if(! itemdict)
00054             return;
00055 
00056         KexiPart::Item* item = 0;
00057         for(KexiPart::ItemDictIterator it(*itemdict); it.current(); ++it) {
00058             if(it.current()->name() == m_actionUri) {
00059                 item = it.current();
00060                 break;
00061             }
00062         }
00063 
00064         if(item)
00065             part->execute(item, parent());
00066     }
00067 };
00068 
00069 KexiFormEventHandler::KexiFormEventHandler()
00070     : m_mainWidget(0)
00071 {
00072 }
00073 
00074 KexiFormEventHandler::~KexiFormEventHandler()
00075 {
00076 }
00077 
00078 void KexiFormEventHandler::setMainWidgetForEventHandling(KexiMainWindow *mainWin, QWidget* mainWidget)
00079 {
00080     m_mainWidget = mainWidget;
00081     if (!m_mainWidget)
00082         return;
00083 
00084     //find widgets whose will work as data items
00086     QObjectList *l = m_mainWidget->queryList( "KexiPushButton" );
00087     QObjectListIt it( *l );
00088     QObject *obj;
00089     QDict<char> tmpSources;
00090     for ( ; (obj = it.current()) != 0; ++it ) {
00091         QString actionName = obj->property("onClickAction").toString();
00092         if (actionName.startsWith("kaction:")) {
00093             actionName = actionName.mid(QString("kaction:").length()); //cut prefix
00094             KAction *action = mainWin->actionCollection()->action( actionName.latin1() );
00095             if (!action)
00096                 continue;
00097             QObject::disconnect( obj, SIGNAL(clicked()), action, SLOT(activate()) ); //safety
00098             QObject::connect( obj, SIGNAL(clicked()), action, SLOT(activate()) );
00099         }
00100         else {
00101             QString actionUri;
00102             if (actionName.startsWith("macro:")) {
00103                 actionUri = actionName.mid(QString("macro:").length()); //cut prefix
00104                 actionName = "macro";
00105             }
00106             else if (actionName.startsWith("script:")) {
00107                 actionUri = actionName.mid(QString("script:").length()); //cut prefix
00108                 actionName = "script";
00109             }
00110             else {
00111                 continue; // ignore unknown actionName
00112             }
00113             KexiFormEventAction* action = new KexiFormEventAction(mainWin, obj, actionName, actionUri);
00114             QObject::disconnect( obj, SIGNAL(clicked()), action, SLOT(activate()) );
00115             QObject::connect( obj, SIGNAL(clicked()), action, SLOT(activate()) );
00116         }
00117     }
00118     delete l;
00119 }
00120 
KDE Home | KDE Accessibility Home | Description of Access Keys