Inheritance and using the custom wrapper widget 2
using: previous QEAppTemplate and @qelib fixed loss class updated : 2007/09/24.
After we have the qexwidget class already, Now try to create other.
In this example, i'll show the QELabel and the QELabelButton which i used istead of the QLabel widget and QPushButton since it can not work on my phone (or can run but not stable).
The QELabel is inherited from the QEXWidget, and the QELabelButton is inherited from QELabel.
You can see this 2 widget in the main page of the Setting Options program for qeiLock.
again, difficult to explain... the code should explain itself better than me 
QELabel class - qelabel.h
Code:
/***************************************************************************
* *
* (c) 2007, Thanomsub Noppaburana <donga.nb@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef __QE_LABEL_H__
#define __QE_LABEL_H__
#include <qpainter.h>
#include "qeobject.h"
#include "qexwidget.h"
/**
*****************************************************************************
class QELabel definition
*****************************************************************************
*/
class QELabel : public QEXWidget
{
Q_OBJECT
public:
QELabel(const QString &, QWidget *parent, const char *name = 0);
virtual ~QELabel();
virtual void setText(const QString &);
virtual QString getText() const { return m_text; };
virtual void setBoldFont(bool bold);
virtual void setDrawingBGStyle(const DrawingStyle &ds = DS_Normal, bool boldFont = FALSE);
protected:
/* event handling */
virtual bool paintEvent(QPaintEvent *);
protected:
QFont *m_drawingFont;
QFontMetrics *m_fontMetrics;
QString m_text;
int m_margin;
int m_fontPointSize;
int m_fontHeight;
bool isBoldFont;
};
#endif //__QE_LABEL_H__
QELabel class - qelabel.cpp
Code:
/***************************************************************************
* *
* (c) 2007, Thanomsub Noppaburana <donga.nb@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "qeglobal.h"
#include "qelabel.h"
/**
*****************************************************************************
class QELabel implementation
*****************************************************************************
*/
QELabel::QELabel(const QString &txt, QWidget *parent, const char *name)
: QEXWidget(parent, name),
m_drawingFont(0), m_fontMetrics(0), m_text(txt), m_margin(3), m_fontHeight(0),
isBoldFont(FALSE)
{
m_text = txt;
m_fontPointSize = 14;
m_drawingFont = new QFont("SSong", m_fontPointSize);
QFontMetrics fm(*m_drawingFont);
m_fontHeight = fm.height();
m_height = fm.height() + (m_margin*2);
setGeometry(0, 0, 240, m_height);
//setNoBackgroundMode(FALSE);
hookEvents(TRUE);
}
QELabel::~QELabel()
{
if (m_drawingFont) delete m_drawingFont;
hookEvents(FALSE);
}
void QELabel::setText(const QString &txt)
{
if (m_text == txt) return;
m_text = txt;
setDrawingBGStyle(drawingStyle());
QFontMetrics fm(*m_drawingFont);
m_height = fm.height() + (m_margin*2);
m_fontHeight = fm.height();
resize(m_width, m_height);
widget()->setFont(*m_drawingFont);
widget()->update();
}
void QELabel::setBoldFont(bool bold)
{
if (isBoldFont == bold) return;
isBoldFont = bold;
m_drawingFont->setBold(isBoldFont);
}
void QELabel::setDrawingBGStyle(const DrawingStyle &ds, bool boldFont)
{
QEXWidget::setDrawingBGStyle(ds);
m_drawingFont->setBold(boldFont);
switch (ds) {
case DS_ScreenTitle:
m_fontPointSize += 2;
m_drawingFont->setBold(FALSE);
break;
case DS_DialogTitle:
m_fontPointSize += 2;
m_drawingFont->setBold(FALSE);
break;
case DS_Normal:
case DS_HighlightTextBackground:
m_drawingFont->setBold(FALSE);
break;
case DS_CSTBar:
case DS_Dialog_CSTBar:
m_fontPointSize += 2;
m_drawingFont->setBold(TRUE);
break;
}
m_drawingFont->setPointSize(m_fontPointSize);
QFontMetrics fm(*m_drawingFont);
//m_width = (drawingStyle() == DS_Normal) ? fm.width(m_text) : width();
//m_width = width();
m_height = fm.height() + (m_margin*2);
m_fontHeight = fm.height();
resize(m_width, m_height);
widget()->setFont(*m_drawingFont);
widget()->update();
}
bool QELabel::paintEvent(QPaintEvent *)
{
QPainter p(widget());
p.drawPixmap(0, 0, QEGlobal::getDrawingBGPixmap(drawingStyle()));
DrawingStyle ds = drawingStyle();
QColor penColor = ((ds!=DS_Normal) || (ds!=DS_HighlightTextBackground)) ? white : gray;
p.setPen(penColor);
//p.drawText(m_margin, height() - m_fontHeight - m_margin, width()-m_margin, height()-m_margin, AlignLeft, m_text);
p.drawText(m_margin, m_margin, width()-m_margin, height()-m_margin, AlignLeft, m_text);
p.end();
return TRUE;
}
QELabelButton class - qelabelbutton.h
Code:
/***************************************************************************
* *
* (c) 2007, Thanomsub Noppaburana <donga.nb@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef __QE_LABELBUTTON_H__
#define __QE_LABELBUTTON_H__
#include <qpainter.h>
#include <qwidget.h>
#include "qeobject.h"
#include "qelabel.h"
/**
*****************************************************************************
class QELabelButton definition
*****************************************************************************
*/
class QELabelButton : public QELabel
{
Q_OBJECT
public:
QELabelButton(const QString &text, QWidget *parent, const char *name = 0);
QELabelButton(const char *name = 0);
virtual ~QELabelButton();
virtual void setText(const QString &);
int cursorX() { return m_mouseX; };
int cursorY() { return m_mouseY; };
QPoint cursorPos() { return QPoint(m_mouseX, m_mouseY); };
bool isOnPressed() { return m_isMousePressed; };
signals:
void mousePressed(QPoint globalPoint);
void mouseReleased(QPoint globalPoint);
void mouseMoved(QPoint globalPoint);
void mouseDblClicked(QPoint globalPoint);
void clicked();
void doubleClicked();
protected:
void generateBG();
/* event handling */
virtual bool mousePressEvent(QMouseEvent *);
virtual bool mouseReleaseEvent(QMouseEvent *);
virtual bool mouseMoveEvent(QMouseEvent *);
virtual bool mouseDblClickEvent(QMouseEvent *);
virtual bool mouseEnterEvent(QMouseEvent *);
virtual bool mouseLeaveEvent(QMouseEvent *);
virtual bool paintEvent(QPaintEvent *);
private:
QImage bufIMG;
QImage *m_imgbtnUnpressBG;
QImage *m_imgbtnPressBG;
bool m_isMousePressed;
bool m_isMouseReleased;
bool m_isMouseInRegion;
int m_mouseX;
int m_mouseY;
};
#endif //__QE_LABELBUTTON_H__
QELabelButton class - qelabelbutton.cpp
Code:
/***************************************************************************
* *
* (c) 2007, Thanomsub Noppaburana <donga.nb@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "qeglobal.h"
#include "qelabelbutton.h"
/**
*****************************************************************************
class QELabelButton implementation
*****************************************************************************
*/
QELabelButton::QELabelButton(const QString &text, QWidget *parent, const char *name)
: QELabel(text, parent, name),
m_imgbtnUnpressBG(0), m_imgbtnPressBG(0),
m_isMousePressed(FALSE), m_isMouseReleased(TRUE), m_isMouseInRegion(FALSE),
m_mouseX(0), m_mouseY(0)
{
bufIMG = QEGlobal::getSkinPixmap("General_Stretch_Button_S.g");
//bufIMG.setAlphaBuffer(TRUE);
setBackgroundMode(QWidget::NoBackground);
}
QELabelButton::QELabelButton(const char *name)
: QELabel("Button", 0, name),
m_imgbtnUnpressBG(0), m_imgbtnPressBG(0),
m_isMousePressed(FALSE), m_isMouseReleased(TRUE), m_isMouseInRegion(FALSE),
m_mouseX(0), m_mouseY(0)
{
bufIMG = QEGlobal::getSkinPixmap("General_Stretch_Button_S.g");
//bufIMG.setAlphaBuffer(TRUE);
//setBackgroundPixmap(getSkinPixmap("General_Stretch_Button_S.g"));
setBackgroundMode(QWidget::NoBackground);
}
QELabelButton::~QELabelButton()
{
/*m_imgbtnUnpressBG->reset();
m_imgbtnPressBG->reset();
delete m_imgbtnUnpressBG;
delete m_imgbtnPressBG;
*/
}
void QELabelButton::setText(const QString &txt)
{
if (m_text == txt) return;
m_text = txt;
widget()->update();
/* setDrawingBGStyle(drawingStyle());
QFontMetrics fm(*m_drawingFont);
m_height = fm.height() + (m_margin*2);
m_fontHeight = fm.height();
resize(m_width, m_height);
widget()->setFont(*m_drawingFont);
widget()->update();
*/
}
void QELabelButton::generateBG()
{
//m_imgbtnUnpressBG = new QImage(
//m_imgbtnPressBG->reset();
//setBackgroundPixmap(getSkinPixmap("General_Stretch_Button_S.g"));
}
bool QELabelButton::paintEvent(QPaintEvent *ev)
{
/* QPixmap pixBG;
pixBG.convertFromImage(bufIMG.smoothScale(bufIMG.width(), height()));
QPixmap pixMidBG(pixBG.width()-6, height());
bitBlt(&pixMidBG, 0, 0, &pixBG, 3, 0, pixBG.width()-6, height());
QImage imgTmp;
imgTmp = pixMidBG;
pixMidBG.convertFromImage(imgTmp.smoothScale(width()-6, height()));
QPixmap buf2(width(), height());
bitBlt(&buf2, 0, 0, &pixBG, 0, 0, 3, height());
bitBlt(&buf2, width()-3, 0, &pixBG, pixBG.width()-3, 0, 3, height());
bitBlt(&buf2, 3, 0, &pixMidBG, 0, 0, width()-6, height());
QImage imageBG;
imageBG = buf2;
imageBG.setAlphaBuffer(TRUE);
QPainter p(widget());
//p.drawPixmap(0, 0, buf2);
p.drawImage(0, 0, m_isMousePressed ? imageBG : imageBG.mirror(), 0, 0, -1, -1, OrderedAlphaDither);
p.end();
*/
QPainter p(widget());
QBrush brush(m_isMousePressed ? gray : darkGray);
p.fillRect(0, 0, width(), height(), brush);
p.drawRect(0, 0, width(), height());
p.drawPixmap(0, 0, QEGlobal::getDrawingBGPixmap(drawingStyle()));
DrawingStyle ds = drawingStyle();
QColor penColor = ((ds!=DS_Normal) || (ds!=DS_HighlightTextBackground)) ? white : gray;
p.setPen(penColor);
p.drawText(m_margin, m_margin, width()-m_margin, height()-m_margin, AlignCenter | AlignVCenter, m_text);
p.end();
return TRUE;
}
bool QELabelButton::mousePressEvent(QMouseEvent *ev)
{
m_isMouseInRegion = m_isMousePressed = TRUE;
widget()->update();
m_mouseX = ev->globalPos().x();
m_mouseY = ev->globalPos().y();
emit mousePressed(ev->globalPos());
return TRUE;
}
bool QELabelButton::mouseReleaseEvent(QMouseEvent *ev)
{
m_isMousePressed = FALSE;
widget()->update();
m_mouseX = ev->globalPos().x();
m_mouseY = ev->globalPos().y();
emit mouseReleased(ev->globalPos());
if (m_isMouseInRegion) {
emit clicked();
}
m_isMouseInRegion = FALSE;
return TRUE;
}
bool QELabelButton::mouseMoveEvent(QMouseEvent *ev)
{
if (!m_isMousePressed) return TRUE;
m_mouseX = ev->globalPos().x();
m_mouseY = ev->globalPos().y();
emit mouseMoved(ev->globalPos());
return TRUE;
}
bool QELabelButton::mouseDblClickEvent(QMouseEvent *ev)
{
m_isMousePressed = FALSE;
m_mouseX = ev->globalPos().x();
m_mouseY = ev->globalPos().y();
emit mouseDblClicked(ev->globalPos());
emit doubleClicked();
m_isMouseInRegion = FALSE;
return TRUE;
}
bool QELabelButton::mouseEnterEvent(QMouseEvent *)
{
m_isMouseInRegion = TRUE;
return FALSE;
}
bool QELabelButton::mouseLeaveEvent(QMouseEvent *)
{
m_isMouseInRegion = FALSE;
return FALSE;
}
Testing it
Well, after have [b]QEXWidget, QELabel, QELabelButton[/] class already, lets links (or you may copy) it
to the "lib/" of the QEAppTemplate example for testing.
now in the "lib" folder of the QEAppTemplate should have following files:
Code:
lib/qeconfig.cpp
lib/qeobject.cpp
lib/qeglobal.h
lib/qeconfig.h
lib/qeapplication.h
lib/qewidget.h
lib/qelabel.cpp
lib/qeapplication.cpp
lib/qeglobal.cpp
lib/qexwidget.cpp
lib/qeobject.h
lib/qexwidget.h
lib/qepixmap_inc.h
lib/qemainwidget.cpp
lib/qewidget.cpp
lib/qelabelbutton.h
lib/qemainwidget.h
lib/qelabelbutton.cpp
lib/qelabel.h
Now, open the editor for edit the old qeapptemplate.cpp to:
Code:
/***************************************************************************
* *
* (c) 2007, Thanomsub Noppaburana <donga.nb@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "lib/qelabel.h"
#include "lib/qelabelbutton.h"
#include "qeapptemplate.h"
/**
*****************************************************************************
QEAppTemplate class implementation
*****************************************************************************
*/
QEAppTemplate::QEAppTemplate(const char *name)
: QEMainWidget(0, name)
{
}
QEAppTemplate::~QEAppTemplate()
{
printf("(%s)->QEAppTemplate<-::destroy()\n", name());
}
/** GUI creation */
void QEAppTemplate::setupGUI()
{
/* Show normal mode */
showNormalMode(TRUE, TRUE);
/* Setup CST Bar */
getCSTBarWidget(TRUE);
ZPushButton *btn = CSTwidget()->getRightBtn();
connect(btn, SIGNAL(clicked()), qApp, SLOT(slotQuickQuit()));
/* Set Title text */
((QLabel*)getTitleBarWidget())->setText("QE App Template");
/* Get Content widget */
getContentWidget(TRUE)->resize(m_contentWidth, m_contentHeight);
}
void QEAppTemplate::setupContent()
{
QELabel *label1 = new QELabel("TEST LABEL 1", contentWidget(), "lbl1");
((QScrollView*)contentWidget())->addChild(label1->widget());
label1->show();
QELabel *label2 = new QELabel("TEST LABEL 2", contentWidget(), "lbl2");
((QScrollView*)contentWidget())->addChild(label2->widget(), 0, label1->height());
label2->show();
QELabelButton *labelBtn = new QELabelButton("TEST LABEL Button", contentWidget(), "lblBtn");
((QScrollView*)contentWidget())->addChild(labelBtn->widget(), 0, label1->height() + label2->height());
labelBtn->show();
}
If done, lets compile it, strip and transfer the last build to the phone on SD card again for testing.
The result should be:

Hope this would be guide you for make the other better widget for ours