navigateaction.cpp
1/***************************************************************************
2 * This file is part of the KDE project
3 * copyright (C) 2006 by Sebastian Sauer (mail@dipe.org)
4 * copyright (C) 2006 by Bernd Steindorff (bernd@itii.de)
5 * copyright (C) 2006 by Sascha Kupper (kusato@kfnv.de)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this program; see the file COPYING. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 ***************************************************************************/
20
21#include "navigateaction.h"
22
23#include <core/kexi.h>
24#include <core/kexiproject.h>
25#include <core/kexipartmanager.h>
26#include <core/kexipartinfo.h>
27#include <core/kexipart.h>
28#include <core/keximainwindow.h>
29#include <core/kexidialogbase.h>
30
31#include <widget/kexidataawareview.h>
32#include <widget/tableview/kexidataawareobjectiface.h>
33
34#include <tdelocale.h>
35#include <kdebug.h>
36
37using namespace KexiMacro;
38
39namespace KexiMacro {
40
41 template<class ACTIONIMPL>
42 class NavigateVariable : public KexiVariable<ACTIONIMPL>
43 {
44 public:
45 NavigateVariable(ACTIONIMPL* actionimpl)
46 : KexiVariable<ACTIONIMPL>(actionimpl, "record", i18n("Record"))
47 {
48 TQStringList list;
49 list << "first" << "previous" << "next" << "last" << "goto";
50 this->appendChild( TDESharedPtr<KoMacro::Variable>( new KoMacro::Variable(list, "@list") ) );
51
52 /*TODO should this actions belong to navigate? maybe it would be more wise to have
53 such kind of functionality in an own e.g. "Modify" action to outline, that
54 we are manipulating the database that way... */
55 //"add" << "save" << "delete" << "query" << "execute" << "cancel" << "reload"
56
57 this->setVariant( list[0] );
58 }
59 };
60
61}
62
64 : KexiAction("navigate", i18n("Navigate"))
65{
66 KoMacro::Variable* navvar = new NavigateVariable<NavigateAction>(this);
67 setVariable(TDESharedPtr<KoMacro::Variable>( navvar ));
68
69 KoMacro::Variable* rowvar = new KexiVariable<NavigateAction>(this, "rownr", i18n("Row"));
70 rowvar->setVariant(0);
71 setVariable(TDESharedPtr<KoMacro::Variable>(rowvar));
72
73 KoMacro::Variable* colvar = new KexiVariable<NavigateAction>(this, "colnr", i18n("Column"));
74 colvar->setVariant(0);
75 setVariable(TDESharedPtr<KoMacro::Variable>(colvar));
76}
77
81
82bool NavigateAction::notifyUpdated(TDESharedPtr<KoMacro::MacroItem> macroitem, const TQString& name)
83{
84 kdDebug()<<"NavigateAction::notifyUpdated() name="<<name<<" macroitem.action="<<(macroitem->action() ? macroitem->action()->name() : "NOACTION")<<endl;
85 TDESharedPtr<KoMacro::Variable> variable = macroitem->variable(name, false);
86 if(! variable) {
87 kdWarning()<<"NavigateAction::notifyUpdated() No such variable="<<name<<" in macroitem."<<endl;
88 return false;
89 }
90
91 variable->clearChildren();
92 if(name == "goto") {
93 const int rownr = macroitem->variant("rownr", true).toInt(); // e.g. "macro" or "script"
94 const int colnr = macroitem->variant("colnr", true).toInt(); // e.g. "macro1" or "macro2" if objectvalue above is "macro"
95
96 macroitem->variable("rownr", true)->setChildren(
97 KoMacro::Variable::List() << TDESharedPtr<KoMacro::Variable>(new KoMacro::Variable(rownr)) );
98 macroitem->variable("colnr", true)->setChildren(
99 KoMacro::Variable::List() << TDESharedPtr<KoMacro::Variable>(new KoMacro::Variable(colnr)) );
100 }
101
102 return true;
103}
104
105void NavigateAction::activate(TDESharedPtr<KoMacro::Context> context)
106{
107 KexiDialogBase* dialog = dynamic_cast<KexiDialogBase*>( mainWin()->activeWindow() );
108 if(! dialog) {
109 throw KoMacro::Exception(i18n("No window active."));
110 }
111
112 KexiViewBase* view = dialog->selectedView();
113 if(! view) {
114 throw KoMacro::Exception(i18n("No view selected for \"%1\".").arg(dialog->caption()));
115 }
116
117 KexiDataAwareView* dbview = dynamic_cast<KexiDataAwareView*>( view );
118 KexiDataAwareObjectInterface* dbobj = dbview ? dbview->dataAwareObject() : 0;
119 if(! dbview) {
120 throw KoMacro::Exception(i18n("The view for \"%1\" could not handle data.").arg(dialog->caption()));
121 }
122
123 const TQString record = context->variable("record")->variant().toString();
124 if(record == "previous") {
125 dbobj->selectPrevRow();
126 }
127 else if(record == "next") {
128 dbobj->selectNextRow();
129 }
130 else if(record == "first") {
131 dbobj->selectFirstRow();
132 }
133 else if(record == "last") {
134 dbobj->selectLastRow();
135 }
136 else if(record == "goto") {
137 int rownr = context->variable("rownr")->variant().toInt() - 1;
138 int colnr = context->variable("colnr")->variant().toInt() - 1;
139 dbobj->setCursorPosition(rownr >= 0 ? rownr : dbobj->currentRow(), colnr >= 0 ? colnr : dbobj->currentColumn());
140 }
141 else {
142 /*
143 virtual void selectNextPage(); //!< page down action
144 virtual void selectPrevPage(); //!< page up action
145 void deleteAllRows();
146 void deleteCurrentRow();
147 void deleteAndStartEditCurrentCell();
148 void startEditOrToggleValue();
149 bool acceptRowEdit();
150 void cancelRowEdit();
151 void sortAscending();
152 void sortDescending();
153 */
154 throw KoMacro::Exception(i18n("Unknown record \"%1\" in view for \"%2\".").arg(record).arg(dialog->caption()));
155 }
156}
157
158//#include "navigateaction.moc"
The KexiDataAwareObjectInterface is an interface for record-based data object.
virtual void setCursorPosition(int row, int col=-1, bool forceSet=false)
Provides a view displaying record-based data.
Base class for child window of Kexi's main application window.
KexiViewBase * selectedView() const
Template class to offer common functionality needed by all KoMacro::Action implementations Kexi provi...
Definition kexiaction.h:44
KexiMainWindow * mainWin() const
Template class to offer common functionality needed by all KoMacro::Variable implementations Kexi pro...
virtual ~NavigateAction()
Destructor.
virtual bool notifyUpdated(TDESharedPtr< KoMacro::MacroItem > macroitem, const TQString &name)
This function is called, when the KoMacro::Variable with name name used within the KoMacro::MacroItem...
virtual void activate(TDESharedPtr< KoMacro::Context > context)
Called if the Action should be executed within the defined context .
Base class for single view embeddable of in KexiDialogBase.
TDESharedPtr< Variable > variable(const TQString &name) const
Definition action.cpp:130
const TQString name() const
The name this Action has.
Definition action.cpp:95
void setVariable(TDESharedPtr< Variable > variable)
Append the Variable variable to list of variables this Action provides.
Definition action.cpp:145
Base Exception class.
Definition exception.h:35
A variable value used to provide abstract access to variables.
Definition variable.h:39
void appendChild(TDESharedPtr< Variable > variable)
Append a Variable to the list of children this Variable has.
Definition variable.cpp:221
void setVariant(const TQVariant &variant, bool detecttype=true)
Set the TQVariant.
Definition variable.cpp:165
TQValueList< TDESharedPtr< Variable > > List
Property to get and set a TQVariant as variable.
Definition variable.h:61