portraitwidget.cpp 3.6 KB

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