portraitwidget.cpp 3.6 KB

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