DeprecationHelper.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * DeprecationHelper.h - This file contains the declarations of helper functions
  3. * which helps centralize the #ifdefs preprocessors regarding deprecation based on Qt versions.
  4. * The functions are defined differently based on the callers' Qt versions.
  5. *
  6. * Copyright (c) 2020 Tien Dat Nguyen <ntd.bk.k56/at/gmail.com>
  7. *
  8. * This file is part of LMMS - https://lmms.io
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program (see COPYING); if not, write to the
  22. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. * Boston, MA 02110-1301 USA.
  24. *
  25. */
  26. #ifndef DEPRECATIONHELPER_H
  27. #define DEPRECATIONHELPER_H
  28. #include <QFontMetrics>
  29. #include <QWheelEvent>
  30. /**
  31. * @brief horizontalAdvance is a backwards-compatible adapter for
  32. * QFontMetrics::horizontalAdvance and width functions.
  33. * @param metrics
  34. * @param text
  35. * @return text's horizontal advance based on metrics.
  36. */
  37. inline int horizontalAdvance(const QFontMetrics& metrics, const QString& text)
  38. {
  39. #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
  40. return metrics.horizontalAdvance(text);
  41. #else
  42. return metrics.width(text);
  43. #endif
  44. }
  45. /**
  46. * @brief position is a backwards-compatible adapter for
  47. * QWheelEvent::position and pos functions.
  48. * @param wheelEvent
  49. * @return the position of wheelEvent
  50. */
  51. inline QPoint position(QWheelEvent *wheelEvent)
  52. {
  53. #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
  54. return wheelEvent->position().toPoint();
  55. #else
  56. return wheelEvent->pos();
  57. #endif
  58. }
  59. #endif // DEPRECATIONHELPER_H