summaryrefslogtreecommitdiff
path: root/noncore/apps/checkbook/listedit.cpp
blob: e40377b589b175decb644f53efc3c5ea4477174f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
                     This file is part of the OPIE Project
               =.
      .=l.            Copyright (c)  2002 Dan Williams <drw@handhelds.org>
     .>+-=
_;:,   .>  :=|.         This file is free software; you can
.> <`_,  > .  <=          redistribute it and/or modify it under
:`=1 )Y*s>-.--  :           the terms of the GNU General Public
.="- .-=="i,   .._         License as published by the Free Software
- .  .-<_>   .<>         Foundation; either version 2 of the License,
  ._= =}    :          or (at your option) any later version.
  .%`+i>    _;_.
  .i_,=:_.   -<s.       This file is distributed in the hope that
  + . -:.    =       it will be useful, but WITHOUT ANY WARRANTY;
  : ..  .:,   . . .    without even the implied warranty of
  =_    +   =;=|`    MERCHANTABILITY or FITNESS FOR A
 _.=:.    :  :=>`:     PARTICULAR PURPOSE. See the GNU General
..}^=.=    =    ;      Public License for more details.
++=  -.   .`   .:
:   = ...= . :.=-        You should have received a copy of the GNU
-.  .:....=;==+<;          General Public License along with this file;
 -_. . .  )=. =           see the file COPYING. If not, write to the
  --    :-=`           Free Software Foundation, Inc.,
                             59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.

*/

#include "listedit.h"

/* OPIE */
#include <opie2/odebug.h>
#include <opie2/oresource.h>

#include <qpe/applnk.h>

using namespace Opie::Core;

/* QT */
#include <qlayout.h>
#include <qlineedit.h>
#include <qlistview.h>
#include <qwidgetstack.h>
#include <qcombobox.h>
#include <qpushbutton.h>

// --- ListEdit ---------------------------------------------------------------
ListEdit::ListEdit( QWidget *parent, const char *sName )
    : QWidget(parent, sName), TableDef(sName)
{
    // get font height
    int fh = fontMetrics().height();

    // create layout
    QGridLayout *layout=new QGridLayout(this);
    layout->setSpacing( 2 );
	layout->setMargin( 4 );

    // type table
    _typeTable = new QListView( this );
    ColumnDef *def=first();
    while( def ) {
        _typeTable->addColumn( def->getName() );
        def=next();
    }
    connect( _typeTable, SIGNAL( clicked(QListViewItem*,const QPoint&,int) ), this, SLOT( slotClick(QListViewItem*,const QPoint&,int) ) );
    layout->addMultiCellWidget(_typeTable, 0,4,0,4);
    _currentItem=NULL;

    // edit field
    _stack=new QWidgetStack( this );
    _stack->setMaximumHeight(fh+5);
    layout->addMultiCellWidget(_stack, 5,5,0,2);
    _typeEdit = new QLineEdit( _stack );
    _stack->raiseWidget(_typeEdit );
    connect( _typeEdit, SIGNAL( textChanged(const QString&) ), this, SLOT( slotEditChanged(const QString&) ) );

    // combo box
    _box=new QComboBox( _stack );
    connect( _box, SIGNAL( activated(const QString&) ), this, SLOT( slotActivated(const QString&) ) );


    // add button
    QPushButton *btn = new QPushButton( Opie::Core::OResource::loadPixmap( "new", Opie::Core::OResource::SmallIcon ),
                                        tr( "Add" ), this );
    btn->setFixedHeight( AppLnk::smallIconSize()+4 );
	connect( btn, SIGNAL( clicked() ), this, SLOT( slotAdd() ) );
	layout->addWidget( btn, 5, 3 );

    // delete button
    btn = new QPushButton( Opie::Core::OResource::loadPixmap( "trash", Opie::Core::OResource::SmallIcon ), tr( "Delete" ), this );
    btn->setFixedHeight( AppLnk::smallIconSize()+4 );
    connect( btn, SIGNAL( clicked() ), this, SLOT( slotDel() ) );
	layout->addWidget( btn, 5, 4 );
}

// --- ~ListEdit --------------------------------------------------------------
ListEdit::~ListEdit()
{
}


// --- slotEditTypeChanged ----------------------------------------------------
void ListEdit::slotEditChanged(const QString &str)
{
    if( !_currentItem || _currentColumn<0 ) return;
    _currentItem->setText(_currentColumn, str);
}

// --- slotAddType ------------------------------------------------------------
void ListEdit::slotAdd()
{
    // construct new row
    QString args[8];
    ColumnDef *pCol=this->first();
    int i=0;
    while( pCol && i<8 ) {
        args[i++]=pCol->getNewValue();
        pCol=this->next();
    }
    _currentItem=new QListViewItem(_typeTable, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7] );

    // fix uniques
    fixTypes();

    // display col 0 of new value
    QPoint pnt;
    slotClick(_currentItem, pnt, 0);
    _typeTable->setSelected( _currentItem, true );

    // make it selected
    _typeEdit->setCursorPosition(0);
    _typeEdit->setSelection(0, _typeEdit->text().length() );
}

// --- slotDel -------------------------------------------------------------
void ListEdit::slotDel()
{
    if( !_currentItem ) return;
    delete _currentItem;
    _currentItem=NULL;
    _typeEdit->setText("");
    _stack->raiseWidget(_typeEdit);
}


// --- fixTypes ----------------------------------------------------------------
// Makes sure all entries have a unique name and empty entries are replaced
// by a generic string. The first version performs the operation on a particular
// column, whereas the 2nd does it for all unique columns.
class ColMap {
    public:
        ColMap(QString sValue, QListViewItem *pEntry) {
            _sValue=sValue;
            _pEntry=pEntry;
        }
        QString &getValue() { return(_sValue); }
        QListViewItem *getItem() { return(_pEntry); }

    protected:
        QString _sValue;
        QListViewItem *_pEntry;
};

class ColList : public QList<QString>
{
    public:
        ColList() : QList<QString>() { }

    protected:
        int compareItems(QCollection::Item, QCollection::Item);
};

int ColList::compareItems(QCollection::Item i1, QCollection::Item i2) {
    return( ((QString *)i1)->compare(*(QString *)i2) );
}

void ListEdit::fixTypes(int iColumn)
{
    // get column def
    ColumnDef *pDef=this->at(iColumn);

    // create map of entries
    if( !_typeTable->childCount() ) return;
    ColMap **colMap=new ColMap *[_typeTable->childCount()];
    QListViewItem *cur=_typeTable->firstChild();
    ColList lst;
    for(int i=0; i<_typeTable->childCount(); i++) {
        colMap[i]=new ColMap(cur->text(iColumn), cur);
        lst.append( &(colMap[i]->getValue()) );
        cur=cur->nextSibling();
    }

    // fix empty entries
    int i=0;
    for(QString *ptr=lst.first(); ptr; ptr=lst.next()) {
        *ptr=ptr->stripWhiteSpace();
        if( ptr->isEmpty() ) {
            i++;
            if( i==1 ) *ptr=pDef->getNewValue();
            else ptr->sprintf("%s %d", (const char *)pDef->getNewValue(), i);
        }
    }

    // fix dups
    lst.sort();
    QString repl;
    for(uint iCur=0; iCur<lst.count()-1; iCur++) {
        QString *current=lst.at(iCur);
        for(uint iNext=iCur+1; iNext<lst.count(); iNext++ ) {
            if( *current!=*lst.at(iNext) ) continue;
            for(int i=2; ; i++) {
                repl.sprintf("%s %d", (const char *)*current, i);
                bool bDup=false;
                uint iChk=iNext+1;
                while( iChk<lst.count() ) {
                    QString *chk=lst.at(iChk);
                    if( !chk->startsWith(*current) ) break;
                    if( *chk==repl ) {
                        bDup=true;
                        break;
                    }
                    iChk++;
                }
                if( !bDup ) {
                    *lst.at(iNext)=repl;
                    break;
                }
            }
        }
    }
    lst.sort();

    // copy back clean up col map
    for(int i=0; i<_typeTable->childCount(); i++) {
        colMap[i]->getItem()->setText(iColumn, colMap[i]->getValue());
        delete colMap[i];
    }
    delete colMap;
}

void ListEdit::fixTypes()
{
    int i;
    ColumnDef *pDef;
    for(pDef=this->first(), i=0; pDef; pDef=this->next(), i++) {
        if( pDef->hasFlag(ColumnDef::typeUnique) )
            fixTypes(i);
    }
    _typeTable->sort();
}


// --- storeInList ------------------------------------------------------------
void ListEdit::storeInList(QStringList &lst)
{
    // delete old content
    lst.clear();

    // add new one
    fixTypes();
    QListViewItem *itm=_typeTable->firstChild();
    while( itm ) {
        int i=0;
        QString sAdd;
        ColumnDef *pDef;
        for(pDef=this->first(), i=0; pDef; pDef=this->next(), i++) {
            if( i>=1 ) sAdd+=";";
            sAdd += itm->text(i);
        }
        lst.append( sAdd );
        itm=itm->nextSibling();
    }
}


// --- slotClicked ------------------------------------------------------------
void ListEdit::slotClick(QListViewItem *itm, const QPoint &pnt, int col)
{
    (void)pnt; // get rid of unused warning;

    // save values
    _currentItem=itm;
    _currentColumn=col;
    if( itm==NULL ) {
        _typeEdit->setText("");
        _stack->raiseWidget(_typeEdit);
        return;
    }

    // display value
    if( _currentColumn<0 ) _currentColumn=0;
    ColumnDef *pDef=this->at(_currentColumn);
    if( pDef->isType(ColumnDef::typeString) ) {
        _typeEdit->setText( _currentItem->text(_currentColumn) );
        _stack->raiseWidget(_typeEdit);
    } else if( pDef->isType(ColumnDef::typeList) ){
        _box->clear();
        _box->insertStringList( pDef->getValueList() );
        QStringList::Iterator itr;
        int i=0;
        for(itr=pDef->getValueList().begin(); itr!=pDef->getValueList().end(); itr++) {
            if( (*itr)==_currentItem->text(_currentColumn) ) {
                _box->setCurrentItem(i);
                i=-1;
                break;
            }
            i++;
        }
        if( i>=0 ) {
            _box->insertItem( _currentItem->text(_currentColumn) );
            _box->setCurrentItem(i);
        }
        _stack->raiseWidget(_box);
    } else {
        odebug << "Unsupported column type for column " << (const char *)pDef->getName() << "" << oendl; 
        _typeEdit->setText("");
        _stack->raiseWidget(_typeEdit);
    }
}


// --- addColumnDef -----------------------------------------------------------
void ListEdit::addColumnDef(ColumnDef *pDef)
{
    _typeTable->addColumn( pDef->getName() );
    _vColumns.append(pDef);
}

// --- addData ----------------------------------------------------------------
void ListEdit::addData(QStringList &lst)
{
    // run through list
    QStringList::Iterator itr;
    for(itr=lst.begin(); itr!=lst.end(); itr++) {
        QStringList split=QStringList::split(";", *itr, true);
        QStringList::Iterator entry;
        QString args[8];
        int i=0;
        for(entry=split.begin(); entry!=split.end() && i<8; entry++, i++) {
            args[i]= (*entry);
        }
        while(i<8) {
            args[i++]="";
        }
        new QListViewItem(_typeTable, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
    }
}

// --- slotActivated ----------------------------------------------------------
void ListEdit::slotActivated(const QString &str)
{
    if( _currentItem==NULL || _currentColumn<0 ) return;
    _currentItem->setText(_currentColumn, str);
}