Make toolbar visible again.
[kdbg.git] / kdbg / textvw.cpp
blob41d199ec27c66af2a2f44128e0e049cc0cfcafbc
1 // $Id$
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #include <qpainter.h>
7 #include <qkeycode.h>
8 #include "textvw.h"
9 #include "textvw.moc"
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13 #include "mydebug.h"
15 #define DEFAULT_WIDTH 100
16 #define DEFAULT_LINEHEIGHT 1
18 KTextView::KTextView(QWidget* parent, const char* name, WFlags f) :
19 QTableView(parent, name, f),
20 m_width(DEFAULT_WIDTH),
21 m_height(DEFAULT_LINEHEIGHT),
22 m_tabWidth(0),
23 m_curRow(-1)
25 setNumCols(1);
26 setBackgroundColor(colorGroup().base());
27 setTableFlags(Tbl_autoScrollBars);
30 KTextView::~KTextView()
35 * Update cell width and hight; returns whether there is a change
36 * Cell geometry: There are 2 pixels to the left and to the right
37 * and 1 pixel _below_ the line
39 bool KTextView::updateCellSize(const QString& text)
41 QPainter p(this);
42 setupPainter(&p);
43 QRect r = p.boundingRect(0,0, 0,0,
44 AlignLeft | SingleLine | DontClip | ExpandTabs,
45 text, text.length());
47 bool update = false;
48 int w = r.width() + 4;
49 if (w > m_width) {
50 m_width = w;
51 update = true;
53 int h = r.height() + 1;
54 if (h > m_height) {
55 m_height = h;
56 update = true;
58 return update;
61 void KTextView::insertLine(const QString& text)
63 m_texts.append(text);
64 setNumRows(m_texts.size());
66 bool update = updateCellSize(text);
68 if (update && autoUpdate()) {
69 updateTableSize();
70 repaint();
75 * TODO: This function doesn't shrink the line length if the longest line
76 * is replaced by a shorter one.
78 void KTextView::replaceLine(int line, const QString& text)
80 if (line < 0 || line >= int(m_texts.size()))
81 return;
83 m_texts[line] = text;
85 bool update = updateCellSize(text);
87 if (update) {
88 updateTableSize();
89 if (autoUpdate()) {
90 repaint();
95 void KTextView::setCursorPosition(int row, int)
97 activateLine(row);
100 void KTextView::cursorPosition(int* row, int* col)
102 *row = m_curRow;
103 *col = 0;
106 int KTextView::cellWidth(int /*col*/)
108 return m_width;
111 int KTextView::cellHeight(int /*row*/)
113 return m_height;
116 int KTextView::textCol() const
118 // by default, the text is in column 0
119 return 0;
122 void KTextView::paintCell(QPainter* p, int row, int /*col*/)
124 if (row >= m_texts.size()) {
125 return;
127 if (row == m_curRow) {
128 // paint background
129 p->fillRect(0,0, m_width,m_height, QBrush(colorGroup().background()));
131 const QString& text = m_texts[row];
132 p->drawText(2,0, m_width-4, m_height,
133 AlignLeft | SingleLine | DontClip | ExpandTabs,
134 text, text.length());
137 void KTextView::keyPressEvent(QKeyEvent* ev)
139 int oldRow = m_curRow;
141 // go to line 0 if no current line
142 if (m_curRow < 0) {
143 activateLine(0);
145 else
147 int numVisibleRows, newRow, newX;
149 switch (ev->key()) {
150 case Key_Up:
151 if (m_curRow > 0) {
152 activateLine(m_curRow-1);
154 break;
155 case Key_Down:
156 if (m_curRow < numRows()-1) {
157 activateLine(m_curRow+1);
159 break;
160 case Key_PageUp:
161 /* this method doesn't work for variable height lines */
162 numVisibleRows = lastRowVisible()-topCell();
163 newRow = m_curRow - QMAX(numVisibleRows,1);
164 if (newRow < 0)
165 newRow = 0;
166 activateLine(newRow);
167 break;
168 case Key_PageDown:
169 /* this method doesn't work for variable height lines */
170 numVisibleRows = lastRowVisible()-topCell();
171 newRow = m_curRow + QMAX(numVisibleRows,1);
172 if (newRow >= numRows())
173 newRow = numRows()-1;
174 activateLine(newRow);
175 break;
177 // scroll left and right by a few pixels
178 case Key_Left:
179 newX = xOffset() - viewWidth()/10;
180 setXOffset(QMAX(newX, 0));
181 break;
182 case Key_Right:
183 newX = xOffset() + viewWidth()/10;
184 setXOffset(QMIN(newX, maxXOffset()));
185 break;
187 default:
188 QWidget::keyPressEvent(ev);
189 return;
192 // make row visible
193 if (m_curRow != oldRow && !rowIsVisible(m_curRow)) {
194 // if the old row was visible, we scroll the view by some pixels
195 // if the old row was not visible, we place active row at the top
196 if (oldRow >= 0 && rowIsVisible(oldRow)) {
197 int diff = m_curRow - oldRow;
198 int newTop = topCell() + diff;
199 setTopCell(QMAX(newTop,0));
200 } else {
201 setTopCell(m_curRow);
204 ev->accept();
207 void KTextView::mousePressEvent(QMouseEvent* ev)
209 int row = findRow(ev->y());
210 activateLine(row);
213 void KTextView::focusInEvent(QFocusEvent*)
216 * The base class does a repaint(), which causes flicker. So we do
217 * nothing here.
221 void KTextView::focusOutEvent(QFocusEvent*)
224 * The base class does a repaint(), which causes flicker. So we do
225 * nothing here.
229 void KTextView::activateLine(int row)
231 if (row == m_curRow)
232 return;
234 int col = textCol();
236 int oldRow = m_curRow;
237 // note that row may be < 0
238 m_curRow = row;
239 // must change m_curRow first, so that updateCell(oldRow) erases the old line!
240 if (oldRow >= 0) {
241 updateCell(oldRow, col);
243 if (row >= 0) {
244 updateCell(row, col);
246 emit lineChanged();
249 /* This is needed to make the kcontrol's color setup working */
250 void KTextView::paletteChange(const QPalette& oldPal)
252 setBackgroundColor(colorGroup().base());
253 QTableView::paletteChange(oldPal);
255 // recompute window size
256 m_width = DEFAULT_WIDTH;
257 m_height = DEFAULT_LINEHEIGHT;
258 for (int i = 0; i < m_texts.size(); i++) {
259 updateCellSize(m_texts[i]);
261 updateTableSize();
264 void KTextView::setTabWidth(int numChars)
266 QFontMetrics fm = font();
267 int newTabWidth = fm.width('x') * numChars;
268 if (newTabWidth == m_tabWidth)
269 return;
270 m_tabWidth = newTabWidth;
272 // recompute window width
273 m_width = DEFAULT_WIDTH;
274 for (int i = 0; i < m_texts.size(); i++) {
275 updateCellSize(m_texts[i]);
278 updateTableSize();
279 if (autoUpdate()) {
280 repaint();
284 void KTextView::setupPainter(QPainter* p)
286 p->setTabStops(m_tabWidth);