Хранилища Subversion OpenInventory

Редакция

Редакция 106 | Редакция 108 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | 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);
106 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);
106 pingvin 298
root_items_list << rootItem1;
97 pingvin 299
 rootItem1->setText(0, modelName);
300
 
95 pingvin 301
 rootItem1->setCheckState(0,Qt::Checked);
104 pingvin 302
 rootItems.append(rootItem1);
95 pingvin 303
 
304
 
104 pingvin 305
 
97 pingvin 306
rootItem2 = new  QTreeWidgetItem(rootItem1);
307
rootItem2->setText(0, root_class_name);
308
rootItem2->setText(1, rootClassID);
309
rootItem2->setCheckState(0,Qt::Checked);
95 pingvin 310
 
98 pingvin 311
addChildsItems(rootItem2);
95 pingvin 312
 
94 pingvin 313
sql_str = tr("select * "
95 pingvin 314
             " from DescriptionOfClasses where  DescriptionOfClasses.DefaultValue = '"       // ,     
94 pingvin 315
             );
316
sql_str.append(rootClassID);
317
 
95 pingvin 318
sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' "));
94 pingvin 319
 
95 pingvin 320
q.prepare(sql_str);
321
 
322
ok = q.exec();
323
if (!ok) {
324
                        QMessageBox::critical( //     .
325
                                                                        this,                      //  .
326
                                                                        QObject::tr("Database Error"),   // .
327
                                                                        q.lastError().text());          //  .
328
                 }
329
field_name = q.record().indexOf(tr("FieldName"));
330
while(q.next()){
331
 
332
                field_name_str = q.value(field_name).toString();
333
 
334
                }
335
 
100 pingvin 336
 
337
 
338
 
94 pingvin 339
}
95 pingvin 340
 
341
 
107 pingvin 342
bool MainWindow::rebuildPreviewModel(QTreeWidgetItem* root_item, QString rootClassID){
343
QSqlQuery q;
344
QString sql_str;
345
QString field_name_str;
346
QString root_class_name;
347
bool ok;
348
int field_name;
95 pingvin 349
 
107 pingvin 350
//ui->treeWidget->clear();
351
 
352
root_class_name = MainWindow::ClassName(rootClassID); //   
353
 
354
 
355
// rootItem1 = new  QTreeWidgetItem(ui->treeWidget);
356
// root_items_list << rootItem1;
357
// rootItem1->setText(0, modelName);
358
 
359
// rootItem1->setCheckState(0,Qt::Checked);
360
// rootItems.append(rootItem1);
361
 
362
// root_item->removeChild(root_item->child(0)); //    
363
if (root_item->childCount()==1){
364
root_item->child(0)->setText(0, root_class_name);
365
root_item->child(0)->setText(1, rootClassID);
366
root_item->child(0)->setCheckState(0,Qt::Checked);}
367
 
368
//delete rootItem2;
369
 
370
 
371
// rootItem2 = new  QTreeWidgetItem(rootItem1);
372
 
373
 
374
 
375
// rootItem2->setText(0, root_class_name);
376
// rootItem2->setText(1, rootClassID);
377
// rootItem2->setCheckState(0,Qt::Checked);
378
 
379
int child_count = root_item->child(0)->childCount();
380
 
381
 
382
 
383
for (int i=0; i < child_count; i++){
384
    root_item->child(0)->removeChild(root_item->child(0)->child(0));
385
}
386
 
387
child_count = root_item->child(0)->childCount();
388
addChildsItems(root_item->child(0));
389
/*
390
sql_str = tr("select * "
391
             " from DescriptionOfClasses where  DescriptionOfClasses.DefaultValue = '"       // ,     
392
             );
393
sql_str.append(rootClassID);
394
 
395
sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' "));
396
 
397
q.prepare(sql_str);
398
 
399
ok = q.exec();
400
if (!ok) {
401
                        QMessageBox::critical( //     .
402
                                                                        this,                      //  .
403
                                                                        QObject::tr("Database Error"),   // .
404
                                                                        q.lastError().text());          //  .
405
                 }
406
field_name = q.record().indexOf(tr("FieldName"));
407
while(q.next()){
408
 
409
                field_name_str = q.value(field_name).toString();
410
 
411
                }
412
 
413
*/
414
 
415
 
416
}
417
 
418
 
419
 
420
 
95 pingvin 421
QString MainWindow::ClassName(QString class_id){ //     ID
422
    QSqlQuery q;
423
    QString sql_str;
424
    QString class_name_str;
425
    bool ok;
426
    int field_class_name;
427
 
428
    sql_str = tr("select * "
429
                 " from ListOfClasses where ListOfClasses.ID = '"       // ,     
430
                 );
431
 
432
    sql_str.append(class_id);
433
    sql_str.append(tr("' "));
434
 
435
    q.prepare(sql_str);
436
 
437
    ok = q.exec();
438
 
439
 
440
    if (!ok) {
441
                            QMessageBox::critical( //     .
442
                                                                            this,                      //  .
443
                                                                            QObject::tr("Database Error"),   // .
444
                                                                            q.lastError().text());          //  .
445
                     }
446
 
447
    field_class_name = q.record().indexOf(tr("ClassName"));
448
    while(q.next()){
449
 
450
                    class_name_str = q.value(field_class_name).toString();
451
 
452
                    }
453
 
454
    return class_name_str;
455
 
456
}
98 pingvin 457
 
458
void MainWindow::addChildsItems(QTreeWidgetItem *perent_class_item){ //          ,      
100 pingvin 459
QStringList chields_list;
460
QString parent_ID;
461
QTreeWidgetItem *Item_tmp;
462
parent_ID = perent_class_item->text(1);
463
int chields_count, i;
98 pingvin 464
 
100 pingvin 465
chields_list = MainWindow::classChields(parent_ID);
466
if(chields_list.isEmpty()) return;
467
chields_count = chields_list.size();
468
for (int l = 0; l < chields_count; ++l){
469
    QString chield_class_name, chield_id;
470
    chield_id = chields_list.at(l);
471
    chield_class_name = MainWindow::ClassName(chield_id);
472
    Item_tmp = new QTreeWidgetItem(perent_class_item);
473
    Item_tmp->setText(0, chield_class_name);
474
    Item_tmp->setText(1, chield_id);
475
    Item_tmp->setCheckState(0, Qt::Checked);
101 pingvin 476
    MainWindow::addChildsItems(Item_tmp);
98 pingvin 477
}
478
 
100 pingvin 479
i++;
99 pingvin 480
 
100 pingvin 481
}
482
 
483
 
99 pingvin 484
/********************************************************
485
*    "" 
486
*
487
*
488
*
489
********************************************************/
490
 
491
QStringList MainWindow::classChields(QString class_id){
492
   // QMap<QString, QString> map;
493
   // TClass class_tmp;
494
   // QList <TClass> chields_class_list; //   
495
    QStringList result;
496
    QSqlQuery q;
497
    QString sql_str;
498
    QString classIdentifer_str; //      -
499
    QString field_id_str;
500
    bool ok;
501
    int field_classIdentifer;
502
    sql_str = tr("select * "
503
                 " from DescriptionOfClasses where  DescriptionOfClasses.DefaultValue = '"       // ,     
504
                 );
505
    sql_str.append(class_id);
506
 
507
    sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' "));
508
 
509
    q.prepare(sql_str);
510
 
511
    ok = q.exec();
512
    if (!ok) {
513
                            QMessageBox::critical( //     .
514
                                                                            this,                      //  .
515
                                                                            QObject::tr("Database Error"),   // .
516
                                                                            q.lastError().text());          //  .
517
                     }
518
    field_classIdentifer = q.record().indexOf(tr("ClassIdentifer"));
519
      while(q.next()){
520
 
521
                    classIdentifer_str = q.value(field_classIdentifer).toString();
522
                    result.append(classIdentifer_str);
523
                    }
524
 
525
    return result;
526
 
527
  }
102 pingvin 528
 
529
 
530
QMap <QString, QString> MainWindow::getClassList(){ //   
531
QMap <QString, QString> result_map;
532
QSqlQuery q;
533
QString sql_str;
103 pingvin 534
QString class_name_str, class_id_str;
535
 
102 pingvin 536
int field_class_id, field_class_name;
537
bool ok;
538
sql_str = tr("select * "
539
             " from ListOfClasses "       // ,     
540
             );
541
 q.prepare(sql_str);
542
 ok = q.exec();
543
 if (!ok) {
544
                         QMessageBox::critical( //     .
545
                                                                         this,                      //  .
546
                                                                         QObject::tr("Database Error"),   // .
547
                                                                         q.lastError().text());          //  .
548
                         return result_map; //   -   
549
                  }
550
field_class_id = q.record().indexOf(tr("ID"));
551
field_class_name = q.record().indexOf(tr("ClassName"));
552
 
103 pingvin 553
while(q.next()){
102 pingvin 554
 
103 pingvin 555
                class_name_str = q.value(field_class_name).toString();
556
                class_id_str = q.value(field_class_id).toString();
557
                result_map[class_id_str] = class_name_str;
558
                }
102 pingvin 559
 return result_map;
560
}
104 pingvin 561
 
562
 
563
 
564
 
565
 
566
void MainWindow::initComboBox(){
567
 
106 pingvin 568
 
104 pingvin 569
    QStringList classesNameList;
570
    QStringList classesID_list;
571
    QStringList tmp_stringList;
572
 
573
 
574
 
575
    QString tmp_str;
576
 
577
    class_list_map = MainWindow::getClassList();
578
    classesID_list = class_list_map.keys();
579
    classesNameList = class_list_map.values();
580
    QMapIterator<QString, QString> interator(class_list_map);
581
//    ui->comboBox->addItems(classesID_list);
582
  //  ui->comboBox->addItems(classesNameList);
583
    while (interator.hasNext()) {
584
         interator.next();
585
         tmp_str =  interator.value();
586
         tmp_str.append(tr(" \t(ID="));
587
         tmp_str.append(interator.key());
588
         tmp_str.append(tr(")"));
589
         tmp_stringList << tmp_str;
590
 
591
     }
592
    ui->comboBox->addItems(tmp_stringList);
593
 
594
 
595
}
105 pingvin 596
 
597
 
106 pingvin 598
void MainWindow::on_comboBox_currentIndexChanged( int index ){
599
QStringList classesNameList;
600
QStringList classesID_list;
601
QString ID_str;
602
classesID_list = class_list_map.keys();
603
classesNameList = class_list_map.values();
604
ID_str =  classesID_list[index];
605
ui->label->setText(ID_str);
107 pingvin 606
if (root_items_list.indexOf(ui->treeWidget->currentItem()) != -1) rebuildPreviewModel(ui->treeWidget->currentItem(), ID_str);
607
 
608
 
105 pingvin 609
}
610
 
106 pingvin 611
void MainWindow::on_treeWidget_itemChanged ( QTreeWidgetItem * item, int column ){
612
    int i;
613
    i++;
614
 
615
}
616
 
617
 
618
void MainWindow::on_treeWidget_itemClicked ( QTreeWidgetItem * item, int column ){
619
    int i;
620
    i++;
621
if (root_items_list.indexOf(item) != -1) ui->comboBox->setEnabled(true);
622
 
623
  //  if ((rootItem1) == (item) ) ui->comboBox->setEnabled(true);
624
else ( ui->comboBox->setEnabled(false));
625
}