Debian Codegraph @

src/Gui/SplitButton.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /****************************************************************************
3 * *
4 * Copyright (c) 2025 Kacper Donat <[email protected]> *
5 * *
6 * This file is part of FreeCAD. *
7 * *
8 * FreeCAD is free software: you can redistribute it and/or modify it *
9 * under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation, either version 2.1 of the *
11 * License, or (at your option) any later version. *
12 * *
13 * FreeCAD is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with FreeCAD. If not, see *
20 * <https://www.gnu.org/licenses/>. *
21 * *
22 ***************************************************************************/
23
24 #include "SplitButton.h"
25
26 #include <QHBoxLayout>
27
28 using namespace Gui;
29
30 SplitButton::SplitButton(QWidget* parent)
31 : SplitButton(QStringLiteral(""), parent)
32 {}
33
34 SplitButton::SplitButton(const QString& text, QWidget* parent)
35 : QWidget(parent)
36 , m_main(new QPushButton(text, this))
37 , m_menuButton(new QToolButton(this))
38 , m_menu(new QMenu(this))
39 {
40 auto* layout = new QHBoxLayout(this);
41
42 layout->setContentsMargins(0, 0, 0, 0);
43 layout->setSpacing(0);
44 layout->addWidget(m_main);
45 layout->addWidget(m_menuButton);
46
47 // Behavior
48 m_main->setAutoDefault(false);
49 m_main->setDefault(false);
50
51 m_menuButton->setMenu(m_menu);
52 m_menuButton->setPopupMode(QToolButton::InstantPopup);
53 m_menuButton->setArrowType(Qt::DownArrow);
54 m_menuButton->setToolButtonStyle(Qt::ToolButtonIconOnly);
55
56 connect(m_main, &QPushButton::clicked, this, &SplitButton::defaultClicked);
57 connect(m_menu, &QMenu::triggered, this, &SplitButton::triggered);
58
59 // Styling to make it look like a single split button
60 m_main->setProperty("splitRole", QLatin1String("main"));
61 m_menuButton->setProperty("splitRole", QLatin1String("menu"));
62 }