Advertisement
Guest User

Wt GridLayout resizing test case

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