kexi

lookupfieldschema.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program 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 program 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 program; see the file COPYING.  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 "lookupfieldschema.h"
00021 #include "utils.h"
00022 
00023 #include <qdom.h>
00024 #include <qvariant.h>
00025 #include <kdebug.h>
00026 
00027 using namespace KexiDB;
00028 
00029 
00030 LookupFieldSchema::RowSource::RowSource()
00031 : m_type(NoType)
00032 , m_values(0)
00033 {
00034 }
00035 
00036 LookupFieldSchema::RowSource::~RowSource()
00037 {
00038     delete m_values;
00039 }
00040 
00041 void LookupFieldSchema::RowSource::setName(const QString& name)
00042 {
00043     m_name = name;
00044     if (m_values)
00045         m_values->clear();
00046 }
00047 
00048 QString LookupFieldSchema::RowSource::typeName() const
00049 {
00050     switch (m_type) {
00051     case Table: return "table";
00052     case Query: return "query";
00053     case SQLStatement: return "sql";
00054     case ValueList: return "valuelist";
00055     case FieldList: return "fieldlist";
00056     default:;
00057     }
00058     return QString::null;
00059 }
00060 
00061 void LookupFieldSchema::RowSource::setTypeByName( const QString& typeName )
00062 {
00063     if (typeName=="table")
00064         setType( Table );
00065     else if (typeName=="query")
00066         setType( Query );
00067     else if (typeName=="sql")
00068         setType( SQLStatement );
00069     else if (typeName=="valuelist")
00070         setType( ValueList );
00071     else if (typeName=="fieldlist")
00072         setType( FieldList );
00073     else
00074         setType( NoType );
00075 }
00076 
00077 QStringList LookupFieldSchema::RowSource::values() const
00078 {
00079     return m_values ? *m_values : QStringList();
00080 }
00081 
00082 void LookupFieldSchema::RowSource::setValues(const QStringList& values)
00083 {
00084     m_name = QString::null;
00085     if (m_values)
00086         *m_values = values;
00087     else
00088         m_values = new QStringList(values);
00089 }
00090 
00091 QString LookupFieldSchema::RowSource::debugString() const
00092 {
00093     return QString("rowSourceType:'%1' rowSourceName:'%2' rowSourceValues:'%3'\n")
00094         .arg(typeName()).arg(name()).arg(m_values ? m_values->join("|") : QString::null);
00095 }
00096 
00097 void LookupFieldSchema::RowSource::debug() const
00098 {
00099     KexiDBDbg << debugString() << endl;
00100 }
00101 
00102 //---------------------------------------
00103 
00104 LookupFieldSchema::LookupFieldSchema()
00105  : m_boundColumn(-1)
00106  , m_visibleColumn(-1)
00107  , m_maximumListRows(KEXIDB_LOOKUP_FIELD_DEFAULT_LIST_ROWS)
00108  , m_displayWidget(KEXIDB_LOOKUP_FIELD_DEFAULT_DISPLAY_WIDGET)
00109  , m_columnHeadersVisible(KEXIDB_LOOKUP_FIELD_DEFAULT_HEADERS_VISIBLE)
00110  , m_limitToList(KEXIDB_LOOKUP_FIELD_DEFAULT_LIMIT_TO_LIST)
00111 {
00112 }
00113 
00114 LookupFieldSchema::~LookupFieldSchema()
00115 {
00116 }
00117 
00118 void LookupFieldSchema::setMaximumListRows(uint rows)
00119 {
00120     if (rows==0)
00121         m_maximumListRows = KEXIDB_LOOKUP_FIELD_DEFAULT_LIST_ROWS;
00122     else if (rows>KEXIDB_LOOKUP_FIELD_MAX_LIST_ROWS)
00123         m_maximumListRows = KEXIDB_LOOKUP_FIELD_MAX_LIST_ROWS;
00124     else
00125         m_maximumListRows = rows;
00126 }
00127 
00128 QString LookupFieldSchema::debugString() const
00129 {
00130     QString columnWidthsStr;
00131     bool first=true;
00132     foreach (QValueList<int>::ConstIterator, it, m_columnWidths) {
00133         if (first)
00134             first = false;
00135         else
00136             columnWidthsStr.append(";");
00137         columnWidthsStr.append( QString::number(*it) );
00138     }
00139 
00140     return QString("LookupFieldSchema( %1\n"
00141         " boundColumn:%2 visibleColumn:%3 maximumListRows:%4 displayWidget:%5\n"
00142         " columnHeadersVisible:%6 limitToList:%7\n"
00143         " columnWidths:%8 )")
00144         .arg(m_rowSource.debugString())
00145         .arg(m_boundColumn).arg(m_visibleColumn).arg(m_maximumListRows)
00146         .arg( m_displayWidget==ComboBox ? "ComboBox" : "ListBox")
00147         .arg(m_columnHeadersVisible).arg(m_limitToList)
00148         .arg(columnWidthsStr);
00149 }
00150 
00151 void LookupFieldSchema::debug() const
00152 {
00153     KexiDBDbg << debugString() << endl;
00154 }
00155 
00156 /* static */
00157 LookupFieldSchema *LookupFieldSchema::loadFromDom(const QDomElement& lookupEl)
00158 {
00159     LookupFieldSchema *lookupFieldSchema = new LookupFieldSchema();
00160     for (QDomNode node = lookupEl.firstChild(); !node.isNull(); node = node.nextSibling()) {
00161         QDomElement el = node.toElement();
00162         QString name( el.tagName() );
00163         if (name=="row-source") {
00164             /*<row-source>
00165                 empty
00166                 | <type>table|query|sql|valuelist|fieldlist</type>  #required because there can be table and query with the same name
00167                                     "fieldlist" (basically a list of column names of a table/query,
00168                                               "Field List" as in MSA)
00169                 <name>string</name> #table/query name, etc. or KEXISQL SELECT QUERY
00170                 <values><value>...</value> #for "valuelist" type
00171                     <value>...</value>
00172                 </values>
00173              </row-source> */
00174             for (el = el.firstChild().toElement(); !el.isNull(); el=el.nextSibling().toElement()) {
00175                 if (el.tagName()=="type")
00176                     lookupFieldSchema->rowSource().setTypeByName( el.text() );
00177                 else if (el.tagName()=="name")
00178                     lookupFieldSchema->rowSource().setName( el.text() );
00180             }
00181         }
00182         else if (name=="bound-column") {
00183             /* <bound-column>
00184                 <number>number</number> #in later implementation there can be more columns
00185                </bound-column> */
00186             QVariant val = KexiDB::loadPropertyValueFromDom( el.firstChild() );
00187             if (val.type()==QVariant::Int)
00188                 lookupFieldSchema->setBoundColumn( val.toInt() );
00189         }
00190         else if (name=="visible-column") {
00191             /* <visible-column> #a column that has to be visible in the combo box
00192                 <number>number</number> #in later implementation there can be more columns
00193                </visible-column> */
00194             QVariant val = KexiDB::loadPropertyValueFromDom( el.firstChild() );
00195             if (val.type()==QVariant::Int)
00196                 lookupFieldSchema->setVisibleColumn( val.toInt() );
00197         }
00198         else if (name=="column-widths") {
00199             /* <column-widths> #column widths, -1 means 'default'
00200                 <number>int</number>
00201                 ...
00202                 <number>int</number>
00203                </column-widths> */
00204             QVariant val;
00205             QValueList<int> columnWidths;
00206             for (el = el.firstChild().toElement(); !el.isNull(); el=el.nextSibling().toElement()) {
00207                 QVariant val = KexiDB::loadPropertyValueFromDom( el );
00208                 if (val.type()==QVariant::Int)
00209                     columnWidths.append(val.toInt());
00210             }
00211             lookupFieldSchema->setColumnWidths( columnWidths );
00212         }
00213         else if (name=="show-column-headers") {
00214             /* <show-column-headers>
00215                 <bool>true/false</bool>
00216                </show-column-headers> */
00217             QVariant val = KexiDB::loadPropertyValueFromDom( el.firstChild() );
00218             if (val.type()==QVariant::Bool)
00219                 lookupFieldSchema->setColumnHeadersVisible( val.toBool() );
00220         }
00221         else if (name=="list-rows") {
00222             /* <list-rows>
00223                 <number>1..100</number>
00224                </list-rows> */
00225             QVariant val = KexiDB::loadPropertyValueFromDom( el.firstChild() );
00226             if (val.type()==QVariant::Int)
00227                 lookupFieldSchema->setMaximumListRows( val.toUInt() );
00228         }
00229         else if (name=="limit-to-list") {
00230             /* <limit-to-list>
00231                 <bool>true/false</bool>
00232                </limit-to-list> */
00233             QVariant val = KexiDB::loadPropertyValueFromDom( el.firstChild() );
00234             if (val.type()==QVariant::Bool)
00235                 lookupFieldSchema->setLimitToList( val.toBool() );
00236         }
00237         else if (name=="display-widget") {
00238             if (el.text()=="combobox")
00239                 lookupFieldSchema->setDisplayWidget( LookupFieldSchema::ComboBox );
00240             else if (el.text()=="listbox")
00241                 lookupFieldSchema->setDisplayWidget( LookupFieldSchema::ListBox );
00242         }
00243     }
00244     return lookupFieldSchema;
00245 }
00246 
00247 /* static */
00248 void LookupFieldSchema::saveToDom(LookupFieldSchema& lookupSchema, QDomDocument& doc, QDomElement& parentEl)
00249 {
00250     QDomElement lookupColumnEl( doc.createElement("lookup-column") );
00251     parentEl.appendChild( lookupColumnEl );
00252 
00253     QDomElement rowSourceEl( doc.createElement("row-source") );
00254     lookupColumnEl.appendChild( rowSourceEl );
00255 
00256     QDomElement rowSourceTypeEl( doc.createElement("type") );
00257     rowSourceEl.appendChild( rowSourceTypeEl );
00258     rowSourceTypeEl.appendChild( doc.createTextNode(lookupSchema.rowSource().typeName()) ); //can be empty
00259 
00260     QDomElement nameEl( doc.createElement("name") );
00261     rowSourceEl.appendChild( nameEl );
00262     nameEl.appendChild( doc.createTextNode(lookupSchema.rowSource().name()) ); //can be empty
00263 
00264     const QStringList& values( lookupSchema.rowSource().values() );
00265     if (!values.isEmpty()) {
00266         QDomElement valuesEl( doc.createElement("values") );
00267         rowSourceEl.appendChild( valuesEl );
00268         for (QStringList::ConstIterator it = values.constBegin(); it!=values.constEnd(); ++it) {
00269             QDomElement valueEl( doc.createElement("value") );
00270             valuesEl.appendChild( valueEl );
00271             valueEl.appendChild( doc.createTextNode(*it) );
00272         }
00273     }
00274 
00275     if (lookupSchema.boundColumn()>=0)
00276         KexiDB::saveNumberElementToDom(doc, lookupColumnEl, "bound-column", lookupSchema.boundColumn());
00277     if (lookupSchema.visibleColumn()>=0)
00278         KexiDB::saveNumberElementToDom(doc, lookupColumnEl, "visible-column", lookupSchema.visibleColumn()); //can be -1
00279 
00280     const QValueList<int> columnWidths = lookupSchema.columnWidths();
00281     if (!columnWidths.isEmpty()) {
00282         QDomElement columnWidthsEl( doc.createElement("column-widths") );
00283         lookupColumnEl.appendChild( columnWidthsEl );
00284         for (QValueList<int>::ConstIterator it = columnWidths.constBegin(); it!=columnWidths.constEnd(); ++it) {
00285             QDomElement columnWidthEl( doc.createElement("number") );
00286             columnWidthsEl.appendChild( columnWidthEl );
00287             columnWidthEl.appendChild( doc.createTextNode( QString::number(*it) ) );
00288         }
00289     }
00290 
00291     if (lookupSchema.columnHeadersVisible()!=KEXIDB_LOOKUP_FIELD_DEFAULT_HEADERS_VISIBLE)
00292         KexiDB::saveBooleanElementToDom(doc, lookupColumnEl, "show-column-headers", lookupSchema.columnHeadersVisible());
00293     if (lookupSchema.maximumListRows()!=KEXIDB_LOOKUP_FIELD_DEFAULT_LIST_ROWS)
00294         KexiDB::saveNumberElementToDom(doc, lookupColumnEl, "list-rows", lookupSchema.maximumListRows());
00295     if (lookupSchema.limitToList()!=KEXIDB_LOOKUP_FIELD_DEFAULT_LIMIT_TO_LIST)
00296         KexiDB::saveBooleanElementToDom(doc, lookupColumnEl, "limit-to-list", lookupSchema.limitToList());
00297     
00298     if (lookupSchema.displayWidget()!=KEXIDB_LOOKUP_FIELD_DEFAULT_DISPLAY_WIDGET) {
00299         QDomElement displayWidgetEl( doc.createElement("display-widget") );
00300         lookupColumnEl.appendChild( displayWidgetEl );
00301         displayWidgetEl.appendChild( 
00302             doc.createTextNode( (lookupSchema.displayWidget()==ListBox) ? "listbox" : "combobox" ) );
00303     }
00304 }
KDE Home | KDE Accessibility Home | Description of Access Keys