File indexing completed on 2024-04-28 05:41:40
0001 /* 0002 This file is part of KCachegrind. 0003 0004 SPDX-FileCopyrightText: 2009-2016 Josef Weidendorfer <Josef.Weidendorfer@gmx.de> 0005 0006 SPDX-License-Identifier: GPL-2.0-only 0007 */ 0008 0009 /* 0010 * QCachegrind configuration dialog 0011 */ 0012 0013 #include "configdialog.h" 0014 0015 #include <QWidget> 0016 #include <QLabel> 0017 #include <QFrame> 0018 #include <QListWidget> 0019 #include <QStackedWidget> 0020 #include <QDialogButtonBox> 0021 #include <QHBoxLayout> 0022 #include <QVBoxLayout> 0023 #include <QTimer> 0024 0025 #include "generalsettings.h" 0026 #include "sourcesettings.h" 0027 #include "colorsettings.h" 0028 0029 // 0030 // ConfigDialog 0031 // 0032 0033 ConfigDialog::ConfigDialog(TraceData* data, QWidget* parent, const QString &s) 0034 : QDialog(parent) 0035 { 0036 setWindowTitle(tr("Configure QCachegrind")); 0037 0038 _listWidget = new QListWidget(this); 0039 _listWidget->setMaximumWidth(140); 0040 _widgetStack = new QStackedWidget(this); 0041 _titleLabel = new QLabel(this); 0042 QFont labelFont; 0043 labelFont.setBold(true); 0044 _titleLabel->setFont(labelFont); 0045 _errorLabel = new QLabel(this); 0046 _errorLabel->setIndent(9); 0047 0048 QDialogButtonBox* bbox = new QDialogButtonBox(this); 0049 bbox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok); 0050 0051 QVBoxLayout* vbox1 = new QVBoxLayout(); 0052 vbox1->addWidget(_titleLabel); 0053 QFrame* f1 = new QFrame(this); 0054 f1->setFrameShape(QFrame::HLine); 0055 vbox1->addWidget(f1); 0056 vbox1->addWidget(_errorLabel); 0057 vbox1->addWidget(_widgetStack); 0058 0059 QHBoxLayout* hbox = new QHBoxLayout(); 0060 hbox->addWidget(_listWidget); 0061 hbox->addLayout(vbox1); 0062 QVBoxLayout* vbox = new QVBoxLayout(this); 0063 vbox->addLayout(hbox); 0064 QFrame* f2 = new QFrame(this); 0065 f2->setFrameStyle(QFrame::HLine | QFrame::Sunken); 0066 vbox->addWidget(f2); 0067 vbox->addWidget(bbox); 0068 0069 connect(bbox, SIGNAL(accepted()), this, SLOT(accept())); 0070 connect(bbox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0071 connect(_listWidget, SIGNAL(currentTextChanged(QString)), 0072 this, SLOT(listItemChanged(QString))); 0073 connect(&_clearTimer, &QTimer::timeout, this, &ConfigDialog::clearError); 0074 0075 addPage(new GeneralSettings(this)); 0076 addPage(new SourceSettings(data, this)); 0077 addPage(new ColorSettings(data, this)); 0078 0079 activate(s); 0080 } 0081 0082 void ConfigDialog::addPage(ConfigPage* p) 0083 { 0084 _widgetStack->addWidget(p); 0085 _listWidget->addItem(p->title()); 0086 _pages.insert(p->title(), p); 0087 } 0088 0089 void ConfigDialog::listItemChanged(QString s) 0090 { 0091 ConfigPage* p = _pages.value(s); 0092 if (!p) return; 0093 0094 _titleLabel->setText(p->longTitle()); 0095 _widgetStack->setCurrentWidget(p); 0096 if (!_activeSetting.isEmpty()) { 0097 p->activate(_activeSetting); 0098 _activeSetting.clear(); 0099 } 0100 } 0101 0102 void ConfigDialog::clearError() 0103 { 0104 _errorLabel->setText(QString()); 0105 } 0106 0107 void ConfigDialog::activate(QString s) 0108 { 0109 if (s.isEmpty()) 0110 _listWidget->setCurrentRow(0); 0111 0112 QString page = s; 0113 _activeSetting.clear(); 0114 int p = s.indexOf(QLatin1Char('/')); 0115 if (p>0) { 0116 page = s.left(p); 0117 _activeSetting = s.mid(p+1); 0118 } 0119 0120 for(int row=0; row<_listWidget->count(); row++) { 0121 QListWidgetItem* i = _listWidget->item(row); 0122 if (i->text() != page) continue; 0123 0124 if (_listWidget->currentRow() == row) 0125 // even without page change, forward activation 0126 listItemChanged(page); 0127 else 0128 _listWidget->setCurrentRow(row); 0129 } 0130 } 0131 0132 QString ConfigDialog::currentPage() 0133 { 0134 return _listWidget->currentItem()->text(); 0135 } 0136 0137 void ConfigDialog::accept() 0138 { 0139 ConfigPage* p; 0140 QString errorMsg, errorItem; 0141 foreach(p, _pages) 0142 if (!p->check(errorMsg, errorItem)) { 0143 if (!errorMsg.isEmpty()) { 0144 errorMsg = QStringLiteral("<font color=red>%1</color>").arg(errorMsg); 0145 _errorLabel->setText(errorMsg); 0146 _clearTimer.start(5000); 0147 } 0148 activate(QStringLiteral("%1/%2").arg(p->title(), errorItem)); 0149 return; 0150 } 0151 0152 foreach(p, _pages) 0153 p->accept(); 0154 0155 QDialog::accept(); 0156 } 0157 0158 #include "moc_configdialog.cpp"