Wt examples: FileEditDialog Class Reference

Wt examples 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
FileEditDialog Class Reference

A dialog for editing a 'file'. More...

Inheritance diagram for FileEditDialog:
[legend]

## Public Member Functions
| FileEditDialog (std::shared_ptr< WAbstractItemModel > model, const WModelIndex &item)

## Private Member Functions
void | handleFinish (DialogCode result)

## Private Attributes
std::shared_ptr< WAbstractItemModel > | model_
WModelIndex | item_
WLineEdit * | nameEdit_
WLineEdit * | sizeEdit_
WComboBox * | typeEdit_
WDatePicker * | createdPicker_
WDatePicker * | modifiedPicker_

## Detailed Description

A dialog for editing a 'file'.

Definition at line 78 of file TreeViewDragDrop.C.

## Constructor & Destructor Documentation

## ◆ FileEditDialog()

FileEditDialog::FileEditDialog | ( | std::shared_ptr< WAbstractItemModel > | model,
|  | const WModelIndex & | item )
| inline

Definition at line 81 of file TreeViewDragDrop.C.

82 : WDialog("Edit..."),
83 model_(model),
84 item_(item)
85 {
86 int modelRow = item_.row();
87
88 resize(300, WLength::Auto);
89
90 /*
91 * Create the form widgets, and load them with data from the model.
92 */
93
94 // name
95 auto nameEdit = std::make_unique<WLineEdit>(asString(model_->data(modelRow, 1)));
96 nameEdit_ = nameEdit.get();
97
98 // type
99 auto typeEdit = std::make_unique<WComboBox>();
100 typeEdit_ = typeEdit.get();
101 typeEdit_->addItem("Document");
102 typeEdit_->addItem("Spreadsheet");
103 typeEdit_->addItem("Presentation");
104 typeEdit_->setCurrentIndex
105 (typeEdit_->findText(asString(model_->data(modelRow, 2))));
106
107 // size
108 auto sizeEdit = std::make_unique<WLineEdit>(asString(model_->data(modelRow, 3)));
109 sizeEdit_ = sizeEdit.get();
110 sizeEdit_->setValidator
111 (std::make_shared<WIntValidator>(0, std::numeric_limits<int>::max()));
112
113
114 // created
115 auto createdPicker = std::make_unique<WDatePicker>();
116 createdPicker_ = createdPicker.get();
117 createdPicker_->lineEdit()->validator()->setMandatory(true);
118 createdPicker_->setFormat(FileModel::dateEditFormat);
119 createdPicker_->setDate(cpp17::any_cast<WDate>(model_->data(modelRow, 4)));
120
121 // modified
122 auto modifiedPicker = std::make_unique<WDatePicker>();
123 modifiedPicker_ = modifiedPicker.get();
124 modifiedPicker_->lineEdit()->validator()->setMandatory(true);
125 modifiedPicker_->setFormat(FileModel::dateEditFormat);
126 modifiedPicker_->setDate(cpp17::any_cast<WDate>(model_->data(modelRow, 5)));
127
128 /*
129 * Use a grid layout for the labels and fields
130 */
131 auto layout = std::make_unique<WGridLayout>();
132
133 std::unique_ptr<WLabel> label;
134 int row = 0;
135
136 label = std::make_unique<WLabel>("Name:");
137 label->setBuddy(nameEdit_);
138 layout->addWidget(std::move(label), row, 0);
139 layout->addWidget(std::move(nameEdit), row, 1);
140 ++row;
141
142 label = std::make_unique<WLabel>("Type:");
143 label->setBuddy(typeEdit_);
144 layout->addWidget(std::move(label), row, 0);
145 layout->addWidget(std::move(typeEdit), row, 1);
146 ++row;
147
148 label = std::make_unique<WLabel>("Size");
149 label->setBuddy(sizeEdit_);
150 layout->addWidget(std::move(label), row, 0);
151 layout->addWidget(std::move(sizeEdit), row, 1);
152 ++row;
153
154 label = std::make_unique<WLabel>("Created:");
155 label->setBuddy(createdPicker_->lineEdit());
156 layout->addWidget(std::move(label), row, 0);
157 layout->addWidget(std::move(createdPicker), row, 2);
158 ++row;
159
160 label = std::make_unique<WLabel>("Modified:");
161 label->setBuddy(modifiedPicker_->lineEdit());
162 layout->addWidget(std::move(label), row, 0);
163 layout->addWidget(std::move(modifiedPicker), row, 2);
164 ++row;
165
166 std::unique_ptr<WPushButton>button;
167 auto buttons = std::make_unique<WContainerWidget>();
168
169 button = std::make_unique<WPushButton>("Save");
170 button->clicked().connect(this, &WDialog::accept);
171 buttons->addWidget(std::move(button));
172
173 button = std::make_unique<WPushButton>("Cancel");
174 contents()->enterPressed().connect(this, &WDialog::accept);
175 button->clicked().connect(this, &WDialog::reject);
176 buttons->addWidget(std::move(button));
177
178 /*
179 * Focus the form widget that corresonds to the selected item.
180 */
181 switch (item.column()) {
182 case 2:
183 typeEdit_->setFocus(); break;
184 case 3:
185 sizeEdit_->setFocus(); break;
186 case 4:
187 createdPicker_->lineEdit()->setFocus();
188 break;
189 case 5:
190 modifiedPicker_->lineEdit()->setFocus();
191 break;
192 default:
193 nameEdit_->setFocus(); break;
194 }
195
196 layout->addWidget(std::move(buttons), row, 0, 0, 3, AlignmentFlag::Center);
197 layout->setColumnStretch(1, 1);
198
199 contents()->setLayout(std::move(layout));
200
201 finished().connect(this, &FileEditDialog::handleFinish);
202
203 show();
204 }
FileEditDialog::handleFinish
void handleFinish(DialogCode result)
Definition TreeViewDragDrop.C:214
FileEditDialog::item_
WModelIndex item_
Definition TreeViewDragDrop.C:208
FileEditDialog::nameEdit_
WLineEdit * nameEdit_
Definition TreeViewDragDrop.C:210
FileEditDialog::model_
std::shared_ptr< WAbstractItemModel > model_
Definition TreeViewDragDrop.C:207
FileEditDialog::sizeEdit_
WLineEdit * sizeEdit_
Definition TreeViewDragDrop.C:210
FileEditDialog::createdPicker_
WDatePicker * createdPicker_
Definition TreeViewDragDrop.C:212
FileEditDialog::modifiedPicker_
WDatePicker * modifiedPicker_
Definition TreeViewDragDrop.C:212
FileEditDialog::typeEdit_
WComboBox * typeEdit_
Definition TreeViewDragDrop.C:211
FileModel::dateEditFormat
static WString dateEditFormat
Date edit format.
Definition TreeViewDragDrop.C:69

## Member Function Documentation

## ◆ handleFinish()

void FileEditDialog::handleFinish | ( | DialogCode | result | ) |
| inlineprivate

Definition at line 214 of file TreeViewDragDrop.C.

215 {
216 if (result == DialogCode::Accepted) {
217 /*
218 * Update the model with data from the edit widgets.
219 *
220 * You will want to do some validation here...
221 *
222 * Note that we directly update the source model to avoid
223 * problems caused by the dynamic sorting of the proxy model,
224 * which reorders row numbers, and would cause us to switch to editing
225 * the wrong data.
226 */
227 std::shared_ptr<WAbstractItemModel> m = model_;
228 int modelRow = item_.row();
229
230 std::shared_ptr<WAbstractProxyModel> proxyModel =
231 std::dynamic_pointer_cast<WAbstractProxyModel>(m);
232 if (proxyModel) {
233 m = proxyModel->sourceModel();
234 modelRow = proxyModel->mapToSource(item_).row();
235 }
236
237 m->setData(modelRow, 1, cpp17::any(nameEdit_->text()));
238 m->setData(modelRow, 2, cpp17::any(typeEdit_->currentText()));
239 m->setData(modelRow, 3, cpp17::any(asNumber
240 (sizeEdit_->text().toUTF8())));
241 m->setData(modelRow, 4, cpp17::any(createdPicker_->date()));
242 m->setData(modelRow, 5, cpp17::any(modifiedPicker_->date()));
243 }
244
245 //delete this;
246 }

## Member Data Documentation

## ◆ createdPicker_

WDatePicker* FileEditDialog::createdPicker_
| private

Definition at line 212 of file TreeViewDragDrop.C.

## ◆ item_

WModelIndex FileEditDialog::item_
| private

Definition at line 208 of file TreeViewDragDrop.C.

## ◆ model_

std::shared_ptr<WAbstractItemModel> FileEditDialog::model_
| private

Definition at line 207 of file TreeViewDragDrop.C.

## ◆ modifiedPicker_

WDatePicker * FileEditDialog::modifiedPicker_
| private

Definition at line 212 of file TreeViewDragDrop.C.

## ◆ nameEdit_

WLineEdit* FileEditDialog::nameEdit_
| private

Definition at line 210 of file TreeViewDragDrop.C.

## ◆ sizeEdit_

WLineEdit * FileEditDialog::sizeEdit_
| private

Definition at line 210 of file TreeViewDragDrop.C.

## ◆ typeEdit_

WComboBox* FileEditDialog::typeEdit_
| private

Definition at line 211 of file TreeViewDragDrop.C.

The documentation for this class was generated from the following file:

- treeview-dragdrop/TreeViewDragDrop.C