Project

General

Profile

Bug #1428 ยป Playground.cpp

Anonymous, 09/07/2012 03:44 PM

 

#include <Wt/WContainerWidget>
#include <Wt/WCompositeWidget>
#include <Wt/WStandardItemModel>
#include <Wt/WTableView>
#include <Wt/WVBoxLayout>
#include <Wt/WItemDelegate>
#include <Wt/WSelectionBox>
#include <Wt/WGroupBox>
#include <Wt/WLineEdit>
#include <WT/WScrollArea>
#include <Wt/WLabel>
#include <Wt/WScrollArea>
#include <Wt/WMenu>

#include <vector>
#include <string>

using namespace Wt;



// helper class for a "label value grid" inside a groupbox
class LabelValue : public WCompositeWidget {
WContainerWidget* impl;
WGridLayout* grid;
int pos;

void Init ()
{
pos = 0;

grid = new WGridLayout;
grid->setColumnStretch(1, 1);
impl->setLayout(grid);
}

public:
LabelValue (std::string title, WContainerWidget* parent = nullptr)
:WCompositeWidget(parent)
{
impl = new WGroupBox(title);
setImplementation(impl);
Init();
}

LabelValue (WContainerWidget* parent = nullptr)
:WCompositeWidget(parent)
{
impl = new WContainerWidget();
setImplementation(impl);
Init();
}

template<class T>
T* Add (std::string label)
{
auto t = new T;
Add(label, t);

return t;
}

WLabel* Add (std::string label, WFormWidget* field)
{
auto labelWidget = Add(label, static_cast<WWidget*>(field));
labelWidget->setBuddy(field);
return labelWidget;
}

WLabel* Add (std::string label, WWidget* field)
{
auto labelWidget = new WLabel(label);

grid->addWidget(labelWidget, pos, 0, 0, 0, AlignLeft | AlignMiddle);
grid->addWidget(field, pos, 1, 0, 0, AlignJustify | AlignMiddle);

++pos;
return labelWidget;
}
};



// a WPanel with a "label value grid" inside
class Panel : public WCompositeWidget {
public:
enum Status { Collapsed, Expanded };

Panel (Status status, WContainerWidget* parent = nullptr)
:WCompositeWidget(parent)
{
auto panel = new WPanel;
setImplementation(panel);

// tryed as workaround, but did not help, http://redmine.webtoolkit.eu/issues/1379
// auto impl = new WContainerWidget;
// setImplementation(impl);
// auto panel = new WPanel(impl);

panel->setTitle("a panel with layouted stuff");
panel->setCollapsible(true);
panel->setCollapsed(status == Collapsed);

auto central = new LabelValue("inside the panel");
panel->setCentralWidget(central);
auto labelValue = central;

// tryed as workaround, but did not help
// auto central = new WContainerWidget;
// panel->setCentralWidget(central);
// auto labelValue = new LabelValue("inside the panel", central);

labelValue->Add<WLineEdit>("first entry");
labelValue->Add<WLineEdit>("second entry");
labelValue->Add<WLineEdit>("third entry");
}
};



// a WPanel with some controls inside
class PanelWithoutLayout : public WCompositeWidget {
public:
enum Status { Collapsed, Expanded };

PanelWithoutLayout (Status status, WContainerWidget* parent = nullptr)
:WCompositeWidget(parent)
{
auto panel = new WPanel;
setImplementation(panel);

panel->setTitle("a panel");
panel->setCollapsible(true);
panel->setCollapsed(status == Collapsed);

auto central = new WGroupBox("inside the panel");
panel->setCentralWidget(central);

(new WLineEdit("first entry", central))->setInline(false);
(new WLineEdit("second entry", central))->setInline(false);
(new WLineEdit("third entry", central))->setInline(false);
}
};




// a dialog with layouted content
class Dialog : public WDialog {
WWidget* North ()
{
auto container = new WContainerWidget;
auto layout = new WHBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
container->setLayout(layout);

layout->addWidget(NorthLeft());
layout->addWidget(NorthRight());

return container;
}

WWidget* NorthLeft ()
{
auto container = new WContainerWidget;
auto layout = new WVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
container->setLayout(layout);

auto labelValue = new LabelValue("left");
layout->addWidget(labelValue);

labelValue->Add<WLineEdit>("first entry");
labelValue->Add<WLineEdit>("second entry");
labelValue->Add<WLineEdit>("third entry");

auto buttonsLayout = new WHBoxLayout;
layout->addLayout(buttonsLayout, 0, AlignCenter);

buttonsLayout->addWidget(new WPushButton("first"));
buttonsLayout->addWidget(new WPushButton("second"));
buttonsLayout->addWidget(new WPushButton("third"));

layout->addStretch(1);

return container;
}

WWidget* NorthRight ()
{
auto container = new WContainerWidget;
auto layout = new WVBoxLayout(container);
layout->setContentsMargins(0, 0, 0, 0);

auto labelValue = new LabelValue("right");
layout->addWidget(labelValue);
layout->addStretch(1);

labelValue->Add<WLineEdit>("first entry");
labelValue->Add<WLineEdit>("second entry");
labelValue->Add<WLineEdit>("third entry");

return container;
}

WWidget* South ()
{
auto container = new WContainerWidget;
auto layout = new WVBoxLayout(container);
layout->setContentsMargins(0, 0, 0, 0);

auto hLayout = new WHBoxLayout;
layout->addLayout(hLayout, 0, AlignCenter);

hLayout->addWidget(new WPushButton("test"));
hLayout->addWidget(new WPushButton("test"));
hLayout->addWidget(new WPushButton("test"));
hLayout->addWidget(new WPushButton("test"));
hLayout->addWidget(new WPushButton("test"));

return container;
}

public:
Dialog ()
: WDialog("example")
{
setResizable(true);

auto layout = new WVBoxLayout(contents());
layout->setContentsMargins(0, 0, 0, 0);


auto scrollingContainer = new WContainerWidget;
auto scrollingLayout = new WVBoxLayout(scrollingContainer);
scrollingLayout->setContentsMargins(0, 0, 0, 0);

auto scrollArea = new WScrollArea;
scrollArea->setWidget(scrollingContainer);

layout->addWidget(scrollArea, 1);

scrollingLayout->addWidget(North());
scrollingLayout->addWidget(new Panel(Panel::Collapsed));
scrollingLayout->addWidget(new Panel(Panel::Expanded));
scrollingLayout->addWidget(new PanelWithoutLayout(PanelWithoutLayout::Collapsed));
scrollingLayout->addWidget(new PanelWithoutLayout(PanelWithoutLayout::Expanded));
scrollingLayout->addStretch(1);


layout->addWidget(South());

resize(600, 400);
setMinimumSize(350, 150);
}
};






class Playground : public Wt::WApplication {
public:
Playground (const WEnvironment& env)
: WApplication(env)
{
auto layout = new WVBoxLayout(root());
layout->addWidget(new Panel(Panel::Collapsed));
layout->addWidget(new Panel(Panel::Expanded));
layout->addWidget(new PanelWithoutLayout(PanelWithoutLayout::Collapsed));
layout->addWidget(new PanelWithoutLayout(PanelWithoutLayout::Expanded));

auto hLayout = new WHBoxLayout;
layout->addLayout(hLayout, 0, AlignCenter);

auto button = new WPushButton("open dialog");
button->clicked().connect([] (const WMouseEvent& ) { (new Dialog)->show(); });
hLayout->addWidget(button);

layout->addStretch(1);
}


static Wt::WApplication* CreateApplication(const Wt::WEnvironment& env)
{
return new Playground(env);
}
};





int main (int argc, char **argv)
{
return WRun(argc, argv, &Playground::CreateApplication);
}



    (1-1/1)