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
|
/***************************************************************************
** xEditList.cpp $Revision: 1.2 $ - $Name: V2-18 $
** QListBox w/ Edit field
**
** Copyright (C) 1996 Joseph Croft <jcroft@unicomp.net>
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Library General Public
** License as published by the Free Software Foundation; either
** version 2 of the License, or (at your option) any later version.
**
** This library 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
** Library General Public License for more details.
**
** You should have received a copy of the GNU Library General Public
** License along with this library; if not, write to the Free
** Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
***************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <qglobal.h>
#include <qkeycode.h>
#include "xEditList.h"
static int dbg = 0;
xEditList::xEditList(xWidgetResInfo *pPRes, QWidget *pParent,
const char *pName,
int cols, int rows, bool _unique) :
xFrame((wdtRes = new xWidgetResInfo(pPRes, QString(""),
QString(""))),
pParent, pName)
{
cols++;
rows++;
setDefPallet(this, wdtRes);
setDefFont(this, wdtRes);
unique = _unique;
if (dbg) fprintf(stdout, "xEditList::xEditList():Enter\n");
if (dbg) fflush(stdout);
if ((pList = new xListBox(wdtRes, this)) != NULL)
{
QFontMetrics fm = pList->QWidget::fontMetrics();
// pList->QWidget::setFont(QFont("helvetica", 12));
pList->setScrollBar(TRUE);
pList->QWidget::resize(fm.maxWidth() * 30, (fm.lineSpacing() * 4));
if (dbg) fprintf(stdout, "xEditList::xEditList():list.w = %d, .h = %d\n",
pList->size().width(), pList->size().height());
if (dbg) fflush(stdout);
connect(pList, SIGNAL(selected(int)), this, SLOT(recvdSelect(int)));
connect(pList, SIGNAL(highlighted(int)), this, SLOT(recvdHighlighted(int)));
}
if ((pEdit = new xLineEdit(wdtRes, this)) != NULL)
{
QFontMetrics fm = pEdit->QWidget::fontMetrics();
if (dbg) fprintf(stdout, "xEditList::xEditList():edit.w = %d, .h = %d\n",
pEdit->size().width(), pEdit->size().height());
// pEdit->QWidget::setFont(QFont("helvetica", 12));
// pEdit->QWidget::resize(fm.maxWidth() * 30, fm.lineSpacing() + 7);
pEdit->QWidget::resize(fm.maxWidth() * 30, pEdit->height());
if (dbg) fprintf(stdout, "xEditList::xEditList():New edit.w = %d, .h = %d\n",
pEdit->size().width(), pEdit->size().height());
pEdit->setLabel(" ");
pEdit->setMargins(0,0);
setMargins(0,0);
if (dbg) fprintf(stdout, "xEditList::xEditList():edit.w = %d, .h = %d\n",
pEdit->size().width(), pEdit->size().height());
if (dbg) fflush(stdout);
connect(pEdit, SIGNAL(returnPressed()), this, SLOT(recvdReturn()));
}
addWidget(pEdit);
addWidget(pList);
setWidgetSpacing(0);
fitFrame();
if (dbg) fprintf(stdout, "xEditList::xEditList():edit.w = %d, .h = %d\n",
pEdit->size().width(), pEdit->size().height());
if (dbg) fprintf(stdout, "xEditList::xEditList():list.w = %d, .h = %d\n",
pList->size().width(), pList->size().height());
if (dbg) fflush(stdout);
}
xEditList::~xEditList()
{
if (pList)
delete pList;
if (pEdit)
delete pEdit;
}
void xEditList::insertItemsSort(const char *pItems, char delim)
{
QString pStr;
int x;
if (dbg) fprintf(stdout, "xEditList::insertItemsSort(%s, %d)\n",
pItems, delim);
x = 0;
for (;;)
{
if (!x && isspace(*pItems))
{
pItems++;
continue;
}
else
{
x = 1;
if (*pItems == delim || *pItems == '\0')
{
if (dbg) fprintf(stdout, "xEditList::insertItemsSort():Testing for Nick |%s|\n", (const char *)pStr);
if (!unique || getIndex((const char *)pStr) < 0)
{
if (dbg) fprintf(stdout, "xEditList::insertItemsSort():Adding Nick |%s|\n", (const char *)pStr);
pList->inSort((const char *)pStr);
}
x = 0;
pStr = "";
if (!(*pItems))
break;
}
else
pStr += *(pItems++);
}
}
if (dbg) fprintf(stdout, "xEditList::insertItemsSort():Exit\n");
}
void xEditList::insertItems(const char *pItems, char delim, int index)
{
QString pStr;
int x;
if (dbg) fprintf(stdout, "xEditList::insertItems(%s, %d, %d)\n",
pItems, delim, index);
if (dbg) fflush(stdout);
x = 0;
pStr = "";
for (;;)
{
if (!x && isspace(*pItems))
{
pItems++;
continue;
}
else
{
x = 1;
if ((delim == ' ' && isspace(*pItems)) || *pItems == delim || *pItems == '\0')
{
if (dbg) fprintf(stdout, "xEditList::insertItems():Testing for |%s|\n", (const char *)pStr);
if (dbg) fflush(stdout);
if (!unique || getIndex((const char *)pStr) < 0)
{
if (dbg) fprintf(stdout, "xEditList::insertItems():Inserting |%s|\n", (const char *)pStr);
if (dbg) fflush(stdout);
pList->insertItem((const char *)pStr);
if (dbg) fprintf(stdout, "xEditList::insertItems():Done w/ Insert, starting over w/ |%s|\n", pItems);
if (dbg) fflush(stdout);
}
x = 0;
pStr = "";
if (!(*pItems))
break;
pItems++;
}
else
pStr += *(pItems++);
}
}
if (dbg) fprintf(stdout, "xEditList::insertItems:Exit");
if (dbg) fflush(stdout);
}
int xEditList::getIndex(const char *pString)
{
int x, cnt;
const char *cp;
if (pList)
cnt = pList->count();
else
return(-1);
for (x = 0; x < cnt; x++)
{
if ((cp = string(x)) != NULL)
{
if (strcmp(cp, pString) == 0)
break;
}
}
return((x == cnt) ? -1 : x);
}
void xEditList::recvdReturn()
{
int index;
if (strlen(pEdit->text()) > 0)
emit gotEntry((const char *)pEdit->text());
else
{
index = pList->currentItem();
#ifdef QT_PRE096
pEdit->setText(pList->string(index));
#else
pEdit->setText(pList->text(index));
#endif
emit gotEntry((const char *)pEdit->text());
}
}
void xEditList::recvdSelect(int index)
{
#ifdef QT_PRE096
pEdit->setText(pList->string(index));
#else
pEdit->setText(pList->text(index));
#endif
emit gotEntry((const char *)pEdit->text());
}
const char *xEditList::string(int x)
{
#ifdef QT_PRE096
return(pList->string(x));
#else
return(pList->text(x));
#endif
}
void xEditList::keyPressEvent(QKeyEvent *pEvt)
{
unsigned int x;
if (dbg) fprintf(stdout, "xListBox::keyPressEvent():Got key - 0x%d\n", pEvt->key());
if (pEvt->key() == Key_Up)
{
if (dbg) fprintf(stdout, "xListBox::keyPressEvent():Key_Up!!\n");
if ((x = (unsigned int)pList->currentItem()) > 0)
pList->setCurrentItem(--x);
}
else if (pEvt->key() == Key_Down)
{
if (dbg) fprintf(stdout, "xListBox::keyPressEvent():Key_Down!!\n");
if ((x = (unsigned int)pList->currentItem()) < pList->count())
pList->setCurrentItem(++x);
}
xFrame::keyPressEvent(pEvt);
}
void xEditList::resizeEvent(QResizeEvent *pEvt)
{
QSize s = pEvt->size();
int h, w;
if (dbg > 1) fprintf(stdout, "xListBox::resizeEvent():Enter\n");
h = s.height() - pEdit->size().height() - (lineWidth() * 2);
w = s.width() - (lineWidth() * 2);
h = (h <= 0) ? 5 : h;
w = (w <= 0) ? 5 : w;
s.setHeight(h);
s.setWidth(w);
// pList->resize(s);
// fitFrame();
if (dbg > 1) fprintf(stdout, "xListBox::resizeEvent():Exit\n");
}
#include "xEditList.moc"
|