File indexing completed on 2024-04-21 04:35:59
0001 /* This file is part of KDevelop 0002 Copyright 2018 Anton Anikin <anton@anikin.xyz> 0003 0004 This program is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 General Public License for more details. 0013 0014 You should have received a copy of the GNU General Public License 0015 along with this program; see the file COPYING. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "menubutton.h" 0021 0022 #include <QMenu> 0023 0024 #include <KLocalizedString> 0025 0026 namespace Valgrind 0027 { 0028 0029 MenuButton::MenuButton(QWidget* parent) 0030 : QPushButton(parent) 0031 { 0032 setMenu(new QMenu(this)); 0033 } 0034 0035 QAction* MenuButton::addAction(const QString& text, const QString& data) 0036 { 0037 auto action = new QAction(text, this); 0038 action->setData(data); 0039 action->setCheckable(true); 0040 action->setChecked(false); 0041 0042 connect(action, &QAction::toggled, this, &MenuButton::updateValue); 0043 0044 menu()->addAction(action); 0045 updateValue(); 0046 0047 return action; 0048 } 0049 0050 QString MenuButton::value() const 0051 { 0052 return m_value; 0053 } 0054 0055 void MenuButton::setValue(const QString& value) 0056 { 0057 if (m_value == value) { 0058 return; 0059 } 0060 0061 const auto enabled = value.split(QLatin1Char(',')); 0062 const auto actions = menu()->actions(); 0063 0064 for (auto action : actions) { 0065 QSignalBlocker blocker(action); 0066 0067 if (value == QStringLiteral("all")) { 0068 action->setChecked(true); 0069 } else if (value == QStringLiteral("none")) { 0070 action->setChecked(false); 0071 } else { 0072 action->setChecked(enabled.contains(action->data().toString())); 0073 } 0074 } 0075 0076 QSignalBlocker blocker(this); 0077 updateValue(); 0078 } 0079 0080 void MenuButton::updateValue() 0081 { 0082 QStringList dataSelected; 0083 QStringList textSelected; 0084 0085 const auto actions = menu()->actions(); 0086 for (auto action : actions) { 0087 if (action->isChecked()) { 0088 dataSelected += action->data().toString(); 0089 textSelected += action->text(); 0090 } 0091 } 0092 0093 if (dataSelected.isEmpty()) { 0094 m_value = QStringLiteral("none"); 0095 setText(i18n("none")); 0096 } else if (dataSelected.size() == actions.size()) { 0097 m_value = QStringLiteral("all"); 0098 setText(i18n("all")); 0099 } else { 0100 m_value = dataSelected.join(QLatin1Char(',')); 0101 setText(textSelected.join(QLatin1String(", "))); 0102 } 0103 0104 emit valueChanged(m_value); 0105 } 0106 0107 }