BmpButton.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "BmpButton.h"
  2. #include <QDebug>
  3. #include <QPainter>
  4. #include <QFile>
  5. #include <QScopedPointer>
  6. #include <QEvent>
  7. #include <QTimer>
  8. class BmpButtonPrivate
  9. {
  10. Q_DISABLE_COPY(BmpButtonPrivate)
  11. public:
  12. explicit BmpButtonPrivate(BmpButton* parent);
  13. ~BmpButtonPrivate();
  14. void initializeTimer();
  15. QScopedPointer<QPixmap> m_NormalPixmap;
  16. QString m_NormalPath;
  17. QScopedPointer<QPixmap> m_PressPixmap;
  18. QString m_PressPath;
  19. QScopedPointer<QPixmap> m_CheckPixmap;
  20. QString m_CheckPath;
  21. BmpButton::ButtonStatus m_Status;
  22. BmpButton::Type m_LanguageType;
  23. QTimer* m_Timer;
  24. bool m_Filter;
  25. bool m_LongPressRestore;
  26. bool m_Reload;
  27. QList<BmpButton::CustomString> m_List;
  28. private:
  29. BmpButton* m_Parent;
  30. };
  31. BmpButton::BmpButton(QWidget *parent)
  32. : QAbstractButton(parent)
  33. , m_Private(new BmpButtonPrivate(this))
  34. {
  35. }
  36. BmpButton::~BmpButton()
  37. {
  38. }
  39. void BmpButton::setNormalBmpPath(const QString &path)
  40. {
  41. if (QFile::exists(path)) {
  42. m_Private->m_NormalPath = path;
  43. m_Private->m_NormalPixmap.reset(new QPixmap(path));
  44. m_Private->m_Reload = false;
  45. if (isVisible()) {
  46. update();
  47. }
  48. }
  49. }
  50. void BmpButton::setPressBmpPath(const QString &path)
  51. {
  52. if (QFile::exists(path)) {
  53. m_Private->m_PressPath = path;
  54. m_Private->m_PressPixmap.reset(new QPixmap(path));
  55. m_Private->m_Reload = false;
  56. if (isVisible()) {
  57. update();
  58. }
  59. }
  60. }
  61. void BmpButton::setCheckBmpPath(const QString &path)
  62. {
  63. if (QFile::exists(path)) {
  64. m_Private->m_CheckPath = path;
  65. m_Private->m_CheckPixmap.reset(new QPixmap(path));
  66. m_Private->m_Reload = false;
  67. if (isVisible()) {
  68. update();
  69. }
  70. }
  71. }
  72. void BmpButton::setStatus(const BmpButton::ButtonStatus &status)
  73. {
  74. if (status != m_Private->m_Status) {
  75. m_Private->m_Status = status;
  76. if (isVisible()) {
  77. update();
  78. }
  79. }
  80. }
  81. BmpButton::ButtonStatus BmpButton::getStatus()
  82. {
  83. return m_Private->m_Status;
  84. }
  85. void BmpButton::enableLongPress(const bool flag)
  86. {
  87. if (flag) {
  88. m_Private->initializeTimer();
  89. }
  90. }
  91. void BmpButton::enableLongPressRestore(const bool flag)
  92. {
  93. m_Private->m_LongPressRestore = flag;
  94. }
  95. void BmpButton::setLanguageType(const BmpButton::Type type)
  96. {
  97. m_Private->m_LanguageType = type;
  98. }
  99. void BmpButton::setCustomText(const QList<BmpButton::CustomString> &text)
  100. {
  101. m_Private->m_List = text;
  102. if (isVisible()) {
  103. update();
  104. }
  105. }
  106. bool BmpButton::event(QEvent *event)
  107. {
  108. switch (event->type()) {
  109. case QEvent::MouseButtonPress:
  110. case QEvent::MouseButtonRelease:
  111. case QEvent::MouseMove: {
  112. event->ignore();
  113. break;
  114. }
  115. default: {
  116. break;
  117. }
  118. }
  119. return QAbstractButton::event(event);
  120. }
  121. void BmpButton::styleChange()
  122. {
  123. m_Private->m_Reload = true;
  124. }
  125. void BmpButton::mousePressEvent(QMouseEvent *event)
  126. {
  127. Q_UNUSED(event);
  128. m_Private->m_Status = BmpButton::B_Press;
  129. if (isVisible()) {
  130. update();
  131. }
  132. m_Private->m_Filter = false;
  133. if (NULL != m_Private->m_Timer) {
  134. m_Private->m_Timer->start();
  135. }
  136. // emit bmpButtomPress();
  137. // QWidget::mousePressEvent(event);
  138. }
  139. void BmpButton::mouseReleaseEvent(QMouseEvent *event)
  140. {
  141. Q_UNUSED(event);
  142. if (NULL != m_Private->m_Timer) {
  143. m_Private->m_Timer->stop();
  144. }
  145. if (isVisible()) {
  146. if ((NULL == m_Private->m_Timer)
  147. || (!m_Private->m_Filter)) {
  148. m_Private->m_Status = BmpButton::B_Normal;
  149. update();
  150. emit released();
  151. }
  152. //emit bmpButtonRelease();
  153. }
  154. //QWidget::mouseReleaseEvent(event);
  155. }
  156. void BmpButton::paintEvent(QPaintEvent *event)
  157. {
  158. Q_UNUSED(event);
  159. QPainter painter(this);
  160. painter.setRenderHint(QPainter::Antialiasing, true);
  161. if (m_Private->m_Reload) {
  162. setNormalBmpPath(m_Private->m_NormalPath);
  163. setPressBmpPath(m_Private->m_PressPath);
  164. setCheckBmpPath(m_Private->m_CheckPath);
  165. }
  166. switch (m_Private->m_Status) {
  167. case BmpButton::B_Check: {
  168. checkPaint(painter);
  169. break;
  170. }
  171. case BmpButton::B_Press: {
  172. pressPaint(painter);
  173. break;
  174. }
  175. case BmpButton::B_Normal:
  176. default : {
  177. normalPaint(painter);
  178. break;
  179. }
  180. }
  181. painter.setPen(Qt::white);
  182. if (0 != m_Private->m_List.size()) {
  183. QString text;
  184. for (int i = 0; i < m_Private->m_List.size(); ++i) {
  185. if ((BmpButton::T_Translate == m_Private->m_LanguageType)
  186. && (m_Private->m_List.at(i).translate)) {
  187. text += QObject::tr(m_Private->m_List.at(i).string.toLocal8Bit().constData());
  188. } else {
  189. text += m_Private->m_List.at(i).string;
  190. }
  191. }
  192. painter.drawText(rect(), Qt::AlignCenter, text);
  193. } else if (!text().isEmpty()) {
  194. if (BmpButton::T_Translate == m_Private->m_LanguageType) {
  195. painter.drawText(rect(), Qt::AlignCenter, QObject::tr(text().toLocal8Bit().constData()));
  196. } else {
  197. painter.drawText(rect(), Qt::AlignCenter, text());
  198. }
  199. }
  200. }
  201. void BmpButton::normalPaint(QPainter &painter)
  202. {
  203. if (NULL != m_Private->m_NormalPixmap) {
  204. int x = (width() - m_Private->m_NormalPixmap->width()) >> 1;
  205. int y = (height() - m_Private->m_NormalPixmap->height()) >> 1;
  206. painter.drawPixmap(x, y, *m_Private->m_NormalPixmap);
  207. }
  208. }
  209. void BmpButton::pressPaint(QPainter &painter)
  210. {
  211. if (NULL != m_Private->m_PressPixmap) {
  212. int x = (width() - m_Private->m_PressPixmap->width()) >> 1;
  213. int y = (height() - m_Private->m_PressPixmap->height()) >> 1;
  214. painter.drawPixmap(x, y, *m_Private->m_PressPixmap);
  215. }
  216. }
  217. void BmpButton::checkPaint(QPainter &painter)
  218. {
  219. if (NULL != m_Private->m_CheckPixmap) {
  220. int x = (width() - m_Private->m_CheckPixmap->width()) >> 1;
  221. int y = (height() - m_Private->m_CheckPixmap->height()) >> 1;
  222. painter.drawPixmap(x, y, *m_Private->m_CheckPixmap);
  223. }
  224. }
  225. void BmpButton::onTimeout()
  226. {
  227. if (isVisible()) {
  228. m_Private->m_Filter = true;
  229. emit longPressed();
  230. }
  231. if (m_Private->m_LongPressRestore) {
  232. m_Private->m_Status = BmpButton::B_Normal;
  233. }
  234. if (isVisible()) {
  235. update();
  236. }
  237. }
  238. BmpButtonPrivate::BmpButtonPrivate(BmpButton* parent)
  239. : m_Parent(parent)
  240. {
  241. m_Status = BmpButton::B_Normal;
  242. m_LanguageType = BmpButton::T_Translate;
  243. m_Timer = NULL;
  244. m_Filter = false;
  245. m_LongPressRestore = true;
  246. m_Reload = false;
  247. m_List.clear();
  248. }
  249. BmpButtonPrivate::~BmpButtonPrivate()
  250. {
  251. }
  252. void BmpButtonPrivate::initializeTimer()
  253. {
  254. if (NULL == m_Timer) {
  255. m_Timer = new QTimer(m_Parent);
  256. m_Timer->setSingleShot(true);
  257. m_Timer->setInterval(1500);
  258. QObject::connect(m_Timer, SIGNAL(timeout()),
  259. m_Parent, SLOT(onTimeout()));
  260. }
  261. }