00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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;
00050 #ifdef ADD_DEFINEQUERY_ROW
00051 index++;
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, 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
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
00116 insertItem("");
00117 #ifdef ADD_DEFINEQUERY_ROW
00118
00119 insertItem(i18n("Define Query..."));
00120 #endif
00121
00122 KCompletion *comp = completionObject();
00123
00124
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());
00134 comp->addItem(it.current()->name());
00135 }
00136
00137
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());
00145 comp->addItem(it.current()->name());
00146 }
00147
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
00178 if (item.mimeType()=="kexi/table") {
00179 int i = 1;
00180 #ifdef ADD_DEFINEQUERY_ROW
00181 i++;
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++;
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
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
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
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
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"