File indexing completed on 2024-04-28 05:41:35

0001 /*
0002     This file is part of KCachegrind.
0003 
0004     SPDX-FileCopyrightText: 2003-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only
0007 */
0008 
0009 /*
0010  * MultiView, enclosing multiple TabView's with a user choosable
0011  * active view (i.e. focus), separated by a splitter.
0012  * Selection of the active view is shown in the next to the right view
0013  * (with wrap around).
0014  */
0015 
0016 #include "multiview.h"
0017 
0018 #include <QDebug>
0019 
0020 #include "config.h"
0021 #include "tabview.h"
0022 
0023 
0024 //
0025 // MultiView
0026 //
0027 
0028 MultiView::MultiView(TopLevelBase* top, QWidget* parent)
0029     : QSplitter(parent), TraceItemView(nullptr, top)
0030 {
0031     // default
0032     setOrientation(Qt::Horizontal);
0033 
0034     appendView();
0035     _active = _views.first();
0036     _active->setActive(true);
0037 }
0038 
0039 void MultiView::setData(TraceData* d)
0040 {
0041     TraceItemView::setData(d);
0042 
0043     foreach(TabView* tv, _views)
0044         tv->setData(d);
0045 }
0046 
0047 void MultiView::setChildCount(int n)
0048 {
0049     while(n< _views.count()) removeView();
0050     while(n> _views.count()) appendView();
0051 }
0052 
0053 void MultiView::appendView()
0054 {
0055     int n = _views.count()+1;
0056 
0057     TabView* tv = new TabView(this, this);
0058     tv->setObjectName(QStringLiteral("TabView-%1").arg(n));
0059     connect(tv, &TabView::tabActivated,
0060             this, &MultiView::tabActivated );
0061     _views.append(tv);
0062     tv->show();
0063 
0064     // no need to waste time with update merging
0065     tv->setMergeUpdates(false);
0066 
0067     // set same attributes as in active view
0068     tv->set(0, _data, _eventType, _eventType2,
0069             _groupType, _partList, _activeItem, nullptr);
0070 
0071     if (0) qDebug() << "MultiView::appendView, now "
0072                     << _views.count();
0073 }
0074 
0075 void MultiView::removeView()
0076 {
0077     if (_views.count()<=1) return;
0078 
0079     TabView* last = _views.last();
0080 
0081     // if last tab is active, make first active
0082     if (last == _active) {
0083         TabView* newActive = _views.first();
0084         newActive->setActive(true);
0085         tabActivated(newActive);
0086     }
0087 
0088     _views.removeAll(last);
0089     delete last;
0090 
0091     if (0) qDebug() << "MultiView::removeView, now "
0092                     << _views.count();
0093 }
0094 
0095 
0096 void MultiView::tabActivated(TabView* newActiveTab)
0097 {
0098     if (_active == newActiveTab) return;
0099 
0100     if (0) qDebug() << "MultiView::tabActivated "
0101                     << newActiveTab->objectName();
0102 
0103     CostItem* oldActiveItem = nullptr;
0104     if (_active) {
0105         oldActiveItem = _active->activeItem();
0106         _active->setActive(false);
0107     }
0108     _active = newActiveTab;
0109 
0110     // make the active item of the new TabView active
0111     if (_active && (oldActiveItem != _active->activeItem()))
0112         TraceItemView::activated(_active->activeItem());
0113 }
0114 
0115 void MultiView::selected(TraceItemView* sender, CostItem* i)
0116 {
0117     if (0) qDebug() << "MultiView::selected " << i->name()
0118                     << ", sender " << sender->widget()->objectName();
0119 
0120     // we react only on selection changes of the active TabView
0121     if (sender != (TraceItemView*)_active) return;
0122 
0123     int idx = _views.indexOf(_active);
0124     idx++;
0125     if (idx == _views.count()) idx = 0;
0126     TabView* next = _views.at(idx);
0127 
0128     // do not change item of active tab
0129     if (next == _active) return;
0130 
0131     next->activate(i);
0132 }
0133 
0134 void MultiView::activated(TraceItemView* sender, CostItem* i)
0135 {
0136     if (0) qDebug() << "MultiView::activated " << i->name()
0137                     << ", sender " << sender->widget()->objectName();
0138 
0139     // we react only on selection changes of the active TabView
0140     if (sender != (TraceItemView*)_active) return;
0141 
0142     TraceItemView::activated(sender,i);
0143 }
0144 
0145 void MultiView::doUpdate(int changeType, bool force)
0146 {
0147     foreach(TabView* tv, _views) {
0148         tv->set(changeType, _data, _eventType, _eventType2,
0149                 _groupType, _partList,
0150                 (tv == _active) ? _activeItem : tv->activeItem(),
0151                 tv->selectedItem());
0152         tv->notifyChange(changeType);
0153         if (tv->isViewVisible())
0154             tv->updateView(force);
0155     }
0156 }
0157 
0158 
0159 void MultiView::restoreLayout(const QString& prefix, const QString& postfix)
0160 {
0161     ConfigGroup* g = ConfigStorage::group(prefix, postfix);
0162 
0163     int panelCount = g->value(QStringLiteral("Panels"), 1).toInt();;
0164     QString o      = g->value(QStringLiteral("Orientation"), QStringLiteral("Vertical")).toString();
0165     QString active = g->value(QStringLiteral("ActivePanel"), QString()).toString();
0166 
0167     setChildCount(panelCount);
0168     setOrientation( o == QLatin1String("Horizontal") ?
0169                         Qt::Horizontal : Qt::Vertical );
0170     if ( panelCount>1 ) {
0171         QList<int> sizes = toIntList(g->value(QStringLiteral("PanelSizes"), QStringList()).toStringList());
0172         setSizes(sizes);
0173     }
0174     delete g;
0175 
0176     TabView* activeTV = nullptr;
0177     foreach(TabView* tv, _views) {
0178         if (tv->objectName() == active) activeTV=tv;
0179         tv->restoreLayout( QStringLiteral("%1-%2").arg(prefix).arg(tv->objectName()),
0180                            postfix);
0181     }
0182 
0183     // activate panel after restoring
0184     if (!activeTV) activeTV = _views.first();
0185 
0186     if (_active == activeTV)
0187         TraceItemView::activated(_active->activeItem());
0188     else
0189         activeTV->setActive(true);
0190 }
0191 
0192 void MultiView::saveLayout(const QString& prefix, const QString& postfix)
0193 {
0194     ConfigGroup* g = ConfigStorage::group(prefix + postfix);
0195 
0196     g->setValue(QStringLiteral("Panels"), childCount());
0197     g->setValue(QStringLiteral("Orientation"),
0198                 QString( (orientation() == Qt::Horizontal) ?
0199                              "Horizontal" : "Vertical"),
0200                 QStringLiteral("Vertical"));
0201 
0202     g->setValue(QStringLiteral("PanelSizes"), toStringList(sizes()));
0203     g->setValue(QStringLiteral("ActivePanel"),
0204                 _active ? QString(_active->objectName()) : QStringLiteral("none"));
0205     delete g;
0206 
0207     foreach(TabView* tv, _views)
0208         tv->saveLayout(QStringLiteral("%1-%2").arg(prefix).arg(tv->objectName()),
0209                        postfix);
0210 }
0211 
0212 
0213 
0214 void MultiView::restoreOptions(const QString& prefix, const QString& postfix)
0215 {
0216     foreach(TabView* tv, _views)
0217         tv->restoreOptions(QStringLiteral("%1-%2").arg(prefix).arg(tv->objectName()),
0218                            postfix);
0219 }
0220 
0221 void MultiView::saveOptions(const QString& prefix, const QString& postfix)
0222 {
0223     foreach(TabView* tv, _views)
0224         tv->saveOptions(QStringLiteral("%1-%2").arg(prefix).arg(tv->objectName()),
0225                         postfix);
0226 }
0227 
0228 #include "moc_multiview.cpp"