kexi

kexidatasourcecombobox.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
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 "kexidatasourcecombobox.h"
00021 
00022 #include <kdebug.h>
00023 #include <kiconloader.h>
00024 #include <klistbox.h>
00025 
00026 #include <kexi.h>
00027 #include <kexiproject.h>
00028 #include <keximainwindow.h>
00029 #include <kexipart.h>
00030 #include <kexipartmanager.h>
00031 #include <kexipartinfo.h>
00032 #include <kexipartitem.h>
00033 
00034 #include <kexidb/connection.h>
00035 
00036 #ifdef KEXI_SHOW_UNIMPLEMENTED
00037 #define ADD_DEFINEQUERY_ROW
00038 #endif
00039 
00041 class KexiDataSourceComboBox::Private
00042 {
00043     public:
00044         Private()
00045          : tablesCount(0)
00046         {
00047         }
00048         int firstTableIndex() const {
00049             int index = 1; //skip empty row
00050 #ifdef ADD_DEFINEQUERY_ROW
00051             index++; /*skip 'define query' row*/
00052 #endif
00053             return index;
00054         }
00055         int firstQueryIndex() const {
00056             return firstTableIndex() + tablesCount;
00057         }
00058 
00059         QGuardedPtr<KexiProject> prj;
00060         QPixmap tableIcon, queryIcon;
00061         int tablesCount;
00062 };
00063 
00064 //------------------------
00065 
00066 KexiDataSourceComboBox::KexiDataSourceComboBox(QWidget *parent, const char *name)
00067  : KComboBox(true/*rw*/, parent, name)
00068  , d(new Private())
00069 {
00070     setInsertionPolicy(NoInsertion);
00071     setCompletionMode(KGlobalSettings::CompletionPopupAuto);
00072     setSizeLimit( 16 );
00073     connect(this, SIGNAL(activated(int)), this, SLOT(slotActivated(int)));
00074 
00075     d->tableIcon = SmallIcon("table");
00076     d->queryIcon = SmallIcon("query");
00077 }
00078 
00079 KexiDataSourceComboBox::~KexiDataSourceComboBox()
00080 {
00081     delete d;
00082 }
00083 
00084 KexiProject* KexiDataSourceComboBox::project() const
00085 {
00086     return d->prj;
00087 }
00088 
00089 void KexiDataSourceComboBox::setProject(KexiProject *prj)
00090 {
00091     if ((KexiProject*)d->prj == prj)
00092         return;
00093 
00094     if (d->prj) {
00095         disconnect(d->prj, 0, this, 0);
00096     }
00097     d->prj = prj;
00098     clear();
00099     d->tablesCount = 0;
00100     if (!d->prj)
00101         return;
00102 
00103     //needed for updating contents of the combo box
00104     connect(d->prj, SIGNAL(newItemStored(KexiPart::Item&)),
00105         this, SLOT(slotNewItemStored(KexiPart::Item&)));
00106     connect(d->prj, SIGNAL(itemRemoved(const KexiPart::Item&)),
00107         this, SLOT(slotItemRemoved(const KexiPart::Item&)));
00108     connect(d->prj, SIGNAL(itemRenamed(const KexiPart::Item&, const QCString&)),
00109         this, SLOT(slotItemRenamed(const KexiPart::Item&, const QCString&)));
00110 
00111     KexiDB::Connection *conn = d->prj->dbConnection();
00112     if (!conn)
00113         return;
00114 
00115     //special item: empty
00116     insertItem("");
00117 #ifdef ADD_DEFINEQUERY_ROW
00118     //special item: define query
00119     insertItem(i18n("Define Query..."));
00120 #endif
00121 
00122     KCompletion *comp = completionObject();
00123 
00124     //tables
00125     KexiPart::Info* partInfo = Kexi::partManager().infoForMimeType("kexi/table");
00126     if (!partInfo)
00127         return;
00128     KexiPart::ItemList list;
00129     prj->getSortedItems(list, partInfo);
00130     list.sort();
00131     d->tablesCount = 0;
00132     for (KexiPart::ItemListIterator it(list); it.current(); ++it, d->tablesCount++) {
00133         insertItem(d->tableIcon, it.current()->name()); //or caption()? 
00134         comp->addItem(it.current()->name());
00135     }
00136 
00137     //queries
00138     partInfo = Kexi::partManager().infoForMimeType("kexi/query");
00139     if (!partInfo)
00140         return;
00141     prj->getSortedItems(list, partInfo);
00142     list.sort();
00143     for (KexiPart::ItemListIterator it(list); it.current(); ++it) {
00144         insertItem(d->queryIcon, it.current()->name()); //or caption()? 
00145         comp->addItem(it.current()->name());
00146     }
00147 //  setCurrentText("");
00148     setCurrentItem(0);
00149 }
00150 
00151 void KexiDataSourceComboBox::setDataSource(const QCString& mimeType, const QCString& name)
00152 {
00153     if (name.isEmpty()) {
00154         clearEdit();
00155         setCurrentItem(0);
00156         emit dataSourceSelected();
00157         return;
00158     }
00159 
00160     QCString mt(mimeType);
00161     if (mimeType.isEmpty())
00162         mt="kexi/table";
00163     int i = findItem(mt, name);
00164     if (i==-1) {
00165         if (mimeType.isEmpty())
00166             i = findItem("kexi/query", name);
00167         if (i==-1)
00168             return;
00169     }
00170     setCurrentItem(i);
00171     slotActivated(i);
00172 }
00173 
00174 void KexiDataSourceComboBox::slotNewItemStored(KexiPart::Item& item)
00175 {
00176     QString name(item.name());
00177     //insert a new item, maintaining sort order and splitting to tables and queries
00178     if (item.mimeType()=="kexi/table") {
00179         int i = 1; /*skip empty row*/
00180 #ifdef ADD_DEFINEQUERY_ROW
00181         i++; /*skip 'define query' row*/
00182 #endif
00183         for (; i < d->firstQueryIndex() && name>=text(i); i++)
00184             ;
00185         insertItem(d->tableIcon, name, i);
00186         completionObject()->addItem(name);
00187         d->tablesCount++;
00188     }
00189     else if (item.mimeType()=="kexi/query") {
00190         int i;
00191         for (i=d->firstQueryIndex(); i<count() && name>=text(i); i++)
00192             ;
00193         insertItem(d->queryIcon, name, i);
00194         completionObject()->addItem(name);
00195     }
00196 }
00197 
00198 int KexiDataSourceComboBox::findItem(const QCString& mimeType, const QCString& name)
00199 {
00200     int i, end;
00201     if (mimeType=="kexi/table") {
00202         i = 0;
00203 #ifdef ADD_DEFINEQUERY_ROW
00204         i++; //skip 'define query'
00205 #endif
00206         end = d->firstQueryIndex();
00207     }
00208     else if (mimeType=="kexi/query") {
00209         i = d->firstQueryIndex();
00210         end = count();
00211     }
00212     else
00213         return -1;
00214 
00215     QString nameString(name);
00216 
00217     for (; i<end; i++)
00218         if (text(i)==nameString)
00219             return i;
00220     
00221     return -1;
00222 }
00223 
00224 void KexiDataSourceComboBox::slotItemRemoved(const KexiPart::Item& item)
00225 {
00226     const int i = findItem(item.mimeType(), item.name().latin1());
00227     if (i==-1)
00228         return;
00229     removeItem(i);
00230     completionObject()->removeItem(item.name());
00231     if (item.mimeType()=="kexi/table")
00232         d->tablesCount--;
00233     if (currentItem()==i) {
00234         if (i==(count()-1))
00235             setCurrentItem(i-1);
00236         else
00237             setCurrentItem(i);
00238     }
00239 /*  if (item.mimeType()=="kexi/table" || item.mime()=="kexi/query") {
00240         QString name(item.name());
00241         uint i, end;
00242         if (item.mimeType()=="kexi/table") {
00243             i = 1; //skip 'define query'
00244             end = 1+d->tablesCount;
00245         }
00246         else { //kexi/query
00247             i = 1+d->tablesCount;
00248             end = count();
00249         }
00250         for (; i<end; i++) {
00251             if (text(i)==name) {
00252                 removeItem(i);
00253                 completionObject()->removeItem(name);
00254                 if (item.mimeType()=="kexi/table")
00255                     d->tablesCount--;
00256                 if (currentItem()==i) {
00257                     if (i==(count()-1))
00258                         setCurrentItem(i-1);
00259                     else
00260                         setCurrentItem(i);
00261                 }
00262                 break;
00263             }
00264         }
00265     }*/
00266 }
00267 
00268 void KexiDataSourceComboBox::slotItemRenamed(const KexiPart::Item& item, const QCString& oldName)
00269 {
00270     const int i = findItem(item.mimeType(), oldName);
00271     if (i==-1)
00272         return;
00273     changeItem(item.name(), i);
00274     completionObject()->removeItem(QString(oldName));
00275     completionObject()->addItem(item.name());
00276 /*  if (item.mimeType()=="kexi/table" || item.mime()=="kexi/query") {
00277         QString oldStrName(oldName);
00278         uint i, end;
00279         if (item.mimeType()=="kexi/table") {
00280             i = 1; //skip 'define query'
00281             end = 1+d->tablesCount;
00282         }
00283         else { //kexi/query
00284             i = 1+d->tablesCount;
00285             end = count();
00286         }
00287         for (; i<end; i++) {
00288             if (text(i)==oldStrName) {
00289                 changeItem(item.name(), i);
00290                 completionObject()->removeItem(oldStrName);
00291                 completionObject()->addItem(item.name());
00292                 break;
00293             }
00294         }
00295     }*/
00296 }
00297 
00298 void KexiDataSourceComboBox::slotActivated( int index )
00299 {
00300     if (index >= d->firstTableIndex() && index < count())
00301         emit dataSourceSelected();
00302 }
00303 
00304 QCString KexiDataSourceComboBox::selectedMimeType() const
00305 {
00306     const int index = currentItem();
00307     if (index >= d->firstTableIndex() && index < (int)d->firstQueryIndex())
00308         return "kexi/table";
00309     else if (index >= (int)d->firstQueryIndex() && index < count())
00310         return "kexi/query";
00311     return 0;
00312 }
00313 
00314 QCString KexiDataSourceComboBox::selectedName() const
00315 {
00316     const int index = currentItem();
00317     if (index >= d->firstTableIndex() && index < count())
00318         return text(index).latin1();
00319     return 0;
00320 }
00321 
00322 #include "kexidatasourcecombobox.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys