summaryrefslogtreecommitdiffstats
path: root/kexi/formeditor/test/kfd_part.cpp
blob: f309283f0e58cb6c651db52978e55c558e1ccaad (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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/* This file is part of the KDE project
   Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>

   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 <tqworkspace.h>
#include <tqdockarea.h>
#include <tqdockwindow.h>
#include <tqhbox.h>
#include <tqpainter.h>
#include <tqevent.h>
#include <tqobjectlist.h>

#include <kdeversion.h>
#include <kaction.h>
#include <kinstance.h>
#include <klocale.h>
#include <kaboutdata.h>
#include <kdebug.h>
#include <kstdaction.h>
#include <kapplication.h>
#include <kiconloader.h>
#include <kfiledialog.h>
#include <klibloader.h>
#include <kmessagebox.h>

#include "form.h"
#include "formIO.h"
#include "objecttree.h"
#include "container.h"
#include "formmanager.h"
#include "objecttreeview.h"
#include <koproperty/set.h>
#include <koproperty/editor.h>

#include "kfd_part.h"

/*
#define ENABLE_ACTION(name, enable)
	if(actionCollection()->action( name ))
		actionCollection()->action( name )->setEnabled( enable )
*/

class KFDPart_FormManager : public KFormDesigner::FormManager
{
	public:
		/*! Constructs FormManager object.
		 See WidgetLibrary's constructor documentation for information about
		 \a supportedFactoryGroups parameter.
		 Using \a options you can control manager's behaviour, see \ref Options. */
		KFDPart_FormManager(KFormDesignerPart *part, int options = 0, const char *name = 0)
		 : KFormDesigner::FormManager(part, options, name)
		 , m_part(part)
		{
		}

		virtual KAction* action( const char* name)
		{
			return m_part->actionCollection()->action( name );
		}

		virtual void enableAction( const char* name, bool enable ) {
			if(m_part->actionCollection()->action( name ))
				m_part->actionCollection()->action( name )->setEnabled( enable );
		}

		KFormDesignerPart *m_part;
};

KInstance *KFDFactory::m_instance = 0L;

KFDFactory::KFDFactory()
{}

KFDFactory::~KFDFactory()
{
	if (m_instance)
	{
		delete m_instance->aboutData();
		delete m_instance;
	}

	m_instance = 0;
}

KParts::Part*
KFDFactory::createPartObject( TQWidget *parentWidget, const char *, TQObject *, const char *name,
  const char *classname, const TQStringList &args)
{
	bool readOnly = (classname == "KParts::ReadOnlyPart");
	KFormDesignerPart *part = new KFormDesignerPart(parentWidget, name, readOnly, args);
	return part;
}

KInstance*
KFDFactory::instance()
{
	if (!m_instance)
		m_instance = new KInstance(aboutData());
	return m_instance;
}

KAboutData*
KFDFactory::aboutData()
{
	KAboutData *about = new KAboutData("kformdesigner_part", I18N_NOOP("Form Designer Part"), "0.3");
	return about;
}

KFormDesigner::WidgetLibrary* KFormDesignerPart::static_formsLibrary = 0L;

KFormDesignerPart::KFormDesignerPart(TQWidget *parent, const char *name, bool readOnly, const TQStringList &args)
: KParts::ReadWritePart(parent, name), m_count(0)
{
	setInstance(KFDFactory::instance());
	instance()->iconLoader()->addAppDir("kexi");
	instance()->iconLoader()->addAppDir("kformdesigner");

	setReadWrite(!readOnly);
	m_uniqueFormMode = true;
	m_openingFile = false;

	if(!args.grep("multipleMode").isEmpty())
		setUniqueFormMode(false);
	m_inShell = (!args.grep("shell").isEmpty());

	TQHBox *container = new TQHBox(parent, "kfd_container_widget");

	m_workspace = new TQWorkspace(container, "kfd_workspace");
	m_workspace->show();

	TQStringList supportedFactoryGroups;
/* @todo add configuration for supported factory groups */
	static_formsLibrary = KFormDesigner::FormManager::createWidgetLibrary( 
		new KFDPart_FormManager(this, 0, "kfd_manager"), supportedFactoryGroups );

	if(!readOnly)
	{
		TQDockArea *dockArea = new TQDockAreaQt::Vertical, TQDockArea::Reverse, container, "kfd_part_dockarea");

		TQDockWindow *dockTree = new TQDockWindow(dockArea);
		KFormDesigner::ObjectTreeView *view = new KFormDesigner::ObjectTreeView(dockTree);
		dockTree->setWidget(view);
		dockTree->setCaption(i18n("Objects"));
		dockTree->setResizeEnabled(true);
		dockTree->setFixedExtentWidth(256);

		TQDockWindow *dockEditor = new TQDockWindow(dockArea);
		m_editor = new KoProperty::Editor(dockEditor);
		dockEditor->setWidget(m_editor);
		dockEditor->setCaption(i18n("Properties"));
		dockEditor->setResizeEnabled(true);

		KFormDesigner::FormManager::self()->setEditor(m_editor);
		KFormDesigner::FormManager::self()->setObjectTreeView(view);

		setupActions();
		setModified(false);

		// action stuff
//		connect(m_manager, TQT_SIGNAL(widgetSelected(KFormDesigner::Form*, bool)), TQT_SLOT(slotWidgetSelected(KFormDesigner::Form*, bool)));
//		connect(m_manager, TQT_SIGNAL(formWidgetSelected(KFormDesigner::Form*)), TQT_SLOT(slotFormWidgetSelected(KFormDesigner::Form*)));
//		connect(m_manager, TQT_SIGNAL(noFormSelected()), TQT_SLOT(slotNoFormSelected()));
		connect(KFormDesigner::FormManager::self(), TQT_SIGNAL(undoEnabled(bool, const TQString&)), TQT_SLOT(setUndoEnabled(bool, const TQString&)));
		connect(KFormDesigner::FormManager::self(), TQT_SIGNAL(redoEnabled(bool, const TQString&)), TQT_SLOT(setRedoEnabled(bool, const TQString&)));
		connect(KFormDesigner::FormManager::self(), TQT_SIGNAL(dirty(KFormDesigner::Form*, bool)), this, TQT_SLOT(slotFormModified(KFormDesigner::Form*, bool)));
	}

	container->show();
	setWidget(container);
	connect(m_workspace, TQT_SIGNAL(windowActivated(TQWidget*)), KFormDesigner::FormManager::self(), TQT_SLOT(windowChanged(TQWidget*)));
	connect(KFormDesigner::FormManager::self(), TQT_SIGNAL(propertySetSwitched(KoProperty::Set*, bool, const TQCString&)),
		this, TQT_SLOT(slotPropertySetSwitched(KoProperty::Set*, bool, const TQCString&)));

//	slotNoFormSelected();
	KFormDesigner::FormManager::self()->emitNoFormSelected();
}

KFormDesignerPart::~KFormDesignerPart()
{
}

KFormDesigner::WidgetLibrary* KFormDesignerPart::formsLibrary()
{
	return static_formsLibrary;
}

void
KFormDesignerPart::setupActions()
{
	KStdAction::open(this, TQT_SLOT(open()), actionCollection());
	KStdAction::openNew(this, TQT_SLOT(createBlankForm()), actionCollection());
	KStdAction::save(this, TQT_SLOT(save()), actionCollection());
	KStdAction::saveAs(this, TQT_SLOT(saveAs()), actionCollection());
	KStdAction::cut(KFormDesigner::FormManager::self(), TQT_SLOT(cutWidget()), actionCollection());
	KStdAction::copy(KFormDesigner::FormManager::self(), TQT_SLOT(copyWidget()), actionCollection());
	KStdAction::paste(KFormDesigner::FormManager::self(), TQT_SLOT(pasteWidget()), actionCollection());
	KStdAction::undo(KFormDesigner::FormManager::self(), TQT_SLOT(undo()), actionCollection());
	KStdAction::redo(KFormDesigner::FormManager::self(), TQT_SLOT(redo()), actionCollection());
	KStdAction::selectAll(KFormDesigner::FormManager::self(), TQT_SLOT(selectAll()), actionCollection());
	new KAction(i18n("Clear Widget Contents"), "editclear", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(clearWidgetContent()), actionCollection(), "clear_contents");
	new KAction(i18n("Delete Widget"), "editdelete", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(deleteWidget()), actionCollection(), "edit_delete");
	new KAction(i18n("Preview Form"), "filequickprint", CTRL+Key_T, this, TQT_SLOT(slotPreviewForm()), actionCollection(), "preview_form");
	new KAction(i18n("Edit Tab Order"), "tab_order", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(editTabOrder()), actionCollection(), "taborder");
	new KAction(i18n("Edit Pixmap Collection"), "icons", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(editFormPixmapCollection()), actionCollection(), "pixmap_collection");
	new KAction(i18n("Edit Form Connections"), "connections", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(editConnections()), actionCollection(), "form_connections");

	KActionMenu *layoutMenu = new KActionMenu(i18n("Group Widgets"), "", actionCollection(), "layout_menu");
	layoutMenu->insert(new KAction(i18n("&Horizontally"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutHBox()), actionCollection(), "layout_hbox"));
	layoutMenu->insert(new KAction(i18n("&Vertically"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutVBox()), actionCollection(), "layout_vbox"));
	layoutMenu->insert(new KAction(i18n("In &Grid"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutGrid()), actionCollection(), "layout_grid"));
	layoutMenu->insert(new KAction(i18n("By &Rows"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutHFlow()), actionCollection(), "layout_hflow"));
	layoutMenu->insert(new KAction(i18n("By &Columns"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutVFlow()), actionCollection(), "layout_vflow"));
	layoutMenu->insert(new KAction(i18n("Horizontally in &Splitter"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutHSplitter()), actionCollection(), "layout_hsplitter"));
	layoutMenu->insert(new KAction(i18n("Verti&cally in Splitter"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutVSplitter()), actionCollection(), "layout_vsplitter"));
	new KAction(i18n("&Ungroup Widgets"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(breakLayout()), actionCollection(), "break_layout");

/*
	new KAction(i18n("Lay Out Widgets &Horizontally"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutHBox()), actionCollection(), "layout_hbox");
	new KAction(i18n("Lay Out Widgets &Vertically"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutVBox()), actionCollection(), "layout_vbox");
	new KAction(i18n("Lay Out Widgets in &Grid"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutGrid()), actionCollection(), "layout_grid");
	new KAction(i18n("Lay Out Widgets H&orizontally in Splitter"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutHSplitter()), actionCollection(), "layout_hsplitter");
	new KAction(i18n("Lay Out Widgets Verti&cally in Splitter"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutVSplitter()), actionCollection(), "layout_vsplitter");
	new KAction(i18n("&Break Layout"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(breakLayout()), actionCollection(), "break_layout");
*/
	new KAction(i18n("Bring Widget to Front"), "raise", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(bringWidgetToFront()), actionCollection(), "format_raise");
	new KAction(i18n("Send Widget to Back"), "lower", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(sendWidgetToBack()), actionCollection(), "format_lower");

	KActionMenu *alignMenu = new KActionMenu(i18n("Align Widgets' Positions"), "aopos2grid", actionCollection(), "align_menu");
	alignMenu->insert( new KAction(i18n("To Left"), "aoleft", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToLeft()), actionCollection(), "align_to_left") );
	alignMenu->insert( new KAction(i18n("To Right"), "aoright", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToRight()), actionCollection(), "align_to_right") );
	alignMenu->insert( new KAction(i18n("To Top"), "aotop", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToTop()), actionCollection(), "align_to_top") );
	alignMenu->insert( new KAction(i18n("To Bottom"), "aobottom", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToBottom()), actionCollection(), "align_to_bottom") );
	alignMenu->insert( new KAction(i18n("To Grid"), "aopos2grid", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToGrid()), actionCollection(), "align_to_grid") );

	KActionMenu *sizeMenu = new KActionMenu(i18n("Adjust Widgets' Sizes"), "aogrid", actionCollection(), "adjust_size_menu");
	sizeMenu->insert( new KAction(i18n("To Fit"), "aofit", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(adjustWidgetSize()), actionCollection(), "adjust_to_fit") );
	sizeMenu->insert( new KAction(i18n("To Grid"), "aogrid", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(adjustSizeToGrid()), actionCollection(), "adjust_size_grid") );
	sizeMenu->insert( new KAction(i18n("To Shortest"), "aoshortest", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(adjustHeightToSmall()), actionCollection(), "adjust_height_small") );
	sizeMenu->insert( new KAction(i18n("To Tallest"), "aotallest", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(adjustHeightToBig()), actionCollection(), "adjust_height_big") );
	sizeMenu->insert( new KAction(i18n("To Narrowest"), "aonarrowest", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(adjustWidthToSmall()), actionCollection(), "adjust_width_small") );
	sizeMenu->insert( new KAction(i18n("To Widest"), "aowidest", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(adjustWidthToBig()), actionCollection(), "adjust_width_big") );

	if(m_inShell)
		setXMLFile("kformdesigner_part_shell.rc");
	else
		setXMLFile("kformdesigner_part.rc");
	KFormDesigner::FormManager::self()->createActions(formsLibrary(), actionCollection(), this);
}

void
KFormDesignerPart::createBlankForm()
{
	if(KFormDesigner::FormManager::self()->activeForm() && m_uniqueFormMode)
	{
		m_openingFile = true;
		closeURL();
		m_openingFile = false;
	}

	if(m_uniqueFormMode && KFormDesigner::FormManager::self()->activeForm() 
		&& !KFormDesigner::FormManager::self()->activeForm()->isModified() 
		&& KFormDesigner::FormManager::self()->activeForm()->filename().isNull())
		return;  // active form is already a blank one

	TQString n = i18n("Form") + TQString::number(++m_count);
	Form *form = new Form(formsLibrary(), n.latin1(), 
		false/*!designMode, we need to set it early enough*/);
	FormWidgetBase *w = new FormWidgetBase(this, m_workspace, n.latin1());

	w->setCaption(n);
	w->setIcon(SmallIcon("form"));
	w->resize(350, 300);
	w->show();
	w->setFocus();

	form->createToplevel(w, w);
	KFormDesigner::FormManager::self()->importForm(form);
}

void
KFormDesignerPart::open()
{
	m_openingFile = true;
	KURL url = KFileDialog::getOpenURL("::kformdesigner", i18n("*.ui|TQt Designer UI Files"), m_workspace->tqtopLevelWidget());
	if(!url.isEmpty())
		ReadWritePart::openURL(url);
	m_openingFile = false;
}

bool
KFormDesignerPart::openFile()
{
	Form *form = new Form(formsLibrary());
	FormWidgetBase *w = new FormWidgetBase(this, m_workspace);
	form->createToplevel(w, w);

	if(!KFormDesigner::FormIO::loadFormFromFile(form, w, m_file))
	{
		delete form;
		delete w;
		return false;
	}

	w->show();
	KFormDesigner::FormManager::self()->importForm(form, !isReadWrite());
	return true;
}

bool
KFormDesignerPart::saveFile()
{
	KFormDesigner::FormIO::saveFormToFile(KFormDesigner::FormManager::self()->activeForm(), m_file);
	return true;
}

void
KFormDesignerPart::saveAs()
{
	KURL url = KFileDialog::getSaveURL("::kformdesigner", i18n("*.ui|TQt Designer UI Files"), 
		m_workspace->tqtopLevelWidget());
	if(url.isEmpty())
		return;
	else
		ReadWritePart::saveAs(url);
}

bool
KFormDesignerPart::closeForm(Form *form)
{
	int res = KMessageBox::questionYesNoCancel( m_workspace->tqtopLevelWidget(),
		i18n( "The form \"%1\" has been modified.\n"
		"Do you want to save your changes or discard them?" ).tqarg( form->objectTree()->name() ),
		i18n( "Close Form" ), KStdGuiItem::save(), KStdGuiItem::discard() );

	if(res == KMessageBox::Yes)
		save();

	return (res != KMessageBox::Cancel);
}

bool
KFormDesignerPart::closeForms()
{
	TQWidgetList list = m_workspace->windowList(TQWorkspace::CreationOrder);
	for(TQWidget *w = list.first(); w; w = list.next())
		if(w->close() == false)
			return false;

	return true;
}

bool
KFormDesignerPart::closeURL()
{
	if(!KFormDesigner::FormManager::self()->activeForm())
		return true;

	if(m_uniqueFormMode || !m_openingFile) {
		if (!closeForms())
			return false;
	}

	delete (KoProperty::Editor*)m_editor;
	return true;
}

void
KFormDesignerPart::slotFormModified(Form *, bool isDirty)
{
	setModified(isDirty);
}

void
KFormDesignerPart::slotPreviewForm()
{
	if(!KFormDesigner::FormManager::self()->activeForm())
		return;

	FormWidgetBase *w = new FormWidgetBase(this, m_workspace);
	KFormDesigner::FormManager::self()->previewForm(KFormDesigner::FormManager::self()->activeForm(), w);
}

#if 0

void
KFormDesignerPart::slotWidgetSelected(Form *form, bool multiple)
{
	enableFormActions();
	// Enable edit actions
	ENABLE_ACTION("edit_copy", true);
	ENABLE_ACTION("edit_cut", true);
	ENABLE_ACTION("edit_delete", true);
	ENABLE_ACTION("clear_contents", true);

	// 'Align Widgets' menu
	ENABLE_ACTION("align_menu", multiple);
	ENABLE_ACTION("align_to_left", multiple);
	ENABLE_ACTION("align_to_right", multiple);
	ENABLE_ACTION("align_to_top", multiple);
	ENABLE_ACTION("align_to_bottom", multiple);

	ENABLE_ACTION("adjust_size_menu", true);
	ENABLE_ACTION("adjust_width_small", multiple);
	ENABLE_ACTION("adjust_width_big", multiple);
	ENABLE_ACTION("adjust_height_small", multiple);
	ENABLE_ACTION("adjust_height_big", multiple);

	ENABLE_ACTION("format_raise", true);
	ENABLE_ACTION("format_lower", true);

	// If the widgets selected is a container, we enable tqlayout actions
	bool containerSelected = false;
	if(!multiple)
	{
		KFormDesigner::ObjectTreeItem *item = form->objectTree()->lookup( form->selectedWidgets()->first()->name() );
		if(item && item->container())
			containerSelected = true;
	}
	const bool twoSelected = form->selectedWidgets()->count()==2;
	// Layout actions
	ENABLE_ACTION("layout_menu", multiple || containerSelected);
	ENABLE_ACTION("layout_hbox", multiple || containerSelected);
	ENABLE_ACTION("layout_vbox", multiple || containerSelected);
	ENABLE_ACTION("layout_grid", multiple || containerSelected);
	ENABLE_ACTION("layout_hsplitter", twoSelected);
	ENABLE_ACTION("layout_vsplitter", twoSelected);

	KFormDesigner::Container *container = KFormDesigner::FormManager::self()->activeForm()->activeContainer();
	ENABLE_ACTION("break_layout", (container->layoutType() != KFormDesigner::Container::NoLayout));
}

void
KFormDesignerPart::slotFormWidgetSelected(Form *form)
{
	disableWidgetActions();
	enableFormActions();

	const bool twoSelected = form->selectedWidgets()->count()==2;
	const bool hasChildren = !form->objectTree()->tqchildren()->isEmpty();

	// Layout actions
	ENABLE_ACTION("layout_menu", hasChildren);
	ENABLE_ACTION("layout_hbox", hasChildren);
	ENABLE_ACTION("layout_vbox", hasChildren);
	ENABLE_ACTION("layout_grid", hasChildren);
	ENABLE_ACTION("layout_hsplitter", twoSelected);
	ENABLE_ACTION("layout_vsplitter", twoSelected);
	ENABLE_ACTION("break_layout", (form->toplevelContainer()->layoutType() != KFormDesigner::Container::NoLayout));
}

void
KFormDesignerPart::slotNoFormSelected()
{
	disableWidgetActions();

	// Disable paste action
	ENABLE_ACTION("edit_paste", false);

	ENABLE_ACTION("edit_undo", false);
	ENABLE_ACTION("edit_redo", false);

	// Disable 'Tools' actions
	ENABLE_ACTION("pixmap_collection", false);
	ENABLE_ACTION("form_connections", false);
	ENABLE_ACTION("taborder", false);
	ENABLE_ACTION("change_style", KFormDesigner::FormManager::self()->activeForm());

	// Disable items in 'File'
	ENABLE_ACTION("file_save", false);
	ENABLE_ACTION("file_save_as", false);
	ENABLE_ACTION("preview_form", false);
}

void
KFormDesignerPart::enableFormActions()
{
	// Enable 'Tools' actions
	ENABLE_ACTION("pixmap_collection", true);
	ENABLE_ACTION("form_connections", true);
	ENABLE_ACTION("taborder", true);
	ENABLE_ACTION("change_style", true);

	// Enable items in 'File'
	ENABLE_ACTION("file_save", true);
	ENABLE_ACTION("file_save_as", true);
	ENABLE_ACTION("preview_form", true);

	ENABLE_ACTION("edit_paste", KFormDesigner::FormManager::self()->isPasteEnabled());
	ENABLE_ACTION("edit_select_all", true);
}

void
KFormDesignerPart::disableWidgetActions()
{
	// Disable edit actions
	ENABLE_ACTION("edit_copy", false);
	ENABLE_ACTION("edit_cut", false);
	ENABLE_ACTION("edit_delete", false);
	ENABLE_ACTION("clear_contents", false);

	// Disable format functions
	ENABLE_ACTION("align_menu", false);
	ENABLE_ACTION("align_to_left", false);
	ENABLE_ACTION("align_to_right", false);
	ENABLE_ACTION("align_to_top", false);
	ENABLE_ACTION("align_to_bottom", false);
	ENABLE_ACTION("adjust_size_menu", false);
	ENABLE_ACTION("format_raise", false);
	ENABLE_ACTION("format_lower", false);

	ENABLE_ACTION("layout_menu", false);
	ENABLE_ACTION("layout_hbox", false);
	ENABLE_ACTION("layout_vbox", false);
	ENABLE_ACTION("layout_grid", false);
	ENABLE_ACTION("layout_hsplitter", false);
	ENABLE_ACTION("layout_vsplitter", false);
	ENABLE_ACTION("break_layout", false);
}
#endif

void
KFormDesignerPart::setUndoEnabled(bool enabled, const TQString &text)
{
	KAction *undoAction = actionCollection()->action("edit_undo");
	if(undoAction)
	{
		if(!text.isNull())
			undoAction->setText(text);
	}
}

void
KFormDesignerPart::setRedoEnabled(bool enabled, const TQString &text)
{
	KAction *redoAction = actionCollection()->action("edit_redo");
	if(redoAction)
	{
		if(!text.isNull())
			redoAction->setText(text);
	}
}


//////  FormWidgetBase : helper widget to draw rects on top of widgets

//tqrepaint all tqchildren widgets
static void repaintAll(TQWidget *w)
{
	w->tqrepaint();
	TQObjectList *list = w->queryList(TQWIDGET_OBJECT_NAME_STRING);
	TQObjectListIt it(*list);
	for (TQObject *obj; (obj=it.current()); ++it ) {
		static_cast<TQWidget*>(obj)->tqrepaint();
	}
	delete list;
}

void
FormWidgetBase::drawRects(const TQValueList<TQRect> &list, int type)
{
	TQPainter p;
	p.begin(this, true);
	bool unclipped = testWFlags( WPaintUnclipped );
	setWFlags( WPaintUnclipped );

	if (prev_rect.isValid()) {
		//redraw prev. selection's rectangle
		p.drawPixmap( TQPoint(prev_rect.x()-2, prev_rect.y()-2), buffer, TQRect(prev_rect.x()-2, prev_rect.y()-2, prev_rect.width()+4, prev_rect.height()+4));
	}
	p.setBrush(TQBrush::NoBrush);
	if(type == 1) // selection rect
		p.setPen(TQPen(white, 1, TQt::DotLine));
	else if(type == 2) // insert rect
		p.setPen(TQPen(white, 2));
	p.setRasterOp(XorROP);

	prev_rect = TQRect();
	TQValueList<TQRect>::ConstIterator endIt = list.constEnd();
	for(TQValueList<TQRect>::ConstIterator it = list.constBegin(); it != endIt; ++it) {
		p.drawRect(*it);
		prev_rect = prev_rect.unite(*it);
	}

	if (!unclipped)
		clearWFlags( WPaintUnclipped );
	p.end();
}

void
FormWidgetBase::drawRect(const TQRect& r, int type)
{
	TQValueList<TQRect> l;
	l.append(r);
	drawRects(l, type);
}

void
FormWidgetBase::initBuffer()
{
	repaintAll(this);
	buffer.resize( width(), height() );
	buffer = TQPixmap::grabWindow( winId() );
	prev_rect = TQRect();
}

void
FormWidgetBase::clearForm()
{
	TQPainter p;
	p.begin(this, true);
	bool unclipped = testWFlags( WPaintUnclipped );
	setWFlags( WPaintUnclipped );

	//redraw entire form surface
	p.drawPixmap( TQPoint(0,0), buffer, TQRect(0,0,buffer.width(), buffer.height()) );

	if (!unclipped)
		clearWFlags( WPaintUnclipped );
	p.end();

	repaintAll(this);
}

void
FormWidgetBase::highlightWidgets(TQWidget *from, TQWidget *to)//, const TQPoint &point)
{
	TQPoint fromPoint, toPoint;
	if(from && from->parentWidget() && (from != this))
		fromPoint = from->parentWidget()->mapTo(this, from->pos());
	if(to && to->parentWidget() && (to != this))
		toPoint = to->parentWidget()->mapTo(this, to->pos());

	TQPainter p;
	p.begin(this, true);
	bool unclipped = testWFlags( WPaintUnclipped );
	setWFlags( WPaintUnclipped );

	if (prev_rect.isValid()) {
		//redraw prev. selection's rectangle
		p.drawPixmap( TQPoint(prev_rect.x(), prev_rect.y()), buffer, TQRect(prev_rect.x(), prev_rect.y(), prev_rect.width(), prev_rect.height()));
	}

	p.setPen( TQPen(TQt::red, 2) );

	if(to)
	{
		TQPixmap pix1 = TQPixmap::grabWidget(from);
		TQPixmap pix2 = TQPixmap::grabWidget(to);

		if((from != this) && (to != this))
			p.drawLine( from->parentWidget()->mapTo(this, from->tqgeometry().center()), to->parentWidget()->mapTo(this, to->tqgeometry().center()) );

		p.drawPixmap(fromPoint.x(), fromPoint.y(), pix1);
		p.drawPixmap(toPoint.x(), toPoint.y(), pix2);

		if(to == this)
			p.drawRoundRect(2, 2, width()-4, height()-4, 4, 4);
		else
			p.drawRoundRect(toPoint.x(), toPoint.y(), to->width(), to->height(), 5, 5);
	}

	if(from == this)
		p.drawRoundRect(2, 2, width()-4, height()-4, 4, 4);
	else
		p.drawRoundRect(fromPoint.x(),  fromPoint.y(), from->width(), from->height(), 5, 5);

	if((to == this) || (from == this))
		prev_rect = TQRect(0, 0, buffer.width(), buffer.height());
	else if(to)
	{
		prev_rect.setX( (fromPoint.x() < toPoint.x()) ? (fromPoint.x() - 5) : (toPoint.x() - 5) );
		prev_rect.setY( (fromPoint.y() < toPoint.y()) ? (fromPoint.y() - 5) : (toPoint.y() - 5) );
		prev_rect.setRight( (fromPoint.x() < toPoint.x()) ? (toPoint.x() + to->width() + 10) : (fromPoint.x() + from->width() + 10) );
		prev_rect.setBottom( (fromPoint.y() < toPoint.y()) ? (toPoint.y() + to->height() + 10) : (fromPoint.y() + from->height() + 10) ) ;
	}
	else
		prev_rect = TQRect(fromPoint.x()- 5,  fromPoint.y() -5, from->width() + 10, from->height() + 10);

	if (!unclipped)
		clearWFlags( WPaintUnclipped );
	p.end();
}

void
FormWidgetBase::closeEvent(TQCloseEvent *ev)
{
	Form *form = KFormDesigner::FormManager::self()->formForWidget(this);
	if(!form || !form->isModified() || !form->objectTree()) // == preview form
		ev->accept();
	else
	{
		bool close = m_part->closeForm(form);
		if(close)
			ev->accept();
		else
			ev->ignore();
	}
}

void KFormDesignerPart::slotPropertySetSwitched(KoProperty::Set *set, bool forceReload, 
	const TQCString& propertyToSelect)
{
	if (m_editor) {
		if (propertyToSelect.isEmpty() && forceReload)
			m_editor->changeSet(set, propertyToSelect);
		else
			m_editor->changeSet(set);
	}
}

K_EXPORT_COMPONENT_FACTORY(libkformdesigner_part, KFDFactory)

#include "kfd_part.moc"