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 , prevIndex(-1)
00047 , showTables(true)
00048 , showQueries(true)
00049 {
00050 }
00051 int firstTableIndex() const {
00052 int index = 1;
00053 #ifdef ADD_DEFINEQUERY_ROW
00054 index++;
00055 #endif
00056 return index;
00057 }
00058 int firstQueryIndex() const {
00059 return firstTableIndex() + tablesCount;
00060 }
00061
00062 QGuardedPtr<KexiProject> prj;
00063 QPixmap tableIcon, queryIcon;
00064 int tablesCount;
00065 int prevIndex;
00066 bool showTables : 1;
00067 bool showQueries : 1;
00068 };
00069
00070
00071
00072 KexiDataSourceComboBox::KexiDataSourceComboBox(QWidget *parent, const char *name)
00073 : KComboBox(true, parent, name)
00074 , d(new Private())
00075 {
00076 setInsertionPolicy(NoInsertion);
00077 setCompletionMode(KGlobalSettings::CompletionPopupAuto);
00078 setSizeLimit( 16 );
00079 connect(this, SIGNAL(activated(int)), this, SLOT(slotActivated(int)));
00080 connect(this, SIGNAL(returnPressed(const QString &)), this, SLOT(slotReturnPressed(const QString &)));
00081
00082 d->tableIcon = SmallIcon("table");
00083 d->queryIcon = SmallIcon("query");
00084 }
00085
00086 KexiDataSourceComboBox::~KexiDataSourceComboBox()
00087 {
00088 delete d;
00089 }
00090
00091 KexiProject* KexiDataSourceComboBox::project() const
00092 {
00093 return d->prj;
00094 }
00095
00096 void KexiDataSourceComboBox::setProject(KexiProject *prj, bool showTables, bool showQueries)
00097 {
00098 if ((KexiProject*)d->prj == prj)
00099 return;
00100
00101 if (d->prj) {
00102 disconnect(d->prj, 0, this, 0);
00103 }
00104 d->prj = prj;
00105 d->showTables = showTables;
00106 d->showQueries = showQueries;
00107 clear();
00108 d->tablesCount = 0;
00109 if (!d->prj)
00110 return;
00111
00112
00113 connect(d->prj, SIGNAL(newItemStored(KexiPart::Item&)),
00114 this, SLOT(slotNewItemStored(KexiPart::Item&)));
00115 connect(d->prj, SIGNAL(itemRemoved(const KexiPart::Item&)),
00116 this, SLOT(slotItemRemoved(const KexiPart::Item&)));
00117 connect(d->prj, SIGNAL(itemRenamed(const KexiPart::Item&, const QCString&)),
00118 this, SLOT(slotItemRenamed(const KexiPart::Item&, const QCString&)));
00119
00120 KexiDB::Connection *conn = d->prj->dbConnection();
00121 if (!conn)
00122 return;
00123
00124
00125 insertItem("");
00126 #ifdef ADD_DEFINEQUERY_ROW
00127
00128 insertItem(i18n("Define Query..."));
00129 #endif
00130
00131 KCompletion *comp = completionObject();
00132
00133 if (d->showTables) {
00134
00135 KexiPart::Info* partInfo = Kexi::partManager().infoForMimeType("kexi/table");
00136 if (!partInfo)
00137 return;
00138 KexiPart::ItemList list;
00139 prj->getSortedItems(list, partInfo);
00140 list.sort();
00141 d->tablesCount = 0;
00142 for (KexiPart::ItemListIterator it(list); it.current(); ++it, d->tablesCount++) {
00143 insertItem(d->tableIcon, it.current()->name());
00144 comp->addItem(it.current()->name());
00145 }
00146 }
00147
00148 if (d->showQueries) {
00149
00150 KexiPart::Info* partInfo = Kexi::partManager().infoForMimeType("kexi/query");
00151 if (!partInfo)
00152 return;
00153 KexiPart::ItemList list;
00154 prj->getSortedItems(list, partInfo);
00155 list.sort();
00156 for (KexiPart::ItemListIterator it(list); it.current(); ++it) {
00157 insertItem(d->queryIcon, it.current()->name());
00158 comp->addItem(it.current()->name());
00159 }
00160 }
00161
00162 setCurrentItem(0);
00163 }
00164
00165 void KexiDataSourceComboBox::setDataSource(const QString& mimeType, const QString& name)
00166 {
00167 if (name.isEmpty()) {
00168 clearEdit();
00169 setCurrentItem(0);
00170 d->prevIndex = -1;
00171 emit dataSourceChanged();
00172 return;
00173 }
00174
00175 QString mt(mimeType);
00176 if (mimeType.isEmpty())
00177 mt="kexi/table";
00178 int i = findItem(mt, name);
00179 if (i==-1) {
00180 if (mimeType.isEmpty())
00181 i = findItem("kexi/query", name);
00182 if (i==-1)
00183 return;
00184 }
00185 setCurrentItem(i);
00186 slotActivated(i);
00187 }
00188
00189 void KexiDataSourceComboBox::slotNewItemStored(KexiPart::Item& item)
00190 {
00191 QString name(item.name());
00192
00193 if (item.mimeType()=="kexi/table") {
00194 int i = 1;
00195 #ifdef ADD_DEFINEQUERY_ROW
00196 i++;
00197 #endif
00198 for (; i < d->firstQueryIndex() && name>=text(i); i++)
00199 ;
00200 insertItem(d->tableIcon, name, i);
00201 completionObject()->addItem(name);
00202 d->tablesCount++;
00203 }
00204 else if (item.mimeType()=="kexi/query") {
00205 int i;
00206 for (i=d->firstQueryIndex(); i<count() && name>=text(i); i++)
00207 ;
00208 insertItem(d->queryIcon, name, i);
00209 completionObject()->addItem(name);
00210 }
00211 }
00212
00213 int KexiDataSourceComboBox::findItem(const QString& mimeType, const QString& name)
00214 {
00215 int i, end;
00216 if (mimeType=="kexi/table") {
00217 i = 0;
00218 #ifdef ADD_DEFINEQUERY_ROW
00219 i++;
00220 #endif
00221 end = d->firstQueryIndex();
00222 }
00223 else if (mimeType=="kexi/query") {
00224 i = d->firstQueryIndex();
00225 end = count();
00226 }
00227 else
00228 return -1;
00229
00230 QString nameString(name);
00231
00232 for (; i<end; i++)
00233 if (text(i)==nameString)
00234 return i;
00235
00236 return -1;
00237 }
00238
00239 void KexiDataSourceComboBox::slotItemRemoved(const KexiPart::Item& item)
00240 {
00241 const int i = findItem(item.mimeType(), item.name());
00242 if (i==-1)
00243 return;
00244 removeItem(i);
00245 completionObject()->removeItem(item.name());
00246 if (item.mimeType()=="kexi/table")
00247 d->tablesCount--;
00248 #if 0
00249 if (currentItem()==i) {
00250 if (i==(count()-1))
00251 setCurrentItem(i-1);
00252 else
00253 setCurrentItem(i);
00254 }
00255 #endif
00256 }
00257
00258 void KexiDataSourceComboBox::slotItemRenamed(const KexiPart::Item& item, const QCString& oldName)
00259 {
00260 const int i = findItem(item.mimeType(), QString(oldName));
00261 if (i==-1)
00262 return;
00263 changeItem(item.name(), i);
00264 completionObject()->removeItem(QString(oldName));
00265 completionObject()->addItem(item.name());
00266 setCurrentText(oldName);
00267 }
00268
00269 void KexiDataSourceComboBox::slotActivated( int index )
00270 {
00271 if (index >= d->firstTableIndex() && index < count() && d->prevIndex!=currentItem()) {
00272 d->prevIndex = currentItem();
00273 emit dataSourceChanged();
00274 }
00275 }
00276
00277 QString KexiDataSourceComboBox::selectedMimeType() const
00278 {
00279 if (selectedName().isEmpty())
00280 return "";
00281 const int index = currentItem();
00282 if (index >= d->firstTableIndex() && index < (int)d->firstQueryIndex())
00283 return "kexi/table";
00284 else if (index >= (int)d->firstQueryIndex() && index < count())
00285 return "kexi/query";
00286 return "";
00287 }
00288
00289 QString KexiDataSourceComboBox::selectedName() const
00290 {
00291 if (isSelectionValid())
00292 return text(currentItem());
00293 return currentText();
00294 }
00295
00296 bool KexiDataSourceComboBox::isSelectionValid() const
00297 {
00298 const int index = currentItem();
00299 return index >= d->firstTableIndex() && index < count() && text(index)==currentText();
00300 }
00301
00302 void KexiDataSourceComboBox::slotReturnPressed(const QString & text)
00303 {
00304
00305 bool changed = false;
00306 if (text.isEmpty() && 0!=currentItem()) {
00307 setCurrentItem(0);
00308 changed = true;
00309 }
00310 else {
00311 QListBoxItem *item = listBox()->findItem( text, Qt::ExactMatch );
00312 if (item) {
00313 int index = listBox()->index( item );
00314
00315 if (index>=0 && index!=currentItem()) {
00316 setCurrentItem( index );
00317 changed = true;
00318 }
00319 }
00320 }
00321 if (changed)
00322 emit dataSourceChanged();
00323 }
00324
00325 void KexiDataSourceComboBox::focusOutEvent( QFocusEvent *e )
00326 {
00327 KComboBox::focusOutEvent( e );
00328 slotReturnPressed(currentText());
00329 }
00330
00331 #include "kexidatasourcecombobox.moc"