Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "../JuceLibraryCode/JuceHeader.h"
  4.  
  5. class MyTableModel : public TableListBoxModel
  6. {
  7.  
  8. virtual int getNumRows() override
  9. {
  10. return data.size();
  11. }
  12.  
  13. virtual void paintRowBackground(Graphics& g, int rowNumber, int width, int height, bool rowIsSelected) override
  14. {
  15. if (rowIsSelected)
  16. g.fillAll(Colours::lightblue);
  17. }
  18.  
  19. virtual void paintCell(Graphics& g, int rowNumber, int columnId, int width, int height, bool rowIsSelected) override
  20. {
  21. g.setColour(Desktop::getInstance().getDefaultLookAndFeel().findColour(ListBox::textColourId));
  22. g.setFont(Font(14.0));
  23.  
  24. if(columnId == 1)
  25. g.drawText(data[rowNumber], 2, 0, width - 4, height, Justification::centredLeft, true);
  26.  
  27. g.setColour(Desktop::getInstance().getDefaultLookAndFeel().findColour(ListBox::backgroundColourId));
  28. g.fillRect(width - 1, 0, 1, height);
  29. }
  30.  
  31. virtual var getDragSourceDescription(const SparseSet<int>& currentlySelectedRows) override
  32. {
  33. StringArray rows;
  34.  
  35. for (int i = 0; i < currentlySelectedRows.size(); ++i)
  36. rows.add(String(currentlySelectedRows[i]));
  37.  
  38. return rows.joinIntoString(", ");
  39. }
  40.  
  41. public:
  42. void moveRowItem(int sourceIndex, int destIndex)
  43. {
  44. data.move(sourceIndex, destIndex);
  45. }
  46.  
  47. private:
  48. StringArray data{ "A", "B", "C", "D", "E", "F", "G", "H" };
  49. };
  50.  
  51. class MyTableView : public TableListBox, public DragAndDropTarget
  52. {
  53. private:
  54. //==============================================================================
  55. void paintListBoxItem(int rowNumber, Graphics& g, int width, int height, bool rowIsSelected) override
  56. {
  57. if (isDrawInsertPositionLine && insertRowIndex == rowNumber)
  58. {
  59. g.setColour(Colours::red);
  60. g.drawLine(0, 0, width, 0, 8);
  61. }
  62. }
  63.  
  64. virtual bool isInterestedInDragSource(const SourceDetails& dragSourceDetails) override
  65. {
  66. return dragSourceDetails.sourceComponent == this;
  67. }
  68.  
  69. virtual void itemDragEnter(const SourceDetails& dragSourceDetails) override
  70. {
  71. if (isInterestedInDragSource(dragSourceDetails))
  72. {
  73. isDrawInsertPositionLine = true;
  74. }
  75.  
  76. updateContent();
  77. repaint();
  78. }
  79.  
  80. virtual void itemDragMove(const SourceDetails& dragSourceDetails) override
  81. {
  82. auto rows = dragSourceDetails.description;
  83. insertRowIndex = getInsertionIndexForPosition(dragSourceDetails.localPosition.getX(), dragSourceDetails.localPosition.getY());
  84.  
  85. if (isDrawInsertPositionLine)
  86. {
  87. insertRowIndex = getInsertionIndexForPosition(dragSourceDetails.localPosition.getX(), dragSourceDetails.localPosition.getY());
  88. }
  89.  
  90. updateContent();
  91. repaint();
  92. }
  93.  
  94. virtual void itemDragExit(const SourceDetails& dragSourceDetails) override
  95. {
  96. isDrawInsertPositionLine = false;
  97.  
  98. // View‚ðXV
  99. updateContent();
  100. repaint();
  101. }
  102.  
  103. virtual void itemDropped(const SourceDetails& dragSourceDetails) override
  104. {
  105. if (isInterestedInDragSource(dragSourceDetails))
  106. {
  107. auto rowNumber = (int)dragSourceDetails.description;
  108.  
  109. auto model = dynamic_cast<MyTableModel*>(getModel());
  110. if (model)
  111. model->moveRowItem(rowNumber, rowNumber > insertRowIndex ? insertRowIndex : insertRowIndex - 1);
  112.  
  113. insertRowIndex = -1;
  114. selectRow(-1);
  115. }
  116.  
  117. updateContent();
  118. repaint();
  119. }
  120.  
  121. bool isDrawInsertPositionLine { false };
  122. int insertRowIndex { -1 };
  123. };
  124.  
  125. //==============================================================================
  126. /*
  127. This component lives inside our window, and this is where you should put all
  128. your controls and content.
  129. */
  130. class MainComponent : public Component, public DragAndDropContainer
  131. {
  132. public:
  133. //==============================================================================
  134. MainComponent();
  135. ~MainComponent();
  136.  
  137. //==============================================================================
  138. void paint (Graphics&) override;
  139. void resized() override;
  140.  
  141. private:
  142. //==============================================================================
  143. // Your private member variables go here...
  144. MyTableModel tableModel;
  145. MyTableView tableView;
  146.  
  147. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
  148. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement