Update preset.
[bertos.git] / bertos / drv / lcd_gfx_qt.cpp
index 5e862dbb681878b46e47da50a7f0fd1a8f12df1e..69ff66f4c2c14715f9427c2c81e9696a82c3379d 100644 (file)
  * invalidate any other reasons why the executable file might be covered by
  * the GNU General Public License.
  *
- * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
+ * Copyright 2006, 2008 Develer S.r.l. (http://www.develer.com/)
  *
  * -->
  *
- * \version $Id$
- *
  * \author Bernie Innocenti <bernie@codewiz.org>
  *
- * \brief Custom control for graphics LCD emulation (interface)
+ * \brief Custom control for graphics LCD emulation (implementation)
  */
 
 #include "lcd_gfx_qt.h"
 #define LCD_BG_COLOR 0xBB, 0xCC, 0xBB
 
 
-EmulLCD::EmulLCD(QWidget *parent, const char *name) :
-       QFrame(parent, name, Qt::WRepaintNoErase | Qt::WResizeNoErase),
+EmulLCD::EmulLCD(QWidget *parent) :
+       QFrame(parent),
        fg_color(LCD_FG_COLOR),
-       bg_color(LCD_BG_COLOR)
+       bg_brush(QColor(LCD_BG_COLOR))
 {
+       // Optimized rendering: we repaint everything anyway
+       setAttribute(Qt::WA_NoSystemBackground);
+
        // initialize bitmap
        memset(raster, 0xAA, sizeof(raster));
 
        // set widget frame
        setFrameStyle(QFrame::Panel | QFrame::Sunken);
        frame_width = frameWidth();
+
+       setMinimumSize(WIDTH + frame_width * 2, HEIGHT + frame_width * 2);
+
+       #if CONFIG_EMULLCD_SCALE
+               QSizePolicy pol = QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred, QSizePolicy::Frame);
+               pol.setHeightForWidth(true);
+       #else
+               QSizePolicy pol = QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed, QSizePolicy::Frame);
+       #endif
+       setSizePolicy(pol);
 }
 
 
@@ -71,42 +82,55 @@ EmulLCD::~EmulLCD()
        // nop
 }
 
-
-QSizePolicy EmulLCD::sizePolicy() const
+#if CONFIG_EMULLCD_SCALE
+int EmulLCD::heightForWidth(int w) const
 {
-       return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed, false);
-}
+               int h;
 
+               w -= frame_width * 2;
+               h = (w * HEIGHT + WIDTH/2) / WIDTH;
+               h += frame_width * 2;
 
-QSize EmulLCD::sizeHint() const
-{
-       return QSize(
-               WIDTH + frame_width * 2,
-               HEIGHT + frame_width * 2);
+               return h;
 }
-
+#endif // CONFIG_EMULLCD_SCALE
 
 void EmulLCD::paintEvent(QPaintEvent * /*event*/)
 {
        QPainter p(this);
-       QImage img(raster, WIDTH, HEIGHT, 1, NULL, 0, QImage::BigEndian);
+       QImage img(raster, WIDTH, HEIGHT, QImage::Format_Mono);
+
+       #if CONFIG_EMULLCD_SCALE
+               int w = width() - frame_width * 2;
+               int h = height() - frame_width * 2;
+               if ((w != WIDTH) || (h != HEIGHT))
+               {
+                       p.scale((qreal)w / WIDTH, (qreal)h / HEIGHT);
+                       //p.setRenderHint(QPainter::SmoothPixmapTransform);
+               }
+       #endif // CONFIG_EMULLCD_SCALE
 
        p.setBackgroundMode(Qt::OpaqueMode);
+       p.setBackground(bg_brush);
        p.setPen(fg_color);
-       p.setBackgroundColor(bg_color);
-       p.drawImage(frame_width, frame_width, img);
+
+       p.drawImage(QPoint(frame_width, frame_width), img);
 }
 
 void EmulLCD::writeRaster(uint8_t *new_raster)
 {
 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
 
-       /* Straight copy */
-       memcpy(raster, new_raster, sizeof(raster));
+       // Straight copy
+       //memcpy(raster, new_raster, sizeof(raster));
+
+       // Inverting copy
+       for (int i = 0; i < (int)sizeof(raster); ++i)
+               raster[i] = ~new_raster[i];
 
 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
 
-       /* Rotation */
+       // Rotation + inversion
        for (int y = 0; y < HEIGHT; ++y)
        {
                for (int xbyte = 0; xbyte < WIDTH/8; ++xbyte)
@@ -114,12 +138,11 @@ void EmulLCD::writeRaster(uint8_t *new_raster)
                        uint8_t v = 0;
                        for (int xbit = 0; xbit < 8; ++xbit)
                                v |= (new_raster[(xbyte * 8 + xbit) + (y / 8) * WIDTH] & (1 << (y%8)) )
-                                       ? 0 : (1 << (7 - xbit));
+                                       ? (1 << (7 - xbit)) : 0;
 
                        raster[y * ((WIDTH + 7) / 8) + xbyte] = v;
                }
        }
-
 #else
        #error Unsupported bitmap format
 #endif
@@ -142,18 +165,18 @@ DECLARE_WALL(wall_before_raster, WALL_SIZE)
 static uint8_t lcd_raster[RAST_SIZE(EmulLCD::WIDTH, EmulLCD::HEIGHT)];
 DECLARE_WALL(wall_after_raster, WALL_SIZE)
 
-/** Default LCD bitmap */
-struct Bitmap lcd_bitmap;
 
-/*extern "C"*/ void lcd_init(void)
+
+
+/*extern "C"*/ void lcd_gfx_qt_init(Bitmap *lcd_bitmap)
 {
        //FIXME INIT_WALL(wall_before_raster);
        //FIXME INIT_WALL(wall_after_raster);
-       gfx_bitmapInit(&lcd_bitmap, lcd_raster, EmulLCD::WIDTH, EmulLCD::HEIGHT);
-       gfx_bitmapClear(&lcd_bitmap);
+       gfx_bitmapInit(lcd_bitmap, lcd_raster, EmulLCD::WIDTH, EmulLCD::HEIGHT);
+       gfx_bitmapClear(lcd_bitmap);
 }
 
-/*extern "C"*/ void lcd_blitBitmap(Bitmap *bm)
+/*extern "C"*/ void lcd_gfx_qt_blitBitmap(const        Bitmap *bm)
 {
        //FIXME CHECK_WALL(wall_before_raster);
        //FIXME CHECK_WALL(wall_after_raster);