-rw-r--r-- | noncore/apps/tableviewer/db/common.cpp | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/noncore/apps/tableviewer/db/common.cpp b/noncore/apps/tableviewer/db/common.cpp index 6e544ba..b58af85 100644 --- a/noncore/apps/tableviewer/db/common.cpp +++ b/noncore/apps/tableviewer/db/common.cpp @@ -573,906 +573,906 @@ bool TVVariant::closer(TVVariant n, TVVariant o) switch(type()){ case String: { /* case for strings is close is a substring.. closer is * earlier alphabetically */ QString qs1 = n.toString().lower(); QString qs2 = o.toString().lower(); QString qsv = toString().lower(); if (!qs1.startsWith(qsv)) return FALSE; /* contains sub-str, if later than is not closer */ if(QString::compare(qs1, qs2) > 0) return FALSE; return TRUE; } case Int: { /* case for int is smallest absolute difference */ int i1 = n.toInt(); int i2 = o.toInt(); int iv = toInt(); int diff1 = (i1 - iv); if (diff1 < 0) diff1 = -diff1; int diff2 = (i2 - iv); if (diff2 < 0) diff2 = -diff2; if (diff1 < diff2) return TRUE; return FALSE; } case Date: { QDate i1 = n.toDate(); QDate i2 = o.toDate(); QDate iv = toDate(); /* definition of closer is the least difference in days */ int diff1 = i1.daysTo(iv); if (diff1 < 0) diff1 = -diff1; int diff2 = i2.daysTo(iv); if (diff2 < 0) diff2 = -diff2; if (diff1 < diff2) return TRUE; return FALSE; } case Time: { QTime i1 = n.toTime(); QTime i2 = o.toTime(); QTime iv = toTime(); /* definition of closer is the least difference in days */ int diff1 = i1.msecsTo(iv); if (diff1 < 0) diff1 = -diff1; int diff2 = i2.msecsTo(iv); if (diff2 < 0) diff2 = -diff2; if (diff1 < diff2) return TRUE; return FALSE; } default: /* don't know how to do 'closer' on this type, hence never closer * or even close */ break; } return FALSE; } /*! True if n is close to this */ bool TVVariant::close(TVVariant n) { /* Nothing is close to an invalid, so nothing can be closer */ if(type() == Invalid) return FALSE; /* can't be close if of different type */ if(n.type() != type()) return FALSE; switch(type()){ case String: { /* case for strings is close is a substring.. closer is * earlier alphabetically */ QString qs1 = n.toString().lower(); QString qsv = toString().lower(); if (!qs1.startsWith(qsv)) return FALSE; return TRUE; } case Int: case Date: case Time: return TRUE; default: /* don't know how to do 'closer' on this type, hence never closer * or even close */ break; } return FALSE; } /*! \class Key \brief document me! document me! */ Key::Key() : kname(), kexample(), kflags(0) { } Key::Key(QString name, TVVariant example, int flags) : kname(name), kexample(example), kflags(flags) { } Key::Key(const Key &other) { kname = other.kname; kexample = other.kexample; kflags = other.kflags; } Key& Key::operator=(const Key& key) { kname = key.kname; kexample = key.kexample; kflags = key.kflags; return *this; } QString Key::name() const { return QString(kname); } TVVariant Key::example() const { return TVVariant(kexample); } TVVariant::KeyType Key::type() const { return kexample.type(); } void Key::setName(const QString &name) { kname = QString(name); } void Key::setExample(const TVVariant &e) { kexample = TVVariant(e); } int Key::flags() const { return kflags; } void Key::setFlags(int fl) { kflags = fl; } bool Key::delFlag() const { if(kflags & del_flag) return TRUE; return FALSE; } bool Key::newFlag() const { if(kflags & new_flag) return TRUE; return FALSE; } void Key::setDelFlag(bool v) { if(delFlag() != v) kflags = kflags ^ del_flag; } void Key::setNewFlag(bool v) { if(newFlag() != v) kflags = kflags ^ new_flag; } /*! \class KeyList \brief A represntation of keys used for a table. The KeyList class is used to store the representation of keys used in table headings by DBStore. It stores the names and types of the keys */ /*! Constructs a KeyList */ KeyList::KeyList() : QIntDict<Key>(20) { setAutoDelete(TRUE); } /* Should be deep copy, but isn't */ KeyList::KeyList(const KeyList &k) : QIntDict<Key>(k) { KeyListIterator it(k); while(it.current()) { replace(it.currentKey(), new Key(*it.current())); ++it; } setAutoDelete(TRUE); } /*! Destroys a KeyList */ KeyList::~KeyList() { } /* Do a comparision base on Keys */ bool KeyList::operator!=(const KeyList &other) { KeyListIterator it(*this); if (other.getNumFields() != getNumFields()) return TRUE; while(it.current()) { //it.currentKey(), it.current(); if (other.getKeyName(it.currentKey()) != getKeyName(it.currentKey())) return TRUE; if (other.getKeyType(it.currentKey()) != getKeyType(it.currentKey())) return TRUE; ++it; } return FALSE; } /*! Returns the number of keys stored in the KeyList */ int KeyList::getNumFields() const { return count(); } /*! Adds a new key to the KeyList \param name the name of the new key \param type the type of the new key */ int KeyList::addKey(QString name, TVVariant example) { int i = count(); while(find(i) && (i > -1)) i--; replace(i, new Key(name, example, 0)); return i; } int KeyList::addKey(QString name, TVVariant::KeyType type) { /* generate a valid type for the example? */ TVVariant e = TVVariant("0"); switch(type) { case TVVariant::String: return addKey(name, TVVariant("<undefined>").asString()); break; case TVVariant::Date: return addKey(name, TVVariant(QDate::currentDate()).asDate()); break; case TVVariant::Time: return addKey(name, TVVariant(QTime(0,0,0)).toTime()); break; case TVVariant::Int: return addKey(name, TVVariant(0).toInt()); break; default: qWarning(QObject::tr("KeyList::addKey() Cannot make default " "value for type %1, Key not added.").arg(type)); break; } return -1; } void KeyList::setKeyFlags(int i, int flag) { if(find(i)) find(i)->setFlags(flag); } int KeyList::getKeyFlags(int i) const { if(find(i)) return find(i)->flags(); return 0; } bool KeyList::checkNewFlag(int i) const { if (find(i)) return find(i)->newFlag(); return false; } void KeyList::setNewFlag(int i, bool f) { if(!find(i)) return; find(i)->setNewFlag(f); } bool KeyList::checkDeleteFlag(int i) const { if (find(i)) return find(i)->delFlag(); return false; } void KeyList::setDeleteFlag(int i, bool f) { if(!find(i)) return; find(i)->setDelFlag(f); } /*! Returns the name of the key at index i */ QString KeyList::getKeyName(int i) const { if (find (i)) return find(i)->name(); return QString(); } void KeyList::setKeyName(int i, const QString &n) { if(find(i)) find(i)->setName(n); } /*! Returns the type of the key at index i */ TVVariant::KeyType KeyList::getKeyType(int i) const { if(find(i)) return find(i)->type(); return TVVariant::Invalid; } void KeyList::setKeyType(int i, TVVariant::KeyType t) { if(!find(i)) return; switch(t) { case TVVariant::String: find(i)->setExample(TVVariant(QString("default"))); return; case TVVariant::Int: find(i)->setExample(TVVariant(int(0))); return; case TVVariant::Date: find(i)->setExample(TVVariant(QDate::currentDate())); return; case TVVariant::Time: find(i)->setExample(TVVariant(QTime(0,0,0,0))); return; default: break; } return; } TVVariant KeyList::getKeyExample(int i) const { if(find(i)) return find(i)->example(); return TVVariant(); } void KeyList::setKeyExample(int i, TVVariant example) { if(find(i)) find(i)->setExample(example); } /*! Returns the index of the key with name q */ int KeyList::getKeyIndex(QString q) const { KeyListIterator it(*this); while(it.current()) { if(it.current()->name() == q) return it.currentKey(); ++it; } return -1; } bool KeyList::validIndex(int i) const { if(!find(i)) return FALSE; if(find(i)->delFlag()) return FALSE; return TRUE; } QDataStream &operator<<( QDataStream &s, const KeyList &k) { s << k.getNumFields(); KeyListIterator it(k); while(it.current()) { s << (Q_UINT16)it.currentKey(); s << it.current()->name(); s << it.current()->example(); s << (Q_UINT16)it.current()->flags(); ++it; } return s; } QDataStream &operator>>( QDataStream &s, KeyList &k) { int i; int size; int index = 0; int flags = 0; TVVariant type = TVVariant(); QString name; s >> size; for (i=0; i < size; i++) { s >> (Q_UINT16 &)index; s >> name; s >> type; s >> (Q_UINT16 &)flags; k.replace(index, new Key(name, type, flags)); } return s; } /*! \class DataElem \brief A class representing a single row or element of a table in a DBStore This class holds the data of a row in a table. */ /*! Constructs a DataElem. This function needs a container because the size, types of keys and primary key are all defined by the containing database */ DataElem::DataElem(DBStore *c) : values(20) { int size; contained = c; size = c->getNumFields(); values.setAutoDelete(TRUE); } /*! Destroys a DataElem and frees memory used by the DataElem */ DataElem::~DataElem() { } QDataStream &operator<<( QDataStream &s, const DataElem &d) { int size = d.getNumFields(); s << size; /* redundent data but makes streaming easier */ KeyList k = d.getKeys(); KeyListIterator it(k); while(it.current()) { s << (Q_UINT16)it.currentKey(); s << d.getField(it.currentKey()); ++it; } return s; } QDataStream &operator>>( QDataStream &s, DataElem &d) { int i; int size; TVVariant t; - int index = 0; + Q_UINT16 index = 0; s >> size; /* redundent data but makes streaming easier */ if (size != d.getNumFields()) { owarn << "DataSize mis-match" << oendl; return s; /* sanity check failed.. don't load */ } for(i = 0; i < size; i++) { - s >> (Q_UINT16)index; + s >> index; s >> t; d.setField(index, t); } return s; } /*! Returns the number of possible (not valid) fields in the data element */ int DataElem::getNumFields() const { return contained->getNumFields(); } KeyList DataElem::getKeys() const { return *(contained->getKeys()); } /*! This function determines whether field index i of the element has been set yet. \return A boolean value that is TRUE if the specfied field of this element has been set and FALSE if the field has not yet been set */ bool DataElem::hasValidValue(int i) const { if(!values.find(i)) return FALSE; if(!contained->getKeys()->validIndex(i)) return FALSE; return values.find(i)->isValid(); } /*! This function determines whether field name qs of the element has been set yet. \return A boolean value that is TRUE if the specfied field of this element has been set and FALSE if the field has not yet been set */ bool DataElem::hasValidValue(QString qs) const { int i = contained->getKeyIndex(qs); return hasValidValue(i); } /*! returns the type of the field specified by index i */ TVVariant::KeyType DataElem::getFieldType(int i) const { return contained->getKeyType(i); } /*! returns the type of the field specified by name qs */ TVVariant::KeyType DataElem::getFieldType(QString qs) const { int i = contained->getKeyIndex(qs); return contained->getKeyType(i); } /*! returns a pointer to the data stored in field index i for this data element, (value may not be valid) */ TVVariant DataElem::getField(int i) const { if(hasValidValue(i)) return TVVariant(*values.find(i)); return TVVariant(); } /*! returns a pointer to the data stored in field name qs for this data element, (value may not be valid) */ TVVariant DataElem::getField(QString qs) const { int i = contained->getKeyIndex(qs); return getField(i); } /*! Sets the value of the elements field index i to the value represented in the QString q. \param i index of the field to set \param q a string that can be parsed to get the value to be set */ void DataElem::setField(int i, QString q) { /* from the type of the field, parse q and store */ TVVariant::KeyType kt = contained->getKeyType(i); TVVariant t = TVVariant(q); switch(kt) { case TVVariant::Int: { t.asInt(); setField(i, t); return; } case TVVariant::String: { t.asString(); setField(i, t); return; } case TVVariant::Date: { t.asDate(); setField(i, t); return; } case TVVariant::Time: { t.asTime(); setField(i, t); return; } default: qWarning( QObject::tr("DataElem::setField(%1, %2) No valid type found").arg(i).arg(q) ); } } /*! Sets the value of the elements field index i to the value at the pointer value. \param i index of the field to set \param value a pointer to the (already allocated) value to set */ void DataElem::setField(int i, TVVariant value) { if (value.isValid()) { values.remove(i); values.replace(i, new TVVariant(value)); } } /*! Sets the value of the elements field name qs to the value represented in the QString q. \param qs name of the field to set \param q a string that can be parsed to get the value to be set */ void DataElem::setField(QString qs, QString q) { /* from the type of the field, parse q and store */ int i = contained->getKeyIndex(qs); setField(i, qs); } /*! Sets the value of the elements field name qs to the value at the pointer value. \param qs name of the field to set \param value a pointer to the (already allocated) value to set */ void DataElem::setField(QString qs, TVVariant value) { int i = contained->getKeyIndex(qs); setField(i, value); } void DataElem::unsetField(int i) { values.remove(i); } void DataElem::unsetField(QString qs) { int i = contained->getKeyIndex(qs); unsetField(i); } /*! Converts the data element to a Rich Text QString */ QString DataElem::toQString() const { /* lets make an attempt at this function */ int i; QString scratch = ""; QIntDictIterator<TVVariant> it(values); while (it.current()) { i = it.currentKey(); if(hasValidValue(i)) { scratch += "<B>" + contained->getKeyName(i) + ":</B> "; scratch += getField(i).toString(); scratch += "<br>"; } ++it; } return scratch; } /*! formats individual fields to strings so can be displayed */ QString DataElem::toQString(int i) const { if(hasValidValue(i)) { return getField(i).toString(); } return ""; } /*! formats individual fields to strings so can be sorted by QListView */ QString DataElem::toSortableQString(int i) const { QString scratch = ""; if(hasValidValue(i)) { switch (contained->getKeyType(i)) { case TVVariant::String: { scratch += getField(i).toString(); break; } case TVVariant::Int: { scratch.sprintf("%08d", getField(i).toInt()); break; } case TVVariant::Date: { static QDate epochD(1800, 1, 1); scratch.sprintf("%08d", epochD.daysTo(getField(i).toDate())); break; } case TVVariant::Time: { static QTime epochT(0, 0, 0); scratch.sprintf("%08d", epochT.msecsTo(getField(i).toTime())); break; } default: scratch += "Unknown type"; break; } } return scratch; } /* compare functions */ bool DataElem::lessThan(int i, TVVariant v) const { if (!hasValidValue(i)) return FALSE; if (getField(i).type() != v.type()) return FALSE; return (getField(i) < v); } bool DataElem::moreThan(int i, TVVariant v) const { if (!hasValidValue(i)) return FALSE; if (getField(i).type() != v.type()) return FALSE; return (getField(i) > v); } bool DataElem::equalTo(int i, TVVariant v) const { if (!hasValidValue(i)) return FALSE; if (getField(i).type() != v.type()) return FALSE; return (getField(i) == v); } bool DataElem::contains(int i, TVVariant v) const { if (!hasValidValue(i)) return FALSE; if (getField(i).type() != v.type()) return FALSE; switch(getField(i).type()) { case TVVariant::String: { QString qs1 = getField(i).toString().lower(); QString qs2 = v.toString().lower(); if (qs1.contains(qs2) > 0) return TRUE; break; } /* meaningless for ints */ /* meaningless for time */ /* meaningless for dates */ case TVVariant::Int: case TVVariant::Time: case TVVariant::Date: break; default: owarn << "Tried to compare unknown data type" << oendl; } return FALSE; } bool DataElem::startsWith(int i, TVVariant v) const { if (!hasValidValue(i)) return FALSE; if (getField(i).type() != v.type()) return FALSE; switch(getField(i).type()) { case TVVariant::String: { QString qs1 = getField(i).toString().lower(); QString qs2 = v.toString().lower(); return qs1.startsWith(qs2); } /* meaningless for ints */ /* meaningless for time */ /* meaningless for dates */ case TVVariant::Int: case TVVariant::Time: case TVVariant::Date: return FALSE; default: owarn << "Tried to compare unknown data type" << oendl; } return FALSE; } bool DataElem::endsWith(int i, TVVariant v) const { if (!hasValidValue(i)) return FALSE; if (getField(i).type() != v.type()) return FALSE; switch(getField(i).type()) { case TVVariant::String: { QString qs1 = getField(i).toString().lower(); QString qs2 = v.toString().lower(); return qs1.startsWith(qs2); } /* meaningless for ints */ /* meaningless for time */ /* meaningless for dates */ case TVVariant::Int: case TVVariant::Time: case TVVariant::Date: return FALSE; default: owarn << "Tried to compare unknown data type" << oendl; } return FALSE; } /*! Determins which of the first to parameters are closer to the third, target parameter. \return <UL> <LI>TRUE if the first element is a closer match to the target than the second element</LI> <LI>FALSE if the first element is not a closer match to the target than the second element</LI> </UL> */ bool DataElem::closer(DataElem*d1, DataElem *d2, TVVariant target, int column) { int type; if(!d1) return FALSE; if (!d1->hasValidValue(column)) return FALSE; if(!target.isValid()) return FALSE; type = d1->getField(column).type(); if(d2) { if (type != d2->getField(column).type()) { /* can't do compare */ owarn << "Tried to compare two incompatable types" << oendl; return FALSE; } return target.closer(d1->getField(column), d2->getField(column)); } return target.close(d1->getField(column)); } |