1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/****************************************************************************
**
** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
**
** This file is part of the Edyuk project <http://edyuk.org>
**
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "qlinenumberpanel.h"
/*!
\file qlinenumberpanel.cpp
\brief Implementation of the QLineNumberPanel class
\see QLineNumberPanel
*/
#include "qeditor.h"
#include "qdocument.h"
#include "qdocument_p.h"
#include "qdocumentline.h"
/*!
\ingroup widgets
@{
*/
/*!
\class QLineNumberPanel
\brief A specific panel in charge of drawing line numbers of an editor
*/
QCE_AUTO_REGISTER(QLineNumberPanel)
/*!
\brief Constructor
*/
QLineNumberPanel::QLineNumberPanel(QWidget *p)
: QPanel(p), m_verbose(false)
{
setFixedWidth(20);
setObjectName("lineNumberPanel");
}
/*!
\brief Empty destructor
*/
QLineNumberPanel::~QLineNumberPanel()
{
}
/*!
*/
QString QLineNumberPanel::type() const
{
return "Line numbers";
}
/*!
*/
bool QLineNumberPanel::isVerboseMode() const
{
return m_verbose;
}
/*!
*/
void QLineNumberPanel::setVerboseMode(bool y)
{
m_verbose = y;
update();
}
void QLineNumberPanel::setFont_slot(const QFont &font)
{
setFont(font);
}
/*!
*/
void QLineNumberPanel::editorChange(QEditor *e)
{
if ( editor() )
{
disconnect( editor(), SIGNAL( cursorPositionChanged() ),
this , SLOT ( update() ) );
disconnect( editor()->document(), SIGNAL( fontChanged(QFont) ),
this , SLOT ( setFont_slot(QFont) ) );
}
if ( e )
{
setFixedWidth(fontMetrics().width(QString::number(e->document()->lines())) + 5);
connect(e , SIGNAL( cursorPositionChanged() ),
this, SLOT ( update() ) );
connect(e->document(), SIGNAL( fontChanged(QFont) ),
this, SLOT ( setFont_slot(QFont) ) );
}
}
/*!
*/
bool QLineNumberPanel::paint(QPainter *p, QEditor *e)
{
/*
possible Unicode caracter for wrapping arrow :
0x21B3
0x2937
*/
QFont f(font());
f.setWeight(QFont::Bold);
const QFontMetrics sfm(f);
bool specialFontUsage=false;
QFont specialFont(font());
#ifndef WIN32
static const QChar wrappingArrow(0x2937);
const QFontMetrics specialSfm(sfm);
#if QT_VERSION >= 0x050000 && defined Q_OS_MAC && (QT_VERSION < 0x050200)
if(!specialSfm.inFont(wrappingArrow)){
specialFontUsage=true;
specialFont.setFamily("Gothic Regular");
//specialSfm(specialFont);
}
#endif
#else
// 0xC4 gives a decent wrapping arrow in Wingdings fonts, availables on all windows systems
// this is a hackish fallback to workaround Windows issues with Unicode...
static const QChar wrappingArrow(0xC4);
specialFont.setFamily("Wingdings");
const QFontMetrics specialSfm(specialFont);
specialFontUsage=true;
#endif
int max = e->document()->lines();
if(max<100) max=100; // always reserve 3 line number columns to avoid ugly jumping of width
QString s_width=QString::number(max);
s_width.fill('6');
const int panelWidth = sfm.width(s_width) + 5;
setFixedWidth(panelWidth);
int n, posY,
as = QFontMetrics(e->document()->font()).ascent(),
ls = e->document()->getLineSpacing(),
pageBottom = e->viewport()->height(),
contentsY = e->verticalOffset();
QString txt;
QDocument *d = e->document();
const int cursorLine = e->cursor().lineNumber();
n = d->lineNumber(contentsY);
posY = as + 2 + d->y(n) - contentsY;
//qDebug("first = %i; last = %i", first, last);
//qDebug("beg pos : %i", posY);
for ( ; ; ++n )
{
//qDebug("n = %i; pos = %i", n, posY);
QDocumentLine line = d->line(n);
if ( line.isNull() || ((posY - as) > pageBottom) )
break;
if ( line.isHidden() )
continue;
bool draw = true;
if ( !m_verbose )
{
draw = !((n + 1) % 10) || !n || !line.marks().empty();
}
txt = QString::number(n + 1);
if ( n == cursorLine )
{
draw = true;
p->save();
QFont f = p->font();
f.setWeight(QFont::Bold);
p->setFont(f);
}
if ( draw )
{
p->drawText(width() - 2 - sfm.width(txt),
posY,
txt);
if(specialFontUsage){
if (line.lineSpan()>1) {
p->save();
specialFont.setBold(n == cursorLine); //todo: only get bold on the current wrapped line
p->setFont(specialFont);
}
}
for ( int i = 1; i < line.lineSpan(); ++i )
p->drawText(width() - 2 - specialSfm.width(wrappingArrow), posY + i * ls, wrappingArrow);
if(specialFontUsage){
if (line.lineSpan()>1)
p->restore();
}
} else {
int yOff = posY - (as + 1) + ls / 2;
if ( (n + 1) % 5 )
p->drawPoint(width() - 5, yOff);
else
p->drawLine(width() - 7, yOff, width() - 2, yOff);
}
if ( n == cursorLine )
{
p->restore();
}
posY += ls * line.lineSpan();
}
//p->setPen(Qt::DotLine);
//p->drawLine(width()-1, 0, width()-1, pageBottom);
//setFixedWidth(sfm.width(txt) + 5);
return true;
}
/*! @} */
|