MiniDisplay/minidisplay.cpp
author sl
Wed, 28 May 2014 11:42:49 +0200
changeset 8 9543e1703afb
parent 6 b1b049e28772
child 9 52372bbbc0f8
permissions -rw-r--r--
Adding status bar to display vendor and product strings.
     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     iFont.setStyleStrategy(QFont::NoAntialias);
    77     QString strDemo="0123456789ABCDEF";
    78     QFontMetrics metrics(iFont);
    79     int w=metrics.width(strDemo);
    80     int h=metrics.height();
    81     QSize size(w,h);
    82     QImage image(size,QImage::Format_Mono);
    83     image.fill(0xFFFFFFFF);
    84     //Draw some text into our image
    85     {
    86         QPainter painter(&image);
    87         //painter.begin(&image);
    88         painter.setPen(0xFF000000);
    89         painter.setFont(iFont);
    90         painter.drawText(0,metrics.ascent(),strDemo);
    91     }
    92     //Save image as PNG for validation
    93     image.save("font.png");
    94     //
    95     //int sizeInBytes=image.byteCount();
    96     int pixelCount=w*h;
    97     BitArray bits(pixelCount);
    98 
    99     for (int i=0;i<w;i++)
   100         {
   101         for (int j=0;j<h;j++)
   102             {
   103             QRgb color=image.pixel(i,j);
   104             if (color!=0xffffffff)
   105                 {
   106                 bits.SetBit(i*h+j);
   107                 }
   108             }
   109         }
   110 
   111     if (iDisplay.IsOpen())
   112     {
   113         iDisplay.Clear();
   114         iDisplay.BitBlit(bits,w,h,0,0);
   115         iDisplay.SwapBuffers();
   116     }
   117     //
   118 }
   119 
   120 
   121 QString MiniDisplay::vendor()
   122 {
   123     return QString::fromWCharArray(iDisplay.Vendor());
   124 }
   125 
   126 QString MiniDisplay::product()
   127 {
   128     return QString::fromWCharArray(iDisplay.Product());
   129 }
   130 
   131 QString MiniDisplay::serialNumber()
   132 {
   133     return QString::fromWCharArray(iDisplay.SerialNumber());
   134 }
   135 
   136