1 //=============================================================================
2 //  MuseScore
3 //  Music Composition & Notation
4 //
5 //  Copyright (C) 2019 Werner Schweer and others
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License version 2.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //=============================================================================
19 
20 #include "workspacecombobox.h"
21 
22 #include "musescore.h"
23 #include "preferences.h"
24 #include "workspace.h"
25 
26 namespace Ms {
27 
28 //---------------------------------------------------------
29 //   WorkspaceComboBox
30 //---------------------------------------------------------
31 
WorkspaceComboBox(MuseScore * mScore,QWidget * parent)32 WorkspaceComboBox::WorkspaceComboBox(MuseScore* mScore, QWidget* parent)
33    : QComboBox(parent), _mscore(mScore)
34       {
35       retranslate();
36       connect(_mscore, &MuseScore::workspacesChanged, this, &WorkspaceComboBox::updateWorkspaces);
37       connect(
38          this, QOverload<int>::of(&WorkspaceComboBox::activated),
39          this, &WorkspaceComboBox::workspaceSelected
40          );
41       }
42 
43 //---------------------------------------------------------
44 //   WorkspaceComboBox::changeEvent
45 //---------------------------------------------------------
46 
changeEvent(QEvent * e)47 void WorkspaceComboBox::changeEvent(QEvent* e)
48       {
49       QComboBox::changeEvent(e);
50       switch(e->type()) {
51             case QEvent::LanguageChange:
52                   retranslate();
53                   break;
54             default:
55                   break;
56             }
57       }
58 
59 //---------------------------------------------------------
60 //   WorkspaceComboBox::retranslate
61 //---------------------------------------------------------
62 
retranslate()63 void WorkspaceComboBox::retranslate()
64       {
65       setToolTip(tr("Select workspace"));
66       updateWorkspaces();
67       }
68 
69 //---------------------------------------------------------
70 //   WorkspaceComboBox::updateWorkspaces
71 //---------------------------------------------------------
72 
updateWorkspaces()73 void WorkspaceComboBox::updateWorkspaces()
74       {
75       if (blockUpdateWorkspaces)
76             return;
77 
78       clear();
79       const QList<Workspace*> pl = WorkspacesManager::visibleWorkspaces();
80       int idx = 0;
81       int curIdx = -1;
82       for (Workspace* p : pl) {
83             addItem(qApp->translate("Ms::Workspace", p->name().toUtf8()), p->path());
84             if (p->id() == preferences.getString(PREF_APP_WORKSPACE))
85                   curIdx = idx;
86             ++idx;
87             }
88 
89       //select first workspace in the list if the stored workspace vanished
90       Q_ASSERT(!pl.isEmpty());
91       if (curIdx == -1)
92             curIdx = 0;
93 
94       setCurrentIndex(curIdx);
95       }
96 
97 //---------------------------------------------------------
98 //   WorkspaceComboBox::workspaceSelected
99 //---------------------------------------------------------
100 
workspaceSelected(int idx)101 void WorkspaceComboBox::workspaceSelected(int idx)
102       {
103       if (idx < 0)
104             return;
105 
106       Workspace* w = WorkspacesManager::visibleWorkspaces().at(idx);
107       if (w != WorkspacesManager::currentWorkspace()) {
108             blockUpdateWorkspaces = true;
109             _mscore->changeWorkspace(w);
110             blockUpdateWorkspaces = false;
111             }
112       }
113 
114 } // namespace Ms
115