summaryrefslogtreecommitdiffstats
path: root/kexi/plugins/reports/kexireportview.cpp
blob: 403c0277779656b683e570b069d0e5d2b6427abe (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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* This file is part of the KDE project
   Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
   Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>

   This library 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 library 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 library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kexireportview.h"

#include <kdebug.h>

#include <form.h>
#include <formIO.h>
#include <formmanager.h>
#include <objecttree.h>
#include <widgetpropertyset.h>
#include <container.h>

#include <kexidialogbase.h>
//#include <kexidatasourcewizard.h>
#include <kexidb/fieldlist.h>
#include <kexidb/connection.h>

#include "kexireportform.h"
#include <utils/kexirecordnavigator.h>

#define NO_DSWIZARD

KexiReportScrollView::KexiReportScrollView(TQWidget *tqparent, bool preview)
 : KexiScrollView(tqparent, preview)
{
	if(preview) {
		setRecordNavigatorVisible(true);
		recordNavigator()->setLabelText(i18n("Page:"));
		recordNavigator()->setInsertingButtonVisible(false);
	}
	connect(this, TQT_SIGNAL(resizingStarted()), this, TQT_SLOT(slotResizingStarted()));
}

KexiReportScrollView::~KexiReportScrollView()
{
}

void
KexiReportScrollView::show()
{
	KexiScrollView::show();

	//now get resize mode settings for entire form
	if (m_preview) {
		KexiReportView* fv = dynamic_cast<KexiReportView*>(tqparent());
		int resizeMode = fv ? fv->resizeMode() : KexiReportView::ResizeAuto;
		if (resizeMode == KexiReportView::ResizeAuto)
			setResizePolicy(AutoOneFit);
	}
}

void
KexiReportScrollView::slotResizingStarted()
{
	if(m_form && KFormDesigner::FormManager::self())
		setSnapToGrid(KFormDesigner::FormManager::self()->snapWidgetsToGrid(), m_form->gridSize());
	else
		setSnapToGrid(false);
}

//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

KexiReportView::KexiReportView(KexiMainWindow *win, TQWidget *tqparent, const char *name,
	KexiDB::Connection *conn)
 : KexiViewBase(win, tqparent, name), m_propertySet(0), m_conn(conn)
 , m_resizeMode(KexiReportView::ResizeDefault)
{
	TQHBoxLayout *l = new TQHBoxLayout(this);
	l->setAutoAdd(true);

	m_scrollView = new KexiReportScrollView(this, viewMode()==Kexi::DataViewMode);
	setViewWidget(m_scrollView);
//	m_scrollView->show();

	m_reportform = new KexiReportForm(m_scrollView->viewport(), name/*, conn*/);
//	m_reportform->resize(TQSize(400, 300));
	m_scrollView->setWidget(m_reportform);
	m_scrollView->setResizingEnabled(viewMode()!=Kexi::DataViewMode);

//	initForm();

	if (viewMode()==Kexi::DataViewMode) {
		m_scrollView->viewport()->setPaletteBackgroundColor(m_reportform->palette().active().background());
#if 0
		connect(reportPart()->manager(), TQT_SIGNAL(noFormSelected()), TQT_SLOT(slotNoFormSelected()));
#endif
	}
	else {
		connect(KFormDesigner::FormManager::self(), TQT_SIGNAL(propertySetSwitched(KoProperty::Set *, bool)),
			this, TQT_SLOT(slotPropertySetSwitched(KoProperty::Set *, bool)));
		connect(KFormDesigner::FormManager::self(), TQT_SIGNAL(dirty(KFormDesigner::Form *, bool)),
			this, TQT_SLOT(slotDirty(KFormDesigner::Form *, bool)));

		// action stuff
		/*connect(reportPart()->manager(), TQT_SIGNAL(widgetSelected(KFormDesigner::Form*, bool)), TQT_SLOT(slotWidgetSelected(KFormDesigner::Form*, bool)));
		connect(reportPart()->manager(), TQT_SIGNAL(formWidgetSelected(KFormDesigner::Form*)), TQT_SLOT(slotFormWidgetSelected(KFormDesigner::Form*)));
		connect(reportPart()->manager(), TQT_SIGNAL(undoEnabled(bool, const TQString&)), this, TQT_SLOT(setUndoEnabled(bool)));
		connect(reportPart()->manager(), TQT_SIGNAL(redoEnabled(bool, const TQString&)), this, TQT_SLOT(setRedoEnabled(bool)));*/

		plugSharedAction("edit_copy", KFormDesigner::FormManager::self(), TQT_SLOT(copyWidget()));
		plugSharedAction("edit_cut", KFormDesigner::FormManager::self(), TQT_SLOT(cutWidget()));
		plugSharedAction("edit_paste", KFormDesigner::FormManager::self(), TQT_SLOT(pasteWidget()));
		plugSharedAction("edit_delete", KFormDesigner::FormManager::self(), TQT_SLOT(deleteWidget()));
		plugSharedAction("edit_select_all", KFormDesigner::FormManager::self(), TQT_SLOT(selectAll()));
		plugSharedAction("reportpart_clear_contents", KFormDesigner::FormManager::self(), TQT_SLOT(clearWidgetContent()));
		plugSharedAction("edit_undo", KFormDesigner::FormManager::self(), TQT_SLOT(undo()));
		plugSharedAction("edit_redo", KFormDesigner::FormManager::self(), TQT_SLOT(redo()));

		plugSharedAction("reportpart_format_raise", KFormDesigner::FormManager::self(), TQT_SLOT(bringWidgetToFront()) );
		plugSharedAction("reportpart_format_lower", KFormDesigner::FormManager::self(), TQT_SLOT(sendWidgetToBack()) );

		plugSharedAction("reportpart_align_menu", KFormDesigner::FormManager::self(), 0 );
		plugSharedAction("reportpart_align_to_left", KFormDesigner::FormManager::self(),TQT_SLOT(alignWidgetsToLeft()) );
		plugSharedAction("reportpart_align_to_right", KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToRight()) );
		plugSharedAction("reportpart_align_to_top", KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToTop()) );
		plugSharedAction("reportpart_align_to_bottom", KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToBottom()) );
		plugSharedAction("reportpart_align_to_grid", KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToGrid()) );

		plugSharedAction("reportpart_adjust_size_menu", KFormDesigner::FormManager::self(), 0 );
		plugSharedAction("reportpart_adjust_to_fit", KFormDesigner::FormManager::self(), TQT_SLOT(adjustWidgetSize()) );
		plugSharedAction("reportpart_adjust_size_grid", KFormDesigner::FormManager::self(), TQT_SLOT(adjustSizeToGrid()) );
		plugSharedAction("reportpart_adjust_height_small", KFormDesigner::FormManager::self(),  TQT_SLOT(adjustHeightToSmall()) );
		plugSharedAction("reportpart_adjust_height_big", KFormDesigner::FormManager::self(), TQT_SLOT(adjustHeightToBig()) );
		plugSharedAction("reportpart_adjust_width_small", KFormDesigner::FormManager::self(), TQT_SLOT(adjustWidthToSmall()) );
		plugSharedAction("reportpart_adjust_width_big", KFormDesigner::FormManager::self(), TQT_SLOT(adjustWidthToBig()) );
	}

	initForm();

	connect(this, TQT_SIGNAL(focus(bool)), this, TQT_SLOT(slotFocus(bool)));
	/// @todo skip this if ther're no borders
//	m_reportform->resize( m_reportform->size()+TQSize(m_scrollView->verticalScrollBar()->width(), m_scrollView->horizontalScrollBar()->height()) );
}

KexiReportView::~KexiReportView()
{
	// Important: form window is closed.
	// Set property set to 0 because there is *only one* instance of a property set class
	// in Kexi, so the main window wouldn't know the set in fact has been changed.
	m_propertySet = 0;
	propertySetSwitched();
}

KFormDesigner::Form*
KexiReportView::form() const
{
	if(viewMode()==Kexi::DataViewMode)
		return tempData()->previewForm;
	else
		return tempData()->form;
}

void
KexiReportView::setForm(KFormDesigner::Form *f)
{
	if(viewMode()==Kexi::DataViewMode)
		tempData()->previewForm = f;
	else
		tempData()->form = f;
}

void
KexiReportView::initForm()
{
	setForm( new KFormDesigner::Form(KexiReportPart::library()) );
	form()->createToplevel(m_reportform, m_reportform);

	// Show the form wizard if this is a new Form
//	KexiDB::FieldList *fields = 0;
	if(parentDialog()->id() < 0)
	{
#ifndef NO_DSWIZARD
		KexiDataSourceWizard *w = new KexiDataSourceWizard(mainWin(), (TQWidget*)mainWin(), "datasource_wizard");
		if(!w->exec())
			fields = 0;
		else
			fields = w->fields();
		delete w;
#endif
	}

/*	if(fields)
	{
	 @todo generate a report from a table or a query
		TQDomDocument dom;
		reportPart()->generateForm(fields, dom);
		KFormDesigner::FormIO::loadFormFromDom(form(), m_reportform, dom);
	}
	else*/
		loadForm();

	KFormDesigner::FormManager::self()->importForm(form(), viewMode()==Kexi::DataViewMode);
	m_scrollView->setForm(form());
	m_scrollView->refreshContentsSize();
}

void
KexiReportView::loadForm()
{

//@todo also load m_resizeMode !

	kexipluginsdbg << "KexiReportForm::loadForm() Loading the form with id : " << parentDialog()->id() << endl;
	// If we are previewing the Form, use the tempData instead of the form stored in the db
	if(viewMode()==Kexi::DataViewMode && !tempData()->tempForm.isNull() ) {
		KFormDesigner::FormIO::loadFormFromString(form(), m_reportform, tempData()->tempForm);
		return;
	}

	// normal load
	TQString data;
	loadDataBlock(data);
	KFormDesigner::FormIO::loadFormFromString(form(), m_reportform, data);
}

void
KexiReportView::slotPropertySetSwitched(KoProperty::Set *set, bool forceReload)
{
	m_propertySet = set;
	if (forceReload)
		propertySetReloaded(true/*preservePrevSelection*/);
	else
		propertySetSwitched();
}

tristate
KexiReportView::beforeSwitchTo(int mode, bool &dontStore)
{
	if (mode!=viewMode() && viewMode()!=Kexi::DataViewMode) {
		//remember our pos
		tempData()->scrollViewContentsPos
			= TQPoint(m_scrollView->contentsX(), m_scrollView->contentsY());
	}

	// we don't store on db, but in our TempData
	dontStore = true;
	if(dirty() && (mode == Kexi::DataViewMode) && form()->objectTree())
		KFormDesigner::FormIO::saveFormToString(form(), tempData()->tempForm);

	return true;
}

tristate
KexiReportView::afterSwitchFrom(int mode)
{
	if (mode != 0 && mode != Kexi::DesignViewMode) {
		//preserve contents pos after switching to other view
		m_scrollView->setContentsPos(tempData()->scrollViewContentsPos.x(),
			tempData()->scrollViewContentsPos.y());
	}
//	if (mode == Kexi::DesignViewMode) {
		//m_scrollView->move(0,0);
		//m_scrollView->setContentsPos(0,0);
		//m_scrollView->moveChild(m_reportform, 0, 0);
//	}

	if((mode == Kexi::DesignViewMode) && viewMode()==Kexi::DataViewMode) {
		// The form may have been modified, so we must recreate the preview
		delete m_reportform; // also deletes form()
		m_reportform = new KexiReportForm(m_scrollView->viewport());
		m_scrollView->setWidget(m_reportform);

		initForm();
#if 0
		slotNoFormSelected();
#endif

		//reset position
		m_scrollView->setContentsPos(0,0);
		m_reportform->move(0,0);
	}
	return true;
}

void
KexiReportView::slotDirty(KFormDesigner::Form *dirtyForm, bool isDirty)
{
	if(dirtyForm == form())
		KexiViewBase::setDirty(isDirty);
}

KexiDB::SchemaData*
KexiReportView::storeNewData(const KexiDB::SchemaData& sdata, bool &cancel)
{
	KexiDB::SchemaData *s = KexiViewBase::storeNewData(sdata, cancel);
	kexipluginsdbg << "KexiReportForm::storeNewData(): new id:" << s->id() << endl;

	if (!s || cancel) {
		delete s;
		return 0;
	}
	if (!storeData()) {
		//failure: remove object's schema data to avoid garbage
		m_conn->removeObject( s->id() );
		delete s;
		return 0;
	}
	return s;
}

tristate
KexiReportView::storeData(bool dontAsk)
{
	Q_UNUSED(dontAsk)
	kexipluginsdbg << "KexiReportForm::storeData(): " << parentDialog()->partItem()->name() << " [" << parentDialog()->id() << "]" << endl;
	TQString data;
	KFormDesigner::FormIO::saveFormToString(tempData()->form, data);
	if (!storeDataBlock(data))
		return false;
	tempData()->tempForm = TQString();

	return true;
}

#if 0
/// Action stuff /////////////////
void
KexiReportView::slotWidgetSelected(KFormDesigner::Form *f, bool multiple)
{
	if(f != form())
		return;

	enableFormActions();
	// Enable edit actions
	setAvailable("edit_copy", true);
	setAvailable("edit_cut", true);
	setAvailable("edit_clear", true);

	// 'Align Widgets' menu
	setAvailable("reportpart_align_menu", multiple);
	setAvailable("reportpart_align_to_left", multiple);
	setAvailable("reportpart_align_to_right", multiple);
	setAvailable("reportpart_align_to_top", multiple);
	setAvailable("reportpart_align_to_bottom", multiple);

	setAvailable("reportpart_adjust_size_menu", true);
	setAvailable("reportpart_adjust_width_small", multiple);
	setAvailable("reportpart_adjust_width_big", multiple);
	setAvailable("reportpart_adjust_height_small", multiple);
	setAvailable("reportpart_adjust_height_big", multiple);

	setAvailable("reportpart_format_raise", true);
	setAvailable("reportpart_format_lower", true);
}

void
KexiReportView::slotFormWidgetSelected(KFormDesigner::Form *f)
{
	if(f != form())
		return;

	disableWidgetActions();
	enableFormActions();
}

void
KexiReportView::slotNoFormSelected() // == form in preview mode
{
	disableWidgetActions();

	// Disable paste action
	setAvailable("edit_paste", false);
	setAvailable("edit_undo", false);
	setAvailable("edit_redo", false);
}

void
KexiReportView::enableFormActions()
{
	setAvailable("edit_paste", KFormDesigner::FormManager::self()->isPasteEnabled());
}

void
KexiReportView::disableWidgetActions()
{
	// Disable edit actions
	setAvailable("edit_copy", false);
	setAvailable("edit_cut", false);
	setAvailable("edit_clear", false);

	// Disable format functions
	setAvailable("reportpart_align_menu", false);
	setAvailable("reportpart_align_to_left", false);
	setAvailable("reportpart_align_to_right", false);
	setAvailable("reportpart_align_to_top", false);
	setAvailable("reportpart_align_to_bottom", false);

	setAvailable("reportpart_adjust_size_menu", false);
	setAvailable("reportpart_adjust_width_small", false);
	setAvailable("reportpart_adjust_width_big", false);
	setAvailable("reportpart_adjust_height_small", false);
	setAvailable("reportpart_adjust_height_big", false);

	setAvailable("reportpart_format_raise", false);
	setAvailable("reportpart_format_lower", false);
}

void
KexiReportView::setUndoEnabled(bool enabled)
{
	setAvailable("edit_undo", enabled);
}

void
KexiReportView::setRedoEnabled(bool enabled)
{
	setAvailable("edit_redo", enabled);
}
#endif

TQSize
KexiReportView::preferredSizeHint(const TQSize& otherSize)
{
	return (m_reportform->size()
			+TQSize(m_scrollView->verticalScrollBar()->isVisible() ? m_scrollView->verticalScrollBar()->width()*3/2 : 10,
			 m_scrollView->horizontalScrollBar()->isVisible() ? m_scrollView->horizontalScrollBar()->height()*3/2 : 10))
		.expandedTo( KexiViewBase::preferredSizeHint(otherSize) );
}

void
KexiReportView::resizeEvent( TQResizeEvent *e )
{
	if (viewMode()==Kexi::DataViewMode) {
		m_scrollView->refreshContentsSizeLater(
			e->size().width()!=e->oldSize().width(),
			e->size().height()!=e->oldSize().height()
		);
	}
	KexiViewBase::resizeEvent(e);
	m_scrollView->updateNavPanelGeometry();
}

void
KexiReportView::show()
{
	KexiViewBase::show();

//moved from KexiFormScrollView::show():

	//now get resize mode settings for entire form
	//	if (resizeMode() == KexiFormView::ResizeAuto)
	if (viewMode()==Kexi::DataViewMode) {
		if (resizeMode() == ResizeAuto)
			m_scrollView->setResizePolicy(TQScrollView::AutoOneFit);
	}
}

void
KexiReportView::slotFocus(bool in)
{
	if(in && form() && KFormDesigner::FormManager::self() && KFormDesigner::FormManager::self()->activeForm() != form())
			KFormDesigner::FormManager::self()->windowChanged(form()->widget());//m_dbform);
}


#include "kexireportview.moc"