Rev 97 | Rev 99 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
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(); |
97 | pingvin | 24 | MainWindow::buildPreviewModel(tr(" "), tr("3")); |
93 | pingvin | 25 | |
88 | pingvin | 26 | } |
27 | |||
28 | MainWindow::~MainWindow() |
||
29 | { |
||
30 | delete ui; |
||
31 | } |
||
32 | |||
33 | void MainWindow::changeEvent(QEvent *e) |
||
34 | { |
||
35 | QMainWindow::changeEvent(e); |
||
36 | switch (e->type()) { |
||
37 | case QEvent::LanguageChange: |
||
38 | ui->retranslateUi(this); |
||
39 | break; |
||
40 | default: |
||
41 | break; |
||
42 | } |
||
43 | } |
||
89 | pingvin | 44 | |
45 | |||
46 | bool MainWindow::readModel(){ |
||
47 | bool result; |
||
48 | QString model_str; |
||
49 | QFile file(":/model.txt"); |
||
92 | pingvin | 50 | |
91 | pingvin | 51 | rootItem1 = new QTreeWidgetItem(ui->treeWidget); |
89 | pingvin | 52 | rootItem1->setText(0, tr(" 1")); |
53 | // rootItem2 = new QTreeWidgetItem(rootItem1); |
||
54 | // rootItem2->setText(0, tr(" 2")); |
||
55 | rootItem1->setCheckState(0,Qt::Checked); |
||
56 | // rootItem2->setCheckState(0,Qt::Checked); |
||
57 | |||
58 | |||
59 | |||
60 | result = file.open(QIODevice::ReadOnly); |
||
61 | if (result) { |
||
90 | pingvin | 62 | model_str = QString(tr(file.readAll())); |
89 | pingvin | 63 | } |
64 | |||
91 | pingvin | 65 | setupModelData(model_str.split(QString("\n")), rootItem1); |
89 | pingvin | 66 | |
67 | |||
68 | return result; |
||
69 | |||
70 | } |
||
71 | |||
72 | |||
73 | void MainWindow::setupModelData(const QStringList &lines, QTreeWidgetItem *parent) |
||
74 | { |
||
75 | QList<QTreeWidgetItem*> parents; |
||
76 | QList<int> indentations; |
||
77 | parents << parent; |
||
78 | indentations << 0; |
||
79 | |||
80 | int number = 0; |
||
81 | |||
82 | while (number < lines.count()) { |
||
83 | int position = 0; |
||
84 | while (position < lines[number].length()) { |
||
85 | if (lines[number].mid(position, 1) != " ") |
||
86 | break; |
||
87 | position++; |
||
88 | } |
||
89 | |||
90 | QString lineData = lines[number].mid(position).trimmed(); |
||
91 | |||
92 | if (!lineData.isEmpty()) { |
||
93 | // Read the column data from the rest of the line. |
||
94 | QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts); |
||
95 | QList<QVariant> columnData; |
||
96 | for (int column = 0; column < columnStrings.count(); ++column) |
||
97 | columnData << columnStrings[column]; |
||
98 | |||
99 | if (position > indentations.last()) { |
||
100 | // The last child of the current parent is now the new parent |
||
101 | // unless the current parent has no children. |
||
102 | |||
103 | if (parents.last()->childCount() > 0) { |
||
104 | parents << parents.last()->child(parents.last()->childCount()-1); |
||
105 | indentations << position; |
||
106 | } |
||
107 | } else { |
||
108 | while (position < indentations.last() && parents.count() > 0) { |
||
109 | parents.pop_back(); |
||
110 | indentations.pop_back(); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | // Append a new item to the current parent's list of children. |
||
115 | // // // parents.last()->appendChild(new QTreeWidgetItem(columnData, parents.last())); |
||
116 | QTreeWidgetItem* itm_tmp; |
||
117 | itm_tmp = new QTreeWidgetItem( parents.last()); |
||
118 | |||
119 | itm_tmp->setText(0, QString(columnData.at(0).toString())); |
||
120 | if (columnData.at(1).toString() == "true") { |
||
121 | itm_tmp->setCheckState(0,Qt::Checked); |
||
122 | } |
||
123 | else itm_tmp->setCheckState(0,Qt::Unchecked); |
||
90 | pingvin | 124 | itm_tmp->setText(1, QString(columnData.at(2).toString())); |
125 | itm_tmp->setText(2, QVariant(number).toString()); // - |
||
126 | itm_tmp->setText(3, QVariant(indentations.size()).toString()); |
||
89 | pingvin | 127 | } |
128 | |||
129 | number++; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | |||
134 | /* |
||
135 | void MainWindow::openBase() |
||
136 | { |
||
137 | |||
138 | db = QSqlDatabase::addDatabase("QMYSQL"); |
||
139 | pdb = &db; |
||
140 | raportFrm.pdb = &db; |
||
141 | |||
142 | pdb->setHostName(hostName); |
||
143 | pdb->setDatabaseName(baseName); |
||
144 | pdb->setUserName(userName); |
||
145 | pdb->setPassword(password); |
||
146 | bool ok = pdb->open(); |
||
147 | if (!ok) { |
||
148 | QMessageBox::critical( // . |
||
149 | this, // . |
||
150 | QObject::tr("Database Error"), // . |
||
151 | pdb->lastError().text()); // . |
||
152 | } |
||
153 | if (ok) |
||
154 | {lineEdit-> insert(tr(" ")); |
||
155 | } |
||
156 | else {lineEdit-> insert(tr(" . : ")); |
||
157 | lineEdit-> insert(pdb->lastError().text()); |
||
158 | } |
||
159 | |||
160 | |||
161 | |||
162 | model = new QSqlTableModel(this); |
||
163 | model->setTable(tableName); |
||
164 | |||
165 | model->setEditStrategy(QSqlTableModel::OnManualSubmit); |
||
166 | model->setSort(0, Qt::AscendingOrder); |
||
167 | |||
168 | model->select(); |
||
169 | |||
170 | |||
171 | QSqlField field(tr("age"), QVariant::Int); |
||
172 | field.setValue(QString(tr("123"))); |
||
173 | |||
174 | bool okey; |
||
175 | int index; |
||
176 | QSqlRecord record; |
||
177 | |||
178 | |||
179 | tableView->setModel(model); |
||
180 | view.setModel(model); |
||
181 | |||
182 | |||
183 | tableView->setAlternatingRowColors(true); |
||
184 | view.setAlternatingRowColors(true); |
||
185 | |||
186 | tableView->resizeColumnsToContents(); |
||
187 | |||
188 | |||
189 | view.resizeColumnsToContents(); |
||
190 | |||
191 | tableView->show(); |
||
192 | |||
193 | |||
194 | |||
195 | initTreeWidget(); |
||
196 | } |
||
197 | */ |
||
91 | pingvin | 198 | |
199 | |||
200 | void MainWindow::creatModelString(){ |
||
201 | |||
202 | MainWindow::modelString.clear(); // |
||
203 | // for (int i =0; i < rootItem1->childCount(); ++i ) |
||
204 | |||
205 | {MainWindow::addChildsString(rootItem1, 0);} |
||
206 | |||
207 | int root_item_child_count; |
||
208 | int i; |
||
209 | root_item_child_count = rootItem1->childCount(); |
||
210 | i++; |
||
211 | |||
212 | } |
||
213 | |||
214 | |||
215 | void MainWindow::addChildsString(QTreeWidgetItem *parentItem, int level){ |
||
216 | int child_count; |
||
217 | child_count = parentItem->childCount(); |
||
218 | for (int i=0; i < child_count; ++i){ |
||
219 | for (int m=0; m < level; ++m){ |
||
220 | modelString.append(tr(" ")); // - , |
||
221 | } |
||
222 | modelString.append(parentItem->child(i)->text(0)); // - |
||
223 | modelString.append(tr("\t\t\t\t")); // - |
||
224 | if (parentItem->child(i)->checkState(0) == Qt::Checked) {modelString.append(tr("true"));} // , "true", |
||
225 | else {modelString.append(tr("false"));} // - "false", |
||
226 | modelString.append(tr("\t\t\t\t")); // - |
||
227 | modelString.append(parentItem->child(i)->text(1)); // - ID |
||
228 | modelString.append(tr("\n")); // |
||
229 | if (parentItem->child(i)->childCount() > 0) { MainWindow::addChildsString(parentItem->child(i), level+1);} |
||
230 | |||
231 | } |
||
232 | int i; |
||
233 | i++; |
||
234 | } |
||
93 | pingvin | 235 | |
236 | |||
237 | bool MainWindow::openDataBase(){ |
||
238 | QString errorString; |
||
239 | sql = QSqlDatabase::addDatabase("QMYSQL"); |
||
240 | sql.setDatabaseName(tr("an_db")); |
||
241 | sql.setHostName(tr("localhost")); |
||
242 | sql.setUserName(tr("an")); |
||
243 | sql.setPassword(tr("393939")); |
||
244 | |||
245 | bool ok; |
||
246 | ok = sql.open(); |
||
247 | |||
248 | /* |
||
249 | if (!ok) { |
||
250 | QMessageBox::critical( // . |
||
251 | this, // . |
||
252 | QObject::tr("Database Error"), // . |
||
253 | sql.lastError().text() ); // . |
||
254 | |||
255 | |||
256 | } |
||
257 | |||
258 | */ |
||
259 | if (!ok) { |
||
260 | QMessageBox::critical( // . |
||
261 | this, // . |
||
262 | QObject::tr("Database Error"), // . |
||
263 | sql.lastError().text()); // . |
||
264 | } |
||
265 | else { |
||
266 | |||
267 | QMessageBox::information( // . |
||
268 | this, // . |
||
269 | QObject::tr("Database Connect"), // . |
||
270 | QObject::tr(" .")); // . |
||
271 | |||
272 | |||
273 | |||
274 | |||
275 | |||
276 | } |
||
277 | |||
278 | return ok; |
||
279 | } |
||
94 | pingvin | 280 | |
281 | |||
97 | pingvin | 282 | bool MainWindow::buildPreviewModel(QString modelName, QString rootClassID){ |
94 | pingvin | 283 | QSqlQuery q; |
284 | QString sql_str; |
||
95 | pingvin | 285 | QString field_name_str; |
286 | QString root_class_name; |
||
287 | bool ok; |
||
94 | pingvin | 288 | int field_name; |
95 | pingvin | 289 | |
290 | //ui->treeWidget->clear(); |
||
291 | |||
292 | root_class_name = MainWindow::ClassName(rootClassID); // |
||
293 | |||
294 | rootItem1 = new QTreeWidgetItem(ui->treeWidget); |
||
97 | pingvin | 295 | rootItem1->setText(0, modelName); |
296 | |||
95 | pingvin | 297 | rootItem1->setCheckState(0,Qt::Checked); |
298 | |||
299 | |||
97 | pingvin | 300 | rootItem2 = new QTreeWidgetItem(rootItem1); |
301 | rootItem2->setText(0, root_class_name); |
||
302 | rootItem2->setText(1, rootClassID); |
||
303 | rootItem2->setCheckState(0,Qt::Checked); |
||
95 | pingvin | 304 | |
98 | pingvin | 305 | addChildsItems(rootItem2); |
95 | pingvin | 306 | |
94 | pingvin | 307 | sql_str = tr("select * " |
95 | pingvin | 308 | " from DescriptionOfClasses where DescriptionOfClasses.DefaultValue = '" // , |
94 | pingvin | 309 | ); |
310 | sql_str.append(rootClassID); |
||
311 | |||
95 | pingvin | 312 | sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' ")); |
94 | pingvin | 313 | |
95 | pingvin | 314 | q.prepare(sql_str); |
315 | |||
316 | ok = q.exec(); |
||
317 | if (!ok) { |
||
318 | QMessageBox::critical( // . |
||
319 | this, // . |
||
320 | QObject::tr("Database Error"), // . |
||
321 | q.lastError().text()); // . |
||
322 | } |
||
323 | field_name = q.record().indexOf(tr("FieldName")); |
||
324 | while(q.next()){ |
||
325 | |||
326 | field_name_str = q.value(field_name).toString(); |
||
327 | |||
328 | } |
||
329 | |||
94 | pingvin | 330 | } |
95 | pingvin | 331 | |
332 | |||
333 | |||
334 | QString MainWindow::ClassName(QString class_id){ // ID |
||
335 | QSqlQuery q; |
||
336 | QString sql_str; |
||
337 | QString class_name_str; |
||
338 | bool ok; |
||
339 | int field_class_name; |
||
340 | |||
341 | sql_str = tr("select * " |
||
342 | " from ListOfClasses where ListOfClasses.ID = '" // , |
||
343 | ); |
||
344 | |||
345 | sql_str.append(class_id); |
||
346 | sql_str.append(tr("' ")); |
||
347 | |||
348 | q.prepare(sql_str); |
||
349 | |||
350 | ok = q.exec(); |
||
351 | |||
352 | |||
353 | if (!ok) { |
||
354 | QMessageBox::critical( // . |
||
355 | this, // . |
||
356 | QObject::tr("Database Error"), // . |
||
357 | q.lastError().text()); // . |
||
358 | } |
||
359 | |||
360 | field_class_name = q.record().indexOf(tr("ClassName")); |
||
361 | while(q.next()){ |
||
362 | |||
363 | class_name_str = q.value(field_class_name).toString(); |
||
364 | |||
365 | } |
||
366 | |||
367 | return class_name_str; |
||
368 | |||
369 | } |
||
98 | pingvin | 370 | |
371 | void MainWindow::addChildsItems(QTreeWidgetItem *perent_class_item){ // , |
||
372 | |||
373 | } |
||
374 |