window_theme_editor_block.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "ui/rp_widget.h"
  9. namespace Ui {
  10. class BoxContent;
  11. } // namespace Ui;
  12. class ColorEditor;
  13. namespace Window {
  14. namespace Theme {
  15. class EditorBlock final : public Ui::RpWidget {
  16. public:
  17. enum class Type {
  18. Existing,
  19. New,
  20. };
  21. struct Context {
  22. struct {
  23. QPointer<Ui::BoxContent> box;
  24. QPointer<ColorEditor> editor;
  25. } colorEditor;
  26. QString name;
  27. QString possibleCopyOf;
  28. rpl::event_stream<> updated;
  29. rpl::event_stream<> resized;
  30. struct AppendData {
  31. QString name;
  32. QString possibleCopyOf;
  33. QColor value;
  34. QString description;
  35. };
  36. rpl::event_stream<AppendData> appended;
  37. struct ChangeData {
  38. QStringList names;
  39. QColor value;
  40. };
  41. rpl::event_stream<ChangeData> changed;
  42. struct EditionData {
  43. QString name;
  44. QString copyOf;
  45. QColor value;
  46. };
  47. rpl::event_stream<EditionData> pending;
  48. struct ScrollData {
  49. Type type = {};
  50. int position = 0;
  51. int height = 0;
  52. };
  53. rpl::event_stream<ScrollData> scroll;
  54. };
  55. EditorBlock(QWidget *parent, Type type, Context *context);
  56. void filterRows(const QString &query);
  57. void chooseRow();
  58. bool hasSelected() const {
  59. return (_selected >= 0);
  60. }
  61. void clearSelected() {
  62. setSelected(-1);
  63. }
  64. bool selectSkip(int direction);
  65. void feed(const QString &name, QColor value, const QString &copyOfExisting = QString());
  66. bool feedCopy(const QString &name, const QString &copyOf);
  67. const QColor *find(const QString &name);
  68. bool feedDescription(const QString &name, const QString &description);
  69. void sortByDistance(const QColor &to);
  70. protected:
  71. void paintEvent(QPaintEvent *e) override;
  72. int resizeGetHeight(int newWidth) override;
  73. void mousePressEvent(QMouseEvent *e) override;
  74. void mouseReleaseEvent(QMouseEvent *e) override;
  75. void mouseMoveEvent(QMouseEvent *e) override;
  76. void leaveEventHook(QEvent *e) override;
  77. private:
  78. class Row;
  79. void addRow(const QString &name, const QString &copyOf, QColor value);
  80. void removeRow(const QString &name, bool removeCopyReferences = true);
  81. void addToSearch(const Row &row);
  82. void removeFromSearch(const Row &row);
  83. template <typename Callback>
  84. void enumerateRows(Callback callback);
  85. template <typename Callback>
  86. void enumerateRows(Callback callback) const;
  87. template <typename Callback>
  88. void enumerateRowsFrom(int top, Callback callback);
  89. template <typename Callback>
  90. void enumerateRowsFrom(int top, Callback callback) const;
  91. Row &rowAtIndex(int index);
  92. int findRowIndex(const QString &name) const;
  93. Row *findRow(const QString &name);
  94. int findRowIndex(const Row *row);
  95. void updateRow(const Row &row);
  96. void paintRow(Painter &p, int index, const Row &row);
  97. void updateSelected(QPoint localPosition);
  98. void setSelected(int selected);
  99. void setPressed(int pressed);
  100. void addRowRipple(int index);
  101. void stopLastRipple(int index);
  102. void scrollToSelected();
  103. bool isEditing() const {
  104. return !_context->name.isEmpty();
  105. }
  106. void saveEditing(QColor value);
  107. void cancelEditing();
  108. bool checkCopyOf(int index, const QString &possibleCopyOf);
  109. void checkCopiesChanged(int startIndex, QStringList names, QColor value);
  110. void activateRow(const Row &row);
  111. bool isSearch() const {
  112. return !_searchQuery.isEmpty();
  113. }
  114. void searchByQuery(QString query);
  115. void resetSearch() {
  116. searchByQuery(QString());
  117. }
  118. Type _type = Type::Existing;
  119. Context *_context = nullptr;
  120. std::vector<Row> _data;
  121. QMap<QString, int> _indices;
  122. QString _searchQuery;
  123. std::vector<int> _searchResults;
  124. base::flat_map<QChar, base::flat_set<int>> _searchIndex;
  125. int _selected = -1;
  126. int _pressed = -1;
  127. int _editing = -1;
  128. QPoint _lastGlobalPos;
  129. bool _mouseSelection = false;
  130. QBrush _transparent;
  131. };
  132. } // namespace Theme
  133. } // namespace Window