Хранилища Subversion OpenInventory

Редакция

Редакция 76 | Редакция 78 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | 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
        QSqlRecord rec = record(index.row());
48
 
49
        rec.setValue(index.column(), value);
50
        rec.setGenerated(index.column(), true);
51
 
75 pingvin 52
 
53
        if (index.column()==1){ //    
54
            QSqlQuery qu;
55
            QString old_field_name; //   
56
            QString new_field_name; //   
57
            QString query_str;
58
            QString type_str; //   
59
            old_field_name = old_value.toString(); //   (  )  
60
            new_field_name = value.toString();
61
            type_str = data(ModelForDescriptionTable::index(index.row(), 3)).toString(); //     3 ,   
62
            query_str = tr("ALTER TABLE ");
63
            query_str.append(ModelForDescriptionTable::Inctance);
64
            query_str.append(tr(" CHANGE COLUMN `"));
65
            query_str.append(old_field_name);
66
            query_str.append(tr("` `"));
67
            query_str.append(new_field_name);
68
            query_str.append(tr("` "));
69
            query_str.append(type_str);
70
            query_str.append(tr(" CHARACTER SET cp1251 COLLATE cp1251_general_ci DEFAULT NULL"));
71
            qu.prepare(query_str);
72
            bool ok;
73
            ok = qu.exec();
74
            if (!ok) return false;
75
 
76
            /*
77
            if (!ok) {
78
 
79
                            QMessageBox::critical( //     .
80
                                                                                        this,                      //  .
81
                                                                                        QObject::tr("Database Error"),   // .
82
                                                                                        qu.lastError().text() ); //  .
83
 
84
                                                                        return ok; //    
85
 
86
                                 }
87
 
88
*/
89
 
90
 
91
          //  rec.setValue(index.column()+2, QVariant(str_tmp));
92
          //  rec.setGenerated(index.column()+2, true);
93
          //  newindex = MyModel::index(index.row(), index.column()+2);
94
          //  QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
61 pingvin 95
        }
75 pingvin 96
 
97
 
61 pingvin 98
/*
99
 
100
        if (index.column()==0){
101
            rec.setValue(index.column()+3, QVariant(str_tmp));
102
            rec.setGenerated(index.column()+3, true);
103
            newindex = MyModel::index(index.row(), index.column()+3);
104
            QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
105
        }
106
*/
107
 
77 pingvin 108
        if (index.column()==3){ //    
109
///ALTER TABLE `an_db`.`1_Inctance` MODIFY COLUMN `int` INTEGER;
110
            QSqlQuery qu;
111
            QString old_type, new_type, field_name, query_str;
112
            old_type = old_value.toString(); //   (  )  
113
            new_type = value.toString(); //   
114
            field_name = data(ModelForDescriptionTable::index(index.row(), 1)).toString(); //     1 ,   
115
            query_str = tr("ALTER TABLE ");
116
            query_str.append(ModelForDescriptionTable::Inctance);
117
            query_str.append(tr(" MODIFY COLUMN `"));
118
            query_str.append(field_name);
119
            query_str.append(tr("` "));
120
            query_str.append(new_type);
121
            qu.prepare(query_str);
122
            bool ok;
123
            ok = qu.exec();
124
            if (!ok) return false;
125
 
126
        }
127
 
128
        lResult = QSqlTableModel::setData(index, value, role);
129
        if (lResult) {
61 pingvin 130
        lResult = updateRowInTable(index.row(), rec);
77 pingvin 131
        emit field_changed();
61 pingvin 132
  ///      QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
133
 
134
 
135
    }
77 pingvin 136
 
137
 
138
 
139
 
61 pingvin 140
    return lResult;
141
}
142
 
143
 
144
 
145
QVariant ModelForDescriptionTable::data(const QModelIndex &index, int role) const {
146
 
147
     QVariant value = QSqlTableModel::data(index, role);
148
  switch (role) {
149
        case Qt::DisplayRole: return value;
150
        case Qt::EditRole: return value;
151
        case Qt::TextColorRole:
152
            if(index.column() == 1)
153
                return qVariantFromValue(QColor(Qt::darkGreen));
154
            else
155
              return value;
156
        case Qt::TextAlignmentRole: return value;
157
        case Qt::FontRole:
158
           if(index.column() == 1) {
159
               QFont font = QFont("Helvetica", 16, QFont::Bold);
160
              return qVariantFromValue(font);
161
           }else
162
               return value;
163
 
164
       case Qt::BackgroundColorRole: {
165
                int a = (index.row() % 2) ? 14 : 0;
166
                if(index.column() == 2)
167
                           return qVariantFromValue(QColor(220,240-a,230-a));
62 pingvin 168
                        else if(index.column() == 6)
61 pingvin 169
                            return qVariantFromValue(QColor(200,220-a,255-a));
170
                        else
171
                           return value;
172
                                    }
173
 
174
 
175
 
176
     case Qt::CheckStateRole: return value;
177
 
178
     case Qt::SizeHintRole:return value;
179
 
180
       }
181
  return value;
182
}
75 pingvin 183
 
184
 
185
void ModelForDescriptionTable::setInctance(QString currInctatce){ //    -
186
    ModelForDescriptionTable::Inctance = currInctatce;
187
}