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

0001 /*
0002     This file is part of KCachegrind.
0003 
0004     SPDX-FileCopyrightText: 2010-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only
0007 */
0008 
0009 /*
0010  * Global configuration for GUI components of KCachegrind
0011  */
0012 
0013 #include "globalguiconfig.h"
0014 
0015 #include "config.h"
0016 
0017 
0018 //
0019 // ConfigColorSettings
0020 //
0021 
0022 ConfigColorSetting::ConfigColorSetting(QString n)
0023 {
0024     _name = n;
0025     reset();
0026 }
0027 
0028 ConfigColorSetting::ConfigColorSetting(QString n, QColor c)
0029 {
0030     _name = n;
0031     setColor(c);
0032 }
0033 
0034 void ConfigColorSetting::setColor(const QColor& c)
0035 {
0036     _color = c;
0037     _automatic = (c == colorForName(_name));
0038 }
0039 
0040 QColor ConfigColorSetting::colorForName(QString n)
0041 {
0042     int h = 0, s = 100;
0043     foreach(const QChar c, n) {
0044         h = (h * 37 + s* c.unicode()) % 256;
0045         s = (s * 17 + h* c.unicode()) % 192;
0046     }
0047 
0048     return QColor::fromHsv(h, 64+s, 192);
0049 }
0050 
0051 QColor ConfigColorSetting::autoColor() const
0052 {
0053     return colorForName(_name);
0054 }
0055 
0056 void ConfigColorSetting::reset()
0057 {
0058     _automatic = true;
0059     _color = colorForName(_name);
0060 }
0061 
0062 
0063 //
0064 // GlobalGUIConfig
0065 //
0066 
0067 GlobalGUIConfig::GlobalGUIConfig()
0068     : GlobalConfig()
0069 {
0070 }
0071 
0072 GlobalGUIConfig::~GlobalGUIConfig()
0073 {
0074     qDeleteAll(_colors);
0075     _colors.clear();
0076 }
0077 
0078 GlobalGUIConfig* GlobalGUIConfig::config()
0079 {
0080     GlobalGUIConfig* gc;
0081 
0082     if (_config == nullptr) {
0083         gc = new GlobalGUIConfig();
0084         _config = gc;
0085     }
0086     else {
0087         gc = dynamic_cast<GlobalGUIConfig*>(_config);
0088         if (gc == nullptr)
0089             qFatal("Internal error: config object is not a GlobalGUIConfig.");
0090     }
0091 
0092     return gc;
0093 }
0094 
0095 void GlobalGUIConfig::saveOptions()
0096 {
0097     // color options
0098     ConfigGroup* colorConfig = ConfigStorage::group(QStringLiteral("CostColors"));
0099     int count = 1;
0100     foreach(ConfigColorSetting* cs, _colors) {
0101         if ( !cs->_automatic ) {
0102             colorConfig->setValue( QStringLiteral("Name%1").arg(count),
0103                                    cs->_name);
0104             colorConfig->setValue( QStringLiteral("Color%1").arg(count),
0105                                    cs->_color);
0106             count++;
0107         }
0108     }
0109     colorConfig->setValue(QStringLiteral("Count"), count-1);
0110     delete colorConfig;
0111 
0112     GlobalConfig::saveOptions();
0113 }
0114 
0115 void GlobalGUIConfig::readOptions()
0116 {
0117     int i, count;
0118 
0119     // color options
0120     _colors.clear();
0121     // colors for default event types:
0122     //  red for LL/L2 misses
0123     colorSetting(QStringLiteral("EventType-I2mr"))->_color = QColor(240, 0, 0);
0124     colorSetting(QStringLiteral("EventType-D2mr"))->_color = QColor(180,40,40);
0125     colorSetting(QStringLiteral("EventType-D2mw"))->_color = QColor(120,80,80);
0126     colorSetting(QStringLiteral("EventType-ILmr"))->_color = QColor(240, 0, 0);
0127     colorSetting(QStringLiteral("EventType-DLmr"))->_color = QColor(180,40,40);
0128     colorSetting(QStringLiteral("EventType-DLmw"))->_color = QColor(120,80,80);
0129     //  green for L1 misses
0130     colorSetting(QStringLiteral("EventType-I1mr"))->_color = QColor(0, 240, 0);
0131     colorSetting(QStringLiteral("EventType-D1mr"))->_color = QColor(40,180,40);
0132     colorSetting(QStringLiteral("EventType-D1mw"))->_color = QColor(80,120,80);
0133     // yellow for branches/mispredictions
0134     colorSetting(QStringLiteral("EventType-Bc")) ->_color = QColor(240,240, 0);
0135     colorSetting(QStringLiteral("EventType-Bcm"))->_color = QColor(200,200,30);
0136     colorSetting(QStringLiteral("EventType-Bi")) ->_color = QColor(160,160,60);
0137     colorSetting(QStringLiteral("EventType-Bim"))->_color = QColor(120,120,90);
0138     // blue for normal accesses
0139     colorSetting(QStringLiteral("EventType-Ir"))->_color = QColor(0, 0, 240);
0140     colorSetting(QStringLiteral("EventType-Dr"))->_color = QColor(40,40,180);
0141     colorSetting(QStringLiteral("EventType-Dw"))->_color = QColor(80,80,120);
0142 
0143     ConfigGroup* colorConfig = ConfigStorage::group(QStringLiteral("CostColors"));
0144     count = colorConfig->value(QStringLiteral("Count"), 0).toInt();
0145     for(i=1; i<=count; ++i) {
0146         QString n = colorConfig->value(QStringLiteral("Name%1").arg(i),
0147                                        QString()).toString();
0148         QColor color = colorConfig->value<QColor>(QStringLiteral("Color%1").arg(i),
0149                                                   QColor(Qt::black));
0150 
0151         if (n.isEmpty()) continue;
0152 
0153         ConfigColorSetting* cs = new ConfigColorSetting(n,color);
0154         _colors.insert(n, cs);
0155     }
0156     delete colorConfig;
0157 
0158     GlobalConfig::readOptions();
0159 }
0160 
0161 ConfigColorSetting* GlobalGUIConfig::groupColorSetting(CostItem* cost)
0162 {
0163     if (!cost)
0164         return colorSetting(QStringLiteral("default"));
0165 
0166     return groupColorSetting(cost->type(), cost->name());
0167 }
0168 
0169 ConfigColorSetting* GlobalGUIConfig::groupColorSetting(ProfileContext::Type t,
0170                                                        QString name)
0171 {
0172     QString n = ProfileContext::typeName(t) + '-' + name;
0173     return colorSetting(n);
0174 }
0175 
0176 QColor GlobalGUIConfig::groupColor(CostItem* cost)
0177 {
0178     return groupColorSetting(cost)->color();
0179 }
0180 
0181 QColor GlobalGUIConfig::eventTypeColor(EventType* t)
0182 {
0183     QString n;
0184 
0185     if (!t)
0186         n = QStringLiteral("EventType-default");
0187     else
0188         n = QStringLiteral("EventType-%1").arg(t->name());
0189 
0190     return colorSetting(n)->color();
0191 }
0192 
0193 QColor GlobalGUIConfig::functionColor(ProfileContext::Type gt,
0194                                       TraceFunction* f)
0195 {
0196     ProfileCostArray* group = f;
0197     QString n;
0198 
0199     switch(gt) {
0200     case ProfileContext::Object: group = f->object(); break;
0201     case ProfileContext::Class:  group = f->cls(); break;
0202     case ProfileContext::File:   group = f->file(); break;
0203     default:
0204         break;
0205     }
0206 
0207     if (group != f) {
0208         // first look for manual color of a function in a group
0209         n = ProfileContext::typeName(group->type()) +
0210             '-' + group->name() +
0211             '-' + f->name();
0212 
0213         ConfigColorSetting* cs = colorSetting(n, false);
0214         if (cs) return cs->color();
0215     }
0216     return groupColor(group);
0217 }
0218 
0219 ConfigColorSetting* GlobalGUIConfig::colorSetting(const QString& n,
0220                                                   bool createNew)
0221 {
0222     // predefined ?
0223     GlobalGUIConfig* c = config();
0224     ConfigColorSetting* cs = c->_colors.value(n, nullptr);
0225     if (cs || !createNew) return cs;
0226 
0227     cs = new ConfigColorSetting(n);
0228     c->_colors.insert(n, cs);
0229 
0230     return cs;
0231 }
0232