kword

KWQtSqlSerialDataSource.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
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 "KWQtSqlSerialDataSource.h"
00021 #include "KWQtSqlSerialDataSource.moc"
00022 #include "KWQtSqlEasyFilter.h"
00023 
00024 #include <klocale.h>
00025 #include <qlayout.h>
00026 #include <qsqlcursor.h>
00027 #include <qdatatable.h>
00028 #include <qcheckbox.h>
00029 #include <qsqlrecord.h>
00030 #include <qsqlquery.h>
00031 #include <kdebug.h>
00032 
00033 #define KWQtSqlBarIcon( x ) BarIcon( x, db->KWInstance() )
00034 
00035 /******************************************************************
00036  *
00037  * Class: KWQtSqlSerialDataSource
00038  *
00039  ******************************************************************/
00040 
00041 KWQtSqlSerialDataSource::KWQtSqlSerialDataSource(KInstance *inst,QObject *parent)
00042     : KWQtSqlSerialDataSourceBase(inst,parent)
00043 {
00044   myquery=0;
00045 }
00046 
00047 KWQtSqlSerialDataSource::~KWQtSqlSerialDataSource()
00048 {
00049         delete myquery;
00050         QSqlDatabase::removeDatabase("KWQTSQLPOWER");
00051 }
00052 
00053 
00054 QString KWQtSqlSerialDataSource::getValue( const QString &name, int record ) const
00055 {
00056         int num=record;
00057 
00058         if (!myquery) return name;
00059         if ( num < 0 || num > (int)myquery->size() )
00060                 return name;
00061         if (!myquery->seek(num,false)) return i18n(">>>Illegal position within datasource<<<");
00062         if (!myquery->contains(name)) return i18n(">>>Field %1 is unknown in the current database query<<<").arg(name);
00063         return (myquery->value(name)).toString();
00064 }
00065 
00066 void KWQtSqlSerialDataSource::save( QDomDocument & /*doc*/, QDomElement & /*parent*/)
00067 {
00068 /*
00069     QDomElement def=doc.createElement(QString::fromLatin1("DEFINITION"));
00070     parent.appendChild(def);
00071     for (DbRecord::Iterator it=sampleRecord.begin();it!=sampleRecord.end();++it)
00072     {
00073         QDomElement defEnt=doc.createElement(QString::fromLatin1("FIELD"));
00074         defEnt.setAttribute(QString::fromLatin1("name"),it.key());
00075         def.appendChild(defEnt);
00076     }
00077     QDomElement cont=doc.createElement(QString::fromLatin1("CONTENT"));
00078     parent.appendChild(cont);
00079     for (Db::Iterator dbI=db.begin();dbI!=db.end();++dbI)
00080     {
00081         QDomElement rec=doc.createElement(QString::fromLatin1("RECORD"));
00082         cont.appendChild(rec);
00083         for (DbRecord::Iterator it=sampleRecord.begin();it!=sampleRecord.end();++it)
00084         {
00085             QDomElement recEnt=doc.createElement(QString::fromLatin1("ITEM"));
00086             recEnt.setAttribute(QString::fromLatin1("name"),it.key());
00087             recEnt.setAttribute(QString::fromLatin1("data"),(*dbI)[it.key()]);
00088             rec.appendChild(recEnt);
00089         }
00090     }
00091 */
00092 }
00093 
00094 void KWQtSqlSerialDataSource::load( QDomElement& /*parentElem*/ )
00095 {
00096 /*
00097     db.clear();
00098     sampleRecord.clear();
00099     QDomNode defNd=parentElem.namedItem("DEFINITION");
00100     if (defNd.isNull()) return;
00101     QDomElement def=defNd.toElement();
00102     for (QDomElement defEnt=def.firstChild().toElement();!defEnt.isNull();defEnt=defEnt.nextSibling().toElement())
00103     {
00104         sampleRecord[defEnt.attribute(QString::fromLatin1("name"))]=defEnt.attribute(QString::fromLatin1("name"));//i18n( "No Value" );
00105     }
00106     QDomNode contNd=parentElem.namedItem("CONTENT");
00107     if (contNd.isNull()) return;
00108     for (QDomNode rec=contNd.firstChild();!rec.isNull();rec=rec.nextSibling())
00109     {
00110         appendRecord();
00111         for (QDomElement recEnt=rec.firstChild().toElement();!recEnt.isNull();recEnt=recEnt.nextSibling().toElement())
00112         {
00113             setValue(recEnt.attribute(QString::fromLatin1("name")),
00114                 recEnt.attribute(QString::fromLatin1("data")),db.count()-1);
00115         }
00116     }
00117 */
00118 }
00119 
00120 bool KWQtSqlSerialDataSource::showConfigDialog(QWidget *par,int action)
00121 {
00122     bool ret=false;
00123     if (action==KWSLEdit)
00124     {
00125         KWQtSqlDataSourceEditor *dia=new KWQtSqlDataSourceEditor(par,this);
00126         ret=dia->exec();
00127         delete dia;
00128     }
00129     else ret=KWQtSqlSerialDataSourceBase::showConfigDialog(par,action);
00130 
00131     return ret;
00132 }
00133 
00134 void KWQtSqlSerialDataSource::refresh(bool force)
00135 {
00136         if ((force) || (myquery==0))
00137         {
00138                 if (myquery)
00139                 {
00140                         delete myquery;
00141                         myquery=0;
00142                 }
00143                 if ((!database) || (!database->isOpen())) openDatabase();
00144         if ((!database) || (!database->isOpen())) return;
00145                 myquery=new QSqlCursor(tableName,true,database);
00146                 myquery->setMode(QSqlCursor::ReadOnly);
00147         myquery->select(filter);
00148         }
00149         kdDebug()<<QString("There were %1 rows in the query").arg(myquery->size())<<endl;
00150 
00151 }
00152 
00153 
00154 
00155 
00156 
00157 /******************************************************************
00158  *
00159  * Class: KWQtSqlDataSourceEditor
00160  *
00161  ******************************************************************/
00162 
00163 
00164 
00165 KWQtSqlDataSourceEditor::KWQtSqlDataSourceEditor( QWidget *parent, KWQtSqlSerialDataSource *db_ )
00166         :KDialogBase( Plain, i18n( "Mail Merge - Editor" ), Ok | Cancel, Ok, parent, "", true ), db( db_ )
00167 {
00168     tableName=db->tableName;
00169     filter=db->filter;
00170         (new QVBoxLayout(plainPage()))->setAutoAdd(true);
00171         setMainWidget(widget=new QtSqlDataSourceEditor(plainPage()));
00172     connect(widget->tableCombo,SIGNAL(activated(int)),this,SLOT(tableChanged(int)));
00173     connect(widget->editFilter,SIGNAL(clicked()),this,SLOT(editFilter()));
00174     updateTableCombo();
00175 
00176 //        connect(this,SIGNAL(okClicked()),this,SLOT(slotSetQuery()));
00177 }
00178 
00179 void KWQtSqlDataSourceEditor::tableChanged(int item)
00180 {
00181     tableName=widget->tableCombo->text(item);
00182     QSqlCursor *tmpCursor=new QSqlCursor(tableName,true,db->database);
00183     tmpCursor->setMode(QSqlCursor::ReadOnly);
00184 
00185     if (widget->filterCheckBox->isChecked()) tmpCursor->select(filter);
00186 
00187     widget->DataTable->setSqlCursor(tmpCursor,true,true);
00188     widget->DataTable->refresh(QDataTable::RefreshAll);
00189 }
00190 
00191 void KWQtSqlDataSourceEditor::updateTableCombo()
00192 {
00193     widget->tableCombo->clear();
00194         if (!db->database) return;
00195     widget->tableCombo->insertItem("");
00196         widget->tableCombo->insertStringList(db->database->tables());
00197 }
00198 
00199 void KWQtSqlDataSourceEditor::slotSetQuery()
00200 {
00201         db->tableName=tableName;
00202     db->filter=filter;
00203         db->refresh(true);
00204 }
00205 
00206 
00207 void KWQtSqlDataSourceEditor::editFilter()
00208 {
00209     KWQtSqlEasyFilter *f=new KWQtSqlEasyFilter(static_cast<QWidget*>(parent()));
00210     f->exec();
00211 }
00212 
00213 extern "C" {
00214         KWORD_MAILMERGE_EXPORT KWMailMergeDataSource *create_kwmailmerge_qtsqldb(KInstance *inst,QObject *parent)
00215         {
00216                 return new KWQtSqlSerialDataSource(inst,parent);
00217         }
00218 }
KDE Home | KDE Accessibility Home | Description of Access Keys