1 //=========================================================
2 //  MusE
3 //  Linux Music Editor
4 //
5 //  function_dialog_base.cpp
6 //  (C) Copyright 2018 Tim E. Real (terminator356 on users dot sourceforge dot net)
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU General Public License
10 //  as published by the Free Software Foundation; version 2 of
11 //  the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU General Public License for more details.
17 //
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program; if not, write to the Free Software
20 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 //=========================================================
23 
24 #include <QAbstractButton>
25 #include "function_dialog_base.h"
26 
27 // Forwards from header:
28 #include <QWidget>
29 #include <QButtonGroup>
30 #include "xml.h"
31 
32 namespace MusEGui {
33 
FunctionDialogBase(QWidget * parent)34 FunctionDialogBase::FunctionDialogBase(QWidget* parent)
35   : QDialog(parent)
36 {
37   _range_container = NULL;
38   _parts_container = NULL;
39   _range_group = new QButtonGroup;
40   _parts_group = new QButtonGroup;
41   _range_group->setExclusive(true);
42   _parts_group->setExclusive(true);
43 }
44 
~FunctionDialogBase()45 FunctionDialogBase::~FunctionDialogBase()
46 {
47   delete _parts_group;
48   delete _range_group;
49 }
50 
pull_values()51 void FunctionDialogBase::pull_values()
52 {
53   //--------------------------------------------
54   // Grab current IDs from range and parts groups.
55   //--------------------------------------------
56 
57   setCurRange(_range_group->checkedId());
58   setCurParts(_parts_group->checkedId());
59 
60   //--------------------------------------------
61   // Set convenience return flags.
62   //--------------------------------------------
63 
64   setReturnFlags(calc_return_flags());
65 }
66 
accept()67 void FunctionDialogBase::accept()
68 {
69   pull_values();
70   QDialog::accept();
71 }
72 
setupButton(QButtonGroup * group,int buttonID,bool show)73 void FunctionDialogBase::setupButton(QButtonGroup* group, int buttonID, bool show)
74 {
75   QAbstractButton* bt = group->button(buttonID);
76   if(!bt)
77     return;
78   bt->setEnabled(show);
79   bt->setVisible(show);
80 }
81 
setupDialog()82 void FunctionDialogBase::setupDialog()
83 {
84   const FunctionDialogElements_t elem = elements();
85 
86   //--------------------------------------------
87   // Show or hide the range and parts containers.
88   // The containers need to be enabled first before
89   //  their controls can be enabled in setupButton().
90   //--------------------------------------------
91 
92   if(_range_container)
93   {
94     const bool show =
95       elem & (FunctionAllEventsButton | FunctionSelectedEventsButton |
96                    FunctionLoopedButton | FunctionSelectedLoopedButton);
97     _range_container->setEnabled(show);
98     _range_container->setVisible(show);
99   }
100 
101   if(_parts_container)
102   {
103     const bool show = elem & (FunctionAllPartsButton | FunctionSelectedPartsButton);
104     _parts_container->setEnabled(show);
105     _parts_container->setVisible(show);
106   }
107 
108   //--------------------------------------------
109   // Now show or hide the individual controls in
110   //  the range and parts containers.
111   //--------------------------------------------
112 
113   setupButton(_range_group, FunctionAllEventsButton, elem & FunctionAllEventsButton);
114   setupButton(_range_group, FunctionSelectedEventsButton, elem & FunctionSelectedEventsButton);
115   setupButton(_range_group, FunctionLoopedButton, elem & FunctionLoopedButton);
116   setupButton(_range_group, FunctionSelectedLoopedButton, elem & FunctionSelectedLoopedButton);
117 
118   setupButton(_parts_group, FunctionAllPartsButton, elem & FunctionAllPartsButton);
119   setupButton(_parts_group, FunctionSelectedPartsButton, elem & FunctionSelectedPartsButton);
120 
121   //---------------------------------------
122   // If the current range is invalid or the
123   //  button is not visible, choose another.
124   //---------------------------------------
125 
126   QAbstractButton* bt = _range_group->button(curRange());
127   if(!bt || !bt->isEnabled())
128   {
129     QList<QAbstractButton*> bl = _range_group->buttons();
130     const int range_group_sz = bl.size();
131     for(int i = 0; i < range_group_sz; ++i)
132     {
133       bt = bl.at(i);
134       if(bt->isEnabled())
135       {
136         setCurRange(_range_group->id(bt));
137         break;
138       }
139     }
140   }
141   bt =_range_group->button(curRange());
142   if(bt)
143     bt->setChecked(true);
144 
145   //---------------------------------------
146   // If the current parts is invalid or the
147   //  button is not visible, choose another.
148   //---------------------------------------
149 
150   bt = _parts_group->button(curParts());
151   if(!bt || !bt->isEnabled())
152   {
153     QList<QAbstractButton*> bl = _parts_group->buttons();
154     const int parts_group_sz = bl.size();
155     for(int i = 0; i < parts_group_sz; ++i)
156     {
157       bt = bl.at(i);
158       if(bt->isEnabled())
159       {
160         setCurParts(_parts_group->id(bt));
161         break;
162       }
163     }
164   }
165   bt = _parts_group->button(curParts());
166   if(bt)
167     bt->setChecked(true);
168 }
169 
exec()170 int FunctionDialogBase::exec()
171 {
172   //-----------------------------------
173   // Set up the dialog according to the
174   //  various settings.
175   //-----------------------------------
176 
177   setupDialog();
178 
179   //-----------------------------------
180   // Now execute the dialog.
181   //-----------------------------------
182 
183   return QDialog::exec();
184 }
185 
read_configuration(const QString &,MusECore::Xml &)186 bool FunctionDialogBase::read_configuration(const QString& /*tag*/, MusECore::Xml& /*xml*/)
187 {
188 //   if (tag == "range")
189 //   {
190 //     setCurRange(xml.parseInt());
191 //     return true;
192 //   }
193 //   else if (tag == "parts")
194 //   {
195 //     setCurParts(xml.parseInt());
196 //     return true;
197 //   }
198 
199   return false;
200 }
201 
write_configuration(int,MusECore::Xml &)202 void FunctionDialogBase::write_configuration(int /*level*/, MusECore::Xml& /*xml*/)
203 {
204 //   xml.intTag(level, "range", curRange());
205 //   xml.intTag(level, "parts", curParts());
206 }
207 
208 } // namespace MusEGui
209 
210 
211