Хранилища Subversion OpenInventory

Редакция

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