|
| 1 | + |
| 2 | +/** \ingroup gui |
| 3 | + * \class QgsColorRampButton |
| 4 | + * A cross platform button subclass for selecting color ramps. Will open color ramp dialogs when clicked. |
| 5 | + * Offers live updates to button from color ramp dialog. An attached drop down menu allows for access to |
| 6 | + * saved color ramps, as well as option to invert the current color ramp and create new ramps. |
| 7 | + * \note Added in version 3.0 |
| 8 | + */ |
| 9 | + |
| 10 | +class QgsColorRampButton : QToolButton |
| 11 | +{ |
| 12 | +%TypeHeaderCode |
| 13 | +#include <qgscolorrampbutton.h> |
| 14 | +%End |
| 15 | + |
| 16 | + public: |
| 17 | + |
| 18 | + /** Construct a new color ramp button. |
| 19 | + * @param parent The parent QWidget for the dialog |
| 20 | + * @param dialogTitle The title to show in the color ramp dialog |
| 21 | + */ |
| 22 | + QgsColorRampButton( QWidget *parent = nullptr, const QString& dialogTitle = QString() ); |
| 23 | + |
| 24 | + virtual ~QgsColorRampButton(); |
| 25 | + |
| 26 | + virtual QSize sizeHint() const; |
| 27 | + |
| 28 | + /** Return the current color ramp. |
| 29 | + * @returns currently selected color |
| 30 | + * @see setColor |
| 31 | + */ |
| 32 | + QgsColorRamp* colorRamp() const; |
| 33 | + |
| 34 | + /** Set the title for the color ramp dialog window. |
| 35 | + * @param title Title for the color ramp dialog |
| 36 | + * @see colorRampDialogTitle |
| 37 | + */ |
| 38 | + void setColorRampDialogTitle( const QString& title ); |
| 39 | + |
| 40 | + /** Returns the title for the color ramp dialog window. |
| 41 | + * @returns title for the color ramp dialog |
| 42 | + * @see setColorRampDialogTitle |
| 43 | + */ |
| 44 | + QString colorRampDialogTitle() const; |
| 45 | + |
| 46 | + /** Returns whether the button accepts live updates from QgsColorRampDialog. |
| 47 | + * @returns true if the button will be accepted immediately when the dialog's color ramp changes |
| 48 | + * @see setAcceptLiveUpdates |
| 49 | + */ |
| 50 | + bool acceptLiveUpdates() const; |
| 51 | + |
| 52 | + /** Sets whether the button accepts live updates from QgsColorRampDialog. Live updates may cause changes |
| 53 | + * that are not undoable on QColorRampDialog cancel. |
| 54 | + * @param accept set to true to enable live updates |
| 55 | + * @see acceptLiveUpdates |
| 56 | + */ |
| 57 | + void setAcceptLiveUpdates( const bool accept ); |
| 58 | + |
| 59 | + /** Sets whether the drop down menu should be shown for the button. The default behaviour is to |
| 60 | + * show the menu. |
| 61 | + * @param showMenu set to false to hide the drop down menu |
| 62 | + * @see showMenu |
| 63 | + */ |
| 64 | + void setShowMenu( const bool showMenu ); |
| 65 | + |
| 66 | + /** Returns whether the drop down menu is shown for the button. |
| 67 | + * @returns true if drop down menu is shown |
| 68 | + * @see setShowMenu |
| 69 | + */ |
| 70 | + bool showMenu() const; |
| 71 | + |
| 72 | + /** Sets the default color ramp for the button, which is shown in the button's drop down menu for the |
| 73 | + * "default color ramp" option. |
| 74 | + * @param colorramp default color ramp for the button. Set to a null pointer to disable the default color |
| 75 | + * ramp option. |
| 76 | + * @see defaultColorRamp |
| 77 | + */ |
| 78 | + void setDefaultColorRamp( QgsColorRamp* colorramp ); |
| 79 | + |
| 80 | + /** Returns the default color ramp for the button, which is shown in the button's drop down menu for the |
| 81 | + * "default color ramp" option. |
| 82 | + * @returns default color ramp for the button. Returns a null pointer if the default color ramp |
| 83 | + * option is disabled. |
| 84 | + * @see setDefaultColorRamp |
| 85 | + */ |
| 86 | + QgsColorRamp* defaultColorRamp() const; |
| 87 | + |
| 88 | + /** Sets whether a random colors option is shown in the button's drop down menu. |
| 89 | + * @param showNull set to true to show a null option |
| 90 | + * @see showRandom() |
| 91 | + */ |
| 92 | + void setShowRandomColorRamp( bool showRandom ); |
| 93 | + |
| 94 | + /** Returns whether random colors option is shown in the button's drop down menu. |
| 95 | + * @see setShowRandom() |
| 96 | + */ |
| 97 | + bool showRandomColorRamp() const; |
| 98 | + |
| 99 | + /** Returns true if the current color is null. |
| 100 | + * @see setShowNull() |
| 101 | + * @see showNull() |
| 102 | + */ |
| 103 | + bool isRandomColorRamp() const; |
| 104 | + |
| 105 | + /** Sets whether a set to null (clear) option is shown in the button's drop down menu. |
| 106 | + * @param showNull set to true to show a null option |
| 107 | + * @see showNull() |
| 108 | + * @see isNull() |
| 109 | + */ |
| 110 | + void setShowNull( bool showNull ); |
| 111 | + |
| 112 | + /** Returns whether the set to null (clear) option is shown in the button's drop down menu. |
| 113 | + * @see setShowNull() |
| 114 | + * @see isNull() |
| 115 | + */ |
| 116 | + bool showNull() const; |
| 117 | + |
| 118 | + /** Returns true if the current color is null. |
| 119 | + * @see setShowNull() |
| 120 | + * @see showNull() |
| 121 | + */ |
| 122 | + bool isNull() const; |
| 123 | + |
| 124 | + /** Sets the context string for the color ramp button. The context string is passed to all color ramp |
| 125 | + * preview icons shown in the button's drop down menu, to (eventually) allow them to customise their display colors |
| 126 | + * based on the context. |
| 127 | + * @param context context string for the color dialog button's color ramp preview icons |
| 128 | + * @see context |
| 129 | + */ |
| 130 | + void setContext( const QString& context ); |
| 131 | + |
| 132 | + /** Returns the context string for the color ramp button. The context string is passed to all color ramp |
| 133 | + * preview icons shown in the button's drop down menu, to (eventually) allow them to customise their display colors |
| 134 | + * based on the context. |
| 135 | + * @returns context context string for the color dialog button's color ramp preview icons |
| 136 | + * @see setContext |
| 137 | + */ |
| 138 | + QString context() const; |
| 139 | + |
| 140 | + /** Sets whether the color ramp button only shows gradient type ramps |
| 141 | + * @param gradientonly set to true to show only gradient type ramps |
| 142 | + * @see showGradientOnly |
| 143 | + */ |
| 144 | + void setShowGradientOnly( bool gradientonly ); |
| 145 | + |
| 146 | + /** Returns true if the color ramp button only shows gradient type ramps |
| 147 | + * @see setShowGradientOnly |
| 148 | + */ |
| 149 | + bool showGradientOnly() const; |
| 150 | + |
| 151 | + public slots: |
| 152 | + |
| 153 | + /** Sets the current color ramp for the button. Will emit a colorRampChanged() signal if the color ramp is different |
| 154 | + * to the previous color ramp. |
| 155 | + * @param colorramp New color ramp for the button |
| 156 | + * @see setRandomColorRamp, setColorRampFromName, colorRamp |
| 157 | + */ |
| 158 | + void setColorRamp( QgsColorRamp* colorramp ); |
| 159 | + |
| 160 | + /** Sets the current color ramp for the button to random colors. Will emit a colorRampChanged() signal |
| 161 | + * if the color ramp is different to the previous color ramp. |
| 162 | + * @see setColorRamp, setColorRampFromName, colorRamp |
| 163 | + */ |
| 164 | + void setRandomColorRamp(); |
| 165 | + |
| 166 | + /** Sets the current color ramp for the button using a saved color ramp name. Will emit a colorRampChanged() signal |
| 167 | + * if the color ramp is different to the previous color ramp. |
| 168 | + * @param name Name of saved color ramp |
| 169 | + * @see setColorRamp, setRandomColorRamp, colorRamp |
| 170 | + */ |
| 171 | + void setColorRampFromName( QString name = QString() ); |
| 172 | + |
| 173 | + /** Sets the background pixmap for the button based upon current color ramp. |
| 174 | + * @param colorramp Color ramp for button background. If no color ramp is specified, the button's current |
| 175 | + * color ramp will be used |
| 176 | + */ |
| 177 | + void setButtonBackground( QgsColorRamp* colorramp = nullptr ); |
| 178 | + |
| 179 | + /** Sets color ramp to the button's default color ramp, if set. |
| 180 | + * @see setDefaultColorRamp |
| 181 | + * @see defaultColorRamp |
| 182 | + * @see setToNull() |
| 183 | + */ |
| 184 | + void setToDefaultColorRamp(); |
| 185 | + |
| 186 | + /** Sets color ramp to null. |
| 187 | + * @see setToDefaultColorRamp() |
| 188 | + */ |
| 189 | + void setToNull(); |
| 190 | + |
| 191 | + signals: |
| 192 | + |
| 193 | + /** Emitted whenever a new color ramp is set for the button. The color ramp is always valid. |
| 194 | + * In case the new color ramp is the same, no signal is emitted to avoid infinite loops. |
| 195 | + */ |
| 196 | + void colorRampChanged(); |
| 197 | + |
| 198 | + protected: |
| 199 | + |
| 200 | + bool event( QEvent *e ); |
| 201 | + void changeEvent( QEvent* e ); |
| 202 | + void showEvent( QShowEvent* e ); |
| 203 | + void resizeEvent( QResizeEvent *event ); |
| 204 | + |
| 205 | + /** |
| 206 | + * Reimplemented to detect right mouse button clicks on the color ramp button |
| 207 | + */ |
| 208 | + void mousePressEvent( QMouseEvent* e ); |
| 209 | + |
| 210 | +}; |
0 commit comments