3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
16 KTextView::KTextView(QWidget
* parent
, const char* name
, WFlags f
) :
17 QTableView(parent
, name
, f
),
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
)
40 QRect r
= p
.boundingRect(0,0, 0,0,
41 AlignLeft
| SingleLine
| DontClip
| ExpandTabs
,
45 int w
= r
.width() + 4;
50 int h
= r
.height() + 1;
58 void KTextView::insertLine(const QString
& text
)
61 setNumRows(m_texts
.size());
63 bool update
= updateCellSize(text
);
65 if (update
&& autoUpdate()) {
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()))
82 bool update
= updateCellSize(text
);
92 void KTextView::setCursorPosition(int row
, int)
97 void KTextView::cursorPosition(int* row
, int* col
)
103 int KTextView::cellWidth(int /*col*/)
108 int KTextView::cellHeight(int /*row*/)
113 int KTextView::textCol() const
115 // by default, the text is in column 0
119 void KTextView::paintCell(QPainter
* p
, int row
, int /*col*/)
121 if (row
>= m_texts
.size()) {
124 if (row
== m_curRow
) {
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
144 int numVisibleRows
, newRow
, newX
;
149 activateLine(m_curRow
-1);
153 if (m_curRow
< numRows()-1) {
154 activateLine(m_curRow
+1);
158 /* this method doesn't work for variable height lines */
159 numVisibleRows
= lastRowVisible()-topCell();
160 newRow
= m_curRow
- QMAX(numVisibleRows
,1);
163 activateLine(newRow
);
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
);
174 // scroll left and right by a few pixels
176 newX
= xOffset() - viewWidth()/10;
177 setXOffset(QMAX(newX
, 0));
180 newX
= xOffset() + viewWidth()/10;
181 setXOffset(QMIN(newX
, maxXOffset()));
185 QWidget::keyPressEvent(ev
);
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));
198 setTopCell(m_curRow
);
204 void KTextView::mousePressEvent(QMouseEvent
* ev
)
206 int row
= findRow(ev
->y());
210 void KTextView::focusInEvent(QFocusEvent
*)
213 * The base class does a repaint(), which causes flicker. So we do
218 void KTextView::focusOutEvent(QFocusEvent
*)
221 * The base class does a repaint(), which causes flicker. So we do
226 void KTextView::activateLine(int row
)
233 int oldRow
= m_curRow
;
234 // note that row may be < 0
236 // must change m_curRow first, so that updateCell(oldRow) erases the old line!
238 updateCell(oldRow
, col
);
241 updateCell(row
, col
);
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
);