summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/forms/kexiformeventhandler.cpp
blob: 55e999db915f20b0b538fd09986973e90e164ca1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* This file is part of the KDE project
   Copyright (C) 2005-2006 Jaroslaw Staniek <js@iidea.pl>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kexiformeventhandler.h"

#include <tqwidget.h>
#include <tqobjectlist.h>

#include <kdebug.h>
#include <klocale.h>

#include <tableview/kexitableitem.h>
#include <tableview/kexitableviewdata.h>
#include <kexidb/queryschema.h>
#include <keximainwindow.h>
#include <kexidialogbase.h>
#include <kexipart.h>
#include <kexipartinfo.h>
#include <kexipartitem.h>

KexiFormEventAction::ActionData::ActionData()
{
}

bool KexiFormEventAction::ActionData::isEmpty() const
{
	return string.isEmpty();
}

KexiPart::Info* KexiFormEventAction::ActionData::decodeString(
	TQString& actionType, TQString& actionArg, bool& ok) const
{
	const int idx = string.find(':');
	ok = false;
	if (idx==-1)
		return 0;
	const TQString _actionType = string.left(idx);
	const TQString _actionArg = string.mid(idx+1);
	if (_actionType.isEmpty() || _actionArg.isEmpty())
		return 0;
	KexiPart::Info *info = 0;
	if (_actionType!="kaction" && _actionType!="currentForm") {
		info = Kexi::partManager().infoForMimeType( TQString("kexi/%1").arg(_actionType) );
		if (!info)
			return 0;
	}
	actionType = _actionType;
	actionArg = _actionArg;
	ok = true;
	return info;
}

//-------------------------------------

KexiFormEventAction::KexiFormEventAction(KexiMainWindow *mainWin, TQObject* parent, 
	const TQString& actionName, const TQString& objectName, const TQString& actionOption)
 : KAction(parent), m_mainWin(mainWin), m_actionName(actionName), m_objectName(objectName)
 , m_actionOption(actionOption)
{
}

KexiFormEventAction::~KexiFormEventAction()
{
}

void KexiFormEventAction::activate()
{
	KexiProject* project = m_mainWin->project();
	if (!project)
		return;
	KexiPart::Part* part = Kexi::partManager().partForMimeType( 
		TQString("kexi/%1").arg(m_actionName) );
	if (!part)
		return;
	KexiPart::Item* item = project->item( part->info(), m_objectName );
	if (!item)
		return;
	bool actionCancelled = false;
	if (m_actionOption.isEmpty()) { // backward compatibility (good defaults)
		if (part->info()->isExecuteSupported())
			part->execute(item, parent());
		else
			m_mainWin->openObject(item, Kexi::DataViewMode, actionCancelled);
	}
	else {
//! @todo react on failure...
		if (m_actionOption == "open")
			m_mainWin->openObject(item, Kexi::DataViewMode, actionCancelled);
		else if (m_actionOption == "execute")
			part->execute(item, parent());
		else if (m_actionOption == "print") {
			if (part->info()->isPrintingSupported())
				m_mainWin->printItem(item);
		}
		else if (m_actionOption == "printPreview") {
			if (part->info()->isPrintingSupported())
				m_mainWin->printPreviewForItem(item);
		}
		else if (m_actionOption == "pageSetup") {
			if (part->info()->isPrintingSupported())
				m_mainWin->showPageSetupForItem(item);
		}
		else if (m_actionOption == "exportToCSV"
			|| m_actionOption == "copyToClipboardAsCSV")
		{
			if (part->info()->isDataExportSupported())
				m_mainWin->executeCustomActionForObject(item, m_actionOption);
		}
		else if (m_actionOption == "new")
			m_mainWin->newObject( part->info(), actionCancelled );
		else if (m_actionOption == "design")
			m_mainWin->openObject(item, Kexi::DesignViewMode, actionCancelled);
		else if (m_actionOption == "editText")
			m_mainWin->openObject(item, Kexi::TextViewMode, actionCancelled);
		else if (m_actionOption == "close") {
			tristate res = m_mainWin->closeObject(item);
			if (~res)
				actionCancelled = true;
		}
	}
}

//------------------------------------------

KexiFormEventHandler::KexiFormEventHandler()
	: m_mainWidget(0)
{
}

KexiFormEventHandler::~KexiFormEventHandler()
{
}

void KexiFormEventHandler::setMainWidgetForEventHandling(KexiMainWindow *mainWin, TQWidget* mainWidget)
{
	m_mainWidget = mainWidget;
	if (!m_mainWidget)
		return;

	//find widgets whose will work as data items
//! @todo look for other widgets too
	TQObjectList *l = m_mainWidget->queryList( "KexiPushButton" );
	TQObjectListIt it( *l );
	TQObject *obj;
	for ( ; (obj = it.current()) != 0; ++it ) {
		bool ok;
		KexiFormEventAction::ActionData data;
		data.string = obj->property("onClickAction").toString();
		data.option = obj->property("onClickActionOption").toString();
		if (data.isEmpty())
			continue;

		TQString actionType, actionArg;
		KexiPart::Info* partInfo = data.decodeString(actionType, actionArg, ok);
		if (!ok)
			continue;
		if (actionType=="kaction" || actionType=="currentForm") {
			KAction *action = mainWin->actionCollection()->action( actionArg.latin1() );
			if (!action)
				continue;
			TQObject::disconnect( obj, TQT_SIGNAL(clicked()), action, TQT_SLOT(activate()) ); //safety
			TQObject::connect( obj, TQT_SIGNAL(clicked()), action, TQT_SLOT(activate()) );
		}
		else if (partInfo) { //'open or execute' action
			KexiFormEventAction* action = new KexiFormEventAction(mainWin, obj, actionType, actionArg, 
				data.option);
			TQObject::disconnect( obj, TQT_SIGNAL(clicked()), action, TQT_SLOT(activate()) );
			TQObject::connect( obj, TQT_SIGNAL(clicked()), action, TQT_SLOT(activate()) );
		}
	}
	delete l;
}