Added an "hadjust" XML attribute to title block cells, allowing too long texts to be reduced until they fit the cell.

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/branches/0.3@1150 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
xavier 2011-02-08 06:53:47 +00:00
parent 1cbbe67520
commit a3b1ac7b74
6 changed files with 282 additions and 125 deletions

File diff suppressed because one or more lines are too long

View File

@ -476,19 +476,20 @@ bool QETApp::closeEveryEditor() {
La famille "Sans Serif" est utilisee par defaut mais peut etre surchargee
dans la configuration (diagramfont).
*/
QFont QETApp::diagramTextsFont(int size) {
QFont QETApp::diagramTextsFont(qreal size) {
// acces a la configuration de l'application
QSettings &qet_settings = QETApp::settings();
// police a utiliser pour le rendu de texte
QString diagram_texts_family = qet_settings.value("diagramfont", "Sans Serif").toString();
int diagram_texts_size = qet_settings.value("diagramsize", 9).toInt();
qreal diagram_texts_size = qet_settings.value("diagramsize", 9.0).toDouble();
if (size != -1) {
if (size != -1.0) {
diagram_texts_size = size;
}
QFont diagram_texts_font = QFont(diagram_texts_family, diagram_texts_size);
if (diagram_texts_size <= 4) {
QFont diagram_texts_font = QFont(diagram_texts_family);
diagram_texts_font.setPointSizeF(diagram_texts_size);
if (diagram_texts_size <= 4.0) {
diagram_texts_font.setWeight(QFont::Light);
}
return(diagram_texts_font);

View File

@ -93,7 +93,7 @@ class QETApp : public QETSingleApplication {
public:
static void overrideLangDir(const QString &);
static QString lang_dir; ///< Dossier contenant les fichiers de langue
static QFont diagramTextsFont(int = -1);
static QFont diagramTextsFont(qreal = -1.0);
static QETDiagramEditor *diagramEditorForFile(const QString &);
static QList<QETDiagramEditor *> diagramEditors();
static QList<QETElementEditor *> elementEditors();

View File

@ -37,6 +37,7 @@ class TitleBlockCell {
bool display_label;
int alignment;
int font_size;
bool hadjust;
QString logo_reference;
};
#endif

View File

@ -292,6 +292,9 @@ bool TitleBlockTemplate::loadCells(const QDomElement &xml_element) {
if (halignment == "bottom") loaded_cell -> alignment |= Qt::AlignBottom;
else if (halignment == "top") loaded_cell -> alignment |= Qt::AlignTop;
else loaded_cell -> alignment |= Qt::AlignVCenter;
// horizontal text adjustment
loaded_cell -> hadjust = cell_element.attribute("hadjust", "true") == "true";
}
}
}
@ -525,8 +528,8 @@ void TitleBlockTemplate::render(QPainter &painter, const DiagramContext &diagram
painter.drawPixmap(cell_rect, *(bitmap_logos_[cells_[i][j].logo_reference]));
}
} else {
painter.setFont(cells_[i][j].font_size == -1 ? QETApp::diagramTextsFont() : QETApp::diagramTextsFont(cells_[i][j].font_size));
painter.drawText(cell_rect, cells_[i][j].alignment, finalTextForCell(cells_[i][j], diagram_context));
QString final_text = finalTextForCell(cells_[i][j], diagram_context);
renderTextCell(painter, final_text, cells_[i][j], cell_rect);
}
// draw again the border rect of the current cell, without the brush this time
@ -556,6 +559,47 @@ QString TitleBlockTemplate::finalTextForCell(const TitleBlockCell &cell, const D
return(cell_text);
}
/**
This method uses a \a painter to render the \a text of a \a cell
into the \a cell_rect rectangle.
The alignment, font_size and other cell parameters are taken into account
when rendering.
@param painter QPainter used to render the text
@param text Text to render
@param cell Cell the rendered text is rattached to
@param cell_rect Rectangle delimiting the cell area
*/
void TitleBlockTemplate::renderTextCell(QPainter &painter, const QString &text, const TitleBlockCell &cell, const QRectF &cell_rect) const {
QFont text_font = cell.font_size == -1 ? QETApp::diagramTextsFont() : QETApp::diagramTextsFont(cell.font_size);
painter.setFont(text_font);
if (cell.hadjust) {
QFontMetricsF font_metrics(text_font);
QRectF font_rect = font_metrics.boundingRect(QRect(-10000, -10000, 10000, 10000), cell.alignment, text);
if (font_rect.width() > cell_rect.width()) {
qreal ratio = qreal(cell_rect.width()) / qreal(font_rect.width());
painter.save();
painter.translate(cell_rect.topLeft());
qreal vertical_adjustment = cell_rect.height() * (1 - ratio) / 2.0;
painter.translate(0.0, vertical_adjustment);
painter.scale(ratio, ratio);
QRectF new_world_cell_rect(cell_rect);
new_world_cell_rect.moveTo(0, 0.0);
new_world_cell_rect.setWidth(new_world_cell_rect.width() / ratio);
painter.drawText(new_world_cell_rect, cell.alignment, text);
painter.restore();
return;
}
}
// Still here? Let's draw the text normally
painter.drawText(cell_rect, cell.alignment, text);
}
/**
@return the width between two borders
@param start start border number

View File

@ -78,6 +78,7 @@ class TitleBlockTemplate : public QObject {
void initCells();
int lengthRange(int, int, const QList<int> &) const;
QString finalTextForCell(const TitleBlockCell &, const DiagramContext &) const;
void renderTextCell(QPainter &, const QString &, const TitleBlockCell &, const QRectF &) const;
// attributs
private: