button.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #if defined(Hiro_Button)
  2. namespace hiro {
  3. auto pButton::construct() -> void {
  4. qtWidget = qtButton = new QtButton(*this);
  5. qtButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
  6. qtButton->connect(qtButton, SIGNAL(released()), SLOT(onActivate()));
  7. setBordered(state().bordered);
  8. setIcon(state().icon);
  9. setOrientation(state().orientation);
  10. setText(state().text);
  11. pWidget::construct();
  12. }
  13. auto pButton::destruct() -> void {
  14. delete qtButton;
  15. qtWidget = qtButton = nullptr;
  16. }
  17. auto pButton::minimumSize() const -> Size {
  18. auto size = pFont::size(qtWidget->font(), state().text ? state().text : " ");
  19. if(state().orientation == Orientation::Horizontal) {
  20. size.setWidth(size.width() + state().icon.width());
  21. size.setHeight(max(size.height(), state().icon.height()));
  22. }
  23. if(state().orientation == Orientation::Vertical) {
  24. size.setWidth(max(size.width(), state().icon.width()));
  25. size.setHeight(size.height() + state().icon.height());
  26. }
  27. return {size.width() + (state().text ? 20 : 12), size.height() + 12};
  28. }
  29. auto pButton::setBordered(bool bordered) -> void {
  30. qtButton->setAutoRaise(!bordered);
  31. }
  32. auto pButton::setIcon(const image& icon) -> void {
  33. qtButton->setIconSize(QSize(icon.width(), icon.height()));
  34. qtButton->setIcon(CreateIcon(icon));
  35. qtButton->setStyleSheet("text-align: top;");
  36. }
  37. auto pButton::setOrientation(Orientation orientation) -> void {
  38. switch(orientation) {
  39. case Orientation::Horizontal: qtButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); break;
  40. case Orientation::Vertical: qtButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); break;
  41. }
  42. }
  43. auto pButton::setText(const string& text) -> void {
  44. qtButton->setText(QString::fromUtf8(text));
  45. }
  46. auto QtButton::onActivate() -> void {
  47. p.self().doActivate();
  48. }
  49. }
  50. #endif