KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

730 lines
26KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. * Boston, MA 02110-1301, USA.
  15. */
  16. #include <tqworkspace.h>
  17. #include <tqdockarea.h>
  18. #include <tqdockwindow.h>
  19. #include <tqhbox.h>
  20. #include <tqpainter.h>
  21. #include <tqevent.h>
  22. #include <tqobjectlist.h>
  23. #include <tdeversion.h>
  24. #include <kaction.h>
  25. #include <kinstance.h>
  26. #include <klocale.h>
  27. #include <kaboutdata.h>
  28. #include <kdebug.h>
  29. #include <kstdaction.h>
  30. #include <kapplication.h>
  31. #include <kiconloader.h>
  32. #include <tdefiledialog.h>
  33. #include <klibloader.h>
  34. #include <kmessagebox.h>
  35. #include "form.h"
  36. #include "formIO.h"
  37. #include "objecttree.h"
  38. #include "container.h"
  39. #include "formmanager.h"
  40. #include "objecttreeview.h"
  41. #include <koproperty/set.h>
  42. #include <koproperty/editor.h>
  43. #include "kfd_part.h"
  44. /*
  45. #define ENABLE_ACTION(name, enable)
  46. if(actionCollection()->action( name ))
  47. actionCollection()->action( name )->setEnabled( enable )
  48. */
  49. class KFDPart_FormManager : public KFormDesigner::FormManager
  50. {
  51. public:
  52. /*! Constructs FormManager object.
  53. See WidgetLibrary's constructor documentation for information about
  54. \a supportedFactoryGroups parameter.
  55. Using \a options you can control manager's behaviour, see \ref Options. */
  56. KFDPart_FormManager(KFormDesignerPart *part, int options = 0, const char *name = 0)
  57. : KFormDesigner::FormManager(part, options, name)
  58. , m_part(part)
  59. {
  60. }
  61. virtual KAction* action( const char* name)
  62. {
  63. return m_part->actionCollection()->action( name );
  64. }
  65. virtual void enableAction( const char* name, bool enable ) {
  66. if(m_part->actionCollection()->action( name ))
  67. m_part->actionCollection()->action( name )->setEnabled( enable );
  68. }
  69. KFormDesignerPart *m_part;
  70. };
  71. TDEInstance *KFDFactory::m_instance = 0L;
  72. KFDFactory::KFDFactory()
  73. {}
  74. KFDFactory::~KFDFactory()
  75. {
  76. if (m_instance)
  77. {
  78. delete m_instance->aboutData();
  79. delete m_instance;
  80. }
  81. m_instance = 0;
  82. }
  83. KParts::Part*
  84. KFDFactory::createPartObject( TQWidget *parentWidget, const char *, TQObject *, const char *name,
  85. const char *classname, const TQStringList &args)
  86. {
  87. bool readOnly = (classname == "KParts::ReadOnlyPart");
  88. KFormDesignerPart *part = new KFormDesignerPart(parentWidget, name, readOnly, args);
  89. return part;
  90. }
  91. TDEInstance*
  92. KFDFactory::instance()
  93. {
  94. if (!m_instance)
  95. m_instance = new TDEInstance(aboutData());
  96. return m_instance;
  97. }
  98. TDEAboutData*
  99. KFDFactory::aboutData()
  100. {
  101. TDEAboutData *about = new TDEAboutData("kformdesigner_part", I18N_NOOP("Form Designer Part"), "0.3");
  102. return about;
  103. }
  104. KFormDesigner::WidgetLibrary* KFormDesignerPart::static_formsLibrary = 0L;
  105. KFormDesignerPart::KFormDesignerPart(TQWidget *parent, const char *name, bool readOnly, const TQStringList &args)
  106. : KParts::ReadWritePart(parent, name), m_count(0)
  107. {
  108. setInstance(KFDFactory::instance());
  109. instance()->iconLoader()->addAppDir("kexi");
  110. instance()->iconLoader()->addAppDir("kformdesigner");
  111. setReadWrite(!readOnly);
  112. m_uniqueFormMode = true;
  113. m_openingFile = false;
  114. if(!args.grep("multipleMode").isEmpty())
  115. setUniqueFormMode(false);
  116. m_inShell = (!args.grep("shell").isEmpty());
  117. TQHBox *container = new TQHBox(parent, "kfd_container_widget");
  118. m_workspace = new TQWorkspace(container, "kfd_workspace");
  119. m_workspace->show();
  120. TQStringList supportedFactoryGroups;
  121. /* @todo add configuration for supported factory groups */
  122. static_formsLibrary = KFormDesigner::FormManager::createWidgetLibrary(
  123. new KFDPart_FormManager(this, 0, "kfd_manager"), supportedFactoryGroups );
  124. if(!readOnly)
  125. {
  126. TQDockArea *dockArea = new TQDockAreaQt::Vertical, TQDockArea::Reverse, container, "kfd_part_dockarea");
  127. TQDockWindow *dockTree = new TQDockWindow(dockArea);
  128. KFormDesigner::ObjectTreeView *view = new KFormDesigner::ObjectTreeView(dockTree);
  129. dockTree->setWidget(view);
  130. dockTree->setCaption(i18n("Objects"));
  131. dockTree->setResizeEnabled(true);
  132. dockTree->setFixedExtentWidth(256);
  133. TQDockWindow *dockEditor = new TQDockWindow(dockArea);
  134. m_editor = new KoProperty::Editor(dockEditor);
  135. dockEditor->setWidget(m_editor);
  136. dockEditor->setCaption(i18n("Properties"));
  137. dockEditor->setResizeEnabled(true);
  138. KFormDesigner::FormManager::self()->setEditor(m_editor);
  139. KFormDesigner::FormManager::self()->setObjectTreeView(view);
  140. setupActions();
  141. setModified(false);
  142. // action stuff
  143. // connect(m_manager, TQT_SIGNAL(widgetSelected(KFormDesigner::Form*, bool)), TQT_SLOT(slotWidgetSelected(KFormDesigner::Form*, bool)));
  144. // connect(m_manager, TQT_SIGNAL(formWidgetSelected(KFormDesigner::Form*)), TQT_SLOT(slotFormWidgetSelected(KFormDesigner::Form*)));
  145. // connect(m_manager, TQT_SIGNAL(noFormSelected()), TQT_SLOT(slotNoFormSelected()));
  146. connect(KFormDesigner::FormManager::self(), TQT_SIGNAL(undoEnabled(bool, const TQString&)), TQT_SLOT(setUndoEnabled(bool, const TQString&)));
  147. connect(KFormDesigner::FormManager::self(), TQT_SIGNAL(redoEnabled(bool, const TQString&)), TQT_SLOT(setRedoEnabled(bool, const TQString&)));
  148. connect(KFormDesigner::FormManager::self(), TQT_SIGNAL(dirty(KFormDesigner::Form*, bool)), this, TQT_SLOT(slotFormModified(KFormDesigner::Form*, bool)));
  149. }
  150. container->show();
  151. setWidget(container);
  152. connect(m_workspace, TQT_SIGNAL(windowActivated(TQWidget*)), KFormDesigner::FormManager::self(), TQT_SLOT(windowChanged(TQWidget*)));
  153. connect(KFormDesigner::FormManager::self(), TQT_SIGNAL(propertySetSwitched(KoProperty::Set*, bool, const TQCString&)),
  154. this, TQT_SLOT(slotPropertySetSwitched(KoProperty::Set*, bool, const TQCString&)));
  155. // slotNoFormSelected();
  156. KFormDesigner::FormManager::self()->emitNoFormSelected();
  157. }
  158. KFormDesignerPart::~KFormDesignerPart()
  159. {
  160. }
  161. KFormDesigner::WidgetLibrary* KFormDesignerPart::formsLibrary()
  162. {
  163. return static_formsLibrary;
  164. }
  165. void
  166. KFormDesignerPart::setupActions()
  167. {
  168. KStdAction::open(this, TQT_SLOT(open()), actionCollection());
  169. KStdAction::openNew(this, TQT_SLOT(createBlankForm()), actionCollection());
  170. KStdAction::save(this, TQT_SLOT(save()), actionCollection());
  171. KStdAction::saveAs(this, TQT_SLOT(saveAs()), actionCollection());
  172. KStdAction::cut(KFormDesigner::FormManager::self(), TQT_SLOT(cutWidget()), actionCollection());
  173. KStdAction::copy(KFormDesigner::FormManager::self(), TQT_SLOT(copyWidget()), actionCollection());
  174. KStdAction::paste(KFormDesigner::FormManager::self(), TQT_SLOT(pasteWidget()), actionCollection());
  175. KStdAction::undo(KFormDesigner::FormManager::self(), TQT_SLOT(undo()), actionCollection());
  176. KStdAction::redo(KFormDesigner::FormManager::self(), TQT_SLOT(redo()), actionCollection());
  177. KStdAction::selectAll(KFormDesigner::FormManager::self(), TQT_SLOT(selectAll()), actionCollection());
  178. new KAction(i18n("Clear Widget Contents"), "editclear", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(clearWidgetContent()), actionCollection(), "clear_contents");
  179. new KAction(i18n("Delete Widget"), "editdelete", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(deleteWidget()), actionCollection(), "edit_delete");
  180. new KAction(i18n("Preview Form"), "filequickprint", CTRL+Key_T, this, TQT_SLOT(slotPreviewForm()), actionCollection(), "preview_form");
  181. new KAction(i18n("Edit Tab Order"), "tab_order", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(editTabOrder()), actionCollection(), "taborder");
  182. new KAction(i18n("Edit Pixmap Collection"), "icons", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(editFormPixmapCollection()), actionCollection(), "pixmap_collection");
  183. new KAction(i18n("Edit Form Connections"), "connections", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(editConnections()), actionCollection(), "form_connections");
  184. KActionMenu *layoutMenu = new KActionMenu(i18n("Group Widgets"), "", actionCollection(), "layout_menu");
  185. layoutMenu->insert(new KAction(i18n("&Horizontally"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutHBox()), actionCollection(), "layout_hbox"));
  186. layoutMenu->insert(new KAction(i18n("&Vertically"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutVBox()), actionCollection(), "layout_vbox"));
  187. layoutMenu->insert(new KAction(i18n("In &Grid"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutGrid()), actionCollection(), "layout_grid"));
  188. layoutMenu->insert(new KAction(i18n("By &Rows"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutHFlow()), actionCollection(), "layout_hflow"));
  189. layoutMenu->insert(new KAction(i18n("By &Columns"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutVFlow()), actionCollection(), "layout_vflow"));
  190. layoutMenu->insert(new KAction(i18n("Horizontally in &Splitter"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutHSplitter()), actionCollection(), "layout_hsplitter"));
  191. layoutMenu->insert(new KAction(i18n("Verti&cally in Splitter"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutVSplitter()), actionCollection(), "layout_vsplitter"));
  192. new KAction(i18n("&Ungroup Widgets"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(breakLayout()), actionCollection(), "break_layout");
  193. /*
  194. new KAction(i18n("Lay Out Widgets &Horizontally"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutHBox()), actionCollection(), "layout_hbox");
  195. new KAction(i18n("Lay Out Widgets &Vertically"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutVBox()), actionCollection(), "layout_vbox");
  196. new KAction(i18n("Lay Out Widgets in &Grid"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutGrid()), actionCollection(), "layout_grid");
  197. new KAction(i18n("Lay Out Widgets H&orizontally in Splitter"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutHSplitter()), actionCollection(), "layout_hsplitter");
  198. new KAction(i18n("Lay Out Widgets Verti&cally in Splitter"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(layoutVSplitter()), actionCollection(), "layout_vsplitter");
  199. new KAction(i18n("&Break Layout"), TQString(), KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(breakLayout()), actionCollection(), "break_layout");
  200. */
  201. new KAction(i18n("Bring Widget to Front"), "raise", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(bringWidgetToFront()), actionCollection(), "format_raise");
  202. new KAction(i18n("Send Widget to Back"), "lower", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(sendWidgetToBack()), actionCollection(), "format_lower");
  203. KActionMenu *alignMenu = new KActionMenu(i18n("Align Widgets' Positions"), "aopos2grid", actionCollection(), "align_menu");
  204. alignMenu->insert( new KAction(i18n("To Left"), "aoleft", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToLeft()), actionCollection(), "align_to_left") );
  205. alignMenu->insert( new KAction(i18n("To Right"), "aoright", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToRight()), actionCollection(), "align_to_right") );
  206. alignMenu->insert( new KAction(i18n("To Top"), "aotop", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToTop()), actionCollection(), "align_to_top") );
  207. alignMenu->insert( new KAction(i18n("To Bottom"), "aobottom", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToBottom()), actionCollection(), "align_to_bottom") );
  208. alignMenu->insert( new KAction(i18n("To Grid"), "aopos2grid", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(alignWidgetsToGrid()), actionCollection(), "align_to_grid") );
  209. KActionMenu *sizeMenu = new KActionMenu(i18n("Adjust Widgets' Sizes"), "aogrid", actionCollection(), "adjust_size_menu");
  210. sizeMenu->insert( new KAction(i18n("To Fit"), "aofit", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(adjustWidgetSize()), actionCollection(), "adjust_to_fit") );
  211. sizeMenu->insert( new KAction(i18n("To Grid"), "aogrid", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(adjustSizeToGrid()), actionCollection(), "adjust_size_grid") );
  212. sizeMenu->insert( new KAction(i18n("To Shortest"), "aoshortest", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(adjustHeightToSmall()), actionCollection(), "adjust_height_small") );
  213. sizeMenu->insert( new KAction(i18n("To Tallest"), "aotallest", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(adjustHeightToBig()), actionCollection(), "adjust_height_big") );
  214. sizeMenu->insert( new KAction(i18n("To Narrowest"), "aonarrowest", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(adjustWidthToSmall()), actionCollection(), "adjust_width_small") );
  215. sizeMenu->insert( new KAction(i18n("To Widest"), "aowidest", KShortcut(0), KFormDesigner::FormManager::self(), TQT_SLOT(adjustWidthToBig()), actionCollection(), "adjust_width_big") );
  216. if(m_inShell)
  217. setXMLFile("kformdesigner_part_shell.rc");
  218. else
  219. setXMLFile("kformdesigner_part.rc");
  220. KFormDesigner::FormManager::self()->createActions(formsLibrary(), actionCollection(), this);
  221. }
  222. void
  223. KFormDesignerPart::createBlankForm()
  224. {
  225. if(KFormDesigner::FormManager::self()->activeForm() && m_uniqueFormMode)
  226. {
  227. m_openingFile = true;
  228. closeURL();
  229. m_openingFile = false;
  230. }
  231. if(m_uniqueFormMode && KFormDesigner::FormManager::self()->activeForm()
  232. && !KFormDesigner::FormManager::self()->activeForm()->isModified()
  233. && KFormDesigner::FormManager::self()->activeForm()->filename().isNull())
  234. return; // active form is already a blank one
  235. TQString n = i18n("Form") + TQString::number(++m_count);
  236. Form *form = new Form(formsLibrary(), n.latin1(),
  237. false/*!designMode, we need to set it early enough*/);
  238. FormWidgetBase *w = new FormWidgetBase(this, m_workspace, n.latin1());
  239. w->setCaption(n);
  240. w->setIcon(SmallIcon("form"));
  241. w->resize(350, 300);
  242. w->show();
  243. w->setFocus();
  244. form->createToplevel(w, w);
  245. KFormDesigner::FormManager::self()->importForm(form);
  246. }
  247. void
  248. KFormDesignerPart::open()
  249. {
  250. m_openingFile = true;
  251. KURL url = KFileDialog::getOpenURL("::kformdesigner", i18n("*.ui|TQt Designer UI Files"), m_workspace->topLevelWidget());
  252. if(!url.isEmpty())
  253. ReadWritePart::openURL(url);
  254. m_openingFile = false;
  255. }
  256. bool
  257. KFormDesignerPart::openFile()
  258. {
  259. Form *form = new Form(formsLibrary());
  260. FormWidgetBase *w = new FormWidgetBase(this, m_workspace);
  261. form->createToplevel(w, w);
  262. if(!KFormDesigner::FormIO::loadFormFromFile(form, w, m_file))
  263. {
  264. delete form;
  265. delete w;
  266. return false;
  267. }
  268. w->show();
  269. KFormDesigner::FormManager::self()->importForm(form, !isReadWrite());
  270. return true;
  271. }
  272. bool
  273. KFormDesignerPart::saveFile()
  274. {
  275. KFormDesigner::FormIO::saveFormToFile(KFormDesigner::FormManager::self()->activeForm(), m_file);
  276. return true;
  277. }
  278. void
  279. KFormDesignerPart::saveAs()
  280. {
  281. KURL url = KFileDialog::getSaveURL("::kformdesigner", i18n("*.ui|TQt Designer UI Files"),
  282. m_workspace->topLevelWidget());
  283. if(url.isEmpty())
  284. return;
  285. else
  286. ReadWritePart::saveAs(url);
  287. }
  288. bool
  289. KFormDesignerPart::closeForm(Form *form)
  290. {
  291. int res = KMessageBox::questionYesNoCancel( m_workspace->topLevelWidget(),
  292. i18n( "The form \"%1\" has been modified.\n"
  293. "Do you want to save your changes or discard them?" ).arg( form->objectTree()->name() ),
  294. i18n( "Close Form" ), KStdGuiItem::save(), KStdGuiItem::discard() );
  295. if(res == KMessageBox::Yes)
  296. save();
  297. return (res != KMessageBox::Cancel);
  298. }
  299. bool
  300. KFormDesignerPart::closeForms()
  301. {
  302. TQWidgetList list = m_workspace->windowList(TQWorkspace::CreationOrder);
  303. for(TQWidget *w = list.first(); w; w = list.next())
  304. if(w->close() == false)
  305. return false;
  306. return true;
  307. }
  308. bool
  309. KFormDesignerPart::closeURL()
  310. {
  311. if(!KFormDesigner::FormManager::self()->activeForm())
  312. return true;
  313. if(m_uniqueFormMode || !m_openingFile) {
  314. if (!closeForms())
  315. return false;
  316. }
  317. delete (KoProperty::Editor*)m_editor;
  318. return true;
  319. }
  320. void
  321. KFormDesignerPart::slotFormModified(Form *, bool isDirty)
  322. {
  323. setModified(isDirty);
  324. }
  325. void
  326. KFormDesignerPart::slotPreviewForm()
  327. {
  328. if(!KFormDesigner::FormManager::self()->activeForm())
  329. return;
  330. FormWidgetBase *w = new FormWidgetBase(this, m_workspace);
  331. KFormDesigner::FormManager::self()->previewForm(KFormDesigner::FormManager::self()->activeForm(), w);
  332. }
  333. #if 0
  334. void
  335. KFormDesignerPart::slotWidgetSelected(Form *form, bool multiple)
  336. {
  337. enableFormActions();
  338. // Enable edit actions
  339. ENABLE_ACTION("edit_copy", true);
  340. ENABLE_ACTION("edit_cut", true);
  341. ENABLE_ACTION("edit_delete", true);
  342. ENABLE_ACTION("clear_contents", true);
  343. // 'Align Widgets' menu
  344. ENABLE_ACTION("align_menu", multiple);
  345. ENABLE_ACTION("align_to_left", multiple);
  346. ENABLE_ACTION("align_to_right", multiple);
  347. ENABLE_ACTION("align_to_top", multiple);
  348. ENABLE_ACTION("align_to_bottom", multiple);
  349. ENABLE_ACTION("adjust_size_menu", true);
  350. ENABLE_ACTION("adjust_width_small", multiple);
  351. ENABLE_ACTION("adjust_width_big", multiple);
  352. ENABLE_ACTION("adjust_height_small", multiple);
  353. ENABLE_ACTION("adjust_height_big", multiple);
  354. ENABLE_ACTION("format_raise", true);
  355. ENABLE_ACTION("format_lower", true);
  356. // If the widgets selected is a container, we enable layout actions
  357. bool containerSelected = false;
  358. if(!multiple)
  359. {
  360. KFormDesigner::ObjectTreeItem *item = form->objectTree()->lookup( form->selectedWidgets()->first()->name() );
  361. if(item && item->container())
  362. containerSelected = true;
  363. }
  364. const bool twoSelected = form->selectedWidgets()->count()==2;
  365. // Layout actions
  366. ENABLE_ACTION("layout_menu", multiple || containerSelected);
  367. ENABLE_ACTION("layout_hbox", multiple || containerSelected);
  368. ENABLE_ACTION("layout_vbox", multiple || containerSelected);
  369. ENABLE_ACTION("layout_grid", multiple || containerSelected);
  370. ENABLE_ACTION("layout_hsplitter", twoSelected);
  371. ENABLE_ACTION("layout_vsplitter", twoSelected);
  372. KFormDesigner::Container *container = KFormDesigner::FormManager::self()->activeForm()->activeContainer();
  373. ENABLE_ACTION("break_layout", (container->layoutType() != KFormDesigner::Container::NoLayout));
  374. }
  375. void
  376. KFormDesignerPart::slotFormWidgetSelected(Form *form)
  377. {
  378. disableWidgetActions();
  379. enableFormActions();
  380. const bool twoSelected = form->selectedWidgets()->count()==2;
  381. const bool hasChildren = !form->objectTree()->children()->isEmpty();
  382. // Layout actions
  383. ENABLE_ACTION("layout_menu", hasChildren);
  384. ENABLE_ACTION("layout_hbox", hasChildren);
  385. ENABLE_ACTION("layout_vbox", hasChildren);
  386. ENABLE_ACTION("layout_grid", hasChildren);
  387. ENABLE_ACTION("layout_hsplitter", twoSelected);
  388. ENABLE_ACTION("layout_vsplitter", twoSelected);
  389. ENABLE_ACTION("break_layout", (form->toplevelContainer()->layoutType() != KFormDesigner::Container::NoLayout));
  390. }
  391. void
  392. KFormDesignerPart::slotNoFormSelected()
  393. {
  394. disableWidgetActions();
  395. // Disable paste action
  396. ENABLE_ACTION("edit_paste", false);
  397. ENABLE_ACTION("edit_undo", false);
  398. ENABLE_ACTION("edit_redo", false);
  399. // Disable 'Tools' actions
  400. ENABLE_ACTION("pixmap_collection", false);
  401. ENABLE_ACTION("form_connections", false);
  402. ENABLE_ACTION("taborder", false);
  403. ENABLE_ACTION("change_style", KFormDesigner::FormManager::self()->activeForm());
  404. // Disable items in 'File'
  405. ENABLE_ACTION("file_save", false);
  406. ENABLE_ACTION("file_save_as", false);
  407. ENABLE_ACTION("preview_form", false);
  408. }
  409. void
  410. KFormDesignerPart::enableFormActions()
  411. {
  412. // Enable 'Tools' actions
  413. ENABLE_ACTION("pixmap_collection", true);
  414. ENABLE_ACTION("form_connections", true);
  415. ENABLE_ACTION("taborder", true);
  416. ENABLE_ACTION("change_style", true);
  417. // Enable items in 'File'
  418. ENABLE_ACTION("file_save", true);
  419. ENABLE_ACTION("file_save_as", true);
  420. ENABLE_ACTION("preview_form", true);
  421. ENABLE_ACTION("edit_paste", KFormDesigner::FormManager::self()->isPasteEnabled());
  422. ENABLE_ACTION("edit_select_all", true);
  423. }
  424. void
  425. KFormDesignerPart::disableWidgetActions()
  426. {
  427. // Disable edit actions
  428. ENABLE_ACTION("edit_copy", false);
  429. ENABLE_ACTION("edit_cut", false);
  430. ENABLE_ACTION("edit_delete", false);
  431. ENABLE_ACTION("clear_contents", false);
  432. // Disable format functions
  433. ENABLE_ACTION("align_menu", false);
  434. ENABLE_ACTION("align_to_left", false);
  435. ENABLE_ACTION("align_to_right", false);
  436. ENABLE_ACTION("align_to_top", false);
  437. ENABLE_ACTION("align_to_bottom", false);
  438. ENABLE_ACTION("adjust_size_menu", false);
  439. ENABLE_ACTION("format_raise", false);
  440. ENABLE_ACTION("format_lower", false);
  441. ENABLE_ACTION("layout_menu", false);
  442. ENABLE_ACTION("layout_hbox", false);
  443. ENABLE_ACTION("layout_vbox", false);
  444. ENABLE_ACTION("layout_grid", false);
  445. ENABLE_ACTION("layout_hsplitter", false);
  446. ENABLE_ACTION("layout_vsplitter", false);
  447. ENABLE_ACTION("break_layout", false);
  448. }
  449. #endif
  450. void
  451. KFormDesignerPart::setUndoEnabled(bool enabled, const TQString &text)
  452. {
  453. KAction *undoAction = actionCollection()->action("edit_undo");
  454. if(undoAction)
  455. {
  456. if(!text.isNull())
  457. undoAction->setText(text);
  458. }
  459. }
  460. void
  461. KFormDesignerPart::setRedoEnabled(bool enabled, const TQString &text)
  462. {
  463. KAction *redoAction = actionCollection()->action("edit_redo");
  464. if(redoAction)
  465. {
  466. if(!text.isNull())
  467. redoAction->setText(text);
  468. }
  469. }
  470. ////// FormWidgetBase : helper widget to draw rects on top of widgets
  471. //repaint all children widgets
  472. static void repaintAll(TQWidget *w)
  473. {
  474. w->repaint();
  475. TQObjectList *list = w->queryList(TQWIDGET_OBJECT_NAME_STRING);
  476. TQObjectListIt it(*list);
  477. for (TQObject *obj; (obj=it.current()); ++it ) {
  478. static_cast<TQWidget*>(obj)->repaint();
  479. }
  480. delete list;
  481. }
  482. void
  483. FormWidgetBase::drawRects(const TQValueList<TQRect> &list, int type)
  484. {
  485. TQPainter p;
  486. p.begin(this, true);
  487. bool unclipped = testWFlags( WPaintUnclipped );
  488. setWFlags( WPaintUnclipped );
  489. if (prev_rect.isValid()) {
  490. //redraw prev. selection's rectangle
  491. 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));
  492. }
  493. p.setBrush(TQBrush::NoBrush);
  494. if(type == 1) // selection rect
  495. p.setPen(TQPen(white, 1, TQt::DotLine));
  496. else if(type == 2) // insert rect
  497. p.setPen(TQPen(white, 2));
  498. p.setRasterOp(XorROP);
  499. prev_rect = TQRect();
  500. TQValueList<TQRect>::ConstIterator endIt = list.constEnd();
  501. for(TQValueList<TQRect>::ConstIterator it = list.constBegin(); it != endIt; ++it) {
  502. p.drawRect(*it);
  503. prev_rect = prev_rect.unite(*it);
  504. }
  505. if (!unclipped)
  506. clearWFlags( WPaintUnclipped );
  507. p.end();
  508. }
  509. void
  510. FormWidgetBase::drawRect(const TQRect& r, int type)
  511. {
  512. TQValueList<TQRect> l;
  513. l.append(r);
  514. drawRects(l, type);
  515. }
  516. void
  517. FormWidgetBase::initBuffer()
  518. {
  519. repaintAll(this);
  520. buffer.resize( width(), height() );
  521. buffer = TQPixmap::grabWindow( winId() );
  522. prev_rect = TQRect();
  523. }
  524. void
  525. FormWidgetBase::clearForm()
  526. {
  527. TQPainter p;
  528. p.begin(this, true);
  529. bool unclipped = testWFlags( WPaintUnclipped );
  530. setWFlags( WPaintUnclipped );
  531. //redraw entire form surface
  532. p.drawPixmap( TQPoint(0,0), buffer, TQRect(0,0,buffer.width(), buffer.height()) );
  533. if (!unclipped)
  534. clearWFlags( WPaintUnclipped );
  535. p.end();
  536. repaintAll(this);
  537. }
  538. void
  539. FormWidgetBase::highlightWidgets(TQWidget *from, TQWidget *to)//, const TQPoint &point)
  540. {
  541. TQPoint fromPoint, toPoint;
  542. if(from && from->parentWidget() && (from != this))
  543. fromPoint = from->parentWidget()->mapTo(this, from->pos());
  544. if(to && to->parentWidget() && (to != this))
  545. toPoint = to->parentWidget()->mapTo(this, to->pos());
  546. TQPainter p;
  547. p.begin(this, true);
  548. bool unclipped = testWFlags( WPaintUnclipped );
  549. setWFlags( WPaintUnclipped );
  550. if (prev_rect.isValid()) {
  551. //redraw prev. selection's rectangle
  552. p.drawPixmap( TQPoint(prev_rect.x(), prev_rect.y()), buffer, TQRect(prev_rect.x(), prev_rect.y(), prev_rect.width(), prev_rect.height()));
  553. }
  554. p.setPen( TQPen(TQt::red, 2) );
  555. if(to)
  556. {
  557. TQPixmap pix1 = TQPixmap::grabWidget(from);
  558. TQPixmap pix2 = TQPixmap::grabWidget(to);
  559. if((from != this) && (to != this))
  560. p.drawLine( from->parentWidget()->mapTo(this, from->geometry().center()), to->parentWidget()->mapTo(this, to->geometry().center()) );
  561. p.drawPixmap(fromPoint.x(), fromPoint.y(), pix1);
  562. p.drawPixmap(toPoint.x(), toPoint.y(), pix2);
  563. if(to == this)
  564. p.drawRoundRect(2, 2, width()-4, height()-4, 4, 4);
  565. else
  566. p.drawRoundRect(toPoint.x(), toPoint.y(), to->width(), to->height(), 5, 5);
  567. }
  568. if(from == this)
  569. p.drawRoundRect(2, 2, width()-4, height()-4, 4, 4);
  570. else
  571. p.drawRoundRect(fromPoint.x(), fromPoint.y(), from->width(), from->height(), 5, 5);
  572. if((to == this) || (from == this))
  573. prev_rect = TQRect(0, 0, buffer.width(), buffer.height());
  574. else if(to)
  575. {
  576. prev_rect.setX( (fromPoint.x() < toPoint.x()) ? (fromPoint.x() - 5) : (toPoint.x() - 5) );
  577. prev_rect.setY( (fromPoint.y() < toPoint.y()) ? (fromPoint.y() - 5) : (toPoint.y() - 5) );
  578. prev_rect.setRight( (fromPoint.x() < toPoint.x()) ? (toPoint.x() + to->width() + 10) : (fromPoint.x() + from->width() + 10) );
  579. prev_rect.setBottom( (fromPoint.y() < toPoint.y()) ? (toPoint.y() + to->height() + 10) : (fromPoint.y() + from->height() + 10) ) ;
  580. }
  581. else
  582. prev_rect = TQRect(fromPoint.x()- 5, fromPoint.y() -5, from->width() + 10, from->height() + 10);
  583. if (!unclipped)
  584. clearWFlags( WPaintUnclipped );
  585. p.end();
  586. }
  587. void
  588. FormWidgetBase::closeEvent(TQCloseEvent *ev)
  589. {
  590. Form *form = KFormDesigner::FormManager::self()->formForWidget(this);
  591. if(!form || !form->isModified() || !form->objectTree()) // == preview form
  592. ev->accept();
  593. else
  594. {
  595. bool close = m_part->closeForm(form);
  596. if(close)
  597. ev->accept();
  598. else
  599. ev->ignore();
  600. }
  601. }
  602. void KFormDesignerPart::slotPropertySetSwitched(KoProperty::Set *set, bool forceReload,
  603. const TQCString& propertyToSelect)
  604. {
  605. if (m_editor) {
  606. if (propertyToSelect.isEmpty() && forceReload)
  607. m_editor->changeSet(set, propertyToSelect);
  608. else
  609. m_editor->changeSet(set);
  610. }
  611. }
  612. K_EXPORT_COMPONENT_FACTORY(libkformdesigner_part, KFDFactory)
  613. #include "kfd_part.moc"