Хранилища Subversion OpenInventory

Редакция

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