00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "kvttablemodel.h"
00024
00025 #include "prefs.h"
00026 #include "languagesettings.h"
00027
00028 #include <KIcon>
00029 #include <KLocale>
00030 #include <KDebug>
00031 #include <keduvoclesson.h>
00032 #include <keduvocexpression.h>
00033 #include <keduvocgrade.h>
00034 #include <QSize>
00035
00036 KVTTableModel::KVTTableModel(QObject *parent) : QAbstractTableModel(parent)
00037 {
00038 m_doc = 0;
00039 }
00040
00041
00042 void KVTTableModel::setDocument(KEduVocDocument * doc)
00043 {
00044 m_doc = doc;
00045
00046 }
00047
00048
00049 int KVTTableModel::rowCount(const QModelIndex &parent) const
00050 {
00051 Q_UNUSED(parent);
00052 return m_doc->entryCount();
00053 }
00054
00055
00056 int KVTTableModel::columnCount(const QModelIndex &parent) const
00057 {
00058 Q_UNUSED(parent);
00059
00060 return 2 + m_doc->identifierCount();
00061 }
00062
00063
00064 QVariant KVTTableModel::data(const QModelIndex &index, int role) const
00065 {
00066 if (!index.isValid())
00067 return QVariant();
00068
00069
00070
00071 switch (role) {
00072 case KVTTableModel::LessonsRole: {
00073 QStringList sl = m_doc->lessonNames();
00074 return QVariant(sl);
00075 break;
00076 }
00077
00078 case KVTTableModel::LessonRole: {
00079 return QVariant(m_doc->entry(index.row())->lesson());
00080 break;
00081 }
00082
00083 case KVTTableModel::StateRole: {
00084 if (m_doc->entry(index.row())->isActive()) {
00085 return 1;
00086 }
00087 return 0;
00088 break;
00089 }
00090
00091 case KVTTableModel::GradeRole: {
00092 if (index.column() > KV_COL_TRANS) {
00093
00094
00095
00096 return QVariant(m_doc->entry(index.row())->translation(index.column() - KV_COL_TRANS).gradeFrom(0).grade());
00097
00098 } else if (index.column() == 2) {
00099 QList<QVariant> result;
00100 for (int i = 1; i < m_doc->identifierCount(); ++i) {
00101 if (m_doc->entry(index.row())->translation(0).gradeFrom(i).practiceCount() != 0)
00102 result.append(QVariant(m_doc->entry(index.row())->translation(0).gradeFrom(i).grade()));
00103 else
00104 result.append(QVariant(KV_NORM_GRADE));
00105 }
00106 return QVariant(result);
00107 }
00108 break;
00109 }
00110
00111 case Qt::FontRole: {
00112 return QVariant(Prefs::tableFont());
00113 break;
00114 }
00115
00116 case Qt::DisplayRole: {
00117 QVariant result;
00118 if (index.column() == 0) {
00119
00120 result = m_doc->lesson(m_doc->entry(index.row())->lesson()).name();
00121 } else if (index.column() == 1) {
00122 if (m_doc->entry(index.row())->isActive()) {
00123
00124 return "@active@";
00125 } else
00126
00127 return "@inactive@";
00128 } else {
00129 result = m_doc->entry(index.row())->translation(index.column() - KV_COL_TRANS).text();
00130 }
00131 return result;
00132 }
00133 case KVTTableModel::LocaleRole: {
00134 return m_doc->identifier(index.column() - KV_COL_TRANS).locale();
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 default:
00156 break;
00157 }
00158 return QVariant();
00159 }
00160
00161
00162 QVariant KVTTableModel::headerData(int section, Qt::Orientation orientation, int role) const
00163 {
00164 if (orientation == Qt::Horizontal) {
00165 if (role == Qt::DisplayRole) {
00166 if (section == 0)
00167 return i18n("Lesson");
00168 else if (section == 1) {
00169 return QString();
00170 } else {
00171 return m_doc->identifier(section - KV_COL_TRANS).name();
00172 }
00173 }
00174 if (role == Qt::DecorationRole) {
00175 if ( m_headerPixmaps.contains(section) ) {
00176 return m_headerPixmaps.value(section);
00177 }
00178 }
00179 if (role == Qt::SizeHintRole) {
00180 switch (section) {
00181 case 0:
00182 return QSize(m_doc->sizeHint(-1), 25);
00183 break;
00184 case 1:
00185 return QSize(25, 25);
00186 break;
00187 default:
00188 return QSize(m_doc->sizeHint(section - KV_COL_TRANS), 25);
00189 break;
00190 }
00191 }
00192 return QVariant();
00193 } else {
00194 return QAbstractTableModel::headerData(section, orientation, role);
00195 }
00196 }
00197
00198
00199 Qt::ItemFlags KVTTableModel::flags(const QModelIndex &index) const
00200 {
00201 if (!index.isValid())
00202 return Qt::ItemIsEnabled;
00203
00204 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
00205 }
00206
00207
00208 bool KVTTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
00209 {
00210 if (index.isValid() && role == Qt::EditRole) {
00211 if (index.column() == 0)
00212 m_doc->entry(index.row())->setLesson(value.toInt());
00213 else if (index.column() == 1) {
00214 m_doc->entry(index.row())->setActive(value.toInt());
00215 } else {
00216 m_doc->entry(index.row())->setTranslation(index.column() - 2, value.toString());
00217 }
00218 emit dataChanged(index, index);
00219 m_doc->setModified(true);
00220 return true;
00221 } else if (index.isValid() && role == KVTTableModel::LessonRole) {
00222 m_doc->entry(index.row())->setLesson(value.toInt());
00223 }
00224 return false;
00225 }
00226
00227
00228 bool KVTTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
00229 {
00230 if (orientation == Qt::Horizontal) {
00231 if (role == Qt::SizeHintRole) {
00232 switch (section) {
00233 case 0:
00234 m_doc->setSizeHint(-1, qvariant_cast<QSize>(value).width());
00235 ;
00236 case 1:
00237 default:
00238 m_doc->setSizeHint(section - KV_COL_TRANS, qvariant_cast<QSize>(value).width());
00239 }
00240 }
00241 emit headerDataChanged(orientation, section, section);
00242 return true;
00243 }
00244 return false;
00245 }
00246
00247
00248 bool KVTTableModel::insertRows(int row, int count, const QModelIndex & parent)
00249 {
00250 Q_UNUSED(parent);
00251 if (count < 1 || row < 0) {
00252 return false;
00253 }
00254
00255 int bottomRow = row + count -1;
00256 beginInsertRows(QModelIndex(), row, bottomRow);
00257
00258 for (int i = bottomRow; i >= row; i--) {
00259 KEduVocExpression* entry = new KEduVocExpression(QString(), m_doc->currentLesson() );
00260 m_doc->insertEntry(entry, i);
00261 }
00262
00263 endInsertRows();
00264 m_doc->setModified(true);
00265 return true;
00266 }
00267
00268
00269 bool KVTTableModel::removeRows(int row, int count, const QModelIndex & parent)
00270 {
00271 Q_UNUSED(parent);
00272 if (count < 1 || row < 0 || row + count > m_doc->entryCount()) {
00273 return false;
00274 }
00275
00276 int bottomRow = row + count - 1;
00277 beginRemoveRows(QModelIndex(), row, bottomRow);
00278
00279 for (int i = bottomRow; i >= row; i--)
00280 m_doc->removeEntry(i);
00281
00282 endRemoveRows();
00283 m_doc->setModified(true);
00284 return true;
00285 }
00286
00287
00288 void KVTTableModel::appendTranslation()
00289 {
00290 beginInsertColumns(QModelIndex(), columnCount(QModelIndex()), columnCount(QModelIndex()));
00291
00292 m_doc->appendIdentifier();
00293
00294 endInsertColumns();
00295 m_doc->setModified(true);
00296 }
00297
00298
00299 bool KVTTableModel::removeTranslation(int translationIndex)
00300 {
00301 if( translationIndex < 0 || translationIndex >= m_doc->identifierCount() ) {
00302 return false;
00303 }
00304
00305 beginRemoveColumns(QModelIndex(), translationIndex, translationIndex);
00306 m_doc->removeIdentifier(translationIndex - KV_COL_TRANS);
00307 endRemoveColumns();
00308
00309 m_doc->setModified(true);
00310 return true;
00311 }
00312
00313 void KVTTableModel::dataChangedFromOutside(const QModelIndex & firstRow, const QModelIndex & lastRow)
00314 {
00315 kDebug() << "Updating: " << firstRow.row() << ", " << firstRow.column() << " to " << lastRow.row() << ", " << lastRow.column() << " columnCount: " << columnCount(QModelIndex());
00316 QModelIndex topLeft = index(firstRow.row()-1, 0);
00317 QModelIndex bottomRight = index(lastRow.row()+1, columnCount(QModelIndex()));
00318 emit dataChanged(topLeft, bottomRight);
00319 }
00320
00321 void KVTTableModel::appendEntry()
00322 {
00323 KEduVocExpression* entry = new KEduVocExpression(QString(), m_doc->currentLesson() );
00324
00325
00326 beginInsertRows(QModelIndex(), m_doc->entryCount(), m_doc->entryCount());
00327 m_doc->insertEntry(entry, m_doc->entryCount());
00328 endInsertRows();
00329 }
00330
00331 void KVTTableModel::loadLanguageSettings()
00332 {
00333 reset();
00334
00335 for (int i=0; i<m_doc->identifierCount(); i++){
00336 LanguageSettings currentSettings(m_doc->identifier(i).locale());
00337 currentSettings.readConfig();
00338 QString icon = currentSettings.icon();
00339 m_headerPixmaps[i + KV_COL_TRANS] = QPixmap(icon);
00340
00341 emit headerDataChanged(Qt::Horizontal, i + KV_COL_TRANS, i + KV_COL_TRANS);
00342 }
00343 }
00344
00345 #include "kvttablemodel.moc"
00346
00347