portraitwidget.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * portraitwidget.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "portraitwidget.h"
  12. #include "ui_portraitwidget.h"
  13. #include "../Animation.h"
  14. #include "../lib/entities/hero/CHeroHandler.h"
  15. PortraitWidget::PortraitWidget(CGHeroInstance & h, QWidget *parent):
  16. QDialog(parent),
  17. ui(new Ui::PortraitWidget),
  18. hero(h),
  19. portraitIndex(0)
  20. {
  21. ui->setupUi(this);
  22. ui->portraitView->setScene(&scene);
  23. ui->portraitView->fitInView(scene.itemsBoundingRect(), Qt::KeepAspectRatio);
  24. show();
  25. }
  26. PortraitWidget::~PortraitWidget()
  27. {
  28. delete ui;
  29. }
  30. void PortraitWidget::obtainData()
  31. {
  32. portraitIndex = VLC->heroh->getById(hero.getPortraitSource())->getIndex();
  33. if(hero.customPortraitSource.isValid())
  34. {
  35. ui->isDefault->setChecked(true);
  36. }
  37. drawPortrait();
  38. }
  39. void PortraitWidget::commitChanges()
  40. {
  41. if(portraitIndex == VLC->heroh->getById(HeroTypeID(hero.subID))->getIndex())
  42. hero.customPortraitSource = HeroTypeID::NONE;
  43. else
  44. hero.customPortraitSource = VLC->heroh->getByIndex(portraitIndex)->getId();
  45. }
  46. void PortraitWidget::drawPortrait()
  47. {
  48. static Animation portraitAnimation(AnimationPath::builtin("PortraitsLarge").getOriginalName());
  49. portraitAnimation.preload();
  50. auto icon = VLC->heroTypes()->getByIndex(portraitIndex)->getIconIndex();
  51. pixmap = QPixmap::fromImage(*portraitAnimation.getImage(icon));
  52. scene.addPixmap(pixmap);
  53. ui->portraitView->fitInView(scene.itemsBoundingRect(), Qt::KeepAspectRatio);
  54. }
  55. void PortraitWidget::resizeEvent(QResizeEvent *)
  56. {
  57. ui->portraitView->fitInView(scene.itemsBoundingRect(), Qt::KeepAspectRatio);
  58. }
  59. void PortraitWidget::on_isDefault_toggled(bool checked)
  60. {
  61. if(checked)
  62. {
  63. ui->buttonNext->setEnabled(false);
  64. ui->buttonPrev->setEnabled(false);
  65. portraitIndex = VLC->heroh->getById(HeroTypeID(hero.subID))->getIndex();
  66. }
  67. else
  68. {
  69. ui->buttonNext->setEnabled(true);
  70. ui->buttonPrev->setEnabled(true);
  71. }
  72. drawPortrait();
  73. }
  74. void PortraitWidget::on_buttonNext_clicked()
  75. {
  76. if(portraitIndex < VLC->heroh->size() - 1)
  77. ++portraitIndex;
  78. else
  79. portraitIndex = 0;
  80. drawPortrait();
  81. }
  82. void PortraitWidget::on_buttonPrev_clicked()
  83. {
  84. if(portraitIndex > 0)
  85. --portraitIndex;
  86. else
  87. portraitIndex = VLC->heroh->size() - 1;
  88. drawPortrait();
  89. }
  90. PortraitDelegate::PortraitDelegate(CGHeroInstance & h)
  91. : BaseInspectorItemDelegate()
  92. , hero(h)
  93. {
  94. }
  95. QWidget * PortraitDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  96. {
  97. return new PortraitWidget(hero, parent);
  98. }
  99. void PortraitDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  100. {
  101. if(auto * ed = qobject_cast<PortraitWidget *>(editor))
  102. {
  103. ed->obtainData();
  104. }
  105. else
  106. {
  107. QStyledItemDelegate::setEditorData(editor, index);
  108. }
  109. }
  110. void PortraitDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  111. {
  112. if(auto * ed = qobject_cast<PortraitWidget *>(editor))
  113. {
  114. ed->commitChanges();
  115. updateModelData(model, index);
  116. }
  117. else
  118. {
  119. QStyledItemDelegate::setModelData(editor, model, index);
  120. }
  121. }
  122. void PortraitDelegate::updateModelData(QAbstractItemModel * model, const QModelIndex & index) const
  123. {
  124. QMap<int, QVariant> data;
  125. if(hero.customPortraitSource == HeroTypeID::NONE)
  126. data[Qt::DisplayRole] = QObject::tr("Default");
  127. else
  128. data[Qt::DisplayRole] = QString::fromStdString(hero.customPortraitSource.toHeroType()->getNameTranslated());
  129. model->setItemData(index, data);
  130. }