Хранилища Subversion OpenInventory

Редакция

Редакция 105 | Редакция 107 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | 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
 
342
 
343
QString MainWindow::ClassName(QString class_id){ //     ID
344
    QSqlQuery q;
345
    QString sql_str;
346
    QString class_name_str;
347
    bool ok;
348
    int field_class_name;
349
 
350
    sql_str = tr("select * "
351
                 " from ListOfClasses where ListOfClasses.ID = '"       // ,     
352
                 );
353
 
354
    sql_str.append(class_id);
355
    sql_str.append(tr("' "));
356
 
357
    q.prepare(sql_str);
358
 
359
    ok = q.exec();
360
 
361
 
362
    if (!ok) {
363
                            QMessageBox::critical( //     .
364
                                                                            this,                      //  .
365
                                                                            QObject::tr("Database Error"),   // .
366
                                                                            q.lastError().text());          //  .
367
                     }
368
 
369
    field_class_name = q.record().indexOf(tr("ClassName"));
370
    while(q.next()){
371
 
372
                    class_name_str = q.value(field_class_name).toString();
373
 
374
                    }
375
 
376
    return class_name_str;
377
 
378
}
98 pingvin 379
 
380
void MainWindow::addChildsItems(QTreeWidgetItem *perent_class_item){ //          ,      
100 pingvin 381
QStringList chields_list;
382
QString parent_ID;
383
QTreeWidgetItem *Item_tmp;
384
parent_ID = perent_class_item->text(1);
385
int chields_count, i;
98 pingvin 386
 
100 pingvin 387
chields_list = MainWindow::classChields(parent_ID);
388
if(chields_list.isEmpty()) return;
389
chields_count = chields_list.size();
390
for (int l = 0; l < chields_count; ++l){
391
    QString chield_class_name, chield_id;
392
    chield_id = chields_list.at(l);
393
    chield_class_name = MainWindow::ClassName(chield_id);
394
    Item_tmp = new QTreeWidgetItem(perent_class_item);
395
    Item_tmp->setText(0, chield_class_name);
396
    Item_tmp->setText(1, chield_id);
397
    Item_tmp->setCheckState(0, Qt::Checked);
101 pingvin 398
    MainWindow::addChildsItems(Item_tmp);
98 pingvin 399
}
400
 
100 pingvin 401
i++;
99 pingvin 402
 
100 pingvin 403
}
404
 
405
 
99 pingvin 406
/********************************************************
407
*    "" 
408
*
409
*
410
*
411
********************************************************/
412
 
413
QStringList MainWindow::classChields(QString class_id){
414
   // QMap<QString, QString> map;
415
   // TClass class_tmp;
416
   // QList <TClass> chields_class_list; //   
417
    QStringList result;
418
    QSqlQuery q;
419
    QString sql_str;
420
    QString classIdentifer_str; //      -
421
    QString field_id_str;
422
    bool ok;
423
    int field_classIdentifer;
424
    sql_str = tr("select * "
425
                 " from DescriptionOfClasses where  DescriptionOfClasses.DefaultValue = '"       // ,     
426
                 );
427
    sql_str.append(class_id);
428
 
429
    sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' "));
430
 
431
    q.prepare(sql_str);
432
 
433
    ok = q.exec();
434
    if (!ok) {
435
                            QMessageBox::critical( //     .
436
                                                                            this,                      //  .
437
                                                                            QObject::tr("Database Error"),   // .
438
                                                                            q.lastError().text());          //  .
439
                     }
440
    field_classIdentifer = q.record().indexOf(tr("ClassIdentifer"));
441
      while(q.next()){
442
 
443
                    classIdentifer_str = q.value(field_classIdentifer).toString();
444
                    result.append(classIdentifer_str);
445
                    }
446
 
447
    return result;
448
 
449
  }
102 pingvin 450
 
451
 
452
QMap <QString, QString> MainWindow::getClassList(){ //   
453
QMap <QString, QString> result_map;
454
QSqlQuery q;
455
QString sql_str;
103 pingvin 456
QString class_name_str, class_id_str;
457
 
102 pingvin 458
int field_class_id, field_class_name;
459
bool ok;
460
sql_str = tr("select * "
461
             " from ListOfClasses "       // ,     
462
             );
463
 q.prepare(sql_str);
464
 ok = q.exec();
465
 if (!ok) {
466
                         QMessageBox::critical( //     .
467
                                                                         this,                      //  .
468
                                                                         QObject::tr("Database Error"),   // .
469
                                                                         q.lastError().text());          //  .
470
                         return result_map; //   -   
471
                  }
472
field_class_id = q.record().indexOf(tr("ID"));
473
field_class_name = q.record().indexOf(tr("ClassName"));
474
 
103 pingvin 475
while(q.next()){
102 pingvin 476
 
103 pingvin 477
                class_name_str = q.value(field_class_name).toString();
478
                class_id_str = q.value(field_class_id).toString();
479
                result_map[class_id_str] = class_name_str;
480
                }
102 pingvin 481
 return result_map;
482
}
104 pingvin 483
 
484
 
485
 
486
 
487
 
488
void MainWindow::initComboBox(){
489
 
106 pingvin 490
 
104 pingvin 491
    QStringList classesNameList;
492
    QStringList classesID_list;
493
    QStringList tmp_stringList;
494
 
495
 
496
 
497
    QString tmp_str;
498
 
499
    class_list_map = MainWindow::getClassList();
500
    classesID_list = class_list_map.keys();
501
    classesNameList = class_list_map.values();
502
    QMapIterator<QString, QString> interator(class_list_map);
503
//    ui->comboBox->addItems(classesID_list);
504
  //  ui->comboBox->addItems(classesNameList);
505
    while (interator.hasNext()) {
506
         interator.next();
507
         tmp_str =  interator.value();
508
         tmp_str.append(tr(" \t(ID="));
509
         tmp_str.append(interator.key());
510
         tmp_str.append(tr(")"));
511
         tmp_stringList << tmp_str;
512
 
513
     }
514
    ui->comboBox->addItems(tmp_stringList);
515
 
516
 
517
}
105 pingvin 518
 
519
 
106 pingvin 520
void MainWindow::on_comboBox_currentIndexChanged( int index ){
521
QStringList classesNameList;
522
QStringList classesID_list;
523
QString ID_str;
524
classesID_list = class_list_map.keys();
525
classesNameList = class_list_map.values();
526
ID_str =  classesID_list[index];
527
ui->label->setText(ID_str);
105 pingvin 528
}
529
 
106 pingvin 530
void MainWindow::on_treeWidget_itemChanged ( QTreeWidgetItem * item, int column ){
531
    int i;
532
    i++;
533
 
534
}
535
 
536
 
537
void MainWindow::on_treeWidget_itemClicked ( QTreeWidgetItem * item, int column ){
538
    int i;
539
    i++;
540
if (root_items_list.indexOf(item) != -1) ui->comboBox->setEnabled(true);
541
 
542
  //  if ((rootItem1) == (item) ) ui->comboBox->setEnabled(true);
543
else ( ui->comboBox->setEnabled(false));
544
}