openshot-audio  0.1.6
juce_LassoComponent.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission is granted to use this software under the terms of either:
8  a) the GPL v2 (or any later version)
9  b) the Affero GPL v3
10 
11  Details of these licenses can be found at: www.gnu.org/licenses
12 
13  JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15  A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17  ------------------------------------------------------------------------------
18 
19  To release a closed-source product which uses JUCE, commercial licenses are
20  available: visit www.juce.com for more information.
21 
22  ==============================================================================
23 */
24 
25 #ifndef JUCE_LASSOCOMPONENT_H_INCLUDED
26 #define JUCE_LASSOCOMPONENT_H_INCLUDED
27 
28 
29 //==============================================================================
38 template <class SelectableItemType>
40 {
41 public:
43  virtual ~LassoSource() {}
44 
54  virtual void findLassoItemsInArea (Array <SelectableItemType>& itemsFound,
55  const Rectangle<int>& area) = 0;
56 
65 };
66 
67 
68 //==============================================================================
95 template <class SelectableItemType>
96 class LassoComponent : public Component
97 {
98 public:
99  //==============================================================================
101  LassoComponent() : source (nullptr)
102  {
103  }
104 
105  //==============================================================================
117  {
118  jassert (source == nullptr); // this suggests that you didn't call endLasso() after the last drag...
119  jassert (lassoSource != nullptr); // the source can't be null!
120  jassert (getParentComponent() != nullptr); // you need to add this to a parent component for it to work!
121 
122  source = lassoSource;
123 
124  if (lassoSource != nullptr)
125  originalSelection = lassoSource->getLassoSelection().getItemArray();
126 
127  setSize (0, 0);
128  dragStartPos = e.getMouseDownPosition();
129  }
130 
143  void dragLasso (const MouseEvent& e)
144  {
145  if (source != nullptr)
146  {
147  setBounds (Rectangle<int> (dragStartPos, e.getPosition()));
148  setVisible (true);
149 
150  Array<SelectableItemType> itemsInLasso;
151  source->findLassoItemsInArea (itemsInLasso, getBounds());
152 
153  if (e.mods.isShiftDown())
154  {
155  itemsInLasso.removeValuesIn (originalSelection); // to avoid duplicates
156  itemsInLasso.addArray (originalSelection);
157  }
158  else if (e.mods.isCommandDown() || e.mods.isAltDown())
159  {
160  Array<SelectableItemType> originalMinusNew (originalSelection);
161  originalMinusNew.removeValuesIn (itemsInLasso);
162 
163  itemsInLasso.removeValuesIn (originalSelection);
164  itemsInLasso.addArray (originalMinusNew);
165  }
166 
167  source->getLassoSelection() = SelectedItemSet<SelectableItemType> (itemsInLasso);
168  }
169  }
170 
174  void endLasso()
175  {
176  source = nullptr;
177  originalSelection.clear();
178  setVisible (false);
179  }
180 
181  //==============================================================================
193  {
194  lassoFillColourId = 0x1000440,
195  lassoOutlineColourId = 0x1000441,
196  };
197 
198  //==============================================================================
200  void paint (Graphics& g) override
201  {
202  getLookAndFeel().drawLasso (g, *this);
203 
204  // this suggests that you've left a lasso comp lying around after the
205  // mouse drag has finished.. Be careful to call endLasso() when you get a
206  // mouse-up event.
208  }
209 
211  bool hitTest (int, int) override { return false; }
212 
213 private:
214  //==============================================================================
215  Array<SelectableItemType> originalSelection;
217  Point<int> dragStartPos;
218 
220 };
221 
222 
223 #endif // JUCE_LASSOCOMPONENT_H_INCLUDED
bool hitTest(int, int) override
Definition: juce_LassoComponent.h:211
void dragLasso(const MouseEvent &e)
Definition: juce_LassoComponent.h:143
void setSize(int newWidth, int newHeight)
Definition: juce_Component.cpp:1206
virtual void findLassoItemsInArea(Array< SelectableItemType > &itemsFound, const Rectangle< int > &area)=0
void endLasso()
Definition: juce_LassoComponent.h:174
virtual void drawLasso(Graphics &, Component &lassoComp)=0
Definition: juce_LassoComponent.h:39
bool isCommandDown() const noexcept
Definition: juce_ModifierKeys.h:66
LookAndFeel & getLookAndFeel() const noexcept
Definition: juce_Component.cpp:2120
Definition: juce_Point.h:39
Component * getParentComponent() const noexcept
Definition: juce_Component.h:762
Definition: juce_LassoComponent.h:195
virtual ~LassoSource()
Definition: juce_LassoComponent.h:43
bool isAltDown() const noexcept
Definition: juce_ModifierKeys.h:109
void beginLasso(const MouseEvent &e, LassoSource< SelectableItemType > *lassoSource)
Definition: juce_LassoComponent.h:116
virtual SelectedItemSet< SelectableItemType > & getLassoSelection()=0
Definition: juce_Rectangle.h:36
LassoComponent()
Definition: juce_LassoComponent.h:101
void addArray(const Type *elementsToAdd, int numElementsToAdd)
Definition: juce_Array.h:597
Definition: juce_Component.h:33
Definition: juce_LassoComponent.h:194
static bool JUCE_CALLTYPE isMouseButtonDownAnywhere() noexcept
Definition: juce_Component.cpp:2979
const ModifierKeys mods
Definition: juce_MouseEvent.h:110
const Rectangle< int > & getBounds() const noexcept
Definition: juce_Component.h:301
void removeValuesIn(const OtherArrayType &otherArray)
Definition: juce_Array.h:910
bool isShiftDown() const noexcept
Definition: juce_ModifierKeys.h:97
void clear()
Definition: juce_core.h:201
void setBounds(int x, int y, int width, int height)
Definition: juce_Component.cpp:1100
Definition: juce_Array.h:60
#define jassert(a)
Definition: juce_PlatformDefs.h:146
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
Definition: juce_PlatformDefs.h:198
Definition: juce_GraphicsContext.h:42
Point< int > getPosition() const noexcept
Definition: juce_MouseEvent.cpp:94
virtual void setVisible(bool shouldBeVisible)
Definition: juce_Component.cpp:511
Definition: juce_SelectedItemSet.h:43
#define nullptr
Definition: juce_CompilerSupport.h:151
Definition: juce_MouseEvent.h:36
Point< int > getMouseDownPosition() const noexcept
Definition: juce_MouseEvent.cpp:97
ColourIds
Definition: juce_LassoComponent.h:192
Definition: juce_LassoComponent.h:96
void paint(Graphics &g) override
Definition: juce_LassoComponent.h:200