2
// File : kvi_listview.cpp (/usr/build/NEW_kvirc/kvirc/src/kvilib/kvi_listview.cpp)
3
// Last major modification : Tue May 11 1999 00:03:01 by Szymon Stefanek
5
// This file is part of the KVirc irc client distribution
6
// Copyright (C) 1999-2000 Szymon Stefanek (stefanek@tin.it)
8
// This program is FREE software. You can redistribute it and/or
9
// modify it under the terms of the GNU General Public License
10
// as published by the Free Software Foundation; either version 2
11
// of the License, or (at your opinion) any later version.
13
// This program is distributed in the HOPE that it will be USEFUL,
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
// See the GNU General Public License for more details.
18
// You should have received a copy of the GNU General Public License
19
// along with this program. If not, write to the Free Software Foundation,
20
// Inc. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
#include "kvi_listview.h"
24
#include <qnamespace.h>
26
//=============================================================================
28
// ### Field editor for QListView ###
30
//=============================================================================
32
KviFieldEditor::KviFieldEditor(QWidget *parent)
36
// connect(this,SIGNAL(returnPressed()),this,SLOT(pressedReturnKey()));
37
// setFrameStyle(QFrame::Panel|QFrame::Sunken);...oops....
40
KviFieldEditor::~KviFieldEditor(){}
42
void KviFieldEditor::edit(const QString &str,bool bEnd)
52
void KviFieldEditor::keyPressEvent(QKeyEvent *e)
54
if(e->key() == Qt::Key_Return){
55
QLineEdit::keyPressEvent(e);
58
} else if(e->key() == Qt::Key_Up){
59
QLineEdit::keyPressEvent(e);
62
} else if(e->key() == Qt::Key_Down){
63
QLineEdit::keyPressEvent(e);
64
emit keyDownPressed();
66
} else if(e->key() == Qt::Key_Right){
67
if((unsigned int)cursorPosition() == text().length()){
68
QLineEdit::keyPressEvent(e);
69
emit keyRightPressed();
71
} else QLineEdit::keyPressEvent(e);
72
} else if(e->key() == Qt::Key_Left){
73
if(cursorPosition() == 0){
74
QLineEdit::keyPressEvent(e);
75
emit keyLeftPressed();
77
} else QLineEdit::keyPressEvent(e);
78
} else QLineEdit::keyPressEvent(e);
81
void KviFieldEditor::mousePressEvent(QMouseEvent *e)
83
//if the mouse is outside of this widget release...
84
if(!rect().contains(e->pos()))terminateEdit(true);
85
else QLineEdit::mousePressEvent(e);
88
void KviFieldEditor::terminateEdit(bool bCommitChanges)
93
if(bCommitChanges)emit editFinished(text());
98
//void KviFieldEditor::handleKeyEvent(QKeyEvent *e){ keyPressEvent(e); }
99
void KviFieldEditor::focusOutEvent(QFocusEvent *){ terminateEdit(true); }
100
//void KviFieldEditor::pressedReturnKey(){ terminateEdit(true); }
105
KviListView::KviListView(QWidget * par)
108
connect(this,SIGNAL(doubleClicked(QListViewItem *)),this,SLOT(itemDoubleClicked(QListViewItem *)));
109
m_pEdit = new KviFieldEditor(viewport());
110
connect(m_pEdit,SIGNAL(editFinished(const QString &)),this,SLOT(editTerminated(const QString &)));
111
connect(m_pEdit,SIGNAL(keyUpPressed()),this,SLOT(editorKeyUp()));
112
connect(m_pEdit,SIGNAL(keyDownPressed()),this,SLOT(editorKeyDown()));
113
connect(m_pEdit,SIGNAL(keyLeftPressed()),this,SLOT(editorKeyLeft()));
114
connect(m_pEdit,SIGNAL(keyRightPressed()),this,SLOT(editorKeyRight()));
116
m_pCurEditedItem = 0;
117
m_iCurEditedColumn = -1;
118
setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
119
setAllColumnsShowFocus(true);
122
QWidget * w = (QWidget *)parent();
124
w->installEventFilter(this);
125
w = (QWidget *)w->parent();
130
KviListView::~KviListView()
134
int KviListView::addColumn(const QString &label,int width)
137
return QListView::addColumn(label,width);
140
void KviListView::itemDoubleClicked(QListViewItem *it)
143
QPoint pnt = QCursor::pos();
144
pnt = viewport()->mapFromGlobal(pnt);
147
int xLeft = - contentsX();
148
while(col < m_iColumnCount){
149
xLeft += columnWidth(col);
158
void KviListView::editField(QListViewItem *it,int column,bool bEnd)
160
QRect rct = itemRect(it);
161
int fieldWidth = columnWidth(column);
162
int fieldX = - contentsX();
163
for(int i = 0;i< column;i++)fieldX += columnWidth(i);
164
m_pEdit->move(fieldX - 1,rct.top() - 1);
165
m_pEdit->resize(fieldWidth + 2 ,rct.height() + 2);
166
m_pCurEditedItem = it;
167
m_iCurEditedColumn = column;
168
m_pEdit->edit(it->text(column),bEnd);
171
void KviListView::editTerminated(const QString &newText)
173
if(m_pCurEditedItem && (m_iCurEditedColumn > -1) && (m_iCurEditedColumn < m_iColumnCount))
174
m_pCurEditedItem->setText(m_iCurEditedColumn,newText);
175
m_pCurEditedItem = 0;
176
m_iCurEditedColumn = -1;
180
bool KviListView::eventFilter(QObject * o,QEvent * e)
182
if(m_pEdit->isVisible()){
183
if((e->type() == QEvent::Accel)||(e->type() == QEvent::KeyPress)){
184
if(((QKeyEvent *)e)->key() != Qt::Key_Tab)m_pEdit->handleKeyEvent((QKeyEvent *)e);
185
else editorKeyRight();
186
((QKeyEvent *)e)->accept();
190
return QListView::eventFilter(o,e);
193
void KviListView::editorKeyLeft()
195
//Accept the previous changes...
196
QListViewItem * i = m_pCurEditedItem;
197
int idx = m_iCurEditedColumn;
198
m_pEdit->terminateEdit(true);
199
//And edit the next field
201
if(idx >= 0)editField(i,idx);
202
else editField(i,m_iColumnCount - 1);
205
void KviListView::editorKeyRight()
207
//Accept the previous changes...
208
QListViewItem * i = m_pCurEditedItem;
209
int idx = m_iCurEditedColumn;
210
m_pEdit->terminateEdit(true);
211
//And edit the next field
213
if(idx < m_iColumnCount)editField(i,idx,false);
214
else editField(i,0,false);
217
void KviListView::editorKeyUp()
219
QListViewItem * i = m_pCurEditedItem ? m_pCurEditedItem->itemAbove() : 0;
221
int idx = m_iCurEditedColumn;
222
m_pEdit->terminateEdit(true);
223
//And edit the next field
228
void KviListView::editorKeyDown()
230
QListViewItem * i = m_pCurEditedItem ? m_pCurEditedItem->itemBelow() : 0;
232
int idx = m_iCurEditedColumn;
233
m_pEdit->terminateEdit(true);
234
//And edit the next field
239
void KviListView::drawContentsOffset(QPainter *p,int ox,int oy,int cx,int cy,int cw,int ch)
241
QListView::drawContentsOffset(p,ox,oy,cx,cy,cw,ch);
242
// debug("%d,%d,%d,%d,%d,%d",ox,oy,cx,cy,cw,ch);
245
colorGroup().text().rgb(&r1,&g1,&b1);
246
colorGroup().base().rgb(&r2,&g2,&b2);
247
QColor clr((r1 + r2) >> 1,(g1 + g2) >> 1,(b1 + b2) >> 1);
248
p->setPen(QPen(clr,1,Qt::DotLine));
251
while((nCol <= columns()))
253
if((curX > ox) && (curX >= cx) && (curX <= cx + cw))
255
int realX = (curX - ox) - 1;
256
int realY = (cy - oy);
257
p->drawLine(realX,- oy,realX,realY + ch);
259
curX += columnWidth(nCol);
262
QListViewItem * it = itemAt(QPoint(1,cy - oy));
264
QRect r = itemRect(it);
266
int itmH = r.height();
267
if(itmH <= 0)return; // no more rectangles ?
268
int idx = it->itemPos() / itmH;
269
int realBottom = cy + ch - oy;
270
while((curY < realBottom) && (idx <= childCount()))
274
int realX = (cx - ox);
275
p->drawLine(- ox,curY,realX + cw,curY);
283
#include "m_kvi_listview.moc"