Хранилища Subversion OpenInventory

Редакция

Редакция 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
 
31
        if (index.column()==1){ //    
32
            QSqlQuery qu;
33
            QString old_field_name; //   
34
            QString new_field_name; //   
35
            QString query_str;
36
            QString type_str; //   
37
            old_field_name = old_value.toString(); //   (  )  
38
            new_field_name = value.toString();
39
            type_str = data(ModelForDescriptionTable::index(index.row(), 3)).toString(); //     3 ,   
40
            query_str = tr("ALTER TABLE ");
41
            query_str.append(ModelForDescriptionTable::Inctance);
42
            query_str.append(tr(" CHANGE COLUMN `"));
43
            query_str.append(old_field_name);
44
            query_str.append(tr("` `"));
45
            query_str.append(new_field_name);
46
            query_str.append(tr("` "));
47
            query_str.append(type_str);
48
            query_str.append(tr(" CHARACTER SET cp1251 COLLATE cp1251_general_ci DEFAULT NULL"));
49
            qu.prepare(query_str);
50
            bool ok;
51
            ok = qu.exec();
52
            if (!ok) return false;
79 pingvin 53
        }
75 pingvin 54
 
55
 
81 pingvin 56
       if (index.column()==3){ //    
77 pingvin 57
///ALTER TABLE `an_db`.`1_Inctance` MODIFY COLUMN `int` INTEGER;
79 pingvin 58
 
59
            bool ok;
80 pingvin 60
            ok = QSqlTableModel::setData(ModelForDescriptionTable::index(index.row(), 4), QVariant(tr("NULL")), Qt::EditRole); //    
79 pingvin 61
            if (ok) {
62
 
63
 
77 pingvin 64
            QSqlQuery qu;
65
            QString old_type, new_type, field_name, query_str;
66
            old_type = old_value.toString(); //   (  )  
67
            new_type = value.toString(); //   
68
            field_name = data(ModelForDescriptionTable::index(index.row(), 1)).toString(); //     1 ,   
69
            query_str = tr("ALTER TABLE ");
70
            query_str.append(ModelForDescriptionTable::Inctance);
71
            query_str.append(tr(" MODIFY COLUMN `"));
72
            query_str.append(field_name);
73
            query_str.append(tr("` "));
74
            query_str.append(new_type);
75
            qu.prepare(query_str);
79 pingvin 76
 
77 pingvin 77
            ok = qu.exec();
78
            if (!ok) return false;
78 pingvin 79
        }
79 pingvin 80
            else return false;
81
        }
81 pingvin 82
 
83
       if (index.column()==4){ //     
78 pingvin 84
///ALTER TABLE `an_db`.`2_Inctance` MODIFY COLUMN `` CHAR(10)  CHARACTER SET cp1251 COLLATE cp1251_general_ci DEFAULT '';
85
            QSqlQuery qu;
86
            QString field_type, new_default_value, field_name, query_str;
87
 
88
            field_type = data(ModelForDescriptionTable::index(index.row(), 3)).toString(); //     3 ,   
89
            field_name = data(ModelForDescriptionTable::index(index.row(), 1)).toString(); //     1 ,   
90
            new_default_value = value.toString();
91
            query_str = tr("ALTER TABLE ");
92
            query_str.append(ModelForDescriptionTable::Inctance);
93
            query_str.append(tr(" MODIFY COLUMN `"));
94
            query_str.append(field_name);
95
            query_str.append(tr("` "));
96
            query_str.append(field_type);
97
 
98
            if (new_default_value != tr("NULL")) {
99
            query_str.append(tr(" DEFAULT '"));
100
 
101
 
102
            query_str.append(new_default_value);
103
            query_str.append(tr("'")); }
104
            else {
105
                query_str.append(tr(" DEFAULT ")); //     NULL    
106
                query_str.append(new_default_value);
107
            }
108
            qu.prepare(query_str);
109
            bool ok;
110
            ok = qu.exec();
111
            if (!ok) return false;
112
 
77 pingvin 113
        }
114
 
78 pingvin 115
 
116
        QSqlRecord rec = record(index.row());
117
        rec.setValue(index.column(), value);
118
        rec.setGenerated(index.column(), true);
119
 
120
 
77 pingvin 121
        lResult = QSqlTableModel::setData(index, value, role);
122
        if (lResult) {
61 pingvin 123
        lResult = updateRowInTable(index.row(), rec);
77 pingvin 124
        emit field_changed();
61 pingvin 125
  ///      QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
81 pingvin 126
                    }
61 pingvin 127
 
128
 
77 pingvin 129
 
130
 
81 pingvin 131
    return lResult;
132
}
77 pingvin 133
 
134
 
81 pingvin 135
 
136
 
137
bool ModelForDescriptionTable::onlySetData(const QModelIndex &index, const QVariant &value, int role){
138
     bool lResult = false;
139
 
140
     QSqlRecord rec = record(index.row());
141
     rec.setValue(index.column(), value);
142
     rec.setGenerated(index.column(), true);
143
 
144
 
145
     lResult = QSqlTableModel::setData(index, value, role);
146
     if (lResult) {
147
     lResult = updateRowInTable(index.row(), rec);
148
 //    emit field_changed();
149
///      QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
150
                 }
151
 
152
 
153
 
154
 
155
 return lResult;
156
 
61 pingvin 157
}
158
 
159
 
160
 
81 pingvin 161
 
162
 
61 pingvin 163
QVariant ModelForDescriptionTable::data(const QModelIndex &index, int role) const {
164
 
165
     QVariant value = QSqlTableModel::data(index, role);
166
  switch (role) {
167
        case Qt::DisplayRole: return value;
168
        case Qt::EditRole: return value;
169
        case Qt::TextColorRole:
170
            if(index.column() == 1)
171
                return qVariantFromValue(QColor(Qt::darkGreen));
172
            else
173
              return value;
174
        case Qt::TextAlignmentRole: return value;
175
        case Qt::FontRole:
176
           if(index.column() == 1) {
177
               QFont font = QFont("Helvetica", 16, QFont::Bold);
178
              return qVariantFromValue(font);
179
           }else
180
               return value;
181
 
182
       case Qt::BackgroundColorRole: {
183
                int a = (index.row() % 2) ? 14 : 0;
184
                if(index.column() == 2)
185
                           return qVariantFromValue(QColor(220,240-a,230-a));
62 pingvin 186
                        else if(index.column() == 6)
61 pingvin 187
                            return qVariantFromValue(QColor(200,220-a,255-a));
188
                        else
189
                           return value;
190
                                    }
191
 
192
 
193
 
194
     case Qt::CheckStateRole: return value;
195
 
196
     case Qt::SizeHintRole:return value;
197
 
198
       }
199
  return value;
200
}
75 pingvin 201
 
202
 
203
void ModelForDescriptionTable::setInctance(QString currInctatce){ //    -
204
    ModelForDescriptionTable::Inctance = currInctatce;
205
}