Редакция 182 | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 182 | Редакция 184 | ||
---|---|---|---|
1 | 1 | #include "mymodel.h"
|
|
2 | 2 | ||
3 | 3 | MyModel::MyModel(QObject *parent) : |
|
4 | 4 | QSqlTableModel(parent) |
|
5 | 5 | {
|
|
6 | 6 | setEditStrategy(QSqlTableModel::OnManualSubmit); |
|
7 | 7 | }
|
|
8 | 8 | ||
9 | 9 | Qt::ItemFlags MyModel::flags( |
|
10 | 10 | const QModelIndex &index) const { |
|
11 | 11 | Qt::ItemFlags flags = QSqlTableModel::flags(index); |
|
12 | 12 | if ((index.column() != 3) && (index.column() != 0) /* && (index.column() != 5)*/) |
|
13 | 13 | flags |= Qt::ItemIsEditable; |
|
14 | 14 | //if (index.column() == 4)
|
|
15 | 15 | // flags |= Qt::ItemIsUserCheckable;
|
|
16 | 16 | else
|
|
17 | 17 | {
|
|
18 | 18 | if ((index.column() == 3) || (index.column() == 0) /* || (index.column() == 5)*/) flags &= ~Qt::ItemIsEditable; // çàïðåùàåì ðåäàêòèðîâàíèå 0, 3 è 5 ñòîëáöà |
|
19 | 19 | }
|
|
20 | 20 | return flags; |
|
21 | 21 | }
|
|
22 | 22 | ||
23 | 23 | ||
24 | 24 | bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role) { |
|
25 | 25 | //// if (index.column()==3) return false; // â òðåòüåé êîëîíêå íå äàåì ïîìåíÿòü çíà÷åíèå
|
|
26 | 26 | bool lResult = false; |
|
27 | 27 | ||
28 | 28 | /*
|
|
29 | 29 | QString str_tmp;
|
|
30 | 30 | if (index.column()==1) { // åñëè ìåíÿåì èìÿ êëàññà
|
|
31 | 31 | str_tmp = value.toString(); // òî ïîìåíÿåì è èìÿ òàáëèöû
|
|
32 | 32 | str_tmp.append(tr(" instance"));
|
|
33 | 33 | }
|
|
34 | 34 | */
|
|
35 | 35 | /*
|
|
36 | 36 | QString str_tmp;
|
|
37 | 37 | if (index.column()==0) { // åñëè ìåíÿåì èìÿ êëàññà
|
|
38 | 38 | str_tmp = value.toString(); // òî ïîìåíÿåì è èìÿ òàáëèöû
|
|
39 | 39 | str_tmp.append(tr("_instance"));
|
|
40 | 40 | }
|
|
41 | 41 | */
|
|
42 | 42 | lResult = QSqlTableModel::setData(index, value, role); |
|
43 | 43 | ||
44 | 44 | // if (index.column() == 5) return lResult;
|
|
45 | 45 | if (lResult) { |
|
46 | 46 | QModelIndex newindex;
|
|
47 | 47 | QSqlRecord rec = record(index.row()); |
|
48 | 48 | ||
49 | 49 | if (value.isNull()) { |
|
50 | 50 | rec.setNull(index.column()); |
|
51 | 51 | ||
52 | 52 | }
|
|
53 | 53 | else rec.setValue(index.column(), value); |
|
54 | 54 | ||
55 | 55 | rec.setGenerated(index.column(), true); |
|
56 | 56 | ||
57 | 57 | /*
|
|
58 | 58 | if (index.column()==1){
|
|
59 | 59 | rec.setValue(index.column()+2, QVariant(str_tmp));
|
|
60 | 60 | rec.setGenerated(index.column()+2, true);
|
|
61 | 61 | newindex = MyModel::index(index.row(), index.column()+2);
|
|
62 | 62 | QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
|
|
63 | 63 | }
|
|
64 | 64 | */
|
|
65 | 65 | /*
|
|
66 | 66 | ||
67 | 67 | if (index.column()==0){
|
|
68 | 68 | rec.setValue(index.column()+3, QVariant(str_tmp));
|
|
69 | 69 | rec.setGenerated(index.column()+3, true);
|
|
70 | 70 | newindex = MyModel::index(index.row(), index.column()+3);
|
|
71 | 71 | QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
|
|
72 | 72 | }
|
|
73 | 73 | */
|
|
74 | 74 | ||
75 | 75 | lResult = updateRowInTable(index.row(), rec); |
|
76 | 76 | /// QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
|
|
77 | 77 | // submitAll();
|
|
78 | 78 | // this->select();
|
|
79 | 79 | ||
80 | 80 | }
|
|
- | 81 | if (value.isNull()) { |
|
- | 82 | select(); |
|
81 | 83 | emit dataChanged(index, index); |
|
- | 84 | }
|
|
82 | 85 | return lResult; |
|
- | 86 | }
|
|
- | 87 | ||
- | 88 | ||
- | 89 | ||
- | 90 | QVariant MyModel::data(const QModelIndex &index, int role) const { |
|
- | 91 | ||
- | 92 | QVariant value = QSqlTableModel::data(index, role); |
|
- | 93 | switch (role) { |
|
- | 94 | case Qt::DisplayRole: return value; |
|
- | 95 | case Qt::EditRole: return value; |
|
- | 96 | case Qt::TextColorRole: |
|
- | 97 | if(index.column() == 1) |
|
- | 98 | return qVariantFromValue(QColor(Qt::blue)); |
|
- | 99 | else
|
|
- | 100 | return value; |
|
- | 101 | case Qt::TextAlignmentRole: return value; |
|
- | 102 | case Qt::FontRole: |
|
- | 103 | if(index.column() == 1) { |
|
- | 104 | QFont font = QFont("Helvetica", 16, QFont::Bold); |
|
- | 105 | return qVariantFromValue(font); |
|
- | 106 | }else |
|
- | 107 | return value; |
|
- | 108 | ||
- | 109 | case Qt::BackgroundColorRole: { |
|
- | 110 | int a = (index.row() % 2) ? 14 : 0; |
|
- | 111 | if(index.column() == 2) |
|
- | 112 | return qVariantFromValue(QColor(220,240-a,230-a)); |
|
- | 113 | else if(index.column() == 4) |
|
- | 114 | return qVariantFromValue(QColor(200,220-a,255-a)); |
|
- | 115 | else
|
|
- | 116 | return value; |
|
- | 117 | }
|
|
- | 118 | ||
- | 119 | ||
- | 120 | ||
- | 121 | case Qt::CheckStateRole: return value; |
|
- | 122 | ||
- | 123 | case Qt::SizeHintRole: |
|
- | 124 | {
|
|
- | 125 | ||
- | 126 | int height = value.toSize().height(); |
|
- | 127 | int widht = value.toSize().width(); |
|
- | 128 | if(index.column() == 5) { |
|
- | 129 | QVariant currentImage = QSqlTableModel::data(index, 0); |
|
- | 130 | QByteArray bytes = currentImage.toByteArray(); |
|
- | 131 | QPixmap pxmp;
|
|
- | 132 | ||
- | 133 | ||
- | 134 | if (currentImage.isValid()) { |
|
- | 135 | pxmp.loadFromData(bytes); |
|
- | 136 | if (height < pxmp.height()) height = pxmp.height() +10; |
|
- | 137 | if (widht < pxmp.width()) widht = pxmp.width() +10; |
|
- | 138 | return QVariant(QSize(widht, height)); |
|
- | 139 | ||
- | 140 | }
|
|
- | 141 | else return value; |
|
- | 142 | ||
- | 143 | }
|
|
- | 144 | ||
- | 145 | ||
- | 146 | // QSize size(200,100);
|
|
- | 147 | ||
- | 148 | return value; |
|
- | 149 | // return QVariant(size);
|
|
- | 150 | }
|
|
- | 151 | ||
- | 152 | case Qt::DecorationRole: { |
|
- | 153 | if(index.column() == 1) return iconProvider.icon(QFileIconProvider::Folder); |
|
- | 154 | else return QVariant(); |
|
- | 155 | /*{
|
|
- | 156 | QVariant currentImage = QSqlTableModel::data(index, 0);
|
|
- | 157 | QByteArray bytes = currentImage.toByteArray();
|
|
- | 158 | QPixmap pxmp;
|
|
- | 159 | if (currentImage.isValid()) {
|
|
- | 160 | pxmp.loadFromData(bytes);
|
|
- | 161 | QIcon icon;
|
|
- | 162 | icon.addPixmap(pxmp);
|
|
- | 163 | return icon;
|
|
- | 164 | ||
- | 165 | }
|
|
- | 166 | else {
|
|
- | 167 | //QItemDelegate::paint(painter, option, index);
|
|
- | 168 | return value;
|
|
- | 169 | }
|
|
- | 170 | ||
- | 171 | }
|
|
- | 172 | */
|
|
- | 173 | ||
- | 174 | }
|
|
- | 175 | ||
- | 176 | ||
- | 177 | ||
- | 178 | }
|
|
- | 179 | return value; |
|
- | 180 | }
|
|
- | 181 | ||
- | 182 |