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 //  if (m_focusedTableView != sender())
00247 //      return;
00248     kdDebug() << "HEADER CTXT MENU!" <<endl;
00249     invalidateActions();
00250     executePopup( pos );
00251 //  m_tableQueryPopup->exec(pos);
00252 }
00253 
00254 void KexiRelationWidget::connectionContextMenuRequest(const QPoint& pos)
00255 {
00256     invalidateActions();
00257     executePopup( pos );
00258 //  m_connectionPopup->exec(pos);
00259 }
00260 
00261 void KexiRelationWidget::emptyAreaContextMenuRequest( const QPoint& /*pos*/ )
00262 {
00263     invalidateActions();
00264     //TODO
00265 }
00266 
00267 void KexiRelationWidget::invalidateActions()
00268 {
00269     setAvailable("edit_delete", m_relationView->selectedConnection() || m_relationView->focusedTableView());
00270 }
00271 
00272 void KexiRelationWidget::executePopup( QPoint pos )
00273 {
00274     if (pos==QPoint(-1,-1)) {
00275         pos = mapToGlobal( 
00276             m_relationView->focusedTableView() ? m_relationView->focusedTableView()->pos() + m_relationView->focusedTableView()->rect().center() : rect().center() );
00277     }
00278     if (m_relationView->focusedTableView())
00279         m_tableQueryPopup->exec(pos);
00280     else if (m_relationView->selectedConnection())
00281         m_connectionPopup->exec(pos);
00282 }
00283 
00284 void KexiRelationWidget::removeSelectedObject()
00285 {
00286     m_relationView->removeSelectedObject();
00287 }
00288 
00289 void KexiRelationWidget::openSelectedTable()
00290 {
00292     if (!m_relationView->focusedTableView() || !m_relationView->focusedTableView()->schema()->table())
00293         return;
00294     bool openingCancelled;
00295     m_win->openObject("kexi/table", m_relationView->focusedTableView()->schema()->name(), 
00296         Kexi::DataViewMode, openingCancelled);
00297 }
00298 
00299 void KexiRelationWidget::designSelectedTable()
00300 {
00302     if (!m_relationView->focusedTableView() || !m_relationView->focusedTableView()->schema()->table())
00303         return;
00304     bool openingCancelled;
00305     m_win->openObject("kexi/table", m_relationView->focusedTableView()->schema()->name(), 
00306         Kexi::DesignViewMode, openingCancelled);
00307 }
00308 
00309 QSize KexiRelationWidget::sizeHint() const
00310 {
00311     return m_relationView->sizeHint();
00312 }
00313 
00314 void KexiRelationWidget::slotTableHidden(KexiDB::TableSchema &table)
00315 {
00316     const QString &t = table.name().lower();
00317     int i;
00318     for (i=0; i<m_tableCombo->count() && t > m_tableCombo->text(i).lower(); i++)
00319         ;
00320     m_tableCombo->insertItem(table.name(), i);
00321     if (!m_tableCombo->isEnabled()) {
00322         m_tableCombo->setCurrentItem(0);
00323         m_tableCombo->setEnabled(true);
00324         m_btnAdd->setEnabled(true);
00325     }
00326 
00327     emit tableHidden(table);
00328 }
00329 
00330 void KexiRelationWidget::aboutToShowPopupMenu()
00331 {
00333     if (m_relationView->focusedTableView() && m_relationView->focusedTableView()->schema()->table()) {
00334         m_tableQueryPopup->changeTitle(m_tableQueryPopupTitleID, SmallIcon("table"),
00335             QString(m_relationView->focusedTableView()->schema()->name()) + " : " + i18n("Table"));
00336     }
00337     else if (m_relationView->selectedConnection()) {
00338         m_connectionPopup->changeTitle( m_connectionPopupTitleID, 
00339              m_relationView->selectedConnection()->toString() + " : " + i18n("Relationship") );
00340     }
00341 }
00342 
00343 void
00344 KexiRelationWidget::slotTableFieldDoubleClicked(QListViewItem *i,const QPoint&,int)
00345 {
00346     if (!sender()->isA("KexiRelationViewTable"))
00347         return;
00348     emit tableFieldDoubleClicked( 
00349         static_cast<const KexiRelationViewTable*>(sender())->schema()->table(), i->text(0) );
00350 }
00351 
00352 void 
00353 KexiRelationWidget::clear()
00354 {
00355     m_relationView->clear();
00356     fillTablesCombo();
00357 }
00358 
00360 void KexiRelationWidget::removeAllConnections()
00361 {
00362     m_relationView->removeAllConnections();
00363 }
00364 
00365 void 
00366 KexiRelationWidget::fillTablesCombo()
00367 {
00368     m_tableCombo->clear();
00369     QStringList tmp = m_conn->tableNames();
00370     tmp.sort();
00371     m_tableCombo->insertStringList(tmp);
00372 }
00373 
00374 void
00375 KexiRelationWidget::objectCreated(const QCString &mime, const QCString& name)
00376 {
00377     if (mime=="kexi/table" || mime=="kexi/query") {
00379         m_tableCombo->insertItem(QString(name));
00380         m_tableCombo->listBox()->sort();
00381     }
00382 }
00383 
00384 void
00385 KexiRelationWidget::objectDeleted(const QCString &mime, const QCString& name)
00386 {
00387     if (mime=="kexi/table" || mime=="kexi/query") {
00388         QString strName(name);
00389         for (int i=0; i<m_tableCombo->count(); i++) {
00391             if (m_tableCombo->text(i)==strName) {
00392                 m_tableCombo->removeItem(i);
00393                 if (m_tableCombo->currentItem()==i) {
00394                     if (i==(m_tableCombo->count()-1))
00395                         m_tableCombo->setCurrentItem(i-1);
00396                     else
00397                         m_tableCombo->setCurrentItem(i);
00398                 }
00399                 break;
00400             }
00401         }
00402     }
00403 }
00404 
00405 void
00406 KexiRelationWidget::objectRenamed(const QCString &mime, const QCString& name, const QCString& newName)
00407 {
00408     if (mime=="kexi/table" || mime=="kexi/query") {
00409         QString strName(name);
00410         for (int i=0; i<m_tableCombo->count(); i++) {
00412             if (m_tableCombo->text(i)==strName) {
00413                 m_tableCombo->changeItem(QString(newName), i);
00414                 m_tableCombo->listBox()->sort();
00415                 break;
00416             }
00417         }
00418     }
00419 }
00420 
00421 void
00422 KexiRelationWidget::hideAllTablesExcept( KexiDB::TableSchema::List* tables )
00423 {
00424     m_relationView->hideAllTablesExcept(tables);
00425 }
00426 
00427 #include "kexirelationwidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys