QModelIndex MessageModel::index(int row, int column, const QModelIndex &parent /*= QModelIndex()*/) const { if (row < 0 || column < 0) return QModelIndex(); if (!parent.isValid()) { // The context items if (row >= rowCount() || column >= columnCount()) { return QModelIndex(); } return createIndex(row, column, 0); //internalpointer points to the 'owner' object } else { // The message items if (parent.internalPointer()) return QModelIndex(); int toprow = parent.row(); ContextItem *c = cntxtList.at(toprow); if (row > c->messageItemsInList()) return QModelIndex(); return createIndex(row, column, c); } }
bool MessageModel::findMessage(int *contextNo, int *itemNo, const QString &findText, int findWhere, bool matchSubstring, Qt::CaseSensitivity cs) { bool found = false; if (contextsInList() <= 0) return false; int pass = 0; int scopeNum = *contextNo; int itemNum = *itemNo; MessageItem *m = 0; // We want to search the scope we started from *again*, since we did not necessarily search that *completely* when we started. // (Problaby we started somewhere in the middle of it.) // Therefore, "pass <=" and not "pass < " while (!found && pass <= contextsInList()) { ContextItem *c = contextList().at(scopeNum); for (int mit = itemNum; mit < c->messageItemsInList() ; ++mit) { m = c->messageItem(mit); QString searchText; switch (findWhere) { case SourceText: searchText = m->sourceText(); break; case Translations: searchText = m->translation(); break; case Comments: searchText = m->comment(); break; } if (matchSubstring) { if (searchText.indexOf(findText,0, cs) >= 0) { found = true; break; } } else { if (cs == Qt::CaseInsensitive) { if (findText.toLower() == searchText.toLower()) { found = true; break; } } else { if ( findText == searchText ) { found = true; break; } } } } itemNum = 0; ++pass; ++scopeNum; if (scopeNum >= contextsInList()) { scopeNum = 0; //delayedMsg = tr("Search wrapped."); } } if (found) { *itemNo = itemNum; *contextNo = scopeNum; } return found; }
MessageItem *MessageModel::messageItem(int context, int message) const { ContextItem *c = contextItem(context); if (c && message >= 0 && message < c->messageItemsInList()) return c->messageItem(message); return 0; }
QVariant MessageModel::data(const QModelIndex &index, int role) const { int row = index.row(); int column = index.column(); ContextItem *cntxtItem = static_cast<ContextItem *>(index.internalPointer()); if (cntxtItem) { if (row >= cntxtItem->messageItemsInList() || !index.isValid()) return QVariant(); MessageItem *msgItem = cntxtItem->messageItem(row); if (role == Qt::DisplayRole) { switch(column) { case 0: // done return QVariant(); case 1: // source text return msgItem->sourceText().simplified(); case 2: // translation return msgItem->translation().simplified(); } } else if ((role == Qt::DecorationRole) && (column == 0)) { if (msgItem->message().type() == MetaTranslatorMessage::Unfinished && msgItem->translation().isEmpty()) return qVariantFromValue(*TrWindow::pxEmpty); else if (msgItem->message().type() == MetaTranslatorMessage::Unfinished && msgItem->danger()) return qVariantFromValue(*TrWindow::pxDanger); else if (msgItem->message().type() == MetaTranslatorMessage::Finished && msgItem->danger()) return qVariantFromValue(*TrWindow::pxWarning); else if (msgItem->message().type() == MetaTranslatorMessage::Finished) return qVariantFromValue(*TrWindow::pxOn); else if (msgItem->message().type() == MetaTranslatorMessage::Unfinished) return qVariantFromValue(*TrWindow::pxOff); else if (msgItem->message().type() == MetaTranslatorMessage::Obsolete) return qVariantFromValue(*TrWindow::pxObsolete); } } else { if (row >= cntxtList.count() || !index.isValid()) return QVariant(); ContextItem *cntxtItem = cntxtList.at(row); if (role == Qt::DisplayRole) { switch(column) { case 0: // done return QVariant(); case 1: // context return cntxtItem->context().simplified(); case 2: // items QString s; int itemCount = cntxtItem->messageItemsInList(); int obsoleteCount = cntxtItem->obsolete(); int finishedCount = cntxtItem->finishedCount(); s.sprintf("%d/%d", finishedCount, itemCount - obsoleteCount); return s; } } else if ((role == Qt::DecorationRole) && (column == 0)) { if (cntxtItem->isContextObsolete()) return qVariantFromValue(*TrWindow::pxObsolete); else if (cntxtItem->isFinished()) return qVariantFromValue(*TrWindow::pxOn); else return qVariantFromValue(*TrWindow::pxOff); } } return QVariant(); }