kexi

kexiformeventhandler.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005-2006 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 
00028 #include <tableview/kexitableitem.h>
00029 #include <tableview/kexitableviewdata.h>
00030 #include <kexidb/queryschema.h>
00031 #include <keximainwindow.h>
00032 #include <kexidialogbase.h>
00033 #include <kexipart.h>
00034 #include <kexipartinfo.h>
00035 #include <kexipartitem.h>
00036 
00037 KexiFormEventAction::ActionData::ActionData()
00038 {
00039 }
00040 
00041 bool KexiFormEventAction::ActionData::isEmpty() const
00042 {
00043     return string.isEmpty();
00044 }
00045 
00046 KexiPart::Info* KexiFormEventAction::ActionData::decodeString(
00047     QString& actionType, QString& actionArg, bool& ok) const
00048 {
00049     const int idx = string.find(':');
00050     ok = false;
00051     if (idx==-1)
00052         return 0;
00053     const QString _actionType = string.left(idx);
00054     const QString _actionArg = string.mid(idx+1);
00055     if (_actionType.isEmpty() || _actionArg.isEmpty())
00056         return 0;
00057     KexiPart::Info *info = 0;
00058     if (_actionType!="kaction" && _actionType!="currentForm") {
00059         info = Kexi::partManager().infoForMimeType( QString("kexi/%1").arg(_actionType) );
00060         if (!info)
00061             return 0;
00062     }
00063     actionType = _actionType;
00064     actionArg = _actionArg;
00065     ok = true;
00066     return info;
00067 }
00068 
00069 //-------------------------------------
00070 
00071 KexiFormEventAction::KexiFormEventAction(KexiMainWindow *mainWin, QObject* parent, 
00072     const QString& actionName, const QString& objectName, const QString& actionOption)
00073  : KAction(parent), m_mainWin(mainWin), m_actionName(actionName), m_objectName(objectName)
00074  , m_actionOption(actionOption)
00075 {
00076 }
00077 
00078 KexiFormEventAction::~KexiFormEventAction()
00079 {
00080 }
00081 
00082 void KexiFormEventAction::activate()
00083 {
00084     KexiProject* project = m_mainWin->project();
00085     if (!project)
00086         return;
00087     KexiPart::Part* part = Kexi::partManager().partForMimeType( 
00088         QString("kexi/%1").arg(m_actionName) );
00089     if (!part)
00090         return;
00091     KexiPart::Item* item = project->item( part->info(), m_objectName );
00092     if (!item)
00093         return;
00094     bool actionCancelled = false;
00095     if (m_actionOption.isEmpty()) { // backward compatibility (good defaults)
00096         if (part->info()->isExecuteSupported())
00097             part->execute(item, parent());
00098         else
00099             m_mainWin->openObject(item, Kexi::DataViewMode, actionCancelled);
00100     }
00101     else {
00103         if (m_actionOption == "open")
00104             m_mainWin->openObject(item, Kexi::DataViewMode, actionCancelled);
00105         else if (m_actionOption == "execute")
00106             part->execute(item, parent());
00107         else if (m_actionOption == "print") {
00108             if (part->info()->isPrintingSupported())
00109                 m_mainWin->printItem(item);
00110         }
00111         else if (m_actionOption == "printPreview") {
00112             if (part->info()->isPrintingSupported())
00113                 m_mainWin->printPreviewForItem(item);
00114         }
00115         else if (m_actionOption == "pageSetup") {
00116             if (part->info()->isPrintingSupported())
00117                 m_mainWin->showPageSetupForItem(item);
00118         }
00119         else if (m_actionOption == "exportToCSV"
00120             || m_actionOption == "copyToClipboardAsCSV")
00121         {
00122             if (part->info()->isDataExportSupported())
00123                 m_mainWin->executeCustomActionForObject(item, m_actionOption);
00124         }
00125         else if (m_actionOption == "new")
00126             m_mainWin->newObject( part->info(), actionCancelled );
00127         else if (m_actionOption == "design")
00128             m_mainWin->openObject(item, Kexi::DesignViewMode, actionCancelled);
00129         else if (m_actionOption == "editText")
00130             m_mainWin->openObject(item, Kexi::TextViewMode, actionCancelled);
00131         else if (m_actionOption == "close") {
00132             tristate res = m_mainWin->closeObject(item);
00133             if (~res)
00134                 actionCancelled = true;
00135         }
00136     }
00137 }
00138 
00139 //------------------------------------------
00140 
00141 KexiFormEventHandler::KexiFormEventHandler()
00142     : m_mainWidget(0)
00143 {
00144 }
00145 
00146 KexiFormEventHandler::~KexiFormEventHandler()
00147 {
00148 }
00149 
00150 void KexiFormEventHandler::setMainWidgetForEventHandling(KexiMainWindow *mainWin, QWidget* mainWidget)
00151 {
00152     m_mainWidget = mainWidget;
00153     if (!m_mainWidget)
00154         return;
00155 
00156     //find widgets whose will work as data items
00158     QObjectList *l = m_mainWidget->queryList( "KexiPushButton" );
00159     QObjectListIt it( *l );
00160     QObject *obj;
00161     for ( ; (obj = it.current()) != 0; ++it ) {
00162         bool ok;
00163         KexiFormEventAction::ActionData data;
00164         data.string = obj->property("onClickAction").toString();
00165         data.option = obj->property("onClickActionOption").toString();
00166         if (data.isEmpty())
00167             continue;
00168 
00169         QString actionType, actionArg;
00170         KexiPart::Info* partInfo = data.decodeString(actionType, actionArg, ok);
00171         if (!ok)
00172             continue;
00173         if (actionType=="kaction" || actionType=="currentForm") {
00174             KAction *action = mainWin->actionCollection()->action( actionArg.latin1() );
00175             if (!action)
00176                 continue;
00177             QObject::disconnect( obj, SIGNAL(clicked()), action, SLOT(activate()) ); //safety
00178             QObject::connect( obj, SIGNAL(clicked()), action, SLOT(activate()) );
00179         }
00180         else if (partInfo) { //'open or execute' action
00181             KexiFormEventAction* action = new KexiFormEventAction(mainWin, obj, actionType, actionArg, 
00182                 data.option);
00183             QObject::disconnect( obj, SIGNAL(clicked()), action, SLOT(activate()) );
00184             QObject::connect( obj, SIGNAL(clicked()), action, SLOT(activate()) );
00185         }
00186     }
00187     delete l;
00188 }
KDE Home | KDE Accessibility Home | Description of Access Keys