Хранилища Subversion OpenInventory

Редакция

Редакция 90 | Редакция 92 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | 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++;
16
 
88 pingvin 17
}
18
 
19
MainWindow::~MainWindow()
20
{
21
    delete ui;
22
}
23
 
24
void MainWindow::changeEvent(QEvent *e)
25
{
26
    QMainWindow::changeEvent(e);
27
    switch (e->type()) {
28
    case QEvent::LanguageChange:
29
        ui->retranslateUi(this);
30
        break;
31
    default:
32
        break;
33
    }
34
}
89 pingvin 35
 
36
 
37
bool MainWindow::readModel(){
38
    bool result;
39
    QString model_str;
40
    QFile file(":/model.txt");
91 pingvin 41
   rootItem1 = new  QTreeWidgetItem(ui->treeWidget);
89 pingvin 42
    rootItem1->setText(0, tr(" 1"));
43
   // rootItem2 = new  QTreeWidgetItem(rootItem1);
44
   // rootItem2->setText(0, tr(" 2"));
45
    rootItem1->setCheckState(0,Qt::Checked);
46
   // rootItem2->setCheckState(0,Qt::Checked);
47
 
48
 
49
 
50
    result = file.open(QIODevice::ReadOnly);
51
    if (result) {
90 pingvin 52
                     model_str = QString(tr(file.readAll()));
89 pingvin 53
                }
54
 
91 pingvin 55
    setupModelData(model_str.split(QString("\n")), rootItem1);
89 pingvin 56
 
57
 
58
    return result;
59
 
60
}
61
 
62
 
63
void MainWindow::setupModelData(const QStringList &lines, QTreeWidgetItem *parent)
64
{
65
    QList<QTreeWidgetItem*> parents;
66
    QList<int> indentations;
67
    parents << parent;
68
    indentations << 0;
69
 
70
    int number = 0;
71
 
72
    while (number < lines.count()) {
73
        int position = 0;
74
        while (position < lines[number].length()) {
75
            if (lines[number].mid(position, 1) != " ")
76
                break;
77
            position++;
78
        }
79
 
80
        QString lineData = lines[number].mid(position).trimmed();
81
 
82
        if (!lineData.isEmpty()) {
83
            // Read the column data from the rest of the line.
84
            QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
85
            QList<QVariant> columnData;
86
            for (int column = 0; column < columnStrings.count(); ++column)
87
                columnData << columnStrings[column];
88
 
89
            if (position > indentations.last()) {
90
                // The last child of the current parent is now the new parent
91
                // unless the current parent has no children.
92
 
93
                if (parents.last()->childCount() > 0) {
94
                    parents << parents.last()->child(parents.last()->childCount()-1);
95
                    indentations << position;
96
                }
97
            } else {
98
                while (position < indentations.last() && parents.count() > 0) {
99
                    parents.pop_back();
100
                    indentations.pop_back();
101
                }
102
            }
103
 
104
            // Append a new item to the current parent's list of children.
105
          // // //  parents.last()->appendChild(new QTreeWidgetItem(columnData, parents.last()));
106
            QTreeWidgetItem* itm_tmp;
107
            itm_tmp = new QTreeWidgetItem( parents.last());
108
 
109
            itm_tmp->setText(0, QString(columnData.at(0).toString()));
110
            if (columnData.at(1).toString() == "true") {
111
                                                            itm_tmp->setCheckState(0,Qt::Checked);
112
                                                        }
113
            else itm_tmp->setCheckState(0,Qt::Unchecked);
90 pingvin 114
            itm_tmp->setText(1, QString(columnData.at(2).toString()));
115
            itm_tmp->setText(2, QVariant(number).toString()); //    - 
116
            itm_tmp->setText(3, QVariant(indentations.size()).toString());
89 pingvin 117
        }
118
 
119
        number++;
120
    }
121
}
122
 
123
 
124
/*
125
void MainWindow::openBase()
126
{
127
 
128
        db = QSqlDatabase::addDatabase("QMYSQL");
129
        pdb = &db;
130
        raportFrm.pdb = &db;
131
 
132
        pdb->setHostName(hostName);
133
        pdb->setDatabaseName(baseName);
134
        pdb->setUserName(userName);
135
        pdb->setPassword(password);
136
        bool ok = pdb->open();
137
        if (!ok) {
138
                                QMessageBox::critical( //     .
139
                                                                                this,                      //  .
140
                                                                                QObject::tr("Database Error"),   // .
141
                                                                                pdb->lastError().text());          //  .
142
                         }
143
        if (ok)
144
        {lineEdit-> insert(tr("  "));
145
        }
146
        else {lineEdit-> insert(tr(" .  : "));
147
                  lineEdit-> insert(pdb->lastError().text());
148
                 }
149
 
150
 
151
 
152
        model = new QSqlTableModel(this);
153
        model->setTable(tableName);
154
 
155
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
156
        model->setSort(0, Qt::AscendingOrder);
157
 
158
        model->select();
159
 
160
 
161
        QSqlField field(tr("age"), QVariant::Int);
162
        field.setValue(QString(tr("123")));
163
 
164
        bool okey;
165
        int index;
166
        QSqlRecord record;
167
 
168
 
169
        tableView->setModel(model);
170
        view.setModel(model);
171
 
172
 
173
        tableView->setAlternatingRowColors(true);
174
        view.setAlternatingRowColors(true);
175
 
176
        tableView->resizeColumnsToContents();
177
 
178
 
179
        view.resizeColumnsToContents();
180
 
181
        tableView->show();
182
 
183
 
184
 
185
        initTreeWidget();
186
}
187
*/
91 pingvin 188
 
189
 
190
void MainWindow::creatModelString(){
191
 
192
    MainWindow::modelString.clear(); //   
193
 //   for (int i =0; i < rootItem1->childCount(); ++i )
194
 
195
    {MainWindow::addChildsString(rootItem1, 0);}
196
 
197
    int root_item_child_count;
198
    int i;
199
    root_item_child_count = rootItem1->childCount();
200
    i++;
201
 
202
}
203
 
204
 
205
void MainWindow::addChildsString(QTreeWidgetItem *parentItem, int level){
206
    int child_count;
207
    child_count = parentItem->childCount();
208
    for (int i=0; i < child_count; ++i){
209
        for (int m=0; m < level; ++m){
210
        modelString.append(tr("    ")); //   -  ,     
211
    }
212
        modelString.append(parentItem->child(i)->text(0)); //    -  
213
        modelString.append(tr("\t\t\t\t")); //   -   
214
        if (parentItem->child(i)->checkState(0) ==  Qt::Checked) {modelString.append(tr("true"));} //   ,  "true",      
215
            else {modelString.append(tr("false"));} //     -  "false",       
216
        modelString.append(tr("\t\t\t\t")); //   -   
217
        modelString.append(parentItem->child(i)->text(1)); //    - ID 
218
         modelString.append(tr("\n")); //  
219
        if (parentItem->child(i)->childCount() > 0) { MainWindow::addChildsString(parentItem->child(i), level+1);}
220
 
221
    }
222
int i;
223
i++;
224
}