summaryrefslogtreecommitdiff
path: root/noncore/apps/tableviewer/db/common.cpp
Unidiff
Diffstat (limited to 'noncore/apps/tableviewer/db/common.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/tableviewer/db/common.cpp165
1 files changed, 81 insertions, 84 deletions
diff --git a/noncore/apps/tableviewer/db/common.cpp b/noncore/apps/tableviewer/db/common.cpp
index b58af85..c35dbea 100644
--- a/noncore/apps/tableviewer/db/common.cpp
+++ b/noncore/apps/tableviewer/db/common.cpp
@@ -13,14 +13,14 @@
13** 13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information. 14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15** 15**
16** Contact info@trolltech.com if any conditions of this licensing are 16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you. 17** not clear to you.
18** 18**
19**********************************************************************/ 19**********************************************************************/
20#include "common.h" 20#include "common.h"
21#include "datacache.h" 21#include "datacache.h"
22 22
23/* OPIE */ 23/* OPIE */
24#include <opie2/odebug.h> 24#include <opie2/odebug.h>
25#include <qpe/timestring.h> 25#include <qpe/timestring.h>
26using namespace Opie::Core; 26using namespace Opie::Core;
@@ -85,19 +85,19 @@ int parseNextNumber(QString *q) {
85 </UL> 85 </UL>
86*/ 86*/
87int QStringVector::compareItems(Item a, Item b) 87int QStringVector::compareItems(Item a, Item b)
88{ 88{
89 QString *qa = (QString *)a; 89 QString *qa = (QString *)a;
90 QString *qb = (QString *)b; 90 QString *qb = (QString *)b;
91 91
92 return QString::compare(*qa, *qb); 92 return QString::compare(*qa, *qb);
93} 93}
94 94
95/*! 95/*!
96 \class TVVariant 96 \class TVVariant
97 A way of abstracting void * and keeping information on 97 A way of abstracting void * and keeping information on
98 the keytypes and behaviours in one place 98 the keytypes and behaviours in one place
99*/ 99*/
100 100
101TVVariantPrivate::TVVariantPrivate() 101TVVariantPrivate::TVVariantPrivate()
102{ 102{
103 typ = TVVariant::Invalid; 103 typ = TVVariant::Invalid;
@@ -317,13 +317,13 @@ void TVVariant::load(QDataStream &s )
317 int x; 317 int x;
318 s >> x; 318 s >> x;
319 d->value.i = x; 319 d->value.i = x;
320 } 320 }
321 break; 321 break;
322 default: 322 default:
323 ofatal << "Unrecognized data type" << oendl; 323 ofatal << "Unrecognized data type" << oendl;
324 } 324 }
325} 325}
326 326
327void TVVariant::save( QDataStream &s ) const 327void TVVariant::save( QDataStream &s ) const
328{ 328{
329 s << type(); 329 s << type();
@@ -379,13 +379,13 @@ const QString TVVariant::toString() const
379 case String: 379 case String:
380 return *((QString*)d->value.ptr); 380 return *((QString*)d->value.ptr);
381 case Date: 381 case Date:
382 return ((QDate*)d->value.ptr)->toString(); 382 return ((QDate*)d->value.ptr)->toString();
383 case Time: 383 case Time:
384 return ((QTime*)d->value.ptr)->toString(); 384 return ((QTime*)d->value.ptr)->toString();
385 case Int: 385 case Int:
386 return QString::number(d->value.i); 386 return QString::number(d->value.i);
387 case Invalid: 387 case Invalid:
388 default: 388 default:
389 return QString::null; 389 return QString::null;
390 } 390 }
391 return QString::null; 391 return QString::null;
@@ -421,22 +421,22 @@ const QDate TVVariant::toDate() const
421 int month = parseNextNumber(&q); 421 int month = parseNextNumber(&q);
422 int year = parseNextNumber(&q); 422 int year = parseNextNumber(&q);
423 if (!QDate::isValid(year, month, day)) 423 if (!QDate::isValid(year, month, day))
424 return QDate(); 424 return QDate();
425 return QDate(year, month, day); 425 return QDate(year, month, day);
426 } 426 }
427 427
428 428
429 return QDate(); 429 return QDate();
430} 430}
431 431
432const QTime TVVariant::toTime() const 432const QTime TVVariant::toTime() const
433{ 433{
434 if(d->typ == Time) 434 if(d->typ == Time)
435 return *((QTime *)d->value.ptr); 435 return *((QTime *)d->value.ptr);
436 436
437 if(d->typ == String) { 437 if(d->typ == String) {
438 QString q = toString(); 438 QString q = toString();
439 int hour = parseNextNumber(&q); 439 int hour = parseNextNumber(&q);
440 int minute = parseNextNumber(&q); 440 int minute = parseNextNumber(&q);
441 int second = parseNextNumber(&q); 441 int second = parseNextNumber(&q);
442 int msecond = parseNextNumber(&q); 442 int msecond = parseNextNumber(&q);
@@ -451,13 +451,13 @@ const QTime TVVariant::toTime() const
451#define TV_VARIANT_AS( f ) Q##f& TVVariant::as##f() { \ 451#define TV_VARIANT_AS( f ) Q##f& TVVariant::as##f() { \
452 if ( d->typ != f ) \ 452 if ( d->typ != f ) \
453 *this = TVVariant( to##f() ); \ 453 *this = TVVariant( to##f() ); \
454 else \ 454 else \
455 detach(); \ 455 detach(); \
456 return *((Q##f*)d->value.ptr); } 456 return *((Q##f*)d->value.ptr); }
457 457
458TV_VARIANT_AS(String) 458TV_VARIANT_AS(String)
459TV_VARIANT_AS(Date) 459TV_VARIANT_AS(Date)
460TV_VARIANT_AS(Time) 460TV_VARIANT_AS(Time)
461 461
462#undef TV_VARIANT_AS 462#undef TV_VARIANT_AS
463 463
@@ -557,66 +557,66 @@ bool TVVariant::operator>( const TVVariant &v ) const
557 557
558/*! True if n is closer to this than o */ 558/*! True if n is closer to this than o */
559bool TVVariant::closer(TVVariant n, TVVariant o) 559bool TVVariant::closer(TVVariant n, TVVariant o)
560{ 560{
561 /* Nothing is close to an invalid, so nothing can be closer */ 561 /* Nothing is close to an invalid, so nothing can be closer */
562 if(d->typ == Invalid) 562 if(d->typ == Invalid)
563 return FALSE; 563 return FALSE;
564 564
565 /* can't be closer if of different type */ 565 /* can't be closer if of different type */
566 if(n.type() != type()) 566 if(n.type() != type())
567 return FALSE; 567 return FALSE;
568 568
569 /* if new shares type, and old doesn't, then new is closer */ 569 /* if new shares type, and old doesn't, then new is closer */
570 if(o.type() != type()) 570 if(o.type() != type())
571 return TRUE; 571 return TRUE;
572 572
573 switch(type()){ 573 switch(type()){
574 case String: { 574 case String: {
575 /* case for strings is close is a substring.. closer is 575 /* case for strings is close is a substring.. closer is
576 * earlier alphabetically */ 576 * earlier alphabetically */
577 QString qs1 = n.toString().lower(); 577 QString qs1 = n.toString().lower();
578 QString qs2 = o.toString().lower(); 578 QString qs2 = o.toString().lower();
579 QString qsv = toString().lower(); 579 QString qsv = toString().lower();
580 580
581 if (!qs1.startsWith(qsv)) 581 if (!qs1.startsWith(qsv))
582 return FALSE; 582 return FALSE;
583 583
584 /* contains sub-str, if later than is not closer */ 584 /* contains sub-str, if later than is not closer */
585 if(QString::compare(qs1, qs2) > 0) 585 if(QString::compare(qs1, qs2) > 0)
586 return FALSE; 586 return FALSE;
587 return TRUE; 587 return TRUE;
588 } 588 }
589 case Int: { 589 case Int: {
590 /* case for int is smallest absolute difference */ 590 /* case for int is smallest absolute difference */
591 int i1 = n.toInt(); 591 int i1 = n.toInt();
592 int i2 = o.toInt(); 592 int i2 = o.toInt();
593 int iv = toInt(); 593 int iv = toInt();
594 594
595 int diff1 = (i1 - iv); 595 int diff1 = (i1 - iv);
596 if (diff1 < 0) 596 if (diff1 < 0)
597 diff1 = -diff1; 597 diff1 = -diff1;
598 int diff2 = (i2 - iv); 598 int diff2 = (i2 - iv);
599 if (diff2 < 0) 599 if (diff2 < 0)
600 diff2 = -diff2; 600 diff2 = -diff2;
601 601
602 if (diff1 < diff2) 602 if (diff1 < diff2)
603 return TRUE; 603 return TRUE;
604 return FALSE; 604 return FALSE;
605 } 605 }
606 case Date: { 606 case Date: {
607 QDate i1 = n.toDate(); 607 QDate i1 = n.toDate();
608 QDate i2 = o.toDate(); 608 QDate i2 = o.toDate();
609 QDate iv = toDate(); 609 QDate iv = toDate();
610 610
611 /* definition of closer is the least difference in days */ 611 /* definition of closer is the least difference in days */
612 int diff1 = i1.daysTo(iv); 612 int diff1 = i1.daysTo(iv);
613 if (diff1 < 0) 613 if (diff1 < 0)
614 diff1 = -diff1; 614 diff1 = -diff1;
615 int diff2 = i2.daysTo(iv); 615 int diff2 = i2.daysTo(iv);
616 if (diff2 < 0) 616 if (diff2 < 0)
617 diff2 = -diff2; 617 diff2 = -diff2;
618 618
619 if (diff1 < diff2) 619 if (diff1 < diff2)
620 return TRUE; 620 return TRUE;
621 return FALSE; 621 return FALSE;
622 } 622 }
@@ -624,16 +624,16 @@ bool TVVariant::closer(TVVariant n, TVVariant o)
624 QTime i1 = n.toTime(); 624 QTime i1 = n.toTime();
625 QTime i2 = o.toTime(); 625 QTime i2 = o.toTime();
626 QTime iv = toTime(); 626 QTime iv = toTime();
627 627
628 /* definition of closer is the least difference in days */ 628 /* definition of closer is the least difference in days */
629 int diff1 = i1.msecsTo(iv); 629 int diff1 = i1.msecsTo(iv);
630 if (diff1 < 0) 630 if (diff1 < 0)
631 diff1 = -diff1; 631 diff1 = -diff1;
632 int diff2 = i2.msecsTo(iv); 632 int diff2 = i2.msecsTo(iv);
633 if (diff2 < 0) 633 if (diff2 < 0)
634 diff2 = -diff2; 634 diff2 = -diff2;
635 if (diff1 < diff2) 635 if (diff1 < diff2)
636 return TRUE; 636 return TRUE;
637 return FALSE; 637 return FALSE;
638 } 638 }
639 default: 639 default:
@@ -646,30 +646,30 @@ bool TVVariant::closer(TVVariant n, TVVariant o)
646 646
647/*! True if n is close to this */ 647/*! True if n is close to this */
648bool TVVariant::close(TVVariant n) 648bool TVVariant::close(TVVariant n)
649{ 649{
650 /* Nothing is close to an invalid, so nothing can be closer */ 650 /* Nothing is close to an invalid, so nothing can be closer */
651 if(type() == Invalid) 651 if(type() == Invalid)
652 return FALSE; 652 return FALSE;
653 653
654 /* can't be close if of different type */ 654 /* can't be close if of different type */
655 if(n.type() != type()) 655 if(n.type() != type())
656 return FALSE; 656 return FALSE;
657 657
658 switch(type()){ 658 switch(type()){
659 case String: { 659 case String: {
660 /* case for strings is close is a substring.. closer is 660 /* case for strings is close is a substring.. closer is
661 * earlier alphabetically */ 661 * earlier alphabetically */
662 QString qs1 = n.toString().lower(); 662 QString qs1 = n.toString().lower();
663 QString qsv = toString().lower(); 663 QString qsv = toString().lower();
664 664
665 if (!qs1.startsWith(qsv)) 665 if (!qs1.startsWith(qsv))
666 return FALSE; 666 return FALSE;
667 return TRUE; 667 return TRUE;
668 } 668 }
669 case Int: 669 case Int:
670 case Date: 670 case Date:
671 case Time: 671 case Time:
672 return TRUE; 672 return TRUE;
673 default: 673 default:
674 /* don't know how to do 'closer' on this type, hence never closer 674 /* don't know how to do 'closer' on this type, hence never closer
675 * or even close */ 675 * or even close */
@@ -678,29 +678,29 @@ bool TVVariant::close(TVVariant n)
678 return FALSE; 678 return FALSE;
679} 679}
680 680
681/*! 681/*!
682 \class Key 682 \class Key
683 \brief document me! 683 \brief document me!
684 684
685 document me! 685 document me!
686*/ 686*/
687 687
688Key::Key() : kname(), kexample(), kflags(0) { } 688Key::Key() : kname(), kexample(), kflags(0) { }
689 689
690Key::Key(QString name, TVVariant example, int flags) : 690Key::Key(QString name, TVVariant example, int flags) :
691 kname(name), kexample(example), kflags(flags) { } 691 kname(name), kexample(example), kflags(flags) { }
692 692
693Key::Key(const Key &other) 693Key::Key(const Key &other)
694{ 694{
695 kname = other.kname; 695 kname = other.kname;
696 kexample = other.kexample; 696 kexample = other.kexample;
697 kflags = other.kflags; 697 kflags = other.kflags;
698} 698}
699 699
700Key& Key::operator=(const Key& key) 700Key& Key::operator=(const Key& key)
701{ 701{
702 kname = key.kname; 702 kname = key.kname;
703 kexample = key.kexample; 703 kexample = key.kexample;
704 kflags = key.kflags; 704 kflags = key.kflags;
705 return *this; 705 return *this;
706} 706}
@@ -774,19 +774,19 @@ void Key::setNewFlag(bool v)
774 headings by DBStore. It stores the names and types of the keys 774 headings by DBStore. It stores the names and types of the keys
775*/ 775*/
776 776
777/*! 777/*!
778 Constructs a KeyList 778 Constructs a KeyList
779*/ 779*/
780KeyList::KeyList() : QIntDict<Key>(20) 780KeyList::KeyList() : QIntDict<Key>(20)
781{ 781{
782 setAutoDelete(TRUE); 782 setAutoDelete(TRUE);
783} 783}
784 784
785/* Should be deep copy, but isn't */ 785/* Should be deep copy, but isn't */
786KeyList::KeyList(const KeyList &k) : QIntDict<Key>(k) 786KeyList::KeyList(const KeyList &k) : QIntDict<Key>(k)
787{ 787{
788 KeyListIterator it(k); 788 KeyListIterator it(k);
789 while(it.current()) { 789 while(it.current()) {
790 replace(it.currentKey(), new Key(*it.current())); 790 replace(it.currentKey(), new Key(*it.current()));
791 ++it; 791 ++it;
792 } 792 }
@@ -819,13 +819,13 @@ bool KeyList::operator!=(const KeyList &other)
819 return FALSE; 819 return FALSE;
820} 820}
821 821
822/*! 822/*!
823 Returns the number of keys stored in the KeyList 823 Returns the number of keys stored in the KeyList
824*/ 824*/
825int KeyList::getNumFields() const 825int KeyList::getNumFields() const
826{ 826{
827 return count(); 827 return count();
828} 828}
829 829
830/*! 830/*!
831 Adds a new key to the KeyList 831 Adds a new key to the KeyList
@@ -844,67 +844,66 @@ int KeyList::addKey(QString name, TVVariant example)
844 844
845int KeyList::addKey(QString name, TVVariant::KeyType type) 845int KeyList::addKey(QString name, TVVariant::KeyType type)
846{ 846{
847 /* generate a valid type for the example? */ 847 /* generate a valid type for the example? */
848 TVVariant e = TVVariant("0"); 848 TVVariant e = TVVariant("0");
849 switch(type) { 849 switch(type) {
850 case TVVariant::String: 850 case TVVariant::String:
851 return addKey(name, TVVariant("<undefined>").asString()); 851 return addKey(name, TVVariant("<undefined>").asString());
852 break; 852 break;
853 case TVVariant::Date: 853 case TVVariant::Date:
854 return addKey(name, TVVariant(QDate::currentDate()).asDate()); 854 return addKey(name, TVVariant(QDate::currentDate()).asDate());
855 break; 855 break;
856 case TVVariant::Time: 856 case TVVariant::Time:
857 return addKey(name, TVVariant(QTime(0,0,0)).toTime()); 857 return addKey(name, TVVariant(QTime(0,0,0)).toTime());
858 break; 858 break;
859 case TVVariant::Int: 859 case TVVariant::Int:
860 return addKey(name, TVVariant(0).toInt()); 860 return addKey(name, TVVariant(0).toInt());
861 break; 861 break;
862 default: 862 default:
863 qWarning(QObject::tr("KeyList::addKey() Cannot make default " 863 owarn << "KeyList::addKey() Cannot make default value for type " << type << ", Key not added." << oendl;
864 "value for type %1, Key not added.").arg(type));
865 break; 864 break;
866 } 865 }
867 return -1; 866 return -1;
868} 867}
869 868
870void KeyList::setKeyFlags(int i, int flag) 869void KeyList::setKeyFlags(int i, int flag)
871{ 870{
872 if(find(i)) 871 if(find(i))
873 find(i)->setFlags(flag); 872 find(i)->setFlags(flag);
874} 873}
875 874
876int KeyList::getKeyFlags(int i) const 875int KeyList::getKeyFlags(int i) const
877{ 876{
878 if(find(i)) 877 if(find(i))
879 return find(i)->flags(); 878 return find(i)->flags();
880 return 0; 879 return 0;
881} 880}
882 881
883bool KeyList::checkNewFlag(int i) const 882bool KeyList::checkNewFlag(int i) const
884{ 883{
885 if (find(i)) 884 if (find(i))
886 return find(i)->newFlag(); 885 return find(i)->newFlag();
887 return false; 886 return false;
888} 887}
889 888
890void KeyList::setNewFlag(int i, bool f) 889void KeyList::setNewFlag(int i, bool f)
891{ 890{
892 if(!find(i)) 891 if(!find(i))
893 return; 892 return;
894 find(i)->setNewFlag(f); 893 find(i)->setNewFlag(f);
895} 894}
896 895
897bool KeyList::checkDeleteFlag(int i) const 896bool KeyList::checkDeleteFlag(int i) const
898{ 897{
899 if (find(i)) 898 if (find(i))
900 return find(i)->delFlag(); 899 return find(i)->delFlag();
901 return false; 900 return false;
902} 901}
903 902
904void KeyList::setDeleteFlag(int i, bool f) 903void KeyList::setDeleteFlag(int i, bool f)
905{ 904{
906 if(!find(i)) 905 if(!find(i))
907 return; 906 return;
908 find(i)->setDelFlag(f); 907 find(i)->setDelFlag(f);
909} 908}
910 909
@@ -915,13 +914,13 @@ QString KeyList::getKeyName(int i) const
915{ 914{
916 if (find (i)) 915 if (find (i))
917 return find(i)->name(); 916 return find(i)->name();
918 return QString(); 917 return QString();
919} 918}
920 919
921void KeyList::setKeyName(int i, const QString &n) 920void KeyList::setKeyName(int i, const QString &n)
922{ 921{
923 if(find(i)) 922 if(find(i))
924 find(i)->setName(n); 923 find(i)->setName(n);
925} 924}
926 925
927/*! 926/*!
@@ -931,13 +930,13 @@ TVVariant::KeyType KeyList::getKeyType(int i) const
931{ 930{
932 if(find(i)) 931 if(find(i))
933 return find(i)->type(); 932 return find(i)->type();
934 return TVVariant::Invalid; 933 return TVVariant::Invalid;
935} 934}
936 935
937void KeyList::setKeyType(int i, TVVariant::KeyType t) 936void KeyList::setKeyType(int i, TVVariant::KeyType t)
938{ 937{
939 if(!find(i)) 938 if(!find(i))
940 return; 939 return;
941 switch(t) { 940 switch(t) {
942 case TVVariant::String: 941 case TVVariant::String:
943 find(i)->setExample(TVVariant(QString("default"))); 942 find(i)->setExample(TVVariant(QString("default")));
@@ -1037,15 +1036,15 @@ QDataStream &operator>>( QDataStream &s, KeyList &k)
1037 1036
1038 This class holds the data of a row in a table. 1037 This class holds the data of a row in a table.
1039*/ 1038*/
1040 1039
1041 1040
1042/*! 1041/*!
1043 Constructs a DataElem. This function needs a container because the 1042 Constructs a DataElem. This function needs a container because the
1044 size, types of keys and primary key are all defined by the containing 1043 size, types of keys and primary key are all defined by the containing
1045 database 1044 database
1046*/ 1045*/
1047DataElem::DataElem(DBStore *c) : values(20) 1046DataElem::DataElem(DBStore *c) : values(20)
1048{ 1047{
1049 int size; 1048 int size;
1050 contained = c; 1049 contained = c;
1051 size = c->getNumFields(); 1050 size = c->getNumFields();
@@ -1083,13 +1082,13 @@ QDataStream &operator>>( QDataStream &s, DataElem &d)
1083 int size; 1082 int size;
1084 TVVariant t; 1083 TVVariant t;
1085 Q_UINT16 index = 0; 1084 Q_UINT16 index = 0;
1086 1085
1087 s >> size; /* redundent data but makes streaming easier */ 1086 s >> size; /* redundent data but makes streaming easier */
1088 if (size != d.getNumFields()) { 1087 if (size != d.getNumFields()) {
1089 owarn << "DataSize mis-match" << oendl; 1088 owarn << "DataSize mis-match" << oendl;
1090 return s; /* sanity check failed.. don't load */ 1089 return s; /* sanity check failed.. don't load */
1091 } 1090 }
1092 1091
1093 for(i = 0; i < size; i++) { 1092 for(i = 0; i < size; i++) {
1094 s >> index; 1093 s >> index;
1095 s >> t; 1094 s >> t;
@@ -1106,68 +1105,68 @@ int DataElem::getNumFields() const
1106 1105
1107KeyList DataElem::getKeys() const 1106KeyList DataElem::getKeys() const
1108{ 1107{
1109 return *(contained->getKeys()); 1108 return *(contained->getKeys());
1110} 1109}
1111 1110
1112/*! 1111/*!
1113 This function determines whether field index i of the element has been 1112 This function determines whether field index i of the element has been
1114 set yet. 1113 set yet.
1115 1114
1116 \return A boolean value that is TRUE if the specfied field of this 1115 \return A boolean value that is TRUE if the specfied field of this
1117 element has been set and FALSE if the field has not yet been set 1116 element has been set and FALSE if the field has not yet been set
1118*/ 1117*/
1119bool DataElem::hasValidValue(int i) const 1118bool DataElem::hasValidValue(int i) const
1120{ 1119{
1121 if(!values.find(i)) 1120 if(!values.find(i))
1122 return FALSE; 1121 return FALSE;
1123 if(!contained->getKeys()->validIndex(i)) 1122 if(!contained->getKeys()->validIndex(i))
1124 return FALSE; 1123 return FALSE;
1125 return values.find(i)->isValid(); 1124 return values.find(i)->isValid();
1126} 1125}
1127 1126
1128/*! 1127/*!
1129 This function determines whether field name qs of the element has been 1128 This function determines whether field name qs of the element has been
1130 set yet. 1129 set yet.
1131 1130
1132 \return A boolean value that is TRUE if the specfied field of this 1131 \return A boolean value that is TRUE if the specfied field of this
1133 element has been set and FALSE if the field has not yet been set 1132 element has been set and FALSE if the field has not yet been set
1134*/ 1133*/
1135bool DataElem::hasValidValue(QString qs) const 1134bool DataElem::hasValidValue(QString qs) const
1136{ 1135{
1137 int i = contained->getKeyIndex(qs); 1136 int i = contained->getKeyIndex(qs);
1138 return hasValidValue(i); 1137 return hasValidValue(i);
1139} 1138}
1140 1139
1141/*! returns the type of the field specified by index i */ 1140/*! returns the type of the field specified by index i */
1142TVVariant::KeyType DataElem::getFieldType(int i) const 1141TVVariant::KeyType DataElem::getFieldType(int i) const
1143{ 1142{
1144 return contained->getKeyType(i); 1143 return contained->getKeyType(i);
1145} 1144}
1146 1145
1147/*! returns the type of the field specified by name qs */ 1146/*! returns the type of the field specified by name qs */
1148TVVariant::KeyType DataElem::getFieldType(QString qs) const 1147TVVariant::KeyType DataElem::getFieldType(QString qs) const
1149{ 1148{
1150 int i = contained->getKeyIndex(qs); 1149 int i = contained->getKeyIndex(qs);
1151 return contained->getKeyType(i); 1150 return contained->getKeyType(i);
1152} 1151}
1153 1152
1154/*! 1153/*!
1155 returns a pointer to the data stored in field index i for this 1154 returns a pointer to the data stored in field index i for this
1156 data element, (value may not be valid) 1155 data element, (value may not be valid)
1157*/ 1156*/
1158TVVariant DataElem::getField(int i) const 1157TVVariant DataElem::getField(int i) const
1159{ 1158{
1160 if(hasValidValue(i)) 1159 if(hasValidValue(i))
1161 return TVVariant(*values.find(i)); 1160 return TVVariant(*values.find(i));
1162 return TVVariant(); 1161 return TVVariant();
1163} 1162}
1164 1163
1165/*! 1164/*!
1166 returns a pointer to the data stored in field name qs for this 1165 returns a pointer to the data stored in field name qs for this
1167 data element, (value may not be valid) 1166 data element, (value may not be valid)
1168*/ 1167*/
1169TVVariant DataElem::getField(QString qs) const 1168TVVariant DataElem::getField(QString qs) const
1170{ 1169{
1171 int i = contained->getKeyIndex(qs); 1170 int i = contained->getKeyIndex(qs);
1172 return getField(i); 1171 return getField(i);
1173} 1172}
@@ -1176,13 +1175,13 @@ TVVariant DataElem::getField(QString qs) const
1176 Sets the value of the elements field index i to the value represented in 1175 Sets the value of the elements field index i to the value represented in
1177 the QString q. 1176 the QString q.
1178 1177
1179 \param i index of the field to set 1178 \param i index of the field to set
1180 \param q a string that can be parsed to get the value to be set 1179 \param q a string that can be parsed to get the value to be set
1181*/ 1180*/
1182void DataElem::setField(int i, QString q) 1181void DataElem::setField(int i, QString q)
1183{ 1182{
1184 /* from the type of the field, parse q and store */ 1183 /* from the type of the field, parse q and store */
1185 TVVariant::KeyType kt = contained->getKeyType(i); 1184 TVVariant::KeyType kt = contained->getKeyType(i);
1186 1185
1187 TVVariant t = TVVariant(q); 1186 TVVariant t = TVVariant(q);
1188 1187
@@ -1205,26 +1204,24 @@ void DataElem::setField(int i, QString q)
1205 case TVVariant::Time: { 1204 case TVVariant::Time: {
1206 t.asTime(); 1205 t.asTime();
1207 setField(i, t); 1206 setField(i, t);
1208 return; 1207 return;
1209 } 1208 }
1210 default: 1209 default:
1211 qWarning( 1210 owarn << "DataElem::setField(" << i << ", " << q << ") No valid type found" << oendl;
1212 QObject::tr("DataElem::setField(%1, %2) No valid type found").arg(i).arg(q)
1213 );
1214 } 1211 }
1215} 1212}
1216 1213
1217/*! 1214/*!
1218 Sets the value of the elements field index i to the value at the pointer 1215 Sets the value of the elements field index i to the value at the pointer
1219 value. 1216 value.
1220 1217
1221 \param i index of the field to set 1218 \param i index of the field to set
1222 \param value a pointer to the (already allocated) value to set 1219 \param value a pointer to the (already allocated) value to set
1223*/ 1220*/
1224void DataElem::setField(int i, TVVariant value) 1221void DataElem::setField(int i, TVVariant value)
1225{ 1222{
1226 if (value.isValid()) { 1223 if (value.isValid()) {
1227 values.remove(i); 1224 values.remove(i);
1228 values.replace(i, new TVVariant(value)); 1225 values.replace(i, new TVVariant(value));
1229 } 1226 }
1230} 1227}
@@ -1233,25 +1230,25 @@ void DataElem::setField(int i, TVVariant value)
1233 Sets the value of the elements field name qs to the value represented in 1230 Sets the value of the elements field name qs to the value represented in
1234 the QString q. 1231 the QString q.
1235 1232
1236 \param qs name of the field to set 1233 \param qs name of the field to set
1237 \param q a string that can be parsed to get the value to be set 1234 \param q a string that can be parsed to get the value to be set
1238*/ 1235*/
1239void DataElem::setField(QString qs, QString q) 1236void DataElem::setField(QString qs, QString q)
1240{ 1237{
1241 /* from the type of the field, parse q and store */ 1238 /* from the type of the field, parse q and store */
1242 int i = contained->getKeyIndex(qs); 1239 int i = contained->getKeyIndex(qs);
1243 setField(i, qs); 1240 setField(i, qs);
1244} 1241}
1245 1242
1246/*! 1243/*!
1247 Sets the value of the elements field name qs to the value at the pointer 1244 Sets the value of the elements field name qs to the value at the pointer
1248 value. 1245 value.
1249 1246
1250 \param qs name of the field to set 1247 \param qs name of the field to set
1251 \param value a pointer to the (already allocated) value to set 1248 \param value a pointer to the (already allocated) value to set
1252*/ 1249*/
1253void DataElem::setField(QString qs, TVVariant value) 1250void DataElem::setField(QString qs, TVVariant value)
1254{ 1251{
1255 int i = contained->getKeyIndex(qs); 1252 int i = contained->getKeyIndex(qs);
1256 setField(i, value); 1253 setField(i, value);
1257} 1254}
@@ -1264,13 +1261,13 @@ void DataElem::unsetField(QString qs)
1264{ 1261{
1265 int i = contained->getKeyIndex(qs); 1262 int i = contained->getKeyIndex(qs);
1266 unsetField(i); 1263 unsetField(i);
1267} 1264}
1268 1265
1269/*! 1266/*!
1270 Converts the data element to a Rich Text QString 1267 Converts the data element to a Rich Text QString
1271*/ 1268*/
1272QString DataElem::toQString() const 1269QString DataElem::toQString() const
1273{ 1270{
1274 /* lets make an attempt at this function */ 1271 /* lets make an attempt at this function */
1275 int i; 1272 int i;
1276 QString scratch = ""; 1273 QString scratch = "";
@@ -1287,21 +1284,21 @@ QString DataElem::toQString() const
1287 ++it; 1284 ++it;
1288 } 1285 }
1289 return scratch; 1286 return scratch;
1290} 1287}
1291 1288
1292/*! formats individual fields to strings so can be displayed */ 1289/*! formats individual fields to strings so can be displayed */
1293QString DataElem::toQString(int i) const 1290QString DataElem::toQString(int i) const
1294{ 1291{
1295 if(hasValidValue(i)) { 1292 if(hasValidValue(i)) {
1296 return getField(i).toString(); 1293 return getField(i).toString();
1297 } 1294 }
1298 return ""; 1295 return "";
1299} 1296}
1300/*! formats individual fields to strings so can be sorted by QListView */ 1297/*! formats individual fields to strings so can be sorted by QListView */
1301QString DataElem::toSortableQString(int i) const 1298QString DataElem::toSortableQString(int i) const
1302{ 1299{
1303 QString scratch = ""; 1300 QString scratch = "";
1304 if(hasValidValue(i)) { 1301 if(hasValidValue(i)) {
1305 switch (contained->getKeyType(i)) { 1302 switch (contained->getKeyType(i)) {
1306 case TVVariant::String: { 1303 case TVVariant::String: {
1307 scratch += getField(i).toString(); 1304 scratch += getField(i).toString();
@@ -1310,19 +1307,19 @@ QString DataElem::toSortableQString(int i) const
1310 case TVVariant::Int: { 1307 case TVVariant::Int: {
1311 scratch.sprintf("%08d", getField(i).toInt()); 1308 scratch.sprintf("%08d", getField(i).toInt());
1312 break; 1309 break;
1313 } 1310 }
1314 case TVVariant::Date: { 1311 case TVVariant::Date: {
1315 static QDate epochD(1800, 1, 1); 1312 static QDate epochD(1800, 1, 1);
1316 scratch.sprintf("%08d", 1313 scratch.sprintf("%08d",
1317 epochD.daysTo(getField(i).toDate())); 1314 epochD.daysTo(getField(i).toDate()));
1318 break; 1315 break;
1319 } 1316 }
1320 case TVVariant::Time: { 1317 case TVVariant::Time: {
1321 static QTime epochT(0, 0, 0); 1318 static QTime epochT(0, 0, 0);
1322 scratch.sprintf("%08d", 1319 scratch.sprintf("%08d",
1323 epochT.msecsTo(getField(i).toTime())); 1320 epochT.msecsTo(getField(i).toTime()));
1324 break; 1321 break;
1325 } 1322 }
1326 default: 1323 default:
1327 scratch += "Unknown type"; 1324 scratch += "Unknown type";
1328 break; 1325 break;
@@ -1377,18 +1374,18 @@ bool DataElem::contains(int i, TVVariant v) const
1377 if (qs1.contains(qs2) > 0) return TRUE; 1374 if (qs1.contains(qs2) > 0) return TRUE;
1378 break; 1375 break;
1379 } 1376 }
1380 /* meaningless for ints */ 1377 /* meaningless for ints */
1381 /* meaningless for time */ 1378 /* meaningless for time */
1382 /* meaningless for dates */ 1379 /* meaningless for dates */
1383 case TVVariant::Int: 1380 case TVVariant::Int:
1384 case TVVariant::Time: 1381 case TVVariant::Time:
1385 case TVVariant::Date: 1382 case TVVariant::Date:
1386 break; 1383 break;
1387 default: 1384 default:
1388 owarn << "Tried to compare unknown data type" << oendl; 1385 owarn << "Tried to compare unknown data type" << oendl;
1389 } 1386 }
1390 return FALSE; 1387 return FALSE;
1391} 1388}
1392 1389
1393bool DataElem::startsWith(int i, TVVariant v) const 1390bool DataElem::startsWith(int i, TVVariant v) const
1394{ 1391{
@@ -1403,18 +1400,18 @@ bool DataElem::startsWith(int i, TVVariant v) const
1403 QString qs2 = v.toString().lower(); 1400 QString qs2 = v.toString().lower();
1404 return qs1.startsWith(qs2); 1401 return qs1.startsWith(qs2);
1405 } 1402 }
1406 /* meaningless for ints */ 1403 /* meaningless for ints */
1407 /* meaningless for time */ 1404 /* meaningless for time */
1408 /* meaningless for dates */ 1405 /* meaningless for dates */
1409 case TVVariant::Int: 1406 case TVVariant::Int:
1410 case TVVariant::Time: 1407 case TVVariant::Time:
1411 case TVVariant::Date: 1408 case TVVariant::Date:
1412 return FALSE; 1409 return FALSE;
1413 default: 1410 default:
1414 owarn << "Tried to compare unknown data type" << oendl; 1411 owarn << "Tried to compare unknown data type" << oendl;
1415 } 1412 }
1416 return FALSE; 1413 return FALSE;
1417} 1414}
1418 1415
1419bool DataElem::endsWith(int i, TVVariant v) const 1416bool DataElem::endsWith(int i, TVVariant v) const
1420{ 1417{
@@ -1429,50 +1426,50 @@ bool DataElem::endsWith(int i, TVVariant v) const
1429 QString qs2 = v.toString().lower(); 1426 QString qs2 = v.toString().lower();
1430 return qs1.startsWith(qs2); 1427 return qs1.startsWith(qs2);
1431 } 1428 }
1432 /* meaningless for ints */ 1429 /* meaningless for ints */
1433 /* meaningless for time */ 1430 /* meaningless for time */
1434 /* meaningless for dates */ 1431 /* meaningless for dates */
1435 case TVVariant::Int: 1432 case TVVariant::Int:
1436 case TVVariant::Time: 1433 case TVVariant::Time:
1437 case TVVariant::Date: 1434 case TVVariant::Date:
1438 return FALSE; 1435 return FALSE;
1439 default: 1436 default:
1440 owarn << "Tried to compare unknown data type" << oendl; 1437 owarn << "Tried to compare unknown data type" << oendl;
1441 } 1438 }
1442 return FALSE; 1439 return FALSE;
1443} 1440}
1444 1441
1445/*! 1442/*!
1446 Determins which of the first to parameters are closer to the third, target 1443 Determins which of the first to parameters are closer to the third, target
1447 parameter. 1444 parameter.
1448 1445
1449 \return 1446 \return
1450 <UL> 1447 <UL>
1451 <LI>TRUE if the first element is a closer match to the target than the 1448 <LI>TRUE if the first element is a closer match to the target than the
1452 second element</LI> 1449 second element</LI>
1453 <LI>FALSE if the first element is not a closer match to the target than 1450 <LI>FALSE if the first element is not a closer match to the target than
1454 the second element</LI> 1451 the second element</LI>
1455 </UL> 1452 </UL>
1456*/ 1453*/
1457bool DataElem::closer(DataElem*d1, DataElem *d2, TVVariant target, int column) 1454bool DataElem::closer(DataElem*d1, DataElem *d2, TVVariant target, int column)
1458{ 1455{
1459 int type; 1456 int type;
1460 1457
1461 if(!d1) return FALSE; 1458 if(!d1) return FALSE;
1462 1459
1463 if (!d1->hasValidValue(column)) return FALSE; 1460 if (!d1->hasValidValue(column)) return FALSE;
1464 1461
1465 if(!target.isValid()) return FALSE; 1462 if(!target.isValid()) return FALSE;
1466 1463
1467 type = d1->getField(column).type(); 1464 type = d1->getField(column).type();
1468 1465
1469 if(d2) { 1466 if(d2) {
1470 if (type != d2->getField(column).type()) { 1467 if (type != d2->getField(column).type()) {
1471 /* can't do compare */ 1468 /* can't do compare */
1472 owarn << "Tried to compare two incompatable types" << oendl; 1469 owarn << "Tried to compare two incompatable types" << oendl;
1473 return FALSE; 1470 return FALSE;
1474 } 1471 }
1475 return target.closer(d1->getField(column), d2->getField(column)); 1472 return target.closer(d1->getField(column), d2->getField(column));
1476 } 1473 }
1477 return target.close(d1->getField(column)); 1474 return target.close(d1->getField(column));
1478} 1475}