Редакция 78 | Редакция 80 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
61 | pingvin | 1 | #include "modelfordescriptiontable.h" |
2 | |||
3 | ModelForDescriptionTable::ModelForDescriptionTable(QObject *parent) : |
||
4 | QSqlTableModel(parent) |
||
5 | { |
||
6 | setEditStrategy(QSqlTableModel::OnManualSubmit); |
||
7 | } |
||
8 | |||
9 | Qt::ItemFlags ModelForDescriptionTable::flags( |
||
10 | const QModelIndex &index) const { |
||
11 | Qt::ItemFlags flags = QSqlTableModel::flags(index); |
||
62 | pingvin | 12 | if ((index.column() != 5) && (index.column() != 0)) |
61 | pingvin | 13 | flags |= Qt::ItemIsEditable; |
14 | //if (index.column() == 4) |
||
15 | // flags |= Qt::ItemIsUserCheckable; |
||
16 | else |
||
17 | { |
||
62 | pingvin | 18 | if ((index.column() == 0) || (index.column() == 5)) flags &= ~Qt::ItemIsEditable; // |
61 | pingvin | 19 | } |
20 | return flags; |
||
21 | } |
||
22 | |||
23 | |||
24 | bool ModelForDescriptionTable::setData(const QModelIndex &index, const QVariant &value, int role) { |
||
25 | //// if (index.column()==3) return false; // |
||
26 | bool lResult = false; |
||
75 | pingvin | 27 | QVariant old_value; // |
28 | old_value = data(index); |
||
61 | pingvin | 29 | |
75 | pingvin | 30 | |
61 | pingvin | 31 | /* |
32 | QString str_tmp; |
||
33 | if (index.column()==1) { // |
||
34 | str_tmp = value.toString(); // |
||
35 | str_tmp.append(tr(" instance")); |
||
36 | } |
||
37 | */ |
||
38 | /* |
||
39 | QString str_tmp; |
||
40 | if (index.column()==0) { // |
||
41 | str_tmp = value.toString(); // |
||
42 | str_tmp.append(tr("_instance")); |
||
43 | } |
||
44 | */ |
||
77 | pingvin | 45 | |
61 | pingvin | 46 | QModelIndex newindex; |
47 | |||
48 | |||
75 | pingvin | 49 | |
78 | pingvin | 50 | |
51 | |||
75 | pingvin | 52 | if (index.column()==1){ // |
53 | QSqlQuery qu; |
||
54 | QString old_field_name; // |
||
55 | QString new_field_name; // |
||
56 | QString query_str; |
||
57 | QString type_str; // |
||
58 | old_field_name = old_value.toString(); // ( ) |
||
59 | new_field_name = value.toString(); |
||
60 | type_str = data(ModelForDescriptionTable::index(index.row(), 3)).toString(); // 3 , |
||
61 | query_str = tr("ALTER TABLE "); |
||
62 | query_str.append(ModelForDescriptionTable::Inctance); |
||
63 | query_str.append(tr(" CHANGE COLUMN `")); |
||
64 | query_str.append(old_field_name); |
||
65 | query_str.append(tr("` `")); |
||
66 | query_str.append(new_field_name); |
||
67 | query_str.append(tr("` ")); |
||
68 | query_str.append(type_str); |
||
69 | query_str.append(tr(" CHARACTER SET cp1251 COLLATE cp1251_general_ci DEFAULT NULL")); |
||
70 | qu.prepare(query_str); |
||
71 | bool ok; |
||
72 | ok = qu.exec(); |
||
73 | if (!ok) return false; |
||
79 | pingvin | 74 | } |
75 | pingvin | 75 | /* |
76 | if (!ok) { |
||
77 | |||
78 | QMessageBox::critical( // . |
||
79 | this, // . |
||
80 | QObject::tr("Database Error"), // . |
||
81 | qu.lastError().text() ); // . |
||
82 | |||
83 | return ok; // |
||
84 | |||
85 | } |
||
86 | |||
87 | */ |
||
88 | |||
89 | |||
90 | // rec.setValue(index.column()+2, QVariant(str_tmp)); |
||
91 | // rec.setGenerated(index.column()+2, true); |
||
92 | // newindex = MyModel::index(index.row(), index.column()+2); |
||
93 | // QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole); |
||
94 | |||
95 | |||
79 | pingvin | 96 | |
61 | pingvin | 97 | /* |
98 | |||
99 | if (index.column()==0){ |
||
100 | rec.setValue(index.column()+3, QVariant(str_tmp)); |
||
101 | rec.setGenerated(index.column()+3, true); |
||
102 | newindex = MyModel::index(index.row(), index.column()+3); |
||
103 | QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole); |
||
104 | } |
||
105 | */ |
||
106 | |||
77 | pingvin | 107 | if (index.column()==3){ // |
108 | ///ALTER TABLE `an_db`.`1_Inctance` MODIFY COLUMN `int` INTEGER; |
||
79 | pingvin | 109 | |
110 | bool ok; |
||
111 | ok = setData(ModelForDescriptionTable::index(index.row(), 4), QVariant(tr("NULL")), Qt::EditRole); // |
||
112 | if (ok) { |
||
113 | |||
114 | |||
77 | pingvin | 115 | QSqlQuery qu; |
116 | QString old_type, new_type, field_name, query_str; |
||
117 | old_type = old_value.toString(); // ( ) |
||
118 | new_type = value.toString(); // |
||
119 | field_name = data(ModelForDescriptionTable::index(index.row(), 1)).toString(); // 1 , |
||
120 | query_str = tr("ALTER TABLE "); |
||
121 | query_str.append(ModelForDescriptionTable::Inctance); |
||
122 | query_str.append(tr(" MODIFY COLUMN `")); |
||
123 | query_str.append(field_name); |
||
124 | query_str.append(tr("` ")); |
||
125 | query_str.append(new_type); |
||
126 | qu.prepare(query_str); |
||
79 | pingvin | 127 | |
77 | pingvin | 128 | ok = qu.exec(); |
129 | if (!ok) return false; |
||
78 | pingvin | 130 | } |
79 | pingvin | 131 | else return false; |
132 | } |
||
78 | pingvin | 133 | if (index.column()==4){ // |
134 | ///ALTER TABLE `an_db`.`2_Inctance` MODIFY COLUMN `` CHAR(10) CHARACTER SET cp1251 COLLATE cp1251_general_ci DEFAULT ''; |
||
135 | QSqlQuery qu; |
||
136 | QString field_type, new_default_value, field_name, query_str; |
||
137 | |||
138 | field_type = data(ModelForDescriptionTable::index(index.row(), 3)).toString(); // 3 , |
||
139 | field_name = data(ModelForDescriptionTable::index(index.row(), 1)).toString(); // 1 , |
||
140 | new_default_value = value.toString(); |
||
141 | query_str = tr("ALTER TABLE "); |
||
142 | query_str.append(ModelForDescriptionTable::Inctance); |
||
143 | query_str.append(tr(" MODIFY COLUMN `")); |
||
144 | query_str.append(field_name); |
||
145 | query_str.append(tr("` ")); |
||
146 | query_str.append(field_type); |
||
147 | |||
148 | if (new_default_value != tr("NULL")) { |
||
149 | query_str.append(tr(" DEFAULT '")); |
||
150 | |||
151 | |||
152 | query_str.append(new_default_value); |
||
153 | query_str.append(tr("'")); } |
||
154 | else { |
||
155 | query_str.append(tr(" DEFAULT ")); // NULL |
||
156 | query_str.append(new_default_value); |
||
157 | } |
||
158 | qu.prepare(query_str); |
||
159 | bool ok; |
||
160 | ok = qu.exec(); |
||
161 | if (!ok) return false; |
||
162 | |||
77 | pingvin | 163 | } |
164 | |||
78 | pingvin | 165 | |
166 | QSqlRecord rec = record(index.row()); |
||
167 | rec.setValue(index.column(), value); |
||
168 | rec.setGenerated(index.column(), true); |
||
169 | |||
170 | |||
77 | pingvin | 171 | lResult = QSqlTableModel::setData(index, value, role); |
172 | if (lResult) { |
||
61 | pingvin | 173 | lResult = updateRowInTable(index.row(), rec); |
77 | pingvin | 174 | emit field_changed(); |
61 | pingvin | 175 | /// QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole); |
176 | |||
177 | |||
178 | } |
||
77 | pingvin | 179 | |
180 | |||
181 | |||
182 | |||
61 | pingvin | 183 | return lResult; |
184 | } |
||
185 | |||
186 | |||
187 | |||
188 | QVariant ModelForDescriptionTable::data(const QModelIndex &index, int role) const { |
||
189 | |||
190 | QVariant value = QSqlTableModel::data(index, role); |
||
191 | switch (role) { |
||
192 | case Qt::DisplayRole: return value; |
||
193 | case Qt::EditRole: return value; |
||
194 | case Qt::TextColorRole: |
||
195 | if(index.column() == 1) |
||
196 | return qVariantFromValue(QColor(Qt::darkGreen)); |
||
197 | else |
||
198 | return value; |
||
199 | case Qt::TextAlignmentRole: return value; |
||
200 | case Qt::FontRole: |
||
201 | if(index.column() == 1) { |
||
202 | QFont font = QFont("Helvetica", 16, QFont::Bold); |
||
203 | return qVariantFromValue(font); |
||
204 | }else |
||
205 | return value; |
||
206 | |||
207 | case Qt::BackgroundColorRole: { |
||
208 | int a = (index.row() % 2) ? 14 : 0; |
||
209 | if(index.column() == 2) |
||
210 | return qVariantFromValue(QColor(220,240-a,230-a)); |
||
62 | pingvin | 211 | else if(index.column() == 6) |
61 | pingvin | 212 | return qVariantFromValue(QColor(200,220-a,255-a)); |
213 | else |
||
214 | return value; |
||
215 | } |
||
216 | |||
217 | |||
218 | |||
219 | case Qt::CheckStateRole: return value; |
||
220 | |||
221 | case Qt::SizeHintRole:return value; |
||
222 | |||
223 | } |
||
224 | return value; |
||
225 | } |
||
75 | pingvin | 226 | |
227 | |||
228 | void ModelForDescriptionTable::setInctance(QString currInctatce){ // - |
||
229 | ModelForDescriptionTable::Inctance = currInctatce; |
||
230 | } |