Skip to content

Commit

Permalink
Gui: add support for pie menu
Browse files Browse the repository at this point in the history
  • Loading branch information
realthunder committed Jul 24, 2020
1 parent 75a7f70 commit e3d5306
Show file tree
Hide file tree
Showing 20 changed files with 1,279 additions and 12 deletions.
32 changes: 26 additions & 6 deletions src/Gui/Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "SelectionView.h"
#include "ViewParams.h"
#include "BitmapFactory.h"
#include "PieMenu.h"

#include <Base/Exception.h>
#include <App/Application.h>
Expand Down Expand Up @@ -1146,6 +1147,10 @@ void SelUpAction::onShowMenu()
{
_menu->clear();
setupMenuStyle(_menu);
if (_menu->actions().isEmpty()) {
auto action = _menu->addAction(tr("<None>"));
action->setDisabled(true);
}
TreeWidget::populateSelUpMenu(_menu);
}

Expand Down Expand Up @@ -1353,20 +1358,35 @@ class CmdHistoryMenu: public QMenu
CmdHistoryMenu(QWidget *focus)
:focusWidget(focus)
{}

protected:
void keyPressEvent(QKeyEvent *e)
{
if (isVisible() && e->key() == Qt::Key_Space) {
focusWidget->setFocus();
e->accept();
if (e->key() == Qt::Key_Space) {
if (!isVisible()) {
keyFocus = true;
exec(QCursor::pos());
} else {
focusWidget->setFocus();
e->accept();
}
return;
}
QMenu::keyPressEvent(e);
return;
}

void showEvent(QShowEvent *ev)
{
QMenu::showEvent(ev);
if (keyFocus) {
keyFocus = false;
focusWidget->setFocus();
}
}

public:
QWidget *focusWidget;
bool keyFocus = false;
};

// --------------------------------------------------------------------
Expand Down Expand Up @@ -1506,7 +1526,7 @@ void CmdHistoryAction::onCommandActivated(const QByteArray &name)

void CmdHistoryAction::popup(const QPoint &pt)
{
_menu->exec(pt);
PieMenu::exec(_menu, pt, _pcCmd->getName(), true);
}

// --------------------------------------------------------------------
Expand Down Expand Up @@ -1717,7 +1737,7 @@ void ToolbarMenuAction::update()

void ToolbarMenuAction::popup(const QPoint &pt)
{
_menu->exec(pt);
PieMenu::exec(_menu, pt, _pcCmd->getName());
_menu->setActiveAction(0);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Gui/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ protected Q_SLOTS:
std::unique_ptr<Private> _pimpl;
};

GuiExport void setupMenuStyle(QMenu *);
GuiExport void setupMenuStyle(QWidget *);

} // namespace Gui

Expand Down
6 changes: 6 additions & 0 deletions src/Gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ set(Gui_MOC_HDRS
DlgSettingsNavigation.h
DlgSettingsViewColor.h
DlgSettingsDrawStyles.h
DlgSettingsPieMenu.h
DlgSettingsColorGradientImp.h
DlgSettingsDocumentImp.h
DlgSettingsImageImp.h
Expand Down Expand Up @@ -361,6 +362,7 @@ set(Gui_MOC_HDRS
ReportView.h
SceneInspector.h
SelectionView.h
PieMenu.h
SpinBox.h
Splashscreen.h
TextEdit.h
Expand Down Expand Up @@ -674,6 +676,7 @@ SET(Dialog_Settings_CPP_SRCS
DlgSettingsUnitsImp.cpp
DlgSettingsViewColor.cpp
DlgSettingsDrawStyles.cpp
DlgSettingsPieMenu.cpp
DlgSettingsColorGradientImp.cpp
DlgSettingsDocumentImp.cpp
DlgSettingsImageImp.cpp
Expand All @@ -690,6 +693,7 @@ SET(Dialog_Settings_HPP_SRCS
DlgSettingsUnitsImp.h
DlgSettingsViewColor.h
DlgSettingsDrawStyles.h
DlgSettingsPieMenu.h
DlgSettingsColorGradientImp.h
DlgSettingsDocumentImp.h
DlgSettingsImageImp.h
Expand Down Expand Up @@ -722,6 +726,7 @@ SET(Dock_Windows_CPP_SRCS
PropertyView.cpp
ReportView.cpp
SelectionView.cpp
PieMenu.cpp
ToolBox.cpp
Tree.cpp
TreeView.cpp
Expand All @@ -738,6 +743,7 @@ SET(Dock_Windows_HPP_SRCS
PropertyView.h
ReportView.h
SelectionView.h
PieMenu.h
ToolBox.h
Tree.h
TreeView.h
Expand Down
166 changes: 166 additions & 0 deletions src/Gui/DlgSettingsPieMenu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/****************************************************************************
* Copyright (c) 2020 Zheng, Lei (realthunder) <[email protected]>*
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
****************************************************************************/

#include "PreCompiled.h"

#ifndef _PreComp_
# include <QLabel>
# include <QCheckBox>
# include <QSpinBox>
# include <QApplication>
# include <QGridLayout>
# include <QVBoxLayout>
# include <QHBoxLayout>
#endif

#include "DlgSettingsPieMenu.h"
#include "ViewParams.h"

using namespace Gui::Dialog;

/* TRANSLATOR Gui::Dialog::DlgSettingsPieMenu */

class DlgSettingsPieMenu::Private
{
public:
#define FC_PIEMENU_PARAMS \
FC_PIEMENU_PARAM2(PieMenuIconSize, "Icon size", QSpinBox, 0, 64, 1, value, setValue) \
FC_PIEMENU_PARAM2(PieMenuRadius, "Radius", QSpinBox, 10, 500, 10, value, setValue) \
FC_PIEMENU_PARAM2(PieMenuTriggerRadius, "Trigger radius", QSpinBox, 10, 500, 10, value, setValue) \
FC_PIEMENU_PARAM2(PieMenuFontSize, "Font size", QSpinBox, 0, 32, 1, value, setValue) \
FC_PIEMENU_PARAM2(PieMenuTriggerDelay, "Trigger delay (ms)", QSpinBox, 0, 10000, 100, value, setValue) \
FC_PIEMENU_PARAM(PieMenuTriggerAction, "Trigger action", QCheckBox, isChecked, setChecked) \
FC_PIEMENU_PARAM2(PieMenuAnimationDuration, "Animation duration (ms)", QSpinBox, 0, 1000, 100, value, setValue) \

#undef FC_PIEMENU_PARAM
#define FC_PIEMENU_PARAM(_name, _label, _type, _getter, _setter) \
_type * _name = nullptr;\
QLabel *label##_name = nullptr;

#undef FC_PIEMENU_PARAM2
#define FC_PIEMENU_PARAM2(_name, _label, _type, _min, _max, _step, _setter, _getter) \
FC_PIEMENU_PARAM(_name, _label, _type, _setter, _getter);

FC_PIEMENU_PARAMS

#undef FC_PIEMENU_PARAM
#define FC_PIEMENU_PARAM(_name, _label, _type, _getter, _setter) do {\
label##_name->setText(tr(_label));\
QString tooltip = QApplication::translate("ViewParams", ViewParams::doc##_name()); \
if (!tooltip.isEmpty()) {\
_name->setToolTip(tooltip); \
label##_name->setToolTip(tooltip);\
}\
}while(0);

void init()
{
FC_PIEMENU_PARAMS
}
};

DlgSettingsPieMenu::DlgSettingsPieMenu(QWidget* parent)
: PreferencePage(parent)
, ui(new Private)
{
auto vlayout = new QVBoxLayout(this);
auto hlayout = new QHBoxLayout();
vlayout->addLayout(hlayout);
vlayout->addStretch();
auto layout = new QGridLayout();
hlayout->addLayout(layout);
hlayout->addStretch();

setWindowTitle(tr("Pie menu"));

int row = 0;

#undef FC_PIEMENU_PARAM
#define FC_PIEMENU_PARAM(_name, _label, _type, _getter, _setter) do {\
ui->_name = new _type(parent);\
ui->label##_name = new QLabel(parent);\
ui->label##_name->setMinimumSize(240,0);\
layout->addWidget(ui->label##_name, row, 0);\
layout->addWidget(ui->_name, row, 1);\
++row;\
}while(0);

#undef FC_PIEMENU_PARAM2
#define FC_PIEMENU_PARAM2(_name, _label, _type, _min, _max, _step, _getter, _setter) \
FC_PIEMENU_PARAM(_name, _label, _type, _getter, _setter)\
ui->_name->setMaximum(_max);\
ui->_name->setMinimum(_min);\
ui->_name->setSingleStep(_step);

FC_PIEMENU_PARAMS;

#undef FC_PIEMENU_PARAM2
#define FC_PIEMENU_PARAM2(_name, _label, _type, _min, _max, _step, _setter, _getter) \
FC_PIEMENU_PARAM(_name, _label, _type, _setter, _getter)

ui->init();

#undef FC_PIEMENU_PARAM
#define FC_PIEMENU_PARAM(_name, _label, _type, _getter, _setter) \
ui->_name->_setter(ViewParams::get##_name());\

FC_PIEMENU_PARAMS;
}

/**
* Destroys the object and frees any allocated resources
*/
DlgSettingsPieMenu::~DlgSettingsPieMenu()
{
// no need to delete child widgets, Qt does it all for us
}

void DlgSettingsPieMenu::loadSettings()
{
FC_PIEMENU_PARAMS;
}

void DlgSettingsPieMenu::saveSettings()
{
#undef FC_PIEMENU_PARAM
#define FC_PIEMENU_PARAM(_name, _label, _type, _getter, _setter) \
ViewParams::set##_name(ui->_name->_getter());\

FC_PIEMENU_PARAMS;
}


/**
* Sets the strings of the subwidgets using the current language.
*/
void DlgSettingsPieMenu::changeEvent(QEvent *e)
{
if (e->type() == QEvent::LanguageChange) {
ui->init();
}
else {
QWidget::changeEvent(e);
}
}

#include "moc_DlgSettingsPieMenu.cpp"

58 changes: 58 additions & 0 deletions src/Gui/DlgSettingsPieMenu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/****************************************************************************
* Copyright (c) 2020 Zheng, Lei (realthunder) <[email protected]>*
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
****************************************************************************/

#ifndef GUI_DIALOG_DLGSETTINGSPIEMENU_H
#define GUI_DIALOG_DLGSETTINGSPIEMENU_H

#include "PropertyPage.h"
#include <memory>

namespace Gui {
namespace Dialog {

/**
* The DlgSettingsPie class implements a preference page to change settings
* of Pie Menu.
*/
class DlgSettingsPieMenu : public PreferencePage
{
Q_OBJECT

public:
DlgSettingsPieMenu(QWidget* parent = 0);
~DlgSettingsPieMenu();

void saveSettings();
void loadSettings();

protected:
void changeEvent(QEvent *e);

private:
class Private;
std::unique_ptr<Private> ui;
};

} // namespace Dialog
} // namespace Gui

#endif // GUI_DIALOG_DLGSETTINGSPIEMENU_H
Loading

0 comments on commit e3d5306

Please sign in to comment.