ccColorScaleEditorWidget.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #pragma once
  2. //##########################################################################
  3. //# #
  4. //# CLOUDCOMPARE #
  5. //# #
  6. //# This program is free software; you can redistribute it and/or modify #
  7. //# it under the terms of the GNU General Public License as published by #
  8. //# the Free Software Foundation; version 2 or later of the License. #
  9. //# #
  10. //# This program is distributed in the hope that it will be useful, #
  11. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  12. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  13. //# GNU General Public License for more details. #
  14. //# #
  15. //# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
  16. //# #
  17. //##########################################################################
  18. //Inspired from ccColorScaleEditorWidget by Richard Steffen (LGPL 2.1)
  19. #include "CCPluginAPI.h"
  20. //Qt
  21. #include <QWidget>
  22. //qCC_db
  23. #include <ccColorScale.h>
  24. //! Color scale element as a widget
  25. class CCPLUGIN_LIB_API ColorScaleElementSlider : public QWidget, public ccColorScaleElement
  26. {
  27. Q_OBJECT
  28. public:
  29. //! Default constructor
  30. ColorScaleElementSlider(double relativePos = 0.0,
  31. QColor color = Qt::black,
  32. QWidget* parent = nullptr,
  33. Qt::Orientation orientation = Qt::Horizontal);
  34. //! Sets selection state
  35. inline void setSelected(bool state) { m_selected = state; }
  36. //! Returns selection state
  37. inline bool isSelected() const { return m_selected; }
  38. //! Comparison operator between two (pointers on) color scale elements
  39. static bool IsSmaller(const ColorScaleElementSlider* e1, const ColorScaleElementSlider* e2)
  40. {
  41. return e1->getRelativePos() < e2->getRelativePos();
  42. }
  43. protected:
  44. //inherited from QWidget
  45. void paintEvent(QPaintEvent* e) override;
  46. //! Selection state
  47. bool m_selected;
  48. //! Widget orientation
  49. Qt::Orientation m_orientation;
  50. };
  51. //! Set of color scale elements (widgets)
  52. class CCPLUGIN_LIB_API ColorScaleElementSliders
  53. {
  54. public:
  55. //! Type of the set of elements
  56. using Set = QList<ColorScaleElementSlider*>;
  57. //! Adds a slider element and sort the whole set
  58. /** Should be used instead of push_back/push_front!
  59. **/
  60. void addSlider(ColorScaleElementSlider* slider);
  61. //! Returns the size (shortcut)
  62. inline int size() const { return m_list.size(); }
  63. //! Sorts the set
  64. void sort();
  65. //! Remove all sliders
  66. void clear();
  67. //! Remove a given slider
  68. void removeAt(int i);
  69. //! Returns the currently selected slider index (or -1 if none)
  70. int selected() const;
  71. //! Returns the index of a given slider
  72. int indexOf(ColorScaleElementSlider* slider);
  73. //! Returns a given element
  74. inline ColorScaleElementSlider* element(int index) { return m_list.at(index); }
  75. //! Returns a given element (const version)
  76. inline const ColorScaleElementSlider* element(int index) const { return m_list.at(index); }
  77. //! Return the set of elements
  78. inline Set& elements() { return m_list; }
  79. //! Return the set of elements (const version)
  80. inline const Set& elements() const { return m_list; }
  81. protected:
  82. //! Set of elements
  83. Set m_list;
  84. };
  85. //! Shared set of color scale elements (widgets)
  86. using SharedColorScaleElementSliders = QSharedPointer<ColorScaleElementSliders>;
  87. //! Base color scale editor (sub)Widget
  88. /** A widget with a margin (along a preferred orientation)
  89. **/
  90. class CCPLUGIN_LIB_API ColorScaleEditorBaseWidget : public QWidget
  91. {
  92. Q_OBJECT
  93. public:
  94. //! Defautl constructor
  95. ColorScaleEditorBaseWidget(SharedColorScaleElementSliders sliders,
  96. Qt::Orientation orientation,
  97. int margin,
  98. QWidget* parent = nullptr)
  99. : QWidget(parent)
  100. , m_sliders(sliders)
  101. , m_orientation(orientation)
  102. , m_margin(margin)
  103. {}
  104. //! Returns useful length
  105. int length() const { return (m_orientation == Qt::Horizontal ? contentsRect().width() : contentsRect().height())-2*m_margin; }
  106. //! Sets associated sliders set
  107. virtual void setSliders(SharedColorScaleElementSliders sliders) { m_sliders = sliders; update(); }
  108. //! Returns orientation
  109. Qt::Orientation getOrientation() const { return m_orientation; }
  110. //! Returns margin
  111. int getMargin() const { return m_margin; }
  112. protected:
  113. //! Associated sliders
  114. SharedColorScaleElementSliders m_sliders;
  115. //! Orientation
  116. Qt::Orientation m_orientation;
  117. //! Margin
  118. int m_margin;
  119. };
  120. //! Color bar widget
  121. class CCPLUGIN_LIB_API ColorBarWidget : public ColorScaleEditorBaseWidget
  122. {
  123. Q_OBJECT
  124. public:
  125. //! Default constructor
  126. ColorBarWidget(SharedColorScaleElementSliders sliders, QWidget* parent = nullptr, Qt::Orientation orientation = Qt::Horizontal);
  127. Q_SIGNALS:
  128. //! Signal emitted when the mouse (left) button is clicked
  129. /** \param relativePos relative click position (between 0 and 1)
  130. **/
  131. void pointClicked(double relativePos);
  132. protected:
  133. //inherited from QWidget
  134. void paintEvent(QPaintEvent* e) override;
  135. void mousePressEvent(QMouseEvent* e) override;
  136. };
  137. //! All sliders widget
  138. class CCPLUGIN_LIB_API SlidersWidget : public ColorScaleEditorBaseWidget
  139. {
  140. Q_OBJECT
  141. public:
  142. //! Default constructor
  143. SlidersWidget(SharedColorScaleElementSliders sliders, QWidget* parent = nullptr, Qt::Orientation orientation = Qt::Horizontal);
  144. //! Manually selects a slider
  145. void select(int index, bool silent=false);
  146. //! Adds a new slider widget
  147. /** \param relativePos slider position (relatively to scale boundaries [0.0,1.0])
  148. \param color slider color
  149. \return created slider (pointer on)
  150. **/
  151. ColorScaleElementSlider* addNewSlider(double relativePos, QColor color);
  152. //! Updates slider position
  153. void updateSliderPos(int index);
  154. //! Updates all sliders positions
  155. void updateAllSlidersPos();
  156. Q_SIGNALS:
  157. //! Signal emitted when a slider is changed (position or color)
  158. void sliderModified(int index);
  159. //! Signal emitted when a slider is selected
  160. void sliderSelected(int index);
  161. protected:
  162. //inherited from QWidget
  163. void mousePressEvent(QMouseEvent* e) override;
  164. void mouseMoveEvent(QMouseEvent* e) override;
  165. //virtual void mouseReleaseEvent(QMouseEvent* e);
  166. void mouseDoubleClickEvent(QMouseEvent* e) override;
  167. void resizeEvent(QResizeEvent* e) override;
  168. };
  169. //! All sliders labels widget
  170. class CCPLUGIN_LIB_API SliderLabelWidget : public ColorScaleEditorBaseWidget
  171. {
  172. Q_OBJECT
  173. public:
  174. //! Default constructor
  175. SliderLabelWidget(SharedColorScaleElementSliders sliders, QWidget* parent = nullptr, Qt::Orientation orientation = Qt::Horizontal);
  176. //! Sets text color
  177. inline void setTextColor(QColor color) { m_textColor = color; }
  178. //! Sets displayed numbers precision
  179. inline void setPrecision(int precision) { m_precision = precision; }
  180. protected:
  181. //inherited from QWidget
  182. void paintEvent(QPaintEvent* e) override;
  183. //! Text color
  184. QColor m_textColor;
  185. //! Precision
  186. int m_precision;
  187. };
  188. //! Color scale editor dialog
  189. class CCPLUGIN_LIB_API ccColorScaleEditorWidget : public ColorScaleEditorBaseWidget
  190. {
  191. Q_OBJECT
  192. public:
  193. //! Default constructor
  194. ccColorScaleEditorWidget(QWidget* parent = nullptr, Qt::Orientation orientation = Qt::Horizontal);
  195. //! Destructor
  196. ~ccColorScaleEditorWidget() override = default;
  197. //! Returns the current number of color scale steps
  198. inline int getStepCount() const { return (m_sliders ? m_sliders->size() : 0); }
  199. //! Returns a given slider (pointer on)
  200. inline const ColorScaleElementSlider* getStep(int index) { return m_sliders ? m_sliders->elements().at(index) : nullptr; }
  201. //! Sets a given slider color
  202. void setStepColor(int index, QColor color);
  203. //! Sets a given slider relative position
  204. void setStepRelativePosition(int index, double relativePos);
  205. //! Returns currently selected step index
  206. inline int getSelectedStepIndex() const { return m_sliders ? m_sliders->selected() : -1; }
  207. //! Sets currently selected step index
  208. void setSelectedStepIndex(int index, bool silent=false);
  209. //! Deletes a given step
  210. /** Warning: first and last steps shouldn't be deleted!
  211. **/
  212. void deleteStep(int index);
  213. //! Exports the current color scale
  214. void exportColorScale(ccColorScale::Shared& destScale) const;
  215. //! Imports the current color scale
  216. void importColorScale(ccColorScale::Shared scale);
  217. //! Sets whether to show the color elements labels or not
  218. void showLabels(bool state);
  219. //! Sets the labels color
  220. void setLabelColor(QColor color);
  221. //! Sets the labels precision
  222. void setLabelPrecision(int precision);
  223. //inherited from ColorScaleEditorBaseWidget
  224. void setSliders(SharedColorScaleElementSliders sliders) override;
  225. Q_SIGNALS:
  226. //! Signal emitted when a slider is selected
  227. void stepSelected(int index);
  228. //! Signal emitted when a slider is modified
  229. void stepModified(int index);
  230. protected:
  231. //! Slot called when a 'point' is clicked on the color bar
  232. void onPointClicked(double relativePos);
  233. //! Slot called when a slider is moved or its color is changed
  234. void onSliderModified(int sliderIndex);
  235. //! Slot called when a slider is selected
  236. void onSliderSelected(int sliderIndex);
  237. protected:
  238. //! Associated color bar
  239. ColorBarWidget* m_colorBarWidget;
  240. //! Associated sliders widget
  241. SlidersWidget* m_slidersWidget;
  242. //! Associated (sliders) labels widget
  243. SliderLabelWidget* m_labelsWidget;
  244. };