Source Code (Use browser search to find items of interest.)

Class Index

kdelibs'KButtonBox (./kdelibs/kdeui/kbuttonbox.h:38)

class KButtonBox : public QWidget {
  Q_OBJECT
public:
  /**
   * Orientations for the button box.
   **/
  enum { VERTICAL = 1, HORIZONTAL = 2 };

  /**
    * Create an empty container for buttons.
    *
    *  If @p _orientation is 
    * @p KButtonBox::VERTICAL, the buttons inserted with @ref addButton()
    * are laid out from top to bottom, otherwise they are laid out
    * from left to right.
    */
  KButtonBox(QWidget *parent, int _orientation = HORIZONTAL, 
	     int border = 0, int _autoborder = 6);

  /**
    * Free private data field
    */
  ~KButtonBox();

  /**
    * @return The minimum size needed to fit all buttons.
    *
    * This size is
    * calculated by the width/height of all buttons plus border/autoborder.
    */
  virtual QSize sizeHint() const;

  virtual void resizeEvent(QResizeEvent *);

  /**
    * Add a new @ref QPushButton.  
    *
    * @param noexpand If @p noexpand is @p false, the width
    * of the button is adjusted to fit the other buttons (the maximum
    * of all buttons is taken). If @p noexpand @p true, the width of this
    * button will be set to the minimum width needed for the given text).
    *
    * @return A pointer to the new button.
    */
  QPushButton *addButton(const QString& text, bool noexpand = FALSE);

  /**
    * Add a stretch to the buttonbox. 
    *
    * @see QBoxLayout for details.
    * Can be used to separate buttons (i.e. if you add the buttons "OK",
    * "Cancel", add a stretch and then add the button "Help", "OK" and
    * "Cancel" will be left-aligned (or top-aligned for vertical) while
    * "Help" will be right-aligned (or bottom-aligned for vertical).
    */
  void addStretch(int scale = 1);

  /**
    * This function must be called @bf once after all buttons have been
    * inserted.
    *
    * It will start layout control.
    */
  void layout();

protected:
  class Item;
  class PrivateData;  

  /**
    * @return the best size for a button. Checks all buttons and takes
    * the maximum width/height.
    */
  QSize bestButtonSize() const;
  void  placeButtons();
  QSize buttonSizeHint(QPushButton *) const;
  
  PrivateData *data;
};

kdelibs'KButtonBox::KButtonBox() (./kdelibs/kdeui/kbuttonbox.cpp:73)

KButtonBox::KButtonBox(QWidget *parent, int _orientation,
		       int border, int autoborder)
  :  QWidget(parent)
{
  data = new PrivateData;
  assert(data != 0);

  data->orientation = _orientation;
  data->border = border;
  data->autoborder = autoborder < 0 ? border : autoborder;
  data->buttons.setAutoDelete(TRUE);
}


kdelibs'KButtonBox::~KButtonBox() (./kdelibs/kdeui/kbuttonbox.cpp:86)

KButtonBox::~KButtonBox() {
  delete data;
}


kdelibs'KButtonBox::addButton() (./kdelibs/kdeui/kbuttonbox.cpp:90)

QPushButton *KButtonBox::addButton(const QString& text, bool noexpand) {
  Item *item = new Item;

  item->button = new QPushButton(text, this);
  item->noexpand  = noexpand;
  data->buttons.append(item);
  item->button->adjustSize();

  return item->button;
}	


kdelibs'KButtonBox::addStretch() (./kdelibs/kdeui/kbuttonbox.cpp:101)

void KButtonBox::addStretch(int scale) {
  if(scale > 0) {
    Item *item = new Item;
    item->button = 0;
    item->noexpand  = FALSE;
    item->stretch = scale;
    data->buttons.append(item);
  }
}


kdelibs'KButtonBox::layout() (./kdelibs/kdeui/kbuttonbox.cpp:111)

void KButtonBox::layout() {
  // resize all buttons
  QSize bs = bestButtonSize();

  for(unsigned int i = 0; i < data->buttons.count(); i++) {
    Item *item = data->buttons.at(i);
    QPushButton *b = item->button;
    if(b != 0) {
      if(item->noexpand)
	b->setFixedSize(buttonSizeHint(b));
      else
	b->setFixedSize(bs);
    }
  }

  setMinimumSize(sizeHint());
}


kdelibs'KButtonBox::placeButtons() (./kdelibs/kdeui/kbuttonbox.cpp:129)

void KButtonBox::placeButtons() {
  unsigned int i;

  if(data->orientation == HORIZONTAL) {
    // calculate free size and stretches
    int fs = width() - 2 * data->border;
    int stretch = 0;
    for(i = 0; i < data->buttons.count(); i++) {
      Item *item = data->buttons.at(i);
      if(item->button != 0) {
	fs -= item->button->width();

	// Last button?
	if(i != data->buttons.count() - 1)
	  fs -= data->autoborder;
      } else
	stretch +=item->stretch;
    }

    // distribute buttons
    int x_pos = data->border;
    for(i = 0; i < data->buttons.count(); i++) {
      Item *item = data->buttons.at(i);
      if(item->button != 0) {
	QPushButton *b = item->button;
	b->move(x_pos, (height() - b->height()) / 2);

	x_pos += b->width() + data->autoborder;
      } else
	x_pos += (int)((((double)fs) * item->stretch) / stretch);
    }
  } else { // VERTICAL
    // calcualte free size and stretches
    int fs = height() - 2 * data->border;
    int stretch = 0;
    for(i = 0; i < data->buttons.count(); i++) {
      Item *item = data->buttons.at(i);
      if(item->button != 0)
	fs -= item->button->height() + data->autoborder;
      else
	stretch +=item->stretch;
    }

    // distribute buttons
    int y_pos = data->border;
    for(i = 0; i < data->buttons.count(); i++) {
      Item *item = data->buttons.at(i);
      if(item->button != 0) {
	QPushButton *b = item->button;
	b->move((width() - b->width()) / 2, y_pos);

	y_pos += b->height() + data->autoborder;
      } else
	y_pos += (int)((((double)fs) * item->stretch) / stretch);
    }
  }
}


kdelibs'KButtonBox::resizeEvent() (./kdelibs/kdeui/kbuttonbox.cpp:187)

void KButtonBox::resizeEvent(QResizeEvent *) {
  placeButtons();
}


kdelibs'KButtonBox::bestButtonSize() (./kdelibs/kdeui/kbuttonbox.cpp:191)

QSize KButtonBox::bestButtonSize() const {
  QSize s(0, 0);
  unsigned int i;

  // calculate optimal size
  for(i = 0; i < data->buttons.count(); i++) {
    KButtonBox *that = (KButtonBox*)this; // to remove the const ;(
    Item *item = that->data->buttons.at(i);
    QPushButton *b = item->button;

    if(b != 0 && !item->noexpand) {
      QSize bs = buttonSizeHint(b);

      if(bs.width() > s.width())
	s.setWidth(bs.width());
      if(bs.height() > s.height())
	s.setHeight(bs.height());
    }
  }

  return s;
}


kdelibs'KButtonBox::sizeHint() (./kdelibs/kdeui/kbuttonbox.cpp:214)

QSize KButtonBox::sizeHint() const {
  unsigned int i, dw;

  if(data->buttons.count() == 0)
    return QSize(0, 0);
  else {
    dw = 2 * data->border;

    QSize bs = bestButtonSize();
    for(i = 0; i < data->buttons.count(); i++) {
      KButtonBox *that = (KButtonBox*)this;
      Item *item = that->data->buttons.at(i);
      QPushButton *b = item->button;
      if(b != 0) {
	QSize s;
	if(item->noexpand)
	  s = that->buttonSizeHint(b);
	else
	  s = bs;

	if(data->orientation == HORIZONTAL)
	  dw += s.width();
	else
	  dw += s.height();

	if( i != data->buttons.count() - 1 )
	  dw += data->autoborder;
      }
    }

    if(data->orientation == HORIZONTAL)
	return QSize(dw, bs.height() + 2 * data->border);
    else
	return QSize(bs.width() + 2 * data->border, dw);
  }
}

/*
 * Returns the best size for a button. If a button is less than
 * minButtonWidth pixels wide, return minButtonWidth pixels
 * as minimum width
 */

kdelibs'KButtonBox::buttonSizeHint() (./kdelibs/kdeui/kbuttonbox.cpp:256)

QSize KButtonBox::buttonSizeHint(QPushButton *b) const {
  QSize s = b->sizeHint();
  QSize ms = b->minimumSize();
  if(s.width() < minButtonWidth)
    s.setWidth(minButtonWidth);

  // allows the programmer to override the settings
  if(ms.width() > s.width())
    s.setWidth(ms.width());
  if(ms.height() > s.height())
    s.setHeight(ms.height());

  return s;
}