Advertisement
Guest User

Wt 3.2.3-rc1 Layout maximum width violation

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