summaryrefslogtreecommitdiff
path: root/muse2/muse/cobject.cpp
blob: 5e007fe3be97a8a0f3850a43d6ebe0356cd8f600 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//=========================================================
//  MusE
//  Linux Music Editor
//  $Id: cobject.cpp,v 1.4 2004/02/02 12:10:09 wschweer Exp $
//
//  (C) Copyright 1999/2000 Werner Schweer (ws@seh.de)
//=========================================================

#include "cobject.h"
#include "xml.h"
#include "gui.h"
#include "globals.h"
#include "app.h"

#include <QMdiSubWindow>
#include <QToolBar>
#include <QMenuBar>
#include <QAction>

using std::list;

int TopWin::_widthInit[TOPLEVELTYPE_LAST_ENTRY];
int TopWin::_heightInit[TOPLEVELTYPE_LAST_ENTRY];
QByteArray TopWin::_toolbarSharedInit[TOPLEVELTYPE_LAST_ENTRY];
QByteArray TopWin::_toolbarNonsharedInit[TOPLEVELTYPE_LAST_ENTRY];
bool TopWin::_sharesWhenFree[TOPLEVELTYPE_LAST_ENTRY];
bool TopWin::_sharesWhenSubwin[TOPLEVELTYPE_LAST_ENTRY];
bool TopWin::_defaultSubwin[TOPLEVELTYPE_LAST_ENTRY];
bool TopWin::initInited=false;

TopWin::TopWin(ToplevelType t, QWidget* parent, const char* name, Qt::WindowFlags f)
                                               : QMainWindow(parent, f)
      {
      if (initInited==false)
      {
        for (int i=0;i<TOPLEVELTYPE_LAST_ENTRY;i++)
        {
          _widthInit[i]=800;
          _heightInit[i]=600;
          _sharesWhenFree[i]=false;
          _sharesWhenSubwin[i]=true;
          _defaultSubwin[i]=false;
        }
        
        _defaultSubwin[ARRANGER]=true;
        
        _defaultSubwin[SCORE]=true; //FINDMICH
        _sharesWhenFree[SCORE]=true;
        
        initInited=true;
      }  
      
      initalizing=true;
      
      _type=t;
      
      
      
      setObjectName(QString(name));
      // Allow multiple rows.  Tim.
      //setDockNestingEnabled(true);
      setIconSize(ICON_SIZE);
      
      subwinAction=new QAction(tr("As subwindow"), this);
      subwinAction->setCheckable(true);
      connect(subwinAction, SIGNAL(toggled(bool)), SLOT(setIsMdiWin(bool)));

      shareAction=new QAction(tr("Shares tools and menu"), this);
      shareAction->setCheckable(true);
      connect(shareAction, SIGNAL(toggled(bool)), SLOT(shareToolsAndMenu(bool)));

      mdisubwin=NULL;
      _sharesToolsAndMenu=_defaultSubwin[_type] ? _sharesWhenSubwin[_type] : _sharesWhenFree[_type];
      if (_defaultSubwin[_type])
        setIsMdiWin(true);
      
      
      subwinAction->setChecked(isMdiWin());
      shareAction->setChecked(_sharesToolsAndMenu);
      }



//---------------------------------------------------------
//   readStatus
//---------------------------------------------------------

void TopWin::readStatus(Xml& xml)
      {
      for (;;) {
            Xml::Token token = xml.parse();
            if (token == Xml::Error || token == Xml::End)
                  break;
            QString tag = xml.s1();
            switch (token) {
                  case Xml::TagStart:
                        if (tag == "geometry") {
                              QRect r(readGeometry(xml, tag));
                              resize(r.size());
                              move(r.topLeft());
                              }
                        else if (tag == "toolbars") {
                              if (!restoreState(QByteArray::fromHex(xml.parse1().toAscii())))
                                    fprintf(stderr,"ERROR: couldn't restore toolbars. however, this is not really a problem.\n");
                              }
                        else
                              xml.unknown("TopWin");
                        break;
                  case Xml::TagEnd:
                        if (tag == "topwin")
                              return;
                  default:
                        break;
                  }
            }
      }

//---------------------------------------------------------
//   writeStatus
//---------------------------------------------------------

void TopWin::writeStatus(int level, Xml& xml) const
      {
      xml.tag(level++, "topwin");
      xml.tag(level++, "geometry x=\"%d\" y=\"%d\" w=\"%d\" h=\"%d\"",
            geometry().x(),
            geometry().y(),
            geometry().width(),
            geometry().height());
      xml.tag(level--, "/geometry");
      
      xml.strTag(level, "toolbars", saveState().toHex().data());

      xml.tag(level, "/topwin");
      }

void TopWin::hide()
{
  if (mdisubwin)
    mdisubwin->close();
  
  QMainWindow::hide();
}

void TopWin::show()
{
  if (mdisubwin)
    mdisubwin->show();
  
  QMainWindow::show();
}

void TopWin::setVisible(bool param)
{
  if (mdisubwin)
  {
    if (param)
      mdisubwin->show();
    else
      mdisubwin->close();
  }
  QMainWindow::setVisible(param);
}

QMdiSubWindow* TopWin::createMdiWrapper()
{
  if (mdisubwin==NULL)
  {
    mdisubwin = new QMdiSubWindow();
    mdisubwin->setWidget(this);
  }
  
  return mdisubwin;
}

void TopWin::setIsMdiWin(bool val)
{
  if (val)
  {
    if (!isMdiWin())
    {
      bool vis=isVisible();
      QMdiSubWindow* subwin = createMdiWrapper();
      muse->addMdiSubWindow(subwin);
      subwin->setVisible(vis);
      
      if (_sharesToolsAndMenu == _sharesWhenFree[_type])
        shareToolsAndMenu(_sharesWhenSubwin[_type]);
      
      subwinAction->setChecked(true);
    }
    else
    {
      if (debugMsg) printf("TopWin::setIsMdiWin(true) called, but window is already a MDI win\n");
    }
  }
  else
  {
    if (isMdiWin())
    {
      bool vis=isVisible();
      QMdiSubWindow* mdisubwin_temp=mdisubwin;
      mdisubwin=NULL;
      setParent(NULL);
      mdisubwin_temp->hide();
      //TODO FINDMICH evtl noch ein signal emitten oder sowas?
      delete mdisubwin_temp;
      
      printf("FINDMICH unMDIfied, visible is %i\n",vis);
      setVisible(vis);

      if (_sharesToolsAndMenu == _sharesWhenSubwin[_type])
        shareToolsAndMenu(_sharesWhenFree[_type]);
            
      subwinAction->setChecked(false);
    }
    else
    {
      if (debugMsg) printf("TopWin::setIsMdiWin(false) called, but window is not a MDI win\n");
    }
  }
}

bool TopWin::isMdiWin()
{
  return (mdisubwin!=NULL);
}

void TopWin::insertToolBar(QToolBar*, QToolBar*) { printf("ERROR: THIS SHOULD NEVER HAPPEN: TopWin::insertToolBar called, but it's not implemented! ignoring it\n"); }
void TopWin::insertToolBarBreak(QToolBar*) { printf("ERROR: THIS SHOULD NEVER HAPPEN: TopWin::insertToolBarBreak called, but it's not implemented! ignoring it\n"); }
void TopWin::removeToolBar(QToolBar*) { printf("ERROR: THIS SHOULD NEVER HAPPEN: TopWin::removeToolBar called, but it's not implemented! ignoring it\n"); }
void TopWin::removeToolBarBreak(QToolBar*) { printf("ERROR: THIS SHOULD NEVER HAPPEN: TopWin::removeToolBarBreak called, but it's not implemented! ignoring it\n"); }
void TopWin::addToolBar(Qt::ToolBarArea, QToolBar* tb) { printf("ERROR: THIS SHOULD NEVER HAPPEN: TopWin::addToolBar(Qt::ToolBarArea, QToolBar*) called, but it's not implemented!\nusing addToolBar(QToolBar*) instead\n"); addToolBar(tb);}

void TopWin::addToolBar(QToolBar* toolbar)
{
  _toolbars.push_back(toolbar);
  
  if (!_sharesToolsAndMenu)
    QMainWindow::addToolBar(toolbar);
}

QToolBar* TopWin::addToolBar(const QString& title)
{
  QToolBar* toolbar = new QToolBar(title, this);
  addToolBar(toolbar);
  return toolbar;
}


void TopWin::shareToolsAndMenu(bool val)
{
  _sharesToolsAndMenu = val;
  
  if (!val)
  {
    muse->shareMenuAndToolbarChanged(this, false);
    
    for (list<QToolBar*>::iterator it=_toolbars.begin(); it!=_toolbars.end(); it++)
      if (*it != NULL)
        QMainWindow::addToolBar(*it);
      else
        QMainWindow::addToolBarBreak();
    
    menuBar()->show();
  }
  else
  {
    for (list<QToolBar*>::iterator it=_toolbars.begin(); it!=_toolbars.end(); it++)
      if (*it != NULL)
      {
        QMainWindow::removeToolBar(*it); // this does NOT delete the toolbar, which is good
        (*it)->setParent(NULL);
      }
    
    menuBar()->hide();
    
    muse->shareMenuAndToolbarChanged(this, true);
  }
  
  shareAction->setChecked(val);
}



//---------------------------------------------------------
//   storeInitialState
//---------------------------------------------------------

void TopWin::storeInitialState()
      {
        if (initalizing)
          printf("THIS SHOULD NEVER HAPPEN: STORE INIT STATE CALLED WHILE INITING! please IMMEDIATELY report that to flo!\n");

        _widthInit[_type] = width();
        _heightInit[_type] = height();
        if (sharesToolsAndMenu())
        {
          if (muse->getCurrentMenuSharingTopwin() == this)
            _toolbarSharedInit[_type] = muse->saveState();
        }
        else
          _toolbarNonsharedInit[_type] = saveState();
      }

//---------------------------------------------------------
//   readConfiguration
//---------------------------------------------------------

void TopWin::readConfiguration(ToplevelType t, Xml& xml)
      {
      for (;;) {
            Xml::Token token = xml.parse();
            if (token == Xml::Error || token == Xml::End)
                  break;
            const QString& tag = xml.s1();
            switch (token) {
                  case Xml::TagStart:
                        if (tag == "width")
                              _widthInit[t] = xml.parseInt();
                        else if (tag == "height")
                              _heightInit[t] = xml.parseInt();
                        else if (tag == "nonshared_toolbars")
                              _toolbarNonsharedInit[t] = QByteArray::fromHex(xml.parse1().toAscii());
                        else if (tag == "shared_toolbars")
                              _toolbarSharedInit[t] = QByteArray::fromHex(xml.parse1().toAscii());
                        else
                              xml.unknown("TopWin");
                        break;
                  case Xml::TagEnd:
                        if (tag == "topwin")
                              return;
                  default:
                        break;
                  }
            }
      }

//---------------------------------------------------------
//   writeConfiguration
//---------------------------------------------------------

void TopWin::writeConfiguration(ToplevelType t, int level, Xml& xml)
      {
      xml.tag(level++, "topwin");
      xml.intTag(level, "width", _widthInit[t]);
      xml.intTag(level, "height", _heightInit[t]);
      xml.strTag(level, "nonshared_toolbars", _toolbarNonsharedInit[t].toHex().data());
      xml.strTag(level, "shared_toolbars", _toolbarSharedInit[t].toHex().data());
      xml.etag(level, "topwin");
      }

void TopWin::initTopwinState()
{
  printf("FINDMICH: in initTopwinState()\n");
  
  if (sharesToolsAndMenu())
  {
    if (this == muse->getCurrentMenuSharingTopwin())
    {
      printf("FINDMICH RESTORING for %s...\n",windowTitle().toAscii().data());
      muse->restoreState(_toolbarSharedInit[_type]);
    }
  }
  else
    restoreState(_toolbarNonsharedInit[_type]);
}

void TopWin::restoreMainwinState()
{
  if (sharesToolsAndMenu())
    initTopwinState();
}