3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
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
),
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
)
43 QRect r
= p
.boundingRect(0,0, 0,0,
44 AlignLeft
| SingleLine
| DontClip
| ExpandTabs
,
48 int w
= r
.width() + 4;
53 int h
= r
.height() + 1;
61 void KTextView::insertLine(const QString
& text
)
64 setNumRows(m_texts
.size());
66 bool update
= updateCellSize(text
);
68 if (update
&& autoUpdate()) {
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()))
85 bool update
= updateCellSize(text
);
95 void KTextView::setCursorPosition(int row
, int)
100 void KTextView::cursorPosition(int* row
, int* col
)
106 int KTextView::cellWidth(int /*col*/)
111 int KTextView::cellHeight(int /*row*/)
116 int KTextView::textCol() const
118 // by default, the text is in column 0
122 void KTextView::paintCell(QPainter
* p
, int row
, int /*col*/)
124 if (row
>= m_texts
.size()) {
127 if (row
== m_curRow
) {
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
147 int numVisibleRows
, newRow
, newX
;
152 activateLine(m_curRow
-1);
156 if (m_curRow
< numRows()-1) {
157 activateLine(m_curRow
+1);
161 /* this method doesn't work for variable height lines */
162 numVisibleRows
= lastRowVisible()-topCell();
163 newRow
= m_curRow
- QMAX(numVisibleRows
,1);
166 activateLine(newRow
);
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
);
177 // scroll left and right by a few pixels
179 newX
= xOffset() - viewWidth()/10;
180 setXOffset(QMAX(newX
, 0));
183 newX
= xOffset() + viewWidth()/10;
184 setXOffset(QMIN(newX
, maxXOffset()));
188 QWidget::keyPressEvent(ev
);
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));
201 setTopCell(m_curRow
);
207 void KTextView::mousePressEvent(QMouseEvent
* ev
)
209 int row
= findRow(ev
->y());
213 void KTextView::focusInEvent(QFocusEvent
*)
216 * The base class does a repaint(), which causes flicker. So we do
221 void KTextView::focusOutEvent(QFocusEvent
*)
224 * The base class does a repaint(), which causes flicker. So we do
229 void KTextView::activateLine(int row
)
236 int oldRow
= m_curRow
;
237 // note that row may be < 0
239 // must change m_curRow first, so that updateCell(oldRow) erases the old line!
241 updateCell(oldRow
, col
);
244 updateCell(row
, col
);
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
]);
264 void KTextView::setTabWidth(int numChars
)
266 QFontMetrics fm
= font();
267 int newTabWidth
= fm
.width('x') * numChars
;
268 if (newTabWidth
== m_tabWidth
)
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
]);
284 void KTextView::setupPainter(QPainter
* p
)
286 p
->setTabStops(m_tabWidth
);