summaryrefslogtreecommitdiffstats
path: root/src/gui/dialogs/EventEditDialog.cpp
blob: d1ca56efb0f5804f83ef7e49342c73139befc222 (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
    Rosegarden
    A MIDI and audio sequencer and musical notation editor.
 
    This program is Copyright 2000-2008
        Guillaume Laurent   <glaurent@telegraph-road.org>,
        Chris Cannam        <cannam@all-day-breakfast.com>,
        Richard Bown        <richard.bown@ferventsoftware.com>
 
    The moral rights of Guillaume Laurent, Chris Cannam, and Richard
    Bown to claim authorship of this work have been asserted.
 
    Other copyrights also apply to some parts of this work.  Please
    see the AUTHORS file and individual file headers for details.
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/


#include "EventEditDialog.h"

#include <tdelocale.h>
#include "misc/Strings.h"
#include "base/Event.h"
#include "base/MidiTypes.h"
#include "base/NotationTypes.h"
#include "base/PropertyName.h"
#include "base/RealTime.h"
#include "gui/editors/notation/NotePixmapFactory.h"
#include <kdialogbase.h>
#include <tdemessagebox.h>
#include <tqcheckbox.h>
#include <tqfont.h>
#include <tqgrid.h>
#include <tqgroupbox.h>
#include <tqhbox.h>
#include <tqlabel.h>
#include <tqlineedit.h>
#include <tqobject.h>
#include <tqobjectlist.h>
#include <tqpushbutton.h>
#include <tqscrollview.h>
#include <tqsize.h>
#include <tqspinbox.h>
#include <tqstring.h>
#include <tqtooltip.h>
#include <tqvbox.h>
#include <tqwidget.h>


namespace Rosegarden
{

EventEditDialog::EventEditDialog(TQWidget *parent,
                                 const Event &event,
                                 bool editable) :
        KDialogBase(parent, 0, true, i18n(editable ? "Advanced Event Edit" : "Advanced Event Viewer"),
                    (editable ? (Ok | Cancel) : Ok)),
        m_durationDisplay(0),
        m_durationDisplayAux(0),
        m_persistentGrid(0),
        m_nonPersistentGrid(0),
        m_nonPersistentView(0),
        m_originalEvent(event),
        m_event(event),
        m_type(event.getType()),
        m_absoluteTime(event.getAbsoluteTime()),
        m_duration(event.getDuration()),
        m_subOrdering(event.getSubOrdering()),
        m_modified(false)
{
    TQVBox *vbox = makeVBoxMainWidget();

    TQGroupBox *intrinsicBox = new TQGroupBox
                              (1, TQt::Horizontal, i18n("Intrinsics"), vbox);

    TQGrid *intrinsicGrid = new TQGrid(4, TQt::Horizontal, intrinsicBox);

    new TQLabel(i18n("Event type: "), intrinsicGrid);
    new TQLabel("", intrinsicGrid);
    new TQLabel("", intrinsicGrid);
    TQLineEdit *lineEdit = new TQLineEdit(intrinsicGrid);
    lineEdit->setText(strtoqstr(event.getType()));

    new TQLabel(i18n("Absolute time: "), intrinsicGrid);
    new TQLabel("", intrinsicGrid);
    new TQLabel("", intrinsicGrid);
    TQSpinBox *absoluteTime = new TQSpinBox
                             (INT_MIN, INT_MAX, Note(Note::Shortest).getDuration(), intrinsicGrid);
    absoluteTime->setValue(event.getAbsoluteTime());
    TQObject::connect(absoluteTime, TQ_SIGNAL(valueChanged(int)),
                     this, TQ_SLOT(slotAbsoluteTimeChanged(int)));
    slotAbsoluteTimeChanged(event.getAbsoluteTime());

    new TQLabel(i18n("Duration: "), intrinsicGrid);
    m_durationDisplay = new TQLabel("(note)", intrinsicGrid);
    m_durationDisplay->setMinimumWidth(20);
    m_durationDisplayAux = new TQLabel("(note)", intrinsicGrid);
    m_durationDisplayAux->setMinimumWidth(20);

    TQSpinBox *duration = new TQSpinBox
                         (0, INT_MAX, Note(Note::Shortest).getDuration(), intrinsicGrid);
    duration->setValue(event.getDuration());
    TQObject::connect(duration, TQ_SIGNAL(valueChanged(int)),
                     this, TQ_SLOT(slotDurationChanged(int)));
    slotDurationChanged(event.getDuration());

    new TQLabel(i18n("Sub-ordering: "), intrinsicGrid);
    new TQLabel("", intrinsicGrid);
    new TQLabel("", intrinsicGrid);

    TQSpinBox *subOrdering = new TQSpinBox( -100, 100, 1, intrinsicGrid);
    subOrdering->setValue(event.getSubOrdering());
    TQObject::connect(subOrdering, TQ_SIGNAL(valueChanged(int)),
                     this, TQ_SLOT(slotSubOrderingChanged(int)));
    slotSubOrderingChanged(event.getSubOrdering());

    TQGroupBox *persistentBox = new TQGroupBox
                               (1, TQt::Horizontal, i18n("Persistent properties"), vbox);
    m_persistentGrid = new TQGrid(4, TQt::Horizontal, persistentBox);

    TQLabel *label = new TQLabel(i18n("Name"), m_persistentGrid);
    TQFont font(label->font());
    font.setItalic(true);
    label->setFont(font);

    label = new TQLabel(i18n("Type"), m_persistentGrid);
    label->setFont(font);
    label = new TQLabel(i18n("Value"), m_persistentGrid);
    label->setFont(font);
    label = new TQLabel("", m_persistentGrid);
    label->setFont(font);

    Event::PropertyNames p = event.getPersistentPropertyNames();

    for (Event::PropertyNames::iterator i = p.begin();
            i != p.end(); ++i) {
        addPersistentProperty(*i);
    }

    p = event.getNonPersistentPropertyNames();

    if (p.begin() == p.end()) {
        m_nonPersistentView = 0;
        m_nonPersistentGrid = 0;
    } else {

        TQGroupBox *nonPersistentBox = new TQGroupBox
                                      (1, TQt::Horizontal, i18n("Non-persistent properties"), vbox);
        new TQLabel(i18n("These are cached values, lost if the event is modified."),
                   nonPersistentBox);

        m_nonPersistentView = new TQScrollView(nonPersistentBox);
        //m_nonPersistentView->setHScrollBarMode(TQScrollView::AlwaysOff);
        m_nonPersistentView->setResizePolicy(TQScrollView::AutoOneFit);

        m_nonPersistentGrid = new TQGrid
                              (4, TQt::Horizontal, m_nonPersistentView->viewport());
        m_nonPersistentView->addChild(m_nonPersistentGrid);

        m_nonPersistentGrid->setSpacing(4);
        m_nonPersistentGrid->setMargin(5);

        label = new TQLabel(i18n("Name       "), m_nonPersistentGrid);
        label->setFont(font);
        label = new TQLabel(i18n("Type       "), m_nonPersistentGrid);
        label->setFont(font);
        label = new TQLabel(i18n("Value      "), m_nonPersistentGrid);
        label->setFont(font);
        label = new TQLabel("", m_nonPersistentGrid);
        label->setFont(font);

        for (Event::PropertyNames::iterator i = p.begin();
                i != p.end(); ++i) {

            new TQLabel(strtoqstr(*i), m_nonPersistentGrid, strtoqstr(*i).ascii());
            new TQLabel(strtoqstr(event.getPropertyTypeAsString(*i)), m_nonPersistentGrid, strtoqstr(*i).ascii());
            new TQLabel(strtoqstr(event.getAsString(*i)), m_nonPersistentGrid, strtoqstr(*i).ascii());
            TQPushButton *button = new TQPushButton("P", m_nonPersistentGrid, strtoqstr(*i).ascii());
            button->setFixedSize(TQSize(24, 24));
            TQToolTip::add
                (button, i18n("Make persistent"));
            TQObject::connect(button, TQ_SIGNAL(clicked()),
                             this, TQ_SLOT(slotPropertyMadePersistent()));
        }
    }
}

void
EventEditDialog::addPersistentProperty(const PropertyName &name)
{
    TQLabel *label = new TQLabel(strtoqstr(name), m_persistentGrid, strtoqstr(name).ascii());
    label->show();
    label = new TQLabel(strtoqstr(m_originalEvent.getPropertyTypeAsString(name)),
                       m_persistentGrid, strtoqstr(name).ascii());
    label->show();

    PropertyType type(m_originalEvent.getPropertyType(name));
    switch (type) {

    case Int: {
                int min = INT_MIN, max = INT_MAX;
                // DMM - constrain program changes to a useful range of values
                // Might other types have a similar need for such limits?
                if (m_originalEvent.isa(ProgramChange::EventType)) {
                min = 0;
                max = 127;
            }
        TQSpinBox *spinBox = new TQSpinBox
                            (min, max, 1, m_persistentGrid, strtoqstr(name).ascii());
        spinBox->setValue(m_originalEvent.get<Int>(name));
        TQObject::connect(spinBox, TQ_SIGNAL(valueChanged(int)),
                         this, TQ_SLOT(slotIntPropertyChanged(int)));
        spinBox->show();
        break;
    }
case UInt: {
        int min = 0;
        int max = UINT_MAX;
        if (m_originalEvent.isa(ProgramChange::EventType)) {
                min = 0;
                max = 65535;
            }
            TQSpinBox *spinBox = new TQSpinBox
                                (min, max, 1, m_persistentGrid, strtoqstr(name).ascii());
            spinBox->setValue(m_originalEvent.get<UInt>(name));
            TQObject::connect(spinBox, TQ_SIGNAL(valueChanged(int)),
                             this, TQ_SLOT(slotIntPropertyChanged(int)));
            spinBox->show();
            break;
        }
    case RealTimeT: {
            RealTime realTime = m_originalEvent.get<RealTimeT>(name);

            TQHBox* hbox = new TQHBox(m_persistentGrid);

            // seconds
            //
            TQSpinBox *spinBox = new TQSpinBox
                                (INT_MIN, INT_MAX, 1,
                                 hbox, TQString(strtoqstr(name) + "%sec").ascii());
            spinBox->setValue(realTime.sec);

            TQObject::connect(spinBox, TQ_SIGNAL(valueChanged(int)),
                             this, TQ_SLOT(slotRealTimePropertyChanged(int)));

            // nseconds
            //
            spinBox = new TQSpinBox
                      (INT_MIN, INT_MAX, 1,
                       hbox, TQString(strtoqstr(name) + "%nsec").ascii());
            spinBox->setValue(realTime.nsec);

            TQObject::connect(spinBox, TQ_SIGNAL(valueChanged(int)),
                             this, TQ_SLOT(slotRealTimePropertyChanged(int)));
            spinBox->show();
            break;
        }

    case Bool: {
            TQCheckBox *checkBox = new TQCheckBox
                                  ("", m_persistentGrid, strtoqstr(name).ascii());
            checkBox->setChecked(m_originalEvent.get<Bool>(name));
            TQObject::connect(checkBox, TQ_SIGNAL(activated()),
                             this, TQ_SLOT(slotBoolPropertyChanged()));
            checkBox->show();
            break;
        }

    case String: {
            TQLineEdit *lineEdit = new TQLineEdit
                                  (strtoqstr(m_originalEvent.get<String>(name)),
                                   m_persistentGrid,
                                   strtoqstr(name).ascii());
            TQObject::connect(lineEdit, TQ_SIGNAL(textChanged(const TQString &)),
                             this, TQ_SLOT(slotStringPropertyChanged(const TQString &)));
            lineEdit->show();
            break;
        }
    }

    TQPushButton *button = new TQPushButton("X", m_persistentGrid,
                                          strtoqstr(name).ascii());
    button->setFixedSize(TQSize(24, 24));
    TQToolTip::add
        (button, i18n("Delete this property"));
    TQObject::connect(button, TQ_SIGNAL(clicked()),
                     this, TQ_SLOT(slotPropertyDeleted()));
    button->show();
}

Event
EventEditDialog::getEvent() const
{
    return Event(m_event, m_absoluteTime, m_duration, m_subOrdering);
}

void
EventEditDialog::slotEventTypeChanged(const TQString &type)
{
    std::string t(qstrtostr(type));
    if (t != m_type) {
        m_modified = true;
        m_type = t;
    }
}

void
EventEditDialog::slotAbsoluteTimeChanged(int value)
{
    if (value == m_absoluteTime)
        return ;
    m_modified = true;
    m_absoluteTime = value;
}

void
EventEditDialog::slotDurationChanged(int value)
{
    timeT error = 0;
    m_durationDisplay->setPixmap
    (NotePixmapFactory::toTQPixmap(m_notePixmapFactory.makeNoteMenuPixmap(timeT(value), error)));

    if (error >= value / 2) {
        m_durationDisplayAux->setText("++ ");
    } else if (error > 0) {
        m_durationDisplayAux->setText("+ ");
    } else if (error < 0) {
        m_durationDisplayAux->setText("- ");
    } else {
        m_durationDisplayAux->setText(" ");
    }

    if (timeT(value) == m_duration)
        return ;

    m_modified = true;
    m_duration = value;
}

void
EventEditDialog::slotSubOrderingChanged(int value)
{
    if (value == m_subOrdering)
        return ;
    m_modified = true;
    m_subOrdering = value;
}

void
EventEditDialog::slotIntPropertyChanged(int value)
{
    const TQObject *s = sender();
    const TQSpinBox *spinBox = dynamic_cast<const TQSpinBox *>(s);
    if (!spinBox)
        return ;

    m_modified = true;
    TQString propertyName = spinBox->name();
    m_event.set<Int>(qstrtostr(propertyName), value);
}

void
EventEditDialog::slotRealTimePropertyChanged(int value)
{
    const TQObject *s = sender();
    const TQSpinBox *spinBox = dynamic_cast<const TQSpinBox *>(s);
    if (!spinBox)
        return ;

    m_modified = true;
    TQString propertyFullName = spinBox->name();

    TQString propertyName = propertyFullName.section('%', 0, 0),
                           nsecOrSec = propertyFullName.section('%', 1, 1);

    RealTime realTime = m_event.get<RealTimeT>(qstrtostr(propertyName));

    if (nsecOrSec == "sec")
        realTime.sec = value;
    else
        realTime.nsec = value;

    m_event.set<Int>(qstrtostr(propertyName), value);
}

void
EventEditDialog::slotBoolPropertyChanged()
{
    const TQObject *s = sender();
    const TQCheckBox *checkBox = dynamic_cast<const TQCheckBox *>(s);
    if (!checkBox)
        return ;

    m_modified = true;
    TQString propertyName = checkBox->name();
    bool checked = checkBox->isChecked();

    m_event.set<Bool>(qstrtostr(propertyName), checked);
}

void
EventEditDialog::slotStringPropertyChanged(const TQString &value)
{
    const TQObject *s = sender();
    const TQLineEdit *lineEdit = dynamic_cast<const TQLineEdit *>(s);
    if (!lineEdit)
        return ;

    m_modified = true;
    TQString propertyName = lineEdit->name();
    m_event.set<String>(qstrtostr(propertyName), qstrtostr(value));
}

void
EventEditDialog::slotPropertyDeleted()
{
    const TQObject *s = sender();
    const TQPushButton *pushButton = dynamic_cast<const TQPushButton *>(s);
    if (!pushButton)
        return ;

    TQString propertyName = pushButton->name();

    if (KMessageBox::warningContinueCancel
            (this,
             i18n("Are you sure you want to delete the \"%1\" property?\n\n"
                  "Removing necessary properties may cause unexpected behavior.").
             arg(propertyName),
             i18n("Edit Event"),
             i18n("&Delete")) != KMessageBox::Continue)
        return ;

    m_modified = true;
    TQObjectList *list = m_persistentGrid->queryList(0, propertyName.ascii(), false);
    TQObjectListIt i(*list);
    TQObject *obj;
    while ((obj = i.current()) != 0) {
        ++i;
        delete obj;
    }
    delete list;

    m_event.unset(qstrtostr(propertyName));
}

void
EventEditDialog::slotPropertyMadePersistent()
{
    const TQObject *s = sender();
    const TQPushButton *pushButton = dynamic_cast<const TQPushButton *>(s);
    if (!pushButton)
        return ;

    TQString propertyName = pushButton->name();

    if (KMessageBox::warningContinueCancel
            (this,
             i18n("Are you sure you want to make the \"%1\" property persistent?\n\n"
                  "This could cause problems if it overrides a different "
                  "computed value later on.").
             arg(propertyName),
             i18n("Edit Event"),
             i18n("Make &Persistent")) != KMessageBox::Continue)
        return ;

    TQObjectList *list = m_nonPersistentGrid->queryList(0, propertyName.ascii(), false);
    TQObjectListIt i(*list);
    TQObject *obj;
    while ((obj = i.current()) != 0) {
        ++i;
        delete obj;
    }
    delete list;

    m_modified = true;
    addPersistentProperty(qstrtostr(propertyName));

    PropertyType type =
        m_originalEvent.getPropertyType(qstrtostr(propertyName));

    switch (type) {

    case Int:
        m_event.set<Int>
        (qstrtostr(propertyName),
         m_originalEvent.get<Int>
         (qstrtostr(propertyName)));
        break;

    case UInt:
        m_event.set<UInt>
        (qstrtostr(propertyName),
         m_originalEvent.get<UInt>
         (qstrtostr(propertyName)));
        break;

    case RealTimeT:
        m_event.set<RealTimeT>
        (qstrtostr(propertyName),
         m_originalEvent.get<RealTimeT>
         (qstrtostr(propertyName)));
        break;

    case Bool:
        m_event.set<Bool>
        (qstrtostr(propertyName),
         m_originalEvent.get<Bool>
         (qstrtostr(propertyName)));
        break;

    case String:
        m_event.set<String>
        (qstrtostr(propertyName),
         m_originalEvent.get<String>
         (qstrtostr(propertyName)));
        break;
    }
}

}
#include "EventEditDialog.moc"