Advertisement
Guest User

Wt 3.2.2 dynamic widget adding/removing layout problem

a guest
Sep 4th, 2012
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.08 KB | None | 0 0
  1. #include <Wt/WApplication>
  2. #include <Wt/WComboBox>
  3. #include <Wt/WContainerWidget>
  4. #include <Wt/WGridLayout>
  5. #include <Wt/WVBoxLayout>
  6. #include <Wt/WHBoxLayout>
  7. #include <Wt/WGroupBox>
  8. #include <Wt/WLabel>
  9. #include <Wt/WText>
  10. #include <Wt/WPushButton>
  11. #include <Wt/WLineEdit>
  12. #include <Wt/WPopupMenu>
  13. #include <iostream>
  14.  
  15. class AttributeGroup;
  16.  
  17. class WtApp : public Wt::WApplication
  18. {
  19.    public:
  20.       WtApp( const Wt::WEnvironment& env);
  21.       ~WtApp() {}
  22.  
  23.    private:
  24.       Wt::WText *createText(const Wt::WString& text);
  25.       Wt::WWidget *createSelection();
  26.  
  27.       void choiceChanged(int i);
  28.  
  29.    private:
  30.       Wt::WGridLayout* mLayout;
  31.       Wt::WContainerWidget* mSelections;
  32.       Wt::WVBoxLayout* mSelectionLayout;
  33.       Wt::WText* mStretch;
  34.       AttributeGroup* mAttrGroup;
  35. };
  36.  
  37. class Attribute : public Wt::WGroupBox
  38. {
  39.    public:
  40.       Attribute(const Wt::WString& title,
  41.         const Wt::WString& description)
  42.      : WGroupBox(title)
  43.      , mDesc(description)
  44.      , mLabel(new Wt::WLabel(mDesc))
  45.      , mVBox(new Wt::WVBoxLayout())
  46.      , mHBox(new Wt::WHBoxLayout())
  47.       {
  48.      mHBox->setContentsMargins(0, 0, 0, 0);
  49.      mHBox->addWidget(mLabel, 0);
  50.      mVBox->addLayout(mHBox);
  51.      mVBox->setContentsMargins(0, 0, 0, 3);
  52.      setLayout(mVBox);
  53.      setStyleClass("attrbox");
  54.      hide();
  55.       }
  56.  
  57.       Attribute(const Wt::WString& title)
  58.      : WGroupBox(title)
  59.      , mDesc()
  60.      , mLabel(0)
  61.      , mVBox(new Wt::WVBoxLayout())
  62.      , mHBox(0)
  63.       {
  64.      mVBox->setContentsMargins(0, 0, 0, 3);
  65.      setLayout(mVBox);
  66.      setStyleClass("attrbox");
  67.      hide();
  68.       }
  69.  
  70.    protected:
  71.       Wt::WString mDesc;
  72.       Wt::WLabel* mLabel;
  73.       Wt::WVBoxLayout* mVBox;
  74.       Wt::WHBoxLayout* mHBox;
  75. };
  76.  
  77. class NameAttribute : public Attribute
  78. {
  79.    public:
  80.       NameAttribute(const Wt::WString& name)
  81.      : Attribute(Wt::WString("Edit ")+name,
  82.              Wt::WString())
  83.      , mName(new Wt::WLineEdit())
  84.       {
  85.      mHBox->addWidget(mName, 1);
  86.      mLabel->setBuddy(mName);
  87.       }
  88.  
  89.    private:
  90.       Wt::WLineEdit* mName;
  91. };
  92.  
  93. class ListAttribute : public Attribute
  94. {
  95.    public:
  96.       ListAttribute(const Wt::WString& name,
  97.             const std::vector<Wt::WString>& list)
  98.      : Attribute(Wt::WString("Select ") + name,
  99.              Wt::WString())
  100.      , mList(new Wt::WComboBox())
  101.       {
  102.      for(size_t i=0; i<list.size(); ++i)
  103.         mList->addItem(list[i]);
  104.      mHBox->addWidget(mList, 1);
  105.       }
  106.  
  107.    private:
  108.       Wt::WComboBox* mList;
  109. };
  110.  
  111. class AttributeGroup : public Wt::WGroupBox
  112. {
  113.    public:
  114.       AttributeGroup()
  115.      : WGroupBox(Wt::WString("Attribute Group"))
  116.      , mVBox(new Wt::WVBoxLayout())
  117.      , mAttrs()
  118.      , mItemMap()
  119.      , mResetPB(new Wt::WPushButton(Wt::WString("Reset")))
  120.       {
  121.      Wt::WPopupMenu *popup = new Wt::WPopupMenu();
  122.      Wt::WLabel *menu = new Wt::WLabel(Wt::WString("Attributes"));
  123.      menu->setStyleClass("label-popupmenu");
  124.      menu->clicked().connect(popup, &Wt::WPopupMenu::popup);
  125.  
  126.      addPopupItem(popup, Wt::WString("Name"),
  127.               new NameAttribute("Name"));
  128.      addPopupItem(popup, Wt::WString("Street"),
  129.               new NameAttribute(Wt::WString("Street")));
  130.      std::vector<Wt::WString> continents;
  131.      continents.push_back(Wt::WString("Europe"));
  132.      continents.push_back(Wt::WString("Africa"));
  133.      continents.push_back(Wt::WString("Asia"));
  134.      continents.push_back(Wt::WString("America"));
  135.      addPopupItem(popup, Wt::WString("Continents"),
  136.               new ListAttribute(Wt::WString("Continent"),
  137.                     continents));
  138.      Wt::WPushButton *apply = new Wt::WPushButton(Wt::WString("Apply"));
  139.      mResetPB->clicked().connect(this, &AttributeGroup::reset);
  140.      mResetPB->disable();
  141.  
  142.      Wt::WHBoxLayout* hbox = new Wt::WHBoxLayout();
  143.      hbox->setContentsMargins(0, 0, 0, 0);
  144.      hbox->addWidget(menu, 1);
  145.      hbox->addWidget(mResetPB, 1);
  146.      hbox->addWidget(apply, 1);
  147.      mVBox->addLayout(hbox);
  148.      mVBox->setContentsMargins(0, 0, 0, 0);
  149.      setLayout(mVBox);
  150.      setStyleClass("selectionarea");
  151.       }
  152.  
  153.       void addPopupItem(Wt::WPopupMenu* popup, const Wt::WString& text, Attribute* attr)
  154.       {
  155.      Wt::WPopupMenuItem* item = popup->addItem(text);
  156.      mAttrs.push_back(attr);
  157.      item->setCheckable(true);
  158.      item->setChecked(false);
  159.      item->setData(attr);
  160.      item->triggered().connect(this, &AttributeGroup::attrChanged);
  161.      mItemMap[attr] = item;
  162.      mVBox->addWidget(attr);
  163.       }
  164.  
  165.       void attrChanged(Wt::WPopupMenuItem* item)
  166.       {
  167.      Attribute *attr = reinterpret_cast<Attribute*>(item->data());
  168.      attr->setHidden(!item->isChecked());
  169.      mResetPB->setEnabled(true);
  170.       }
  171.  
  172.       void reset()
  173.       {
  174.      for(std::list<Attribute*>::const_iterator it = mAttrs.begin(); it != mAttrs.end(); ++it)
  175.      {
  176.         Attribute* attr = (*it);
  177.         attr->setHidden(true);
  178.         mItemMap[attr]->setChecked(false);
  179.      }
  180.      mResetPB->setEnabled(false);
  181.       }
  182.  
  183.    private:
  184.       Wt::WVBoxLayout* mVBox;
  185.       std::list<Attribute*> mAttrs;
  186.       std::map<Attribute*,Wt::WPopupMenuItem*> mItemMap;
  187.       Wt::WPushButton* mResetPB;
  188. };
  189.  
  190.  
  191. WtApp::WtApp(const Wt::WEnvironment& env)
  192.    : Wt::WApplication(env)
  193.    , mLayout(0)
  194. {
  195.    setCssTheme("polished");
  196.  
  197.    mLayout = new Wt::WGridLayout(root());
  198.  
  199.    root()->setLayout(mLayout);
  200.  
  201.    // row 0
  202.    mLayout->addWidget(createText("Title"), 0, 0, 0, 3, Wt::AlignTop|Wt::AlignCenter);
  203.  
  204.  
  205.    // row 1
  206.    mLayout->addWidget(createText("SubTitle"), 1, 0, 1, 3, Wt::AlignMiddle|Wt::AlignCenter);
  207.  
  208.    // row 2
  209.    mLayout->addWidget(createSelection(), 2, 0, Wt::AlignLeft);
  210.    mLayout->addWidget(createText("Main Area"), 2, 1, Wt::AlignJustify);
  211.    mLayout->addWidget(createText("Details"), 2, 2, Wt::AlignJustify);
  212.  
  213.    // row 3
  214.    mLayout->addWidget(createText("More details"), 3, 0, 1, 3);
  215.  
  216.    /*
  217.     * Let row 2 and column 1 take the excess space.
  218.     */
  219.    mLayout->setRowStretch(2, 1);
  220.    mLayout->setRowStretch(3, 1);
  221.    mLayout->setColumnStretch(1, 1);
  222.    mLayout->setColumnStretch(2, 0);
  223.    mLayout->setColumnResizable(1);
  224.    mLayout->setRowResizable(2);
  225.    mLayout->setHorizontalSpacing(4);
  226.    mLayout->setVerticalSpacing(2);
  227.    mLayout->setContentsMargins(4, 4, 4, 4);
  228. }
  229.  
  230. Wt::WText *WtApp::createText(const Wt::WString& text)
  231. {
  232.    return new Wt::WText(text);
  233. }
  234.  
  235. Wt::WWidget *WtApp::createSelection()
  236. {
  237.    mSelections = new Wt::WContainerWidget();
  238.    Wt::WGroupBox *gbox1 = new Wt::WGroupBox("Config");
  239.    gbox1->addWidget(createText("Config selection"));
  240.    Wt::WComboBox *cbox1  = new Wt::WComboBox();
  241.    cbox1->addItem("please select");
  242.    cbox1->addItem("System");
  243.    Wt::WGroupBox *gbox2 = new Wt::WGroupBox("Selection Box");
  244.    Wt::WComboBox *cbox2  = new Wt::WComboBox();
  245.    cbox2->addItem("please select");
  246.    cbox2->addItem("Entity 1");
  247.    cbox2->activated().connect(this, &WtApp::choiceChanged);
  248.    mAttrGroup = new AttributeGroup();
  249.  
  250.    Wt::WVBoxLayout* gvbox2 = new Wt::WVBoxLayout();
  251.    gvbox2->addWidget(createText("Test 1"), 0, Wt::AlignTop);
  252.    gvbox2->addWidget(cbox2, 0, Wt::AlignTop);
  253.    gbox2->setLayout(gvbox2);
  254.  
  255.    mStretch = createText("");
  256.    mSelectionLayout = new Wt::WVBoxLayout();
  257.    mSelectionLayout->setContentsMargins(4, 2, 4, 2);
  258.    mSelectionLayout->addWidget(gbox1, 0, Wt::AlignTop);
  259.    mSelectionLayout->addWidget(cbox1, 0, Wt::AlignTop);
  260.    mSelectionLayout->addWidget(gbox2, 0, Wt::AlignTop);
  261.    mSelectionLayout->addWidget(mAttrGroup, 0, Wt::AlignTop);
  262.    mSelectionLayout->addWidget(mStretch, 1);
  263.    mSelections->setContentAlignment(Wt::AlignJustify);
  264.    mSelections->setLayout(mSelectionLayout);
  265.    mSelections->setOverflow(Wt::WContainerWidget::OverflowAuto, Wt::Horizontal);
  266.    mSelections->setStyleClass("inputarea");
  267.    mSelections->setMinimumSize(Wt::WLength(150, Wt::WLength::Pixel), Wt::WLength());
  268.    return mSelections;
  269. }
  270.  
  271. void WtApp::choiceChanged(int i)
  272. {
  273.    mSelections->removeWidget(mStretch);
  274.    mSelections->removeWidget(mAttrGroup);
  275.  
  276.    mSelectionLayout->addWidget(mAttrGroup, 0);
  277.    mSelectionLayout->addWidget(mStretch, 1);
  278. }
  279.  
  280. Wt::WApplication *createApplication(const Wt::WEnvironment& env)
  281. {
  282.    Wt::WApplication* myApplication = 0;
  283.    myApplication = new WtApp( env );
  284.    return myApplication;
  285. }
  286.  
  287.  
  288. int main(int argc, char **argv)
  289. {
  290.    Wt::WRun(argc, argv, &createApplication);
  291.    return 0;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement