MiniDisplay/minidisplay.cpp
author sl
Tue, 27 May 2014 22:14:29 +0200
changeset 5 62a1d3631dcb
parent 4 7d34342ac6e9
child 6 b1b049e28772
permissions -rw-r--r--
Adding font property and example C++ code for rendering font in bitmap.
Cleaning up our MiniDisplay signal names.
     1 #include "minidisplay.h"
     2 #include <QPainter>
     3 
     4 MiniDisplay::MiniDisplay(QQuickItem *parent):
     5     QQuickItem(parent)
     6 {
     7     // By default, QQuickItem does not draw anything. If you subclass
     8     // QQuickItem to create a visual item, you will need to uncomment the
     9     // following line and re-implement updatePaintNode()
    10 
    11     // setFlag(ItemHasContents, true);
    12     //
    13     //qDebug() << "New MiniDisplay";
    14 }
    15 
    16 MiniDisplay::~MiniDisplay()
    17 {
    18     //qDebug() << "Delete MiniDisplay";
    19 }
    20 
    21 
    22 void MiniDisplay::open()
    23 {
    24     if (iDisplay.Open())
    25     {
    26         emit opened();
    27         emit statusChanged();
    28     }
    29     else
    30     {
    31         emit openError();
    32     }
    33 }
    34 
    35 
    36 void MiniDisplay::close()
    37 {
    38     iDisplay.Close();
    39     emit closed();
    40     emit statusChanged();
    41 }
    42 
    43 bool MiniDisplay::isOpen()
    44 {
    45     return iDisplay.IsOpen();
    46 }
    47 
    48 void MiniDisplay::clear()
    49 {
    50     if (!iDisplay.IsOpen()) return;
    51     //
    52     iDisplay.Clear();
    53 }
    54 
    55 void MiniDisplay::fill()
    56 {
    57     if (!iDisplay.IsOpen()) return;
    58     //
    59     iDisplay.SetAllPixels(0xFF);
    60 }
    61 
    62 void MiniDisplay::swapBuffers()
    63 {
    64     if (!iDisplay.IsOpen()) return;
    65     //
    66     iDisplay.SwapBuffers();
    67 }
    68 
    69 /**
    70  * @brief setFont
    71  * @param aFont
    72  */
    73 void MiniDisplay::setFont(const QFont& aFont)
    74 {
    75     iFont=aFont;
    76     QString strDemo="0123456789ABCDEF";
    77     QFontMetrics metrics(iFont);
    78     QSize size(metrics.width(strDemo),metrics.height());
    79     QImage image(size,QImage::Format_Mono);
    80     image.fill(0xFFFFFFFF);
    81     {
    82         QPainter painter(&image);
    83         //painter.begin(&image);
    84         painter.setPen(0xFF000000);
    85         painter.setFont(iFont);
    86         painter.drawText(0,metrics.ascent(),strDemo);
    87     }
    88     //painter.end();
    89     image.save("font.png");
    90 }