-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display ScoreItemModel in a Tree View
Added a ScoreTreeView that displays the score tree in a QTreeView, and also created a QDockWidget called ScoreTreeWidget which is enabled in the debug builds to inspect the score tree model.
- Loading branch information
1 parent
71a0da0
commit 48d860c
Showing
8 changed files
with
259 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
//============================================================================= | ||
// MuseScore | ||
// Music Composition & Notation | ||
// | ||
// Copyright (C) 2020 MuseScore BVBA | ||
// | ||
// This program is free software; you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License version 2 | ||
// as published by the Free Software Foundation and appearing in | ||
// the file LICENCE.GPL | ||
//============================================================================= | ||
|
||
#include "scoretreeview.h" | ||
|
||
namespace Ms { | ||
//--------------------------------------------------------- | ||
// selectElementAndChildren | ||
//--------------------------------------------------------- | ||
|
||
static void selectElementAndChildren(Element* el) | ||
{ | ||
el->score()->selection().add(el); | ||
for (ScoreElement* child : (*el)) { | ||
if (child) { | ||
selectElementAndChildren(toElement(child)); | ||
} | ||
} | ||
} | ||
|
||
//--------------------------------------------------------- | ||
// ScoreTreeView | ||
//--------------------------------------------------------- | ||
|
||
ScoreTreeView::ScoreTreeView(QWidget* parent) | ||
: QTreeView(parent) | ||
{ | ||
} | ||
|
||
//--------------------------------------------------------- | ||
// ~ScoreTreeView | ||
//--------------------------------------------------------- | ||
|
||
ScoreTreeView::~ScoreTreeView() | ||
{ | ||
} | ||
|
||
//--------------------------------------------------------- | ||
// setModel | ||
//--------------------------------------------------------- | ||
|
||
void ScoreTreeView::setModel(QAbstractItemModel* model) | ||
{ | ||
Q_ASSERT(model); // ensure there is a model | ||
Q_ASSERT(dynamic_cast<ScoreItemModel*>(model)); // ensure model is correct type | ||
QTreeView::setModel(model); | ||
} | ||
|
||
//--------------------------------------------------------- | ||
// model | ||
//--------------------------------------------------------- | ||
|
||
ScoreItemModel* ScoreTreeView::model() const | ||
{ | ||
return static_cast<ScoreItemModel*>(QTreeView::model()); | ||
} | ||
|
||
//--------------------------------------------------------- | ||
// currentChanged | ||
//--------------------------------------------------------- | ||
|
||
void ScoreTreeView::currentChanged(const QModelIndex& current, const QModelIndex& previous) | ||
{ | ||
selectElementAtIndex(current); | ||
QTreeView::currentChanged(current, previous); | ||
} | ||
|
||
//--------------------------------------------------------- | ||
// selectElementAtIndex | ||
/// Select the element represented by index and all its children | ||
/// in the score, so they are highlighted in the ScoreView. | ||
//--------------------------------------------------------- | ||
|
||
void ScoreTreeView::selectElementAtIndex(const QModelIndex& index) | ||
{ | ||
ScoreElement* el = static_cast<ScoreElement*>(index.internalPointer()); | ||
|
||
if (!el || !el->isElement()) { | ||
return; | ||
} | ||
|
||
Score* cs = model()->score(); | ||
cs->selection().deselectAll(); | ||
cs->selection().setState(SelState::LIST); | ||
|
||
// This function recursively adds all the children of the selected element | ||
// to the selection in the score. This is necessary because some non leaf node | ||
// elements cannot be selected by themselves (Segments, Measures) etc. | ||
selectElementAndChildren(toElement(el)); | ||
|
||
cs->setSelectionChanged(true); | ||
cs->update(); | ||
} | ||
} // namespace Ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//============================================================================= | ||
// MuseScore | ||
// Music Composition & Notation | ||
// | ||
// Copyright (C) 2020 MuseScore BVBA | ||
// | ||
// This program is free software; you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License version 2 | ||
// as published by the Free Software Foundation and appearing in | ||
// the file LICENCE.GPL | ||
//============================================================================= | ||
|
||
#ifndef __STREEVIEW_H__ | ||
#define __STREEVIEW_H__ | ||
|
||
#include "scoreitemmodel.h" | ||
|
||
namespace Ms { | ||
class ScoreTreeView : public QTreeView | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
ScoreTreeView(QWidget* parent); | ||
~ScoreTreeView(); | ||
|
||
void setModel(QAbstractItemModel* model); | ||
ScoreItemModel* model() const; | ||
|
||
void currentChanged(const QModelIndex& current, const QModelIndex& prev) override; | ||
|
||
private: | ||
void selectElementAtIndex(const QModelIndex& index); | ||
}; | ||
} // namespace Ms | ||
|
||
#endif // __STREEVIEW_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//============================================================================= | ||
// MuseScore | ||
// Music Composition & Notation | ||
// | ||
// Copyright (C) 2020 MuseScore BVBA | ||
// | ||
// This program is free software; you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License version 2 | ||
// as published by the Free Software Foundation and appearing in | ||
// the file LICENCE.GPL | ||
//============================================================================= | ||
|
||
#include "scoretreewidget.h" | ||
|
||
#include "libmscore/score.h" | ||
#include "scoreitemmodel.h" | ||
#include "scoretreeview.h" | ||
|
||
namespace Ms { | ||
//--------------------------------------------------------- | ||
// ScoreTreeWidget | ||
//--------------------------------------------------------- | ||
|
||
ScoreTreeWidget::ScoreTreeWidget(QMainWindow* parent) | ||
: QDockWidget("Score Tree", parent) | ||
{ | ||
_treeView = new ScoreTreeView(this); | ||
setWidget(_treeView); | ||
setAllowedAreas(Qt::RightDockWidgetArea); | ||
parent->addDockWidget(Qt::RightDockWidgetArea, this); | ||
} | ||
|
||
//--------------------------------------------------------- | ||
// ~ScoreTreeWidget | ||
//--------------------------------------------------------- | ||
|
||
ScoreTreeWidget::~ScoreTreeWidget() | ||
{ | ||
} | ||
|
||
//--------------------------------------------------------- | ||
// setScore | ||
//--------------------------------------------------------- | ||
|
||
void ScoreTreeWidget::setScore(Score* s) | ||
{ | ||
QAbstractItemModel* oldModel = _treeView->model(); | ||
if (s) { | ||
_treeView->setModel(new ScoreItemModel(s, _treeView)); | ||
} | ||
delete oldModel; | ||
} | ||
} // namespace Ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//============================================================================= | ||
// MuseScore | ||
// Music Composition & Notation | ||
// | ||
// Copyright (C) 2020 MuseScore BVBA | ||
// | ||
// This program is free software; you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License version 2 | ||
// as published by the Free Software Foundation and appearing in | ||
// the file LICENCE.GPL | ||
//============================================================================= | ||
|
||
#ifndef __STREEWIDGET_H__ | ||
#define __STREEWIDGET_H__ | ||
|
||
namespace Ms { | ||
class Score; | ||
class ScoreTreeView; | ||
|
||
class ScoreTreeWidget : public QDockWidget | ||
{ | ||
Q_OBJECT | ||
|
||
ScoreTreeView * _treeView; | ||
|
||
public: | ||
ScoreTreeWidget(QMainWindow* parent); | ||
~ScoreTreeWidget(); | ||
void setScore(Score* s); | ||
}; | ||
} // namespace Ms | ||
|
||
#endif // __STREEWIDGET_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters