lfpport_qte_choicegroup.cpp

来自「This is a resource based on j2me embedde」· C++ 代码 · 共 2,552 行 · 第 1/5 页

CPP
2,552
字号
  } else {    imgSize.setWidth(0);    imgSize.setHeight(0);  }}/** * Set text wrapping policy. * * @param fitPolicy new policy */void ChoiceButton::setFitPolicy(int fitPolicy) {  this->fitPolicy = fitPolicy;}/** * Construct a button group as the body widget of a ChoiceGroup. * This is for ChoiceGroup type EXCLUSIVE and MULTIPLE. * * @param parent parent widget pointer */ChoiceButtonBoxBody::ChoiceButtonBoxBody(QWidget *parent) :   QVButtonGroup(parent) {  setFocusPolicy(QWidget::NoFocus);}/** Destruct a button group widget */ChoiceButtonBoxBody::~ChoiceButtonBoxBody() {}/** * Override to delegate focus to current selected choice element. *  * @param event focus event to handle */void ChoiceButtonBoxBody::focusInEvent(QFocusEvent *event) {  /* Suppress unused-parameter warning */  (void)event;  QButton *button;   if (QVButtonGroup::isRadioButtonExclusive()) {    // there must be at least one button selected    button = selected();  } else {    button = QVButtonGroup::find(0);  }    if (button != NULL) {    button->setFocus();  }    // Continue with focus activation  // QVButtonGroup::focusInEvent(event);}/** * Override QButtonGroup to traverse between choice elements. * * @param key key that user pressed. Only UP/DOWN keys are processed. */void ChoiceButtonBoxBody::moveFocus(int key) {  QButton *button = (QButton *)QWidget::focusWidget();  if (button != NULL) {    PlatformMScreen * mscreen = PlatformMScreen::getMScreen();    int id = QVButtonGroup::id(button);    button = NULL;    if (key == Qt::Key_Up && id > 0) {      button = QVButtonGroup::find(--id);      if (button != NULL) {    button->setFocus();        int visY = parentWidget()->y();    if (id > 0) {      visY += y() + button->y();    }    mscreen->ensureVisible(parentWidget()->x(), visY, 0, 0);      }    } else if (key == Qt::Key_Down && id < QVButtonGroup::count()-1) {      button = QVButtonGroup::find(++id);      if (button != NULL) {    button->setFocus();    int visY = parentWidget()->y();    if (id < QVButtonGroup::count()-1) {      visY += y() + button->y() + button->height();    } else {      visY += parentWidget()->height();    }    mscreen->ensureVisible(parentWidget()->x(), visY, 0, 0);      }    } else {      // focus does not move      return;    }  }}/** * Size calculations based on the width available. * * @param w the width available for ChoiceButtonBoxBody */QSize ChoiceButtonBoxBody::sizeHint(int w) const {  int maxWidth = 0;  QSize buttonSize, size(0,0);  // Qt uses a padding of 18 to layout QButtons in QButtonGroup  w -= 2*frameWidth() + 18;  for(int i = 0, num = count(); i < num; i++) {    buttonSize = ((ChoiceButton *)find(i))->sizeHint(w);    if (buttonSize.width() > maxWidth) {      maxWidth = buttonSize.width();    }    size.rheight() += buttonSize.height();  }  if (maxWidth > 0) {    size.setWidth(maxWidth + 2*frameWidth() + 18);    size.rheight() += 2*frameWidth() + 5 * (count()-1) + 18;  }  return size;}/** * Overrides QVButtonGroup to set fixed width on ChoiceButtons  * before the internal layout is done. * * @param w the new width of this ChoiceButtonBoxBody * @param h the new height of this ChoiceButtonBoxBody */void ChoiceButtonBoxBody::resize(int w, int h) {  // Qt uses a padding of 18 to layout QButtons in QButtonGroup  int buttonWidth = w - 2*frameWidth() - 18;  for(int i = 0, num = count(); i < num; i++) {    ((ChoiceButton *)find(i))->setFixedWidth(buttonWidth);  }    QVButtonGroup::resize(w, h);}/** * Override QButtonGroup to return the first choice that is selected. * * @return the first choice button that is selected. Null if none. */ChoiceButton * ChoiceButtonBoxBody::selected(){    ChoiceButton *button = NULL;  for(int i= 0; i < count(); i++) {    button = (ChoiceButton *)find(i);    if (button != NULL && button->isOn() && button->isToggleButton()) {      return button;    }  }  return NULL;}/** * Construct a ChoiceGroup widget with label and ChoiceButtonBoxBody. * * @param parent parent widget pointer * @param label label text   * @param layout layout directive associated with this choicegroup * @param exclusive flag for exclusive choice type * @param fitPolicy flag for text wrapping */ChoiceButtonBox::ChoiceButtonBox(QWidget *parent, const QString &label,                  int layout, bool exclusive, int fitPolicy) :   Choice(parent, label, layout, fitPolicy) {  qGroup = new ChoiceButtonBoxBody(this);    qGroup->setExclusive(exclusive);  qGroup->setRadioButtonExclusive(exclusive);  // ChoiceButtonBox with no elements should not take focus  setFocusPolicy(QWidget::NoFocus);  // Delegate focus to ChoiceButtonBoxBody  setFocusProxy(qGroup);}/** Destruct a ChoiceButton Box */ChoiceButtonBox::~ChoiceButtonBox() {  deleteAll();  delete qGroup;}/** * Move the body widget. * * @param x new X coordinate * @param y new Y coordinate */void ChoiceButtonBox::bodyRelocate(int x, int y) {  qGroup->move(x, y);}/** * Resize the body widget. * * @param w new width * @param h new height */void  ChoiceButtonBox::bodyResize(int w, int h) {  qGroup->resize(w, h);}/** * Calculate body widget's width if its height is limited to a given value. *  * @param takenHeight pointer to a value that should be set to real used height * @param h maximum allowed height for the body widget * @return body widget's width */int ChoiceButtonBox::bodyWidthForHeight(int *takenHeight, int h) {  // This function is not used on Qt since we don't layout   // choices horizontally even when there is no space vertically.  // Right now width cannot be height dependent   // NOTE: if fit policy is supported that should change  QSize size = ((QVButtonGroup *)qGroup)->sizeHint();  *takenHeight = size.height();  if (*takenHeight > h) {    *takenHeight = h;  }  return size.width();}/** * Calculate body widget's height if its width is limited to a given value. * * @param takenWidth pointer to a value that should be set to real used width * @param h maximum allowed width for the body widget * @return body widget's height */int ChoiceButtonBox::bodyHeightForWidth(int *takenWidth, int w) {  // Right now height is not dependent on width  QSize size = qGroup->sizeHint(w);  *takenWidth = size.width();  if (*takenWidth > w) {    *takenWidth = w;  }  return size.height();}/** * Insert a choice element with text and image. * * @param elementNum insert location * @param str text portion * @param img image portion * @param selected initial selection state * @return status of this call */MidpError ChoiceButtonBox::insert(int elementNum,                   const QString &str, QPixmap* img,                   jboolean selected) {  MidpError err = KNI_OK;  ChoiceButton *newElement = NULL;  // store initial group count (after an new buttons is created  // with a qGroup as a parent count will be +1 )  int num = qGroup->count();  if (qGroup->isRadioButtonExclusive()) {    newElement = new ChoiceButton(str, img, qGroup, true, fitPolicy);    // images if any are not displayed  } else {    newElement = new ChoiceButton(str, img, qGroup, false, fitPolicy);    newElement->setChecked(selected);    // images if any are not displayed  }  newElement->setFocusPolicy(QWidget::StrongFocus);  // if the last element is added we do not need  // readjust the ids before it  if (elementNum == num) {    if (qGroup->insert(newElement, elementNum) < 0) {      return KNI_EINVAL;    }  } else {    QButton *button = NULL;    for(int i = num-1; i >= elementNum; i--) {      button = qGroup->find(i);      if (button != NULL) {    qGroup->remove(button);    qGroup->insert(button, i+1);      }    }      if (qGroup->insert(newElement, elementNum) < 0) {      return KNI_EINVAL;    }    QPoint p;    for(int i = elementNum+1; i < num+1; i++) {      button = qGroup->find(i);      if (button != NULL) {    p = button->pos();    qGroup->remove(button);    button->reparent(0, p);    p.setY(p.y() + button->height());    button->reparent(qGroup, p, true);    qGroup->insert(button, i);      }    }  }   if (qGroup->isRadioButtonExclusive() && selected) {    qGroup->setButton(elementNum);  }  newElement->show();  setFocusPolicy(QWidget::StrongFocus);  qGroup->setFocusPolicy(QWidget::StrongFocus);  return err;}/** * Delete a choice element. * * @param elementNum index of the element to be deleted * @param selectedIndex new element that should be selected after deletion * @return status of this call */MidpError ChoiceButtonBox::deleteElement(int elementNum, int selectedIndex) {  QButton *button = qGroup->find(elementNum);  if (button == NULL) {    return KNI_EINVAL;  }  qGroup->remove(button);  delete button;  if (qGroup->count() == 0) {    setFocusPolicy(QWidget::NoFocus);    qGroup->setFocusPolicy(QWidget::NoFocus);    return KNI_OK;  }   // if the last element was removed we do not need  // readjust the ids  if (elementNum != qGroup->count()) {    for(int i=elementNum+1; i < qGroup->count()+1; i++) {      button = qGroup->find(i);      if (button != NULL) {    qGroup->remove(button);    // button->reparent(0, QPoint(0,0));    qGroup->insert(button, i-1);      }    }  }  if (selectedIndex >= 0) {    setSelectedIndex(selectedIndex, true);  }  return KNI_OK;}/** * Delete all choice elements. * * @return status of this call */MidpError ChoiceButtonBox::deleteAll() {  int i;  MidpError err = KNI_OK;  for (i = qGroup->count()-1; i >= 0; i--) {    if (deleteElement(i, -1) != KNI_OK) {      err = KNI_EINVAL;    }  }  return err;}/** * Update a choice element with new text and image. * * @param elementNum index of the element * @param str new text * @param img new image * @selected new selection state * @return status of this call */MidpError ChoiceButtonBox::set(int elementNum, const QString &str, QPixmap* img,               jboolean selected) {  /* Suppress unused-parameter warning */  (void)img;  ChoiceButton *button = (ChoiceButton *)qGroup->find(elementNum);  if (button == NULL) {    return KNI_EINVAL;  }  button->setText(str);  button->setImage(img);  setSelectedIndex(elementNum, selected);  repaint();  return KNI_OK;}/** * Set an element's selection state. * If the choice group is exclusive, the old selected element will be * deselected. * * @param elementNum index of the element * @param selected whether it should be selected * @return status of this call */MidpError ChoiceButtonBox::setSelectedIndex(int elementNum, jboolean selected) {  if (qGroup->isRadioButtonExclusive()) {    if (selected) {      qGroup->setButton(elementNum);    }  } else {    QButton *button = qGroup->find(elementNum);    if (button == NULL) {      return KNI_EINVAL;    }    ((ChoiceButton *)button)->setChecked(selected);  }  return KNI_OK;}/** * Get the index of current selected element. * * @param elementNum pointer to the element index to be filled in *      -1 if no selection. * @return status of this call */MidpError ChoiceButtonBox::getSelectedIndex(int *elementNum) {  QButton *button = qGroup->selected();  if (button == NULL) {    *elementNum = -1;    return KNI_OK;  }  *elementNum = qGroup->id(button); // will return -1 if not a member  return KNI_OK;}/** * Set all elements' selection state. * * @param selectedArray array of selection state * @param arrayLength size of the array * @return status of this call */MidpError ChoiceButtonBox::setSelectedFlags(jboolean* selectedArray,                    int arrayLength) {  /* Suppress unused-parameter warning */  (void)arrayLength;  int i, n = qGroup->count();  if (qGroup->isRadioButtonExclusive()) {    for (int i = 0; i < n; i++) {      if (selectedArray[i]) {    setSelectedIndex(i, true);    return KNI_OK;      }    }    setSelectedIndex(0, true);  } else {    ChoiceButton *checkbox;    for (i = 0; i < n; i++) {      checkbox = (ChoiceButton *)qGroup->find(i);      if (checkbox != NULL) {    checkbox->setChecked(selectedArray[i]);      }    }  }  return KNI_OK;}/** * Set all elements' selection state. * * @param selectedArray array of selection state * @param arrayLength size of the array * @return status of this call */MidpError ChoiceButtonBox::getSelectedFlags(int *numSelected,                    jboolean* selectedArray,                     int arrayLength) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?