Added Spanish translations.
[kdbg.git] / kdbg / textvw.cpp
blobce8dfc5a6e170cf5c40ac4b6c424e739cafa91a0
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"
16 KTextView::KTextView(QWidget* parent, const char* name, WFlags f) :
17 QTableView(parent, name, f),
18 m_width(300),
19 m_height(14),
20 m_curRow(-1)
22 setNumCols(1);
23 setBackgroundColor(colorGroup().base());
24 setTableFlags(Tbl_autoScrollBars);
27 KTextView::~KTextView()
32 * Update cell width and hight; returns whether there is a change
33 * Cell geometry: There are 2 pixels to the left and to the right
34 * and 1 pixel _below_ the line
36 bool KTextView::updateCellSize(const QString& text)
38 QPainter p(this);
39 setupPainter(&p);
40 QRect r = p.boundingRect(0,0, 0,0,
41 AlignLeft | SingleLine | DontClip | ExpandTabs,
42 text, text.length());
44 bool update = false;
45 int w = r.width() + 4;
46 if (w > m_width) {
47 m_width = w;
48 update = true;
50 int h = r.height() + 1;
51 if (h > m_height) {
52 m_height = h;
53 update = true;
55 return update;
58 void KTextView::insertLine(const QString& text)
60 m_texts.append(text);
61 setNumRows(m_texts.size());
63 bool update = updateCellSize(text);
65 if (update && autoUpdate()) {
66 updateTableSize();
67 repaint();
72 * TODO: This function doesn't shrink the line length if the longest line
73 * is replaced by a shorter one.
75 void KTextView::replaceLine(int line, const QString& text)
77 if (line < 0 || line >= int(m_texts.size()))
78 return;
80 m_texts[line] = text;
82 bool update = updateCellSize(text);
84 if (update) {
85 updateTableSize();
86 if (autoUpdate()) {
87 repaint();
92 void KTextView::setCursorPosition(int row, int)
94 activateLine(row);
97 void KTextView::cursorPosition(int* row, int* col)
99 *row = m_curRow;
100 *col = 0;
103 int KTextView::cellWidth(int /*col*/)
105 return m_width;
108 int KTextView::cellHeight(int /*row*/)
110 return m_height;
113 int KTextView::textCol() const
115 // by default, the text is in column 0
116 return 0;
119 void KTextView::paintCell(QPainter* p, int row, int /*col*/)
121 if (row >= m_texts.size()) {
122 return;
124 if (row == m_curRow) {
125 // paint background
126 p->fillRect(0,0, m_width,m_height, QBrush(colorGroup().background()));
128 const QString& text = m_texts[row];
129 p->drawText(2,0, m_width-4, m_height,
130 AlignLeft | SingleLine | DontClip | ExpandTabs,
131 text, text.length());
134 void KTextView::keyPressEvent(QKeyEvent* ev)
136 int oldRow = m_curRow;
138 // go to line 0 if no current line
139 if (m_curRow < 0) {
140 activateLine(0);
142 else
144 int numVisibleRows, newRow, newX;
146 switch (ev->key()) {
147 case Key_Up:
148 if (m_curRow > 0) {
149 activateLine(m_curRow-1);
151 break;
152 case Key_Down:
153 if (m_curRow < numRows()-1) {
154 activateLine(m_curRow+1);
156 break;
157 case Key_PageUp:
158 /* this method doesn't work for variable height lines */
159 numVisibleRows = lastRowVisible()-topCell();
160 newRow = m_curRow - QMAX(numVisibleRows,1);
161 if (newRow < 0)
162 newRow = 0;
163 activateLine(newRow);
164 break;
165 case Key_PageDown:
166 /* this method doesn't work for variable height lines */
167 numVisibleRows = lastRowVisible()-topCell();
168 newRow = m_curRow + QMAX(numVisibleRows,1);
169 if (newRow >= numRows())
170 newRow = numRows()-1;
171 activateLine(newRow);
172 break;
174 // scroll left and right by a few pixels
175 case Key_Left:
176 newX = xOffset() - viewWidth()/10;
177 setXOffset(QMAX(newX, 0));
178 break;
179 case Key_Right:
180 newX = xOffset() + viewWidth()/10;
181 setXOffset(QMIN(newX, maxXOffset()));
182 break;
184 default:
185 QWidget::keyPressEvent(ev);
186 return;
189 // make row visible
190 if (m_curRow != oldRow && !rowIsVisible(m_curRow)) {
191 // if the old row was visible, we scroll the view by some pixels
192 // if the old row was not visible, we place active row at the top
193 if (oldRow >= 0 && rowIsVisible(oldRow)) {
194 int diff = m_curRow - oldRow;
195 int newTop = topCell() + diff;
196 setTopCell(QMAX(newTop,0));
197 } else {
198 setTopCell(m_curRow);
201 ev->accept();
204 void KTextView::mousePressEvent(QMouseEvent* ev)
206 int row = findRow(ev->y());
207 activateLine(row);
210 void KTextView::focusInEvent(QFocusEvent*)
213 * The base class does a repaint(), which causes flicker. So we do
214 * nothing here.
218 void KTextView::focusOutEvent(QFocusEvent*)
221 * The base class does a repaint(), which causes flicker. So we do
222 * nothing here.
226 void KTextView::activateLine(int row)
228 if (row == m_curRow)
229 return;
231 int col = textCol();
233 int oldRow = m_curRow;
234 // note that row may be < 0
235 m_curRow = row;
236 // must change m_curRow first, so that updateCell(oldRow) erases the old line!
237 if (oldRow >= 0) {
238 updateCell(oldRow, col);
240 if (row >= 0) {
241 updateCell(row, col);
243 emit lineChanged();
246 /* This is needed to make the kcontrol's color setup working */
247 void KTextView::paletteChange(const QPalette& oldPal)
249 setBackgroundColor(colorGroup().base());
250 QTableView::paletteChange(oldPal);