kexi

navigateaction.cpp

00001 /***************************************************************************
00002  * This file is part of the KDE project
00003  * copyright (C) 2006 by Sebastian Sauer (mail@dipe.org)
00004  * copyright (C) 2006 by Bernd Steindorff (bernd@itii.de)
00005  * copyright (C) 2006 by Sascha Kupper (kusato@kfnv.de)
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this program; see the file COPYING.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  ***************************************************************************/
00020 
00021 #include "navigateaction.h"
00022 
00023 #include <core/kexi.h>
00024 #include <core/kexiproject.h>
00025 #include <core/kexipartmanager.h>
00026 #include <core/kexipartinfo.h>
00027 #include <core/kexipart.h>
00028 #include <core/keximainwindow.h>
00029 #include <core/kexidialogbase.h>
00030 
00031 #include <widget/kexidataawareview.h>
00032 #include <widget/tableview/kexidataawareobjectiface.h>
00033 
00034 #include <klocale.h>
00035 #include <kdebug.h>
00036 
00037 using namespace KexiMacro;
00038 
00039 namespace KexiMacro {
00040 
00041     template<class ACTIONIMPL>
00042     class NavigateVariable : public KexiVariable<ACTIONIMPL>
00043     {
00044         public:
00045             NavigateVariable(ACTIONIMPL* actionimpl)
00046                 : KexiVariable<ACTIONIMPL>(actionimpl, "record", i18n("Record"))
00047             {
00048                 QStringList list;
00049                 list << "first" << "previous" << "next" << "last" << "goto";
00050                 this->appendChild( KSharedPtr<KoMacro::Variable>( new KoMacro::Variable(list, "@list") ) );
00051 
00052                 /*TODO should this actions belong to navigate? maybe it would be more wise to have
00053                 such kind of functionality in an own e.g. "Modify" action to outline, that
00054                 we are manipulating the database that way... */
00055                 //"add" << "save" << "delete" << "query" << "execute" << "cancel" << "reload"
00056 
00057                 this->setVariant( list[0] );
00058             }
00059     };
00060 
00061 }
00062 
00063 NavigateAction::NavigateAction()
00064     : KexiAction("navigate", i18n("Navigate"))
00065 {
00066     KoMacro::Variable* navvar = new NavigateVariable<NavigateAction>(this);
00067     setVariable(KSharedPtr<KoMacro::Variable>( navvar ));
00068 
00069     KoMacro::Variable* rowvar = new KexiVariable<NavigateAction>(this, "rownr", i18n("Row"));
00070     rowvar->setVariant(0);
00071     setVariable(KSharedPtr<KoMacro::Variable>(rowvar));
00072 
00073     KoMacro::Variable* colvar = new KexiVariable<NavigateAction>(this, "colnr", i18n("Column"));
00074     colvar->setVariant(0);
00075     setVariable(KSharedPtr<KoMacro::Variable>(colvar));
00076 }
00077 
00078 NavigateAction::~NavigateAction() 
00079 {
00080 }
00081 
00082 bool NavigateAction::notifyUpdated(KSharedPtr<KoMacro::MacroItem> macroitem, const QString& name)
00083 {
00084     kdDebug()<<"NavigateAction::notifyUpdated() name="<<name<<" macroitem.action="<<(macroitem->action() ? macroitem->action()->name() : "NOACTION")<<endl;
00085     KSharedPtr<KoMacro::Variable> variable = macroitem->variable(name, false);
00086     if(! variable) {
00087         kdWarning()<<"NavigateAction::notifyUpdated() No such variable="<<name<<" in macroitem."<<endl;
00088         return false;
00089     }
00090 
00091     variable->clearChildren();
00092     if(name == "goto") {
00093         const int rownr = macroitem->variant("rownr", true).toInt(); // e.g. "macro" or "script"
00094         const int colnr = macroitem->variant("colnr", true).toInt(); // e.g. "macro1" or "macro2" if objectvalue above is "macro"
00095 
00096         macroitem->variable("rownr", true)->setChildren(
00097             KoMacro::Variable::List() << KSharedPtr<KoMacro::Variable>(new KoMacro::Variable(rownr)) );
00098         macroitem->variable("colnr", true)->setChildren(
00099             KoMacro::Variable::List() << KSharedPtr<KoMacro::Variable>(new KoMacro::Variable(colnr)) );
00100     }
00101 
00102     return true;
00103 }
00104 
00105 void NavigateAction::activate(KSharedPtr<KoMacro::Context> context)
00106 {
00107     KexiDialogBase* dialog = dynamic_cast<KexiDialogBase*>( mainWin()->activeWindow() );
00108     if(! dialog) {
00109         throw KoMacro::Exception(i18n("No window active."));
00110     }
00111 
00112     KexiViewBase* view = dialog->selectedView();
00113     if(! view) {
00114         throw KoMacro::Exception(i18n("No view selected for \"%1\".").arg(dialog->caption()));
00115     }
00116 
00117     KexiDataAwareView* dbview = dynamic_cast<KexiDataAwareView*>( view );
00118     KexiDataAwareObjectInterface* dbobj = dbview ? dbview->dataAwareObject() : 0;
00119     if(! dbview) {
00120         throw KoMacro::Exception(i18n("The view for \"%1\" could not handle data.").arg(dialog->caption()));
00121     }
00122 
00123     const QString record = context->variable("record")->variant().toString();
00124     if(record == "previous") {
00125         dbobj->selectPrevRow();
00126     }
00127     else if(record == "next") {
00128         dbobj->selectNextRow();
00129     }
00130     else if(record == "first") {
00131         dbobj->selectFirstRow();
00132     }
00133     else if(record == "last") {
00134         dbobj->selectLastRow();
00135     }
00136     else if(record == "goto") {
00137         int rownr = context->variable("rownr")->variant().toInt() - 1;
00138         int colnr = context->variable("colnr")->variant().toInt() - 1;
00139         dbobj->setCursorPosition(rownr >= 0 ? rownr : dbobj->currentRow(), colnr >= 0 ? colnr : dbobj->currentColumn());
00140     }
00141     else {
00142         /*
00143         virtual void selectNextPage(); //!< page down action
00144         virtual void selectPrevPage(); //!< page up action
00145         void deleteAllRows();
00146         void deleteCurrentRow();
00147         void deleteAndStartEditCurrentCell();
00148         void startEditOrToggleValue();
00149         bool acceptRowEdit();
00150         void cancelRowEdit();
00151         void sortAscending();
00152         void sortDescending();
00153         */
00154         throw KoMacro::Exception(i18n("Unknown record \"%1\" in view for \"%2\".").arg(record).arg(dialog->caption()));
00155     }
00156 }
00157 
00158 //#include "navigateaction.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys