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