00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "tvkeyedit.h"
00021 #include <qtoolbutton.h>
00022 #include <qlineedit.h>
00023 #include <qcombobox.h>
00024 #include <qlistview.h>
00025 #include <qmessagebox.h>
00026 #include <stdlib.h>
00027 #include <qpushbutton.h>
00028
00029 #include <qpe/qpeapplication.h>
00030
00031
00032
00033
00034 class TVKEListViewItem : public QListViewItem
00035 {
00036 public:
00037 TVKEListViewItem(QString n, TVVariant::KeyType kt, int p, QListView *parent) :
00038 QListViewItem(parent)
00039 {
00040 name = n;
00041 keyType = kt;
00042 position = p;
00043 }
00044
00045 QString text(int i) const
00046 {
00047 if(i) {
00048 return TVVariant::typeToName(keyType);
00049 }
00050 return name;
00051 }
00052
00053
00054 QString key(int, bool) const
00055 {
00056 return QString().sprintf("%08d", position);
00057 }
00058
00059 void setText(int i, const QString &)
00060 {
00061 ;
00062 }
00063
00064 QString getName() const
00065 {
00066 return name;
00067 }
00068
00069 void setName(QString n)
00070 {
00071 name = n;
00072 repaint();
00073 }
00074
00075 TVVariant::KeyType getKeyType() const
00076 {
00077 return keyType;
00078 }
00079
00080 void setKeyType(TVVariant::KeyType k)
00081 {
00082 keyType = k;
00083 repaint();
00084 }
00085
00086 inline int getPos() const
00087 {
00088 return position;
00089 }
00090
00091 private:
00092 QString name;
00093 TVVariant::KeyType keyType;
00094 int position;
00095 };
00096
00097 TVKeyEdit::TVKeyEdit(TableState *t, QWidget* parent, const char *name,
00098 WFlags fl) : TVKeyEdit_gen(parent, name, true, fl)
00099 {
00100 int i;
00101 ts = t;
00102
00103 if(!ts) return;
00104 if(!ts->kRep) return;
00105
00106 working_state = *ts->kRep;
00107
00108 i = 1;
00109 keyTypeEdit->insertItem(TVVariant::typeToName((TVVariant::KeyType)i));
00110 i++;
00111 keyTypeEdit->insertItem(TVVariant::typeToName((TVVariant::KeyType)i));
00112 i++;
00113 keyTypeEdit->insertItem(TVVariant::typeToName((TVVariant::KeyType)i));
00114 i++;
00115 keyTypeEdit->insertItem(TVVariant::typeToName((TVVariant::KeyType)i));
00116
00117 KeyListIterator it(*ts->kRep);
00118 while(it.current()) {
00119 if(t->kRep->validIndex(it.currentKey())) {
00120 new TVKEListViewItem(it.current()->name(),
00121 it.current()->type(),
00122 it.currentKey(),
00123 display);
00124 }
00125 ++it;
00126 }
00127 num_keys = ts->kRep->getNumFields();
00128 if(display->childCount() > 0) {
00129 display->setCurrentItem(display->firstChild());
00130 setTerm(display->currentItem());
00131 } else {
00132 deleteKeyButton->setEnabled(FALSE);
00133 clearKeysButton->setEnabled(FALSE);
00134 keyNameEdit->setEnabled(FALSE);
00135 keyTypeEdit->setEnabled(FALSE);
00136 }
00137
00138 display->setSorting(0);
00139 #ifdef Q_WS_QWS
00140 QPEApplication::showDialog( this );
00141 #endif
00142 }
00143
00147 TVKeyEdit::~TVKeyEdit()
00148 {
00149 }
00150
00151
00152 void TVKeyEdit::newTerm()
00153 {
00154
00155 int i;
00156
00157 i = working_state.addKey("<New Key>", TVVariant::String);
00158
00159 TVKEListViewItem *nItem = new TVKEListViewItem("<New Key>",
00160 TVVariant::String,
00161 i,
00162 display);
00163 display->setCurrentItem(nItem);
00164 setTerm(nItem);
00165
00166 num_keys++;
00167 if(display->childCount() == 1) {
00168 deleteKeyButton->setEnabled(TRUE);
00169 clearKeysButton->setEnabled(TRUE);
00170 keyNameEdit->setEnabled(TRUE);
00171 keyTypeEdit->setEnabled(TRUE);
00172 }
00173 }
00174
00175 void TVKeyEdit::updateTerm(const QString &newName)
00176 {
00177
00178
00179 TVKEListViewItem *i = (TVKEListViewItem *)display->currentItem();
00180 if(i) {
00181 i->setName(newName);
00182 working_state.setKeyName(i->getPos(), newName);
00183 }
00184 }
00185
00186 void TVKeyEdit::updateTerm(int t)
00187 {
00188
00189 t++;
00190 TVKEListViewItem *i = (TVKEListViewItem *)display->currentItem();
00191 if (i) {
00192 i->setKeyType((TVVariant::KeyType)t);
00193 working_state.setKeyType(i->getPos(), (TVVariant::KeyType)t);
00194 }
00195 }
00196
00197
00198
00199
00200
00201
00202
00203 void TVKeyEdit::deleteTerm()
00204 {
00205 TVKEListViewItem *i = (TVKEListViewItem *)display->currentItem();
00206 if (i) {
00207 working_state.setDeleteFlag(i->getPos(), TRUE);
00208 delete i;
00209 }
00210 if(!display->childCount()) {
00211
00212 deleteKeyButton->setEnabled(FALSE);
00213 clearKeysButton->setEnabled(FALSE);
00214 keyNameEdit->setEnabled(FALSE);
00215 keyTypeEdit->setEnabled(FALSE);
00216 }
00217 }
00218
00219
00220 void TVKeyEdit::clearTerms()
00221 {
00222
00223 if (QMessageBox::warning(this, "Delete all keys",
00224 "Are you sure you want to\ndelete all the keys?",
00225 "Yes", "No") == 0)
00226 {
00227 while(display->currentItem())
00228 deleteTerm();
00229 }
00230 }
00231
00232 void TVKeyEdit::setTerm(QListViewItem *target)
00233 {
00234
00235 keyNameEdit->setText(((TVKEListViewItem *)target)->getName());
00236 int t = (int)(((TVKEListViewItem *)target)->getKeyType());
00237 t--;
00238 keyTypeEdit->setCurrentItem(t);
00239 }
00240
00241 KeyList* TVKeyEdit::openEditKeysDialog(TableState *t, QWidget *parent = 0)
00242 {
00243 if(!t)
00244 return 0;
00245 if(!t->kRep)
00246 return 0;
00247
00248 TVKeyEdit *dlg = new TVKeyEdit(t, parent);
00249
00250 if ((dlg->exec() == QDialog::Accepted) &&
00251 (dlg->working_state != *t->kRep))
00252 {
00253 return (new KeyList(dlg->working_state));
00254 }
00255 return 0;
00256 }