Хранилища Subversion OpenInventory

Редакция

Редакция 79 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | 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;
80 pingvin 111
 /////           ok = setData(ModelForDescriptionTable::index(index.row(), 4), QVariant(tr("NULL")), Qt::EditRole); //    
112
            ok = QSqlTableModel::setData(ModelForDescriptionTable::index(index.row(), 4), QVariant(tr("NULL")), Qt::EditRole); //    
79 pingvin 113
            if (ok) {
114
 
115
 
77 pingvin 116
            QSqlQuery qu;
117
            QString old_type, new_type, field_name, query_str;
118
            old_type = old_value.toString(); //   (  )  
119
            new_type = value.toString(); //   
120
            field_name = data(ModelForDescriptionTable::index(index.row(), 1)).toString(); //     1 ,   
121
            query_str = tr("ALTER TABLE ");
122
            query_str.append(ModelForDescriptionTable::Inctance);
123
            query_str.append(tr(" MODIFY COLUMN `"));
124
            query_str.append(field_name);
125
            query_str.append(tr("` "));
126
            query_str.append(new_type);
127
            qu.prepare(query_str);
79 pingvin 128
 
77 pingvin 129
            ok = qu.exec();
130
            if (!ok) return false;
78 pingvin 131
        }
79 pingvin 132
            else return false;
133
        }
78 pingvin 134
        if (index.column()==4){ //     
135
///ALTER TABLE `an_db`.`2_Inctance` MODIFY COLUMN `` CHAR(10)  CHARACTER SET cp1251 COLLATE cp1251_general_ci DEFAULT '';
136
            QSqlQuery qu;
137
            QString field_type, new_default_value, field_name, query_str;
138
 
139
            field_type = data(ModelForDescriptionTable::index(index.row(), 3)).toString(); //     3 ,   
140
            field_name = data(ModelForDescriptionTable::index(index.row(), 1)).toString(); //     1 ,   
141
            new_default_value = value.toString();
142
            query_str = tr("ALTER TABLE ");
143
            query_str.append(ModelForDescriptionTable::Inctance);
144
            query_str.append(tr(" MODIFY COLUMN `"));
145
            query_str.append(field_name);
146
            query_str.append(tr("` "));
147
            query_str.append(field_type);
148
 
149
            if (new_default_value != tr("NULL")) {
150
            query_str.append(tr(" DEFAULT '"));
151
 
152
 
153
            query_str.append(new_default_value);
154
            query_str.append(tr("'")); }
155
            else {
156
                query_str.append(tr(" DEFAULT ")); //     NULL    
157
                query_str.append(new_default_value);
158
            }
159
            qu.prepare(query_str);
160
            bool ok;
161
            ok = qu.exec();
162
            if (!ok) return false;
163
 
77 pingvin 164
        }
165
 
78 pingvin 166
 
167
        QSqlRecord rec = record(index.row());
168
        rec.setValue(index.column(), value);
169
        rec.setGenerated(index.column(), true);
170
 
171
 
77 pingvin 172
        lResult = QSqlTableModel::setData(index, value, role);
173
        if (lResult) {
61 pingvin 174
        lResult = updateRowInTable(index.row(), rec);
77 pingvin 175
        emit field_changed();
61 pingvin 176
  ///      QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
177
 
178
 
179
    }
77 pingvin 180
 
181
 
182
 
183
 
61 pingvin 184
    return lResult;
185
}
186
 
187
 
188
 
189
QVariant ModelForDescriptionTable::data(const QModelIndex &index, int role) const {
190
 
191
     QVariant value = QSqlTableModel::data(index, role);
192
  switch (role) {
193
        case Qt::DisplayRole: return value;
194
        case Qt::EditRole: return value;
195
        case Qt::TextColorRole:
196
            if(index.column() == 1)
197
                return qVariantFromValue(QColor(Qt::darkGreen));
198
            else
199
              return value;
200
        case Qt::TextAlignmentRole: return value;
201
        case Qt::FontRole:
202
           if(index.column() == 1) {
203
               QFont font = QFont("Helvetica", 16, QFont::Bold);
204
              return qVariantFromValue(font);
205
           }else
206
               return value;
207
 
208
       case Qt::BackgroundColorRole: {
209
                int a = (index.row() % 2) ? 14 : 0;
210
                if(index.column() == 2)
211
                           return qVariantFromValue(QColor(220,240-a,230-a));
62 pingvin 212
                        else if(index.column() == 6)
61 pingvin 213
                            return qVariantFromValue(QColor(200,220-a,255-a));
214
                        else
215
                           return value;
216
                                    }
217
 
218
 
219
 
220
     case Qt::CheckStateRole: return value;
221
 
222
     case Qt::SizeHintRole:return value;
223
 
224
       }
225
  return value;
226
}
75 pingvin 227
 
228
 
229
void ModelForDescriptionTable::setInctance(QString currInctatce){ //    -
230
    ModelForDescriptionTable::Inctance = currInctatce;
231
}