Хранилища Subversion OpenInventory

Редакция

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

Редакция 172 Редакция 182
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
-
 
44
 //   if (index.column() == 5) return lResult;
43
    if (lResult) {
45
    if (lResult) {
44
        QModelIndex newindex;
46
        QModelIndex newindex;
45
        QSqlRecord rec = record(index.row());
47
        QSqlRecord rec = record(index.row());
46
48
-
 
49
        if (value.isNull()) {
-
 
50
        rec.setNull(index.column());
-
 
51
-
 
52
        }
47
        rec.setValue(index.column(), value);
53
        else rec.setValue(index.column(), value);
-
 
54
48
        rec.setGenerated(index.column(), true);
55
        rec.setGenerated(index.column(), true);
49
56
50
        /*
57
        /*
51
        if (index.column()==1){
58
        if (index.column()==1){
52
            rec.setValue(index.column()+2, QVariant(str_tmp));
59
            rec.setValue(index.column()+2, QVariant(str_tmp));
53
            rec.setGenerated(index.column()+2, true);
60
            rec.setGenerated(index.column()+2, true);
54
            newindex = MyModel::index(index.row(), index.column()+2);
61
            newindex = MyModel::index(index.row(), index.column()+2);
55
            QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
62
            QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
56
        }
63
        }
57
        */
64
        */
58
/*
65
/*
59

66

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

161

153
                  }
162
                  }
154
                  else {
163
                  else {
155
                      //QItemDelegate::paint(painter, option, index);
164
                      //QItemDelegate::paint(painter, option, index);
156
                  return value;
165
                  return value;
157
                  }
166
                  }
158

167

159
              }
168
              }
160
       */
169
       */
161
170
162
          }
171
          }
163
172
164
173
165
174
166
     }
175
     }
167
  return value;
176
  return value;
168
}
177
}
169
178
170
 
179