kexi

kexirelationwidget.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002   Lucijan Busch <lucijan@gmx.at>
00003    Copyright (C) 2003   Joseph Wenninger<jowenn@kde.org>
00004    Copyright (C) 2003-2004 Jaroslaw Staniek <js@iidea.pl>
00005 
00006    This program is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this program; see the file COPYING.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "kexirelationwidget.h"
00023 
00024 #include <qlayout.h>
00025 #include <qlistbox.h>
00026 #include <qpushbutton.h>
00027 #include <qtimer.h>
00028 
00029 #include <kcombobox.h>
00030 #include <klocale.h>
00031 #include <kdebug.h>
00032 #include <kiconloader.h>
00033 #include <kpushbutton.h>
00034 
00035 #include <kexidb/connection.h>
00036 #include <kexidb/utils.h>
00037 
00038 #include <kexiproject.h>
00039 #include <keximainwindow.h>
00040 #include "kexirelationview.h"
00041 #include "kexirelationviewtable.h"
00042 #include "kexirelationviewconnection.h"
00043 
00044 KexiRelationWidget::KexiRelationWidget(KexiMainWindow *win, QWidget *parent, 
00045     const char *name)
00046     : KexiViewBase(win, parent, name)
00047     , m_win(win)
00048 {
00049     m_conn = m_win->project()->dbConnection();
00050 
00051     QHBoxLayout *hlyr = new QHBoxLayout(0);
00052     QGridLayout *g = new QGridLayout(this);
00053     g->addLayout( hlyr, 0, 0 );
00054 
00055     m_tableCombo = new KComboBox(this, "tables_combo");
00056     m_tableCombo->setMinimumWidth(QFontMetrics(font()).width("w")*20);
00057     QLabel *lbl = new QLabel(m_tableCombo, i18n("Table")+": ", this);
00058     lbl->setIndent(3);
00059     m_tableCombo->setInsertionPolicy(QComboBox::NoInsertion);
00060     hlyr->addWidget(lbl);
00061     hlyr->addWidget(m_tableCombo);
00062     m_tableCombo->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred));
00063     fillTablesCombo();
00064 
00065     m_btnAdd = new KPushButton(i18n("&Add"), this);
00066     hlyr->addWidget(m_btnAdd);
00067     hlyr->addStretch(1);
00068     connect(m_btnAdd, SIGNAL(clicked()), this, SLOT(slotAddTable()));
00069 
00070     m_relationView = new KexiRelationView(this, "relation_view");
00071     setViewWidget(m_relationView);
00072     g->addWidget(m_relationView, 1, 0);
00073     //m_relationView->setFocus();
00074 
00075     //actions
00076     m_tableQueryPopup = new KPopupMenu(this, "m_popup");
00077     m_tableQueryPopupTitleID = m_tableQueryPopup->insertTitle(SmallIcon("table"), "");
00078     connect(m_tableQueryPopup, SIGNAL(aboutToShow()), this, SLOT(aboutToShowPopupMenu()));
00079 
00080     m_connectionPopup = new KPopupMenu(this, "m_connectionPopup");
00081     m_connectionPopupTitleID = m_connectionPopup->insertTitle("");
00082     connect(m_connectionPopup, SIGNAL(aboutToShow()), this, SLOT(aboutToShowPopupMenu()));
00083 
00084     m_areaPopup = new KPopupMenu(this, "m_areaPopup");
00085     
00086     m_openSelectedTableAction = new KAction(i18n("&Open Table"), SmallIcon("fileopen"), KShortcut(),
00087         this, SLOT(openSelectedTable()), this, "relationsview_openTable");
00088     m_openSelectedTableAction->plug( m_tableQueryPopup );
00089     m_designSelectedTableAction = new KAction(i18n("&Design Table"), SmallIcon("edit"), KShortcut(),
00090         this, SLOT(designSelectedTable()), this, "relationsview_designTable");
00091     m_designSelectedTableAction->plug( m_tableQueryPopup );
00092     m_tableQueryPopup->insertSeparator();
00093 
00094     KAction* hide_action = plugSharedAction("edit_delete", i18n("&Hide Table"), m_tableQueryPopup);
00095     hide_action->setIconSet(QIconSet());
00096 
00097     plugSharedAction("edit_delete",m_connectionPopup);
00098     plugSharedAction("edit_delete",this, SLOT(removeSelectedObject()));
00099 
00100     connect(m_relationView, SIGNAL(tableViewGotFocus()),
00101         this, SLOT(tableViewGotFocus()));
00102     connect(m_relationView, SIGNAL(connectionViewGotFocus()),
00103         this, SLOT(connectionViewGotFocus()));
00104     connect(m_relationView, SIGNAL(emptyAreaGotFocus()),
00105         this, SLOT(emptyAreaGotFocus()));
00106     connect(m_relationView, SIGNAL(tableContextMenuRequest( const QPoint& )),
00107         this, SLOT(tableContextMenuRequest( const QPoint& )));
00108     connect(m_relationView, SIGNAL(connectionContextMenuRequest( const QPoint& )),
00109         this, SLOT(connectionContextMenuRequest( const QPoint& )));
00110     connect(m_relationView, SIGNAL(tableHidden(KexiDB::TableSchema&)),
00111         this, SLOT(slotTableHidden(KexiDB::TableSchema&)));
00112     connect(m_relationView, SIGNAL(tablePositionChanged(KexiRelationViewTableContainer*)),
00113         this, SIGNAL(tablePositionChanged(KexiRelationViewTableContainer*)));
00114     connect(m_relationView, SIGNAL(aboutConnectionRemove(KexiRelationViewConnection*)),
00115         this, SIGNAL(aboutConnectionRemove(KexiRelationViewConnection*)));
00116 
00117 #if 0
00118     if(!embedd)
00119     {
00120 /*todo      setContextHelp(i18n("Relations"), i18n("To create a relationship simply drag the source field onto the target field. "
00121             "An arrowhead is used to show which table is the parent (master) and which table is the child (slave) in the relationship."));*/
00122     }
00123 #endif
00124 //  else
00125 //js: while embedding means read-only?      m_relationView->setReadOnly(true);
00126 
00127 #ifdef TESTING_KexiRelationWidget
00128     for (int i=0;i<(int)m_db->tableNames().count();i++)
00129         QTimer::singleShot(100,this,SLOT(slotAddTable()));
00130 #endif
00131 
00132     invalidateActions();
00133 }
00134 
00135 KexiRelationWidget::~KexiRelationWidget()
00136 {
00137 }
00138 
00139 TablesDict* KexiRelationWidget::tables() const
00140 {
00141     return m_relationView->tables();
00142 }
00143 
00144 KexiRelationViewTableContainer* KexiRelationWidget::table(const QString& name) const
00145 {
00146     return m_relationView->tables()->find( name );
00147 }
00148 
00149 const ConnectionList* KexiRelationWidget::connections() const
00150 { 
00151     return m_relationView->connections();
00152 }
00153 
00154 void
00155 KexiRelationWidget::slotAddTable()
00156 {
00157     if (m_tableCombo->currentItem()==-1)
00158         return;
00159     QString tname = m_tableCombo->text(m_tableCombo->currentItem());
00160     KexiDB::TableSchema *t = m_conn->tableSchema(tname);
00161     addTable(t);
00162 }
00163 
00164 void
00165 KexiRelationWidget::addTable(KexiDB::TableSchema *t, const QRect &rect)
00166 {
00167     if (!t)
00168         return;
00169     if (!m_relationView->tableContainer(t)) {
00170         KexiRelationViewTableContainer *c = m_relationView->addTableContainer(t, rect);
00171         kdDebug() << "KexiRelationWidget::slotAddTable(): adding table " << t->name() << endl;
00172         if (!c)
00173             return;
00174         connect(c->tableView(), SIGNAL(doubleClicked(QListViewItem*,const QPoint&,int)),
00175             this, SLOT(slotTableFieldDoubleClicked(QListViewItem*,const QPoint&,int)));
00176     }
00177 
00178     const QString tname = t->name().lower();
00179     const int count = m_tableCombo->count();
00180     int i = 0;
00181     for (; i < count; i++ ) {
00182         if (m_tableCombo->text(i).lower() == tname )
00183             break;
00184     }
00185     if (i<count) {
00186         int oi = m_tableCombo->currentItem();
00187         kdDebug()<<"KexiRelationWidget::slotAddTable(): removing a table from the combo box"<<endl;
00188         m_tableCombo->removeItem(i);
00189         if (m_tableCombo->count()>0) {
00190             if (oi>=m_tableCombo->count()) {
00191                 oi=m_tableCombo->count()-1;
00192             }
00193             m_tableCombo->setCurrentItem(oi);
00194         }
00195         else {
00196             m_tableCombo->setEnabled(false);
00197             m_btnAdd->setEnabled(false);
00198         }
00199     }
00200     emit tableAdded(*t);
00201 }
00202 
00203 void
00204 KexiRelationWidget::addConnection(const SourceConnection& conn)
00205 {
00206     m_relationView->addConnection(conn);
00207 }
00208 
00209 void
00210 KexiRelationWidget::addTable(const QString& t)
00211 {
00212     for(int i=0; i < m_tableCombo->count(); i++)
00213     {
00214         if(m_tableCombo->text(i) == t)
00215         {
00216             m_tableCombo->setCurrentItem(i);
00217             slotAddTable();
00218         }
00219     }
00220 }
00221 
00222 void KexiRelationWidget::tableViewGotFocus()
00223 {
00224 //  if (m_relationView->focusedTableView == sender())
00225 //      return;
00226 //  kdDebug() << "GOT FOCUS!" <<endl;
00227 //  clearSelection();
00228 //  if (m_focusedTableView)
00229 //      m_focusedTableView->unsetFocus();
00230 //  m_focusedTableView = (KexiRelationViewTableContainer*)sender();
00231     invalidateActions();
00232 }
00233 
00234 void KexiRelationWidget::connectionViewGotFocus()
00235 {
00236     invalidateActions();
00237 }
00238 
00239 void KexiRelationWidget::emptyAreaGotFocus()
00240 {
00241     invalidateActions();
00242 }
00243 
00244 void KexiRelationWidget::tableContextMenuRequest(const QPoint& pos)
00245 {
00246     invalidateActions();
00247     executePopup( pos );
00248 }
00249 
00250 void KexiRelationWidget::connectionContextMenuRequest(const QPoint& pos)
00251 {
00252     invalidateActions();
00253     executePopup( pos );
00254 //  m_connectionPopup->exec(pos);
00255 }
00256 
00257 void KexiRelationWidget::emptyAreaContextMenuRequest( const QPoint& /*pos*/ )
00258 {
00259     invalidateActions();
00260     //TODO
00261 }
00262 
00263 void KexiRelationWidget::invalidateActions()
00264 {
00265     setAvailable("edit_delete", m_relationView->selectedConnection() || m_relationView->focusedTableView());
00266 }
00267 
00268 void KexiRelationWidget::executePopup( QPoint pos )
00269 {
00270     if (pos==QPoint(-1,-1)) {
00271         pos = mapToGlobal( 
00272             m_relationView->focusedTableView() ? m_relationView->focusedTableView()->pos() + m_relationView->focusedTableView()->rect().center() : rect().center() );
00273     }
00274     if (m_relationView->focusedTableView())
00275         m_tableQueryPopup->exec(pos);
00276     else if (m_relationView->selectedConnection())
00277         m_connectionPopup->exec(pos);
00278 }
00279 
00280 void KexiRelationWidget::removeSelectedObject()
00281 {
00282     m_relationView->removeSelectedObject();
00283 }
00284 
00285 void KexiRelationWidget::openSelectedTable()
00286 {
00288     if (!m_relationView->focusedTableView() || !m_relationView->focusedTableView()->schema()->table())
00289         return;
00290     bool openingCancelled;
00291     m_win->openObject("kexi/table", m_relationView->focusedTableView()->schema()->name(), 
00292         Kexi::DataViewMode, openingCancelled);
00293 }
00294 
00295 void KexiRelationWidget::designSelectedTable()
00296 {
00298     if (!m_relationView->focusedTableView() || !m_relationView->focusedTableView()->schema()->table())
00299         return;
00300     bool openingCancelled;
00301     m_win->openObject("kexi/table", m_relationView->focusedTableView()->schema()->name(), 
00302         Kexi::DesignViewMode, openingCancelled);
00303 }
00304 
00305 QSize KexiRelationWidget::sizeHint() const
00306 {
00307     return m_relationView->sizeHint();
00308 }
00309 
00310 void KexiRelationWidget::slotTableHidden(KexiDB::TableSchema &table)
00311 {
00312     const QString &t = table.name().lower();
00313     int i;
00314     for (i=0; i<m_tableCombo->count() && t > m_tableCombo->text(i).lower(); i++)
00315         ;
00316     m_tableCombo->insertItem(table.name(), i);
00317     if (!m_tableCombo->isEnabled()) {
00318         m_tableCombo->setCurrentItem(0);
00319         m_tableCombo->setEnabled(true);
00320         m_btnAdd->setEnabled(true);
00321     }
00322 
00323     emit tableHidden(table);
00324 }
00325 
00326 void KexiRelationWidget::aboutToShowPopupMenu()
00327 {
00329     if (m_relationView->focusedTableView() && m_relationView->focusedTableView()->schema()->table()) {
00330         m_tableQueryPopup->changeTitle(m_tableQueryPopupTitleID, SmallIcon("table"),
00331             QString(m_relationView->focusedTableView()->schema()->name()) + " : " + i18n("Table"));
00332     }
00333     else if (m_relationView->selectedConnection()) {
00334         m_connectionPopup->changeTitle( m_connectionPopupTitleID, 
00335              m_relationView->selectedConnection()->toString() + " : " + i18n("Relationship") );
00336     }
00337 }
00338 
00339 void
00340 KexiRelationWidget::slotTableFieldDoubleClicked(QListViewItem *i,const QPoint&,int)
00341 {
00342     if (!sender()->isA("KexiRelationViewTable"))
00343         return;
00344     const KexiRelationViewTable* t = static_cast<const KexiRelationViewTable*>(sender());
00345     const QStringList selectedFieldNames( t->selectedFieldNames() );
00346     if (selectedFieldNames.count()==1)
00347         emit tableFieldDoubleClicked( t->schema()->table(), selectedFieldNames.first() );
00348 }
00349 
00350 void 
00351 KexiRelationWidget::clear()
00352 {
00353     m_relationView->clear();
00354     fillTablesCombo();
00355 }
00356 
00358 void KexiRelationWidget::removeAllConnections()
00359 {
00360     m_relationView->removeAllConnections();
00361 }
00362 
00363 void 
00364 KexiRelationWidget::fillTablesCombo()
00365 {
00366     m_tableCombo->clear();
00367     QStringList tmp = m_conn->tableNames();
00368     tmp.sort();
00369     m_tableCombo->insertStringList(tmp);
00370 }
00371 
00372 void
00373 KexiRelationWidget::objectCreated(const QCString &mime, const QCString& name)
00374 {
00375     if (mime=="kexi/table" || mime=="kexi/query") {
00377         m_tableCombo->insertItem(QString(name));
00378         m_tableCombo->listBox()->sort();
00379     }
00380 }
00381 
00382 void
00383 KexiRelationWidget::objectDeleted(const QCString &mime, const QCString& name)
00384 {
00385     if (mime=="kexi/table" || mime=="kexi/query") {
00386         QString strName(name);
00387         for (int i=0; i<m_tableCombo->count(); i++) {
00389             if (m_tableCombo->text(i)==strName) {
00390                 m_tableCombo->removeItem(i);
00391                 if (m_tableCombo->currentItem()==i) {
00392                     if (i==(m_tableCombo->count()-1))
00393                         m_tableCombo->setCurrentItem(i-1);
00394                     else
00395                         m_tableCombo->setCurrentItem(i);
00396                 }
00397                 break;
00398             }
00399         }
00400     }
00401 }
00402 
00403 void
00404 KexiRelationWidget::objectRenamed(const QCString &mime, const QCString& name, const QCString& newName)
00405 {
00406     if (mime=="kexi/table" || mime=="kexi/query") {
00407         QString strName(name);
00408         for (int i=0; i<m_tableCombo->count(); i++) {
00410             if (m_tableCombo->text(i)==strName) {
00411                 m_tableCombo->changeItem(QString(newName), i);
00412                 m_tableCombo->listBox()->sort();
00413                 break;
00414             }
00415         }
00416     }
00417 }
00418 
00419 void
00420 KexiRelationWidget::hideAllTablesExcept( KexiDB::TableSchema::List* tables )
00421 {
00422     m_relationView->hideAllTablesExcept(tables);
00423 }
00424 
00425 #include "kexirelationwidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys