Редакция 62 | Редакция 76 | К новейшей редакции | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 62 | Редакция 75 | ||
---|---|---|---|
1 | #include "modelfordescriptiontable.h"
|
1 | #include "modelfordescriptiontable.h"
|
2 | 2 | ||
3 | ModelForDescriptionTable::ModelForDescriptionTable(QObject *parent) : |
3 | ModelForDescriptionTable::ModelForDescriptionTable(QObject *parent) : |
4 | QSqlTableModel(parent) |
4 | QSqlTableModel(parent) |
5 | {
|
5 | {
|
6 | setEditStrategy(QSqlTableModel::OnManualSubmit); |
6 | setEditStrategy(QSqlTableModel::OnManualSubmit); |
7 | }
|
7 | }
|
8 | 8 | ||
9 | Qt::ItemFlags ModelForDescriptionTable::flags( |
9 | Qt::ItemFlags ModelForDescriptionTable::flags( |
10 | const QModelIndex &index) const { |
10 | const QModelIndex &index) const { |
11 | Qt::ItemFlags flags = QSqlTableModel::flags(index); |
11 | Qt::ItemFlags flags = QSqlTableModel::flags(index); |
12 | if ((index.column() != 5) && (index.column() != 0)) |
12 | if ((index.column() != 5) && (index.column() != 0)) |
13 | flags |= Qt::ItemIsEditable; |
13 | flags |= Qt::ItemIsEditable; |
14 | //if (index.column() == 4)
|
14 | //if (index.column() == 4)
|
15 | // flags |= Qt::ItemIsUserCheckable;
|
15 | // flags |= Qt::ItemIsUserCheckable;
|
16 | else
|
16 | else
|
17 | {
|
17 | {
|
18 | if ((index.column() == 0) || (index.column() == 5)) flags &= ~Qt::ItemIsEditable; // çàïðåùàåì ðåäàêòèðîâàíèå òðåòüåãî ñòîëáöà |
18 | if ((index.column() == 0) || (index.column() == 5)) flags &= ~Qt::ItemIsEditable; // çàïðåùàåì ðåäàêòèðîâàíèå òðåòüåãî ñòîëáöà |
19 | }
|
19 | }
|
20 | return flags; |
20 | return flags; |
21 | }
|
21 | }
|
22 | 22 | ||
23 | 23 | ||
24 | bool ModelForDescriptionTable::setData(const QModelIndex &index, const QVariant &value, int role) { |
24 | bool ModelForDescriptionTable::setData(const QModelIndex &index, const QVariant &value, int role) { |
25 | //// if (index.column()==3) return false; // â òðåòüåé êîëîíêå íå äàåì ïîìåíÿòü çíà÷åíèå
|
25 | //// if (index.column()==3) return false; // â òðåòüåé êîëîíêå íå äàåì ïîìåíÿòü çíà÷åíèå
|
26 | bool lResult = false; |
26 | bool lResult = false; |
- | 27 | QVariant old_value; // ñîõðàíèì ñòàðîå çíà÷åíèå |
|
- | 28 | old_value = data(index); |
|
- | 29 | ||
27 | 30 | ||
28 | /*
|
31 | /*
|
29 | QString str_tmp;
|
32 | QString str_tmp;
|
30 | if (index.column()==1) { // åñëè ìåíÿåì èìÿ êëàññà
|
33 | if (index.column()==1) { // åñëè ìåíÿåì èìÿ êëàññà
|
31 | str_tmp = value.toString(); // òî ïîìåíÿåì è èìÿ òàáëèöû
|
34 | str_tmp = value.toString(); // òî ïîìåíÿåì è èìÿ òàáëèöû
|
32 | str_tmp.append(tr(" instance"));
|
35 | str_tmp.append(tr(" instance"));
|
33 | }
|
36 | }
|
34 | */
|
37 | */
|
35 | /*
|
38 | /*
|
36 | QString str_tmp;
|
39 | QString str_tmp;
|
37 | if (index.column()==0) { // åñëè ìåíÿåì èìÿ êëàññà
|
40 | if (index.column()==0) { // åñëè ìåíÿåì èìÿ êëàññà
|
38 | str_tmp = value.toString(); // òî ïîìåíÿåì è èìÿ òàáëèöû
|
41 | str_tmp = value.toString(); // òî ïîìåíÿåì è èìÿ òàáëèöû
|
39 | str_tmp.append(tr("_instance"));
|
42 | str_tmp.append(tr("_instance"));
|
40 | }
|
43 | }
|
41 | */
|
44 | */
|
42 | lResult = QSqlTableModel::setData(index, value, role); |
45 | lResult = QSqlTableModel::setData(index, value, role); |
43 | if (lResult) { |
46 | if (lResult) { |
44 | QModelIndex newindex;
|
47 | QModelIndex newindex;
|
45 | QSqlRecord rec = record(index.row()); |
48 | QSqlRecord rec = record(index.row()); |
46 | 49 | ||
47 | rec.setValue(index.column(), value); |
50 | rec.setValue(index.column(), value); |
48 | rec.setGenerated(index.column(), true); |
51 | rec.setGenerated(index.column(), true); |
49 | 52 | ||
- | 53 | ||
- | 54 | if (index.column()==1){ // åñëè ïîìåíÿëîñü èìÿ ïîëÿ |
|
- | 55 | QSqlQuery qu;
|
|
- | 56 | QString old_field_name; // ñòàðîå èìÿ ïîëÿ |
|
- | 57 | QString new_field_name; // íîâîå èìÿ ïîëÿ |
|
- | 58 | QString query_str;
|
|
- | 59 | QString type_str; // òèï äàííûõ ïîëÿ |
|
- | 60 | old_field_name = old_value.toString(); // îïðåäåëèì òåêóùåå (êîòîðîå íóæíî ïîìåíÿòü) èìÿ ïîëÿ |
|
- | 61 | new_field_name = value.toString(); |
|
- | 62 | type_str = data(ModelForDescriptionTable::index(index.row(), 3)).toString(); // òèï ïîëÿ õðàíèòüñÿ â 3 ñòîëáöå, ñ÷èòûâàåì åãî çíà÷åíèå |
|
- | 63 | query_str = tr("ALTER TABLE "); |
|
- | 64 | query_str.append(ModelForDescriptionTable::Inctance); |
|
- | 65 | query_str.append(tr(" CHANGE COLUMN `")); |
|
- | 66 | query_str.append(old_field_name); |
|
- | 67 | query_str.append(tr("` `")); |
|
- | 68 | query_str.append(new_field_name); |
|
- | 69 | query_str.append(tr("` ")); |
|
- | 70 | query_str.append(type_str); |
|
- | 71 | query_str.append(tr(" CHARACTER SET cp1251 COLLATE cp1251_general_ci DEFAULT NULL")); |
|
- | 72 | qu.prepare(query_str); |
|
- | 73 | bool ok; |
|
- | 74 | ok = qu.exec(); |
|
- | 75 | if (!ok) return false; |
|
- | 76 | ||
50 | /*
|
77 | /*
|
51 | if (index.column()==1){
|
78 | if (!ok) {
|
- | 79 | ||
- | 80 | QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
|
|
- | 81 | this, // Ðîäèòåëüñêèé âèäæåò.
|
|
- | 82 | QObject::tr("Database Error"), // Çàãîëîâîê.
|
|
- | 83 | qu.lastError().text() ); // Òåêñò ñîîáùåíèÿ.
|
|
- | 84 | ||
- | 85 | return ok; // âûõîäèì íå ïðèìåíÿÿ èçìåíåíèé
|
|
- | 86 | ||
- | 87 | }
|
|
- | 88 | ||
- | 89 | */
|
|
- | 90 | ||
- | 91 | ||
52 | rec.setValue(index.column()+2, QVariant(str_tmp));
|
92 | // rec.setValue(index.column()+2, QVariant(str_tmp));
|
53 | rec.setGenerated(index.column()+2, true);
|
93 | // rec.setGenerated(index.column()+2, true);
|
54 | newindex = MyModel::index(index.row(), index.column()+2);
|
94 | // newindex = MyModel::index(index.row(), index.column()+2);
|
55 | QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
|
95 | // QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
|
56 | }
|
96 | }
|
- | 97 | ||
57 | */
|
98 | |
58 | /*
|
99 | /*
|
59 | 100 | ||
60 | if (index.column()==0){
|
101 | if (index.column()==0){
|
61 | rec.setValue(index.column()+3, QVariant(str_tmp));
|
102 | rec.setValue(index.column()+3, QVariant(str_tmp));
|
62 | rec.setGenerated(index.column()+3, true);
|
103 | rec.setGenerated(index.column()+3, true);
|
63 | newindex = MyModel::index(index.row(), index.column()+3);
|
104 | newindex = MyModel::index(index.row(), index.column()+3);
|
64 | QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
|
105 | QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
|
65 | }
|
106 | }
|
66 | */
|
107 | */
|
67 | 108 | ||
68 | lResult = updateRowInTable(index.row(), rec); |
109 | lResult = updateRowInTable(index.row(), rec); |
69 | /// QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
|
110 | /// QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
|
70 | 111 | ||
71 | 112 | ||
72 | }
|
113 | }
|
73 | return lResult; |
114 | return lResult; |
74 | }
|
115 | }
|
75 | 116 | ||
76 | 117 | ||
77 | 118 | ||
78 | QVariant ModelForDescriptionTable::data(const QModelIndex &index, int role) const { |
119 | QVariant ModelForDescriptionTable::data(const QModelIndex &index, int role) const { |
79 | 120 | ||
80 | QVariant value = QSqlTableModel::data(index, role); |
121 | QVariant value = QSqlTableModel::data(index, role); |
81 | switch (role) { |
122 | switch (role) { |
82 | case Qt::DisplayRole: return value; |
123 | case Qt::DisplayRole: return value; |
83 | case Qt::EditRole: return value; |
124 | case Qt::EditRole: return value; |
84 | case Qt::TextColorRole: |
125 | case Qt::TextColorRole: |
85 | if(index.column() == 1) |
126 | if(index.column() == 1) |
86 | return qVariantFromValue(QColor(Qt::darkGreen)); |
127 | return qVariantFromValue(QColor(Qt::darkGreen)); |
87 | else
|
128 | else
|
88 | return value; |
129 | return value; |
89 | case Qt::TextAlignmentRole: return value; |
130 | case Qt::TextAlignmentRole: return value; |
90 | case Qt::FontRole: |
131 | case Qt::FontRole: |
91 | if(index.column() == 1) { |
132 | if(index.column() == 1) { |
92 | QFont font = QFont("Helvetica", 16, QFont::Bold); |
133 | QFont font = QFont("Helvetica", 16, QFont::Bold); |
93 | return qVariantFromValue(font); |
134 | return qVariantFromValue(font); |
94 | }else |
135 | }else |
95 | return value; |
136 | return value; |
96 | 137 | ||
97 | case Qt::BackgroundColorRole: { |
138 | case Qt::BackgroundColorRole: { |
98 | int a = (index.row() % 2) ? 14 : 0; |
139 | int a = (index.row() % 2) ? 14 : 0; |
99 | if(index.column() == 2) |
140 | if(index.column() == 2) |
100 | return qVariantFromValue(QColor(220,240-a,230-a)); |
141 | return qVariantFromValue(QColor(220,240-a,230-a)); |
101 | else if(index.column() == 6) |
142 | else if(index.column() == 6) |
102 | return qVariantFromValue(QColor(200,220-a,255-a)); |
143 | return qVariantFromValue(QColor(200,220-a,255-a)); |
103 | else
|
144 | else
|
104 | return value; |
145 | return value; |
105 | }
|
146 | }
|
106 | 147 | ||
107 | 148 | ||
108 | 149 | ||
109 | case Qt::CheckStateRole: return value; |
150 | case Qt::CheckStateRole: return value; |
110 | 151 | ||
111 | case Qt::SizeHintRole:return value; |
152 | case Qt::SizeHintRole:return value; |
112 | 153 | ||
113 | }
|
154 | }
|
114 | return value; |
155 | return value; |
115 | }
|
156 | }
|
- | 157 | ||
- | 158 | ||
- | 159 | void ModelForDescriptionTable::setInctance(QString currInctatce){ // ïåðåäàåì èìÿ òåêóùåé òàáëèöû-õðàíèëèùà |
|
- | 160 | ModelForDescriptionTable::Inctance = currInctatce; |
|
- | 161 | }
|
|
116 | 162 |