lfpport_qte_choicegroup.cpp
来自「This is a resource based on j2me embedde」· C++ 代码 · 共 2,552 行 · 第 1/5 页
CPP
2,552 行
/* * * * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * 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 version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * * This source file is specific for Qt-based configurations. *//** * @file * Qt port of ChoiceGroup. */#include "lfpport_qte_mscreen.h"#include <qnamespace.h>#include "lfpport_qte_util.h"#include <gxpportqt_image.h>#include <lfpport_form.h>#include "lfpport_qte_choicegroup.h"#include <moc_lfpport_qte_choicegroup.cpp>/** * Static pointer to hold active popup button that cannot be deleted. * This popup button is a child of mscreen. It will either be * reused by new popup, or deleted when mscreen is deleted. */static PopupBody *cachedPopupBody;/** * Draw an choice element in a list. * We draw choices ourselves to support text wrapping policy and images. * * @param p QPainter of the choice list * @param x upper left corner X of the drawable area * @param y upper left corner Y of the drawable area * @param w width of the drawable area * @param h height of the drawable area * @param hPad horizontal padding size * @param vPad vertical padding size * @param string the text portion of this choice element * @param pixmap the image portion of this choice element * @param font font to used for the text * @param fitPolicy wrapping or not wrapping */void drawElement(QPainter *p, int x, int y, int w, int h, int hPad, int vPad, QString string, QPixmap *pixmap, QFont font, int fitPolicy) { x += hPad/2; y += vPad/2; w -= hPad; h -= vPad; p->setFont(font); int imgWidth = 0; if (pixmap != NULL) { if (string.isEmpty()) { p->drawPixmap(x, y, *pixmap); } else { imgWidth = pixmap->width(); if (imgWidth > PREF_IMG_WIDTH) { imgWidth = PREF_IMG_WIDTH; } p->drawPixmap(x, y, *pixmap, 0, 0, imgWidth, pixmap->height()); imgWidth += 3; } } p->drawText(x + imgWidth, y, w - imgWidth, h, fitPolicy == TEXT_WRAP_OFF ? Qt::AlignLeft | Qt::AlignTop : Qt::AlignLeft | Qt::AlignTop | Qt::WordBreak, string);}/** * Calculate size of an element. * * @param string the text portion of this choice element * @param pixmap the image portion of this choice element * @param font font to used for the text * @param fitPolicy wrapping or not wrapping * @param parentWidth maximum width allowed * @param hPad horizontal padding size * @param vPad vertical padding size * @return size of an element with the given contents */ QSize sizeElement(QString string, QPixmap *pixmap, QFont font, int fitPolicy, int parentWidth, int hPad, int vPad) { int x = hPad/2; int y = vPad/2; int w = parentWidth - hPad; int h = qteapp_get_mscreen()->getScreenHeight() - vPad; int imgWidth = 0; if (pixmap != NULL) { imgWidth = pixmap->width(); if (imgWidth > PREF_IMG_WIDTH) { imgWidth = PREF_IMG_WIDTH; } if (!string.isEmpty()) { imgWidth += 3; // pad between image and text } } QRect rect = QFontMetrics(font).boundingRect(x+imgWidth, y, w-imgWidth, h, fitPolicy == TEXT_WRAP_OFF ? Qt::AlignLeft | Qt::AlignTop : Qt::AlignLeft | Qt::AlignTop | Qt::WordBreak, string); QSize s = rect.size(); s.rwidth() += hPad; s.rheight() += vPad; if (pixmap != NULL) { if (string.isEmpty()) { s.setHeight(PREF_IMG_HEIGHT); } s.rwidth() += imgWidth; } return s;}/** * Construct a ChoiceGroup base object. * * @param parent parent widget pointer * @param label label string * @param layout layout directive associated with this item * @param fitPolicy wrapping or not wrapping * @param alwaysVertLayout whether label and body should always be on * separated lines */Choice::Choice(QWidget *parent, const QString &label, int layout, int fitPolicy, bool alwaysVertLayout) : Item(parent, label, layout, alwaysVertLayout) { this->fitPolicy = fitPolicy;}/** * Destruct a ChoiceGroup base object. */Choice::~Choice() {}/** * Process events for this ChoiceGroup widget. * Since Qt uses SIGNAL/SLOT to deliver events, this function is not used. * Do nothing here. * * @param eventPtr event * @return true if the event is fully handled. */jboolean Choice::handleEvent(QEvent *eventPtr) { /* Suppress unused-parameter warning */ (void)eventPtr; return KNI_TRUE;}/** * Construct a customized button that supports text wrapping and image. * * @param str label text of this button * @param img image of this button * @param parent parent widget pointer * @param isExclusive whether it is part of an exclusive button group * @param fitPolicy wrapping or not wrapping the label text */ChoiceButton::ChoiceButton(const QString &str, QPixmap* img, QWidget *parent, bool isExclusive, int fitPolicy) : QButton( parent, 0, WRepaintNoErase | WResizeNoErase | WMouseNoMask ) { this->isExclusive = isExclusive; this->fitPolicy = fitPolicy; setToggleButton( TRUE );// Creation of the QSizePolicy object causes a segmentation violation if// running in debug mode on device. So comment out this code.// setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) ); setText(str); setImage(img); connect(this, SIGNAL(clicked()), this, SLOT(setFocus()));}/** Destruct a ChoiceButton. */ChoiceButton::~ChoiceButton() {}/** * Override QButton to notify Form of focus change. * * @param event focus event to handle */void ChoiceButton::focusInEvent(QFocusEvent *event) { // Notify Java if this is caused by user action MidpFormFocusChanged(parent()->parent()); // Continue with focus activation QButton::focusInEvent(event);}/** * Override QButton to notify the button group of status change. * * @param mouseEvent key event */void ChoiceButton::mouseReleaseEvent(QMouseEvent *mouseEvent) { bool curState = QButton::isOn(); QButton::mouseReleaseEvent(mouseEvent); // Notify Java of a state change if the state changed // after user interaction if (curState != QButton::isOn()) { notifyStateChanged(); }}/** * Override QButton to notify the item has to be traversed out. * * @param keyEvent key event */void ChoiceButton::keyPressEvent(QKeyEvent *keyEvent) { ChoiceButtonBoxBody *qGroup = (ChoiceButtonBoxBody *)QButton::parentWidget(); switch(keyEvent->key()) {#ifdef QT_KEYPAD_MODE case Qt::Key_Select:#endif case Qt::Key_Return: { if (isExclusive) { // radio button if (qGroup->selected() != this) { int id = qGroup->id(this); if (id != -1) { qGroup->setButton(id); notifyStateChanged(); } } } else { // check box QButton::setOn(!QButton::isOn()); notifyStateChanged(); } } break; case Qt::Key_Up: case Qt::Key_Left: if (qGroup->find(0)->hasFocus()) { PlatformMScreen * mscreen = PlatformMScreen::getMScreen(); mscreen->keyPressEvent(keyEvent); } else { QButton::keyPressEvent(keyEvent); } break; case Qt::Key_Down: case Qt::Key_Right: if (qGroup->find(qGroup->count() - 1)->hasFocus()) { PlatformMScreen * mscreen = PlatformMScreen::getMScreen(); mscreen->keyPressEvent(keyEvent); } else { QButton::keyPressEvent(keyEvent); } break; default: QButton::keyPressEvent(keyEvent); break; } }/** * Override QButton to notify the item has been traversed out. * * @param keyEvent key event */void ChoiceButton::keyReleaseEvent(QKeyEvent *keyEvent) { ChoiceButtonBoxBody *qGroup = (ChoiceButtonBoxBody *)QButton::parentWidget(); switch(keyEvent->key()) { case Qt::Key_Up: case Qt::Key_Left: if (qGroup->find(0)->hasFocus()) { PlatformMScreen * mscreen = PlatformMScreen::getMScreen(); mscreen->keyReleaseEvent(keyEvent); } else { QButton::keyReleaseEvent(keyEvent); } break; case Qt::Key_Down: case Qt::Key_Right: if (qGroup->find(qGroup->count() - 1)->hasFocus()) { PlatformMScreen * mscreen = PlatformMScreen::getMScreen(); mscreen->keyReleaseEvent(keyEvent); } else { QButton::keyReleaseEvent(keyEvent); } break; default: QButton::keyReleaseEvent(keyEvent); break; } }/** * Notify Form that selection in a Choice element has changed. * For a Choice button that is part of an exclusive group. * */void ChoiceButton::notifyStateChanged() { QButtonGroup *qGroup = (QButtonGroup *)parent(); MidpFormItemPeerStateChanged(((ChoiceButtonBox *)qGroup)->parent(), qGroup->id(this));}/** * Override QButton to do the calculation based on the contents. */QSize ChoiceButton::sizeHint() const { // layout will be forced to happen after there was a resize() call // on ChoiceButton's parent (ChoiceButtonBoxBody) // Fixed width was set in ChoiceButtonBoxBody.resize() return sizeHint(width());}/** * Size calculations based on the width available. * * @param w the width available for this ChoiceButton */QSize ChoiceButton::sizeHint(int w) const { constPolish(); QSize mySize = isExclusive ? style().exclusiveIndicatorSize() : style().indicatorSize(); if (img == NULL && text().isEmpty()) { mySize.rheight() += 4; return mySize; } mySize.rwidth() += 6; // pad between indicator and content if (img != NULL) { if (mySize.height() < imgSize.height()) { mySize.setHeight(imgSize.height()); } mySize.rwidth() += imgSize.width(); if (!text().isEmpty()) { // 2 pixel pad afterimage mySize.rwidth() += 2; } } if (!text().isEmpty()) { w -= mySize.width(); QRect textRect = QFontMetrics(font()).boundingRect(mySize.width(), 0, w, qteapp_get_mscreen()->getScreenHeight(), fitPolicy == TEXT_WRAP_OFF ? AlignLeft | AlignTop : AlignLeft | AlignTop | WordBreak, text()); if (textRect.height() > mySize.height()) { mySize.setHeight(textRect.height()); } if (textRect.width() >= w) { mySize.rwidth() += w; } else { mySize.rwidth() += textRect.width(); } } mySize.rheight() += 4; return mySize; //expandedTo( QApplication::globalStrut() );}/** * Override QButton to custom paint text and image. * * @param paint painter to use */void ChoiceButton::drawButton( QPainter *paint ){ paint->setFont(font()); QSize indSize = isExclusive ? style().exclusiveIndicatorSize() : style().indicatorSize(); int x = 0; int y = 0; if (isExclusive) { style().drawExclusiveIndicator(paint, x, y, indSize.width(), indSize.height(), colorGroup(), isOn(), isDown(), isEnabled() ); } else { style().drawIndicator(paint, x, y, indSize.width(), indSize.height(), colorGroup(), state(), isDown(), isEnabled()); } x = indSize.width() + 6; y = 2; int h = height() - 4; // the following value should be better calculated // (padding is not included) int w = width() - x; // the rest is content QRect focusRect(x - 3, y - 1, w + 3, h + 2); if (img != NULL) { style().drawItem( paint, x, y, imgSize.width(), imgSize.height(), AlignLeft|AlignTop, colorGroup(), isEnabled(), img, NULL); x += imgSize.width() + 2; w -= imgSize.width() + 2; } if (!text().isEmpty()) { int elWidth = 0; if (fitPolicy == TEXT_WRAP_OFF && paint->fontMetrics().width(text()) > w) { elWidth = paint->fontMetrics().width("..."); } style().drawItem( paint, x, y, w - elWidth, h, fitPolicy == TEXT_WRAP_OFF ? AlignLeft | AlignTop : AlignLeft | AlignTop | WordBreak, colorGroup(), isEnabled(), NULL, text()); if (elWidth > 0) { paint->drawText(x + w - elWidth, y, elWidth, h, AlignLeft | AlignTop, "..."); } } if ( hasFocus() ) { if (text().isEmpty() && img == NULL) { // draw focus around indicator 1 pixel wider focusRect.setLeft(1); focusRect.setWidth(indSize.width() + 2); } style().drawFocusRect(paint, focusRect, colorGroup()); } }/** * Set image portion of this element. * * @param pixmap new image */void ChoiceButton::setImage(QPixmap *pixmap) { img = pixmap; if (img != NULL) { // clip image to 16 x 16 imgSize.setWidth(img->width() > 16 ? 16 : img->width() ); imgSize.setHeight(img->height() > 16 ? 16 : img->height() );
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?