Хранилища Subversion OpenInventory

Редакция

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