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