Редакция 101 | Редакция 103 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
88 | pingvin | 1 | #include "mainwindow.h" |
2 | #include "ui_mainwindow.h" |
||
3 | |||
4 | MainWindow::MainWindow(QWidget *parent) : |
||
5 | QMainWindow(parent), |
||
6 | ui(new Ui::MainWindow) |
||
7 | { |
||
8 | ui->setupUi(this); |
||
9 | |||
89 | pingvin | 10 | MainWindow::readModel(); |
11 | |||
91 | pingvin | 12 | //QString model_str; |
13 | MainWindow::creatModelString(); |
||
14 | int i; |
||
15 | i++; |
||
93 | pingvin | 16 | QFile file_tmp("./temp.txt"); |
92 | pingvin | 17 | bool ok; |
18 | ok = file_tmp.open(QIODevice::ReadWrite | QIODevice::Text); |
||
19 | QTextStream out(&file_tmp); |
||
20 | out << MainWindow::modelString; |
||
21 | file_tmp.close(); |
||
91 | pingvin | 22 | |
93 | pingvin | 23 | MainWindow::openDataBase(); |
101 | pingvin | 24 | MainWindow::buildPreviewModel(tr(" "), tr("7")); |
102 | pingvin | 25 | MainWindow::getClassList(); |
93 | pingvin | 26 | |
88 | pingvin | 27 | } |
28 | |||
29 | MainWindow::~MainWindow() |
||
30 | { |
||
31 | delete ui; |
||
32 | } |
||
33 | |||
34 | void MainWindow::changeEvent(QEvent *e) |
||
35 | { |
||
36 | QMainWindow::changeEvent(e); |
||
37 | switch (e->type()) { |
||
38 | case QEvent::LanguageChange: |
||
39 | ui->retranslateUi(this); |
||
40 | break; |
||
41 | default: |
||
42 | break; |
||
43 | } |
||
44 | } |
||
89 | pingvin | 45 | |
46 | |||
47 | bool MainWindow::readModel(){ |
||
48 | bool result; |
||
49 | QString model_str; |
||
50 | QFile file(":/model.txt"); |
||
92 | pingvin | 51 | |
91 | pingvin | 52 | rootItem1 = new QTreeWidgetItem(ui->treeWidget); |
89 | pingvin | 53 | rootItem1->setText(0, tr(" 1")); |
54 | // rootItem2 = new QTreeWidgetItem(rootItem1); |
||
55 | // rootItem2->setText(0, tr(" 2")); |
||
56 | rootItem1->setCheckState(0,Qt::Checked); |
||
57 | // rootItem2->setCheckState(0,Qt::Checked); |
||
58 | |||
59 | |||
60 | |||
61 | result = file.open(QIODevice::ReadOnly); |
||
62 | if (result) { |
||
90 | pingvin | 63 | model_str = QString(tr(file.readAll())); |
89 | pingvin | 64 | } |
65 | |||
91 | pingvin | 66 | setupModelData(model_str.split(QString("\n")), rootItem1); |
89 | pingvin | 67 | |
68 | |||
69 | return result; |
||
70 | |||
71 | } |
||
72 | |||
73 | |||
74 | void MainWindow::setupModelData(const QStringList &lines, QTreeWidgetItem *parent) |
||
75 | { |
||
76 | QList<QTreeWidgetItem*> parents; |
||
77 | QList<int> indentations; |
||
78 | parents << parent; |
||
79 | indentations << 0; |
||
80 | |||
81 | int number = 0; |
||
82 | |||
83 | while (number < lines.count()) { |
||
84 | int position = 0; |
||
85 | while (position < lines[number].length()) { |
||
86 | if (lines[number].mid(position, 1) != " ") |
||
87 | break; |
||
88 | position++; |
||
89 | } |
||
90 | |||
91 | QString lineData = lines[number].mid(position).trimmed(); |
||
92 | |||
93 | if (!lineData.isEmpty()) { |
||
94 | // Read the column data from the rest of the line. |
||
95 | QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts); |
||
96 | QList<QVariant> columnData; |
||
97 | for (int column = 0; column < columnStrings.count(); ++column) |
||
98 | columnData << columnStrings[column]; |
||
99 | |||
100 | if (position > indentations.last()) { |
||
101 | // The last child of the current parent is now the new parent |
||
102 | // unless the current parent has no children. |
||
103 | |||
104 | if (parents.last()->childCount() > 0) { |
||
105 | parents << parents.last()->child(parents.last()->childCount()-1); |
||
106 | indentations << position; |
||
107 | } |
||
108 | } else { |
||
109 | while (position < indentations.last() && parents.count() > 0) { |
||
110 | parents.pop_back(); |
||
111 | indentations.pop_back(); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | // Append a new item to the current parent's list of children. |
||
116 | // // // parents.last()->appendChild(new QTreeWidgetItem(columnData, parents.last())); |
||
117 | QTreeWidgetItem* itm_tmp; |
||
118 | itm_tmp = new QTreeWidgetItem( parents.last()); |
||
119 | |||
120 | itm_tmp->setText(0, QString(columnData.at(0).toString())); |
||
121 | if (columnData.at(1).toString() == "true") { |
||
122 | itm_tmp->setCheckState(0,Qt::Checked); |
||
123 | } |
||
124 | else itm_tmp->setCheckState(0,Qt::Unchecked); |
||
90 | pingvin | 125 | itm_tmp->setText(1, QString(columnData.at(2).toString())); |
126 | itm_tmp->setText(2, QVariant(number).toString()); // - |
||
127 | itm_tmp->setText(3, QVariant(indentations.size()).toString()); |
||
89 | pingvin | 128 | } |
129 | |||
130 | number++; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | |||
135 | /* |
||
136 | void MainWindow::openBase() |
||
137 | { |
||
138 | |||
139 | db = QSqlDatabase::addDatabase("QMYSQL"); |
||
140 | pdb = &db; |
||
141 | raportFrm.pdb = &db; |
||
142 | |||
143 | pdb->setHostName(hostName); |
||
144 | pdb->setDatabaseName(baseName); |
||
145 | pdb->setUserName(userName); |
||
146 | pdb->setPassword(password); |
||
147 | bool ok = pdb->open(); |
||
148 | if (!ok) { |
||
149 | QMessageBox::critical( // . |
||
150 | this, // . |
||
151 | QObject::tr("Database Error"), // . |
||
152 | pdb->lastError().text()); // . |
||
153 | } |
||
154 | if (ok) |
||
155 | {lineEdit-> insert(tr(" ")); |
||
156 | } |
||
157 | else {lineEdit-> insert(tr(" . : ")); |
||
158 | lineEdit-> insert(pdb->lastError().text()); |
||
159 | } |
||
160 | |||
161 | |||
162 | |||
163 | model = new QSqlTableModel(this); |
||
164 | model->setTable(tableName); |
||
165 | |||
166 | model->setEditStrategy(QSqlTableModel::OnManualSubmit); |
||
167 | model->setSort(0, Qt::AscendingOrder); |
||
168 | |||
169 | model->select(); |
||
170 | |||
171 | |||
172 | QSqlField field(tr("age"), QVariant::Int); |
||
173 | field.setValue(QString(tr("123"))); |
||
174 | |||
175 | bool okey; |
||
176 | int index; |
||
177 | QSqlRecord record; |
||
178 | |||
179 | |||
180 | tableView->setModel(model); |
||
181 | view.setModel(model); |
||
182 | |||
183 | |||
184 | tableView->setAlternatingRowColors(true); |
||
185 | view.setAlternatingRowColors(true); |
||
186 | |||
187 | tableView->resizeColumnsToContents(); |
||
188 | |||
189 | |||
190 | view.resizeColumnsToContents(); |
||
191 | |||
192 | tableView->show(); |
||
193 | |||
194 | |||
195 | |||
196 | initTreeWidget(); |
||
197 | } |
||
198 | */ |
||
91 | pingvin | 199 | |
200 | |||
201 | void MainWindow::creatModelString(){ |
||
202 | |||
203 | MainWindow::modelString.clear(); // |
||
204 | // for (int i =0; i < rootItem1->childCount(); ++i ) |
||
205 | |||
206 | {MainWindow::addChildsString(rootItem1, 0);} |
||
207 | |||
208 | int root_item_child_count; |
||
209 | int i; |
||
210 | root_item_child_count = rootItem1->childCount(); |
||
211 | i++; |
||
212 | |||
213 | } |
||
214 | |||
215 | |||
216 | void MainWindow::addChildsString(QTreeWidgetItem *parentItem, int level){ |
||
217 | int child_count; |
||
218 | child_count = parentItem->childCount(); |
||
219 | for (int i=0; i < child_count; ++i){ |
||
220 | for (int m=0; m < level; ++m){ |
||
221 | modelString.append(tr(" ")); // - , |
||
222 | } |
||
223 | modelString.append(parentItem->child(i)->text(0)); // - |
||
224 | modelString.append(tr("\t\t\t\t")); // - |
||
225 | if (parentItem->child(i)->checkState(0) == Qt::Checked) {modelString.append(tr("true"));} // , "true", |
||
226 | else {modelString.append(tr("false"));} // - "false", |
||
227 | modelString.append(tr("\t\t\t\t")); // - |
||
228 | modelString.append(parentItem->child(i)->text(1)); // - ID |
||
229 | modelString.append(tr("\n")); // |
||
230 | if (parentItem->child(i)->childCount() > 0) { MainWindow::addChildsString(parentItem->child(i), level+1);} |
||
231 | |||
232 | } |
||
233 | int i; |
||
234 | i++; |
||
235 | } |
||
93 | pingvin | 236 | |
237 | |||
238 | bool MainWindow::openDataBase(){ |
||
239 | QString errorString; |
||
240 | sql = QSqlDatabase::addDatabase("QMYSQL"); |
||
241 | sql.setDatabaseName(tr("an_db")); |
||
242 | sql.setHostName(tr("localhost")); |
||
243 | sql.setUserName(tr("an")); |
||
244 | sql.setPassword(tr("393939")); |
||
245 | |||
246 | bool ok; |
||
247 | ok = sql.open(); |
||
248 | |||
249 | /* |
||
250 | if (!ok) { |
||
251 | QMessageBox::critical( // . |
||
252 | this, // . |
||
253 | QObject::tr("Database Error"), // . |
||
254 | sql.lastError().text() ); // . |
||
255 | |||
256 | |||
257 | } |
||
258 | |||
259 | */ |
||
260 | if (!ok) { |
||
261 | QMessageBox::critical( // . |
||
262 | this, // . |
||
263 | QObject::tr("Database Error"), // . |
||
264 | sql.lastError().text()); // . |
||
265 | } |
||
266 | else { |
||
267 | |||
268 | QMessageBox::information( // . |
||
269 | this, // . |
||
270 | QObject::tr("Database Connect"), // . |
||
271 | QObject::tr(" .")); // . |
||
272 | |||
273 | |||
274 | |||
275 | |||
276 | |||
277 | } |
||
278 | |||
279 | return ok; |
||
280 | } |
||
94 | pingvin | 281 | |
282 | |||
97 | pingvin | 283 | bool MainWindow::buildPreviewModel(QString modelName, QString rootClassID){ |
94 | pingvin | 284 | QSqlQuery q; |
285 | QString sql_str; |
||
95 | pingvin | 286 | QString field_name_str; |
287 | QString root_class_name; |
||
288 | bool ok; |
||
94 | pingvin | 289 | int field_name; |
95 | pingvin | 290 | |
291 | //ui->treeWidget->clear(); |
||
292 | |||
293 | root_class_name = MainWindow::ClassName(rootClassID); // |
||
294 | |||
295 | rootItem1 = new QTreeWidgetItem(ui->treeWidget); |
||
97 | pingvin | 296 | rootItem1->setText(0, modelName); |
297 | |||
95 | pingvin | 298 | rootItem1->setCheckState(0,Qt::Checked); |
299 | |||
300 | |||
97 | pingvin | 301 | rootItem2 = new QTreeWidgetItem(rootItem1); |
302 | rootItem2->setText(0, root_class_name); |
||
303 | rootItem2->setText(1, rootClassID); |
||
304 | rootItem2->setCheckState(0,Qt::Checked); |
||
95 | pingvin | 305 | |
98 | pingvin | 306 | addChildsItems(rootItem2); |
95 | pingvin | 307 | |
94 | pingvin | 308 | sql_str = tr("select * " |
95 | pingvin | 309 | " from DescriptionOfClasses where DescriptionOfClasses.DefaultValue = '" // , |
94 | pingvin | 310 | ); |
311 | sql_str.append(rootClassID); |
||
312 | |||
95 | pingvin | 313 | sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' ")); |
94 | pingvin | 314 | |
95 | pingvin | 315 | q.prepare(sql_str); |
316 | |||
317 | ok = q.exec(); |
||
318 | if (!ok) { |
||
319 | QMessageBox::critical( // . |
||
320 | this, // . |
||
321 | QObject::tr("Database Error"), // . |
||
322 | q.lastError().text()); // . |
||
323 | } |
||
324 | field_name = q.record().indexOf(tr("FieldName")); |
||
325 | while(q.next()){ |
||
326 | |||
327 | field_name_str = q.value(field_name).toString(); |
||
328 | |||
329 | } |
||
330 | |||
100 | pingvin | 331 | |
332 | |||
333 | |||
94 | pingvin | 334 | } |
95 | pingvin | 335 | |
336 | |||
337 | |||
338 | QString MainWindow::ClassName(QString class_id){ // ID |
||
339 | QSqlQuery q; |
||
340 | QString sql_str; |
||
341 | QString class_name_str; |
||
342 | bool ok; |
||
343 | int field_class_name; |
||
344 | |||
345 | sql_str = tr("select * " |
||
346 | " from ListOfClasses where ListOfClasses.ID = '" // , |
||
347 | ); |
||
348 | |||
349 | sql_str.append(class_id); |
||
350 | sql_str.append(tr("' ")); |
||
351 | |||
352 | q.prepare(sql_str); |
||
353 | |||
354 | ok = q.exec(); |
||
355 | |||
356 | |||
357 | if (!ok) { |
||
358 | QMessageBox::critical( // . |
||
359 | this, // . |
||
360 | QObject::tr("Database Error"), // . |
||
361 | q.lastError().text()); // . |
||
362 | } |
||
363 | |||
364 | field_class_name = q.record().indexOf(tr("ClassName")); |
||
365 | while(q.next()){ |
||
366 | |||
367 | class_name_str = q.value(field_class_name).toString(); |
||
368 | |||
369 | } |
||
370 | |||
371 | return class_name_str; |
||
372 | |||
373 | } |
||
98 | pingvin | 374 | |
375 | void MainWindow::addChildsItems(QTreeWidgetItem *perent_class_item){ // , |
||
100 | pingvin | 376 | QStringList chields_list; |
377 | QString parent_ID; |
||
378 | QTreeWidgetItem *Item_tmp; |
||
379 | parent_ID = perent_class_item->text(1); |
||
380 | int chields_count, i; |
||
98 | pingvin | 381 | |
100 | pingvin | 382 | chields_list = MainWindow::classChields(parent_ID); |
383 | if(chields_list.isEmpty()) return; |
||
384 | chields_count = chields_list.size(); |
||
385 | for (int l = 0; l < chields_count; ++l){ |
||
386 | QString chield_class_name, chield_id; |
||
387 | chield_id = chields_list.at(l); |
||
388 | chield_class_name = MainWindow::ClassName(chield_id); |
||
389 | Item_tmp = new QTreeWidgetItem(perent_class_item); |
||
390 | Item_tmp->setText(0, chield_class_name); |
||
391 | Item_tmp->setText(1, chield_id); |
||
392 | Item_tmp->setCheckState(0, Qt::Checked); |
||
101 | pingvin | 393 | MainWindow::addChildsItems(Item_tmp); |
98 | pingvin | 394 | } |
395 | |||
100 | pingvin | 396 | i++; |
99 | pingvin | 397 | |
100 | pingvin | 398 | } |
399 | |||
400 | |||
99 | pingvin | 401 | /******************************************************** |
402 | * "" |
||
403 | * |
||
404 | * |
||
405 | * |
||
406 | ********************************************************/ |
||
407 | |||
408 | QStringList MainWindow::classChields(QString class_id){ |
||
409 | // QMap<QString, QString> map; |
||
410 | // TClass class_tmp; |
||
411 | // QList <TClass> chields_class_list; // |
||
412 | QStringList result; |
||
413 | QSqlQuery q; |
||
414 | QString sql_str; |
||
415 | QString classIdentifer_str; // - |
||
416 | QString field_id_str; |
||
417 | bool ok; |
||
418 | int field_classIdentifer; |
||
419 | sql_str = tr("select * " |
||
420 | " from DescriptionOfClasses where DescriptionOfClasses.DefaultValue = '" // , |
||
421 | ); |
||
422 | sql_str.append(class_id); |
||
423 | |||
424 | sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' ")); |
||
425 | |||
426 | q.prepare(sql_str); |
||
427 | |||
428 | ok = q.exec(); |
||
429 | if (!ok) { |
||
430 | QMessageBox::critical( // . |
||
431 | this, // . |
||
432 | QObject::tr("Database Error"), // . |
||
433 | q.lastError().text()); // . |
||
434 | } |
||
435 | field_classIdentifer = q.record().indexOf(tr("ClassIdentifer")); |
||
436 | while(q.next()){ |
||
437 | |||
438 | classIdentifer_str = q.value(field_classIdentifer).toString(); |
||
439 | result.append(classIdentifer_str); |
||
440 | } |
||
441 | |||
442 | return result; |
||
443 | |||
444 | } |
||
102 | pingvin | 445 | |
446 | |||
447 | QMap <QString, QString> MainWindow::getClassList(){ // |
||
448 | QMap <QString, QString> result_map; |
||
449 | QSqlQuery q; |
||
450 | QString sql_str; |
||
451 | int field_class_id, field_class_name; |
||
452 | bool ok; |
||
453 | sql_str = tr("select * " |
||
454 | " from ListOfClasses " // , |
||
455 | ); |
||
456 | q.prepare(sql_str); |
||
457 | ok = q.exec(); |
||
458 | if (!ok) { |
||
459 | QMessageBox::critical( // . |
||
460 | this, // . |
||
461 | QObject::tr("Database Error"), // . |
||
462 | q.lastError().text()); // . |
||
463 | return result_map; // - |
||
464 | } |
||
465 | field_class_id = q.record().indexOf(tr("ID")); |
||
466 | field_class_name = q.record().indexOf(tr("ClassName")); |
||
467 | |||
468 | |||
469 | return result_map; |
||
470 | } |