kexi

kexifieldcombobox.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 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 "kexifieldcombobox.h"
00021 
00022 #include <qheader.h>
00023 #include <qlayout.h>
00024 #include <qlabel.h>
00025 #include <qpushbutton.h>
00026 #include <qcursor.h>
00027 #include <qpoint.h>
00028 #include <qapplication.h>
00029 #include <qbitmap.h>
00030 #include <qstyle.h>
00031 #include <qlistbox.h>
00032 
00033 #include <kdebug.h>
00034 #include <kiconloader.h>
00035 #include <kdeversion.h>
00036 #include <kconfig.h>
00037 #include <kglobalsettings.h>
00038 #include <klocale.h>
00039 
00040 #include <kexidb/tableschema.h>
00041 #include <kexidb/queryschema.h>
00042 #include <kexidb/utils.h>
00043 #include <kexidragobjects.h>
00044 #include <kexiproject.h>
00045 
00047 class KexiFieldComboBox::Private
00048 {
00049     public:
00050         Private()
00051 //       : schema(0)
00052          : table(true)
00053         {
00054         }
00055         ~Private()
00056         {
00057 //          delete schema;
00058         }
00059         QGuardedPtr<KexiProject> prj;
00060 //      KexiDB::TableOrQuerySchema* schema;
00061         QPixmap keyIcon, noIcon;
00062         QCString tableOrQueryName;
00063         QString fieldOrExpression;
00064         QMap<QCString, QString> captions;
00065         bool table;
00066 };
00067 
00068 //------------------------
00069 
00070 KexiFieldComboBox::KexiFieldComboBox(QWidget *parent, const char *name)
00071  : KComboBox(true/*rw*/, parent, name)
00072  , d(new Private())
00073 {
00074     d->keyIcon = SmallIcon("key");
00075     d->noIcon = QPixmap(d->keyIcon.size());
00076     QBitmap bmp(d->noIcon.size());
00077     bmp.fill(Qt::color0);
00078     d->noIcon.setMask(bmp);
00079 
00080     setInsertionPolicy(NoInsertion);
00081     setCompletionMode(KGlobalSettings::CompletionPopupAuto);
00082     setSizeLimit( 16 );
00083     connect(this, SIGNAL(activated(int)), this, SLOT(slotActivated(int)));
00084 
00085 //  setAcceptDrops(true);
00086 //  viewport()->setAcceptDrops(true);
00087 }
00088 
00089 KexiFieldComboBox::~KexiFieldComboBox()
00090 {
00091     delete d;
00092 }
00093 
00094 void KexiFieldComboBox::setProject(KexiProject *prj)
00095 {
00096     if ((KexiProject*)d->prj==prj)
00097         return;
00098     d->prj = prj;
00099     setTableOrQuery("", true);
00100 }
00101 
00102 void KexiFieldComboBox::setTableOrQuery(const QCString& name, bool table)
00103 {
00104     d->tableOrQueryName = name;
00105     d->table = table;
00106     clear();
00107     d->captions.clear();
00108     insertItem("");
00109 //  delete d->schema;
00110     if (d->tableOrQueryName.isEmpty() || !d->prj)
00111         return;
00112 
00113     KexiDB::TableOrQuerySchema tableOrQuery(d->prj->dbConnection(), d->tableOrQueryName, table);
00114     if (!tableOrQuery.table() && !tableOrQuery.query())
00115         return;
00116 
00117 //  bool hasPKeys = true; //t->hasPrimaryKeys();
00118     KexiDB::QueryColumnInfo::Vector columns = tableOrQuery.columns();
00119     const int count = columns.count();
00120     for(int i=0; i < count; i++)
00121     {
00122         KexiDB::QueryColumnInfo *colinfo = columns[i];
00123         insertItem(
00124             (colinfo && (colinfo->field->isPrimaryKey() || colinfo->field->isUniqueKey()))
00125             ? d->keyIcon
00126             : d->noIcon
00127             , colinfo->aliasOrName());
00128         completionObject()->addItem(colinfo->aliasOrName());
00129         //store user-friendly caption (used by fieldOrExpressionCaption())
00130         d->captions.insert( colinfo->aliasOrName(), colinfo->captionOrAliasOrName() );
00131     }
00132 
00133     //update selection
00134     setFieldOrExpression(d->fieldOrExpression);
00135 }
00136 
00137 QCString KexiFieldComboBox::setTableOrQueryName() const
00138 {
00139     return d->tableOrQueryName;
00140 }
00141 
00142 void KexiFieldComboBox::setFieldOrExpression(const QString& string)
00143 {
00144     const QString name(string); //string.stripWhiteSpace().lower());
00145     const int pos = name.find('.');
00146     if (pos!=-1) {
00147         QCString objectName = name.left(pos).latin1();
00148         if (d->tableOrQueryName!=objectName) {
00149             d->fieldOrExpression = name;
00150             setCurrentItem(0);
00151             setCurrentText(name);
00153             kexiwarn << "KexiFieldComboBox::setField(): invalid table/query name in '" << name << "'" << endl;
00154             return;
00155         }
00156         d->fieldOrExpression = name.mid(pos+1);
00157     }
00158     else
00159         d->fieldOrExpression = name;
00160 
00161     QListBoxItem *item = listBox()->findItem(d->fieldOrExpression);
00162     if (!item) {
00163         setCurrentItem(0);
00164         setCurrentText(d->fieldOrExpression);
00165         //todo: show 'the item doesn't match' info?
00166         return;
00167     }
00168     setCurrentItem( listBox()->index(item) );
00169 }
00170 
00171 QString KexiFieldComboBox::fieldOrExpression() const
00172 {
00173     return d->fieldOrExpression;
00174 }
00175 
00176 QString KexiFieldComboBox::fieldOrExpressionCaption() const
00177 {
00178     return d->captions[ d->fieldOrExpression.latin1() ];
00179 }
00180 
00181 void KexiFieldComboBox::slotActivated(int i)
00182 {
00183     d->fieldOrExpression = text(i);
00184     emit selected();
00185 }
00186 
00187 #include "kexifieldcombobox.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys