oopapidocs
2.0
|
00001 #include <QDebug> 00002 #include <QStringList> 00003 #include "dataobjecttablemodel.h" 00004 00005 //start id=extractHeaders 00006 DataObjectTableModel:: 00007 DataObjectTableModel(DataObject* headerModel, QObject* parent) : 00008 QAbstractTableModel(parent) { 00009 if (headerModel != NULL) { 00010 extractHeaders(headerModel); 00011 } 00012 } 00013 00014 int DataObjectTableModel:: 00015 fieldIndex(const QString& fieldName) const { 00016 return m_Headers.indexOf(fieldName); 00017 } 00018 00019 void DataObjectTableModel:: 00020 extractHeaders(DataObject* hmodel) { 00021 m_Original = hmodel; 00022 QStringList props = hmodel->propertyNames(); 00023 m_Headers.clear(); 00024 foreach (QString prop, props) { 00025 if (prop == "objectName") continue; 00026 QMetaProperty mprop = m_Original->metaProperty(prop); 00027 m_isEditable << mprop.isWritable(); 00028 m_Headers << prop; 00029 } 00030 } 00031 //end 00032 DataObjectTableModel::~DataObjectTableModel() { 00033 00034 } 00035 00036 int DataObjectTableModel:: 00037 findRow(const QString& name) { 00038 // TODO: Make this a map lookup for better scalability? 00039 int rc = rowCount(); 00040 for (int i=0; i<rc; ++i) { 00041 if (m_Data[i]->objectName() == name) { 00042 return i; 00043 } 00044 } 00045 return -1; 00046 } 00047 00048 void DataObjectTableModel:: 00049 rowChanged(const QString& name) { 00050 int row = findRow(name); 00051 if (row == -1) return; 00052 QModelIndex ul = index(row, 0, QModelIndex()); 00053 QModelIndex br = index(row, m_Headers.size(), QModelIndex()); 00054 emit dataChanged(ul, br); 00055 } 00056 00057 //start id=data 00058 QVariant DataObjectTableModel:: 00059 data(const QModelIndex& index, int role) const { 00060 if (!index.isValid()) 00061 return QVariant(); 00062 int row(index.row()), col(index.column()); 00063 if (row >= rowCount()) return QVariant(); 00064 QSharedPointer<DataObject> lineItem(m_Data.at(row)); 00065 if (lineItem.isNull()) { 00066 qDebug() << "lineitem=0:" << index; 00067 return QVariant(); 00068 } 00069 if (role == Qt::UserRole || role == Qt::ToolTipRole) 00070 return lineItem->objectName(); 00071 else if (role == DisplayRole || role == EditRole) { 00072 return lineItem->property(m_Headers.at(col)); 00073 } else 00074 return QVariant(); 00075 } 00076 00077 00078 bool DataObjectTableModel:: 00079 setData(const QModelIndex& index, const QVariant& value, int role) { 00080 if (index.isValid() && role == EditRole) { 00081 int row(index.row()), col(index.column()); 00082 QSharedPointer<DataObject> lineItem(m_Data.at(row)); 00083 lineItem->setProperty(m_Headers.at(col), value); 00084 emit dataChanged(index, index); 00085 return true; 00086 } 00087 return false; 00088 } 00089 //end 00090 00091 //start id=header 00092 QVariant DataObjectTableModel:: 00093 headerData(int section, Qt::Orientation orientation, int role) const { 00094 if (role != DisplayRole) 00095 return QVariant(); 00096 if (orientation == Qt::Vertical) 00097 return QVariant(section); 00098 if (m_Headers.size() ==0) 00099 return QVariant(); 00100 if (section >= m_Headers.size()) return QVariant(); 00101 return m_Headers.at(section); 00102 } 00103 00104 00105 int DataObjectTableModel:: 00106 rowCount(const QModelIndex&) const { 00107 return m_Data.count(); 00108 } 00109 00110 int DataObjectTableModel:: 00111 columnCount(const QModelIndex& parent) const { 00112 Q_UNUSED(parent); 00113 return m_Headers.size(); 00114 } 00115 //end 00116 00117 00118 //start id=records 00119 bool DataObjectTableModel:: 00120 insertRecord(QSharedPointer<DataObject> newRow, int position, 00121 const QModelIndex& parent) { 00122 if (position==-1) { 00123 position=rowCount()-1; 00124 } 00125 if (position < 0) position = 0; 00126 beginInsertRows(parent, position, position); 00127 m_Data.insert(position, newRow); 00128 connect(newRow.data(), SIGNAL(dataObjectChanged(QString)), 00129 this, SLOT(rowChanged(QString))); 00130 endInsertRows(); 00131 return true; 00132 } 00133 //end 00134 /* 00135 const QSharedPointer<DataObject> DataObjectTableModel:: 00136 operator[](int rowNum) const { 00137 return m_Data.at(rowNum); 00138 } 00139 00140 QSharedPointer<DataObject> DataObjectTableModel:: 00141 operator[](int rowNum) { 00142 return m_Data.at(rowNum); 00143 } 00144 */ 00145 //start id=abstract 00146 ItemFlags DataObjectTableModel:: 00147 flags(const QModelIndex& index) const { 00148 if (!index.isValid()) 00149 return ItemIsEnabled; 00150 if (m_isEditable[index.column()]) 00151 return QAbstractItemModel::flags(index) | ItemIsEditable; 00152 else 00153 return QAbstractItemModel::flags(index); 00154 } 00155 00156 void DataObjectTableModel::clear() { 00157 removeRows(0, rowCount(), index(0, 0)); 00158 // reset(); 00159 } 00160 00161 00162 bool DataObjectTableModel:: 00163 insertRows(int position, int rows, const QModelIndex& parent) { 00164 beginInsertRows(parent, position, position+rows-1); 00165 for (int row = 0; row < rows; ++row) { 00166 QSharedPointer<DataObject> dobj (m_Original->clone()); 00167 connect(dobj.data(), SIGNAL(dataObjectChanged(QString)), 00168 this, SLOT(rowChanged(QString))); 00169 m_Data.insert(position, dobj); 00170 } 00171 endInsertRows(); 00172 return true; 00173 } 00174 00175 bool DataObjectTableModel:: 00176 removeRows(int pos, int rows, const QModelIndex& ) { 00177 // qDebug () << "RemoveRows p: " << pos << "rows:" << rows; 00178 if (pos + rows > m_Data.size() ) { 00179 qDebug() << " removerows invalid for size: "<< m_Data.size(); 00180 return false; 00181 } 00182 // QModelIndex topLeft(index(pos, 0, parent)); 00183 beginRemoveRows(QModelIndex(), pos, pos+rows-1); 00184 for (int row = 0; row < rows; ++row) { 00185 disconnect(m_Data[pos].data(), 0, this, 0); 00186 // qDebug() << "Removing: " << pos << m_Data[pos]->objectName(); 00187 m_Data.removeAt(pos); 00188 } 00189 endRemoveRows(); 00190 // QModelIndex bottomRight(index(pos + rows, columnCount(), parent)); 00191 00192 // emit dataChanged(topLeft, bottomRight); 00193 return true; 00194 } 00195 //end 00196 //start 00197 QStringList DataObjectTableModel:: 00198 toStringList() const { 00199 // const int EMPTYLEN(14); 00200 QStringList biglist; 00201 foreach(QSharedPointer<DataObject> doptr, m_Data) { 00202 QString linestr(doptr->toString()); 00203 // if (linestr.length() > EMPTYLEN) 00204 biglist << linestr; 00205 } 00206 return biglist; 00207 } 00208 00209 QString DataObjectTableModel:: 00210 toString() const { 00211 return toStringList().join("\n"); 00212 } 00213 //end 00214