qspimgcanvas.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "qspimgcanvas.h"
  2. #include <QFileInfo>
  3. QspImgCanvas::QspImgCanvas(QWidget *parent) : QWidget(parent)
  4. {
  5. m_posX = m_posY = 0;
  6. m_isAnim = false;
  7. setWindowTitle(tr("Image"));
  8. setContentsMargins(0,0,0,0);
  9. label_image.setFrameStyle(QFrame::NoFrame);
  10. label_image.setFrameShadow(QFrame::Plain);
  11. label_image.setContentsMargins(0,0,0,0);
  12. layout.setContentsMargins(0,0,0,0);
  13. sizePolicy().setHorizontalPolicy(QSizePolicy::Expanding);
  14. sizePolicy().setVerticalPolicy(QSizePolicy::Expanding);
  15. label_image.sizePolicy().setHorizontalPolicy(QSizePolicy::Expanding);
  16. label_image.sizePolicy().setVerticalPolicy(QSizePolicy::Expanding);
  17. //label_image.setScaledContents(true);
  18. layout.addWidget(&label_image);
  19. setLayout(&layout);
  20. label_image.setMinimumSize(50, 50);
  21. setMinimumSize(50, 50);
  22. //setVisible(false);
  23. //setModal(false);
  24. connect(&m_movie, SIGNAL(frameChanged(int)), this, SLOT(OnNewFrame(int)) );
  25. }
  26. QspImgCanvas::~QspImgCanvas()
  27. {
  28. }
  29. bool QspImgCanvas::OpenFile(const QString &fileName)
  30. {
  31. bool ret;
  32. QFileInfo file(m_path + fileName);
  33. QString path(file.absoluteFilePath());
  34. setWindowTitle(path);
  35. m_isAnim = false;
  36. if (!path.isEmpty())
  37. {
  38. if (file.exists() && file.isFile())
  39. {
  40. if(path.endsWith(".gif", Qt::CaseInsensitive) || path.endsWith(".mng", Qt::CaseInsensitive))
  41. {
  42. m_movie.stop();
  43. m_movie.setFileName(path);
  44. m_movie.start();
  45. if(m_movie.isValid())
  46. m_isAnim = true;
  47. }
  48. if (m_isAnim)
  49. {
  50. //label_image.setMovie(&m_movie);
  51. ret = true;
  52. }
  53. else
  54. {
  55. ret = m_image.load(path);
  56. if(ret)
  57. label_image.setPixmap(m_image);
  58. }
  59. if (ret)
  60. {
  61. resizeEvent(0);
  62. //setVisible(true);
  63. }
  64. return ret;
  65. }
  66. return false;
  67. }
  68. return true;
  69. }
  70. void QspImgCanvas::RefreshUI()
  71. {
  72. // if (m_isAnim)
  73. // m_animation->RefreshUI();
  74. // else
  75. // Refresh();
  76. }
  77. void QspImgCanvas::SetGamePath(const QString &path)
  78. {
  79. m_path = path;
  80. }
  81. bool QspImgCanvas::SetBackgroundColor(const QColor &color)
  82. {
  83. QString sheet = QString::fromLatin1("QLabel { background-color : %1 }").arg(color.name());
  84. label_image.setStyleSheet(sheet);
  85. //wxWindow::SetBackgroundColor(color);
  86. //m_animation->SetBackgroundColor(color);
  87. return true;
  88. }
  89. void QspImgCanvas::resizeEvent(QResizeEvent *event)
  90. {
  91. if (m_isAnim)
  92. {
  93. //label_image.setMovie(&m_movie);
  94. }
  95. else
  96. {
  97. if(!m_image.isNull())
  98. label_image.setPixmap(m_image.scaled(label_image.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
  99. }
  100. }
  101. void QspImgCanvas::OnNewFrame(int frameNumber)
  102. {
  103. if (m_isAnim)
  104. {
  105. if(m_movie.isValid())
  106. {
  107. label_image.setPixmap(m_movie.currentPixmap().scaled(label_image.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
  108. }
  109. }
  110. }
  111. //void QspImgCanvas::keyPressEvent(QKeyEvent *event)
  112. //{
  113. // if(event->key() == Qt::Key_Escape)
  114. // {
  115. // setVisible(false);
  116. // event->ignore();
  117. // }
  118. //}
  119. //void QspImgCanvas::closeEvent(QCloseEvent *event)
  120. //{
  121. // setVisible(false);
  122. // event->ignore();
  123. //}