Skip to content

Commit

Permalink
Enable Event drag&drop in LTMSidebar to allow reordering
Browse files Browse the repository at this point in the history
  • Loading branch information
amtriathlon committed Apr 10, 2018
1 parent 982d357 commit 0f7990a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/Core/Season.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,28 @@ void EditPhaseDialog::nameChanged()
{
applyButton->setEnabled(!nameEdit->text().isEmpty());
}

SeasonEventTreeView::SeasonEventTreeView()
{
setDragDropMode(QAbstractItemView::InternalMove);
setDragDropOverwriteMode(true);
}

void
SeasonEventTreeView::dropEvent(QDropEvent* event)
{
// get the list of the items that are about to be dropped
QTreeWidgetItem* item = selectedItems()[0];

// row we started on
int idx1 = indexFromItem(item).row();

// the default implementation takes care of the actual move inside the tree
QTreeWidget::dropEvent(event);

// moved to !
int idx2 = indexFromItem(item).row();

// notify subscribers in some useful way
Q_EMIT itemMoved(item, idx1, idx2);
}
13 changes: 13 additions & 0 deletions src/Core/Season.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,17 @@ class EditPhaseDialog : public QDialog
QDoubleSpinBox *lowEdit;
};

class SeasonEventTreeView : public QTreeWidget
{
Q_OBJECT

public:
SeasonEventTreeView();

signals:
void itemMoved(QTreeWidgetItem* item, int previous, int actual);

protected:
void dropEvent(QDropEvent* event);
};
#endif /* SEASON_H_ */
37 changes: 36 additions & 1 deletion src/Gui/LTMSidebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ LTMSidebar::LTMSidebar(Context *context) : QWidget(context->mainWindow), context
eventsWidget->addAction(moreEventAct);
connect(moreEventAct, SIGNAL(triggered(void)), this, SLOT(eventPopup(void)));

eventTree = new QTreeWidget;
eventTree = new SeasonEventTreeView;
eventTree->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
allEvents = eventTree->invisibleRootItem();
// Drop for Events
allEvents->setFlags(Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
allEvents->setText(0, tr("Events"));
eventTree->setFrameStyle(QFrame::NoFrame);
eventTree->setColumnCount(2);
Expand Down Expand Up @@ -229,6 +231,7 @@ LTMSidebar::LTMSidebar(Context *context) : QWidget(context->mainWindow), context

// events
connect(eventTree,SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(eventPopup(const QPoint &)));
connect(eventTree,SIGNAL(itemMoved(QTreeWidgetItem *,int, int)), this, SLOT(eventMoved(QTreeWidgetItem*, int, int)));

// presets
connect(chartTree,SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(presetPopup(const QPoint &)));
Expand Down Expand Up @@ -360,6 +363,7 @@ LTMSidebar::dateRangeTreeWidgetSelectionChanged()
for (i=0; i <dateRange->events.count(); i++) {
SeasonEvent event = dateRange->events.at(i);
QTreeWidgetItem *add = new QTreeWidgetItem(allEvents);
add->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
add->setText(0, event.name);
add->setText(1, event.date.toString("MMM d, yyyy"));
}
Expand Down Expand Up @@ -1275,6 +1279,7 @@ LTMSidebar::addEvent()
seasons->seasons[seasonindex].events.append(myevent);

QTreeWidgetItem *add = new QTreeWidgetItem(allEvents);
add->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
add->setText(0, myevent.name);
add->setText(1, myevent.date.toString("MMM d, yyyy"));

Expand Down Expand Up @@ -1358,6 +1363,36 @@ LTMSidebar::editEvent()
active = false;
}

void
LTMSidebar::eventMoved(QTreeWidgetItem*item, int oldposition, int newposition)
{
active = true;

if (dateRangeTree->selectedItems().count()) {

// if a phase is selected (rather than a season), get the season this phase belongs to
QTreeWidgetItem *selectedDateRange = dateRangeTree->selectedItems().first();
if (selectedDateRange->parent() != NULL) {
selectedDateRange = selectedDateRange->parent();
}

int seasonindex = allDateRanges->indexOfChild(selectedDateRange);

// report the move in the seasons
seasons->seasons[seasonindex].events.move(oldposition, newposition);

// save changes away
seasons->writeSeasons();

// deselect actual selection
eventTree->selectedItems().first()->setSelected(false);
// select the move/drop item
item->setSelected(true);
}

active = false;
}

void
LTMSidebar::addPhase()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Gui/LTMSidebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class LTMSidebar : public QWidget
void editEvent();
void deleteEvent();
void addEvent();
void eventMoved(QTreeWidgetItem *, int, int);

void addPhase();

Expand Down Expand Up @@ -118,7 +119,7 @@ class LTMSidebar : public QWidget
QTreeWidgetItem *allCharts;

GcSplitterItem *eventsWidget;
QTreeWidget *eventTree;
SeasonEventTreeView *eventTree;
QTreeWidgetItem *allEvents;

GcSplitterItem *filtersWidget;
Expand Down

0 comments on commit 0f7990a

Please sign in to comment.