File: PaneStack.cpp

package info (click to toggle)
sonic-visualiser 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,744 kB
  • sloc: cpp: 158,888; ansic: 11,920; sh: 1,785; makefile: 517; xml: 64; perl: 31
file content (885 lines) | stat: -rw-r--r-- 24,828 bytes parent folder | download | duplicates (2)
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    This file copyright 2006 Chris Cannam and QMUL.
    
    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 "PaneStack.h"

#include "Pane.h"
#include "widgets/PropertyStack.h"
#include "widgets/IconLoader.h"
#include "widgets/ClickableLabel.h"
#include "layer/Layer.h"
#include "ViewManager.h"
#include "AlignmentView.h"

#include <QApplication>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPainter>
#include <QPalette>
#include <QLabel>
#include <QPushButton>
#include <QSplitter>
#include <QStackedWidget>
#include <QResizeEvent>

#include <iostream>

//#define DEBUG_PANE_STACK 1

namespace sv {

PaneStack::PaneStack(QWidget *parent,
                     ViewManager *viewManager,
                     int options) :
    QFrame(parent),
    m_currentPane(nullptr),
    m_options(options),
    m_splitter(nullptr),
    m_autoResizeStack(nullptr),
    m_propertyStackStack(new QStackedWidget),
    m_viewManager(viewManager),
    m_propertyStackMinWidth(100),
    m_layoutStyle(PropertyStackPerPaneLayout)
{
    QHBoxLayout *layout = new QHBoxLayout;
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);

    if (m_options & int(Option::NoUserResize)) {

        m_autoResizeStack = new QWidget;
        m_autoResizeLayout = new QVBoxLayout;
        m_autoResizeLayout->setContentsMargins(0, 0, 0, 0);
        m_autoResizeLayout->setSpacing(0);
        m_autoResizeStack->setLayout(m_autoResizeLayout);
        layout->addWidget(m_autoResizeStack);
        layout->setStretchFactor(m_autoResizeStack, 1);

    } else {
    
        m_splitter = new QSplitter;
        m_splitter->setOrientation(Qt::Vertical);
        m_splitter->setOpaqueResize(false);
        layout->addWidget(m_splitter);
        layout->setStretchFactor(m_splitter, 1);
    }

    if (m_options & int(Option::NoPropertyStacks)) {
        m_layoutStyle = HiddenPropertyStacksLayout;
    }
    
    m_propertyStackStack->hide();
    layout->addWidget(m_propertyStackStack);

    setLayout(layout);
}

Pane *
PaneStack::addPane(int index)
{
    QFrame *frame = new QFrame;

    QGridLayout *layout = new QGridLayout;
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setHorizontalSpacing(m_viewManager->scalePixelSize(2));

    AlignmentView *av = nullptr;

    if (m_options & int(Option::ShowAlignmentViews)) {
        layout->setVerticalSpacing(0);
        av = new AlignmentView(frame);
        av->setFixedHeight(ViewManager::scalePixelSize(20));
        av->setViewManager(m_viewManager);
        av->setVisible(false); // for now
        layout->addWidget(av, 0, 1);
    } else {
        layout->setVerticalSpacing(m_viewManager->scalePixelSize(2));
    }

    QPushButton *xButton = new QPushButton(frame);
    xButton->setIcon(IconLoader().load("cross"));
    xButton->setFixedSize(QSize(16, 16));
    xButton->setFlat(true);
    xButton->setVisible(!(m_options & int(Option::NoPaneAccessories)));
    if (m_panes.empty() && (m_options & int(Option::NoCloseOnFirstPane))) {
        xButton->setVisible(false);
    }
    layout->addWidget(xButton, 1, 0);
    connect(xButton, SIGNAL(clicked()), this, SLOT(paneDeleteButtonClicked()));

    ClickableLabel *currentIndicator = new ClickableLabel(frame);
    connect(currentIndicator, SIGNAL(clicked()), this, SLOT(indicatorClicked()));
    layout->addWidget(currentIndicator, 2, 0);
    layout->setRowStretch(2, 20);
    currentIndicator->setMinimumWidth(16);
    currentIndicator->setMinimumHeight(16);
    currentIndicator->setScaledContents(true);
    currentIndicator->setVisible(!(m_options & int(Option::NoPaneAccessories)));

    sv_frame_t initialCentreFrame = -1;
    if (!m_panes.empty()) {
        initialCentreFrame = m_panes[0].pane->getCentreFrame();
    }

    Pane *pane = new Pane(frame);
    if (initialCentreFrame >= 0) {
        pane->setViewManager(m_viewManager, initialCentreFrame);
    } else {
        pane->setViewManager(m_viewManager);
    }
    layout->addWidget(pane, 1, 1, 2, 1);
    layout->setColumnStretch(1, 20);

    QWidget *properties = nullptr;
    if (m_options & int(Option::NoPropertyStacks)) {
        properties = new QFrame();
    } else {
        properties = new PropertyStack(frame, pane);
        connect(properties, SIGNAL(propertyContainerSelected(View *, PropertyContainer *)),
                this, SLOT(propertyContainerSelected(View *, PropertyContainer *)));
        connect(properties, SIGNAL(propertyContainerContextMenuRequested(View *, PropertyContainer *, QPoint)),
                this, SLOT(propertyContainerContextMenuRequested(View *, PropertyContainer *, QPoint)));
        connect(properties, SIGNAL(viewSelected(View  *)),
                this, SLOT(viewSelected(View *)));
        connect(properties, SIGNAL(contextHelpChanged(const QString &)),
                this, SIGNAL(contextHelpChanged(const QString &)));
    }
    if (m_layoutStyle == PropertyStackPerPaneLayout) {
        layout->addWidget(properties, 1, 2, 2, 1);
    } else {
        properties->setParent(m_propertyStackStack);
        m_propertyStackStack->addWidget(properties);
    }
    layout->setColumnStretch(2, 0);

    PaneRec rec;
    rec.pane = pane;
    rec.propertyStack = properties;
    rec.xButton = xButton;
    rec.currentIndicator = currentIndicator;
    rec.frame = frame;
    rec.layout = layout;
    rec.alignmentView = av;
    m_panes.push_back(rec);

    frame->setLayout(layout);

    int indexInWidgets = mapInsertionIndexToWidgets(index);
    
    if (m_options & int(Option::NoUserResize)) {
        if (index >= 0) {
            m_autoResizeLayout->insertWidget(indexInWidgets, frame);
        } else {
            m_autoResizeLayout->addWidget(frame);
        }
        frame->adjustSize();
    } else {
        if (index >= 0) {
            m_splitter->insertWidget(indexInWidgets, frame);
        } else {
            m_splitter->addWidget(frame);
        }
    }

    connect(pane, SIGNAL(propertyContainerAdded(PropertyContainer *)),
            this, SLOT(propertyContainerAdded(PropertyContainer *)));
    connect(pane, SIGNAL(propertyContainerRemoved(PropertyContainer *)),
            this, SLOT(propertyContainerRemoved(PropertyContainer *)));
    connect(pane, SIGNAL(paneInteractedWith()),
            this, SLOT(paneInteractedWith()));
    connect(pane, SIGNAL(rightButtonMenuRequested(QPoint)),
            this, SLOT(rightButtonMenuRequested(QPoint)));
    connect(pane, SIGNAL(dropAccepted(QStringList)),
            this, SLOT(paneDropAccepted(QStringList)));
    connect(pane, SIGNAL(dropAccepted(QString)),
            this, SLOT(paneDropAccepted(QString)));
    connect(pane, SIGNAL(doubleClickSelectInvoked(sv_frame_t)),
            this, SIGNAL(doubleClickSelectInvoked(sv_frame_t)));

    emit paneAdded(pane);
    emit paneAdded();

    if (!m_currentPane) {
        setCurrentPane(pane);
    }

    showOrHidePaneAccessories();
    relinkAlignmentViews();

    return pane;
}

int
PaneStack::mapInsertionIndexToWidgets(int index) const
{
    // The index used to refer to pane position within the stack is
    // among visible panes only. But hidden panes are still there in
    // the stack or layout, so when we want to use its insertWidget
    // method we need to adjust the index to skip hidden widgets

    if (index <= 0) {
        return index;
    }

    int counted = 0;

    if (m_options & int(Option::NoUserResize)) {

        // We're using m_autoResizeLayout, a QVBoxLayout
        
        for (int i = 0; i < m_autoResizeLayout->count(); ++i) {
            if (counted == index) return i;
            auto w = dynamic_cast<QWidgetItem *>(m_autoResizeLayout->itemAt(i));
            if (w && dynamic_cast<QFrame *>(w->widget()) &&
                w->widget()->isVisible()) {
                ++counted;
            }
        }
        return m_autoResizeLayout->count();

    } else {

        // We're using m_splitter, a QSplitter
            
        for (int i = 0; i < m_splitter->count(); ++i) {
            if (counted == index) {
                return i;
            }
            if (dynamic_cast<QFrame *>(m_splitter->widget(i)) &&
                m_splitter->widget(i)->isVisible()) {
                ++counted;
            }
        }
        return m_splitter->count();
    }
}

void
PaneStack::relinkAlignmentViews()
{
    if (m_panes.empty()) return;
    auto av = m_panes[0].alignmentView;
    if (av) av->hide();
    for (int i = 1; in_range_for(m_panes, i); ++i) {
        av = m_panes[i].alignmentView;
        if (!av) continue;
        if (!(m_options & int(Option::ShowAlignmentViews))) {
            av->hide();
        } else {
            av->setAboveView(m_panes[i-1].pane);
            av->setBelowView(m_panes[i].pane);
            av->setReferenceView(m_panes[0].pane);
            av->show();
        }
    }

    adjustAlignmentViewHeights(size().height());
}

void
PaneStack::unlinkAlignmentViews()
{
    for (int i = 0; in_range_for(m_panes, i); ++i) {
        auto av = m_panes[i].alignmentView;
        if (!av) continue;
        av->setAboveView(nullptr);
        av->setBelowView(nullptr);
        av->setReferenceView(nullptr);
    }
}

void
PaneStack::resizeEvent(QResizeEvent *ev)
{
    adjustAlignmentViewHeights(ev->size().height());
}

void
PaneStack::adjustAlignmentViewHeights(int forMyHeight)
{
    if (!(m_options & int(Option::ShowAlignmentViews))) return;
    if (!(m_options & int(Option::NoUserResize))) return;
    if (!isVisible()) return;
    if (m_panes.empty()) return;

    int heightPerPane = forMyHeight / int(m_panes.size());

    SVCERR << "heightPerPane = " << heightPerPane << " ("
           << forMyHeight << "/" << m_panes.size() << ")" << endl;
    
    int roomForAlignmentView = heightPerPane / 4;
    int min = ViewManager::scalePixelSize(6);
    int max = ViewManager::scalePixelSize(25);
    int alignmentHeight = roomForAlignmentView;
    if (alignmentHeight < min) {
        alignmentHeight = min;
    }
    if (alignmentHeight > max) {
        alignmentHeight = max;
    }

    SVCERR << "alignmentHeight = " << alignmentHeight << endl;
    
    for (int i = 0; in_range_for(m_panes, i); ++i) {
        auto av = m_panes[i].alignmentView;
        if (!av) continue;
        av->setFixedHeight(alignmentHeight);
    }
}

void
PaneStack::setPropertyStackMinWidth(int mw)
{
    for (std::vector<PaneRec>::iterator i = m_panes.begin();
         i != m_panes.end(); ++i) {
        i->propertyStack->setMinimumWidth(mw);
    }
    m_propertyStackMinWidth = mw;
}

void
PaneStack::setLayoutStyle(LayoutStyle style)
{
    if (m_options & int(Option::NoPropertyStacks)) {
        SVCERR << "NOTE: PaneStack::setLayoutStyle called on PaneStack with NoPropertyStacks option set - this does nothing, its style is always equivalent to HiddenPropertyStacksLayout" << endl;
        return;
    }
    
    if (style == m_layoutStyle) return;
    m_layoutStyle = style;

    std::vector<PaneRec>::iterator i;

    switch (style) {

    case HiddenPropertyStacksLayout:
    case SinglePropertyStackLayout:
        
        for (i = m_panes.begin(); i != m_panes.end(); ++i) {
            i->layout->removeWidget(i->propertyStack);
            i->propertyStack->setParent(m_propertyStackStack);
            m_propertyStackStack->addWidget(i->propertyStack);
        }
        m_propertyStackStack->setVisible(style != HiddenPropertyStacksLayout);
        break;

    case PropertyStackPerPaneLayout:

        for (i = m_panes.begin(); i != m_panes.end(); ++i) {
            m_propertyStackStack->removeWidget(i->propertyStack);
            i->propertyStack->setParent(i->frame);
            i->layout->addWidget(i->propertyStack, 1, 2, 2, 1);
            i->propertyStack->show();
        }
        m_propertyStackStack->hide();
        break;
    }
}

Pane *
PaneStack::getPane(int n)
{
    if (n < (int)m_panes.size()) {
        return m_panes[n].pane;
    } else {
        return nullptr;
    }
}

int
PaneStack::getPaneIndex(Pane *pane)
{
    for (int i = 0; i < getPaneCount(); ++i) {
        if (pane == getPane(i)) {
            return i;
        }
    }
    return -1;
}

Pane *
PaneStack::getHiddenPane(int n)
{
    return m_hiddenPanes[n].pane;
}

void
PaneStack::deletePane(Pane *pane)
{
#ifdef DEBUG_PANE_STACK
    SVCERR << "PaneStack::deletePane(" << pane << ")" << endl;
#endif

    std::vector<PaneRec>::iterator i;
    bool found = false;

    QWidget *stack = nullptr;

    for (i = m_panes.begin(); i != m_panes.end(); ++i) {
        if (i->pane == pane) {
            stack = i->propertyStack;
            m_panes.erase(i);
            found = true;
            break;
        }
    }

    if (!found) {

        for (i = m_hiddenPanes.begin(); i != m_hiddenPanes.end(); ++i) {
            if (i->pane == pane) {
                stack = i->propertyStack;
                m_hiddenPanes.erase(i);
                found = true;
                break;
            }
        }

        if (!found) {
            SVCERR << "WARNING: PaneStack::deletePane(" << pane << "): Pane not found in visible or hidden panes, not deleting" << endl;
            return;
        }
    }

    emit paneAboutToBeDeleted(pane);
    unlinkAlignmentViews();

#ifdef DEBUG_PANE_STACK
    SVCERR << "PaneStack::deletePane: about to delete parent " << pane->parent() << " of pane " << pane << endl;
#endif

    // The property stack associated with the parent was initially
    // created with the same parent as it, so it would be deleted when
    // we delete the pane's parent in a moment -- but it may have been
    // reparented depending on the layout. We'd better delete it
    // separately first. (This fixes a crash on opening a new layer
    // with a new unit type in it, when a long-defunct property box
    // could be signalled from the unit database to tell it that a new
    // unit had appeared.)
    delete stack;

    delete pane->parent();

    if (m_currentPane == pane) {
        if (m_panes.size() > 0) {
            setCurrentPane(m_panes[0].pane);
        } else {
            setCurrentPane(nullptr);
        }
    }

    showOrHidePaneAccessories();
    relinkAlignmentViews();

    emit paneDeleted();
}

void
PaneStack::showOrHidePaneAccessories()
{
#ifdef DEBUG_PANE_STACK
    SVCERR << "PaneStack::showOrHidePaneAccessories: count == " << getPaneCount() << endl;
#endif

    bool multi = (getPaneCount() > 1);
    for (std::vector<PaneRec>::iterator i = m_panes.begin();
         i != m_panes.end(); ++i) {
        bool visible = (multi && !(m_options & int(Option::NoPaneAccessories)));
        bool xvisible = visible;
        if (i == m_panes.begin()) {
            if (m_options & int(Option::NoCloseOnFirstPane)) {
                xvisible = false;
            }
        }
        i->xButton->setVisible(xvisible);
        i->currentIndicator->setVisible(visible);
    }
}

int
PaneStack::getPaneCount() const
{
    return int(m_panes.size());
}

int
PaneStack::getHiddenPaneCount() const
{
    return int(m_hiddenPanes.size());
}

void
PaneStack::hidePane(Pane *pane)
{
    std::vector<PaneRec>::iterator i = m_panes.begin();

    while (i != m_panes.end()) {
        if (i->pane == pane) {

            m_hiddenPanes.push_back(*i);
            m_panes.erase(i);

            QWidget *pw = dynamic_cast<QWidget *>(pane->parent());
            if (pw) pw->hide();

            if (m_currentPane == pane) {
                if (m_panes.size() > 0) {
                    setCurrentPane(m_panes[0].pane);
                } else {
                    setCurrentPane(nullptr);
                }
            }
            
            showOrHidePaneAccessories();
            emit paneHidden(pane);
            emit paneHidden();
            relinkAlignmentViews();
            return;
        }
        ++i;
    }

    SVCERR << "WARNING: PaneStack::hidePane(" << pane << "): Pane not found in visible panes" << endl;
}

void
PaneStack::showPane(Pane *pane)
{
    std::vector<PaneRec>::iterator i = m_hiddenPanes.begin();

    while (i != m_hiddenPanes.end()) {
        if (i->pane == pane) {
            m_panes.push_back(*i);
            m_hiddenPanes.erase(i);
            QWidget *pw = dynamic_cast<QWidget *>(pane->parent());
            if (pw) pw->show();

            //!!! update current pane

            showOrHidePaneAccessories();
            relinkAlignmentViews();

            return;
        }
        ++i;
    }

    SVCERR << "WARNING: PaneStack::showPane(" << pane << "): Pane not found in hidden panes" << endl;
}

void
PaneStack::setCurrentPane(Pane *pane) // may be null
{
    if (m_currentPane == pane) return;
    
    std::vector<PaneRec>::iterator i = m_panes.begin();

    // We used to do this by setting the foreground and background
    // role, but it seems the background role is ignored and the
    // background drawn transparent in Qt 4.1 -- I can't quite see why
    
    QPixmap selectedMap(1, 1);
    selectedMap.fill(QApplication::palette().color(QPalette::WindowText));
    
    QPixmap unselectedMap(1, 1);
    unselectedMap.fill(QApplication::palette().color(QPalette::Window));

    bool found = false;

    while (i != m_panes.end()) {
        if (i->pane == pane) {
            i->currentIndicator->setPixmap(selectedMap);
            if (m_layoutStyle != PropertyStackPerPaneLayout) {
                m_propertyStackStack->setCurrentWidget(i->propertyStack);
            }
            found = true;
        } else {
            i->currentIndicator->setPixmap(unselectedMap);
        }
        ++i;
    }

    if (found || pane == nullptr) {
        m_currentPane = pane;
        emit currentPaneChanged(m_currentPane);
    } else {
        SVCERR << "WARNING: PaneStack::setCurrentPane(" << pane << "): pane is not a visible pane in this stack" << endl;
    }
}

void
PaneStack::setCurrentLayer(Pane *pane, Layer *layer) // may be null
{
    setCurrentPane(pane);

    if (m_currentPane) {

        std::vector<PaneRec>::iterator i = m_panes.begin();

        while (i != m_panes.end()) {

            if (i->pane == pane) {
                PropertyStack *stack = dynamic_cast<PropertyStack *>
                    (i->propertyStack);
                if (stack) {
                    if (stack->containsContainer(layer)) {
                        stack->setCurrentIndex(stack->getContainerIndex(layer));
                        emit currentLayerChanged(pane, layer);
                    } else {
                        stack->setCurrentIndex
                            (stack->getContainerIndex
                             (pane->getPropertyContainer(0)));
                        emit currentLayerChanged(pane, nullptr);
                    }
                }
                break;
            }
            ++i;
        }
    }
}

Pane *
PaneStack::getCurrentPane() 
{
    return m_currentPane;
}

void
PaneStack::propertyContainerAdded(PropertyContainer *)
{
    sizePropertyStacks();
}

void
PaneStack::propertyContainerRemoved(PropertyContainer *)
{
    sizePropertyStacks();
}

void
PaneStack::propertyContainerSelected(View *client, PropertyContainer *pc)
{
    std::vector<PaneRec>::iterator i = m_panes.begin();

    while (i != m_panes.end()) {
        PropertyStack *stack = dynamic_cast<PropertyStack *>(i->propertyStack);
        if (stack &&
            stack->getClient() == client &&
            stack->containsContainer(pc)) {
            setCurrentPane(i->pane);
            break;
        }
        ++i;
    }

    Layer *layer = dynamic_cast<Layer *>(pc);
    if (layer) emit currentLayerChanged(m_currentPane, layer);
    else emit currentLayerChanged(m_currentPane, nullptr);
}

void
PaneStack::propertyContainerContextMenuRequested(View *client,
                                                 PropertyContainer *pc,
                                                 QPoint pos)
{
    Pane *pane = dynamic_cast<Pane *>(client);
    Layer *layer = dynamic_cast<Layer *>(pc);

    if (pane) {
        if (layer) {
            emit layerPropertiesRightButtonMenuRequested(pane, layer, pos);
        } else {
            emit panePropertiesRightButtonMenuRequested(pane, pos);
        }
    }
}

void
PaneStack::viewSelected(View *v)
{
    Pane *p = dynamic_cast<Pane *>(v);
    if (p) setCurrentPane(p);
}

void
PaneStack::paneInteractedWith()
{
    Pane *pane = dynamic_cast<Pane *>(sender());
    if (!pane) return;
    setCurrentPane(pane);
}

void
PaneStack::rightButtonMenuRequested(QPoint position)
{
    Pane *pane = dynamic_cast<Pane *>(sender());
    if (!pane) return;
    emit paneRightButtonMenuRequested(pane, position);
}

void
PaneStack::sizePropertyStacks()
{
    int maxMinWidth = 0;

    if (m_propertyStackMinWidth > 0) maxMinWidth = m_propertyStackMinWidth;

    for (int i = 0; i < (int)m_panes.size(); ++i) {
        if (!m_panes[i].propertyStack) continue;
#ifdef DEBUG_PANE_STACK
        SVDEBUG << "PaneStack::sizePropertyStacks: " << i << ": min " 
                  << m_panes[i].propertyStack->minimumSizeHint().width() << ", hint "
                  << m_panes[i].propertyStack->sizeHint().width() << ", current "
                  << m_panes[i].propertyStack->width() << endl;
#endif

        if (m_panes[i].propertyStack->sizeHint().width() > maxMinWidth) {
            maxMinWidth = m_panes[i].propertyStack->sizeHint().width();
        }
    }

#ifdef DEBUG_PANE_STACK
    SVDEBUG << "PaneStack::sizePropertyStacks: max min width " << maxMinWidth << endl;
#endif

    int setWidth = maxMinWidth;

    m_propertyStackStack->setMaximumWidth(setWidth + 10);

    for (int i = 0; i < (int)m_panes.size(); ++i) {
        if (!m_panes[i].propertyStack) continue;
        m_panes[i].propertyStack->setMinimumWidth(setWidth);
    }

    emit propertyStacksResized(setWidth);
    emit propertyStacksResized();
}
    
void
PaneStack::paneDropAccepted(QStringList uriList)
{
    Pane *pane = dynamic_cast<Pane *>(sender());
    emit dropAccepted(pane, uriList);
}
    
void
PaneStack::paneDropAccepted(QString text)
{
    Pane *pane = dynamic_cast<Pane *>(sender());
    emit dropAccepted(pane, text);
}

void
PaneStack::paneDeleteButtonClicked()
{
    QObject *s = sender();
    for (int i = 0; i < (int)m_panes.size(); ++i) {
        if (m_panes[i].xButton == s) {
            emit paneDeleteButtonClicked(m_panes[i].pane);
        }
    }
}

void
PaneStack::indicatorClicked()
{
    QObject *s = sender();

    for (int i = 0; i < (int)m_panes.size(); ++i) {
        if (m_panes[i].currentIndicator == s) {
            setCurrentPane(m_panes[i].pane);
            return;
        }
    }
}

void
PaneStack::sizePanesEqually()
{
    if (m_options & int(Option::NoUserResize)) {
        return;
    }

    QList<int> sizes = m_splitter->sizes();
    if (sizes.empty()) return;

    int count = sizes.size();

    int variable = 0, total = 0;
    int varicount = 0;

    for (int i = 0; i < count; ++i) {
        total += sizes[i];
    }

    variable = total;

    int j = 0; // index into m_panes, which contains only visible panes
    
    for (int i = 0; i < count; ++i) {
        if (m_splitter->widget(i)->isVisible()) {
            int minh = m_panes[j].pane->minimumSize().height();
            if (minh == m_panes[j].pane->maximumSize().height()) {
                variable -= minh;
            } else {
                varicount++;
            }
            ++j;
        }
    }

    if (total == 0) return;

    sizes.clear();

    int each = (varicount > 0 ? (variable / varicount) : 0);
    int remaining = total;

    j = 0;
    
    for (int i = 0; i < count; ++i) {
        if (i == count - 1) {
            sizes.push_back(remaining);
        } else {
            if (m_splitter->widget(i)->isVisible()) {
                int minh = m_panes[j].pane->minimumSize().height();
                if (minh == m_panes[j].pane->maximumSize().height()) {
                    sizes.push_back(minh);
                    remaining -= minh;
                } else {
                    sizes.push_back(each);
                    remaining -= each;
                }
                ++j;
            } else {
                sizes.push_back(0);
            }
        }
    }

/*
    SVCERR << "sizes: ";
    for (int i = 0; i < sizes.size(); ++i) {
        SVCERR << sizes[i] << " ";
    }
    SVCERR << endl;
*/

    m_splitter->setSizes(sizes);
}

} // end namespace sv