2006-03-05 17:24:44 +00:00
|
|
|
/**
|
|
|
|
* \file QLPainter.C
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author John Levon
|
|
|
|
* \author Abdelrazak Younes
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "QLPainter.h"
|
|
|
|
|
2006-06-20 08:39:16 +00:00
|
|
|
#include "GuiWorkArea.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "QLImage.h"
|
2006-06-17 09:14:58 +00:00
|
|
|
#include "ColorCache.h"
|
|
|
|
#include "FontLoader.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-09-22 09:47:39 +00:00
|
|
|
#include "GuiApplication.h"
|
2006-08-30 14:59:07 +00:00
|
|
|
#include "qt_helpers.h"
|
2006-06-26 16:55:35 +00:00
|
|
|
|
|
|
|
#include "debug.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "language.h"
|
|
|
|
#include "LColor.h"
|
|
|
|
|
|
|
|
#include "frontends/font_metrics.h"
|
|
|
|
|
2006-08-13 22:54:59 +00:00
|
|
|
#include "support/unicode.h"
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QPicture>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QImage>
|
|
|
|
|
2006-08-13 22:54:59 +00:00
|
|
|
using lyx::char_type;
|
|
|
|
using lyx::docstring;
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
using std::endl;
|
|
|
|
using std::string;
|
|
|
|
|
2006-06-20 08:39:16 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
QLPainter::~QLPainter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
|
2006-06-20 08:39:16 +00:00
|
|
|
QLPainter::QLPainter(GuiWorkArea * qwa)
|
2006-03-20 10:43:21 +00:00
|
|
|
: Painter(), qwa_(qwa)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-23 14:12:17 +00:00
|
|
|
void QLPainter::start()
|
|
|
|
{
|
|
|
|
qp_.reset(new QPainter(qwa_->paintDevice()));
|
2006-06-25 11:18:56 +00:00
|
|
|
// new QPainter has default QPen:
|
2006-08-13 22:54:59 +00:00
|
|
|
current_color_ = LColor::black;
|
2006-06-25 11:18:56 +00:00
|
|
|
current_ls_ = line_solid;
|
|
|
|
current_lw_ = line_thin;
|
2006-06-23 14:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::end()
|
|
|
|
{
|
|
|
|
qp_->end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
int QLPainter::paperWidth() const
|
|
|
|
{
|
|
|
|
return qwa_->viewport()->width();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int QLPainter::paperHeight() const
|
|
|
|
{
|
|
|
|
return qwa_->viewport()->height();
|
|
|
|
}
|
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
void QLPainter::setQPainterPen(LColor_color col,
|
2006-03-05 17:24:44 +00:00
|
|
|
Painter::line_style ls, Painter::line_width lw)
|
|
|
|
{
|
2006-06-14 14:23:25 +00:00
|
|
|
if (col == current_color_ && ls == current_ls_ && lw == current_lw_)
|
|
|
|
return;
|
2006-06-11 21:22:36 +00:00
|
|
|
|
2006-06-14 14:23:25 +00:00
|
|
|
current_color_ = col;
|
2006-06-11 21:22:36 +00:00
|
|
|
current_ls_ = ls;
|
|
|
|
current_lw_ = lw;
|
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
QPen pen = qp_.get()->pen();
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-06-14 14:23:25 +00:00
|
|
|
pen.setColor(lcolorcache.get(col));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
switch (ls) {
|
|
|
|
case line_solid: pen.setStyle(Qt::SolidLine); break;
|
|
|
|
case line_onoffdash: pen.setStyle(Qt::DotLine); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (lw) {
|
|
|
|
case line_thin: pen.setWidth(0); break;
|
|
|
|
case line_thick: pen.setWidth(3); break;
|
|
|
|
}
|
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
qp_.get()->setPen(pen);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
|
2006-06-14 14:23:25 +00:00
|
|
|
void QLPainter::point(int x, int y, LColor_color col)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-06-25 11:18:56 +00:00
|
|
|
setQPainterPen(col);
|
2006-06-23 14:12:17 +00:00
|
|
|
qp_->drawPoint(x, y);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::line(int x1, int y1, int x2, int y2,
|
|
|
|
LColor_color col,
|
|
|
|
line_style ls,
|
|
|
|
line_width lw)
|
|
|
|
{
|
2006-06-25 11:18:56 +00:00
|
|
|
setQPainterPen(col, ls, lw);
|
2006-06-23 14:12:17 +00:00
|
|
|
qp_->drawLine(x1, y1, x2, y2);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::lines(int const * xp, int const * yp, int np,
|
|
|
|
LColor_color col,
|
|
|
|
line_style ls,
|
|
|
|
line_width lw)
|
|
|
|
{
|
|
|
|
// FIXME ?
|
|
|
|
|
|
|
|
// Must use new as np is not known at compile time.
|
|
|
|
boost::scoped_array<QPoint> points(new QPoint[np]);
|
|
|
|
|
|
|
|
for (int i = 0; i < np; ++i) {
|
|
|
|
points[i].setX(xp[i]);
|
|
|
|
points[i].setY(yp[i]);
|
|
|
|
}
|
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
setQPainterPen(col, ls, lw);
|
2006-06-23 14:12:17 +00:00
|
|
|
qp_->drawPolyline(points.get(), np);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::rectangle(int x, int y, int w, int h,
|
|
|
|
LColor_color col,
|
|
|
|
line_style ls,
|
|
|
|
line_width lw)
|
|
|
|
{
|
2006-06-25 11:18:56 +00:00
|
|
|
setQPainterPen(col, ls, lw);
|
2006-06-23 14:12:17 +00:00
|
|
|
qp_->drawRect(x, y, w, h);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::fillRectangle(int x, int y, int w, int h, LColor_color col)
|
|
|
|
{
|
2006-06-23 14:12:17 +00:00
|
|
|
qp_->fillRect(x, y, w, h, lcolorcache.get(col));
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::arc(int x, int y, unsigned int w, unsigned int h,
|
|
|
|
int a1, int a2, LColor_color col)
|
|
|
|
{
|
|
|
|
// LyX usings 1/64ths degree, Qt usings 1/16th
|
2006-06-25 11:18:56 +00:00
|
|
|
setQPainterPen(col);
|
2006-06-23 14:12:17 +00:00
|
|
|
qp_->drawArc(x, y, w, h, a1 / 4, a2 / 4);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::image(int x, int y, int w, int h,
|
|
|
|
lyx::graphics::Image const & i)
|
|
|
|
{
|
|
|
|
lyx::graphics::QLImage const & qlimage =
|
|
|
|
static_cast<lyx::graphics::QLImage const &>(i);
|
|
|
|
|
|
|
|
fillRectangle(x, y, w, h, LColor::graphicsbg);
|
|
|
|
|
2006-06-23 14:12:17 +00:00
|
|
|
qp_->drawImage(x, y, qlimage.qimage(), 0, 0, w, h);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-13 22:54:59 +00:00
|
|
|
void QLPainter::text(int x, int y, docstring const & s, LyXFont const & f)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-08-13 22:54:59 +00:00
|
|
|
return text(x, y, reinterpret_cast<char_type const *>(s.data()), s.length(), f);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-13 22:54:59 +00:00
|
|
|
void QLPainter::text(int x, int y, char_type c, LyXFont const & f)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-08-13 22:54:59 +00:00
|
|
|
char_type s[2] = { c, char_type('\0') };
|
2006-03-05 17:24:44 +00:00
|
|
|
return text(x, y, s, 1, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPainter::smallCapsText(int x, int y,
|
|
|
|
QString const & s, LyXFont const & f)
|
|
|
|
{
|
|
|
|
LyXFont smallfont(f);
|
|
|
|
smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
|
|
|
|
|
2006-09-22 09:47:39 +00:00
|
|
|
QFont const & qfont = guiApp->fontLoader().get(f);
|
|
|
|
QFont const & qsmallfont = guiApp->fontLoader().get(smallfont);
|
2006-03-05 17:24:44 +00:00
|
|
|
QFontMetrics const & qfontm = QFontMetrics(qfont);
|
|
|
|
QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
|
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
setQPainterPen(f.realColor());
|
2006-03-05 17:24:44 +00:00
|
|
|
int tmpx = x;
|
|
|
|
size_t ls = s.length();
|
2006-06-25 11:27:33 +00:00
|
|
|
for (unsigned int i = 0; i < ls; ++i) {
|
2006-08-17 08:56:53 +00:00
|
|
|
QChar const c = s[i].toUpper();
|
2006-03-05 17:24:44 +00:00
|
|
|
if (c != s.at(i)) {
|
2006-06-23 14:12:17 +00:00
|
|
|
qp_->setFont(qsmallfont);
|
|
|
|
qp_->drawText(tmpx, y, c);
|
2006-03-05 17:24:44 +00:00
|
|
|
tmpx += qsmallfontm.width(c);
|
|
|
|
} else {
|
2006-06-23 14:12:17 +00:00
|
|
|
qp_->setFont(qfont);
|
|
|
|
qp_->drawText(tmpx, y, c);
|
2006-03-05 17:24:44 +00:00
|
|
|
tmpx += qfontm.width(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-13 22:54:59 +00:00
|
|
|
void QLPainter::text(int x, int y, char_type const * s, size_t ls,
|
2006-03-05 17:24:44 +00:00
|
|
|
LyXFont const & f)
|
|
|
|
{
|
2006-08-13 22:54:59 +00:00
|
|
|
#if 0
|
2006-03-05 17:24:44 +00:00
|
|
|
Encoding const * encoding = f.language()->encoding();
|
|
|
|
if (f.isSymbolFont())
|
|
|
|
encoding = encodings.symbol_encoding();
|
2006-08-13 22:54:59 +00:00
|
|
|
#endif
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-08-13 22:54:59 +00:00
|
|
|
#if 0
|
2006-03-05 17:24:44 +00:00
|
|
|
QString str;
|
|
|
|
str.setLength(ls);
|
2006-06-25 11:27:33 +00:00
|
|
|
for (unsigned int i = 0; i < ls; ++i)
|
2006-03-05 17:24:44 +00:00
|
|
|
str[i] = QChar(encoding->ucs(s[i]));
|
2006-08-13 22:54:59 +00:00
|
|
|
#else
|
2006-08-30 14:59:07 +00:00
|
|
|
QString str = ucs4_to_qstring(s, ls);
|
2006-08-13 22:54:59 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
2006-03-05 17:24:44 +00:00
|
|
|
// HACK: QT3 refuses to show single compose characters
|
2006-06-14 14:23:25 +00:00
|
|
|
// Still needed with Qt4?
|
2006-03-05 17:24:44 +00:00
|
|
|
if (ls == 1 && str[0].unicode() >= 0x05b0 && str[0].unicode() <= 0x05c2)
|
|
|
|
str = ' ' + str;
|
2006-08-13 22:54:59 +00:00
|
|
|
#endif
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
|
2006-06-25 11:18:56 +00:00
|
|
|
setQPainterPen(f.realColor());
|
2006-09-22 09:47:39 +00:00
|
|
|
qp_->setFont(guiApp->fontLoader().get(f));
|
2006-06-14 14:23:25 +00:00
|
|
|
// We need to draw the text as LTR as we use our own bidi code.
|
2006-06-23 14:12:17 +00:00
|
|
|
qp_->setLayoutDirection(Qt::LeftToRight);
|
|
|
|
qp_->drawText(x, y, str);
|
2006-03-05 17:24:44 +00:00
|
|
|
} else {
|
|
|
|
smallCapsText(x, y, str, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f.underbar() == LyXFont::ON) {
|
|
|
|
underline(f, x, y, font_metrics::width(s, ls, f));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2006-06-25 11:18:56 +00:00
|
|
|
|
|
|
|
|
2006-03-20 10:43:21 +00:00
|
|
|
void QLPainter::drawPixmap(int x, int y, QPixmap const & pixmap)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-06-23 14:12:17 +00:00
|
|
|
qp_->drawPixmap(x, y, pixmap);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
2006-03-20 17:25:02 +00:00
|
|
|
|
2006-06-25 11:18:56 +00:00
|
|
|
|
2006-03-20 17:25:02 +00:00
|
|
|
void QLPainter::drawImage(int x, int y, QImage const & image)
|
|
|
|
{
|
2006-06-23 14:12:17 +00:00
|
|
|
qp_->drawImage(x, y, image);
|
2006-03-20 17:25:02 +00:00
|
|
|
}
|
2006-06-20 08:39:16 +00:00
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|