void FormLayoutMatrix::addFormWidget(AbstractFormWidget *fw) { int row, column; bool spaceAvailable = findFreeSpace(fw->getWidthUnits(), fw->getHeightUnits(), row, column); if (spaceAvailable) { setFormWidget(fw, row, column); } else { createFreeSpace(fw->getWidthUnits(), fw->getHeightUnits(), row, column); setFormWidget(fw, row, column); } }
void FormLayoutMatrix::setFormWidget(AbstractFormWidget *fw, int row, int column) { //check boundaries if ((row < m_rows) && (column < m_columns)) { int maxRow = row + fw->getHeightUnits() - 1; int maxColumn = column + fw->getWidthUnits() - 1; //check if the widget fits in current matrix size //if not grow the matrix to fit the mac row/column if ((maxRow >= m_rows) || (maxColumn >= m_columns)) { int rowDelta = maxRow - m_rows + 1; //+1 because row count starts by 1 int columnDelta = maxColumn - m_columns + 1; //+1 because cols count starts by 1 growMatrixSize(m_rows + rowDelta, m_columns + columnDelta); } //first mark all matrix cells which are part of the form widget for (int i = row; i < (row + fw->getHeightUnits()); i++) { for (int j = column; j < (column + fw->getWidthUnits()); j++) { m_matrix[i].replace(j,(FormWidget*)EXTENDED_FORM_WIDGET); } } //set the first row/column that belong to the FW with its address m_matrix[row].replace(column, fw); } else { //increment matrix size //grow function expects row/column count growMatrixSize(row + fw->getHeightUnits(), column + fw->getWidthUnits()); setFormWidget(fw, row, column); } }
void FormLayoutMatrix::formWidgetResize(AbstractFormWidget *fw, int newWidthUnits, int newHeightUnits) { //get current index int row, column; findFormWidgetIndex(fw, row, column); //list of widget that should be removed, cause they are on the way QList<FormWidget*> widgetsToRemove; //list of widget that should be added again to the matrix QList<FormWidget*> widgetsToAdd; //mark all widgets for removal that are in the way with the new FW and its new size //also remove current FW (selected one) to add it again later with new size int maxRow = row + newHeightUnits - 1; int maxColumn = column + newWidthUnits - 1; for (int r = row; ((r <= maxRow) && (r < m_rows)); r++) { for (int c = column; ((c <= maxColumn) && (c < m_columns)); c++) { FormWidget *f = m_matrix[r][c]; if (f != (FormWidget*)FormLayoutMatrix::NO_FORM_WIDGET) { //if cell is an extension and part of another big FW, get its parent if (f == (FormWidget*)FormLayoutMatrix::EXTENDED_FORM_WIDGET) f = getFormWidgetByExtended(r, c); //make sure the widget was not already processed (cause of extended call) if (!widgetsToRemove.contains(f)) { widgetsToRemove.append(f); //rm to make room //the selected fw is not added now, because it is added manually with setFormWidget method if (f != fw) { widgetsToAdd.append(f); //add it later again } } } } } //remove marked widgets for (int i = 0; i < widgetsToRemove.size(); i++) { int rowIndex, columnIndex; FormWidget *f = widgetsToRemove.at(i); findFormWidgetIndex(f, rowIndex, columnIndex); removeFormWidget(rowIndex, columnIndex); } //set the selected widget at the same position but with its new size fw->setWidthUnits(newWidthUnits); fw->setHeightUnits(newHeightUnits); setFormWidget(fw, row, column); //add previously removed widgets again for (int i = 0; i < widgetsToAdd.size(); i++) { addFormWidget(widgetsToAdd.at(i)); } //remove empty rows/cols if they exist simplifyMatrix(); }
void FormLayoutMatrix::formWidgetMovement(AbstractFormWidget *fw, int newRow, int newColumn) { //list of widget that should be removed from the current index QList<FormWidget*> widgetsToRemove; //list of widget that should be added again to the matrix QList<FormWidget*> widgetsToAdd; //remove dragged widget from its original index widgetsToRemove.append(fw); //remove widget (old widget if any) on target index and (eventually if any) widgets //on index + units needed to make sure the dragged widget has enough room int maxRow = newRow + fw->getHeightUnits() - 1; int maxColumn = newColumn + fw->getWidthUnits() - 1; for (int r = newRow; ((r <= maxRow) && (r < m_rows)); r++) { for (int c = newColumn; ((c <= maxColumn) && (c < m_columns)); c++) { FormWidget *f = m_matrix[r][c]; if (f != (FormWidget*)FormLayoutMatrix::NO_FORM_WIDGET) { //if cell is an extension and part of another big FW, get its parent if (f == (FormWidget*)FormLayoutMatrix::EXTENDED_FORM_WIDGET) f = getFormWidgetByExtended(r, c); //if the widget is not the dragged one and is not already processed //(this is possible cause of getFWbyExtended call), remove it //and add it back again at a convenient position if ((f != fw) && (!widgetsToRemove.contains(f))) { widgetsToRemove.append(f); //rm to make room widgetsToAdd.append(f); //add it later again } } } } //remove marked widgets for (int i = 0; i < widgetsToRemove.size(); i++) { int rowIndex, columnIndex; FormWidget *f = widgetsToRemove.at(i); findFormWidgetIndex(f, rowIndex, columnIndex); removeFormWidget(rowIndex, columnIndex); } //set the currently dragged FW to the drop position setFormWidget(fw, newRow, newColumn); //add previously removed widgets again for (int i = 0; i < widgetsToAdd.size(); i++) { addFormWidget(widgetsToAdd.at(i)); } //remove empty rows/cols if they exist simplifyMatrix(); }
ShareCreatePassword::ShareCreatePassword() { auto model = std::make_shared<ShareCreatePasswordFormModel>(); setTemplateText(tr("template-share-create-password")); addFunction("id", &WTemplate::Functions::id); addFunction("block", &WTemplate::Functions::id); // Password auto password = std::make_unique<Wt::WLineEdit>(); password->setEchoMode(Wt::EchoMode::Password); setFormWidget(ShareCreatePasswordFormModel::PasswordField, std::move(password)); // Buttons Wt::WPushButton* unlockBtn = bindNew<Wt::WPushButton>("unlock-btn", tr("msg-unlock")); unlockBtn->clicked().connect([=] { updateModel(model.get()); if (model->validate()) { FS_LOG(UI, DEBUG) << "Create password validation OK"; success().emit(); return; } FS_LOG(UI, DEBUG) << "Create password validation failed"; // Mitigate brute force attemps sleep(1); updateView(model.get()); }); updateView(model.get()); }
// 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_); }
// inline constructor UserFormView() { model = std::make_shared<UserFormModel>(); setTemplateText(tr("userForm-template")); addFunction("id", &WTemplate::Functions::id); addFunction("block", &WTemplate::Functions::id); /* * First Name */ setFormWidget(UserFormModel::FirstNameField, Wt::cpp14::make_unique<Wt::WLineEdit>()); /* * Last Name */ setFormWidget(UserFormModel::LastNameField, Wt::cpp14::make_unique<Wt::WLineEdit>()); /* * Country */ auto countryCB = Wt::cpp14::make_unique<Wt::WComboBox>(); auto countryCB_ = countryCB.get(); countryCB->setModel(model->countryModel()); countryCB_->activated().connect([=] { std::string code = model->countryCode(countryCB_->currentIndex()); model->updateCityModel(code); }); setFormWidget(UserFormModel::CountryField, std::move(countryCB), [=] { // updateViewValue() std::string code = Wt::asString(model->value(UserFormModel::CountryField)).toUTF8(); int row = model->countryModelRow(code); countryCB_->setCurrentIndex(row); }, [=] { // updateModelValue() std::string code = model->countryCode(countryCB_->currentIndex()); model->setValue(UserFormModel::CountryField, code); }); /* * City */ auto cityCB = Wt::cpp14::make_unique<Wt::WComboBox>(); cityCB->setModel(model->cityModel()); setFormWidget(UserFormModel::CityField, std::move(cityCB)); /* * Birth Date */ auto dateEdit = Wt::cpp14::make_unique<Wt::WDateEdit>(); auto dateEdit_ = dateEdit.get(); setFormWidget(UserFormModel::BirthField, std::move(dateEdit), [=] { // updateViewValue() Wt::WDate date = Wt::cpp17::any_cast<Wt::WDate> (model->value(UserFormModel::BirthField)); dateEdit_->setDate(date); }, [=] { // updateModelValue() Wt::WDate date = dateEdit_->date(); model->setValue(UserFormModel::BirthField, date); }); /* * Children */ setFormWidget(UserFormModel::ChildrenField, Wt::cpp14::make_unique<Wt::WSpinBox>()); /* * Remarks */ auto remarksTA = Wt::cpp14::make_unique<Wt::WTextArea>(); remarksTA->setColumns(40); remarksTA->setRows(5); setFormWidget(UserFormModel::RemarksField, std::move(remarksTA)); /* * Title & Buttons */ Wt::WString title = Wt::WString("Create new user"); bindString("title", title); auto button = Wt::cpp14::make_unique<Wt::WPushButton>("Save"); auto button_ = bindWidget("submit-button", std::move(button)); bindString("submit-info", Wt::WString()); button_->clicked().connect(this, &UserFormView::process); updateView(model.get()); }