Qt::ItemFlags TableModel::flags(const QModelIndex &index) const { Qt::ItemFlags flags = QAbstractTableModel::flags(index); if(index.isValid()) { bool can_edit = false; ColumnDefinition cd = m_columns.value(index.column()); if(!cd.isNull()) { can_edit = !cd.isReadOnly(); if(!cd.isVirtual()) { qfu::Table::Field field = tableField(index.column()); if(!field.isNull()) { can_edit = can_edit && field.canUpdate(); int type = columnType(index.column()); // BLOB fields cannot be edited in grid. //can_edit = can_edit && (type != QVariant::ByteArray); if(type == QVariant::Bool) { flags |= Qt::ItemIsUserCheckable;// << Qt::ItemIsEnabled; } } else { can_edit = false; } } } if(can_edit) flags |= Qt::ItemIsEditable; else flags &= ~Qt::ItemIsEditable; //qfInfo() << cd.fieldName() << flags << "checkable:" << (flags & Qt::ItemIsUserCheckable); } return flags; }
void SqlTableModel::removeAllColumnsFunction(QQmlListProperty<TableModelColumn> *list_property) { qfLogFuncFrame(); SqlTableModel *that = static_cast<SqlTableModel*>(list_property->object); while (that->columnCount()) { ColumnDefinition cd = that->removeColumn(0); QF_ASSERT(cd.isNull(), "Error removing column", break); } qDeleteAll(that->m_columns); that->m_columns.clear(); }
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const { static QIcon ico_dirty(QPixmap(":/qf/core/images/pencil.png")); //static QIcon ico_filter(QPixmap(":/libqfgui/images/filter.png")); QVariant ret; if(orientation == Qt::Horizontal) { if(role == Qt::DisplayRole || role == Qt::EditRole) { ColumnDefinition cd = m_columns.value(section); QF_ASSERT(!cd.isNull(), QString("Invalid horizontal section number: %1").arg(section), return ret); ret = cd.caption(); } else if(role == Qt::DecorationRole) {
QVariant TableModel::data(const QModelIndex &index, int role) const { QVariant ret; if(!index.isValid()) return ret; if(role == RawValueRole) { ret = value(index.row(), index.column()); } else if(role == ValueIsDirtyRole) { ret = isDirty(index.row(), index.column()); } else if(role == SortRole) { int type = columnType(index.column()); if(type == QVariant::Bool) ret = value(index.row(), index.column()).toBool(); else ret = data(index, Qt::DisplayRole); } else if(role == Qt::DisplayRole) { ColumnDefinition cd = m_columns.value(index.column()); if(cd.isNull()) { return QString("!%1").arg(index.column()); } if(data(index, ValueIsNullRole).toBool()) { if(isNullReportedAsString()) return QStringLiteral("null"); return QVariant(); } ret = data(index, Qt::EditRole); int type = columnType(index.column()); if(type == QVariant::Invalid) type = ret.type(); /// pokud jsou sloupce virtualni (sloupce se pocitaji, nemusi byt pro ne definovan typ) if(type == QVariant::ByteArray) { const static QString blob_string = "{blob %1%2}"; int size = ret.toByteArray().size(); if(size < 1024) ret = blob_string.arg(size).arg(" B"); else ret = blob_string.arg(size/1024).arg("kB"); } else if(type == QVariant::Bool) { /// display check ret = QString(); } QString format = cd.format(); if(format.isEmpty()) { if(type == QVariant::Date) { format = m_defaultDateFormat; } else if(type == QVariant::Time) { format = m_defaultTimeFormat; //qfInfo() << "format" << format; } else if(type == QVariant::DateTime) { format = m_defaultDateTimeFormat; //qfInfo() << "format" << format; } } if(!format.isEmpty()) { if(type == QVariant::Time) { QTime t = ret.toTime(); ret = t.toString(format); } else if(type == QVariant::Date) { QDate d = ret.toDate(); ret = d.toString(format); } else if(type == QVariant::DateTime) { QDateTime dt = ret.toDateTime(); ret = dt.toString(format); } } } else if(role == Qt::EditRole) { ret = data(index, RawValueRole); ret = rawValueToEdit(index.column(), ret); } else if (role == ValueIsNullRole) { ret = data(index, RawValueRole); return ret.isNull() && ret.isValid(); } else if (role == Qt::TextAlignmentRole) { const ColumnDefinition cd = m_columns.value(index.column()); Qt::Alignment al = cd.alignment(); if(!!al) ret = (int)al; else { if(!data(index, ValueIsNullRole).toBool()) { ret = data(index, RawValueRole); if(ret.type() > QVariant::Bool && ret.type() <= QVariant::Double) ret = Qt::AlignRight; else ret = Qt::AlignLeft; } } } else if(role == Qt::TextColorRole) { int type = columnType(index.column()); if(type == QVariant::ByteArray) return QColor(Qt::blue); if(data(index, ValueIsNullRole).toBool()) { return QColor(Qt::blue); } ret = QVariant(); } else if (role == Qt::BackgroundColorRole) { /// delegate does it } else if (role == Qt::CheckStateRole) { int type = columnType(index.column()); if(type == QVariant::Bool) { //qfInfo() << "BOOL"; return (data(index, Qt::EditRole).toBool()? Qt::Checked: Qt::Unchecked); } return QVariant(); } else if (role == Qt::ToolTipRole) { QString s = data(index, Qt::DisplayRole).toString(); //s = "<pre>" + s + "</pre>"; /// kvuli chybe v Qt 4.6.3, kdy aplikace pada pokud je v textu za sebou 0x09 0x0A s.replace("\t\n", "\n"); ret = s; //ret = data(index, Qt::DisplayRole); //qfInfo() << ret.toString(); } return ret; }