Fix size hinting for LCD widget
[bertos.git] / bertos / drv / lcd_gfx_qt.cpp
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id$
34  *
35  * \author Bernie Innocenti <bernie@codewiz.org>
36  *
37  * \brief Custom control for graphics LCD emulation (interface)
38  */
39
40 #include "lcd_gfx_qt.h"
41 #include <emul/emul.h>
42 #include <cfg/debug.h>
43 #include <gfx/gfx.h> // CONFIG_BITMAP_FMT
44
45 #include <QtGui/QPainter>
46 #include <QtGui/QImage>
47 #include <QtGui/QSizePolicy>
48 #include <QtCore/QSize>
49
50 // Display colors
51 #define LCD_FG_COLOR 0x0, 0x0, 0x0
52 #define LCD_BG_COLOR 0xBB, 0xCC, 0xBB
53
54
55 EmulLCD::EmulLCD(QWidget *parent, const char *name) :
56         QFrame(parent, name, Qt::WRepaintNoErase | Qt::WResizeNoErase),
57         fg_color(LCD_FG_COLOR),
58         bg_color(LCD_BG_COLOR)
59 {
60         // initialize bitmap
61         memset(raster, 0xAA, sizeof(raster));
62
63         // set widget frame
64         setFrameStyle(QFrame::Panel | QFrame::Sunken);
65         frame_width = frameWidth();
66 }
67
68
69 EmulLCD::~EmulLCD()
70 {
71         // nop
72 }
73
74
75 QSizePolicy EmulLCD::sizePolicy() const
76 {
77         return QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed, false);
78 }
79
80
81 QSize EmulLCD::sizeHint() const
82 {
83         return QSize(
84                 WIDTH + frame_width * 2,
85                 HEIGHT + frame_width * 2);
86 }
87
88 QSize EmulLCD::minimumSizeHint() const
89 {
90         return sizeHint();
91 }
92
93 void EmulLCD::paintEvent(QPaintEvent * /*event*/)
94 {
95         QPainter p(this);
96         QImage img(raster, WIDTH, HEIGHT, 1, NULL, 0, QImage::BigEndian);
97
98         p.setBackgroundMode(Qt::OpaqueMode);
99         p.setPen(fg_color);
100         p.setBackgroundColor(bg_color);
101         p.drawImage(frame_width, frame_width, img);
102 }
103
104 void EmulLCD::writeRaster(uint8_t *new_raster)
105 {
106 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
107
108         /* Straight copy */
109         memcpy(raster, new_raster, sizeof(raster));
110
111 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
112
113         /* Rotation */
114         for (int y = 0; y < HEIGHT; ++y)
115         {
116                 for (int xbyte = 0; xbyte < WIDTH/8; ++xbyte)
117                 {
118                         uint8_t v = 0;
119                         for (int xbit = 0; xbit < 8; ++xbit)
120                                 v |= (new_raster[(xbyte * 8 + xbit) + (y / 8) * WIDTH] & (1 << (y%8)) )
121                                         ? 0 : (1 << (7 - xbit));
122
123                         raster[y * ((WIDTH + 7) / 8) + xbyte] = v;
124                 }
125         }
126
127 #else
128         #error Unsupported bitmap format
129 #endif
130
131         repaint();
132 }
133
134
135
136 #include <gfx/gfx.h>
137 #include <cfg/debug.h>
138
139 DECLARE_WALL(wall_before_raster, WALL_SIZE)
140 /**
141  * Raster buffer to draw into.
142  *
143  * Bits in the bitmap bytes have vertical orientation,
144  * as required by the LCD driver.
145  */
146 static uint8_t lcd_raster[RAST_SIZE(EmulLCD::WIDTH, EmulLCD::HEIGHT)];
147 DECLARE_WALL(wall_after_raster, WALL_SIZE)
148
149 /** Default LCD bitmap */
150 struct Bitmap lcd_bitmap;
151
152 /*extern "C"*/ void lcd_init(void)
153 {
154         //FIXME INIT_WALL(wall_before_raster);
155         //FIXME INIT_WALL(wall_after_raster);
156         gfx_bitmapInit(&lcd_bitmap, lcd_raster, EmulLCD::WIDTH, EmulLCD::HEIGHT);
157         gfx_bitmapClear(&lcd_bitmap);
158 }
159
160 /*extern "C"*/ void lcd_blitBitmap(Bitmap *bm)
161 {
162         //FIXME CHECK_WALL(wall_before_raster);
163         //FIXME CHECK_WALL(wall_after_raster);
164         emul->emulLCD->writeRaster(bm->raster);
165 }
166
167 #include "lcd_gfx_qt_moc.cpp"
168