00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kwicmodels.h"
00022
00023 #include "phrasehunter/contextreader.h"
00024 #include "phrasehunter/token.h"
00025 #include "phrasehunter/tokencontext.h"
00026
00027 #include "support/unicodehelpers.h"
00028
00029 #include <boost/lexical_cast.hpp>
00030
00031 #include "qt_utils.h"
00032
00033 using namespace PhraseHunter;
00034
00035 void addAllOccurrences(TokenPtr token,
00036 std::vector<TokenContextPtr>& entries)
00037 {
00038 const OccurrenceMap& all_occurrences = token->allOccurrences();
00039 for(OccurrenceMap::const_iterator it = all_occurrences.begin();
00040 it != all_occurrences.end(); ++it) {
00041
00042 for(PositionList::const_iterator it_inner = it->second.begin();
00043 it_inner != it->second.end(); ++it_inner) {
00044 entries.push_back(TokenContext::emptyContext(it->first, *it_inner, token->length()));
00045 }
00046 }
00047 }
00048
00049 KwicTableModel::KwicTableModel(QObject* parent, ContextReader* reader):
00050 QAbstractTableModel(parent), m_reader(reader), m_width(40)
00051 {
00052 }
00053
00054 QVariant KwicTableModel::headerData(int section, Qt::Orientation orientation, int role) const
00055 {
00056 if(orientation != Qt::Horizontal || role != Qt::DisplayRole) {
00057 return QVariant();
00058 }
00059
00060 switch(section){
00061 case 0:
00062 return QVariant("");
00063 case 1:
00064 return QVariant("Left Context");
00065 case 2:
00066 return QVariant("Token");
00067 case 3:
00068 return QVariant("Right Context");
00069 default:
00070 return QVariant();
00071 }
00072 }
00073
00074
00075 int KwicTableModel::columnCount(const QModelIndex& parent) const
00076 {
00077 return 4;
00078 }
00079
00080 int KwicTableModel::rowCount(const QModelIndex& parent) const
00081 {
00082 return entries.size();
00083 }
00084
00085 QVariant KwicTableModel::getContextString(TokenContextPtr context, const QModelIndex& index) const
00086 {
00087 if(context->empty()) {
00088 m_reader->fillContext(context, m_width);
00089 }
00090
00091 schma::UnicodePtr unistr;
00092
00093 switch(index.column()) {
00094 case 0:
00095 return QVariant(QString::number(index.row()+1));
00096 case 1:
00097 unistr = context->leftContext();
00098 break;
00099 case 2:
00100 unistr = context->token();
00101 break;
00102 case 3:
00103 unistr = context->rightContext();
00104 break;
00105 }
00106 return QVariant(icuToQString(unistr));
00107 }
00108
00109 QVariant KwicTableModel::data(const QModelIndex& index, int role) const
00110 {
00111 return (index.isValid() && role == Qt::DisplayRole)
00112 ? getContextString(entries[index.row()], index)
00113 : QVariant();
00114 }
00115
00116 void KwicTableModel::setData(TokenPtr token)
00117 {
00118 entries.clear();
00119 addAllOccurrences(token, entries);
00120 reset();
00121 }
00122
00123 void KwicTableModel::setData(const TokenVector& tokens)
00124 {
00125 entries.clear();
00126 for(TokenVector::const_iterator it = tokens.begin();
00127 it != tokens.end(); ++it) {
00128 addAllOccurrences(*it, entries);
00129 }
00130 reset();
00131 }