Advertisement
Guest User

Untitled

a guest
Sep 25th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.77 KB | None | 0 0
  1.  
  2. #include <string>
  3. #include <memory>
  4.  
  5. #include <Wt/WApplication>
  6. #include <Wt/WContainerWidget>
  7. #include <Wt/WTreeView>
  8. #include <Wt/WText>
  9. #include <Wt/WLineEdit>
  10. #include <Wt/WStandardItemModel>
  11. #include <Wt/WStandardItem>
  12.  
  13.  
  14.  
  15. std::string loremIpsum("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.");
  16.  
  17.  
  18.  
  19.    class EntryFieldWithListExample : public WCompositeWidget {
  20.       WLineEdit* edit;
  21.       std::unique_ptr<WTreeView> table;
  22.       WTimer* blurDelay;
  23.       bool added;
  24.  
  25.       void OnFocused ()
  26.       {
  27.          if (blurDelay) delete blurDelay;
  28.          blurDelay = nullptr;
  29.  
  30.          if (!added) wApp->domRoot()->addWidget(table.get());
  31.          added = true;
  32.          //layout->addWidget(table, 1);
  33.  
  34.          table->show();
  35.          table->positionAt(edit);
  36.       }
  37.  
  38.       void OnBlurred ()
  39.       {
  40.          if (blurDelay) delete blurDelay;
  41.          blurDelay = new WTimer;
  42.          blurDelay->setSingleShot(true);
  43.          blurDelay->setInterval(100);
  44.          blurDelay->timeout().connect(this, &EntryFieldWithListExample::OnBlurDelayReached);
  45.          blurDelay->start();
  46.       }
  47.  
  48.       void OnBlurDelayReached ()
  49.       {
  50.          if (blurDelay) delete blurDelay;
  51.          blurDelay = nullptr;
  52.  
  53.          table->hide();
  54.       }
  55.  
  56.       void OnItemClicked (const WModelIndex& index)
  57.       {
  58.          edit->setText(asString(index.data(DisplayRole)));
  59.          table->hide();
  60.       }
  61.  
  62.       public:
  63.          EntryFieldWithListExample (WContainerWidget* parent = nullptr)
  64.             :WCompositeWidget(parent), edit(nullptr), blurDelay(nullptr), added(false)
  65.          {
  66.             WContainerWidget* impl = nullptr;
  67.             setImplementation(impl = new WContainerWidget);
  68.  
  69.             WVBoxLayout* layout = new WVBoxLayout(impl);
  70.             layout->setContentsMargins(0, 0, 0, 0);
  71.             layout->addWidget(edit = new WLineEdit);
  72.             edit->setStyleClass("Wt-suggest-dropdown");
  73.  
  74.             table.reset(new WTreeView);
  75.             table->setHeaderHeight(0);
  76.             table->setAlternatingRowColors(true);
  77.             table->setPositionScheme(Absolute);
  78.             table->setPopup(true);
  79.             table->decorationStyle().setBackgroundColor(WColor("Window"));
  80.             table->decorationStyle().setBorder(WBorder(WBorder::Groove, 1, WColor("WindowFrame")));
  81.  
  82.             const int rows = 10000;
  83.             const int columns = 4;
  84.             WStandardItemModel *model = new WStandardItemModel(rows, columns);
  85.             for (int row = 0; row < rows; ++row) {
  86.               for (int column = 0; column < columns; ++column) {
  87.                 WStandardItem *item = new WStandardItem();
  88.                 item->setText("Item " + boost::lexical_cast<std::string>(row)
  89.                               + ", " + boost::lexical_cast<std::string>(column));
  90.                 model->setItem(row, column, item);
  91.               }
  92.             }
  93.             table->setModel(model);
  94.  
  95.             table->resize(600, 250);
  96.             table->hide(); // must be the last call, or the content will not get visible
  97.  
  98.             edit->focussed().connect(this, &EntryFieldWithListExample::OnFocused);
  99.             edit->blurred().connect(this, &EntryFieldWithListExample::OnBlurred);
  100.             table->clicked().connect(this, &EntryFieldWithListExample::OnItemClicked);
  101.          }
  102.    };
  103.  
  104.  
  105.  
  106.    class OpenDialogWidget : public WCompositeWidget {
  107.       std::unique_ptr<WDialog> dlg;
  108.  
  109.       void OpenDialog ()
  110.       {
  111.          dlg.reset(new WDialog("titel"));
  112.          dlg->setClosable(true);
  113.  
  114.          auto layout = new WVBoxLayout(dlg->contents());
  115.          layout->addWidget(new WText(loremIpsum));
  116.          layout->addWidget(new EntryFieldWithListExample);
  117.          layout->addWidget(new WText(loremIpsum));
  118.          layout->addStretch(1);
  119.  
  120.          dlg->resize(800, 600);
  121.          dlg->show();
  122.       }
  123.  
  124.    public:
  125.       OpenDialogWidget (WContainerWidget* parent = nullptr)
  126.       : WCompositeWidget(parent)
  127.       {
  128.          auto impl = new WContainerWidget;
  129.          setImplementation(impl);
  130.  
  131.          auto layout = new WHBoxLayout(impl);
  132.          auto btn = new WPushButton("open dialog");
  133.          layout->addWidget(btn);
  134.  
  135.          btn->clicked().connect(this, &OpenDialogWidget::OpenDialog);
  136.       }
  137.    };
  138.  
  139.  
  140.  
  141.    class Application : public WApplication {
  142.       public:
  143.          Application::Application (const WEnvironment& env)
  144.          : WApplication(env)
  145.          {
  146.             auto stretch = new WVBoxLayout(root());
  147.  
  148.             auto scroll = new WScrollArea;
  149.             stretch->addWidget(scroll);
  150.  
  151.             auto container = new WContainerWidget;
  152.             scroll->setWidget(container);
  153.  
  154.             auto layout = new WVBoxLayout(container);
  155.             layout->addWidget(new WText(loremIpsum));
  156.             layout->addWidget(new EntryFieldWithListExample);
  157.             layout->addWidget(new WText(loremIpsum));
  158.             layout->addWidget(new OpenDialogWidget);
  159.             layout->addStretch(1);
  160.          }
  161.  
  162.          static Wt::WApplication* CreateApplication(const Wt::WEnvironment& env)
  163.          {
  164.             return new Application(env);
  165.          }
  166.    };
  167.  
  168.    int main (int argc, char** argv)
  169.    {
  170.       return WRun(argc, argv, &Application::CreateApplication);
  171.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement