Skip to content

Commit

Permalink
(work in progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
peteschaefer committed Apr 7, 2024
1 parent 891ba1c commit a1c1f55
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/platform/qt/graphic_qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Font_qt::Font_qt(const string& family, int style, float size) {

_font.setBold(style & BOLD);
_font.setItalic(style & ITALIC);
_metrics.reset(new QFontMetricsF(_font));
}

Font_qt::Font_qt(const string& file, float size)
Expand All @@ -69,6 +70,7 @@ Font_qt::Font_qt(const string& file, float size)
#ifdef HAVE_LOG
__log << file << " already loaded, skip\n";
#endif
_metrics.reset(new QFontMetricsF(_font));
return;
}

Expand All @@ -89,6 +91,7 @@ Font_qt::Font_qt(const string& file, float size)
#endif
}
}
_metrics.reset(new QFontMetricsF(_font));
}

string Font_qt::getFamily() const {
Expand Down
5 changes: 5 additions & 0 deletions src/platform/qt/graphic_qt.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Font_qt : public Font {

private:
QFont _font;
sptr<QFontMetricsF> _metrics;

static QMap<QString, QString> _loaded_families;

Expand All @@ -38,6 +39,10 @@ class Font_qt : public Font {

QFont getQFont() const;

qreal getAscent() const {
return _metrics->ascent();
}

virtual float getSize() const override;

virtual sptr<Font> deriveFont(int style) const override;
Expand Down
69 changes: 66 additions & 3 deletions src/platform/qt/graphic_view_qt.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,78 @@ namespace tex
_transform.m31()+dx*_sx, _transform.m32()+dy*_sy, _transform.m33() );
}

void rotate(float angle) override {
//_painter->rotate(qRadiansToDegrees(angle));
_transform.setMatrix(
_sx, _transform.m12(), _transform.m13(),
_transform.m21(), _sy, _transform.m23(),
_transform.m31(), _transform.m32(), _transform.m33() );
}

void rotate(float angle, float px, float py) override {
//_painter->translate(px, py);
//_painter->rotate(qRadiansToDegrees(angle));
//_painter->translate(-px, -py);
_transform.setMatrix(
_sx, _transform.m12(), _transform.m13(),
_transform.m21(), _sy, _transform.m23(),
_transform.m31(), _transform.m32(), _transform.m33() );
}

void drawText(const wstring &t, float x, float y) override {
QGraphicsSimpleTextItem* text = new QGraphicsSimpleTextItem(QString::fromStdWString(t), _group);
QFontMetricsF fmtx(_font->getQFont());
//QFontMetricsF fmtx(_font->getQFont());
text->setFont(_font->getQFont());
text->setPos(x,y-fmtx.ascent()*_sy);
// todo y = baseline. This is not yet precise.
text->setPos(x,y-_font->getAscent()*_sy);
// note: y = baseline
text->setTransform(_transform);
text->setBrush(getQBrush());
_group->addToGroup(text);
}

void drawLine(float x, float y1, float x2, float y2) override {
QGraphicsLineItem* line = new QGraphicsLineItem(x,y1,x2,y2, _group);
line->setPen(makePen());
line->setTransform(_transform);
_group->addToGroup(line);
}

void drawRect(float x, float y, float w, float h) override {
drawRect(x,y,w,h,makePen());
}

void fillRect(float x, float y, float w, float h) override {
drawRect(x,y,w,h,makePen(), getQBrush());
}

void drawRoundRect(float x, float y, float w, float h, float rx, float ry) override {
drawRoundRect(x, y, w, h, rx, ry, makePen());
}

void fillRoundRect(float x, float y, float w, float h, float rx, float ry) override {
drawRoundRect(x, y, w, h, rx, ry, makePen(), getQBrush());
}

private:
void drawRect(float x, float y, float w, float h, QPen pen, QBrush brush = QBrush())
{
QGraphicsRectItem* rect = new QGraphicsRectItem(x,y,w,h);
rect->setPen(pen);
rect->setBrush(brush);
rect->setTransform(_transform);
_group->addToGroup(rect);
}
void drawRoundRect(float x, float y, float w, float h, float rx, float ry, QPen pen, QBrush brush = QBrush())
{
// is a bit more involved. We need the help of a QPaintPath
QPainterPath path;
path.addRoundedRect(x,y,w,h,rx,ry);
QGraphicsPathItem* item = new QGraphicsPathItem(path,_group);
item->setPen(pen);
item->setBrush(brush);
item->setTransform(_transform);
_group->addToGroup(item);
}
};
}
#endif //LATEX_GRAPHIC_VIEW_QT_H
4 changes: 2 additions & 2 deletions src/samples/qt_texgraphicsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ namespace qt_graphics
QGraphicsItem* item = makeGraphicsViewItem(latex,view->width(), 0, text_size);
QRectF bounds = item->boundingRect();
// scale to view
qreal rescale = std::max(view->width()/bounds.width(),view->height()/bounds.height());
item->setScale(rescale);
qreal rescale = std::min(view->width()/bounds.width(),view->height()/bounds.height());
item->setScale(0.9*rescale);
scene->addItem(item);
view->setScene(scene);
}
Expand Down

0 comments on commit a1c1f55

Please sign in to comment.