Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "editpitch.h"
23 :
24 : #include <QKeyEvent>
25 :
26 : #include "ui/view/widgetstatestore.h"
27 : #include "ui/view/widgetnavigationfix.h"
28 :
29 : #include "engraving/dom/pitchspelling.h"
30 :
31 : using namespace mu::notation;
32 : using namespace muse::ui;
33 :
34 : //---------------------------------------------------------
35 : // EditPitch
36 : // To select a MIDI pitch code using human-readable note names
37 : //---------------------------------------------------------
38 :
39 0 : EditPitch::EditPitch(QWidget* parent)
40 0 : : QDialog(parent)
41 : {
42 0 : setObjectName("EditPitchNew");
43 :
44 0 : setup();
45 :
46 0 : tableWidget->setCurrentCell(tableWidget->rowCount() - 1 - 5, 0); // select centre C by default
47 0 : }
48 :
49 0 : EditPitch::EditPitch(QWidget* parent, int midiCode)
50 0 : : QDialog(parent)
51 : {
52 0 : setObjectName("EditPitchEdit");
53 :
54 0 : setup();
55 :
56 0 : tableWidget->setCurrentCell(tableWidget->rowCount() - 1 - (midiCode / 12), midiCode % 12);
57 0 : }
58 :
59 0 : void EditPitch::showEvent(QShowEvent* event)
60 : {
61 0 : WidgetStateStore::restoreGeometry(this);
62 0 : QWidget::showEvent(event);
63 0 : }
64 :
65 0 : void EditPitch::hideEvent(QHideEvent* ev)
66 : {
67 0 : WidgetStateStore::saveGeometry(this);
68 0 : QWidget::hideEvent(ev);
69 0 : }
70 :
71 0 : bool EditPitch::eventFilter(QObject* obj, QEvent* event)
72 : {
73 0 : if (event->type() == QEvent::KeyPress) {
74 0 : QKeyEvent* keyEvent = dynamic_cast<QKeyEvent*>(event);
75 0 : if (keyEvent
76 0 : && WidgetNavigationFix::fixNavigationForTableWidget(
77 0 : WidgetNavigationFix::NavigationChain { tableWidget, buttonBox, buttonBox },
78 : keyEvent->key())) {
79 0 : return true;
80 : }
81 : }
82 :
83 0 : return QDialog::eventFilter(obj, event);
84 : }
85 :
86 0 : void EditPitch::setup()
87 : {
88 0 : setupUi(this);
89 0 : setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);
90 :
91 : //! NOTE: It is necessary for the correct start of navigation in the dialog
92 0 : setFocus();
93 :
94 0 : qApp->installEventFilter(this);
95 :
96 : // Populate table with localized pitch names and center them in cells
97 : static constexpr const char* noteName[] =
98 : { "C", "C♯", "D", "D♯", "E", "F", "F♯", "G", "G♯", "A", "A♯", "B", "C" };
99 :
100 0 : for (int row = 0; row < tableWidget->rowCount(); ++row) {
101 0 : for (int col = 0; col < tableWidget->columnCount(); ++col) {
102 0 : if (row == 0 && col > 7) {
103 : // Skip MIDI notes above G9
104 0 : break;
105 : }
106 0 : QTableWidgetItem* item = tableWidget->item(row, col);
107 0 : if (item) {
108 : int octave;
109 0 : if (col == tableWidget->columnCount() - 1) {
110 0 : octave = 10 - row;
111 : } else {
112 0 : octave = 9 - row;
113 : }
114 0 : std::string pitch = std::string(noteName[col]) + std::to_string(octave);
115 0 : item->setText(muse::mtrc("global/pitchName", pitch.c_str()));
116 0 : item->setTextAlignment(Qt::AlignCenter);
117 0 : }
118 : }
119 : }
120 0 : }
121 :
122 0 : void EditPitch::accept()
123 : {
124 0 : on_tableWidget_cellDoubleClicked(tableWidget->currentRow(), tableWidget->currentColumn());
125 0 : }
126 :
127 0 : void EditPitch::on_tableWidget_cellDoubleClicked(int row, int col)
128 : {
129 : // topmost row contains notes for 10-th MIDI octave (numbered as '9')
130 0 : int pitch = (tableWidget->rowCount() - 1 - row) * 12 + col;
131 0 : done(std::min(pitch, engraving::MAX_PITCH));
132 0 : }
|