kexi

KexiProjectSelector.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 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 "KexiProjectSelector.h"
00021 
00022 #include <kexidb/drivermanager.h>
00023 #include <kexidb/connectiondata.h>
00024 #include "core/kexi.h"
00025 
00026 #include <kapplication.h>
00027 #include <kiconloader.h>
00028 #include <kmimetype.h>
00029 #include <klocale.h>
00030 #include <kdebug.h>
00031 
00032 #include <qlabel.h>
00033 #include <qpushbutton.h>
00034 #include <qlayout.h>
00035 #include <qlistview.h>
00036 
00037 #include <assert.h>
00038 
00040 class KexiProjectSelectorWidgetPrivate
00041 {
00042 public:
00043     KexiProjectSelectorWidgetPrivate()
00044     {
00045         selectable = true;
00046     }
00047     QPixmap fileicon, dbicon;
00048     bool showProjectNameColumn : 1;
00049     bool showConnectionColumns : 1;
00050     bool selectable : 1;
00051 };
00052 
00053 /*================================================================*/
00054 
00056 class ProjectDataLVItem : public QListViewItem
00057 {
00058 public:
00059     ProjectDataLVItem(KexiProjectData *d, 
00060         const KexiDB::Driver::Info& info, KexiProjectSelectorWidget *selector )
00061         : QListViewItem(selector->list)
00062         , data(d)
00063     {
00064         int colnum = 0;
00065         const KexiDB::ConnectionData *cdata = data->constConnectionData();
00066         if (selector->d->showProjectNameColumn)
00067             setText(colnum++, data->caption()+"  ");
00068         
00069         setText(colnum++, data->databaseName()+"  ");
00070         
00071         if (selector->d->showConnectionColumns) {
00072             QString drvname = info.caption.isEmpty() ? cdata->driverName : info.caption;
00073             if (info.fileBased) {
00074                 setText(colnum++, i18n("File") + " ("+drvname+")  " );
00075             } else {
00076                 setText(colnum++, drvname+"  " );
00077             }
00078         
00079             QString conn;
00080             if (!cdata->caption.isEmpty())
00081                 conn = cdata->caption + ": ";
00082             conn += cdata->serverInfoString();
00083             setText(3, conn + "  ");
00084         }
00085     }
00086     ~ProjectDataLVItem() {}
00087     
00088     KexiProjectData *data;
00089 };
00090 
00091 /*================================================================*/
00092 
00097 KexiProjectSelectorWidget::KexiProjectSelectorWidget( 
00098     QWidget* parent, const char* name, 
00099     KexiProjectSet* prj_set, bool showProjectNameColumn,
00100     bool showConnectionColumns )
00101     : KexiProjectSelectorBase( parent, name )
00102     ,m_prj_set(prj_set)
00103     ,d(new KexiProjectSelectorWidgetPrivate())
00104 {
00105     d->showProjectNameColumn = showProjectNameColumn;
00106     d->showConnectionColumns = showConnectionColumns;
00107     QString none, iconname = KMimeType::mimeType( KexiDB::Driver::defaultFileBasedDriverMimeType() )->icon(none,0);
00108     d->fileicon = KGlobal::iconLoader()->loadIcon( iconname, KIcon::Desktop );
00109     setIcon( d->fileicon );
00110     d->dbicon = SmallIcon("database");
00111 //  list->setHScrollBarMode( QScrollView::AlwaysOn );
00112 
00113     if (!d->showConnectionColumns) {
00114         list->removeColumn(2);
00115         list->removeColumn(2);
00116     }
00117     if (!d->showProjectNameColumn) {
00118         list->removeColumn(0);
00119     }
00120     setFocusProxy(list);
00121 
00122     //show projects
00123     setProjectSet( m_prj_set );
00124     connect(list,SIGNAL(doubleClicked(QListViewItem*)),this,SLOT(slotItemExecuted(QListViewItem*)));
00125     connect(list,SIGNAL(returnPressed(QListViewItem*)),this,SLOT(slotItemExecuted(QListViewItem*)));
00126     connect(list,SIGNAL(selectionChanged()),this,SLOT(slotItemSelected()));
00127 }
00128 
00132 KexiProjectSelectorWidget::~KexiProjectSelectorWidget()
00133 {
00134     delete d;
00135 }
00136 
00137 KexiProjectData* KexiProjectSelectorWidget::selectedProjectData() const
00138 {
00139     ProjectDataLVItem *item = static_cast<ProjectDataLVItem*>(list->selectedItem());
00140     if (item)
00141         return item->data;
00142     return 0;
00143 }
00144 
00145 void KexiProjectSelectorWidget::slotItemExecuted(QListViewItem *item)
00146 {
00147     if (!d->selectable)
00148         return;
00149     ProjectDataLVItem *ditem = static_cast<ProjectDataLVItem*>(item);
00150     if (ditem)
00151         emit projectExecuted( ditem->data );
00152 }
00153 
00154 void KexiProjectSelectorWidget::slotItemSelected()
00155 {
00156     if (!d->selectable)
00157         return;
00158     ProjectDataLVItem *ditem = static_cast<ProjectDataLVItem*>(list->selectedItem());
00159     emit selectionChanged( ditem ? ditem->data : 0 );
00160 }
00161 
00162 void KexiProjectSelectorWidget::setProjectSet( KexiProjectSet* prj_set )
00163 {
00164     if (prj_set) {
00165         //old list
00166         list->clear();
00167     }
00168     m_prj_set = prj_set; 
00169     if (!m_prj_set)
00170         return;
00171 //TODO: what with project set's ownership?
00172     if (m_prj_set->error()) {
00173         kdDebug() << "KexiProjectSelectorWidget::setProjectSet() : m_prj_set->error() !"<<endl;
00174         return;
00175     }
00176     KexiDB::DriverManager manager;
00177     KexiProjectData::List prjlist = m_prj_set->list();
00178     KexiProjectData *data = prjlist.first();
00179     while (data) {
00180         KexiDB::Driver::Info info = manager.driverInfo(data->constConnectionData()->driverName);
00181         if (!info.name.isEmpty()) {
00182             ProjectDataLVItem *item = new ProjectDataLVItem(data, info, this);
00183             if (!d->selectable)
00184                 item->setSelectable(false);
00185             if (info.fileBased)
00186                 item->setPixmap( 0, d->fileicon );
00187             else
00188                 item->setPixmap( 0, d->dbicon );
00189         }
00190         else {
00191             kdWarning() << "KexiProjectSelector::KexiProjectSelector(): no driver found for '" 
00192                 << data->constConnectionData()->driverName << "'!" << endl;
00193         }
00194         data=prjlist.next();
00195     }
00196     if (list->firstChild()) {
00197         list->setSelected(list->firstChild(),true);
00198     }
00199 }
00200 
00201 void KexiProjectSelectorWidget::setSelectable(bool set)
00202 {
00203     if (d->selectable == set)
00204         return;
00205     d->selectable = set;
00206     //update items' state
00207     QListViewItemIterator it( list );
00208     while ( it.current() ) {
00209         it.current()->setSelectable( d->selectable );
00210     }
00211 }
00212     
00213 bool KexiProjectSelectorWidget::isSelectable() const
00214 {
00215     return d->selectable;
00216 }
00217 
00218 /*================================================================*/
00219 
00220 KexiProjectSelectorDialog::KexiProjectSelectorDialog( QWidget *parent, const char *name,
00221     KexiProjectSet* prj_set, bool showProjectNameColumn, bool showConnectionColumns)
00222     : KDialogBase( Plain, i18n("Open Recent Project"), 
00223 #ifndef KEXI_NO_UNFINISHED 
00225     Help | 
00226 #endif
00227     Ok | Cancel, Ok, parent, name )
00228 {
00229     init(prj_set, showProjectNameColumn, showConnectionColumns);
00230 }
00231 
00232 KexiProjectSelectorDialog::KexiProjectSelectorDialog( QWidget *parent, const char *name,
00233     KexiDB::ConnectionData* cdata, 
00234     bool showProjectNameColumn, bool showConnectionColumns)
00235     : KDialogBase( 
00236         Plain, i18n("Open Project"), 
00237 #ifndef KEXI_NO_UNFINISHED 
00239     Help | 
00240 #endif
00241     Ok | Cancel, Ok, parent, name, true/*modal*/, false/*sep*/ )
00242 {
00243     setButtonGuiItem(Ok, KGuiItem(i18n("&Open"), "fileopen", i18n("Open Database Connection")));
00244     assert(cdata);
00245     if (!cdata)
00246         return;
00247     KexiProjectSet *prj_set = new KexiProjectSet( *cdata );
00248     init(prj_set, showProjectNameColumn, showConnectionColumns);
00249     
00250     m_sel->label->setText( i18n("Select a project on <b>%1</b> database server to open:")
00251         .arg(cdata->serverInfoString(false)) );
00252 }
00253 
00254 KexiProjectSelectorDialog::~KexiProjectSelectorDialog()
00255 {
00256 }
00257 
00258 void KexiProjectSelectorDialog::init(KexiProjectSet* prj_set, bool showProjectNameColumn, 
00259     bool showConnectionColumns)
00260 {
00261     setSizeGripEnabled(true);
00262     
00263     QVBoxLayout *lyr = new QVBoxLayout(plainPage(), 0, KDialogBase::spacingHint(), "lyr");
00264     m_sel = new KexiProjectSelectorWidget(plainPage(), "sel", 
00265         prj_set, showProjectNameColumn, showConnectionColumns);
00266     lyr->addWidget(m_sel);
00267     setIcon(*m_sel->icon());
00268     m_sel->setFocus();
00269     
00270     connect(m_sel,SIGNAL(projectExecuted(KexiProjectData*)),
00271         this,SLOT(slotProjectExecuted(KexiProjectData*)));
00272     connect(m_sel,SIGNAL(selectionChanged(KexiProjectData*)),
00273         this,SLOT(slotProjectSelectionChanged(KexiProjectData*)));
00274 }
00275 
00276 KexiProjectData* KexiProjectSelectorDialog::selectedProjectData() const
00277 {
00278     return m_sel->selectedProjectData();
00279 }
00280 
00281 void KexiProjectSelectorDialog::slotProjectExecuted(KexiProjectData*)
00282 {
00283     accept();
00284 }
00285 
00286 void KexiProjectSelectorDialog::slotProjectSelectionChanged(KexiProjectData* pdata)
00287 {
00288     enableButtonOK(pdata);
00289 }
00290 
00291 void KexiProjectSelectorDialog::show()
00292 {
00293     KDialogBase::show();
00294     KDialog::centerOnScreen(this);
00295 }
00296 
00297 #include "KexiProjectSelector.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys