void Tester::LoadGrammar() { string h = internalPathNextPart("/share/"); fs::path input = fs::path(appRoot()) / "share" / (h + ".data"); mInput->setText(""); mGrammar->setText(""); mResult->clear(); ifstream data_fs(input.string().c_str()); if (data_fs) { data_fs.unsetf(std::ios::skipws); string data((istream_iterator<char>(data_fs)), istream_iterator<char>()); mInput->setText(data); data_fs.close(); } else { stringstream ss; ss << "share not found: " << h; Wt::WText* t = new Wt::WText(ss.str(), mResult); t->setStyleClass("result_error"); Wt::log("info") << ss.str(); return; } fs::path grammar = fs::path(appRoot()) / "share" / (h + ".lua"); ifstream grammar_fs(grammar.string().c_str()); if (grammar_fs) { grammar_fs.unsetf(std::ios::skipws); string data((istream_iterator<char>(grammar_fs)), istream_iterator<char>()); mGrammar->setText(data); grammar_fs.close(); } }
Wt::WWidget* Tester::Input() { Wt::WContainerWidget* container = new Wt::WContainerWidget(); container->setStyleClass("input_container"); Wt::WText* t = new Wt::WText("Input", container); t->setStyleClass("area_title"); new Wt::WBreak(container); mInput = new Wt::WTextArea(container); mInput->setText("20131220T094700"); new Wt::WBreak(container); t = new Wt::WText("Grammar", container); t->setStyleClass("area_title"); new Wt::WBreak(container); mGrammar = new Wt::WTextArea(container); mGrammar->setRows(15); mGrammar->setText("local l = require 'lpeg'\nl.locale(l)\ngrammar = l.C(l.digit^-4)"); Wt::WPushButton* button = new Wt::WPushButton("Test Grammar", container); button->clicked().connect(this, &Tester::GrammarButton); button = new Wt::WPushButton("Benchmark Grammar", container); button->clicked().connect(this, &Tester::BenchmarkButton); button = new Wt::WPushButton("Share Grammar", container); button->clicked().connect(this, &Tester::ShareGrammar); return container; }
void Tester::TestGrammar(bool benchmark) { mResult->clear(); fs::path grammar = fs::path("/tmp") / (sessionId() + ".lua"); ofstream ofs(grammar.string().c_str()); if (ofs) { ofs << mGrammar->text(); ofs.close(); } else { stringstream ss; ss << "failed to open: " << grammar; Wt::WText* t = new Wt::WText(ss.str(), mResult); t->setStyleClass("result_error"); Wt::log("error") << ss.str(); return; } lua_sandbox* sb = lsb_create(NULL, grammar.string().c_str(), "modules", 8*1024*1024, 1e6, 1024*63); if (!sb) { stringstream ss; ss << "lsb_create() failed"; Wt::WText* t = new Wt::WText(ss.str(), mResult); t->setStyleClass("result_error"); Wt::log("error") << ss.str(); return; } if (lsb_init(sb, nullptr)) { stringstream ss; string error = lsb_get_error(sb); size_t pos = error.find_first_of(':'); if (pos != string::npos) { ss << "line " << error.substr(pos + 1); } else { ss << error; } Wt::WText* t = new Wt::WText(ss.str(), mResult); t->setStyleClass("result_error"); Wt::log("info") << ss.str(); return; } if (benchmark) { Benchmark(sb, mInput->text().narrow()); } else { Match(sb, mInput->text().narrow()); } char* e = lsb_destroy(sb, nullptr); if (e) { stringstream ss; ss << "lsb_destroy() failed: " << e; Wt::WText* t = new Wt::WText(ss.str(), mResult); t->setStyleClass("result_error"); Wt::log("info") << ss.str(); free(e); } }
// Static Functions /// Returns the value of pretty much all widget types static WString getValue(WFormWidget* widget) { Wt::WComboBox* asComboBox = dynamic_cast<Wt::WComboBox*>(widget); if (asComboBox != 0) return asComboBox->currentText(); Wt::WLineEdit* asLineEdit = dynamic_cast<Wt::WLineEdit*>(widget); if (asLineEdit != 0) return asLineEdit->text(); Wt::WSlider* asSlider = dynamic_cast<Wt::WSlider*>(widget); if (asSlider != 0) { std::stringstream out; out << asSlider->value(); return out.str(); } Wt::WTextArea* asTextArea = dynamic_cast<Wt::WTextArea*>(widget); if (asTextArea != 0) return asTextArea->text(); throw std::logic_error("I don't know how to get the value of whatever widget you passed me"); }
void Tester::ShareGrammar() { boost::hash<std::string> string_hash; string data(mInput->text().narrow() + mGrammar->text().narrow()); string h = boost::lexical_cast<string>(string_hash(data)); fs::path input = fs::path(appRoot()) / "share" / (h + ".data"); if (!exists(input)) { ofstream data_fs(input.string().c_str()); if (data_fs) { data_fs << mInput->text(); data_fs.close(); } else { stringstream ss; ss << "failed to open: " << input; Wt::WText* t = new Wt::WText(ss.str(), mResult); t->setStyleClass("result_error"); Wt::log("error") << ss.str(); return; } fs::path grammar = fs::path(appRoot()) / "share" / (h + ".lua"); ofstream lua_fs(grammar.string().c_str()); if (lua_fs) { lua_fs << mGrammar->text(); lua_fs.close(); } else { stringstream ss; ss << "failed to open: " << grammar; Wt::WText* t = new Wt::WText(ss.str(), mResult); t->setStyleClass("result_error"); Wt::log("error") << ss.str(); return; } } setInternalPath("/share/" + h); Wt::WMessageBox* messageBox = new Wt::WMessageBox("Share", makeAbsoluteUrl(bookmarkUrl()), Wt::Information, Wt::Ok); messageBox->buttonClicked().connect(std::bind([=]() { delete messageBox; })); messageBox->show(); }
ribi::WtAboutDialog::WtAboutDialog( const About& about_original, const bool display_close_button) : m_signal_close{}, m_button_close(new Wt::WPushButton) { About about = about_original; about.AddLibrary("Wt version: " + GetWtVersion()); about.AddLibrary("WtAboutDialog version: " + GetVersion()); this->setContentAlignment(Wt::AlignCenter); const int min_width = 800; //Display the general about text { const std::vector<std::string> v = about.CreateAboutText(); for(const auto s: v) { new Wt::WLabel(s.c_str(),this); this->addWidget(new Wt::WBreak); } } this->addWidget(new Wt::WBreak); //Display the libraries used text { Wt::WTextArea * text = new Wt::WTextArea; { const std::vector<std::string> v = about.CreateLibrariesUsedText(); std::string s; for(const auto t: v) { s+=t; s+="\n"; } text->setText(s); } text->setMinimumSize(min_width,100); text->setReadOnly(true); this->addWidget(text); } this->addWidget(new Wt::WBreak); //Display the version history { Wt::WTextArea * text = new Wt::WTextArea; { const std::vector<std::string> v = about.CreateVersionHistory(); std::string s; for(const auto t: v) { s+=t; s+="\n"; } text->setText(s); } text->setMinimumSize(min_width,100); text->setReadOnly(true); this->addWidget(text); } this->addWidget(new Wt::WBreak); //Display the licence text { Wt::WTextArea * text = new Wt::WTextArea; { const std::vector<std::string> v = about.CreateLicenceText(); std::string s; for(const auto t: v) { s+=t; s+="\n"; } text->setText(s); } text->setMinimumSize(min_width,100); text->setReadOnly(true); this->addWidget(text); } addWidget(new Wt::WBreak); { const std::string s = std::string("Source code built at ") + std::string(__DATE__) + std::string(" ") + std::string(__TIME__); new Wt::WLabel(s.c_str(),this); this->addWidget(new Wt::WBreak); } if (display_close_button) { this->addWidget(new Wt::WBreak); this->addWidget(m_button_close); m_button_close->setText("Close"); m_button_close->clicked().connect( this,&ribi::WtAboutDialog::OnClose); } }
// inline constructor UserFormView() { model_ = new UserFormModel(this); setTemplateText(tr("userForm-template")); addFunction("id", &WTemplate::Functions::id); /* * First Name */ setFormWidget(UserFormModel::FirstNameField, new Wt::WLineEdit()); /* * Last Name */ setFormWidget(UserFormModel::LastNameField, new Wt::WLineEdit()); /* * Country */ Wt::WComboBox *countryCB = new Wt::WComboBox(); countryCB->setModel(model_->countryModel()); countryCB->activated().connect(std::bind([=] () { std::string code = model_->countryCode(countryCB->currentIndex()); model_->updateCityModel(code); })); setFormWidget(UserFormModel::CountryField, countryCB, [=] () { // updateViewValue() std::string code = boost::any_cast<std::string> (model_->value(UserFormModel::CountryField)); int row = model_->countryModelRow(code); countryCB->setCurrentIndex(row); }, [=] () { // updateModelValue() std::string code = model_->countryCode(countryCB->currentIndex()); model_->setValue(UserFormModel::CountryField, code); }); /* * City */ Wt::WComboBox *cityCB = new Wt::WComboBox(); cityCB->setModel(model_->cityModel()); setFormWidget(UserFormModel::CityField, cityCB); /* * Birth Date */ Wt::WLineEdit *dateEdit = new Wt::WLineEdit(); Wt::WDatePicker *birthDP = new Wt::WDatePicker(dateEdit); bindWidget("birth-dp", birthDP); setFormWidget(UserFormModel::BirthField, dateEdit, [=] () { // updateViewValue() Wt::WDate date = boost::any_cast<Wt::WDate> (model_->value(UserFormModel::BirthField)); birthDP->setDate(date); }, [=] () { // updateModelValue() Wt::WDate date = birthDP->date(); model_->setValue(UserFormModel::BirthField, date); }); /* * Children */ setFormWidget(UserFormModel::ChildrenField, new Wt::WSpinBox()); /* * Remarks */ Wt::WTextArea *remarksTA = new Wt::WTextArea(); remarksTA->setColumns(40); remarksTA->setRows(5); setFormWidget(UserFormModel::RemarksField, remarksTA); /* * Title & Buttons */ Wt::WString title = Wt::WString("Create new user"); bindString("title", title); Wt::WPushButton *button = new Wt::WPushButton("Save"); bindWidget("submit-button", button); bindString("submit-info", Wt::WString()); button->clicked().connect(this, &UserFormView::process); updateView(model_); }
Wt::WContainerWidget *nav_bar() { // Main Container holding navbar and content Wt::WContainerWidget *container = new Wt::WContainerWidget(); // Content stack shows content, one at a time Wt::WStackedWidget *contentsStack = new Wt::WStackedWidget(container); contentsStack->addStyleClass("container"); // Container holding content for layout purposes Wt::WContainerWidget *container2 = new Wt::WContainerWidget(); // Create a NAVBAR Wt::WNavigationBar *navigation = new Wt::WNavigationBar(container); navigation->setTitle("Halo Viewer", "http://haloviewer.bluewizard.ca/"); navigation->setResponsive(true); navigation->addStyleClass("navbar-fixed-top"); navigation->addStyleClass("navbar-inner"); // Setup a Left-aligned menu. Wt::WMenu *leftMenu = new Wt::WMenu(contentsStack, container2); navigation->addMenu(leftMenu, Wt::AlignCenter); Wt::WText *searchResult = new Wt::WText("Buy or Sell... Bye!"); // Create Spartan profile SpartanProfile *mySpartan = new SpartanProfile(); // Create Profile view container Wt::WContainerWidget *profile = new Wt::WContainerWidget(); profile->addStyleClass("summary-container"); // User summary template (emblem, name, rank etc.) Wt::WTemplate *t = new Wt::WTemplate("<div class=\"summary-container\">\n<img src=\"${emblem-url}\" class=\"emblem-image\" />\n<div class=\"spartan-name\">${spartan-name}</div>\n<div class=\"spartan-rank\">SR ${spartan-rank}</div>\n</div>"); std::string emblem_url("https://image.halocdn.com/h5/emblems/311_0_31_48?width=256&hash=%2blRdfGUDXqlSen9Z1eRb8OoX%2fqYz9zaLqu3TNgYxffs%3d"); boost::replace_all(emblem_url, "&", "&"); t->bindString("emblem-url", emblem_url); t->bindWidget("spartan-name", new Wt::WText("CLWakaLaka")); t->bindWidget("spartan-rank", new Wt::WText("SR 74")); profile->addWidget(t); // Ranks Container shows playlist ranks for a given SpartanProfile Wt::WContainerWidget *summary_layout = new Wt::WContainerWidget(); PlaylistRanksContainer *ranks = new PlaylistRanksContainer(mySpartan); Wt::WContainerWidget *panels = new Wt::WContainerWidget(); panels->setWidth("60%"); Wt::WPanel *panel = new Wt::WPanel(); panel->setTitle("Collapsible panel"); panel->addStyleClass("centered-example"); panel->setCollapsible(true); Wt::WAnimation animation(Wt::WAnimation::SlideInFromTop, Wt::WAnimation::EaseOut, 100); panel->setAnimation(animation); Wt::WText *temp_text = new Wt::WText(mySpartan->printRecentMatches()); panel->setCentralWidget(temp_text); panels->addWidget(panel); RecentMatchesContainer *recent_matches_panel = new RecentMatchesContainer(mySpartan); ranks->setFloatSide(Wt::Left); recent_matches_panel->setFloatSide(Wt::Left); summary_layout->addWidget(ranks); summary_layout->addWidget(recent_matches_panel); // Create TabWidget for profile view Wt::WTabWidget *tabW = new Wt::WTabWidget(profile); Wt::WTextArea *text = new Wt::WTextArea(mySpartan->getRanks()); tabW->addTab(summary_layout, "Summary", Wt::WTabWidget::PreLoading); tabW->addTab(text, "Weapons", Wt::WTabWidget::PreLoading); tabW->addTab(new Wt::WTextArea("You could change any other style attribute of the" " tab widget by modifying the style class." " The style class 'trhead' is applied to this tab."), "Medals", Wt::WTabWidget::PreLoading)->setStyleClass("trhead"); tabW->setStyleClass("tabwidget"); // Add containers to Left Menu leftMenu->addItem("Profile", profile); leftMenu->addItem("Matches", new Wt::WText("Layout contents")); leftMenu->addItem("Sales", searchResult); // Setup a Right-aligned menu. Wt::WMenu *rightMenu = new Wt::WMenu(); navigation->addMenu(rightMenu, Wt::AlignRight); // Create a popup submenu for the Help menu. Wt::WPopupMenu *popup = new Wt::WPopupMenu(); popup->addItem("Contents"); popup->addItem("Index"); popup->addSeparator(); popup->addItem("About"); panel->expanded().connect(std::bind([=] () { temp_text->setText(mySpartan->printRecentMatches()); })); popup->itemSelected().connect(std::bind([=] (Wt::WMenuItem *item) { Wt::WMessageBox *messageBox = new Wt::WMessageBox ("Help", Wt::WString::fromUTF8("<p>Showing Help: {1}</p>").arg(item->text()), Wt::Information, Wt::Ok); messageBox->buttonClicked().connect(std::bind([=] () { delete messageBox; })); messageBox->show(); }, std::placeholders::_1)); Wt::WMenuItem *item = new Wt::WMenuItem("Help"); item->setMenu(popup); rightMenu->addItem(item); // Add a Search control. Wt::WLineEdit *edit = new Wt::WLineEdit(); edit->setEmptyText("Enter a search item"); edit->enterPressed().connect(std::bind([=] () { leftMenu->select(0); // is the index of the "Sales" mySpartan->updateProfile(edit->text().toUTF8()); mySpartan->printRanks(); std::cout << mySpartan->getGamertag() << std::endl; std::cout << mySpartan->getEmblem() << std::endl; if (boost::algorithm::contains(mySpartan->getEmblem(), "https:")) { // Update summary template std::string tmp_url = mySpartan->getEmblem(); boost::replace_all(tmp_url, "&", "&"); t->bindString("emblem-url", tmp_url); t->bindString("spartan-name", mySpartan->getGamertag()); t->bindString("spartan-rank", std::to_string(mySpartan->getSpartanRank())); // Update summary tab //tabW->widget(0)->setText(new Wt::WString(mySpartan->getRanks())); text->setText(mySpartan->getRanks()); ranks->update(); recent_matches_panel->update(); } })); navigation->addSearch(edit, Wt::AlignRight); container->addWidget(contentsStack); return container; }