summaryrefslogtreecommitdiffstats
path: root/kexi/widget/relations/kexirelationwidget.cpp
blob: 2fbda78ec28118d29f47c8ca592585009f251a8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* This file is part of the KDE project
   Copyright (C) 2002   Lucijan Busch <[email protected]>
   Copyright (C) 2003   Joseph Wenninger<[email protected]>
   Copyright (C) 2003-2004 Jaroslaw Staniek <[email protected]>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kexirelationwidget.h"

#include <tqlayout.h>
#include <tqlistbox.h>
#include <tqpushbutton.h>
#include <tqtimer.h>

#include <kcombobox.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <kpushbutton.h>

#include <kexidb/connection.h>
#include <kexidb/utils.h>

#include <kexiproject.h>
#include <keximainwindow.h>
#include "kexirelationview.h"
#include "kexirelationviewtable.h"
#include "kexirelationviewconnection.h"

KexiRelationWidget::KexiRelationWidget(KexiMainWindow *win, TQWidget *parent, 
	const char *name)
	: KexiViewBase(win, parent, name)
	, m_win(win)
{
	m_conn = m_win->project()->dbConnection();

	TQHBoxLayout *hlyr = new TQHBoxLayout(0);
	TQGridLayout *g = new TQGridLayout(this);
	g->addLayout( hlyr, 0, 0 );

	m_tableCombo = new KComboBox(this, "tables_combo");
	m_tableCombo->setMinimumWidth(TQFontMetrics(font()).width("w")*20);
	TQLabel *lbl = new TQLabel(m_tableCombo, i18n("Table")+": ", this);
	lbl->setIndent(3);
	m_tableCombo->setInsertionPolicy(TQComboBox::NoInsertion);
	hlyr->addWidget(lbl);
	hlyr->addWidget(m_tableCombo);
	m_tableCombo->setSizePolicy(TQSizePolicy(TQSizePolicy::MinimumExpanding, TQSizePolicy::Preferred));
	fillTablesCombo();

	m_btnAdd = new KPushButton(i18n("&Add"), this);
	hlyr->addWidget(m_btnAdd);
	hlyr->addStretch(1);
	connect(m_btnAdd, TQ_SIGNAL(clicked()), this, TQ_SLOT(slotAddTable()));

	m_relationView = new KexiRelationView(this, "relation_view");
	setViewWidget(m_relationView);
	g->addWidget(m_relationView, 1, 0);
	//m_relationView->setFocus();

	//actions
	m_tableQueryPopup = new TDEPopupMenu(this, "m_popup");
	m_tableQueryPopupTitleID = m_tableQueryPopup->insertTitle(SmallIcon("table"), "");
	connect(m_tableQueryPopup, TQ_SIGNAL(aboutToShow()), this, TQ_SLOT(aboutToShowPopupMenu()));

	m_connectionPopup = new TDEPopupMenu(this, "m_connectionPopup");
	m_connectionPopupTitleID = m_connectionPopup->insertTitle("");
	connect(m_connectionPopup, TQ_SIGNAL(aboutToShow()), this, TQ_SLOT(aboutToShowPopupMenu()));

	m_areaPopup = new TDEPopupMenu(this, "m_areaPopup");
	
	m_openSelectedTableAction = new TDEAction(i18n("&Open Table"), SmallIcon("document-open"), TDEShortcut(),
		this, TQ_SLOT(openSelectedTable()), this, "relationsview_openTable");
	m_openSelectedTableAction->plug( m_tableQueryPopup );
	m_designSelectedTableAction = new TDEAction(i18n("&Design Table"), SmallIcon("edit"), TDEShortcut(),
		this, TQ_SLOT(designSelectedTable()), this, "relationsview_designTable");
	m_designSelectedTableAction->plug( m_tableQueryPopup );
	m_tableQueryPopup->insertSeparator();

	TDEAction* hide_action = plugSharedAction("edit_delete", i18n("&Hide Table"), m_tableQueryPopup);
	hide_action->setIconSet(TQIconSet());

	plugSharedAction("edit_delete",m_connectionPopup);
	plugSharedAction("edit_delete",this, TQ_SLOT(removeSelectedObject()));

	connect(m_relationView, TQ_SIGNAL(tableViewGotFocus()),
		this, TQ_SLOT(tableViewGotFocus()));
	connect(m_relationView, TQ_SIGNAL(connectionViewGotFocus()),
		this, TQ_SLOT(connectionViewGotFocus()));
	connect(m_relationView, TQ_SIGNAL(emptyAreaGotFocus()),
		this, TQ_SLOT(emptyAreaGotFocus()));
	connect(m_relationView, TQ_SIGNAL(tableContextMenuRequest( const TQPoint& )),
		this, TQ_SLOT(tableContextMenuRequest( const TQPoint& )));
	connect(m_relationView, TQ_SIGNAL(connectionContextMenuRequest( const TQPoint& )),
		this, TQ_SLOT(connectionContextMenuRequest( const TQPoint& )));
	connect(m_relationView, TQ_SIGNAL(tableHidden(KexiDB::TableSchema&)),
		this, TQ_SLOT(slotTableHidden(KexiDB::TableSchema&)));
	connect(m_relationView, TQ_SIGNAL(tablePositionChanged(KexiRelationViewTableContainer*)),
		this, TQ_SIGNAL(tablePositionChanged(KexiRelationViewTableContainer*)));
	connect(m_relationView, TQ_SIGNAL(aboutConnectionRemove(KexiRelationViewConnection*)),
		this, TQ_SIGNAL(aboutConnectionRemove(KexiRelationViewConnection*)));

#if 0
	if(!embedd)
	{
/*todo		setContextHelp(i18n("Relations"), i18n("To create a relationship simply drag the source field onto the target field. "
			"An arrowhead is used to show which table is the parent (master) and which table is the child (slave) in the relationship."));*/
	}
#endif
//	else
//js: while embedding means read-only?		m_relationView->setReadOnly(true);

#ifdef TESTING_KexiRelationWidget
	for (int i=0;i<(int)m_db->tableNames().count();i++)
		TQTimer::singleShot(100,this,TQ_SLOT(slotAddTable()));
#endif

	invalidateActions();
}

KexiRelationWidget::~KexiRelationWidget()
{
}

TablesDict* KexiRelationWidget::tables() const
{
	return m_relationView->tables();
}

KexiRelationViewTableContainer* KexiRelationWidget::table(const TQString& name) const
{
	return m_relationView->tables()->find( name );
}

const ConnectionList* KexiRelationWidget::connections() const
{ 
	return m_relationView->connections();
}

void
KexiRelationWidget::slotAddTable()
{
	if (m_tableCombo->currentItem()==-1)
		return;
	TQString tname = m_tableCombo->text(m_tableCombo->currentItem());
	KexiDB::TableSchema *t = m_conn->tableSchema(tname);
	addTable(t);
}

void
KexiRelationWidget::addTable(KexiDB::TableSchema *t, const TQRect &rect)
{
	if (!t)
		return;
	if (!m_relationView->tableContainer(t)) {
		KexiRelationViewTableContainer *c = m_relationView->addTableContainer(t, rect);
		kdDebug() << "KexiRelationWidget::slotAddTable(): adding table " << t->name() << endl;
		if (!c)
			return;
		connect(c->tableView(), TQ_SIGNAL(doubleClicked(TQListViewItem*,const TQPoint&,int)),
			this, TQ_SLOT(slotTableFieldDoubleClicked(TQListViewItem*,const TQPoint&,int)));
	}

	const TQString tname = t->name().lower();
	const int count = m_tableCombo->count();
	int i = 0;
	for (; i < count; i++ ) {
		if (m_tableCombo->text(i).lower() == tname )
			break;
	}
	if (i<count) {
		int oi = m_tableCombo->currentItem();
		kdDebug()<<"KexiRelationWidget::slotAddTable(): removing a table from the combo box"<<endl;
		m_tableCombo->removeItem(i);
		if (m_tableCombo->count()>0) {
			if (oi>=m_tableCombo->count()) {
				oi=m_tableCombo->count()-1;
			}
			m_tableCombo->setCurrentItem(oi);
		}
		else {
			m_tableCombo->setEnabled(false);
			m_btnAdd->setEnabled(false);
		}
	}
	emit tableAdded(*t);
}

void
KexiRelationWidget::addConnection(const SourceConnection& conn)
{
	m_relationView->addConnection(conn);
}

void
KexiRelationWidget::addTable(const TQString& t)
{
	for(int i=0; i < m_tableCombo->count(); i++)
	{
		if(m_tableCombo->text(i) == t)
		{
			m_tableCombo->setCurrentItem(i);
			slotAddTable();
		}
	}
}

void KexiRelationWidget::tableViewGotFocus()
{
//	if (m_relationView->focusedTableView == sender())
//		return;
//	kdDebug() << "GOT FOCUS!" <<endl;
//	clearSelection();
//	if (m_focusedTableView)
//		m_focusedTableView->unsetFocus();
//	m_focusedTableView = (KexiRelationViewTableContainer*)sender();
	invalidateActions();
}

void KexiRelationWidget::connectionViewGotFocus()
{
	invalidateActions();
}

void KexiRelationWidget::emptyAreaGotFocus()
{
	invalidateActions();
}

void KexiRelationWidget::tableContextMenuRequest(const TQPoint& pos)
{
	invalidateActions();
	executePopup( pos );
}

void KexiRelationWidget::connectionContextMenuRequest(const TQPoint& pos)
{
	invalidateActions();
	executePopup( pos );
//	m_connectionPopup->exec(pos);
}

void KexiRelationWidget::emptyAreaContextMenuRequest( const TQPoint& /*pos*/ )
{
	invalidateActions();
	//TODO
}

void KexiRelationWidget::invalidateActions()
{
	setAvailable("edit_delete", m_relationView->selectedConnection() || m_relationView->focusedTableView());
}

void KexiRelationWidget::executePopup( TQPoint pos )
{
	if (pos==TQPoint(-1,-1)) {
		pos = mapToGlobal( 
			m_relationView->focusedTableView() ? m_relationView->focusedTableView()->pos() + m_relationView->focusedTableView()->rect().center() : rect().center() );
	}
	if (m_relationView->focusedTableView())
		m_tableQueryPopup->exec(pos);
	else if (m_relationView->selectedConnection())
		m_connectionPopup->exec(pos);
}

void KexiRelationWidget::removeSelectedObject()
{
	m_relationView->removeSelectedObject();
}

void KexiRelationWidget::openSelectedTable()
{
/*! @todo what about query? */
	if (!m_relationView->focusedTableView() || !m_relationView->focusedTableView()->schema()->table())
		return;
	bool openingCancelled;
	m_win->openObject("kexi/table", m_relationView->focusedTableView()->schema()->name(), 
		Kexi::DataViewMode, openingCancelled);
}

void KexiRelationWidget::designSelectedTable()
{
/*! @todo what about query? */
	if (!m_relationView->focusedTableView() || !m_relationView->focusedTableView()->schema()->table())
		return;
	bool openingCancelled;
	m_win->openObject("kexi/table", m_relationView->focusedTableView()->schema()->name(), 
		Kexi::DesignViewMode, openingCancelled);
}

TQSize KexiRelationWidget::sizeHint() const
{
	return m_relationView->sizeHint();
}

void KexiRelationWidget::slotTableHidden(KexiDB::TableSchema &table)
{
	const TQString &t = table.name().lower();
	int i;
	for (i=0; i<m_tableCombo->count() && t > m_tableCombo->text(i).lower(); i++)
		;
	m_tableCombo->insertItem(table.name(), i);
	if (!m_tableCombo->isEnabled()) {
		m_tableCombo->setCurrentItem(0);
		m_tableCombo->setEnabled(true);
		m_btnAdd->setEnabled(true);
	}

	emit tableHidden(table);
}

void KexiRelationWidget::aboutToShowPopupMenu()
{
/*! @todo what about query? */
	if (m_relationView->focusedTableView() && m_relationView->focusedTableView()->schema()->table()) {
		m_tableQueryPopup->changeTitle(m_tableQueryPopupTitleID, SmallIcon("table"),
			TQString(m_relationView->focusedTableView()->schema()->name()) + " : " + i18n("Table"));
	}
	else if (m_relationView->selectedConnection()) {
		m_connectionPopup->changeTitle( m_connectionPopupTitleID, 
			 m_relationView->selectedConnection()->toString() + " : " + i18n("Relationship") );
	}
}

void
KexiRelationWidget::slotTableFieldDoubleClicked(TQListViewItem *i,const TQPoint&,int)
{
	if (!sender()->isA("KexiRelationViewTable"))
		return;
	const KexiRelationViewTable* t = static_cast<const KexiRelationViewTable*>(sender());
	const TQStringList selectedFieldNames( t->selectedFieldNames() );
	if (selectedFieldNames.count()==1)
		emit tableFieldDoubleClicked( t->schema()->table(), selectedFieldNames.first() );
}

void 
KexiRelationWidget::clear()
{
	m_relationView->clear();
	fillTablesCombo();
}

/*! Removes all coonections from the view. */
void KexiRelationWidget::removeAllConnections()
{
	m_relationView->removeAllConnections();
}

void 
KexiRelationWidget::fillTablesCombo()
{
	m_tableCombo->clear();
	TQStringList tmp = m_conn->tableNames();
	tmp.sort();
	m_tableCombo->insertStringList(tmp);
}

void
KexiRelationWidget::objectCreated(const TQCString &mime, const TQCString& name)
{
	if (mime=="kexi/table" || mime=="kexi/query") {
//! @todo query?
		m_tableCombo->insertItem(TQString(name));
		m_tableCombo->listBox()->sort();
	}
}

void
KexiRelationWidget::objectDeleted(const TQCString &mime, const TQCString& name)
{
	if (mime=="kexi/table" || mime=="kexi/query") {
		TQString strName(name);
		for (int i=0; i<m_tableCombo->count(); i++) {
//! @todo query?
			if (m_tableCombo->text(i)==strName) {
				m_tableCombo->removeItem(i);
				if (m_tableCombo->currentItem()==i) {
					if (i==(m_tableCombo->count()-1))
						m_tableCombo->setCurrentItem(i-1);
					else
						m_tableCombo->setCurrentItem(i);
				}
				break;
			}
		}
	}
}

void
KexiRelationWidget::objectRenamed(const TQCString &mime, const TQCString& name, const TQCString& newName)
{
	if (mime=="kexi/table" || mime=="kexi/query") {
		TQString strName(name);
		for (int i=0; i<m_tableCombo->count(); i++) {
//! @todo query?
			if (m_tableCombo->text(i)==strName) {
				m_tableCombo->changeItem(TQString(newName), i);
				m_tableCombo->listBox()->sort();
				break;
			}
		}
	}
}

void
KexiRelationWidget::hideAllTablesExcept( KexiDB::TableSchema::List* tables )
{
	m_relationView->hideAllTablesExcept(tables);
}

#include "kexirelationwidget.moc"