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