File size: 8,455 Bytes
a5ffdcd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | /****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file gpl-2.0.txt included in the
** packaging of this file.
**
** This program 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/
#include "qg_snaptoolbar.h"
#include "lc_actiongroupmanager.h"
#include "lc_snapoptionswidgetsholder.h"
#include "qg_actionhandler.h"
#include "rs_settings.h"
QAction* QG_SnapToolBar::justAddAction(QString name, const QMap<QString, QAction*> &actionsMap){
auto* action = actionsMap[name];
addAction(action);
return action;
}
QAction* QG_SnapToolBar::addOwnAction(QString name, const QMap<QString, QAction*> &actionsMap){
auto* action = actionsMap[name];
connect(action, &QAction::triggered, this, &QG_SnapToolBar::actionTriggered);
addAction(action);
return action;
}
/*
* Constructs a QG_CadDToolBarSnap as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*/
QG_SnapToolBar::QG_SnapToolBar(QWidget* parent, QG_ActionHandler* ah, LC_ActionGroupManager* agm, const QMap<QString, QAction*> &actionsMap)
: QToolBar(parent), m_actionHandler(ah){
auto action = justAddAction("ExclusiveSnapMode", actionsMap);
connect(action, &QAction::triggered, agm, &LC_ActionGroupManager::toggleExclusiveSnapMode);
// fixme - sand - rework this !
m_actionSnapMiddleManual = justAddAction("SnapMiddleManual", actionsMap);
connect(m_actionSnapMiddleManual, &QAction::triggered, m_actionHandler, &QG_ActionHandler::slotSnapMiddleManual);
// m_actionSnapMiddleManual = addOwnAction("SnapMiddleManual", actionsMap);
m_actionSnapFree = addOwnAction("SnapFree", actionsMap);
m_actionSnapGrid = addOwnAction("SnapGrid", actionsMap);
m_actionSnapEnd = addOwnAction("SnapEnd", actionsMap);
m_actionSnapOnEntity = addOwnAction("SnapEntity", actionsMap);
m_actionSnapCenter = addOwnAction("SnapCenter", actionsMap);
m_actionSnapMiddle = addOwnAction("SnapMiddle", actionsMap);
m_actionSnapDistance = addOwnAction("SnapDistance", actionsMap);
m_actionSnapIntersection = addOwnAction("SnapIntersection", actionsMap);
addSeparator();
m_actionRestrictHorizontal = addOwnAction("RestrictHorizontal", actionsMap);
m_actionRestrictVertical = addOwnAction("RestrictVertical", actionsMap);
m_actionRestrictOrthogonal = justAddAction("RestrictOrthogonal", actionsMap);
connect(m_actionRestrictOrthogonal, &QAction::triggered, this, &QG_SnapToolBar::slotRestrictOrthogonal);
m_actionRestrictNothing = justAddAction("RestrictNothing", actionsMap);
connect(m_actionRestrictNothing, &QAction::triggered, this, &QG_SnapToolBar::slotRestrictNothing);
// todo - in general, restrict nothing has no practical sense at all - as buttons are toggled, the amount of clicks is the same
// todo - so probably, it's better to keep it hidden for now.
// todo - However, it may be practical, if "restrict to angle" will be implemented
m_actionRestrictNothing->setVisible(false);
addSeparator();
m_actionRelZero = justAddAction("SetRelativeZero", actionsMap);
m_actionLockRelZero = justAddAction("LockRelativeZero", actionsMap);
m_actionLockRelZero->setCheckable(true);
connect(m_actionLockRelZero, &QAction::triggered, m_actionHandler, &QG_ActionHandler::slotLockRelativeZero);
//restore snapMode from saved preferences
setSnaps( RS_SnapMode::fromInt(LC_GET_ONE_INT("Snap", "SnapMode", 0)));
}
void QG_SnapToolBar::slotUnsetSnapMiddleManual(){
m_actionSnapMiddleManual->setChecked(false);
}
void QG_SnapToolBar::saveSnapMode(){
//@write default snap mode from prefrences.
unsigned int snapFlags {RS_SnapMode::toInt( getSnaps())};
LC_SET_ONE("Snap", "SnapMode", QString::number(snapFlags));
// no need to delete child widgets, Qt does it all for us
}
void QG_SnapToolBar::setSnaps ( RS_SnapMode const& s ){
m_actionSnapFree->setChecked(s.snapFree);
m_actionSnapGrid->setChecked(s.snapGrid);
m_actionSnapEnd->setChecked(s.snapEndpoint);
m_actionSnapOnEntity->setChecked(s.snapOnEntity);
m_actionSnapCenter->setChecked(s.snapCenter);
m_actionSnapMiddle->setChecked(s.snapMiddle);
m_actionSnapDistance->setChecked(s.snapDistance);
m_actionSnapIntersection->setChecked(s.snapIntersection);
bool restHorizontal = s.restriction == RS2::RestrictHorizontal;
bool restOrtho = s.restriction == RS2::RestrictOrthogonal;
bool restVertical = s.restriction == RS2::RestrictVertical;
m_actionRestrictHorizontal->setChecked(restHorizontal || restOrtho);
m_actionRestrictVertical->setChecked(restVertical || restOrtho);
m_actionRestrictOrthogonal->setChecked(restOrtho);
m_actionRestrictNothing->setChecked(s.restriction==RS2::RestrictNothing);
}
RS_SnapMode QG_SnapToolBar::getSnaps() const{
RS_SnapMode s;
s.snapFree = m_actionSnapFree->isChecked();
s.snapGrid = m_actionSnapGrid->isChecked();
s.snapEndpoint = m_actionSnapEnd->isChecked();
s.snapOnEntity = m_actionSnapOnEntity->isChecked();
s.snapCenter = m_actionSnapCenter->isChecked();
s.snapMiddle = m_actionSnapMiddle->isChecked();
s.snapDistance = m_actionSnapDistance->isChecked();
s.snapIntersection = m_actionSnapIntersection->isChecked();
// removed Restrict Othogonal button
// todo simplify internal restrict rules
int const rH = (m_actionRestrictHorizontal != nullptr && m_actionRestrictHorizontal->isChecked())? 1:0;
int const rV = (m_actionRestrictVertical != nullptr && m_actionRestrictVertical->isChecked())? 2: 0;
switch (rH + rV) {
case 3:
s.restriction = RS2::RestrictOrthogonal;
break;
case 2:
s.restriction = RS2::RestrictVertical;
break;
case 1:
s.restriction = RS2::RestrictHorizontal;
break;
default:
s.restriction = RS2::RestrictNothing;
}
return s;
}
bool QG_SnapToolBar::lockedRelativeZero() const{
return m_actionLockRelZero->isChecked();
}
void QG_SnapToolBar::setLockedRelativeZero(bool on){
m_actionLockRelZero->setChecked(on);
m_actionLockRelZero->setToolTip(tr("Relative zero position is %1").arg(on ? tr("locked") : tr("unlocked")));
}
void QG_SnapToolBar::setUCSActive(bool on){
ucsMode->setChecked(on);
ucsMode->setToolTip(tr("Coordinate system: %1").arg(on ? tr("User") : tr("World")));
}
/* Slots */
void QG_SnapToolBar::slotRestrictNothing(bool checked){
if (checked) {
m_actionRestrictVertical->setChecked(!checked);
m_actionRestrictHorizontal->setChecked(!checked);
m_actionRestrictOrthogonal->setChecked(!checked);
m_actionRestrictNothing->setChecked(checked);
actionTriggered();
}
}
void QG_SnapToolBar::slotRestrictOrthogonal(bool checked){
m_actionRestrictVertical->setChecked(checked);
m_actionRestrictHorizontal->setChecked(checked);
m_actionRestrictNothing->setChecked(!checked);
actionTriggered();
}
void QG_SnapToolBar::slotEnableRelativeZeroSnaps(const bool enabled){
m_actionRelZero->setEnabled(enabled);
m_actionLockRelZero->setEnabled(enabled);
}
void QG_SnapToolBar::actionTriggered(){
m_actionHandler->setSnaps(getSnaps());
}
LC_SnapOptionsWidgetsHolder *QG_SnapToolBar::getSnapOptionsHolder() {
auto* snapOptionsHolder = new LC_SnapOptionsWidgetsHolder(this);
snapOptionsHolder->setLocatedOnLeft(false);
addWidget(snapOptionsHolder);
return snapOptionsHolder;
}
|