QtUtil.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <QString>
  10. #include <CryCommon/StlUtils.h>
  11. #include <QApplication>
  12. #include <QDropEvent>
  13. #include <QWidget>
  14. #include <QModelIndexList>
  15. #ifdef LoadCursor
  16. #undef LoadCursor
  17. #endif
  18. class QWaitCursor
  19. {
  20. public:
  21. QWaitCursor()
  22. {
  23. QGuiApplication::setOverrideCursor(Qt::BusyCursor);
  24. }
  25. ~QWaitCursor()
  26. {
  27. QGuiApplication::restoreOverrideCursor();
  28. }
  29. };
  30. namespace QtUtil
  31. {
  32. inline QString trimRight(const QString& str)
  33. {
  34. // We prepend a char, so that the left doesn't get trimmed, then we remove it after trimming
  35. return QString(QStringLiteral("A") + str).trimmed().remove(0, 1);
  36. }
  37. //! ModelIndexListHasExactlyOneRow returns true only if the given list of indices has exactly one row represented.
  38. //! It will return false in all other cases, including when it is empty, or it has multiple different rows represented.
  39. //! A list of model indexes from for example a selection model can contain different indices representing the same row,
  40. //! but different columns. Often, such controls will select an entire row (all columns) when the user clicks on an item,
  41. //! resulting in for example, 5 modelindexes selected (but they are all referring to the same row, which is the same logical item),
  42. //! and we need to differentiate this from the case where the user has selected multiple different rows in a multi-select.
  43. inline bool ModelIndexListHasExactlyOneRow(const QModelIndexList& indexes)
  44. {
  45. int numberOfUniqueRows = 0;
  46. int lastSeenRow = -1;
  47. for (const auto& element : indexes)
  48. {
  49. if (element.row() != lastSeenRow)
  50. {
  51. lastSeenRow = element.row();
  52. numberOfUniqueRows++;
  53. if (numberOfUniqueRows > 1) // we only care if its exactly 1 row so can early out
  54. {
  55. return false;
  56. }
  57. }
  58. }
  59. return (numberOfUniqueRows == 1);
  60. }
  61. template<typename ... Args>
  62. struct Select
  63. {
  64. template<typename C, typename R>
  65. static auto OverloadOf(R(C::* pmf)(Args...))->decltype(pmf) {
  66. return pmf;
  67. }
  68. };
  69. }
  70. namespace stl
  71. {
  72. //! Case insensitive less key for QString
  73. template <>
  74. struct less_stricmp<QString>
  75. {
  76. bool operator()(const QString& left, const QString& right) const
  77. {
  78. return left.compare(right, Qt::CaseInsensitive) < 0;
  79. }
  80. };
  81. }