Хранилища Subversion OpenInventory

Редакция

Редакция 131 | Редакция 136 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | 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)));
118 pingvin 10
    model_is_build = false;
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
 
114 pingvin 24
    ui->treeWidget->setAlternatingRowColors(true);
25
    ui->treeWidget_2->setAlternatingRowColors(true);
132 pingvin 26
 
27
 
28
    MainWindow::readSettings();
29
 
93 pingvin 30
    MainWindow::openDataBase();
131 pingvin 31
    MainWindow::buildPreviewModel(tr(" "), tr("1"));
104 pingvin 32
    MainWindow::initComboBox();
109 pingvin 33
    getDatabaseData();
127 pingvin 34
    //// sql_mogel = new QSqlTableModel();
35
    sql_mogel = new MyModel();
36
 
37
 
118 pingvin 38
    model_is_build = true;
119 pingvin 39
    connect(sql_mogel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(on_sql_mogel_dataChanged(QModelIndex,QModelIndex)));
40
    ui->pushButton_3->setEnabled(false);
41
    ui->pushButton_4->setEnabled(false);
42
 
132 pingvin 43
   // setFrm.show();
119 pingvin 44
 
132 pingvin 45
    connect(ui->getsettingAct, SIGNAL(triggered()), this, SLOT(getSetting())); //       " "  " "
46
    connect(setFrm.pushButton, SIGNAL(clicked()), this, SLOT(applySetting()));
47
 
48
 
49
 
88 pingvin 50
}
51
 
52
MainWindow::~MainWindow()
53
{
117 pingvin 54
    delete sql_mogel;
88 pingvin 55
    delete ui;
56
}
57
 
58
void MainWindow::changeEvent(QEvent *e)
59
{
60
    QMainWindow::changeEvent(e);
61
    switch (e->type()) {
62
    case QEvent::LanguageChange:
63
        ui->retranslateUi(this);
64
        break;
65
    default:
66
        break;
67
    }
68
}
89 pingvin 69
 
70
 
71
bool MainWindow::readModel(){
72
    bool result;
73
    QString model_str;
74
    QFile file(":/model.txt");
92 pingvin 75
 
91 pingvin 76
   rootItem1 = new  QTreeWidgetItem(ui->treeWidget);
89 pingvin 77
    rootItem1->setText(0, tr(" 1"));
78
   // rootItem2 = new  QTreeWidgetItem(rootItem1);
79
   // rootItem2->setText(0, tr(" 2"));
80
    rootItem1->setCheckState(0,Qt::Checked);
81
   // rootItem2->setCheckState(0,Qt::Checked);
82
 
83
 
84
 
85
    result = file.open(QIODevice::ReadOnly);
86
    if (result) {
90 pingvin 87
                     model_str = QString(tr(file.readAll()));
89 pingvin 88
                }
89
 
91 pingvin 90
    setupModelData(model_str.split(QString("\n")), rootItem1);
89 pingvin 91
 
92
 
93
    return result;
94
 
95
}
96
 
97
 
98
void MainWindow::setupModelData(const QStringList &lines, QTreeWidgetItem *parent)
99
{
100
    QList<QTreeWidgetItem*> parents;
101
    QList<int> indentations;
102
    parents << parent;
103
    indentations << 0;
104
 
105
    int number = 0;
106
 
107
    while (number < lines.count()) {
108
        int position = 0;
109
        while (position < lines[number].length()) {
110
            if (lines[number].mid(position, 1) != " ")
111
                break;
112
            position++;
113
        }
114
 
115
        QString lineData = lines[number].mid(position).trimmed();
116
 
117
        if (!lineData.isEmpty()) {
118
            // Read the column data from the rest of the line.
119
            QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
120
            QList<QVariant> columnData;
121
            for (int column = 0; column < columnStrings.count(); ++column)
122
                columnData << columnStrings[column];
123
 
124
            if (position > indentations.last()) {
125
                // The last child of the current parent is now the new parent
126
                // unless the current parent has no children.
127
 
128
                if (parents.last()->childCount() > 0) {
129
                    parents << parents.last()->child(parents.last()->childCount()-1);
130
                    indentations << position;
131
                }
132
            } else {
133
                while (position < indentations.last() && parents.count() > 0) {
134
                    parents.pop_back();
135
                    indentations.pop_back();
136
                }
137
            }
138
 
139
            // Append a new item to the current parent's list of children.
140
          // // //  parents.last()->appendChild(new QTreeWidgetItem(columnData, parents.last()));
141
            QTreeWidgetItem* itm_tmp;
142
            itm_tmp = new QTreeWidgetItem( parents.last());
143
 
144
            itm_tmp->setText(0, QString(columnData.at(0).toString()));
145
            if (columnData.at(1).toString() == "true") {
146
                                                            itm_tmp->setCheckState(0,Qt::Checked);
147
                                                        }
148
            else itm_tmp->setCheckState(0,Qt::Unchecked);
90 pingvin 149
            itm_tmp->setText(1, QString(columnData.at(2).toString()));
150
            itm_tmp->setText(2, QVariant(number).toString()); //    - 
151
            itm_tmp->setText(3, QVariant(indentations.size()).toString());
89 pingvin 152
        }
153
 
154
        number++;
155
    }
156
}
157
 
158
 
159
/*
160
void MainWindow::openBase()
161
{
162
 
163
        db = QSqlDatabase::addDatabase("QMYSQL");
164
        pdb = &db;
165
        raportFrm.pdb = &db;
166
 
167
        pdb->setHostName(hostName);
168
        pdb->setDatabaseName(baseName);
169
        pdb->setUserName(userName);
170
        pdb->setPassword(password);
171
        bool ok = pdb->open();
172
        if (!ok) {
173
                                QMessageBox::critical( //     .
174
                                                                                this,                      //  .
175
                                                                                QObject::tr("Database Error"),   // .
176
                                                                                pdb->lastError().text());          //  .
177
                         }
178
        if (ok)
179
        {lineEdit-> insert(tr("  "));
180
        }
181
        else {lineEdit-> insert(tr(" .  : "));
182
                  lineEdit-> insert(pdb->lastError().text());
183
                 }
184
 
185
 
186
 
187
        model = new QSqlTableModel(this);
188
        model->setTable(tableName);
189
 
190
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
191
        model->setSort(0, Qt::AscendingOrder);
192
 
193
        model->select();
194
 
195
 
196
        QSqlField field(tr("age"), QVariant::Int);
197
        field.setValue(QString(tr("123")));
198
 
199
        bool okey;
200
        int index;
201
        QSqlRecord record;
202
 
203
 
204
        tableView->setModel(model);
205
        view.setModel(model);
206
 
207
 
208
        tableView->setAlternatingRowColors(true);
209
        view.setAlternatingRowColors(true);
210
 
211
        tableView->resizeColumnsToContents();
212
 
213
 
214
        view.resizeColumnsToContents();
215
 
216
        tableView->show();
217
 
218
 
219
 
220
        initTreeWidget();
221
}
222
*/
91 pingvin 223
 
224
 
225
void MainWindow::creatModelString(){
226
 
227
    MainWindow::modelString.clear(); //   
228
 //   for (int i =0; i < rootItem1->childCount(); ++i )
229
 
230
    {MainWindow::addChildsString(rootItem1, 0);}
231
 
232
    int root_item_child_count;
233
    int i;
234
    root_item_child_count = rootItem1->childCount();
235
    i++;
236
 
237
}
238
 
239
 
240
void MainWindow::addChildsString(QTreeWidgetItem *parentItem, int level){
241
    int child_count;
242
    child_count = parentItem->childCount();
243
    for (int i=0; i < child_count; ++i){
244
        for (int m=0; m < level; ++m){
245
        modelString.append(tr("    ")); //   -  ,     
246
    }
247
        modelString.append(parentItem->child(i)->text(0)); //    -  
248
        modelString.append(tr("\t\t\t\t")); //   -   
249
        if (parentItem->child(i)->checkState(0) ==  Qt::Checked) {modelString.append(tr("true"));} //   ,  "true",      
250
            else {modelString.append(tr("false"));} //     -  "false",       
251
        modelString.append(tr("\t\t\t\t")); //   -   
252
        modelString.append(parentItem->child(i)->text(1)); //    - ID 
253
         modelString.append(tr("\n")); //  
254
        if (parentItem->child(i)->childCount() > 0) { MainWindow::addChildsString(parentItem->child(i), level+1);}
255
 
256
    }
257
int i;
258
i++;
259
}
93 pingvin 260
 
261
 
262
bool MainWindow::openDataBase(){
263
    QString errorString;
264
    sql = QSqlDatabase::addDatabase("QMYSQL");
131 pingvin 265
//    sql.setDatabaseName(tr("an_db"));
266
 
132 pingvin 267
    // // // sql.setDatabaseName(tr("inventory"));
131 pingvin 268
 
132 pingvin 269
    sql.setDatabaseName(baseName);
131 pingvin 270
 
271
 
272
 
132 pingvin 273
    // // // sql.setHostName(tr("localhost"));
93 pingvin 274
 
132 pingvin 275
    sql.setHostName(hostName);
276
 
277
 
278
    // // // sql.setUserName(tr("an"));
279
 
280
    sql.setUserName(userName);
281
 
282
 
283
 
284
   // // //  sql.setPassword(tr("393939"));
285
 
286
 
287
    sql.setPassword(password);
288
 
289
 
290
 
93 pingvin 291
    bool ok;
292
    ok = sql.open();
293
 
294
    /*
295
    if (!ok) {
296
                                QMessageBox::critical( //     .
297
                                                                                this,                      //  .
298
                                                                                QObject::tr("Database Error"),   // .
299
                                                                                sql.lastError().text() );         //  .
300
 
301
 
302
                            }
303
 
304
*/
305
    if (!ok) {
306
                            QMessageBox::critical( //     .
307
                                                                            this,                      //  .
308
                                                                            QObject::tr("Database Error"),   // .
309
                                                                            sql.lastError().text());          //  .
310
                     }
311
else {
312
 
313
        QMessageBox::information( //     .
314
                                                        this,                      //  .
315
                                                        QObject::tr("Database Connect"),   // .
316
                                                        QObject::tr("     ."));         //  .
317
 
318
 
319
 
320
 
321
 
322
    }
323
 
324
     return ok;
325
}
94 pingvin 326
 
327
 
97 pingvin 328
bool MainWindow::buildPreviewModel(QString modelName, QString rootClassID){
94 pingvin 329
QSqlQuery q;
330
QString sql_str;
95 pingvin 331
QString field_name_str;
332
QString root_class_name;
333
bool ok;
94 pingvin 334
int field_name;
95 pingvin 335
 
336
//ui->treeWidget->clear();
337
 
338
root_class_name = MainWindow::ClassName(rootClassID); //   
339
 
340
rootItem1 = new  QTreeWidgetItem(ui->treeWidget);
106 pingvin 341
root_items_list << rootItem1;
97 pingvin 342
 rootItem1->setText(0, modelName);
343
 
95 pingvin 344
 rootItem1->setCheckState(0,Qt::Checked);
104 pingvin 345
 rootItems.append(rootItem1);
95 pingvin 346
 
347
 
104 pingvin 348
 
97 pingvin 349
rootItem2 = new  QTreeWidgetItem(rootItem1);
350
rootItem2->setText(0, root_class_name);
351
rootItem2->setText(1, rootClassID);
352
rootItem2->setCheckState(0,Qt::Checked);
95 pingvin 353
 
98 pingvin 354
addChildsItems(rootItem2);
95 pingvin 355
 
94 pingvin 356
sql_str = tr("select * "
95 pingvin 357
             " from DescriptionOfClasses where  DescriptionOfClasses.DefaultValue = '"       // ,     
94 pingvin 358
             );
359
sql_str.append(rootClassID);
360
 
95 pingvin 361
sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' "));
94 pingvin 362
 
95 pingvin 363
q.prepare(sql_str);
364
 
365
ok = q.exec();
366
if (!ok) {
367
                        QMessageBox::critical( //     .
368
                                                                        this,                      //  .
369
                                                                        QObject::tr("Database Error"),   // .
370
                                                                        q.lastError().text());          //  .
371
                 }
372
field_name = q.record().indexOf(tr("FieldName"));
373
while(q.next()){
374
 
375
                field_name_str = q.value(field_name).toString();
376
 
377
                }
378
 
100 pingvin 379
 
380
 
381
 
94 pingvin 382
}
95 pingvin 383
 
384
 
107 pingvin 385
bool MainWindow::rebuildPreviewModel(QTreeWidgetItem* root_item, QString rootClassID){
386
QSqlQuery q;
387
QString sql_str;
388
QString field_name_str;
389
QString root_class_name;
390
bool ok;
391
int field_name;
95 pingvin 392
 
107 pingvin 393
//ui->treeWidget->clear();
394
 
395
root_class_name = MainWindow::ClassName(rootClassID); //   
396
 
397
 
398
// rootItem1 = new  QTreeWidgetItem(ui->treeWidget);
399
// root_items_list << rootItem1;
400
// rootItem1->setText(0, modelName);
401
 
402
// rootItem1->setCheckState(0,Qt::Checked);
403
// rootItems.append(rootItem1);
404
 
405
// root_item->removeChild(root_item->child(0)); //    
406
if (root_item->childCount()==1){
407
root_item->child(0)->setText(0, root_class_name);
408
root_item->child(0)->setText(1, rootClassID);
409
root_item->child(0)->setCheckState(0,Qt::Checked);}
410
 
411
//delete rootItem2;
412
 
413
 
414
// rootItem2 = new  QTreeWidgetItem(rootItem1);
415
 
416
 
417
 
418
// rootItem2->setText(0, root_class_name);
419
// rootItem2->setText(1, rootClassID);
420
// rootItem2->setCheckState(0,Qt::Checked);
421
 
422
int child_count = root_item->child(0)->childCount();
423
 
424
 
425
 
426
for (int i=0; i < child_count; i++){
427
    root_item->child(0)->removeChild(root_item->child(0)->child(0));
428
}
429
 
430
child_count = root_item->child(0)->childCount();
431
addChildsItems(root_item->child(0));
432
/*
433
sql_str = tr("select * "
434
             " from DescriptionOfClasses where  DescriptionOfClasses.DefaultValue = '"       // ,     
435
             );
436
sql_str.append(rootClassID);
437
 
438
sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' "));
439
 
440
q.prepare(sql_str);
441
 
442
ok = q.exec();
443
if (!ok) {
444
                        QMessageBox::critical( //     .
445
                                                                        this,                      //  .
446
                                                                        QObject::tr("Database Error"),   // .
447
                                                                        q.lastError().text());          //  .
448
                 }
449
field_name = q.record().indexOf(tr("FieldName"));
450
while(q.next()){
451
 
452
                field_name_str = q.value(field_name).toString();
453
 
454
                }
455
 
456
*/
457
 
458
 
459
}
460
 
461
 
462
 
463
 
95 pingvin 464
QString MainWindow::ClassName(QString class_id){ //     ID
465
    QSqlQuery q;
466
    QString sql_str;
467
    QString class_name_str;
468
    bool ok;
469
    int field_class_name;
470
 
471
    sql_str = tr("select * "
472
                 " from ListOfClasses where ListOfClasses.ID = '"       // ,     
473
                 );
474
 
475
    sql_str.append(class_id);
476
    sql_str.append(tr("' "));
477
 
478
    q.prepare(sql_str);
479
 
480
    ok = q.exec();
481
 
482
 
483
    if (!ok) {
484
                            QMessageBox::critical( //     .
485
                                                                            this,                      //  .
486
                                                                            QObject::tr("Database Error"),   // .
487
                                                                            q.lastError().text());          //  .
488
                     }
489
 
490
    field_class_name = q.record().indexOf(tr("ClassName"));
491
    while(q.next()){
492
 
493
                    class_name_str = q.value(field_class_name).toString();
494
 
495
                    }
496
 
497
    return class_name_str;
498
 
499
}
98 pingvin 500
 
501
void MainWindow::addChildsItems(QTreeWidgetItem *perent_class_item){ //          ,      
100 pingvin 502
QStringList chields_list;
503
QString parent_ID;
504
QTreeWidgetItem *Item_tmp;
505
parent_ID = perent_class_item->text(1);
506
int chields_count, i;
98 pingvin 507
 
100 pingvin 508
chields_list = MainWindow::classChields(parent_ID);
509
if(chields_list.isEmpty()) return;
510
chields_count = chields_list.size();
511
for (int l = 0; l < chields_count; ++l){
512
    QString chield_class_name, chield_id;
513
    chield_id = chields_list.at(l);
514
    chield_class_name = MainWindow::ClassName(chield_id);
515
    Item_tmp = new QTreeWidgetItem(perent_class_item);
516
    Item_tmp->setText(0, chield_class_name);
517
    Item_tmp->setText(1, chield_id);
518
    Item_tmp->setCheckState(0, Qt::Checked);
101 pingvin 519
    MainWindow::addChildsItems(Item_tmp);
98 pingvin 520
}
521
 
100 pingvin 522
i++;
99 pingvin 523
 
100 pingvin 524
}
525
 
526
 
99 pingvin 527
/********************************************************
528
*    "" 
529
*
530
*
531
*
532
********************************************************/
533
 
534
QStringList MainWindow::classChields(QString class_id){
535
   // QMap<QString, QString> map;
536
   // TClass class_tmp;
537
   // QList <TClass> chields_class_list; //   
538
    QStringList result;
539
    QSqlQuery q;
540
    QString sql_str;
541
    QString classIdentifer_str; //      -
542
    QString field_id_str;
543
    bool ok;
544
    int field_classIdentifer;
545
    sql_str = tr("select * "
546
                 " from DescriptionOfClasses where  DescriptionOfClasses.DefaultValue = '"       // ,     
547
                 );
548
    sql_str.append(class_id);
549
 
550
    sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' "));
551
 
552
    q.prepare(sql_str);
553
 
554
    ok = q.exec();
555
    if (!ok) {
556
                            QMessageBox::critical( //     .
557
                                                                            this,                      //  .
558
                                                                            QObject::tr("Database Error"),   // .
559
                                                                            q.lastError().text());          //  .
560
                     }
561
    field_classIdentifer = q.record().indexOf(tr("ClassIdentifer"));
562
      while(q.next()){
563
 
564
                    classIdentifer_str = q.value(field_classIdentifer).toString();
565
                    result.append(classIdentifer_str);
566
                    }
567
 
568
    return result;
569
 
570
  }
102 pingvin 571
 
572
 
573
QMap <QString, QString> MainWindow::getClassList(){ //   
574
QMap <QString, QString> result_map;
575
QSqlQuery q;
576
QString sql_str;
103 pingvin 577
QString class_name_str, class_id_str;
578
 
102 pingvin 579
int field_class_id, field_class_name;
580
bool ok;
581
sql_str = tr("select * "
582
             " from ListOfClasses "       // ,     
583
             );
584
 q.prepare(sql_str);
585
 ok = q.exec();
586
 if (!ok) {
587
                         QMessageBox::critical( //     .
588
                                                                         this,                      //  .
589
                                                                         QObject::tr("Database Error"),   // .
590
                                                                         q.lastError().text());          //  .
591
                         return result_map; //   -   
592
                  }
593
field_class_id = q.record().indexOf(tr("ID"));
594
field_class_name = q.record().indexOf(tr("ClassName"));
595
 
103 pingvin 596
while(q.next()){
102 pingvin 597
 
103 pingvin 598
                class_name_str = q.value(field_class_name).toString();
599
                class_id_str = q.value(field_class_id).toString();
600
                result_map[class_id_str] = class_name_str;
601
                }
102 pingvin 602
 return result_map;
603
}
104 pingvin 604
 
605
 
606
 
607
 
608
 
609
void MainWindow::initComboBox(){
610
 
106 pingvin 611
 
104 pingvin 612
    QStringList classesNameList;
613
    QStringList classesID_list;
614
    QStringList tmp_stringList;
615
 
616
 
617
 
618
    QString tmp_str;
619
 
620
    class_list_map = MainWindow::getClassList();
621
    classesID_list = class_list_map.keys();
622
    classesNameList = class_list_map.values();
623
    QMapIterator<QString, QString> interator(class_list_map);
624
//    ui->comboBox->addItems(classesID_list);
625
  //  ui->comboBox->addItems(classesNameList);
626
    while (interator.hasNext()) {
627
         interator.next();
628
         tmp_str =  interator.value();
629
         tmp_str.append(tr(" \t(ID="));
630
         tmp_str.append(interator.key());
631
         tmp_str.append(tr(")"));
632
         tmp_stringList << tmp_str;
633
 
634
     }
132 pingvin 635
//    int count = ui->comboBox->count();
636
 
637
    ui->comboBox->clear();
104 pingvin 638
    ui->comboBox->addItems(tmp_stringList);
639
 
640
 
641
}
105 pingvin 642
 
643
 
106 pingvin 644
void MainWindow::on_comboBox_currentIndexChanged( int index ){
645
QStringList classesNameList;
646
QStringList classesID_list;
647
QString ID_str;
132 pingvin 648
if (index == -1) return;
649
 
106 pingvin 650
classesID_list = class_list_map.keys();
651
classesNameList = class_list_map.values();
652
ID_str =  classesID_list[index];
653
ui->label->setText(ID_str);
107 pingvin 654
if (root_items_list.indexOf(ui->treeWidget->currentItem()) != -1) rebuildPreviewModel(ui->treeWidget->currentItem(), ID_str);
118 pingvin 655
getDatabaseData();
107 pingvin 656
 
657
 
105 pingvin 658
}
659
 
106 pingvin 660
void MainWindow::on_treeWidget_itemChanged ( QTreeWidgetItem * item, int column ){
661
    int i;
662
    i++;
663
 
118 pingvin 664
    if ((column) == 0 && (model_is_build)) getDatabaseData();
665
 
106 pingvin 666
}
667
 
668
 
669
void MainWindow::on_treeWidget_itemClicked ( QTreeWidgetItem * item, int column ){
670
    int i;
671
    i++;
108 pingvin 672
    if (root_items_list.indexOf(item) != -1) {
673
        QStringList classesNameList;
674
        QStringList classesID_list;
106 pingvin 675
 
108 pingvin 676
        int index;
677
        classesID_list = class_list_map.keys();
678
        classesNameList = class_list_map.values();
679
        index = classesID_list.indexOf(item->child(0)->text(1));
132 pingvin 680
        if (index != -1) ui->comboBox->setCurrentIndex(index);
108 pingvin 681
 
682
        ui->comboBox->setEnabled(true);
683
 
684
 
685
    }
106 pingvin 686
  //  if ((rootItem1) == (item) ) ui->comboBox->setEnabled(true);
687
else ( ui->comboBox->setEnabled(false));
688
}
110 pingvin 689
 
125 pingvin 690
QMap <QString, QString> MainWindow::getFieldsList(QString class_id){ //    
110 pingvin 691
 
125 pingvin 692
}