File size: 12,901 Bytes
985c397 | 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2009 Jürgen Riegel <juergen.riegel@web.de> *
* *
* 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 <Base/Console.h>
#include <Gui/Application.h>
#include <Gui/BitmapFactory.h>
#include <Gui/Document.h>
#include <Gui/ViewProvider.h>
#include <Gui/WaitCursor.h>
#include "ui_TaskAppearance.h"
#include "TaskAppearance.h"
using namespace Gui::TaskView;
namespace sp = std::placeholders;
/* TRANSLATOR Gui::TaskView::TaskAppearance */
TaskAppearance::TaskAppearance(QWidget* parent)
: TaskBox(Gui::BitmapFactory().pixmap("document-new"), tr("Appearance"), true, parent)
{
// we need a separate container widget to add all controls to
proxy = new QWidget(this);
ui = new Ui_TaskAppearance();
ui->setupUi(proxy);
setupConnections();
ui->textLabel1_3->hide();
ui->changePlot->hide();
QMetaObject::connectSlotsByName(this);
this->groupLayout()->addWidget(proxy);
Gui::Selection().Attach(this);
// NOLINTBEGIN
this->connectChangedObject = Gui::Application::Instance->signalChangedObject.connect(
std::bind(&TaskAppearance::slotChangedObject, this, sp::_1, sp::_2)
);
// NOLINTEND
}
TaskAppearance::~TaskAppearance()
{
delete ui;
this->connectChangedObject.disconnect();
Gui::Selection().Detach(this);
}
void TaskAppearance::setupConnections()
{
// clang-format off
connect(ui->changeMode, &QComboBox::textActivated,
this, &TaskAppearance::onChangeModeActivated);
connect(ui->changePlot, &QComboBox::textActivated,
this, &TaskAppearance::onChangePlotActivated);
connect(ui->spinTransparency, qOverload<int>(&QSpinBox::valueChanged),
this, &TaskAppearance::onTransparencyValueChanged);
connect(ui->spinPointSize, qOverload<int>(&QSpinBox::valueChanged),
this, &TaskAppearance::onPointSizeValueChanged);
connect(ui->spinLineWidth, qOverload<int>(&QSpinBox::valueChanged),
this, &TaskAppearance::onLineWidthValueChanged);
// clang-format on
}
void TaskAppearance::changeEvent(QEvent* e)
{
TaskBox::changeEvent(e);
if (e->type() == QEvent::LanguageChange) {
ui->retranslateUi(proxy);
}
}
/// @cond DOXERR
void TaskAppearance::OnChange(
Gui::SelectionSingleton::SubjectType& rCaller,
Gui::SelectionSingleton::MessageType Reason
)
{
Q_UNUSED(rCaller);
if (Reason.Type == SelectionChanges::AddSelection || Reason.Type == SelectionChanges::RmvSelection
|| Reason.Type == SelectionChanges::SetSelection
|| Reason.Type == SelectionChanges::ClrSelection) {
std::vector<Gui::ViewProvider*> views = getSelection();
setDisplayModes(views);
setPointSize(views);
setLineWidth(views);
setTransparency(views);
}
}
/// @endcond
void TaskAppearance::slotChangedObject(const Gui::ViewProvider& obj, const App::Property& prop)
{
// This method gets called if a property of any view provider is changed.
// We pick out all the properties for which we need to update this dialog.
const std::vector<Gui::ViewProvider*> Provider = getSelection();
auto vp = std::find_if(Provider.begin(), Provider.end(), [&obj](Gui::ViewProvider* v) {
return v == &obj;
});
if (vp != Provider.end()) {
std::string prop_name = obj.getPropertyName(&prop);
if (prop.isDerivedFrom<App::PropertyInteger>()) {
long value = static_cast<const App::PropertyInteger&>(prop).getValue();
if (prop_name == "Transparency") {
bool blocked = ui->spinTransparency->blockSignals(true);
ui->spinTransparency->setValue(value);
ui->spinTransparency->blockSignals(blocked);
blocked = ui->horizontalSlider->blockSignals(true);
ui->horizontalSlider->setValue(value);
ui->horizontalSlider->blockSignals(blocked);
}
}
else if (prop.isDerivedFrom<App::PropertyFloat>()) {
float value = static_cast<const App::PropertyFloat&>(prop).getValue();
if (prop_name == "PointSize") {
bool blocked = ui->spinPointSize->blockSignals(true);
ui->spinPointSize->setValue((int)value);
ui->spinPointSize->blockSignals(blocked);
}
else if (prop_name == "LineWidth") {
bool blocked = ui->spinLineWidth->blockSignals(true);
ui->spinLineWidth->setValue((int)value);
ui->spinLineWidth->blockSignals(blocked);
}
}
}
}
/**
* Sets the 'Display' property of all selected view providers.
*/
void TaskAppearance::onChangeModeActivated(const QString& s)
{
Gui::WaitCursor wc;
std::vector<Gui::ViewProvider*> Provider = getSelection();
for (const auto& It : Provider) {
App::Property* prop = It->getPropertyByName("DisplayMode");
if (prop && prop->is<App::PropertyEnumeration>()) {
auto Display = static_cast<App::PropertyEnumeration*>(prop);
Display->setValue((const char*)s.toLatin1());
}
}
}
void TaskAppearance::onChangePlotActivated(const QString& s)
{
Base::Console().log("Plot = %s\n", (const char*)s.toLatin1());
}
/**
* Sets the 'Transparency' property of all selected view providers.
*/
void TaskAppearance::onTransparencyValueChanged(int transparency)
{
std::vector<Gui::ViewProvider*> Provider = getSelection();
for (const auto& It : Provider) {
App::Property* prop = It->getPropertyByName("Transparency");
if (prop && prop->isDerivedFrom<App::PropertyInteger>()) {
auto Transparency = static_cast<App::PropertyInteger*>(prop);
Transparency->setValue(transparency);
}
}
}
/**
* Sets the 'PointSize' property of all selected view providers.
*/
void TaskAppearance::onPointSizeValueChanged(int pointsize)
{
std::vector<Gui::ViewProvider*> Provider = getSelection();
for (const auto& It : Provider) {
App::Property* prop = It->getPropertyByName("PointSize");
if (prop && prop->isDerivedFrom<App::PropertyFloat>()) {
auto PointSize = static_cast<App::PropertyFloat*>(prop);
PointSize->setValue(static_cast<float>(pointsize));
}
}
}
/**
* Sets the 'LineWidth' property of all selected view providers.
*/
void TaskAppearance::onLineWidthValueChanged(int linewidth)
{
std::vector<Gui::ViewProvider*> Provider = getSelection();
for (const auto& It : Provider) {
App::Property* prop = It->getPropertyByName("LineWidth");
if (prop && prop->isDerivedFrom<App::PropertyFloat>()) {
auto LineWidth = static_cast<App::PropertyFloat*>(prop);
LineWidth->setValue(static_cast<float>(linewidth));
}
}
}
void TaskAppearance::setDisplayModes(const std::vector<Gui::ViewProvider*>& views)
{
QStringList commonModes, modes;
for (auto it = views.begin(); it != views.end(); ++it) {
App::Property* prop = (*it)->getPropertyByName("DisplayMode");
if (prop && prop->is<App::PropertyEnumeration>()) {
auto display = static_cast<App::PropertyEnumeration*>(prop);
if (!display->hasEnums()) {
return;
}
std::vector<std::string> value = display->getEnumVector();
if (it == views.begin()) {
for (const auto& jt : value) {
commonModes << QLatin1String(jt.c_str());
}
}
else {
for (const auto& jt : value) {
if (commonModes.contains(QLatin1String(jt.c_str()))) {
modes << QLatin1String(jt.c_str());
}
}
commonModes = modes;
modes.clear();
}
}
}
ui->changeMode->clear();
ui->changeMode->addItems(commonModes);
ui->changeMode->setDisabled(commonModes.isEmpty());
// find the display mode to activate
for (const auto view : views) {
App::Property* prop = view->getPropertyByName("DisplayMode");
if (prop && prop->is<App::PropertyEnumeration>()) {
auto display = static_cast<App::PropertyEnumeration*>(prop);
QString activeMode = QString::fromLatin1(display->getValueAsString());
int index = ui->changeMode->findText(activeMode);
if (index != -1) {
ui->changeMode->setCurrentIndex(index);
break;
}
}
}
}
void TaskAppearance::setPointSize(const std::vector<Gui::ViewProvider*>& views)
{
bool pointSize = false;
for (const auto& view : views) {
App::Property* prop = view->getPropertyByName("PointSize");
if (prop && prop->isDerivedFrom<App::PropertyFloat>()) {
bool blocked = ui->spinPointSize->blockSignals(true);
ui->spinPointSize->setValue((int)static_cast<App::PropertyFloat*>(prop)->getValue());
ui->spinPointSize->blockSignals(blocked);
pointSize = true;
break;
}
}
ui->spinPointSize->setEnabled(pointSize);
}
void TaskAppearance::setLineWidth(const std::vector<Gui::ViewProvider*>& views)
{
bool lineWidth = false;
for (const auto& view : views) {
App::Property* prop = view->getPropertyByName("LineWidth");
if (prop && prop->isDerivedFrom<App::PropertyFloat>()) {
bool blocked = ui->spinLineWidth->blockSignals(true);
ui->spinLineWidth->setValue((int)static_cast<App::PropertyFloat*>(prop)->getValue());
ui->spinLineWidth->blockSignals(blocked);
lineWidth = true;
break;
}
}
ui->spinLineWidth->setEnabled(lineWidth);
}
void TaskAppearance::setTransparency(const std::vector<Gui::ViewProvider*>& views)
{
bool transparency = false;
for (const auto& view : views) {
App::Property* prop = view->getPropertyByName("Transparency");
if (prop && prop->isDerivedFrom<App::PropertyInteger>()) {
bool blocked = ui->spinTransparency->blockSignals(true);
ui->spinTransparency->setValue(static_cast<App::PropertyInteger*>(prop)->getValue());
ui->spinTransparency->blockSignals(blocked);
transparency = true;
break;
}
}
ui->spinTransparency->setEnabled(transparency);
ui->horizontalSlider->setEnabled(transparency);
}
std::vector<Gui::ViewProvider*> TaskAppearance::getSelection() const
{
std::vector<Gui::ViewProvider*> views;
// get the complete selection
std::vector<SelectionSingleton::SelObj> sel = Selection().getCompleteSelection();
for (const auto& it : sel) {
Gui::ViewProvider* view = Application::Instance->getDocument(it.pDoc)->getViewProvider(
it.pObject
);
if (view) {
views.push_back(view);
}
}
return views;
}
#include "moc_TaskAppearance.cpp"
|