Редакция 86 | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | RSS
Редакция | Автор | № строки | Строка |
---|---|---|---|
84 | pingvin | 1 | #include "modelforpointers.h" |
2 | |||
3 | ModelForPointers::ModelForPointers(QObject *parent) : |
||
4 | QSqlTableModel(parent) |
||
5 | { |
||
6 | setEditStrategy(QSqlTableModel::OnManualSubmit); |
||
7 | } |
||
8 | |||
9 | Qt::ItemFlags ModelForPointers::flags( |
||
10 | const QModelIndex &index) const { |
||
11 | Qt::ItemFlags flags = QSqlTableModel::flags(index); |
||
12 | if ((index.column() != 3) && (index.column() != 0)) |
||
13 | flags |= Qt::ItemIsEditable; |
||
14 | //if (index.column() == 4) |
||
15 | // flags |= Qt::ItemIsUserCheckable; |
||
16 | else |
||
17 | { |
||
18 | if ((index.column() == 3) || (index.column() == 0)) flags &= ~Qt::ItemIsEditable; // |
||
19 | } |
||
20 | return flags; |
||
21 | } |
||
22 | |||
23 | |||
24 | bool ModelForPointers::setData(const QModelIndex &index, const QVariant &value, int role) { |
||
25 | //// if (index.column()==3) return false; // |
||
26 | bool lResult = false; |
||
86 | pingvin | 27 | QVariant old_value; // |
28 | old_value = data(index); |
||
84 | pingvin | 29 | |
86 | pingvin | 30 | if (index.column()==1){ // |
31 | QSqlQuery qu; |
||
32 | QString old_field_name; // |
||
33 | QString new_field_name; // |
||
34 | QString query_str; |
||
35 | QString type_str; // |
||
36 | old_field_name = old_value.toString(); // ( ) |
||
37 | new_field_name = value.toString(); |
||
38 | // type_str = data(ModelForPointers::index(index.row(), 3)).toString(); // 3 , |
||
39 | type_str = tr("CHAR(10)"); |
||
84 | pingvin | 40 | |
86 | pingvin | 41 | query_str = tr("ALTER TABLE "); |
42 | query_str.append(ModelForPointers::Inctance); |
||
43 | query_str.append(tr(" CHANGE COLUMN `")); |
||
44 | query_str.append(old_field_name); |
||
45 | query_str.append(tr("` `")); |
||
46 | query_str.append(new_field_name); |
||
47 | query_str.append(tr("` ")); |
||
48 | query_str.append(type_str); |
||
49 | query_str.append(tr(" CHARACTER SET cp1251 COLLATE cp1251_general_ci DEFAULT NULL")); |
||
50 | qu.prepare(query_str); |
||
51 | bool ok; |
||
52 | ok = qu.exec(); |
||
53 | if (!ok) return false; |
||
54 | } |
||
55 | |||
56 | |||
57 | |||
84 | pingvin | 58 | lResult = QSqlTableModel::setData(index, value, role); |
59 | if (lResult) { |
||
60 | QModelIndex newindex; |
||
61 | QSqlRecord rec = record(index.row()); |
||
62 | |||
63 | rec.setValue(index.column(), value); |
||
64 | rec.setGenerated(index.column(), true); |
||
65 | |||
66 | |||
67 | lResult = updateRowInTable(index.row(), rec); |
||
86 | pingvin | 68 | emit field_changed(); |
84 | pingvin | 69 | /// QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole); |
70 | |||
71 | |||
72 | } |
||
73 | return lResult; |
||
74 | } |
||
75 | |||
76 | |||
77 | |||
78 | QVariant ModelForPointers::data(const QModelIndex &index, int role) const { |
||
79 | |||
80 | QVariant value = QSqlTableModel::data(index, role); |
||
81 | switch (role) { |
||
134 | pingvin | 82 | case Qt::DisplayRole: { |
83 | if(index.column() == 4){ |
||
84 | QSqlQuery q; |
||
85 | QString sql_str; |
||
86 | bool ok; |
||
87 | sql_str = tr("select * from `ListOfClasses` where `ID` = "); |
||
88 | sql_str.append(value.toString()); |
||
89 | |||
90 | q.prepare(sql_str); |
||
91 | |||
92 | ok = q.exec(); |
||
93 | |||
94 | if (ok) { |
||
95 | while(q.next()){ |
||
96 | QString class_name = q.value(1).toString(); |
||
97 | if (!class_name.isEmpty()) return class_name; |
||
98 | |||
99 | } |
||
100 | |||
101 | |||
102 | } |
||
103 | |||
104 | } |
||
105 | |||
106 | |||
107 | return value; |
||
108 | } |
||
109 | |||
110 | |||
111 | |||
84 | pingvin | 112 | case Qt::EditRole: return value; |
113 | case Qt::TextColorRole: |
||
114 | if(index.column() == 1) |
||
115 | return qVariantFromValue(QColor(Qt::blue)); |
||
116 | else |
||
117 | return value; |
||
118 | case Qt::TextAlignmentRole: return value; |
||
119 | case Qt::FontRole: |
||
120 | if(index.column() == 1) { |
||
121 | QFont font = QFont("Helvetica", 16, QFont::Bold); |
||
122 | return qVariantFromValue(font); |
||
123 | }else |
||
124 | return value; |
||
125 | |||
126 | case Qt::BackgroundColorRole: { |
||
127 | int a = (index.row() % 2) ? 14 : 0; |
||
128 | if(index.column() == 2) |
||
129 | return qVariantFromValue(QColor(220,240-a,230-a)); |
||
130 | else if(index.column() == 4) |
||
131 | return qVariantFromValue(QColor(200,220-a,255-a)); |
||
132 | else |
||
133 | return value; |
||
134 | } |
||
135 | |||
136 | |||
137 | |||
138 | case Qt::CheckStateRole: return value; |
||
139 | |||
140 | case Qt::SizeHintRole:return value; |
||
141 | |||
142 | } |
||
143 | return value; |
||
144 | } |
||
145 | |||
146 | void ModelForPointers::setInctance(QString currInctatce){ // - |
||
147 | ModelForPointers::Inctance = currInctatce; |
||
148 | } |
||
149 | |||
150 | |||
151 | bool ModelForPointers::onlySetData(const QModelIndex &index, const QVariant &value, int role){ |
||
152 | bool lResult = false; |
||
153 | |||
154 | QSqlRecord rec = record(index.row()); |
||
155 | rec.setValue(index.column(), value); |
||
156 | rec.setGenerated(index.column(), true); |
||
157 | |||
158 | |||
159 | lResult = QSqlTableModel::setData(index, value, role); |
||
160 | if (lResult) { |
||
161 | lResult = updateRowInTable(index.row(), rec); |
||
162 | // emit field_changed(); |
||
163 | /// QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole); |
||
164 | } |
||
165 | |||
166 | |||
167 | |||
168 | |||
169 | return lResult; |
||
170 | |||
171 | } |
||
172 |