Merge pull request #4788 from 0penBrain/UserEditMode

Gui: add user edit mode (default edit mode set by user)
This commit is contained in:
Yorik van Havre
2021-09-02 11:50:03 +02:00
committed by GitHub
14 changed files with 1591 additions and 7 deletions

View File

@@ -227,7 +227,7 @@ void Action::setMenuRole(QAction::MenuRole menuRole)
* to the command object.
*/
ActionGroup::ActionGroup ( Command* pcCmd,QObject * parent)
: Action(pcCmd, parent), _group(0), _dropDown(false),_external(false),_toggle(false)
: Action(pcCmd, parent), _group(0), _dropDown(false),_external(false),_toggle(false),_isMode(false)
{
_group = new QActionGroup(this);
connect(_group, SIGNAL(triggered(QAction*)), this, SLOT(onActivated (QAction*)));
@@ -336,7 +336,7 @@ void ActionGroup::setCheckedAction(int i)
QAction* a = _group->actions()[i];
a->setChecked(true);
this->setIcon(a->icon());
this->setToolTip(a->toolTip());
if (!this->_isMode) this->setToolTip(a->toolTip());
this->setProperty("defaultAction", QVariant(i));
}
@@ -378,7 +378,7 @@ void ActionGroup::onActivated (QAction* a)
}
#endif
this->setIcon(a->icon());
this->setToolTip(a->toolTip());
if (!this->_isMode) this->setToolTip(a->toolTip());
this->setProperty("defaultAction", QVariant(index));
_pcCmd->invoke(index, Command::TriggerChildAction);
}

View File

@@ -107,6 +107,7 @@ public:
void setExclusive (bool);
bool isExclusive() const;
void setVisible (bool);
void setIsMode(bool b) { _isMode = b; }
void setDropDownMenu(bool b) { _dropDown = b; }
QAction* addAction(QAction*);
@@ -126,6 +127,7 @@ protected:
bool _dropDown;
bool _external;
bool _toggle;
bool _isMode;
};
// --------------------------------------------------------------------

View File

@@ -1244,6 +1244,50 @@ void Application::tryClose(QCloseEvent * e)
}
}
int Application::getUserEditMode(const std::string &mode) const
{
if (mode.empty()) {
return userEditMode;
}
for (auto const &uem : userEditModes) {
if (uem.second == mode) {
return uem.first;
}
}
return -1;
}
std::string Application::getUserEditModeName(int mode) const
{
if (mode == -1) {
return userEditModes.at(userEditMode);
}
if (userEditModes.find(mode) != userEditModes.end()) {
return userEditModes.at(mode);
}
return "";
}
bool Application::setUserEditMode(int mode)
{
if (userEditModes.find(mode) != userEditModes.end() && userEditMode != mode) {
userEditMode = mode;
this->signalUserEditModeChanged(userEditMode);
return true;
}
return false;
}
bool Application::setUserEditMode(const std::string &mode)
{
for (auto const &uem : userEditModes) {
if (uem.second == mode) {
return setUserEditMode(uem.first);
}
}
return false;
}
/**
* Activate the matching workbench to the registered workbench handler with name \a name.
* The handler must be an instance of a class written in Python.

View File

@@ -27,6 +27,7 @@
#include <QPixmap>
#include <string>
#include <vector>
#include <map>
#define putpix()
@@ -133,6 +134,8 @@ public:
boost::signals2::signal<void (const Gui::ViewProviderDocumentObject&)> signalInEdit;
/// signal on leaving edit mode
boost::signals2::signal<void (const Gui::ViewProviderDocumentObject&)> signalResetEdit;
/// signal on changing user edit mode
boost::signals2::signal<void (int)> signalUserEditModeChanged;
//@}
/** @name methods for Document handling */
@@ -227,6 +230,28 @@ public:
static void runApplication(void);
void tryClose( QCloseEvent * e );
//@}
/** @name User edit mode */
//@{
protected:
// the below std::map is a translation of 'EditMode' enum in ViewProvider.h
// to add a new edit mode, it should first be added there
// this is only used for GUI user interaction (menu, toolbar, Python API)
const std::map <int, std::string> userEditModes {
{0, QT_TRANSLATE_NOOP("EditMode", "Default")},
{1, QT_TRANSLATE_NOOP("EditMode", "Transform")},
{2, QT_TRANSLATE_NOOP("EditMode", "Cutting")},
{3, QT_TRANSLATE_NOOP("EditMode", "Color")}
};
int userEditMode = userEditModes.begin()->first;
public:
std::map <int, std::string> listUserEditModes() const { return userEditModes; }
int getUserEditMode(const std::string &mode = "") const;
std::string getUserEditModeName(int mode = -1) const;
bool setUserEditMode(int mode);
bool setUserEditMode(const std::string &mode);
//@}
public:
//---------------------------------------------------------------------
@@ -293,6 +318,10 @@ public:
static PyObject* sAddDocObserver (PyObject *self,PyObject *args);
static PyObject* sRemoveDocObserver (PyObject *self,PyObject *args);
static PyObject* sListUserEditModes (PyObject *self,PyObject *args);
static PyObject* sGetUserEditMode (PyObject *self,PyObject *args);
static PyObject* sSetUserEditMode (PyObject *self,PyObject *args);
static PyMethodDef Methods[];

View File

@@ -205,6 +205,18 @@ PyMethodDef Application::Methods[] = {
{"removeDocumentObserver", (PyCFunction) Application::sRemoveDocObserver, METH_VARARGS,
"removeDocumentObserver() -> None\n\n"
"Remove an added document observer."},
{"listUserEditModes", (PyCFunction) Application::sListUserEditModes, METH_VARARGS,
"listUserEditModes() -> list\n\n"
"List available user edit modes"},
{"getUserEditMode", (PyCFunction) Application::sGetUserEditMode, METH_VARARGS,
"getUserEditMode() -> string\n\n"
"Get current user edit mode"},
{"setUserEditMode", (PyCFunction) Application::sSetUserEditMode, METH_VARARGS,
"setUserEditMode(string=mode) -> Bool\n\n"
"Set user edit mode to 'mode', returns True if exists, false otherwise"},
{"reload", (PyCFunction) Application::sReload, METH_VARARGS,
"reload(name) -> doc\n\n"
@@ -1485,3 +1497,29 @@ PyObject* Application::sCoinRemoveAllChildren(PyObject * /*self*/, PyObject *arg
}PY_CATCH;
}
PyObject* Application::sListUserEditModes(PyObject * /*self*/, PyObject *args)
{
Py::List ret;
if (!PyArg_ParseTuple(args, ""))
return NULL;
for (auto const &uem : Instance->listUserEditModes()) {
ret.append(Py::String(uem.second));
}
return Py::new_reference_to(ret);
}
PyObject* Application::sGetUserEditMode(PyObject * /*self*/, PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
return Py::new_reference_to(Py::String(Instance->getUserEditModeName()));
}
PyObject* Application::sSetUserEditMode(PyObject * /*self*/, PyObject *args)
{
char *mode = "";
if (!PyArg_ParseTuple(args, "s", &mode))
return NULL;
bool ok = Instance->setUserEditMode(std::string(mode));
return Py::new_reference_to(Py::Boolean(ok));
}

View File

@@ -31,6 +31,7 @@
#include <vector>
#include <Base/Type.h>
#include <Gui/Application.h>
/** @defgroup CommandMacros Helper macros for running commands through Python interpreter */
//@{
@@ -179,8 +180,8 @@
auto __obj = _obj;\
if(__obj && __obj->getNameInDocument()) {\
Gui::Command::doCommand(Gui::Command::Gui,\
"Gui.ActiveDocument.setEdit(App.getDocument('%s').getObject('%s'))",\
__obj->getDocument()->getName(), __obj->getNameInDocument());\
"Gui.ActiveDocument.setEdit(App.getDocument('%s').getObject('%s'), %i)",\
__obj->getDocument()->getName(), __obj->getNameInDocument(), Gui::Application::Instance->getUserEditMode());\
}\
}while(0)

View File

@@ -29,6 +29,7 @@
# include <QWhatsThis>
# include <QDesktopServices>
# include <QUrl>
# include <boost_bind_bind.hpp>
#endif
#include <boost/scoped_ptr.hpp>
@@ -63,6 +64,7 @@
using Base::Console;
using Base::Sequencer;
using namespace Gui;
namespace bp = boost::placeholders;
//===========================================================================
@@ -813,6 +815,100 @@ void StdCmdUnitsCalculator::activated(int iMsg)
dlg->show();
}
//===========================================================================
// StdCmdUserEditMode
//===========================================================================
class StdCmdUserEditMode : public Gui::Command
{
public:
StdCmdUserEditMode();
virtual ~StdCmdUserEditMode(){}
virtual void languageChange();
virtual const char* className() const {return "StdCmdUserEditMode";}
void updateIcon(int mode);
protected:
virtual void activated(int iMsg);
virtual bool isActive(void);
virtual Gui::Action * createAction(void);
};
StdCmdUserEditMode::StdCmdUserEditMode()
: Command("Std_UserEditMode")
{
sGroup = QT_TR_NOOP("Edit mode");
sMenuText = QT_TR_NOOP("Edit mode");
sToolTipText = QT_TR_NOOP("Defines behavior when editing an object from tree");
sStatusTip = QT_TR_NOOP("Defines behavior when editing an object from tree");
sWhatsThis = "Std_UserEditMode";
sPixmap = "EditModeDefault";
eType = ForEdit;
this->getGuiApplication()->signalUserEditModeChanged.connect(boost::bind(&StdCmdUserEditMode::updateIcon, this, bp::_1));
}
Gui::Action * StdCmdUserEditMode::createAction(void)
{
Gui::ActionGroup* pcAction = new Gui::ActionGroup(this, Gui::getMainWindow());
pcAction->setDropDownMenu(true);
pcAction->setIsMode(true);
applyCommandData(this->className(), pcAction);
for (auto const &uem : Gui::Application::Instance->listUserEditModes()) {
QAction* act = pcAction->addAction(QString());
auto modeName = QString::fromStdString(uem.second);
act->setCheckable(true);
act->setIcon(BitmapFactory().iconFromTheme(qPrintable(QString::fromLatin1("EditMode")+modeName)));
act->setObjectName(QString::fromLatin1("Std_EditMode")+modeName);
act->setWhatsThis(QString::fromLatin1(getWhatsThis()));
if (uem.first == 0) {
pcAction->setIcon(act->icon());
act->setChecked(true);
}
}
_pcAction = pcAction;
languageChange();
return pcAction;
}
void StdCmdUserEditMode::languageChange()
{
Command::languageChange();
if (!_pcAction)
return;
Gui::ActionGroup* pcAction = qobject_cast<Gui::ActionGroup*>(_pcAction);
QList<QAction*> a = pcAction->actions();
for (int i = 0 ; i < a.count() ; i++) {
auto modeName = QString::fromStdString(Gui::Application::Instance->getUserEditModeName(i));
a[i]->setText(QCoreApplication::translate(
"EditMode", qPrintable(modeName)));
a[i]->setToolTip(QCoreApplication::translate(
"EditMode", qPrintable(modeName+QString::fromLatin1(" mode"))));
}
}
void StdCmdUserEditMode::updateIcon(int mode)
{
Gui::ActionGroup *actionGroup = dynamic_cast<Gui::ActionGroup *>(_pcAction);
if (!actionGroup)
return;
actionGroup->setCheckedAction(mode);
}
void StdCmdUserEditMode::activated(int iMsg)
{
Gui::Application::Instance->setUserEditMode(iMsg);
}
bool StdCmdUserEditMode::isActive(void)
{
return true;
}
namespace Gui {
void CreateStdCommands(void)
@@ -842,6 +938,7 @@ void CreateStdCommands(void)
rcCmdMgr.addCommand(new StdCmdPythonWebsite());
rcCmdMgr.addCommand(new StdCmdTextDocument());
rcCmdMgr.addCommand(new StdCmdUnitsCalculator());
rcCmdMgr.addCommand(new StdCmdUserEditMode());
//rcCmdMgr.addCommand(new StdCmdMeasurementSimple());
//rcCmdMgr.addCommand(new StdCmdDownloadOnlineHelp());
//rcCmdMgr.addCommand(new StdCmdDescription());

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 9.5 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 22 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 19 KiB

View File

@@ -0,0 +1,231 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="64"
height="64"
viewBox="0 0 64 64"
id="svg2"
version="1.1">
<title
id="title930">Std_AxisCross</title>
<defs
id="defs4">
<linearGradient
id="linearGradient926">
<stop
style="stop-color:#ef2929;stop-opacity:1"
offset="0"
id="stop922" />
<stop
style="stop-color:#a40000;stop-opacity:1"
offset="1"
id="stop924" />
</linearGradient>
<linearGradient
id="linearGradient918">
<stop
style="stop-color:#8ae234;stop-opacity:1"
offset="0"
id="stop914" />
<stop
style="stop-color:#4e9a06;stop-opacity:1"
offset="1"
id="stop916" />
</linearGradient>
<linearGradient
id="linearGradient910">
<stop
style="stop-color:#729fcf;stop-opacity:1"
offset="0"
id="stop906" />
<stop
style="stop-color:#204a87;stop-opacity:1"
offset="1"
id="stop908" />
</linearGradient>
<marker
orient="auto"
refY="0"
refX="0"
id="marker4732"
style="overflow:visible">
<path
id="path4734"
style="fill:#00ff00;fill-opacity:1;fill-rule:evenodd;stroke:#00ff08;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
transform="matrix(-1.1,0,0,-1.1,-1.1,0)" />
</marker>
<marker
orient="auto"
refY="0"
refX="0"
id="Arrow2Lstart"
style="overflow:visible">
<path
id="path4174"
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:0.625;stroke-linejoin:round;stroke-opacity:1"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
transform="matrix(1.1,0,0,1.1,1.1,0)" />
</marker>
<marker
orient="auto"
refY="0"
refX="0"
id="Arrow1Lstart"
style="overflow:visible">
<path
id="path4156"
d="M 0,0 5,-5 -12.5,0 5,5 0,0 z"
style="fill:#000000;fill-opacity:1;fill-rule:evenodd;stroke:#000000;stroke-width:1pt;stroke-opacity:1"
transform="matrix(0.8,0,0,0.8,10,0)" />
</marker>
<linearGradient
id="linearGradient4067-6">
<stop
style="stop-color:#888a85;stop-opacity:1;"
offset="0"
id="stop4069-7" />
<stop
style="stop-color:#2e3436;stop-opacity:1;"
offset="1"
id="stop4071-5" />
</linearGradient>
<linearGradient
xlink:href="#linearGradient910"
id="linearGradient912"
x1="16.275192"
y1="999.11859"
x2="20.275194"
y2="1005.3622"
gradientUnits="userSpaceOnUse"
gradientTransform="translate(-0.32823259,0.25769489)" />
<linearGradient
xlink:href="#linearGradient918"
id="linearGradient920"
x1="37.791718"
y1="1008.163"
x2="41.744087"
y2="1014.2588"
gradientUnits="userSpaceOnUse"
gradientTransform="translate(0,0.67970822)" />
<linearGradient
xlink:href="#linearGradient926"
id="linearGradient928"
x1="32.554726"
y1="1037.6899"
x2="38.136963"
y2="1044.7837"
gradientUnits="userSpaceOnUse"
gradientTransform="translate(0,0.67970822)" />
</defs>
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title>Std_AxisCross</dc:title>
<dc:creator>
<cc:Agent>
<dc:title>[bitacovir]</dc:title>
</cc:Agent>
</dc:creator>
<dc:rights>
<cc:Agent>
<dc:title>FreeCAD LGPL2+</dc:title>
</cc:Agent>
</dc:rights>
<dc:publisher>
<cc:Agent>
<dc:title>FreeCAD</dc:title>
</cc:Agent>
</dc:publisher>
<dc:date>2020/12/20</dc:date>
</cc:Work>
</rdf:RDF>
</metadata>
<g
id="layer1"
transform="translate(0,-988.36216)">
<g
id="g4036-9-5"
transform="matrix(0.42333463,-1.0406928,1.2322046,0.04560555,-18.902112,1031.4247)"
style="fill:#73d216;fill-opacity:1;stroke:#172a04;stroke-width:1.753;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1">
<g
id="g4033-8-3"
transform="matrix(0.70857077,0.64561924,-0.7760537,0.70710678,30.848953,1.7173836)"
style="fill:#73d216;fill-opacity:1;stroke:#172a04;stroke-width:1.75119;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1">
<rect
style="fill:#73d216;fill-opacity:1;stroke:#172a04;stroke-width:1.75119;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect3261-1-56"
width="23"
height="6"
x="-25"
y="-38"
transform="scale(-1)" />
</g>
</g>
<path
style="fill:none;fill-opacity:1;stroke:#8ae234;stroke-width:2;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 17.758513,1025.996 23.516681,-15.1976"
id="path4083-0-29" />
<g
style="fill:#cc0000;fill-opacity:1;stroke:#280000;stroke-width:1.77168;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
transform="matrix(-0.09950839,-1.2704661,0.99694669,-0.07808517,-7.0272263,1034.6728)"
id="g4033-8-5">
<rect
transform="matrix(-0.61568026,0.78799608,-0.61567913,-0.78799696,0,0)"
y="-22.253193"
x="11.761694"
height="4.9622731"
width="24.257404"
id="rect3261-1-6"
style="fill:#cc0000;fill-opacity:1;stroke:#280000;stroke-width:1.65466;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
</g>
<path
style="fill:none;stroke:#ef2929;stroke-width:2;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="M 38.136962,1045.4634 17.397878,1027.7269"
id="path4083-0-2" />
<path
id="path872-8"
d="m 44,1049.0419 -8.562552,-14.5923 c -4.048591,1.9848 -5.937899,5.0071 -7.162254,8.3488 z"
style="fill:url(#linearGradient928);fill-opacity:1;stroke:#280000;stroke-width:2.50713;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
<path
style="fill:url(#linearGradient920);fill-opacity:1;stroke:#172a04;stroke-width:2.50713;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 52.025756,1005.4549 -16.544154,3.5417 c 0.611589,4.4672 2.886818,7.2108 5.674284,9.4235 z"
id="path872-8-1" />
<g
id="g4036-9"
transform="matrix(-0.64428531,-0.92040759,0.70710678,-1.0101525,0.936348,1060.8558)"
style="fill:#3465a4;fill-opacity:1;stroke:#0b1521;stroke-width:1.753;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1">
<g
id="g4033-8"
transform="matrix(0.53748674,0.48973482,-0.7760537,0.70710678,30.821878,1.6927139)"
style="fill:#3465a4;fill-opacity:1;stroke:#0b1521;stroke-width:1.75119;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1">
<rect
style="fill:#3465a4;fill-opacity:1;stroke:#0b1521;stroke-width:1.75119;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect3261-1"
width="23"
height="6"
x="-25"
y="-38"
transform="scale(-1)" />
</g>
</g>
<path
style="fill:none;stroke:#729fcf;stroke-width:2;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="M 16.157967,1027.8686 V 999.86865"
id="path4083-0" />
<path
style="fill:url(#linearGradient912);fill-opacity:1;stroke:#0b1521;stroke-width:2.50713;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
d="m 17.171767,991.6197 -5.5,16.0002 c 4.142481,1.7805 7.666483,1.2466 11,0 z"
id="path872" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@@ -241,6 +241,10 @@
<file>document-package.svg</file>
<file>Std_Alignment.svg</file>
<file>Std_DuplicateSelection.svg</file>
<file>EditModeDefault.svg</file>
<file>EditModeTransform.svg</file>
<file>EditModeCutting.svg</file>
<file>EditModeColor.svg</file>
</qresource>
<!-- Demonstrating support for an embedded icon theme -->
<!-- See also http://permalink.gmane.org/gmane.comp.lib.qt.general/26374 -->

View File

@@ -420,6 +420,9 @@ public:
* you can handle most of the events in the viewer by yourself
*/
//@{
// the below enum is reflected in 'userEditModes' std::map in Application.h
// so it is possible for the user to choose a default one through GUI
// if you add a mode here, consider to make it accessible there too
enum EditMode {Default = 0,
Transform,
Cutting,

View File

@@ -588,7 +588,7 @@ MenuItem* StdWorkbench::setupMenuBar() const
<< "Std_Refresh" << "Std_BoxSelection" << "Std_BoxElementSelection"
<< "Std_SelectAll" << "Std_Delete" << "Std_SendToPythonConsole"
<< "Separator" << "Std_Placement" << "Std_TransformManip" << "Std_Alignment"
<< "Std_Edit" << "Separator" << "Std_DlgPreferences";
<< "Std_Edit" << "Separator" << "Std_UserEditMode" << "Separator" << "Std_DlgPreferences";
MenuItem* axoviews = new MenuItem;
axoviews->setCommand("Axonometric");
@@ -713,7 +713,7 @@ ToolBarItem* StdWorkbench::setupToolBars() const
file->setCommand("File");
*file << "Std_New" << "Std_Open" << "Std_Save" << "Std_Print" << "Separator" << "Std_Cut"
<< "Std_Copy" << "Std_Paste" << "Separator" << "Std_Undo" << "Std_Redo" << "Separator"
<< "Std_Refresh" << "Separator" << "Std_WhatsThis";
<< "Std_UserEditMode" << "Separator" << "Std_Refresh" << "Separator" << "Std_WhatsThis";
// Workbench switcher
ToolBarItem* wb = new ToolBarItem( root );