Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "../JuceLibraryCode/JuceHeader.h"
- class MyTableModel : public TableListBoxModel
- {
- virtual int getNumRows() override
- {
- return data.size();
- }
- virtual void paintRowBackground(Graphics& g, int rowNumber, int width, int height, bool rowIsSelected) override
- {
- if (rowIsSelected)
- g.fillAll(Colours::lightblue);
- }
- virtual void paintCell(Graphics& g, int rowNumber, int columnId, int width, int height, bool rowIsSelected) override
- {
- g.setColour(Desktop::getInstance().getDefaultLookAndFeel().findColour(ListBox::textColourId));
- g.setFont(Font(14.0));
- if(columnId == 1)
- g.drawText(data[rowNumber], 2, 0, width - 4, height, Justification::centredLeft, true);
- g.setColour(Desktop::getInstance().getDefaultLookAndFeel().findColour(ListBox::backgroundColourId));
- g.fillRect(width - 1, 0, 1, height);
- }
- virtual var getDragSourceDescription(const SparseSet<int>& currentlySelectedRows) override
- {
- StringArray rows;
- for (int i = 0; i < currentlySelectedRows.size(); ++i)
- rows.add(String(currentlySelectedRows[i]));
- return rows.joinIntoString(", ");
- }
- public:
- void moveRowItem(int sourceIndex, int destIndex)
- {
- data.move(sourceIndex, destIndex);
- }
- private:
- StringArray data{ "A", "B", "C", "D", "E", "F", "G", "H" };
- };
- class MyTableView : public TableListBox, public DragAndDropTarget
- {
- private:
- //==============================================================================
- void paintListBoxItem(int rowNumber, Graphics& g, int width, int height, bool rowIsSelected) override
- {
- if (isDrawInsertPositionLine && insertRowIndex == rowNumber)
- {
- g.setColour(Colours::red);
- g.drawLine(0, 0, width, 0, 8);
- }
- }
- virtual bool isInterestedInDragSource(const SourceDetails& dragSourceDetails) override
- {
- return dragSourceDetails.sourceComponent == this;
- }
- virtual void itemDragEnter(const SourceDetails& dragSourceDetails) override
- {
- if (isInterestedInDragSource(dragSourceDetails))
- {
- isDrawInsertPositionLine = true;
- }
- updateContent();
- repaint();
- }
- virtual void itemDragMove(const SourceDetails& dragSourceDetails) override
- {
- auto rows = dragSourceDetails.description;
- insertRowIndex = getInsertionIndexForPosition(dragSourceDetails.localPosition.getX(), dragSourceDetails.localPosition.getY());
- if (isDrawInsertPositionLine)
- {
- insertRowIndex = getInsertionIndexForPosition(dragSourceDetails.localPosition.getX(), dragSourceDetails.localPosition.getY());
- }
- updateContent();
- repaint();
- }
- virtual void itemDragExit(const SourceDetails& dragSourceDetails) override
- {
- isDrawInsertPositionLine = false;
- // View‚ðXV
- updateContent();
- repaint();
- }
- virtual void itemDropped(const SourceDetails& dragSourceDetails) override
- {
- if (isInterestedInDragSource(dragSourceDetails))
- {
- auto rowNumber = (int)dragSourceDetails.description;
- auto model = dynamic_cast<MyTableModel*>(getModel());
- if (model)
- model->moveRowItem(rowNumber, rowNumber > insertRowIndex ? insertRowIndex : insertRowIndex - 1);
- insertRowIndex = -1;
- selectRow(-1);
- }
- updateContent();
- repaint();
- }
- bool isDrawInsertPositionLine { false };
- int insertRowIndex { -1 };
- };
- //==============================================================================
- /*
- This component lives inside our window, and this is where you should put all
- your controls and content.
- */
- class MainComponent : public Component, public DragAndDropContainer
- {
- public:
- //==============================================================================
- MainComponent();
- ~MainComponent();
- //==============================================================================
- void paint (Graphics&) override;
- void resized() override;
- private:
- //==============================================================================
- // Your private member variables go here...
- MyTableModel tableModel;
- MyTableView tableView;
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement