Хранилища Subversion OpenInventory

Редакция

Редакция 182 | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS

Редакция 182 Редакция 184
1
#include "mymodel.h"
1
#include "mymodel.h"
2
2
3
MyModel::MyModel(QObject *parent) :
3
MyModel::MyModel(QObject *parent) :
4
      QSqlTableModel(parent)
4
      QSqlTableModel(parent)
5
{
5
{
6
    setEditStrategy(QSqlTableModel::OnManualSubmit);
6
    setEditStrategy(QSqlTableModel::OnManualSubmit);
7
}
7
}
8
8
9
Qt::ItemFlags MyModel::flags(
9
Qt::ItemFlags MyModel::flags(
10
         const QModelIndex &index) const {
10
         const QModelIndex &index) const {
11
Qt::ItemFlags flags = QSqlTableModel::flags(index);
11
Qt::ItemFlags flags = QSqlTableModel::flags(index);
12
if ((index.column() != 3) && (index.column() != 0) /* && (index.column() != 5)*/)
12
if ((index.column() != 3) && (index.column() != 0) /* && (index.column() != 5)*/)
13
     flags |= Qt::ItemIsEditable;
13
     flags |= Qt::ItemIsEditable;
14
//if (index.column() == 4)
14
//if (index.column() == 4)
15
//     flags |= Qt::ItemIsUserCheckable;
15
//     flags |= Qt::ItemIsUserCheckable;
16
else
16
else
17
{
17
{
18
    if ((index.column() == 3) || (index.column() == 0) /* || (index.column() == 5)*/) flags &= ~Qt::ItemIsEditable; // çàïðåùàåì ðåäàêòèðîâàíèå 0, 3 è 5 ñòîëáöà
18
    if ((index.column() == 3) || (index.column() == 0) /* || (index.column() == 5)*/) flags &= ~Qt::ItemIsEditable; // çàïðåùàåì ðåäàêòèðîâàíèå 0, 3 è 5 ñòîëáöà
19
}
19
}
20
    return flags;
20
    return flags;
21
}
21
}
22
22
23
23
24
bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role) {
24
bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role) {
25
  ////  if (index.column()==3) return false; // â òðåòüåé êîëîíêå íå äàåì ïîìåíÿòü çíà÷åíèå
25
  ////  if (index.column()==3) return false; // â òðåòüåé êîëîíêå íå äàåì ïîìåíÿòü çíà÷åíèå
26
    bool lResult = false;
26
    bool lResult = false;
27
27
28
    /*
28
    /*
29
    QString str_tmp;
29
    QString str_tmp;
30
    if (index.column()==1) {                // åñëè ìåíÿåì èìÿ êëàññà
30
    if (index.column()==1) {                // åñëè ìåíÿåì èìÿ êëàññà
31
        str_tmp = value.toString();         // òî ïîìåíÿåì è èìÿ òàáëèöû
31
        str_tmp = value.toString();         // òî ïîìåíÿåì è èìÿ òàáëèöû
32
        str_tmp.append(tr(" instance"));
32
        str_tmp.append(tr(" instance"));
33
      }
33
      }
34
      */
34
      */
35
/*
35
/*
36
    QString str_tmp;
36
    QString str_tmp;
37
    if (index.column()==0) {                // åñëè ìåíÿåì èìÿ êëàññà
37
    if (index.column()==0) {                // åñëè ìåíÿåì èìÿ êëàññà
38
        str_tmp = value.toString();         // òî ïîìåíÿåì è èìÿ òàáëèöû
38
        str_tmp = value.toString();         // òî ïîìåíÿåì è èìÿ òàáëèöû
39
        str_tmp.append(tr("_instance"));
39
        str_tmp.append(tr("_instance"));
40
      }
40
      }
41
*/
41
*/
42
    lResult = QSqlTableModel::setData(index, value, role);
42
    lResult = QSqlTableModel::setData(index, value, role);
43
43
44
 //   if (index.column() == 5) return lResult;
44
 //   if (index.column() == 5) return lResult;
45
    if (lResult) {
45
    if (lResult) {
46
        QModelIndex newindex;
46
        QModelIndex newindex;
47
        QSqlRecord rec = record(index.row());
47
        QSqlRecord rec = record(index.row());
48
48
49
        if (value.isNull()) {
49
        if (value.isNull()) {
50
        rec.setNull(index.column());
50
        rec.setNull(index.column());
51
51
52
        }
52
        }
53
        else rec.setValue(index.column(), value);
53
        else rec.setValue(index.column(), value);
54
54
55
        rec.setGenerated(index.column(), true);
55
        rec.setGenerated(index.column(), true);
56
56
57
        /*
57
        /*
58
        if (index.column()==1){
58
        if (index.column()==1){
59
            rec.setValue(index.column()+2, QVariant(str_tmp));
59
            rec.setValue(index.column()+2, QVariant(str_tmp));
60
            rec.setGenerated(index.column()+2, true);
60
            rec.setGenerated(index.column()+2, true);
61
            newindex = MyModel::index(index.row(), index.column()+2);
61
            newindex = MyModel::index(index.row(), index.column()+2);
62
            QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
62
            QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
63
        }
63
        }
64
        */
64
        */
65
/*
65
/*
66

66

67
        if (index.column()==0){
67
        if (index.column()==0){
68
            rec.setValue(index.column()+3, QVariant(str_tmp));
68
            rec.setValue(index.column()+3, QVariant(str_tmp));
69
            rec.setGenerated(index.column()+3, true);
69
            rec.setGenerated(index.column()+3, true);
70
            newindex = MyModel::index(index.row(), index.column()+3);
70
            newindex = MyModel::index(index.row(), index.column()+3);
71
            QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
71
            QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
72
        }
72
        }
73
*/
73
*/
74
74
75
        lResult = updateRowInTable(index.row(), rec);
75
        lResult = updateRowInTable(index.row(), rec);
76
  ///      QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
76
  ///      QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
77
  //          submitAll();
77
  //          submitAll();
78
   //         this->select();
78
   //         this->select();
79
79
80
    }
80
    }
-
 
81
    if (value.isNull()) {
-
 
82
        select();
81
    emit dataChanged(index, index);;
83
        emit dataChanged(index, index);
-
 
84
    }
82
    return lResult;
85
    return lResult;
83
}
86
}
84
87
85
88
86
89
87
QVariant MyModel::data(const QModelIndex &index, int role) const {
90
QVariant MyModel::data(const QModelIndex &index, int role) const {
88
91
89
     QVariant value = QSqlTableModel::data(index, role);
92
     QVariant value = QSqlTableModel::data(index, role);
90
  switch (role) {
93
  switch (role) {
91
        case Qt::DisplayRole: return value;
94
        case Qt::DisplayRole: return value;
92
        case Qt::EditRole: return value;
95
        case Qt::EditRole: return value;
93
        case Qt::TextColorRole:
96
        case Qt::TextColorRole:
94
            if(index.column() == 1)
97
            if(index.column() == 1)
95
                return qVariantFromValue(QColor(Qt::blue));
98
                return qVariantFromValue(QColor(Qt::blue));
96
            else
99
            else
97
              return value;
100
              return value;
98
        case Qt::TextAlignmentRole: return value;
101
        case Qt::TextAlignmentRole: return value;
99
        case Qt::FontRole:
102
        case Qt::FontRole:
100
           if(index.column() == 1) {
103
           if(index.column() == 1) {
101
               QFont font = QFont("Helvetica", 16, QFont::Bold);
104
               QFont font = QFont("Helvetica", 16, QFont::Bold);
102
              return qVariantFromValue(font);
105
              return qVariantFromValue(font);
103
           }else
106
           }else
104
               return value;
107
               return value;
105
108
106
       case Qt::BackgroundColorRole: {
109
       case Qt::BackgroundColorRole: {
107
                int a = (index.row() % 2) ? 14 : 0;
110
                int a = (index.row() % 2) ? 14 : 0;
108
                if(index.column() == 2)
111
                if(index.column() == 2)
109
                           return qVariantFromValue(QColor(220,240-a,230-a));
112
                           return qVariantFromValue(QColor(220,240-a,230-a));
110
                        else if(index.column() == 4)
113
                        else if(index.column() == 4)
111
                            return qVariantFromValue(QColor(200,220-a,255-a));
114
                            return qVariantFromValue(QColor(200,220-a,255-a));
112
                        else
115
                        else
113
                           return value;
116
                           return value;
114
                                    }
117
                                    }
115
118
116
119
117
120
118
     case Qt::CheckStateRole: return value;
121
     case Qt::CheckStateRole: return value;
119
122
120
     case Qt::SizeHintRole:
123
     case Qt::SizeHintRole:
121
              {
124
              {
122
125
123
             int height = value.toSize().height();
126
             int height = value.toSize().height();
124
             int widht = value.toSize().width();
127
             int widht = value.toSize().width();
125
             if(index.column() == 5) {
128
             if(index.column() == 5) {
126
                               QVariant currentImage = QSqlTableModel::data(index, 0);
129
                               QVariant currentImage = QSqlTableModel::data(index, 0);
127
                               QByteArray bytes = currentImage.toByteArray();
130
                               QByteArray bytes = currentImage.toByteArray();
128
                               QPixmap pxmp;
131
                               QPixmap pxmp;
129
132
130
133
131
                               if (currentImage.isValid()) {
134
                               if (currentImage.isValid()) {
132
                                    pxmp.loadFromData(bytes);
135
                                    pxmp.loadFromData(bytes);
133
                                    if (height < pxmp.height()) height =  pxmp.height() +10;
136
                                    if (height < pxmp.height()) height =  pxmp.height() +10;
134
                                    if (widht < pxmp.width()) widht = pxmp.width() +10;
137
                                    if (widht < pxmp.width()) widht = pxmp.width() +10;
135
                                    return QVariant(QSize(widht, height));
138
                                    return QVariant(QSize(widht, height));
136
139
137
                                }
140
                                }
138
                               else return value;
141
                               else return value;
139
142
140
                           }
143
                           }
141
144
142
145
143
        //     QSize size(200,100);
146
        //     QSize size(200,100);
144
147
145
                 return value;
148
                 return value;
146
          //        return QVariant(size);
149
          //        return QVariant(size);
147
              }
150
              }
148
151
149
      case Qt::DecorationRole: {
152
      case Qt::DecorationRole: {
150
              if(index.column() == 1) return iconProvider.icon(QFileIconProvider::Folder);
153
              if(index.column() == 1) return iconProvider.icon(QFileIconProvider::Folder);
151
              else return QVariant();
154
              else return QVariant();
152
              /*{
155
              /*{
153
                  QVariant currentImage = QSqlTableModel::data(index, 0);
156
                  QVariant currentImage = QSqlTableModel::data(index, 0);
154
                  QByteArray bytes = currentImage.toByteArray();
157
                  QByteArray bytes = currentImage.toByteArray();
155
                  QPixmap pxmp;
158
                  QPixmap pxmp;
156
                  if (currentImage.isValid()) {
159
                  if (currentImage.isValid()) {
157
                       pxmp.loadFromData(bytes);
160
                       pxmp.loadFromData(bytes);
158
                       QIcon icon;
161
                       QIcon icon;
159
                       icon.addPixmap(pxmp);
162
                       icon.addPixmap(pxmp);
160
                       return icon;
163
                       return icon;
161

164

162
                  }
165
                  }
163
                  else {
166
                  else {
164
                      //QItemDelegate::paint(painter, option, index);
167
                      //QItemDelegate::paint(painter, option, index);
165
                  return value;
168
                  return value;
166
                  }
169
                  }
167

170

168
              }
171
              }
169
       */
172
       */
170
173
171
          }
174
          }
172
175
173
176
174
177
175
     }
178
     }
176
  return value;
179
  return value;
177
}
180
}
178
181
179
 
182