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 TableView(parent
, name
, f
),
20 m_width(DEFAULT_WIDTH
),
21 m_height(DEFAULT_LINEHEIGHT
),
26 setBackgroundColor(colorGroup().base());
29 KTextView::~KTextView()
34 * Update cell width and hight; returns whether there is a change
35 * Cell geometry: There are 2 pixels to the left and to the right
36 * and 1 pixel _below_ the line
38 bool KTextView::updateCellSize(const QString
& text
)
42 QRect r
= p
.boundingRect(0,0, 0,0,
43 AlignLeft
| SingleLine
| DontClip
| ExpandTabs
,
47 int w
= r
.width() + 4;
52 int h
= r
.height() + 1;
60 void KTextView::insertLine(const QString
& text
)
63 setNumRows(m_texts
.size());
74 * TODO: This function doesn't shrink the line length if the longest line
75 * is replaced by a shorter one.
77 void KTextView::replaceLine(int line
, const QString
& text
)
79 if (line
< 0 || line
>= int(m_texts
.size()))
84 bool update
= updateCellSize(text
);
94 void KTextView::setCursorPosition(int row
, int)
99 void KTextView::cursorPosition(int* row
, int* col
)
105 int KTextView::cellWidth(int /*col*/) const
110 int KTextView::cellHeight(int /*row*/) const
115 int KTextView::textCol() const
117 // by default, the text is in column 0
121 void KTextView::paintCell(QPainter
* p
, int row
, int /*col*/)
123 if (row
>= m_texts
.size()) {
126 if (row
== m_curRow
) {
128 p
->fillRect(0,0, m_width
,m_height
, QBrush(colorGroup().background()));
130 const QString
& text
= m_texts
[row
];
131 p
->drawText(2,0, m_width
-4, m_height
,
132 AlignLeft
| SingleLine
| DontClip
| ExpandTabs
,
133 text
, text
.length());
136 void KTextView::keyPressEvent(QKeyEvent
* ev
)
138 int oldRow
= m_curRow
;
140 // go to line 0 if no current line
146 int numVisibleRows
, newRow
, newX
;
151 activateLine(m_curRow
-1);
155 if (m_curRow
< numRows()-1) {
156 activateLine(m_curRow
+1);
160 /* this method doesn't work for variable height lines */
161 numVisibleRows
= lastRowVisible()-topCell();
162 newRow
= m_curRow
- QMAX(numVisibleRows
,1);
165 activateLine(newRow
);
168 /* this method doesn't work for variable height lines */
169 numVisibleRows
= lastRowVisible()-topCell();
170 newRow
= m_curRow
+ QMAX(numVisibleRows
,1);
171 if (newRow
>= numRows())
172 newRow
= numRows()-1;
173 activateLine(newRow
);
176 // scroll left and right by a few pixels
178 newX
= xOffset() - viewWidth()/10;
179 setXOffset(QMAX(newX
, 0));
182 newX
= xOffset() + viewWidth()/10;
183 setXOffset(QMIN(newX
, maxXOffset()));
187 QWidget::keyPressEvent(ev
);
192 if (m_curRow
!= oldRow
&& !rowIsVisible(m_curRow
)) {
193 // if the old row was visible, we scroll the view by some pixels
194 // if the old row was not visible, we place active row at the top
195 if (oldRow
>= 0 && rowIsVisible(oldRow
)) {
196 int diff
= m_curRow
- oldRow
;
197 int newTop
= topCell() + diff
;
198 setTopCell(QMAX(newTop
,0));
200 setTopCell(m_curRow
);
206 void KTextView::mousePressEvent(QMouseEvent
* ev
)
208 int row
= findRow(ev
->y());
212 void KTextView::focusInEvent(QFocusEvent
*)
215 * The base class does a repaint(), which causes flicker. So we do
220 void KTextView::focusOutEvent(QFocusEvent
*)
223 * The base class does a repaint(), which causes flicker. So we do
228 void KTextView::activateLine(int row
)
235 int oldRow
= m_curRow
;
236 // note that row may be < 0
238 // must change m_curRow first, so that updateCell(oldRow) erases the old line!
240 updateCell(oldRow
, col
);
243 updateCell(row
, col
);
248 /* This is needed to make the kcontrol's color setup working */
249 void KTextView::paletteChange(const QPalette
& oldPal
)
251 setBackgroundColor(colorGroup().base());
252 TableView::paletteChange(oldPal
);
254 // recompute window size
255 m_width
= DEFAULT_WIDTH
;
256 m_height
= DEFAULT_LINEHEIGHT
;
257 for (int i
= 0; i
< m_texts
.size(); i
++) {
258 updateCellSize(m_texts
[i
]);
263 void KTextView::setTabWidth(int numChars
)
265 QFontMetrics fm
= font();
266 int newTabWidth
= fm
.width('x') * numChars
;
267 if (newTabWidth
== m_tabWidth
)
269 m_tabWidth
= newTabWidth
;
271 // recompute window width
272 m_width
= DEFAULT_WIDTH
;
273 for (int i
= 0; i
< m_texts
.size(); i
++) {
274 updateCellSize(m_texts
[i
]);
283 void KTextView::setupPainter(QPainter
* p
)
285 p
->setTabStops(m_tabWidth
);