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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/* This file is part of the KDE project
Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
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 "KWTQtSqlSerialDataSource.h"
#include "KWTQtSqlSerialDataSource.moc"
#include "KWTQtSqlEasyFilter.h"
#include <klocale.h>
#include <tqlayout.h>
#include <tqsqlcursor.h>
#include <tqdatatable.h>
#include <tqcheckbox.h>
#include <tqsqlrecord.h>
#include <tqsqlquery.h>
#include <kdebug.h>
#define KWTQtSqlBarIcon( x ) BarIcon( x, db->KWInstance() )
/******************************************************************
*
* Class: KWTQtSqlSerialDataSource
*
******************************************************************/
KWTQtSqlSerialDataSource::KWTQtSqlSerialDataSource(KInstance *inst,TQObject *tqparent)
: KWTQtSqlSerialDataSourceBase(inst,tqparent)
{
myquery=0;
}
KWTQtSqlSerialDataSource::~KWTQtSqlSerialDataSource()
{
delete myquery;
TQSqlDatabase::removeDatabase("KWTQTSTQLPOWER");
}
TQString KWTQtSqlSerialDataSource::getValue( const TQString &name, int record ) const
{
int num=record;
if (!myquery) return name;
if ( num < 0 || num > (int)myquery->size() )
return name;
if (!myquery->seek(num,false)) return i18n(">>>Illegal position within datasource<<<");
if (!myquery->tqcontains(name)) return i18n(">>>Field %1 is unknown in the current database query<<<").tqarg(name);
return (myquery->value(name)).toString();
}
void KWTQtSqlSerialDataSource::save( TQDomDocument & /*doc*/, TQDomElement & /*tqparent*/)
{
/*
TQDomElement def=doc.createElement(TQString::tqfromLatin1("DEFINITION"));
tqparent.appendChild(def);
for (DbRecord::Iterator it=sampleRecord.begin();it!=sampleRecord.end();++it)
{
TQDomElement defEnt=doc.createElement(TQString::tqfromLatin1("FIELD"));
defEnt.setAttribute(TQString::tqfromLatin1("name"),it.key());
def.appendChild(defEnt);
}
TQDomElement cont=doc.createElement(TQString::tqfromLatin1("CONTENT"));
tqparent.appendChild(cont);
for (Db::Iterator dbI=db.begin();dbI!=db.end();++dbI)
{
TQDomElement rec=doc.createElement(TQString::tqfromLatin1("RECORD"));
cont.appendChild(rec);
for (DbRecord::Iterator it=sampleRecord.begin();it!=sampleRecord.end();++it)
{
TQDomElement recEnt=doc.createElement(TQString::tqfromLatin1("ITEM"));
recEnt.setAttribute(TQString::tqfromLatin1("name"),it.key());
recEnt.setAttribute(TQString::tqfromLatin1("data"),(*dbI)[it.key()]);
rec.appendChild(recEnt);
}
}
*/
}
void KWTQtSqlSerialDataSource::load( TQDomElement& /*tqparentElem*/ )
{
/*
db.clear();
sampleRecord.clear();
TQDomNode defNd=tqparentElem.namedItem("DEFINITION");
if (defNd.isNull()) return;
TQDomElement def=defNd.toElement();
for (TQDomElement defEnt=def.firstChild().toElement();!defEnt.isNull();defEnt=defEnt.nextSibling().toElement())
{
sampleRecord[defEnt.attribute(TQString::tqfromLatin1("name"))]=defEnt.attribute(TQString::tqfromLatin1("name"));//i18n( "No Value" );
}
TQDomNode contNd=tqparentElem.namedItem("CONTENT");
if (contNd.isNull()) return;
for (TQDomNode rec=contNd.firstChild();!rec.isNull();rec=rec.nextSibling())
{
appendRecord();
for (TQDomElement recEnt=rec.firstChild().toElement();!recEnt.isNull();recEnt=recEnt.nextSibling().toElement())
{
setValue(recEnt.attribute(TQString::tqfromLatin1("name")),
recEnt.attribute(TQString::tqfromLatin1("data")),db.count()-1);
}
}
*/
}
bool KWTQtSqlSerialDataSource::showConfigDialog(TQWidget *par,int action)
{
bool ret=false;
if (action==KWSLEdit)
{
KWTQtSqlDataSourceEditor *dia=new KWTQtSqlDataSourceEditor(par,this);
ret=dia->exec();
delete dia;
}
else ret=KWTQtSqlSerialDataSourceBase::showConfigDialog(par,action);
return ret;
}
void KWTQtSqlSerialDataSource::refresh(bool force)
{
if ((force) || (myquery==0))
{
if (myquery)
{
delete myquery;
myquery=0;
}
if ((!database) || (!database->isOpen())) openDatabase();
if ((!database) || (!database->isOpen())) return;
myquery=new TQSqlCursor(tableName,true,database);
myquery->setMode(TQSqlCursor::ReadOnly);
myquery->select(filter);
}
kdDebug()<<TQString("There were %1 rows in the query").tqarg(myquery->size())<<endl;
}
/******************************************************************
*
* Class: KWTQtSqlDataSourceEditor
*
******************************************************************/
KWTQtSqlDataSourceEditor::KWTQtSqlDataSourceEditor( TQWidget *tqparent, KWTQtSqlSerialDataSource *db_ )
:KDialogBase( Plain, i18n( "Mail Merge - Editor" ), Ok | Cancel, Ok, tqparent, "", true ), db( db_ )
{
tableName=db->tableName;
filter=db->filter;
(new TQVBoxLayout(plainPage()))->setAutoAdd(true);
setMainWidget(widget=new TQtSqlDataSourceEditor(plainPage()));
connect(widget->tableCombo,TQT_SIGNAL(activated(int)),this,TQT_SLOT(tableChanged(int)));
connect(widget->editFilter,TQT_SIGNAL(clicked()),this,TQT_SLOT(editFilter()));
updateTableCombo();
// connect(this,TQT_SIGNAL(okClicked()),this,TQT_SLOT(slotSetQuery()));
}
void KWTQtSqlDataSourceEditor::tableChanged(int item)
{
tableName=widget->tableCombo->text(item);
TQSqlCursor *tmpCursor=new TQSqlCursor(tableName,true,db->database);
tmpCursor->setMode(TQSqlCursor::ReadOnly);
if (widget->filterCheckBox->isChecked()) tmpCursor->select(filter);
widget->DataTable->setSqlCursor(tmpCursor,true,true);
widget->DataTable->refresh(TQDataTable::RefreshAll);
}
void KWTQtSqlDataSourceEditor::updateTableCombo()
{
widget->tableCombo->clear();
if (!db->database) return;
widget->tableCombo->insertItem("");
widget->tableCombo->insertStringList(db->database->tables());
}
void KWTQtSqlDataSourceEditor::slotSetQuery()
{
db->tableName=tableName;
db->filter=filter;
db->refresh(true);
}
void KWTQtSqlDataSourceEditor::editFilter()
{
KWTQtSqlEasyFilter *f=new KWTQtSqlEasyFilter(static_cast<TQWidget*>(tqparent()));
f->exec();
}
extern "C" {
KWORD_MAILMERGE_EXPORT KWMailMergeDataSource *create_kwmailmerge_qtsqldb(KInstance *inst,TQObject *tqparent)
{
return new KWTQtSqlSerialDataSource(inst,tqparent);
}
}
|