Хранилища Subversion OpenInventory

Редакция

Редакция 75 | Редакция 77 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | 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
*/
45
    lResult = QSqlTableModel::setData(index, value, role);
46
    if (lResult) {
47
        QModelIndex newindex;
48
        QSqlRecord rec = record(index.row());
49
 
50
        rec.setValue(index.column(), value);
51
        rec.setGenerated(index.column(), true);
52
 
75 pingvin 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
 
77
            /*
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
 
92
          //  rec.setValue(index.column()+2, QVariant(str_tmp));
93
          //  rec.setGenerated(index.column()+2, true);
94
          //  newindex = MyModel::index(index.row(), index.column()+2);
95
          //  QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
61 pingvin 96
        }
75 pingvin 97
 
98
 
61 pingvin 99
/*
100
 
101
        if (index.column()==0){
102
            rec.setValue(index.column()+3, QVariant(str_tmp));
103
            rec.setGenerated(index.column()+3, true);
104
            newindex = MyModel::index(index.row(), index.column()+3);
105
            QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
106
        }
107
*/
108
 
109
        lResult = updateRowInTable(index.row(), rec);
76 pingvin 110
        emit field_name_changed();
61 pingvin 111
  ///      QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
112
 
113
 
114
    }
115
    return lResult;
116
}
117
 
118
 
119
 
120
QVariant ModelForDescriptionTable::data(const QModelIndex &index, int role) const {
121
 
122
     QVariant value = QSqlTableModel::data(index, role);
123
  switch (role) {
124
        case Qt::DisplayRole: return value;
125
        case Qt::EditRole: return value;
126
        case Qt::TextColorRole:
127
            if(index.column() == 1)
128
                return qVariantFromValue(QColor(Qt::darkGreen));
129
            else
130
              return value;
131
        case Qt::TextAlignmentRole: return value;
132
        case Qt::FontRole:
133
           if(index.column() == 1) {
134
               QFont font = QFont("Helvetica", 16, QFont::Bold);
135
              return qVariantFromValue(font);
136
           }else
137
               return value;
138
 
139
       case Qt::BackgroundColorRole: {
140
                int a = (index.row() % 2) ? 14 : 0;
141
                if(index.column() == 2)
142
                           return qVariantFromValue(QColor(220,240-a,230-a));
62 pingvin 143
                        else if(index.column() == 6)
61 pingvin 144
                            return qVariantFromValue(QColor(200,220-a,255-a));
145
                        else
146
                           return value;
147
                                    }
148
 
149
 
150
 
151
     case Qt::CheckStateRole: return value;
152
 
153
     case Qt::SizeHintRole:return value;
154
 
155
       }
156
  return value;
157
}
75 pingvin 158
 
159
 
160
void ModelForDescriptionTable::setInctance(QString currInctatce){ //    -
161
    ModelForDescriptionTable::Inctance = currInctatce;
162
}