~ubuntu-branches/ubuntu/feisty/kvirc/feisty

« back to all changes in this revision

Viewing changes to src/kvilib/kvi_listview.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Robin Verduijn
  • Date: 2002-04-16 13:47:41 UTC
  • Revision ID: james.westby@ubuntu.com-20020416134741-02slrqk6ige4cchu
Tags: 1:2.1.2-11
* #138169: The problem in bug #138169 is due to a bug in libtool. See
  bug #98342 for details. KVirc still doesn't build correctly even with
  the latest libtool (1.4.2-4). When this gets properly fixed I'll update
  kvirc's build dependency on libtool. In the mean time, I've applied a
  patch from that bug report which fixes it for me.
  (Closes: #138169)
* Redid debian/rules somewhat; no longer try to build differently
  depending on how KDE is installed. If the preferred configuration breaks
  for some platform, I'd rather know about it.
* Don't link versus qt-mt anymore.
* GNU config automated update: config.sub (20010907 to 20020307),
  config.guess (20010904 to 20020320)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
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
 
4
//
 
5
//   This file is part of the KVirc irc client distribution
 
6
//   Copyright (C) 1999-2000 Szymon Stefanek (stefanek@tin.it)
 
7
//
 
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.
 
12
//
 
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.
 
17
//
 
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.
 
21
//
 
22
#include "kvi_listview.h"
 
23
 
 
24
#include <qnamespace.h>
 
25
 
 
26
//=============================================================================
 
27
//
 
28
// ### Field editor for QListView ###
 
29
//
 
30
//=============================================================================
 
31
 
 
32
KviFieldEditor::KviFieldEditor(QWidget *parent)
 
33
:QLineEdit(parent)
 
34
{
 
35
        hide();
 
36
//      connect(this,SIGNAL(returnPressed()),this,SLOT(pressedReturnKey()));
 
37
//      setFrameStyle(QFrame::Panel|QFrame::Sunken);...oops....
 
38
}
 
39
 
 
40
KviFieldEditor::~KviFieldEditor(){}
 
41
 
 
42
void KviFieldEditor::edit(const QString &str,bool bEnd)
 
43
{
 
44
        setText(str);
 
45
        if(bEnd)end(false);
 
46
        else home(false);
 
47
        show();
 
48
        grabMouse();
 
49
        setFocus();
 
50
}
 
51
 
 
52
void KviFieldEditor::keyPressEvent(QKeyEvent *e)
 
53
{
 
54
        if(e->key() == Qt::Key_Return){
 
55
                QLineEdit::keyPressEvent(e);
 
56
                terminateEdit(true);
 
57
                e->accept();
 
58
        } else if(e->key() == Qt::Key_Up){
 
59
                QLineEdit::keyPressEvent(e);
 
60
                emit keyUpPressed();
 
61
                e->accept();            
 
62
        } else if(e->key() == Qt::Key_Down){
 
63
                QLineEdit::keyPressEvent(e);
 
64
                emit keyDownPressed();
 
65
                e->accept();
 
66
        } else if(e->key() == Qt::Key_Right){
 
67
                if((unsigned int)cursorPosition() == text().length()){
 
68
                        QLineEdit::keyPressEvent(e);
 
69
                        emit keyRightPressed();
 
70
                        e->accept();    
 
71
                } else QLineEdit::keyPressEvent(e);
 
72
        } else if(e->key() == Qt::Key_Left){
 
73
                if(cursorPosition() == 0){
 
74
                        QLineEdit::keyPressEvent(e);
 
75
                        emit keyLeftPressed();
 
76
                        e->accept();
 
77
                } else QLineEdit::keyPressEvent(e);
 
78
        } else QLineEdit::keyPressEvent(e);
 
79
}
 
80
 
 
81
void KviFieldEditor::mousePressEvent(QMouseEvent *e)
 
82
{
 
83
        //if the mouse is outside of this widget release...
 
84
        if(!rect().contains(e->pos()))terminateEdit(true);
 
85
        else QLineEdit::mousePressEvent(e);
 
86
}
 
87
 
 
88
void KviFieldEditor::terminateEdit(bool bCommitChanges)
 
89
{
 
90
        releaseMouse();
 
91
        if(isVisible()){
 
92
                hide();
 
93
                if(bCommitChanges)emit editFinished(text());
 
94
                setText("");
 
95
        }
 
96
}
 
97
 
 
98
//void KviFieldEditor::handleKeyEvent(QKeyEvent *e){ keyPressEvent(e); }
 
99
void KviFieldEditor::focusOutEvent(QFocusEvent *){ terminateEdit(true); }
 
100
//void KviFieldEditor::pressedReturnKey(){ terminateEdit(true); }
 
101
 
 
102
 
 
103
 
 
104
 
 
105
KviListView::KviListView(QWidget * par)
 
106
:QListView(par)
 
107
{
 
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()));
 
115
        m_iColumnCount = 0;
 
116
        m_pCurEditedItem = 0;
 
117
        m_iCurEditedColumn = -1;
 
118
        setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
 
119
        setAllColumnsShowFocus(true);
 
120
 
 
121
        if(parent()){
 
122
                QWidget * w = (QWidget *)parent();
 
123
                while(w){
 
124
                        w->installEventFilter(this);
 
125
                        w = (QWidget *)w->parent();
 
126
                }
 
127
        }
 
128
}
 
129
 
 
130
KviListView::~KviListView()
 
131
{
 
132
}
 
133
 
 
134
int KviListView::addColumn(const QString &label,int width)
 
135
{
 
136
        m_iColumnCount++;
 
137
        return QListView::addColumn(label,width);
 
138
}
 
139
 
 
140
void KviListView::itemDoubleClicked(QListViewItem *it)
 
141
{
 
142
        if(!it)return;
 
143
        QPoint pnt = QCursor::pos();
 
144
        pnt = viewport()->mapFromGlobal(pnt);
 
145
 
 
146
        int col = 0;
 
147
        int xLeft = - contentsX();
 
148
        while(col < m_iColumnCount){
 
149
                xLeft += columnWidth(col);
 
150
                if(pnt.x() < xLeft){
 
151
                        editField(it,col);
 
152
                        return;
 
153
                }
 
154
                col++;
 
155
        }
 
156
}
 
157
 
 
158
void KviListView::editField(QListViewItem *it,int column,bool bEnd)
 
159
{
 
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);
 
169
}
 
170
 
 
171
void KviListView::editTerminated(const QString &newText)
 
172
{
 
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;
 
177
        setFocus();
 
178
}
 
179
 
 
180
bool KviListView::eventFilter(QObject * o,QEvent * e)
 
181
{
 
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();
 
187
                        return true;
 
188
                }
 
189
        }
 
190
        return QListView::eventFilter(o,e);
 
191
}
 
192
 
 
193
void KviListView::editorKeyLeft()
 
194
{
 
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
 
200
        --idx;
 
201
        if(idx >= 0)editField(i,idx);
 
202
        else editField(i,m_iColumnCount - 1);
 
203
}
 
204
 
 
205
void KviListView::editorKeyRight()
 
206
{
 
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
 
212
        ++idx;
 
213
        if(idx < m_iColumnCount)editField(i,idx,false);
 
214
        else editField(i,0,false);
 
215
}
 
216
 
 
217
void KviListView::editorKeyUp()
 
218
{
 
219
        QListViewItem * i = m_pCurEditedItem ? m_pCurEditedItem->itemAbove() : 0;
 
220
        if(!i)return;
 
221
        int idx = m_iCurEditedColumn;
 
222
        m_pEdit->terminateEdit(true);
 
223
        //And edit the next field
 
224
        editField(i,idx);
 
225
//      ensureVisible(i);
 
226
}
 
227
 
 
228
void KviListView::editorKeyDown()
 
229
{
 
230
        QListViewItem * i = m_pCurEditedItem ? m_pCurEditedItem->itemBelow() : 0;
 
231
        if(!i)return;
 
232
        int idx = m_iCurEditedColumn;
 
233
        m_pEdit->terminateEdit(true);
 
234
        //And edit the next field
 
235
        editField(i,idx);
 
236
//      ensureVisible(i);
 
237
}
 
238
 
 
239
void KviListView::drawContentsOffset(QPainter *p,int ox,int oy,int cx,int cy,int cw,int ch)
 
240
{
 
241
        QListView::drawContentsOffset(p,ox,oy,cx,cy,cw,ch);
 
242
//      debug("%d,%d,%d,%d,%d,%d",ox,oy,cx,cy,cw,ch);
 
243
        int r1,g1,b1;
 
244
        int r2,g2,b2;
 
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));
 
249
        int nCol = 0;
 
250
        int curX = 0;
 
251
        while((nCol <= columns()))
 
252
        {
 
253
                if((curX > ox) && (curX >= cx) && (curX <= cx + cw))
 
254
                {
 
255
                        int realX = (curX - ox) - 1;
 
256
                        int realY = (cy - oy);
 
257
                        p->drawLine(realX,- oy,realX,realY + ch);
 
258
                }
 
259
                curX += columnWidth(nCol);
 
260
                nCol++;
 
261
        }
 
262
        QListViewItem * it = itemAt(QPoint(1,cy - oy));
 
263
        if(!it)return;
 
264
        QRect r =  itemRect(it);
 
265
        int curY = r.y();
 
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()))
 
271
        {
 
272
                if(curY > 0)
 
273
                {
 
274
                        int realX = (cx - ox);
 
275
                        p->drawLine(- ox,curY,realX + cw,curY); 
 
276
                }
 
277
                curY += itmH;
 
278
                idx++;
 
279
        }
 
280
}
 
281
 
 
282
 
 
283
#include "m_kvi_listview.moc"