23#include <tqstringlist.h>
30#include <tqlineedit.h>
31#include <tqpopupmenu.h>
35#include <tdepopupmenu.h>
36#include <tdeglobalsettings.h>
37#include <tdemessagebox.h>
39#include <kexidb/tableschema.h>
40#include <kexidb/indexschema.h>
41#include <kexidb/utils.h>
43#include "kexirelationview.h"
44#include "kexirelationviewtable.h"
45#include "kexirelationviewconnection.h"
48KexiRelationView::KexiRelationView(TQWidget *parent,
const char *name)
49 : TQScrollView(parent, name, WStaticContents)
53 m_selectedConnection = 0;
55 m_focusedTableView = 0;
56 setFrameStyle(TQFrame::WinPanel | TQFrame::Sunken);
60 viewport()->setPaletteBackgroundColor(colorGroup().mid());
61 setFocusPolicy(TQWidget::WheelFocus);
62 setResizePolicy(Manual);
76 m_removeSelectedTableQueryAction =
new TDEAction(i18n(
"&Hide Selected Table/Query"),
"edit-delete",
"",
77 this, TQ_SLOT(removeSelectedTableQuery()), parent->actionCollection(),
"relationsview_removeSelectedTableQuery");
78 m_removeSelectedConnectionAction =
new TDEAction(i18n(
"&Remove Selected Relationship"),
"button_cancel",
"",
79 this, TQ_SLOT(removeSelectedConnection()), parent->actionCollection(),
"relationsview_removeSelectedConnection");
80 m_openSelectedTableQueryAction =
new TDEAction(i18n(
"&Open Selected Table/Query"),
"",
"",
81 this, TQ_SLOT(openSelectedTableQuery()), 0,
"relationsview_openSelectedTableQuery");
89 m_popup =
new TDEPopupMenu(
this,
"m_popup");
90 m_openSelectedTableQueryAction->plug( m_popup );
91 m_removeSelectedTableQueryAction->plug( m_popup );
92 m_removeSelectedConnectionAction->plug( m_popup );
97 setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding,
true);
100KexiRelationView::~KexiRelationView()
116KexiRelationViewTableContainer *
119 return t ? m_tables.find(t->name()) : 0;
122KexiRelationViewTableContainer*
128 kdDebug() <<
"KexiRelationView::addTable(): " << t->name() <<
", " << viewport() << endl;
132 kdWarning() <<
"KexiRelationView::addTable(): table already added" << endl;
136 c =
new KexiRelationViewTableContainer(
this,
140 connect(c, TQ_SIGNAL(endDrag()),
this, TQ_SLOT(slotTableViewEndDrag()));
141 connect(c, TQ_SIGNAL(gotFocus()),
this, TQ_SLOT(slotTableViewGotFocus()));
144 connect(c, TQ_SIGNAL(contextMenuRequest(
const TQPoint&)),
145 this, TQ_SIGNAL(tableContextMenuRequest(
const TQPoint&)));
147 addChild(c, 100,100);
148 if (rect.isValid()) {
149 TQSize finalSize = c->size().expandedTo( c->sizeHint() );
151 r.setSize( finalSize + TQSize(0,10) );
152 moveChild( c, rect.left(), rect.top() );
155 c->resize(c->sizeHint());
165 if (!rect.isValid()) {
167 c->resize(c->sizeHint());
171 if(m_tables.count() > 0)
174 TQDictIterator<KexiRelationViewTableContainer> it(m_tables);
175 for(; it.current(); ++it)
177 int right = (*it)->x() + (*it)->width();
190 TQPoint p = viewportToContents(TQPoint(x, y));
191 recalculateSize(p.x() + c->width(), p.y() + c->height());
192 if (!rect.isValid()) {
196 m_tables.insert(t->name(), c);
198 connect(c, TQ_SIGNAL(moved(KexiRelationViewTableContainer *)),
this,
210 SourceConnection conn = _conn;
211 kdDebug() <<
"KexiRelationView::addConnection()" << endl;
213 KexiRelationViewTableContainer *master = m_tables[conn.masterTable];
214 KexiRelationViewTableContainer *details = m_tables[conn.detailsTable];
215 if (!master || !details)
222 if (!masterTable || !detailsTable)
228 if (!masterFld || !detailsFld)
235 masterFld = detailsFld;
238 masterTable = detailsTable;
239 detailsTable = tmpTable;
240 KexiRelationViewTableContainer *tmp = master;
243 TQString tmp_masterTable = conn.masterTable;
244 conn.masterTable = conn.detailsTable;
245 conn.detailsTable = tmp_masterTable;
246 TQString tmp_masterField = conn.masterField;
247 conn.masterField = conn.detailsField;
248 conn.detailsField = tmp_masterField;
254 KexiRelationViewConnection *connView =
new KexiRelationViewConnection(master, details, conn,
this);
255 m_connectionViews.append(connView);
256 updateContents(connView->connectionRect());
269 kdDebug() <<
"KexiRelationView::addConnection: adding self" << endl;
270 RelationList l = m_relation->projectRelations();
272 m_relation->updateRelationList(
this, l);
278KexiRelationView::drawContents(TQPainter *p,
int cx,
int cy,
int cw,
int ch)
280 KexiRelationViewConnection *cview;
283 TQRect clipping(cx, cy, cw, ch);
284 for(cview = m_connectionViews.first(); cview; cview = m_connectionViews.next())
286 if(clipping.intersects(cview->oldRect() | cview->connectionRect()))
287 cview->drawConnection(p);
292KexiRelationView::slotTableScrolling(
const TQString& table)
294 KexiRelationViewTableContainer *c = m_tables[table];
303 KexiRelationViewConnection *cview;
305 for (ConnectionListIterator it(m_connectionViews); ((cview=it.current())); ++it) {
307 if(cview->masterTable() == c || cview->detailsTable() == c
308 || cview->connectionRect().intersects(r))
310 r |= cview->oldRect();
311 kdDebug() << r << endl;
312 r |= cview->connectionRect();
313 kdDebug() << r << endl;
326 TQPoint p = viewportToContents(TQPoint(c->x(), c->y()));
327 recalculateSize(p.x() + c->width(), p.y() + c->height());
329 emit tablePositionChanged(c);
333KexiRelationView::setReadOnly(
bool b)
349KexiRelationView::slotListUpdate(TQObject *)
354 m_connectionViews.clear();
355 RelationList rl = m_relation->projectRelations();
358 RelationList::ConstIterator it, end( rl.constEnd() );
359 for( it = rl.begin(); it != end; ++it)
371KexiRelationView::contentsMousePressEvent(TQMouseEvent *ev)
373 KexiRelationViewConnection *cview;
374 for(cview = m_connectionViews.first(); cview; cview = m_connectionViews.next())
376 if(!cview->matchesPoint(ev->pos(), 3))
380 cview->setSelected(
true);
381 updateContents(cview->connectionRect());
382 m_selectedConnection = cview;
383 emit connectionViewGotFocus();
386 if(ev->button() == TQt::RightButton) {
387 kdDebug() <<
"KexiRelationView::contentsMousePressEvent(): context" << endl;
391 emit connectionContextMenuRequest( ev->globalPos() );
399 if(ev->button() == TQt::RightButton) {
402 emit emptyAreaContextMenuRequest( ev->globalPos() );
406 emit emptyAreaGotFocus();
414 if (m_focusedTableView) {
415 m_focusedTableView->unsetFocus();
416 m_focusedTableView = 0;
420 if (m_selectedConnection) {
421 m_selectedConnection->setSelected(
false);
422 updateContents(m_selectedConnection->connectionRect());
423 m_selectedConnection = 0;
429KexiRelationView::keyPressEvent(TQKeyEvent *ev)
431 kdDebug() <<
"KexiRelationView::keyPressEvent()" << endl;
433 if (ev->key()==TDEGlobalSettings::contextMenuKey()) {
434 if (m_selectedConnection) {
435 emit connectionContextMenuRequest(
436 mapToGlobal(m_selectedConnection->connectionRect().center()) );
442 if(ev->key() == Key_Delete)
448KexiRelationView::recalculateSize(
int width,
int height)
450 kdDebug() <<
"recalculateSize(" << width <<
", " << height <<
")" << endl;
451 int newW = contentsWidth(), newH = contentsHeight();
452 kdDebug() <<
"contentsSize(" << newW <<
", " << newH <<
")" << endl;
460 resizeContents(newW, newH);
467KexiRelationView::stretchExpandSize()
469 int max_x=-1, max_y=-1;
470 TQDictIterator<KexiRelationViewTableContainer> it(m_tables);
471 for (;it.current(); ++it) {
472 if (it.current()->right()>max_x)
473 max_x = it.current()->right();
474 if (it.current()->bottom()>max_y)
475 max_y = it.current()->bottom();
477 TQPoint p = viewportToContents(TQPoint(max_x, max_y) + TQPoint(3,3));
478 resizeContents(p.x(), p.y());
481void KexiRelationView::slotTableViewEndDrag()
483 kdDebug() <<
"END DRAG!" <<endl;
491 if (m_selectedConnection) {
492 removeConnection(m_selectedConnection);
495 RelationList l = m_relation->projectRelations();
497 for(RelationList::Iterator it = l.begin(); it != l.end(); ++it)
499 if((*it).srcTable == m_selectedConnection->connection().srcTable
500 && (*it).rcvTable == m_selectedConnection->connection().rcvTable
501 && (*it).srcField == m_selectedConnection->connection().srcField
502 && (*it).rcvField == m_selectedConnection->connection().rcvField)
504 kdDebug() <<
"KexiRelationView::removeSelectedConnection(): matching found!" << endl;
513 kdDebug() <<
"KexiRelationView::removeSelectedConnection(): d2" << endl;
514 m_relation->updateRelationList(
this, nl);
515 kdDebug() <<
"KexiRelationView::removeSelectedConnection(): d3" << endl;
517 delete m_selectedConnection;
518 m_selectedConnection = 0;
521 else if (m_focusedTableView) {
522 KexiRelationViewTableContainer *tmp = m_focusedTableView;
523 m_focusedTableView = 0;
529KexiRelationView::hideTable(KexiRelationViewTableContainer* tableView)
534 TQPtrListIterator<KexiRelationViewConnection> it(m_connectionViews);
535 for (;it.current();) {
536 if (it.current()->masterTable() == tableView
537 || it.current()->detailsTable() == tableView)
540 removeConnection(it.current());
546 m_tables.take(tableView->schema()->name());
548 emit tableHidden( *ts );
555 for (TablesDictIterator it(m_tables); it.current();) {
557 if (!table ||
tables->findRef( table )!=-1) {
561 hideTable(it.current());
566KexiRelationView::removeConnection(KexiRelationViewConnection *conn)
568 emit aboutConnectionRemove(conn);
569 m_connectionViews.remove(conn);
570 updateContents(conn->connectionRect());
571 kdDebug() <<
"KexiRelationView::removeConnection()" << endl;
574void KexiRelationView::slotTableViewGotFocus()
576 if (m_focusedTableView == sender())
578 kdDebug() <<
"GOT FOCUS!" <<endl;
582 m_focusedTableView = (KexiRelationViewTableContainer*)sender();
584 emit tableViewGotFocus();
587TQSize KexiRelationView::sizeHint()
const
589 return TQSize(TQScrollView::sizeHint());
595 m_tables.setAutoDelete(
true);
597 m_tables.setAutoDelete(
false);
604 m_connectionViews.setAutoDelete(
true);
605 m_connectionViews.clear();
606 m_connectionViews.setAutoDelete(
false);
622void KexiRelationView::invalidateActions()
624 setAvailable("edit_delete", m_selectedConnection || m_focusedTableView);
627void KexiRelationView::executePopup( TQPoint pos )
629 if (pos==TQPoint(-1,-1)) {
630 pos = mapToGlobal( m_focusedTableView ? m_focusedTableView->pos() + m_focusedTableView->rect().center() : rect().center() );
632 if (m_focusedTableView)
633 m_tableQueryPopup->exec(pos);
634 else if (m_selectedConnection)
635 m_connectionPopup->exec(pos);
639#include "kexirelationview.moc"
TQPtrList< TableSchema > List
Type of tables list.
void containerMoved(KexiRelationViewTableContainer *c)
KexiRelationViewTableContainer * addTableContainer(KexiDB::TableSchema *t, const TQRect &rect=TQRect())
void clearSelection()
Clears current selection - table/query or connection.
KexiRelationViewTableContainer * tableContainer(KexiDB::TableSchema *t) const
void hideAllTablesExcept(KexiDB::TableSchema::List *tables)
void addConnection(const SourceConnection &_conn)
Adds a connection con to the area. This changes only visual representation.
void removeSelectedObject()
removes selected table or connection
void removeAllConnections()