widgetutils.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #ifndef WIDGETUTILS_H
  2. #define WIDGETUTILS_H
  3. #include <QtGlobal>
  4. #include <QSize>
  5. #include <QMessageBox>
  6. #include <QVariant>
  7. #include <QModelIndex>
  8. #include <QList>
  9. class QWidget;
  10. class QAbstractScrollArea;
  11. class QKeyEvent;
  12. class QActionGroup;
  13. class QAction;
  14. class QScrollArea;
  15. class QListView;
  16. class QMenu;
  17. class QShortcut;
  18. class QFormLayout;
  19. class QLineEdit;
  20. namespace vnotex
  21. {
  22. class WidgetUtils
  23. {
  24. public:
  25. WidgetUtils() = delete;
  26. static void setPropertyDynamically(QWidget *p_widget,
  27. const char *p_prop,
  28. const QVariant &p_val = QVariant());
  29. static void updateStyle(QWidget *p_widget);
  30. static qreal calculateScaleFactor(bool p_update = false);
  31. static bool isScrollBarVisible(QAbstractScrollArea *p_widget, bool p_horizontal);
  32. static QSize availableScreenSize(QWidget *p_widget);
  33. static void openUrlByDesktop(const QUrl &p_url);
  34. // Given @p_event, try to process it by injecting proper event instead if it
  35. // triggers Vi operation.
  36. // Return true if @p_event is handled properly.
  37. // @p_escTargetWidget: the widget to accept the ESC event.
  38. static bool processKeyEventLikeVi(QWidget *p_widget,
  39. QKeyEvent *p_event,
  40. QWidget *p_escTargetWidget = nullptr);
  41. static bool isViControlModifier(int p_modifiers);
  42. static bool isMetaKey(int p_key);
  43. static void clearActionGroup(QActionGroup *p_actGroup);
  44. static void addActionShortcut(QAction *p_action,
  45. const QString &p_shortcut,
  46. Qt::ShortcutContext p_context = Qt::WindowShortcut);
  47. // Just add a shortcut text hint to the action.
  48. static void addActionShortcutText(QAction *p_action,
  49. const QString &p_shortcut);
  50. static QShortcut *createShortcut(const QString &p_shortcut,
  51. QWidget *p_widget,
  52. Qt::ShortcutContext p_context = Qt::WindowShortcut);
  53. static void updateSize(QWidget *p_widget);
  54. static void resizeToHideScrollBarLater(QScrollArea *p_scroll, bool p_vertical, bool p_horizontal);
  55. static QVector<QModelIndex> getVisibleIndexes(const QListView *p_view);
  56. static QString getMonospaceFont();
  57. static QAction *findActionByObjectName(const QList<QAction *> &p_actions, const QString &p_objName);
  58. static void insertActionAfter(QMenu *p_menu, QAction *p_after, QAction *p_action);
  59. static QFormLayout *createFormLayout(QWidget *p_parent = nullptr);
  60. // Select the base name part of the line edit content.
  61. static void selectBaseName(QLineEdit *p_lineEdit);
  62. private:
  63. static void resizeToHideScrollBar(QScrollArea *p_scroll, bool p_vertical, bool p_horizontal);
  64. };
  65. } // ns vnotex
  66. #endif // WIDGETUTILS_H