summaryrefslogtreecommitdiffstats
path: root/kexi/widget/relations/kexirelationviewtable.cpp
blob: b40b976127749f860508404b5cf2852d9dc79093 (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
426
427
428
429
/* This file is part of the KDE project
   Copyright (C) 2002, 2003 Lucijan Busch <lucijan@gmx.at>
   Copyright (C) 2003-2007 Jaroslaw Staniek <js@iidea.pl>

   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 <stdlib.h>

#include <tqheader.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqpushbutton.h>
#include <tqcursor.h>
#include <tqpoint.h>
#include <tqapplication.h>
#include <tqbitmap.h>
#include <tqstyle.h>

#include <kdebug.h>
#include <kiconloader.h>
#include <tdeversion.h>
#include <kconfig.h>
#include <kglobalsettings.h>

#include <kexidb/tableschema.h>
#include <kexidb/utils.h>
#include <kexidragobjects.h>
#include "kexirelationviewtable.h"
#include "kexirelationview.h"

KexiRelationViewTableContainer::KexiRelationViewTableContainer(
	KexiRelationView *parent, KexiDB::TableOrQuerySchema *schema)
 : TQFrame(parent,"KexiRelationViewTableContainer" )
// , m_table(t)
 , m_parent(parent)
//	, m_mousePressed(false)
{

//	setFixedSize(100, 150);
//js:	resize(100, 150);
	//setMouseTracking(true);

	setFrameStyle( TQFrame::WinPanel | TQFrame::Raised );

	TQVBoxLayout *lyr = new TQVBoxLayout(this,4,1); //js: using Q*BoxLayout is a good idea

	m_tableHeader = new KexiRelationViewTableContainerHeader(schema->name(), this);

	m_tableHeader->unsetFocus();
	m_tableHeader->setSizePolicy(TQSizePolicy(TQSizePolicy::Minimum, TQSizePolicy::Fixed));
	lyr->addWidget(m_tableHeader);
	connect(m_tableHeader,TQT_SIGNAL(moved()),this,TQT_SLOT(moved()));
	connect(m_tableHeader, TQT_SIGNAL(endDrag()), this, TQT_SIGNAL(endDrag()));

	m_tableView = new KexiRelationViewTable(schema, parent, this, "KexiRelationViewTable");
	//m_tableHeader->setFocusProxy( m_tableView );
	m_tableView->setSizePolicy(TQSizePolicy(TQSizePolicy::Minimum, TQSizePolicy::Minimum));
	
	m_tableView->setMaximumSize( m_tableView->sizeHint() );
	
//	m_tableView->resize( m_tableView->sizeHint() );
	lyr->addWidget(m_tableView, 0);
	connect(m_tableView, TQT_SIGNAL(tableScrolling()), this, TQT_SLOT(moved()));
	connect(m_tableView, TQT_SIGNAL(contextMenu(KListView*, TQListViewItem*, const TQPoint&)),
		this, TQT_SLOT(slotContextMenu(KListView*, TQListViewItem*, const TQPoint&)));
}

KexiRelationViewTableContainer::~KexiRelationViewTableContainer()
{
}

KexiDB::TableOrQuerySchema* KexiRelationViewTableContainer::schema() const
{
	return m_tableView->schema();
}

void KexiRelationViewTableContainer::slotContextMenu(KListView *, TQListViewItem *, const TQPoint &p)
{
//	m_parent->executePopup(p);
	emit contextMenuRequest( p );
}

void KexiRelationViewTableContainer::moved() {
//	kdDebug()<<"finally emitting moved"<<endl;
	emit moved(this);
}

int KexiRelationViewTableContainer::globalY(const TQString &field)
{
//	kdDebug() << "KexiRelationViewTableContainer::globalY()" << endl;
//	TQPoint o = mapFromGlobal(TQPoint(0, (m_tableView->globalY(field))/*+m_parent->contentsY()*/));

	TQPoint o(0, (m_tableView->globalY(field)) + m_parent->contentsY());
//	kdDebug() << "KexiRelationViewTableContainer::globalY() db2" << endl;
	return m_parent->viewport()->mapFromGlobal(o).y();
}

#if 0//js
TQSize KexiRelationViewTableContainer::sizeHint()
{
#ifdef TQ_WS_WIN
	TQSize s = m_tableView->sizeHint()
		+ TQSize(  2 * 5 , m_tableHeader->height() + 2 * 5 );
#else
	TQSize s = m_tableView->sizeHint();
	s.setWidth(s.width() + 4);
	s.setHeight(m_tableHeader->height() + s.height());
#endif
	return s;
}
#endif

void KexiRelationViewTableContainer::setFocus()
{
	kdDebug() << "SET FOCUS" << endl;
	//select 1st:
	if (m_tableView->firstChild()) {
		if (!m_tableView->selectedItems().first())
			m_tableView->setSelected( m_tableView->firstChild(), true );
	}
	m_tableHeader->setFocus();
	m_tableView->setFocus();
/*	TQPalette p = tqApp->palette();
	p.setColor( TQPalette::Active, TQColorGroup::Highlight, KGlobalSettings::highlightColor() );
	p.setColor( TQPalette::Active, TQColorGroup::HighlightedText, KGlobalSettings::highlightedTextColor() );
	m_tableView->setPalette(p);*/

	raise();
	repaint();
	emit gotFocus();
}

void KexiRelationViewTableContainer::unsetFocus()
{
	kdDebug() << "UNSET FOCUS" << endl;
//	if (m_tableView->selectedItem()) //unselect item if was selected
//		m_tableView->setSelected(m_tableView->selectedItem(), false);
//	m_tableView->clearSelection();
	m_tableHeader->unsetFocus();

	m_tableView->clearSelection();

//	m_tableView->unsetPalette();
/*	TQPalette p = m_tableView->palette();
//	p.setColor( TQPalette::Active, TQColorGroup::Highlight, KGlobalSettings::highlightColor() );
//	p.setColor( TQPalette::Active, TQColorGroup::HighlightedText, KGlobalSettings::highlightedTextColor() );
	p.setColor( TQPalette::Active, TQColorGroup::Highlight, p.color(TQPalette::Active, TQColorGroup::Background ) );
//	p.setColor( TQPalette::Active, TQColorGroup::Highlight, gray );
	p.setColor( TQPalette::Active, TQColorGroup::HighlightedText, p.color(TQPalette::Active, TQColorGroup::Foreground ) );
//	p.setColor( TQPalette::Active, TQColorGroup::Highlight, green );
//	p.setColor( TQPalette::Active, TQColorGroup::HighlightedText, blue );
	m_tableView->setPalette(p);*/
	
	clearFocus();
	repaint();
}


//END KexiRelationViewTableContainer

//============================================================================
//BEGIN KexiRelatoinViewTableContainerHeader

KexiRelationViewTableContainerHeader::KexiRelationViewTableContainerHeader(
	const TQString& text,TQWidget *parent)
	:TQLabel(text,parent),m_dragging(false) 
{
	setMargin(1);
	m_activeBG = KGlobalSettings::activeTitleColor();
	m_activeFG = KGlobalSettings::activeTextColor();
	m_inactiveBG = KGlobalSettings::inactiveTitleColor();
	m_inactiveFG = KGlobalSettings::inactiveTextColor();

	installEventFilter(this);
}

KexiRelationViewTableContainerHeader::~KexiRelationViewTableContainerHeader() 
{
}

void KexiRelationViewTableContainerHeader::setFocus()
{
	setPaletteBackgroundColor(m_activeBG);
	setPaletteForegroundColor(m_activeFG);
}

void KexiRelationViewTableContainerHeader::unsetFocus()
{
	setPaletteBackgroundColor(m_inactiveBG);
	setPaletteForegroundColor(m_inactiveFG);
}

bool KexiRelationViewTableContainerHeader::eventFilter(TQObject *, TQEvent *ev) 
{
	if (ev->type()==TQEvent::MouseMove)
	{
		if (m_dragging && TQT_TQMOUSEEVENT(ev)->state()==Qt::LeftButton) {
			int diffX,diffY;
			diffX=TQT_TQMOUSEEVENT(ev)->globalPos().x()-m_grabX;
			diffY=TQT_TQMOUSEEVENT(ev)->globalPos().y()-m_grabY;
			if ((abs(diffX)>2) || (abs(diffY)>2))
			{
				TQPoint newPos=parentWidget()->pos()+TQPoint(diffX,diffY);
//correct the x position
				if (newPos.x()<0) {
					m_offsetX+=newPos.x();
					newPos.setX(0);
				}
				else if (m_offsetX<0) {
					m_offsetX+=newPos.x();
					if (m_offsetX>0) {
						newPos.setX(m_offsetX);
						m_offsetX=0;
					}
					else newPos.setX(0);
				}
//correct the y position
				if (newPos.y()<0) {
					m_offsetY+=newPos.y();
					newPos.setY(0);
				}
				else
				if (m_offsetY<0) {
					m_offsetY+=newPos.y();
					if (m_offsetY>0) {
						newPos.setY(m_offsetY);
						m_offsetY=0;
					}
					else newPos.setY(0);
				}
//move and update helpers

				parentWidget()->move(newPos);
				m_grabX=TQT_TQMOUSEEVENT(ev)->globalPos().x();
				m_grabY=TQT_TQMOUSEEVENT(ev)->globalPos().y();
//				kdDebug()<<"HEADER:emitting moved"<<endl;
				emit moved();
			}
			return true;
		}
	}
	return false;
}

void KexiRelationViewTableContainerHeader::mousePressEvent(TQMouseEvent *ev) {
	kdDebug()<<"KexiRelationViewTableContainerHeader::Mouse Press Event"<<endl;
	parentWidget()->setFocus();
	ev->accept();
	if (ev->button()==Qt::LeftButton) {
		m_dragging=true;
		m_grabX=ev->globalPos().x();
		m_grabY=ev->globalPos().y();
		m_offsetX=0;
		m_offsetY=0;
		setCursor(TQt::SizeAllCursor);
		return;
	}
	if (ev->button()==Qt::RightButton) {
		emit static_cast<KexiRelationViewTableContainer*>(parentWidget())
			->contextMenuRequest(ev->globalPos());
	}
//	TQLabel::mousePressEvent(ev);
}

void KexiRelationViewTableContainerHeader::mouseReleaseEvent(TQMouseEvent *ev) {
	kdDebug()<<"KexiRelationViewTableContainerHeader::Mouse Release Event"<<endl;
	if (m_dragging && ev->button() & Qt::LeftButton) {
		setCursor(TQt::ArrowCursor);
		m_dragging=false;
		emit endDrag();
	}
	ev->accept();
}

//END KexiRelatoinViewTableContainerHeader


//=====================================================================================

KexiRelationViewTable::KexiRelationViewTable(KexiDB::TableOrQuerySchema* tableOrQuerySchema, 
	KexiRelationView *view, TQWidget *parent, const char *name)
 : KexiFieldListView(parent, name, KexiFieldListView::ShowAsterisk)
 , m_view(view)
{
	setSchema(tableOrQuerySchema);
	header()->hide();

	connect(this, TQT_SIGNAL(dropped(TQDropEvent *, TQListViewItem *)), this, TQT_SLOT(slotDropped(TQDropEvent *)));
	connect(this, TQT_SIGNAL(contentsMoving(int, int)), this, TQT_SLOT(slotContentsMoving(int,int)));
}

KexiRelationViewTable::~KexiRelationViewTable()
{
}

TQSize KexiRelationViewTable::sizeHint() const
{
	TQFontMetrics fm(fontMetrics());

//	kdDebug() << schema()->name() << " cw=" << columnWidth(0) + fm.width("i") 
//		<< ", " << fm.width(schema()->name()+"  ") << endl; 

	int maxWidth = -1;
	const int iconWidth = IconSize(KIcon::Small) + fm.width("i")+20;
	for (TQListViewItem *item = firstChild(); item; item	= item->nextSibling())
		maxWidth = TQMAX(maxWidth, iconWidth + fm.width(item->text(0)));

	const uint rowCount = TQMIN( 8, childCount() );

	TQSize s(
		TQMAX( maxWidth, fm.width(schema()->name()+" ")), 
		rowCount*firstChild()->totalHeight() + 4 );
	return s;
}

#if 0
void KexiRelationViewTable::setReadOnly(bool b)
{
	setAcceptDrops(!b);
	viewport()->setAcceptDrops(!b);
}
#endif

int
KexiRelationViewTable::globalY(const TQString &item)
{
	TQListViewItem *i = findItem(item, 0);
	if (!i)
		return -1;
	int y = itemRect(i).y() + (itemRect(i).height() / 2);
	if (contentsY() > itemPos(i))
		y = 0;
	else if (y == 0)
		y = height();
	return mapToGlobal(TQPoint(0, y)).y();
}

bool
KexiRelationViewTable::acceptDrag(TQDropEvent *ev) const
{
//	kdDebug() << "KexiRelationViewTable::acceptDrag()" << endl;
	TQListViewItem *receiver = itemAt(ev->pos() - TQPoint(0,contentsY()));
	if (!receiver || !KexiFieldDrag::canDecodeSingle(ev))
		return false;
	TQString sourceMimeType;
	TQString srcTable;
	TQString srcField;
	if (!KexiFieldDrag::decodeSingle(ev,sourceMimeType,srcTable,srcField))
		return false;
	if (sourceMimeType!="kexi/table" && sourceMimeType=="kexi/query")
		return false;
	TQString f = receiver->text(0).stripWhiteSpace();
	if (!srcField.stripWhiteSpace().startsWith("*") && !f.startsWith("*") && ev->source() != (TQWidget*)this)
		return true;

	return false;
}

void
KexiRelationViewTable::slotDropped(TQDropEvent *ev)
{
	TQListViewItem *recever = itemAt(ev->pos() - TQPoint(0,contentsY()));
	if (!recever || !KexiFieldDrag::canDecodeSingle(ev)) {
		ev->ignore();
		return;
	}
	TQString sourceMimeType;
	TQString srcTable;
	TQString srcField;
	if (!KexiFieldDrag::decodeSingle(ev,sourceMimeType,srcTable,srcField))
		return;
	if (sourceMimeType!="kexi/table" && sourceMimeType=="kexi/query")
		return;
//		kdDebug() << "KexiRelationViewTable::slotDropped() srcfield: " << srcField << endl;

	TQString rcvField = recever->text(0);

	SourceConnection s;
	s.masterTable = srcTable;
	s.detailsTable = schema()->name();
	s.masterField = srcField;
	s.detailsField = rcvField;

	m_view->addConnection(s);

	kdDebug() << "KexiRelationViewTable::slotDropped() " << srcTable << ":" << srcField << " " 
		<< schema()->name() << ":" << rcvField << endl;
	ev->accept();
}

void
KexiRelationViewTable::slotContentsMoving(int,int)
{
	emit tableScrolling();
}

void KexiRelationViewTable::contentsMousePressEvent(TQMouseEvent *ev)
{
	parentWidget()->setFocus();
	setFocus();
	KListView::contentsMousePressEvent(ev);
//	if (ev->button()==Qt::RightButton)
//		static_cast<KexiRelationView*>(parentWidget())->executePopup(ev->pos());
}

TQRect KexiRelationViewTable::drawItemHighlighter(TQPainter *painter, TQListViewItem *item)
{
	if (painter) {
		tqstyle().tqdrawPrimitive(TQStyle::PE_FocusRect, painter, itemRect(item), colorGroup(),
			TQStyle::Style_FocusAtBorder);
	}
	return itemRect(item);
}

#include "kexirelationviewtable.moc"