File: textvw.cpp

package info (click to toggle)
kdbg 1.2.4-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,504 kB
  • ctags: 1,945
  • sloc: cpp: 15,961; sh: 8,183; makefile: 481; perl: 71; ansic: 16
file content (286 lines) | stat: -rw-r--r-- 6,048 bytes parent folder | download
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
// $Id: textvw.cpp,v 1.9.2.1 2001/12/28 21:10:28 jsixt Exp $

// Copyright by Johannes Sixt
// This file is under GPL, the GNU General Public Licence

#include <qpainter.h>
#include <qkeycode.h>
#include "textvw.h"
#include "textvw.moc"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mydebug.h"

#define DEFAULT_WIDTH 100
#define DEFAULT_LINEHEIGHT 1

KTextView::KTextView(QWidget* parent, const char* name, WFlags f) :
	TableView(parent, name, f),
	m_width(DEFAULT_WIDTH),
	m_height(DEFAULT_LINEHEIGHT),
	m_tabWidth(0),
	m_curRow(-1)
{
    setNumCols(1);
    setBackgroundColor(colorGroup().base());
}

KTextView::~KTextView()
{
}

/*
 * Update cell width and hight; returns whether there is a change
 * Cell geometry: There are 2 pixels to the left and to the right
 * and 1 pixel _below_ the line
 */
bool KTextView::updateCellSize(const QString& text)
{
    QPainter p(this);
    setupPainter(&p);
    QRect r = p.boundingRect(0,0, 0,0,
			     AlignLeft | SingleLine | DontClip | ExpandTabs,
			     text, text.length());

    bool update = false;
    int w = r.width() + 4;
    if (w > m_width) {
	m_width = w;
	update = true;
    }
    int h = r.height() + 1;
    if (h > m_height) {
	m_height = h;
	update = true;
    }
    return update;
}

void KTextView::insertLine(const QString& text)
{
    m_texts.append(text);
    setNumRows(m_texts.size());

    updateCellSize(text);

    if (autoUpdate()) {
	updateTableSize();
	repaint();
    }
}

/*
 * TODO: This function doesn't shrink the line length if the longest line
 * is replaced by a shorter one.
 */
void KTextView::replaceLine(int line, const QString& text)
{
    if (line < 0 || line >= int(m_texts.size()))
	return;

    m_texts[line] = text;

    bool update = updateCellSize(text);

    if (autoUpdate()) {
	if (update) {
	    updateTableSize();
	}
	repaint();
    }
}

void KTextView::setCursorPosition(int row, int)
{
    activateLine(row);
}

void KTextView::cursorPosition(int* row, int* col)
{
    *row = m_curRow;
    *col = 0;
}

int KTextView::cellWidth(int /*col*/) const 
{
    return m_width;
}

int KTextView::cellHeight(int /*row*/) const
{
    return m_height;
}

int KTextView::textCol() const
{
    // by default, the text is in column 0
    return 0;
}

void KTextView::paintCell(QPainter* p, int row, int /*col*/)
{
    if (row >= m_texts.size()) {
	return;
    }
    if (row == m_curRow) {
	// paint background
	p->fillRect(0,0, m_width,m_height, QBrush(colorGroup().background()));
    }
    const QString& text = m_texts[row];
    p->drawText(2,0, m_width-4, m_height,
		AlignLeft | SingleLine | DontClip | ExpandTabs,
		text, text.length());
}

void KTextView::keyPressEvent(QKeyEvent* ev)
{
    int oldRow = m_curRow;

    // go to line 0 if no current line
    if (m_curRow < 0) {
	activateLine(0);
    }
    else
    {
	int numVisibleRows, newRow, newX;

	switch (ev->key()) {
	case Key_Up:
	    if (m_curRow > 0) {
		activateLine(m_curRow-1);
	    }
	    break;
	case Key_Down:
	    if (m_curRow < numRows()-1) {
		activateLine(m_curRow+1);
	    }
	    break;
	case Key_PageUp:
	    /* this method doesn't work for variable height lines */
	    numVisibleRows = lastRowVisible()-topCell();
	    newRow = m_curRow - QMAX(numVisibleRows,1);
	    if (newRow < 0)
		newRow = 0;
	    activateLine(newRow);
	    break;
	case Key_PageDown:
	    /* this method doesn't work for variable height lines */
	    numVisibleRows = lastRowVisible()-topCell();
	    newRow = m_curRow + QMAX(numVisibleRows,1);
	    if (newRow >= numRows())
		newRow = numRows()-1;
	    activateLine(newRow);
	    break;

	// scroll left and right by a few pixels
	case Key_Left:
	    newX = xOffset() - viewWidth()/10;
	    setXOffset(QMAX(newX, 0));
	    break;
	case Key_Right:
	    newX = xOffset() + viewWidth()/10;
	    setXOffset(QMIN(newX, maxXOffset()));
	    break;

	default:
	    QWidget::keyPressEvent(ev);
	    return;
	}
    }
    // make row visible
    if (m_curRow != oldRow && !rowIsVisible(m_curRow)) {
	// if the old row was visible, we scroll the view by some pixels
	// if the old row was not visible, we place active row at the top
	if (oldRow >= 0 && rowIsVisible(oldRow)) {
	    int diff = m_curRow - oldRow;
	    int newTop = topCell() + diff;
	    setTopCell(QMAX(newTop,0));
	} else {
	    setTopCell(m_curRow);
	}
    }
    ev->accept();
}

void KTextView::mousePressEvent(QMouseEvent* ev)
{
    int row = findRow(ev->y());
    activateLine(row);
}

void KTextView::focusInEvent(QFocusEvent*)
{
    /*
     * The base class does a repaint(), which causes flicker. So we do
     * nothing here.
     */
}

void KTextView::focusOutEvent(QFocusEvent*)
{
    /*
     * The base class does a repaint(), which causes flicker. So we do
     * nothing here.
     */
}

void KTextView::activateLine(int row)
{
    if (row == m_curRow)
	return;

    int col = textCol();

    int oldRow = m_curRow;
    // note that row may be < 0
    m_curRow = row;
    // must change m_curRow first, so that updateCell(oldRow) erases the old line!
    if (oldRow >= 0) {
	updateCell(oldRow, col);
    }
    if (row >= 0) {
	updateCell(row, col);
    }
    emit lineChanged();
}

/* This is needed to make the kcontrol's color setup working */
void KTextView::paletteChange(const QPalette& oldPal)
{
    setBackgroundColor(colorGroup().base());
    TableView::paletteChange(oldPal);

    // recompute window size
    m_width = DEFAULT_WIDTH;
    m_height = DEFAULT_LINEHEIGHT;
    for (int i = 0; i < m_texts.size(); i++) {
	updateCellSize(m_texts[i]);
    }
    updateTableSize();
}

void KTextView::setTabWidth(int numChars)
{
    QFontMetrics fm = font();
    int newTabWidth = fm.width('x') * numChars;
    if (newTabWidth == m_tabWidth)
	return;
    m_tabWidth = newTabWidth;

    // recompute window width
    m_width = DEFAULT_WIDTH;
    for (int i = 0; i < m_texts.size(); i++) {
	updateCellSize(m_texts[i]);
    }

    updateTableSize();
    if (autoUpdate()) {
	repaint();
    }
}

void KTextView::setupPainter(QPainter* p)
{
    p->setTabStops(m_tabWidth);
}