kexi

containerfactory.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2003 by Lucijan Busch          lucijan@kde.org          *
00003  *   Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Library General Public License as       *
00007  *   published by the Free Software Foundation; either version 2 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  ***************************************************************************/
00010 
00011 #include <qwidgetstack.h>
00012 #include <qframe.h>
00013 #include <qbuttongroup.h>
00014 #include <qwidget.h>
00015 #include <qhbox.h>
00016 #include <qvbox.h>
00017 #include <qstring.h>
00018 #include <qpopupmenu.h>
00019 #include <qdom.h>
00020 #include <qevent.h>
00021 #include <qobjectlist.h>
00022 #include <qpainter.h>
00023 #include <qvaluevector.h>
00024 #include <qfileinfo.h>
00025 #include <qscrollview.h>
00026 #include <qtabbar.h>
00027 #include <qsplitter.h>
00028 #include <qlayout.h>
00029 
00030 #include <kiconloader.h>
00031 #include <kgenericfactory.h>
00032 #include <ktextedit.h>
00033 #include <klineedit.h>
00034 #include <klocale.h>
00035 #include <kdebug.h>
00036 #include <kdeversion.h>
00037 
00038 #include "containerfactory.h"
00039 #include "container.h"
00040 #include "form.h"
00041 #include "formIO.h"
00042 #include "objecttree.h"
00043 #include "commands.h"
00044 #include "formmanager.h"
00045 #include "widgetlibrary.h"
00046 #include <formeditor/utils.h>
00047 
00048 #if KDE_VERSION < KDE_MAKE_VERSION(3,1,9)
00049 # define KInputDialog QInputDialog
00050 # include <qinputdialog.h>
00051 # include <qlineedit.h>
00052 #else
00053 # include <kinputdialog.h>
00054 #endif
00055 
00056 /*
00057 class KFORMEDITOR_EXPORT MyTabWidget : public KTabWidget
00058 {
00059     public:
00060         MyTabWidget(QWidget *parent, const char *name, QObject *container)
00061          : KTabWidget(parent, name)
00062         {
00063             m_container = container;
00064 
00065             QObjectList *list = new QObjectList(*children());
00066             for(QObject *obj = list->first(); obj; obj = list->next())
00067             {
00068                 if(obj->isA("KTabBar"))
00069                     obj->installEventFilter(this);
00070             }
00071             delete list;
00072         }
00073         ~MyTabWidget() {;}
00074 
00075         void setContainer(QObject *container)
00076         {
00077             m_container = container;
00078         }
00079         virtual bool eventFilter(QObject *o, QEvent *ev)
00080         {
00081             if((!m_container) || (ev->type() != QEvent::MouseButtonRelease))
00082                 return KTabWidget::eventFilter(o, ev);
00083 
00084             QMouseEvent *mev = static_cast<QMouseEvent*>(ev);
00085             if(mev->button() != RightButton)
00086                 return KTabWidget::eventFilter(o, ev);
00087 
00088             bool ok = m_container->eventFilter(this, ev);
00089             if(!ok)
00090                 return KTabWidget::eventFilter(o, ev);
00091             return true;
00092         }
00093 
00094     private:
00095         QGuardedPtr<QObject>   m_container;
00096 };*/
00097 
00098 QSize
00099 KFDTabWidget::sizeHint() const
00100 {
00101     QSize s(30,30); // default min size
00102     for(int i=0; i < count(); i++)
00103         s = s.expandedTo( KFormDesigner::getSizeFromChildren(page(i)) );
00104 
00105     return s + QSize(10/*margin*/, tabBar()->height() + 20/*margin*/);
00106 }
00107 
00108 
00110 
00111 HBox::HBox(QWidget *parent, const char *name)
00112  : QFrame(parent, name), m_preview(false)
00113 {}
00114 
00115 void
00116 HBox::paintEvent(QPaintEvent *)
00117 {
00118     if(m_preview) return;
00119     QPainter p(this);
00120     p.setPen(QPen(red, 2, Qt::DashLine));
00121     p.drawRect(1, 1, width()-1, height() - 1);
00122 }
00123 
00124 VBox::VBox(QWidget *parent, const char *name)
00125  : QFrame(parent, name), m_preview(false)
00126 {}
00127 
00128 void
00129 VBox::paintEvent(QPaintEvent *)
00130 {
00131     if(m_preview) return;
00132     QPainter p(this);
00133     p.setPen(QPen(blue, 2, Qt::DashLine));
00134     p.drawRect(1, 1, width()-1, height() - 1);
00135 }
00136 
00137 Grid::Grid(QWidget *parent, const char *name)
00138  : QFrame(parent, name), m_preview(false)
00139 {}
00140 
00141 void
00142 Grid::paintEvent(QPaintEvent *)
00143 {
00144     if(m_preview) return;
00145     QPainter p(this);
00146     p.setPen(QPen(darkGreen, 2, Qt::DashLine));
00147     p.drawRect(1, 1, width()-1, height() - 1);
00148 }
00149 
00150 HFlow::HFlow(QWidget *parent, const char *name)
00151  : QFrame(parent, name), m_preview(false)
00152 {}
00153 
00154 void
00155 HFlow::paintEvent(QPaintEvent *)
00156 {
00157     if(m_preview) return;
00158     QPainter p(this);
00159     p.setPen(QPen(magenta, 2, Qt::DashLine));
00160     p.drawRect(1, 1, width()-1, height() - 1);
00161 }
00162 
00163 VFlow::VFlow(QWidget *parent, const char *name)
00164  : QFrame(parent, name), m_preview(false)
00165 {}
00166 
00167 void
00168 VFlow::paintEvent(QPaintEvent *)
00169 {
00170     if(m_preview) return;
00171     QPainter p(this);
00172     p.setPen(QPen(cyan, 2, Qt::DashLine));
00173     p.drawRect(1, 1, width()-1, height() - 1);
00174 }
00175 
00176 QSize
00177 VFlow::sizeHint() const
00178 {
00179     if(layout())
00180         return layout()->sizeHint();
00181     else
00182         return QSize(700, 50); // default
00183 }
00184 
00186 
00187 InsertPageCommand::InsertPageCommand(KFormDesigner::Container *container, QWidget *parent)
00188   : KCommand()
00189 {
00190     m_containername = container->widget()->name();
00191     m_form = container->form();
00192     m_parentname = parent->name();
00193     m_pageid = -1;
00194 }
00195 
00196 void
00197 InsertPageCommand::execute()
00198 {
00199     KFormDesigner::Container *container = m_form->objectTree()->lookup(m_containername)->container();
00200     QWidget *parent = m_form->objectTree()->lookup(m_parentname)->widget();
00201     if(m_name.isEmpty()) {
00202         m_name = container->form()->objectTree()->generateUniqueName(
00203             container->form()->library()->displayName("QWidget").latin1());
00204     }
00205 
00206     QWidget *page = new QWidget(parent, m_name.latin1());
00207     new KFormDesigner::Container(container, page, parent);
00208 
00209     QCString classname = parent->className();
00210     if(classname == "KFDTabWidget")
00211     {
00212         TabWidgetBase *tab = dynamic_cast<TabWidgetBase*>(parent);
00213         QString n = i18n("Page %1").arg(tab->count() + 1);
00214         tab->addTab(page, n);
00215         tab->showPage(page);
00216 
00217         KFormDesigner::ObjectTreeItem *item = container->form()->objectTree()->lookup(m_name);
00218         item->addModifiedProperty("title", n);
00219     }
00220     else if(classname == "QWidgetStack")
00221     {
00222         QWidgetStack *stack = (QWidgetStack*)parent;
00223         stack->addWidget(page, m_pageid);
00224         stack->raiseWidget(page);
00225         m_pageid = stack->id(page);
00226 
00227         KFormDesigner::ObjectTreeItem *item = container->form()->objectTree()->lookup(m_name);
00228         item->addModifiedProperty("id", stack->id(page));
00229     }
00230 }
00231 
00232 void
00233 InsertPageCommand::unexecute()
00234 {
00235     QWidget *page = m_form->objectTree()->lookup(m_name)->widget();
00236     QWidget *parent = m_form->objectTree()->lookup(m_parentname)->widget();
00237 
00238     KFormDesigner::WidgetList list;
00239     list.append(page);
00240     KCommand *com = new KFormDesigner::DeleteWidgetCommand(list, m_form);
00241 
00242     QCString classname = parent->className();
00243     if(classname == "KFDTabWidget")
00244     {
00245         TabWidgetBase *tab = dynamic_cast<TabWidgetBase*>(parent);
00246         tab->removePage(page);
00247     }
00248     else if(classname == "QWidgetStack")
00249     {
00250         QWidgetStack *stack = (QWidgetStack*)parent;
00251         int id = stack->id(page) - 1;
00252         while(!stack->widget(id))
00253             id--;
00254 
00255         stack->raiseWidget(id);
00256         stack->removeWidget(page);
00257     }
00258 
00259     com->execute();
00260     delete com;
00261 }
00262 
00263 QString
00264 InsertPageCommand::name() const
00265 {
00266     return i18n("Add Page");
00267 }
00268 
00270 
00271 SubForm::SubForm(QWidget *parent, const char *name)
00272 : QScrollView(parent, name), m_form(0), m_widget(0)
00273 {
00274     setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
00275     viewport()->setPaletteBackgroundColor(colorGroup().mid());
00276 }
00277 
00278 void
00279 SubForm::setFormName(const QString &name)
00280 {
00281     if(name.isEmpty())
00282         return;
00283 
00284     QFileInfo info(name);
00285     if(!info.exists() 
00286         || (KFormDesigner::FormManager::self()->activeForm() 
00287             && (info.fileName() == KFormDesigner::FormManager::self()->activeForm()->filename()) ) )
00288         return; // we check if this is valid form
00289 
00290     // we create the container widget
00291     delete m_widget;
00292     m_widget = new QWidget(viewport(), "subform_widget");
00293     m_widget->show();
00294     addChild(m_widget);
00295     m_form = new KFormDesigner::Form(
00296         KFormDesigner::FormManager::self()->activeForm()->library(), this->name());
00297     m_form->createToplevel(m_widget);
00298 
00299     // and load the sub form
00300     KFormDesigner::FormIO::loadFormFromFile(m_form, m_widget, name);
00301     m_form->setDesignMode(false);
00302 
00303     m_formName = name;
00304 
00305 }
00306 
00308 
00309 ContainerFactory::ContainerFactory(QObject *parent, const char *, const QStringList &)
00310  : KFormDesigner::WidgetFactory(parent, "containers")
00311 {
00312     KFormDesigner::WidgetInfo *wBtnGroup = new KFormDesigner::WidgetInfo(this);
00313     wBtnGroup->setPixmap("frame");
00314     wBtnGroup->setClassName("QButtonGroup");
00315     wBtnGroup->setName(i18n("Button Group"));
00316     wBtnGroup->setNamePrefix(
00317         i18n("Widget name. This string will be used to name widgets of this class. It must _not_ contain white spaces and non latin1 characters.", "buttonGroup"));
00318     wBtnGroup->setDescription(i18n("A simple container to group buttons"));
00319     addClass(wBtnGroup);
00320 
00321     KFormDesigner::WidgetInfo *wTabWidget = new KFormDesigner::WidgetInfo(this);
00322     wTabWidget->setPixmap("tabwidget");
00323     wTabWidget->setClassName("KFDTabWidget");
00324 #if KDE_VERSION >= KDE_MAKE_VERSION(3,1,9)
00325     wTabWidget->addAlternateClassName("KTabWidget");
00326     wTabWidget->addAlternateClassName("QTabWidget");
00327 //tmp:  wTabWidget->setSavingName("QTabWidget");
00328     wTabWidget->setSavingName("KTabWidget");
00329 #else
00330     wTabWidget->setSavingName("QTabWidget");
00331 #endif
00332     wTabWidget->setIncludeFileName("ktabwidget.h");
00333     wTabWidget->setName(i18n("Tab Widget"));
00334     wTabWidget->setNamePrefix(
00335         i18n("Widget name. This string will be used to name widgets of this class. It must _not_ contain white spaces and non latin1 characters.", "tabWidget"));
00336     wTabWidget->setDescription(i18n("A widget to display multiple pages using tabs"));
00337     addClass(wTabWidget);
00338 
00339     KFormDesigner::WidgetInfo *wWidget = new KFormDesigner::WidgetInfo(this);
00340     wWidget->setPixmap("frame");
00341     wWidget->setClassName("QWidget");
00342     wWidget->setName(i18n("Basic container"));
00343     wWidget->setNamePrefix(
00344         i18n("Widget name. This string will be used to name widgets of this class. It must _not_ contain white spaces and non latin1 characters.", "container"));
00345     wWidget->setDescription(i18n("An empty container with no frame"));
00346     addClass(wWidget);
00347 
00348     KFormDesigner::WidgetInfo *wGroupBox = new KFormDesigner::WidgetInfo(this);
00349     wGroupBox->setPixmap("groupbox");
00350     wGroupBox->setClassName("QGroupBox");
00351     wGroupBox->setName(i18n("Group Box"));
00352     wGroupBox->setNamePrefix(
00353         i18n("Widget name. This string will be used to name widgets of this class. It must _not_ contain white spaces and non latin1 characters.", "groupBox"));
00354     wGroupBox->setDescription(i18n("A container to group some widgets"));
00355     addClass(wGroupBox);
00356 
00357     KFormDesigner::WidgetInfo *wFrame = new KFormDesigner::WidgetInfo(this);
00358     wFrame->setPixmap("frame");
00359     wFrame->setClassName("QFrame");
00360     wFrame->setName(i18n("Frame"));
00361     wFrame->setNamePrefix(
00362         i18n("Widget name. This string will be used to name widgets of this class. It must _not_ contain white spaces and non latin1 characters.", "frame"));
00363     wFrame->setDescription(i18n("A simple frame container"));
00364     addClass(wFrame);
00365 
00366     KFormDesigner::WidgetInfo *wWidgetStack = new KFormDesigner::WidgetInfo(this);
00367     wWidgetStack->setPixmap("widgetstack");
00368     wWidgetStack->setClassName("QWidgetStack");
00369     wWidgetStack->setName(i18n("Widget Stack"));
00370     wWidgetStack->setNamePrefix(
00371         i18n("Widget name. This string will be used to name widgets of this class. It must _not_ contain white spaces and non latin1 characters.", "widgetStack"));
00372     wWidgetStack->setDescription(i18n("A container with multiple pages"));
00373     addClass(wWidgetStack);
00374 
00375     KFormDesigner::WidgetInfo *wHBox = new KFormDesigner::WidgetInfo(this);
00376     wHBox->setPixmap("frame");
00377     wHBox->setClassName("HBox");
00378     wHBox->setName(i18n("Horizontal Box"));
00379     wHBox->setNamePrefix(
00380         i18n("Widget name. This string will be used to name widgets of this class. It must _not_ contain white spaces and non latin1 characters.", "horizontalBox"));
00381     wHBox->setDescription(i18n("A simple container to group widgets horizontally"));
00382     addClass(wHBox);
00383 
00384     KFormDesigner::WidgetInfo *wVBox = new KFormDesigner::WidgetInfo(this);
00385     wVBox->setPixmap("frame");
00386     wVBox->setClassName("VBox");
00387     wVBox->setName(i18n("Vertical Box"));
00388     wVBox->setNamePrefix(
00389         i18n("Widget name. This string will be used to name widgets of this class. It must _not_ contain white spaces and non latin1 characters.", "verticalBox"));
00390     wVBox->setDescription(i18n("A simple container to group widgets vertically"));
00391     addClass(wVBox);
00392 
00393     KFormDesigner::WidgetInfo *wGrid = new KFormDesigner::WidgetInfo(this);
00394     wGrid->setPixmap("frame");
00395     wGrid->setClassName("Grid");
00396     wGrid->setName(i18n("Grid Box"));
00397     wGrid->setNamePrefix(
00398         i18n("Widget name. This string will be used to name widgets of this class. It must _not_ contain white spaces and non latin1 characters.", "gridBox"));
00399     wGrid->setDescription(i18n("A simple container to group widgets in a grid"));
00400     addClass(wGrid);
00401 
00402     KFormDesigner::WidgetInfo *wSplitter = new KFormDesigner::WidgetInfo(this);
00404     wSplitter->setPixmap("frame");
00405     wSplitter->setClassName("Splitter");
00406     wSplitter->addAlternateClassName("QSplitter");
00407     wSplitter->setName(i18n("Splitter"));
00408     wSplitter->setNamePrefix(
00409         i18n("Widget name. This string will be used to name widgets of this class. It must _not_ contain white spaces and non latin1 characters.", "splitter"));
00410     wSplitter->setDescription(i18n("A container that enables user to resize its children"));
00411     addClass(wSplitter);
00412 
00413     KFormDesigner::WidgetInfo *wHFlow = new KFormDesigner::WidgetInfo(this);
00415     wHFlow->setPixmap("frame");
00416     wHFlow->setClassName("HFlow");
00417     wHFlow->setName(i18n("Row Layout"));
00418     wHFlow->setNamePrefix(
00419         i18n("Widget name. This string will be used to name widgets of this class. It must _not_ contain white spaces and non latin1 characters.", "rowLayout"));
00420     wHFlow->setDescription(i18n("A simple container to group widgets by rows"));
00421     addClass(wHFlow);
00422 
00423     KFormDesigner::WidgetInfo *wVFlow = new KFormDesigner::WidgetInfo(this);
00425     wVFlow->setPixmap("frame");
00426     wVFlow->setClassName("VFlow");
00427     wVFlow->setName(i18n("Column Layout"));
00428     wVFlow->setNamePrefix(
00429         i18n("Widget name. This string will be used to name widgets of this class. It must _not_ contain white spaces and non latin1 characters.", "columnLayout"));
00430     wVFlow->setDescription(i18n("A simple container to group widgets by columns"));
00431     addClass(wVFlow);
00432 
00433     KFormDesigner::WidgetInfo *wSubForm = new KFormDesigner::WidgetInfo(this);
00434     wSubForm->setPixmap("form");
00435     wSubForm->setClassName("SubForm");
00436     wSubForm->setName(i18n("Sub Form"));
00437     wSubForm->setNamePrefix(
00438         i18n("Widget name. This string will be used to name widgets of this class. It must _not_ contain white spaces and non latin1 characters.", "subForm"));
00439     wSubForm->setDescription(i18n("A form widget included in another Form"));
00440     wSubForm->setAutoSyncForProperty( "formName", false );
00441     addClass(wSubForm);
00442 
00443     //groupbox
00444     m_propDesc["title"] = i18n("Title");
00445     m_propDesc["flat"] = i18n("Flat");
00446 
00447     //tab widget
00448     m_propDesc["tabPosition"] = i18n("Tab Position");
00449     m_propDesc["currentPage"] = i18n("Current Page");
00450     m_propDesc["tabShape"] = i18n("Tab Shape");
00451 
00452     m_propDesc["tabPosition"] = i18n("Tab Position");
00453     m_propDesc["tabPosition"] = i18n("Tab Position");
00454 
00455     m_propValDesc["Rounded"] = i18n("for Tab Shape", "Rounded");
00456     m_propValDesc["Triangular"] = i18n("for Tab Shape", "Triangular");
00457 }
00458 
00459 QWidget*
00460 ContainerFactory::createWidget(const QCString &c, QWidget *p, const char *n, 
00461     KFormDesigner::Container *container, int options)
00462 {
00463     if(c == "QButtonGroup")
00464     {
00465         QString text = container->form()->library()->textForWidgetName(n, c);
00466         QButtonGroup *w = new QButtonGroup(/*i18n("Button Group")*/text, p, n);
00467         new KFormDesigner::Container(container, w, container);
00468         return w;
00469     }
00470     else if(c == "KFDTabWidget")
00471     {
00472         //MyTabWidget *tab = new MyTabWidget(p, n, container);
00473         KFDTabWidget *tab = new KFDTabWidget(p, n);
00474 #if defined(USE_KTabWidget) && KDE_VERSION >= KDE_MAKE_VERSION(3,1,9)
00475         tab->setTabReorderingEnabled(true);
00476         connect(tab, SIGNAL(movedTab(int,int)), this, SLOT(reorderTabs(int,int)));
00477 #endif
00478         container->form()->objectTree()->addItem(container->objectTree(), 
00479             new KFormDesigner::ObjectTreeItem(
00480                 container->form()->library()->displayName(c), n, tab, container));
00481 //      m_manager = container->form()->manager();
00482 
00483         // if we are loading, don't add this tab
00484         if(container->form()->interactiveMode())
00485         {
00486             //m_widget=tab;
00487             setWidget(tab, container);
00488 //          m_container=container;
00489             addTabPage();
00490         }
00491 
00492         return tab;
00493     }
00494     else if(c == "QWidget")
00495     {
00496         QWidget *w = new QWidget(p, n);
00497         new KFormDesigner::Container(container, w, p);
00498         return w;
00499     }
00500     else if(c == "QGroupBox")
00501     {
00502         QString text = container->form()->library()->textForWidgetName(n, c);
00503         QGroupBox *w = new QGroupBox(/*i18n("Group Box")*/text, p, n);
00504         new KFormDesigner::Container(container, w, container);
00505         return w;
00506     }
00507     else if(c == "QFrame")
00508     {
00509         QFrame *w = new QFrame(p, n);
00510         w->setLineWidth(2);
00511         w->setFrameStyle(QFrame::StyledPanel|QFrame::Raised);
00512         new KFormDesigner::Container(container, w, container);
00513         return w;
00514     }
00515     else if(c == "QWidgetStack")
00516     {
00517         QWidgetStack *stack = new QWidgetStack(p, n);
00518         stack->setLineWidth(2);
00519         stack->setFrameStyle(QFrame::StyledPanel|QFrame::Raised);
00520         container->form()->objectTree()->addItem( container->objectTree(), 
00521             new KFormDesigner::ObjectTreeItem( 
00522                 container->form()->library()->displayName(c), n, stack, container));
00523 
00524         if(container->form()->interactiveMode())
00525         {
00526             //m_widget = stack;
00527             setWidget(stack, container);
00528 //          m_container = container;
00529             addStackPage();
00530         }
00531         return stack;
00532     }
00533     else if(c == "HBox") {
00534         HBox *w = new HBox(p, n);
00535         new KFormDesigner::Container(container, w, container);
00536         return w;
00537     }
00538     else if(c == "VBox") {
00539         VBox *w = new VBox(p, n);
00540         new KFormDesigner::Container(container, w, container);
00541         return w;
00542     }
00543     else if(c == "Grid") {
00544         Grid *w = new Grid(p, n);
00545         new KFormDesigner::Container(container, w, container);
00546         return w;
00547     }
00548     else if(c == "HFlow") {
00549         HFlow *w = new HFlow(p, n);
00550         new KFormDesigner::Container(container, w, container);
00551         return w;
00552     }
00553     else if(c == "VFlow") {
00554         VFlow *w = new VFlow(p, n);
00555         new KFormDesigner::Container(container, w, container);
00556         return w;
00557     }
00558     else if(c == "SubForm") {
00559         SubForm *subform = new SubForm(p, n);
00560         return subform;
00561     }
00562     else if(c == "QSplitter") {
00563         QSplitter *split = new QSplitter(p, n);
00564         if (0 == (options & WidgetFactory::AnyOrientation))
00565             split->setOrientation(
00566                 (options & WidgetFactory::VerticalOrientation) ? Qt::Vertical : Qt::Horizontal);
00567         new KFormDesigner::Container(container, split, container);
00568         return split;
00569     }
00570 
00571     return 0;
00572 }
00573 
00574 bool
00575 ContainerFactory::previewWidget(const QCString &classname, QWidget *widget, KFormDesigner::Container *container)
00576 {
00577     if(classname == "WidgetStack")
00578     {
00579         QWidgetStack *stack = ((QWidgetStack*)widget);
00580         KFormDesigner::ObjectTreeItem *tree = container->form()->objectTree()->lookup(widget->name());
00581         if(!tree->modifiedProperties()->contains("frameShape"))
00582             stack->setFrameStyle(QFrame::NoFrame);
00583     }
00584     else if(classname == "HBox")
00585         ((HBox*)widget)->setPreviewMode();
00586     else if(classname == "VBox")
00587         ((VBox*)widget)->setPreviewMode();
00588     else if(classname == "Grid")
00589         ((Grid*)widget)->setPreviewMode();
00590     else if(classname == "HFlow")
00591         ((HFlow*)widget)->setPreviewMode();
00592     else if(classname == "VFlow")
00593         ((VFlow*)widget)->setPreviewMode();
00594     else
00595         return false;
00596     return true;
00597 }
00598 
00599 bool
00600 ContainerFactory::createMenuActions(const QCString &classname, QWidget *w, QPopupMenu *menu,
00601     KFormDesigner::Container *container)
00602 {
00603     setWidget(w, container);
00604     //m_widget = w;
00605 //  m_container = container;
00606 
00607     if((classname == "KFDTabWidget") || (w->parentWidget()->parentWidget()->inherits("QTabWidget")))
00608     {
00609         if(w->parentWidget()->parentWidget()->inherits("QTabWidget"))
00610         {
00611             //m_widget = w->parentWidget()->parentWidget();
00612             setWidget(w->parentWidget()->parentWidget(), m_container->toplevel());
00613 //          m_container = m_container->toplevel();
00614         }
00615 
00616         int id = menu->insertItem(SmallIconSet("tab_new"), i18n("Add Page"), this, SLOT(addTabPage()) );
00617         id = menu->insertItem(SmallIconSet("edit"), i18n("Rename Page"), this, SLOT(renameTabPage()));
00618         id = menu->insertItem(SmallIconSet("tab_remove"), i18n("Remove Page"), this, SLOT(removeTabPage()));
00619 //      if( dynamic_cast<TabWidgetBase*>(m_widget)->count() == 1)
00620         if( dynamic_cast<TabWidgetBase*>(widget())->count() == 1)
00621             menu->setItemEnabled(id, false);
00622         return true;
00623     }
00624     else if(w->parentWidget()->isA("QWidgetStack") && !w->parentWidget()->parentWidget()->inherits("QTabWidget"))
00625     {
00626         //m_widget = w->parentWidget();
00627         QWidgetStack *stack = (QWidgetStack*)w->parentWidget(); //m_widget;
00628         setWidget(
00629             w->parentWidget(), 
00630             container->form()->objectTree()->lookup(stack->name())->parent()->container()
00631         );
00632 //      m_container = container->form()->objectTree()->lookup(m_widget->name())->parent()->container();
00633 //      m_container = container->form()->objectTree()->lookup(stack->name())->parent()->container();
00634 
00635         int id = menu->insertItem(SmallIconSet("tab_new"), i18n("Add Page"), this, SLOT(addStackPage()) );
00636 
00637         id = menu->insertItem(SmallIconSet("tab_remove"), i18n("Remove Page"), this, SLOT(removeStackPage()) );
00638 //      if( ((QWidgetStack*)m_widget)->children()->count() == 4) // == the stack has only one page
00639         if(stack->children()->count() == 4) // == the stack has only one page
00640             menu->setItemEnabled(id, false);
00641 
00642         id = menu->insertItem(SmallIconSet("next"), i18n("Jump to Next Page"), this, SLOT(nextStackPage()));
00643         if(!stack->widget(stack->id(stack->visibleWidget())+1))
00644             menu->setItemEnabled(id, false);
00645 
00646         id = menu->insertItem(SmallIconSet("previous"), i18n("Jump to Previous Page"), this, SLOT(prevStackPage()));
00647         if(!stack->widget(stack->id(stack->visibleWidget()) -1) )
00648             menu->setItemEnabled(id, false);
00649         return true;
00650     }
00651     return false;
00652 }
00653 
00654 bool
00655 ContainerFactory::startEditing(const QCString &classname, QWidget *w, KFormDesigner::Container *container)
00656 {
00657     m_container = container;
00658     if(classname == "QButtonGroup")
00659     {
00660         QButtonGroup *group = static_cast<QButtonGroup*>(w);
00661         QRect r = QRect(group->x()+2, group->y()-5, group->width()-10, w->fontMetrics().height() + 10);
00662         createEditor(classname, group->title(), group, container, r, Qt::AlignAuto);
00663         return true;
00664     }
00665     if(classname == "QGroupBox")
00666     {
00667         QGroupBox *group = static_cast<QGroupBox*>(w);
00668         QRect r = QRect(group->x()+2, group->y()-5, group->width()-10, w->fontMetrics().height() + 10);
00669         createEditor(classname, group->title(), group, container, r, Qt::AlignAuto);
00670         return true;
00671     }
00672     return false;
00673 }
00674 
00675 bool
00676 ContainerFactory::saveSpecialProperty(const QCString &, const QString &name, const QVariant &, QWidget *w, QDomElement &parentNode, QDomDocument &parent)
00677 {
00678     if((name == "title") && (w->parentWidget()->parentWidget()->inherits("QTabWidget")))
00679     {
00680         TabWidgetBase *tab = dynamic_cast<TabWidgetBase*>(w->parentWidget()->parentWidget());
00681         KFormDesigner::FormIO::savePropertyElement(parentNode, parent, "attribute", "title", tab->tabLabel(w));
00682     }
00683     else if((name == "id") && (w->parentWidget()->isA("QWidgetStack")))
00684     {
00685         QWidgetStack *stack = (QWidgetStack*)w->parentWidget();
00686         KFormDesigner::FormIO::savePropertyElement(parentNode, parent, "attribute", "id", stack->id(w));
00687     }
00688     else
00689         return false;
00690     return true;
00691 }
00692 
00693 bool
00694 ContainerFactory::readSpecialProperty(const QCString &, QDomElement &node, QWidget *w, KFormDesigner::ObjectTreeItem *item)
00695 {
00696     QString name = node.attribute("name");
00697     if((name == "title") && (item->parent()->widget()->inherits("QTabWidget")))
00698     {
00699         TabWidgetBase *tab = dynamic_cast<TabWidgetBase*>(w->parentWidget());
00700         tab->addTab(w, node.firstChild().toElement().text());
00701         item->addModifiedProperty("title", node.firstChild().toElement().text());
00702         return true;
00703     }
00704 
00705     if((name == "id") && (w->parentWidget()->isA("QWidgetStack")))
00706     {
00707         QWidgetStack *stack = (QWidgetStack*)w->parentWidget();
00708         int id = KFormDesigner::FormIO::readPropertyValue(node.firstChild(), w, name).toInt();
00709         stack->addWidget(w, id);
00710         stack->raiseWidget(w);
00711         item->addModifiedProperty("id", id);
00712         return true;
00713     }
00714 
00715     return false;
00716 }
00717 
00718 QValueList<QCString>
00719 ContainerFactory::autoSaveProperties(const QCString &c)
00720 {
00721     QValueList<QCString> lst;
00722 //  if(c == "SubForm")
00723 //      lst << "formName";
00724     if(c == "QSplitter")
00725         lst << "orientation";
00726     return lst;
00727 }
00728 
00729 bool
00730 ContainerFactory::isPropertyVisibleInternal(const QCString &classname,
00731     QWidget *w, const QCString &property, bool isTopLevel)
00732 {
00733     bool ok = true;
00734 
00735     if((classname == "HBox") || (classname == "VBox") || (classname == "Grid") ||
00736         (classname == "HFlow") || (classname == "VFlow"))
00737     {
00738         return property == "name" || property == "geometry";
00739     }
00740     else if (classname == "QGroupBox") {
00741         ok =
00742 #ifdef KEXI_NO_UNFINISHED
00743 
00746             (m_showAdvancedProperties || (property != "checkable" && property != "checked")) &&
00747 #endif
00748             true
00749             ;
00750     }
00751     else if (classname == "KFDTabWidget") {
00752         ok = (m_showAdvancedProperties || (property != "tabReorderingEnabled" && property != "hoverCloseButton" && property != "hoverCloseButtonDelayed"));
00753     }
00754 
00755     return ok && WidgetFactory::isPropertyVisibleInternal(classname, w, property, isTopLevel);
00756 }
00757 
00758 bool
00759 ContainerFactory::changeText(const QString &text)
00760 {
00761     changeProperty("title", text, m_container->form());
00762     return true;
00763 }
00764 
00765 void
00766 ContainerFactory::resizeEditor(QWidget *editor, QWidget *widget, const QCString &)
00767 {
00768     QSize s = widget->size();
00769     editor->move(widget->x() + 2, widget->y() - 5);
00770     editor->resize(s.width() - 20, widget->fontMetrics().height() +10);
00771 }
00772 
00773 // Widget Specific slots used in menu items
00774 
00775 void ContainerFactory::addTabPage()
00776 {
00777 //  if (!m_widget->inherits("QTabWidget"))
00778     if (!widget()->inherits("QTabWidget"))
00779         return;
00780     KCommand *com = new InsertPageCommand(m_container, widget());
00781     if(dynamic_cast<TabWidgetBase*>(widget())->count() == 0)
00782     {
00783         com->execute();
00784         delete com;
00785     }
00786     else
00787         m_container->form()->addCommand(com, true);
00788 }
00789 
00790 void ContainerFactory::removeTabPage()
00791 {
00792     if (!widget()->inherits("QTabWidget"))
00793         return;
00794     TabWidgetBase *tab = dynamic_cast<TabWidgetBase*>(widget());
00795     QWidget *w = tab->currentPage();
00796 
00797     KFormDesigner::WidgetList list;
00798     list.append(w);
00799     KCommand *com = new KFormDesigner::DeleteWidgetCommand(list, m_container->form());
00800     tab->removePage(w);
00801     m_container->form()->addCommand(com, true);
00802 }
00803 
00804 void ContainerFactory::renameTabPage()
00805 {
00806     if (!widget()->inherits("QTabWidget"))
00807         return;
00808     TabWidgetBase *tab = dynamic_cast<TabWidgetBase*>(widget());
00809     QWidget *w = tab->currentPage();
00810     bool ok;
00811 
00812     QString name = KInputDialog::getText(i18n("New Page Title"), i18n("Enter a new title for the current page:"),
00813 #if KDE_VERSION < KDE_MAKE_VERSION(3,1,9)
00814            QLineEdit::Normal,
00815 #endif
00816            tab->tabLabel(w), &ok, w->topLevelWidget());
00817     if(ok)
00818         tab->changeTab(w, name);
00819 }
00820 
00821 void ContainerFactory::reorderTabs(int oldpos, int newpos)
00822 {
00823     KFormDesigner::ObjectTreeItem *tab 
00824         = KFormDesigner::FormManager::self()->activeForm()->objectTree()->lookup(sender()->name());
00825     if(!tab)
00826         return;
00827 
00828     KFormDesigner::ObjectTreeItem *item = tab->children()->take(oldpos);
00829     tab->children()->insert(newpos, item);
00830 }
00831 
00832 void ContainerFactory::addStackPage()
00833 {
00834     if (!widget()->isA("QWidgetStack"))
00835         return;
00836     KCommand *com = new InsertPageCommand(m_container, widget());
00837     if(!((QWidgetStack*)widget())->visibleWidget())
00838     {
00839         com->execute();
00840         delete com;
00841     }
00842     else
00843         m_container->form()->addCommand(com, true);
00844 }
00845 
00846 void ContainerFactory::removeStackPage()
00847 {
00848     if (!widget()->isA("QWidgetStack"))
00849         return;
00850     QWidgetStack *stack = (QWidgetStack*)widget();
00851     QWidget *page = stack->visibleWidget();
00852 
00853     KFormDesigner::WidgetList list;
00854     list.append(page);
00855     KCommand *com = new KFormDesigner::DeleteWidgetCommand(list, m_container->form());
00856 
00857     // raise prev widget
00858     int id = stack->id(page) - 1;
00859     while(!stack->widget(id))
00860         id--;
00861     stack->raiseWidget(id);
00862 
00863     stack->removeWidget(page);
00864     m_container->form()->addCommand(com, true);
00865 }
00866 
00867 void ContainerFactory::prevStackPage()
00868 {
00869     QWidgetStack *stack = (QWidgetStack*)widget();
00870     int id = stack->id(stack->visibleWidget()) - 1;
00871     if(stack->widget(id))
00872         stack->raiseWidget(id);
00873 }
00874 
00875 void ContainerFactory::nextStackPage()
00876 {
00877     QWidgetStack *stack = (QWidgetStack*)widget();
00878     int id = stack->id(stack->visibleWidget()) + 1;
00879     if(stack->widget(id))
00880         stack->raiseWidget(id);
00881 }
00882 
00883 ContainerFactory::~ContainerFactory()
00884 {
00885 }
00886 
00887 KFORMDESIGNER_WIDGET_FACTORY(ContainerFactory, containers)
00888 
00889 #include "containerfactory.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys